Skip to content

Commit 6c00a06

Browse files
authored
gguf : add python reader example (#5216)
* Update CMakeLists.txt * Create reader.py * Update reader.py * Update reader.py another whitespace :| * Update reader.py * lintlintlint
1 parent ea9c8e1 commit 6c00a06

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ else()
3838
add_subdirectory(speculative)
3939
add_subdirectory(lookahead)
4040
add_subdirectory(lookup)
41+
add_subdirectory(gguf)
4142
add_subdirectory(train-text-from-scratch)
4243
add_subdirectory(imatrix)
4344
if (LLAMA_BUILD_SERVER)

gguf-py/examples/reader.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
from pathlib import Path
4+
from gguf.gguf_reader import GGUFReader
5+
6+
7+
sys.path.insert(0, str(Path(__file__).parent.parent))
8+
9+
10+
def read_gguf_file(gguf_file_path):
11+
"""
12+
Reads and prints key-value pairs and tensor information from a GGUF file in an improved format.
13+
14+
Parameters:
15+
- gguf_file_path: Path to the GGUF file.
16+
"""
17+
18+
reader = GGUFReader(gguf_file_path)
19+
20+
# List all key-value pairs in a columnized format
21+
print("Key-Value Pairs:")
22+
max_key_length = max(len(key) for key in reader.fields.keys())
23+
for key, field in reader.fields.items():
24+
value = field.parts[field.data[0]]
25+
print(f"{key:{max_key_length}} : {value}")
26+
print("----")
27+
28+
# List all tensors
29+
print("Tensors:")
30+
tensor_info_format = "{:<30} | Shape: {:<15} | Size: {:<12} | Quantization: {}"
31+
print(tensor_info_format.format("Tensor Name", "Shape", "Size", "Quantization"))
32+
print("-" * 80)
33+
for tensor in reader.tensors:
34+
shape_str = "x".join(map(str, tensor.shape))
35+
size_str = str(tensor.n_elements)
36+
quantization_str = tensor.tensor_type.name
37+
print(tensor_info_format.format(tensor.name, shape_str, size_str, quantization_str))
38+
39+
40+
if __name__ == '__main__':
41+
if len(sys.argv) < 2:
42+
print("Usage: reader.py <path_to_gguf_file>")
43+
sys.exit(1)
44+
gguf_file_path = sys.argv[1]
45+
read_gguf_file(gguf_file_path)

0 commit comments

Comments
 (0)