Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor metadata keying and fix license type #8

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 12 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,39 +444,19 @@ export const parseRawMetadata = async (
const metadata: Record<string, any> = {}

const setKey = (keyName: string, value: MetadataValue) => {
// this is a bad way to write this and should be clean it up
// but since there are never more than 3 layers currently, it's fine for now
const [key1, key2, key3, key4, key5] = keyName.split('.')

if (!key2) {
metadata[key1] = value
return
}
if (!key3) {
if (!metadata[key1]) metadata[key1] = {}
metadata[key1][key2] = value
return
}
if (!key4) {
if (!metadata[key1]) metadata[key1] = {}
if (!metadata[key1][key2]) metadata[key1][key2] = {}
metadata[key1][key2][key3] = value
return
}
if (!key5) {
if (!metadata[key1]) metadata[key1] = {}
if (!metadata[key1][key2]) metadata[key1][key2] = {}
if (!metadata[key1][key2][key3]) metadata[key1][key2][key3] = {}
metadata[key1][key2][key3][key4] = value
return
}
if (!metadata[key1]) metadata[key1] = {}
if (!metadata[key1][key2]) metadata[key1][key2] = {}
if (!metadata[key1][key2][key3]) metadata[key1][key2][key3] = {}
if (!metadata[key1][key2][key3][key4]) {
metadata[key1][key2][key3][key4] = {}
const keys = keyName.split('.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice cleanup!

let obj = metadata
for (const [index, key] of keys.entries()) {
if (!index) continue
const prevKey = keys[index - 1]
if (!obj[prevKey]) obj[prevKey] = {}
if (index === keys.length - 1) {
if (typeof obj[prevKey] === 'object') {
obj[prevKey][key] = value
}
}
obj = obj[prevKey]
}
metadata[key1][key2][key3][key4][key5] = value
}

for (let i = 0; i < numKv.value; ++i) {
Expand Down
55 changes: 55 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,59 @@ describe('gguf', () => {
},
1000 * 30,
)

test(
'DarkForest-20B-v3.0.IQ1_M.gguf',
async () => {
const file = await fetchPartialFile(
'https://huggingface.co/backyardai/DarkForest-20B-v3.0-GGUF/resolve/main/DarkForest-20B-v3.0.IQ1_M.gguf',
0,
// 10mb
1024 * 1024 * 10,
)

const fileName = path.join(
__dirname,
'models',
'DarkForest-20B-v3.0.IQ1_M.gguf',
)

await writeFile(fileName, Buffer.from(file))

const { error, metadata } = await gguf(fileName)

expect(error).toBe(undefined)
expect(metadata).not.toBe(undefined)
if (!metadata) return // for types

expect(metadata.general.architecture).toBe('llama')
if (!isLlamaMetadata(metadata)) return // for types

expect(metadata.llama).toBeTruthy()

expect(metadata).toEqual({
general: {
architecture: 'llama',
license: 'other',
name: 'F:\\merger\\mergekit\\V3_ultraperecision\\V3_u_4_scaling',
quantization_version: 2,
},
llama: {
attention: {
head_count: 40,
head_count_kv: 40,
layer_norm_rms_epsilon: 0.000009999999747378752,
},
context_length: 4096,
embedding_length: 5120,
feed_forward_length: 13824,
rope: {
dimension_count: 128,
freq_base: 10000,
},
},
})
},
1000 * 30,
)
})
Loading