|
1 | 1 | import { beforeAll, describe, expect, it } from "vitest";
|
2 |
| -import type { GGUFParseOutput } from "./gguf"; |
| 2 | +import type { GGUFParseOutput, MetadataValue } from "./gguf"; |
3 | 3 | import {
|
4 | 4 | GGMLFileQuantizationType,
|
5 | 5 | GGMLQuantizationType,
|
| 6 | + GGUFValueType, |
6 | 7 | gguf,
|
7 | 8 | ggufAllShards,
|
8 | 9 | parseGgufShardFilename,
|
@@ -325,4 +326,143 @@ describe("gguf", () => {
|
325 | 326 | nearestQuant = findNearestQuantType(GGMLFileQuantizationType.F16, visionQuants);
|
326 | 327 | expect(nearestQuant).toEqual(GGMLFileQuantizationType.F16);
|
327 | 328 | });
|
| 329 | + |
| 330 | + it("should not return typedMetadata by default", async () => { |
| 331 | + const result = await gguf(URL_LLAMA); |
| 332 | + expect(result).not.toHaveProperty("typedMetadata"); |
| 333 | + expect(result).toHaveProperty("metadata"); |
| 334 | + expect(result).toHaveProperty("tensorInfos"); |
| 335 | + expect(result).toHaveProperty("tensorDataOffset"); |
| 336 | + }); |
| 337 | + |
| 338 | + it("should return typedMetadata when requested", async () => { |
| 339 | + const { metadata, typedMetadata, tensorInfos } = await gguf(URL_LLAMA, { typedMetadata: true }); |
| 340 | + |
| 341 | + // Should have both metadata and typedMetadata |
| 342 | + expect(metadata).toBeDefined(); |
| 343 | + expect(typedMetadata).toBeDefined(); |
| 344 | + expect(tensorInfos).toBeDefined(); |
| 345 | + |
| 346 | + // Basic structure checks |
| 347 | + expect(typedMetadata.version).toEqual({ |
| 348 | + value: 2, |
| 349 | + type: GGUFValueType.UINT32, |
| 350 | + }); |
| 351 | + expect(typedMetadata.tensor_count).toEqual({ |
| 352 | + value: 291n, |
| 353 | + type: GGUFValueType.UINT64, |
| 354 | + }); |
| 355 | + expect(typedMetadata.kv_count).toEqual({ |
| 356 | + value: 19n, |
| 357 | + type: GGUFValueType.UINT64, |
| 358 | + }); |
| 359 | + |
| 360 | + // Check string metadata |
| 361 | + expect(typedMetadata["general.architecture"]).toEqual({ |
| 362 | + value: "llama", |
| 363 | + type: GGUFValueType.STRING, |
| 364 | + }); |
| 365 | + expect(typedMetadata["general.name"]).toEqual({ |
| 366 | + value: "LLaMA v2", |
| 367 | + type: GGUFValueType.STRING, |
| 368 | + }); |
| 369 | + |
| 370 | + // Check numeric metadata |
| 371 | + expect(typedMetadata["general.file_type"]).toEqual({ |
| 372 | + value: GGMLFileQuantizationType.Q2_K, |
| 373 | + type: GGUFValueType.UINT32, |
| 374 | + }); |
| 375 | + expect(typedMetadata["llama.attention.head_count"]).toEqual({ |
| 376 | + value: 32, |
| 377 | + type: GGUFValueType.UINT32, |
| 378 | + }); |
| 379 | + |
| 380 | + // Check float metadata |
| 381 | + expect(typedMetadata["llama.attention.layer_norm_rms_epsilon"]).toEqual({ |
| 382 | + value: 9.999999974752427e-7, |
| 383 | + type: GGUFValueType.FLOAT32, |
| 384 | + }); |
| 385 | + }); |
| 386 | + |
| 387 | + it("should return typedMetadata with parameter count", async () => { |
| 388 | + const { metadata, typedMetadata, tensorInfos, parameterCount } = await gguf(URL_LLAMA, { |
| 389 | + typedMetadata: true, |
| 390 | + computeParametersCount: true, |
| 391 | + }); |
| 392 | + |
| 393 | + expect(metadata).toBeDefined(); |
| 394 | + expect(typedMetadata).toBeDefined(); |
| 395 | + expect(tensorInfos).toBeDefined(); |
| 396 | + expect(parameterCount).toEqual(6_738_415_616); |
| 397 | + |
| 398 | + // Verify typedMetadata structure is still correct |
| 399 | + expect(typedMetadata.version).toEqual({ |
| 400 | + value: 2, |
| 401 | + type: GGUFValueType.UINT32, |
| 402 | + }); |
| 403 | + expect(typedMetadata["general.architecture"]).toEqual({ |
| 404 | + value: "llama", |
| 405 | + type: GGUFValueType.STRING, |
| 406 | + }); |
| 407 | + }); |
| 408 | + |
| 409 | + it("should handle typedMetadata for V1 files", async () => { |
| 410 | + const { typedMetadata } = await gguf(URL_V1, { typedMetadata: true }); |
| 411 | + |
| 412 | + // V1 files use UINT32 for counts instead of UINT64 |
| 413 | + expect(typedMetadata.version).toEqual({ |
| 414 | + value: 1, |
| 415 | + type: GGUFValueType.UINT32, |
| 416 | + }); |
| 417 | + expect(typedMetadata.tensor_count).toEqual({ |
| 418 | + value: 48n, |
| 419 | + type: GGUFValueType.UINT32, |
| 420 | + }); |
| 421 | + expect(typedMetadata.kv_count).toEqual({ |
| 422 | + value: 18n, |
| 423 | + type: GGUFValueType.UINT32, |
| 424 | + }); |
| 425 | + |
| 426 | + // Check other fields are properly typed |
| 427 | + expect(typedMetadata["general.architecture"]).toEqual({ |
| 428 | + value: "llama", |
| 429 | + type: GGUFValueType.STRING, |
| 430 | + }); |
| 431 | + expect(typedMetadata["llama.attention.head_count"]).toEqual({ |
| 432 | + value: 8, |
| 433 | + type: GGUFValueType.UINT32, |
| 434 | + }); |
| 435 | + }); |
| 436 | + |
| 437 | + it("should handle array metadata types in typedMetadata", async () => { |
| 438 | + const { typedMetadata } = await gguf(URL_LLAMA, { typedMetadata: true }); |
| 439 | + |
| 440 | + // Check if tokens array is properly handled |
| 441 | + if (typedMetadata["tokenizer.ggml.tokens"]) { |
| 442 | + expect(typedMetadata["tokenizer.ggml.tokens"].type).toEqual(GGUFValueType.ARRAY); |
| 443 | + expect(Array.isArray(typedMetadata["tokenizer.ggml.tokens"].value)).toBe(true); |
| 444 | + } |
| 445 | + |
| 446 | + // Check if scores array is properly handled |
| 447 | + if (typedMetadata["tokenizer.ggml.scores"]) { |
| 448 | + expect(typedMetadata["tokenizer.ggml.scores"].type).toEqual(GGUFValueType.ARRAY); |
| 449 | + expect(Array.isArray(typedMetadata["tokenizer.ggml.scores"].value)).toBe(true); |
| 450 | + } |
| 451 | + }); |
| 452 | + |
| 453 | + it("should maintain consistency between metadata and typedMetadata values", async () => { |
| 454 | + const { metadata, typedMetadata } = await gguf(URL_LLAMA, { typedMetadata: true }); |
| 455 | + |
| 456 | + // All keys should be present in both |
| 457 | + const metadataKeys = Object.keys(metadata); |
| 458 | + const typedMetadataKeys = Object.keys(typedMetadata); |
| 459 | + |
| 460 | + expect(metadataKeys.sort()).toEqual(typedMetadataKeys.sort()); |
| 461 | + |
| 462 | + // Values should match for all keys |
| 463 | + const metadataAsRecord = metadata as Record<string, MetadataValue>; |
| 464 | + for (const key of metadataKeys) { |
| 465 | + expect(typedMetadata[key].value).toEqual(metadataAsRecord[key]); |
| 466 | + } |
| 467 | + }); |
328 | 468 | });
|
0 commit comments