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

gguf: add CLI #1221

Merged
merged 4 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 36 additions & 0 deletions packages/gguf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,42 @@ In case you want to use your own GGUF metadata structure, you can disable strict
const { metadata, tensorInfos }: GGUFParseOutput<{ strict: false }> = await gguf(URL_LLAMA);
```

## Command line interface

This package provides a CLI equivalent to [`gguf_dump.py`](https://github.com/ggml-org/llama.cpp/blob/7a2c913e66353362d7f28d612fd3c9d51a831eda/gguf-py/gguf/scripts/gguf_dump.py) script. You can dump GGUF metadata and list of tensors using this command:

```bash
npx @huggingface/gguf my_model.gguf
```

Example for the output:

```
* Dumping 36 key/value pair(s)
Idx | Count | Value
----|--------|----------------------------------------------------------------------------------
1 | 1 | version = 3
2 | 1 | tensor_count = 292
3 | 1 | kv_count = 33
4 | 1 | general.architecture = "llama"
5 | 1 | general.type = "model"
6 | 1 | general.name = "Meta Llama 3.1 8B Instruct"
7 | 1 | general.finetune = "Instruct"
8 | 1 | general.basename = "Meta-Llama-3.1"

[truncated]

* Dumping 292 tensor(s)
Idx | Num Elements | Shape | Data Type | Name
----|--------------|--------------------------------|-----------|--------------------------
1 | 64 | 64, 1, 1, 1 | F32 | rope_freqs.weight
2 | 525336576 | 4096, 128256, 1, 1 | Q4_K | token_embd.weight
3 | 4096 | 4096, 1, 1, 1 | F32 | blk.0.attn_norm.weight
4 | 58720256 | 14336, 4096, 1, 1 | Q6_K | blk.0.ffn_down.weight

[truncated]
```

## Hugging Face Hub

The Hub supports all file formats and has built-in features for GGUF format.
Expand Down
6 changes: 5 additions & 1 deletion packages/gguf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"bin": {
"gguf-dump": "./dist/cli.js"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -18,6 +21,7 @@
}
},
"browser": {
"./src/cli.ts": false,
"./src/utils/FileBlob.ts": false,
"./dist/index.js": "./dist/browser/index.js",
"./dist/index.mjs": "./dist/browser/index.mjs"
Expand All @@ -32,7 +36,7 @@
"format": "prettier --write .",
"format:check": "prettier --check .",
"prepublishOnly": "pnpm run build",
"build": "tsup src/index.ts --format cjs,esm --clean && tsc --emitDeclarationOnly --declaration",
"build": "tsup src/index.ts src/cli.ts --format cjs,esm --clean && tsc --emitDeclarationOnly --declaration",
"build:llm": "tsx scripts/generate-llm.ts && pnpm run format",
"test": "vitest run",
"check": "tsc"
Expand Down
102 changes: 102 additions & 0 deletions packages/gguf/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env node

import { GGMLQuantizationType, gguf } from ".";

interface PrintColumnHeader {
name: string;
maxWidth?: number;
alignRight?: boolean;
}

const mapDtypeToName = Object.fromEntries(Object.entries(GGMLQuantizationType).map(([name, value]) => [value, name]));

async function main() {
const ggufPath = process.argv[2];
const { metadata, tensorInfos } = await gguf(ggufPath, {
allowLocalFile: true,
});

// TODO: print info about endianess
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we still need this todo ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, would be nice if we can make it outputs the same as gguf-py script

console.log(`* Dumping ${Object.keys(metadata).length} key/value pair(s)`);
printTable(
[
{ name: "Idx", alignRight: true },
// { name: 'Type' }, // TODO: support this
{ name: "Count", alignRight: true },
{ name: "Value" },
],
Object.entries(metadata).map(([key, value], i) => {
const MAX_LEN = 50;
let strVal = "";
let count = 1;
if (Array.isArray(value)) {
strVal = JSON.stringify(value);
count = value.length;
} else if (value instanceof String || typeof value === "string") {
strVal = JSON.stringify(value);
} else {
strVal = value.toString();
}
strVal = strVal.length > MAX_LEN ? strVal.slice(0, MAX_LEN) + "..." : strVal;
return [(i + 1).toString(), count.toString(), `${key} = ${strVal}`];
})
);

console.log();
console.log(`* Dumping ${tensorInfos.length} tensor(s)`);
printTable(
[
{ name: "Idx", alignRight: true },
{ name: "Num Elements", alignRight: true },
{ name: "Shape" },
{ name: "Data Type" },
{ name: "Name" },
],
tensorInfos.map((tensorInfo, i) => {
const shape = [1n, 1n, 1n, 1n];
tensorInfo.shape.forEach((dim, i) => {
shape[i] = dim;
});
return [
(i + 1).toString(),
shape.reduce((acc, n) => acc * n, 1n).toString(),
shape.map((n) => n.toString().padStart(6)).join(", "),
mapDtypeToName[tensorInfo.dtype],
tensorInfo.name,
];
})
);
}

function printTable(header: PrintColumnHeader[], rows: string[][], leftPad = 2) {
const leftPadStr = " ".repeat(leftPad);

// Calculate column widths
const columnWidths = header.map((h, i) => {
const maxContentWidth = Math.max(h.name.length, ...rows.map((row) => (row[i] || "").length));
return h.maxWidth ? Math.min(maxContentWidth, h.maxWidth) : maxContentWidth;
});

// Print header
const headerLine = header
.map((h, i) => {
return h.name.padEnd(columnWidths[i]);
})
.join(" | ");
console.log(leftPadStr + headerLine);

// Print separator
console.log(leftPadStr + columnWidths.map((w) => "-".repeat(w)).join("-|-"));

// Print rows
for (const row of rows) {
const line = header
.map((h, i) => {
return h.alignRight ? (row[i] || "").padStart(columnWidths[i]) : (row[i] || "").padEnd(columnWidths[i]);
})
.join(" | ");
console.log(leftPadStr + line);
}
}

main();