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