|
| 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