2024-02-13 17:56:38 +00:00
|
|
|
#!/usr/bin/env python3
|
2024-05-03 19:36:41 +00:00
|
|
|
import logging
|
2024-02-13 17:56:38 +00:00
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
from gguf.gguf_reader import GGUFReader
|
|
|
|
|
2024-05-03 19:36:41 +00:00
|
|
|
logger = logging.getLogger("reader")
|
2024-02-13 17:56:38 +00:00
|
|
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
|
|
|
|
|
|
|
def read_gguf_file(gguf_file_path):
|
|
|
|
"""
|
|
|
|
Reads and prints key-value pairs and tensor information from a GGUF file in an improved format.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
- gguf_file_path: Path to the GGUF file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
reader = GGUFReader(gguf_file_path)
|
|
|
|
|
|
|
|
# List all key-value pairs in a columnized format
|
2024-05-03 19:36:41 +00:00
|
|
|
print("Key-Value Pairs:") # noqa: NP100
|
2024-02-13 17:56:38 +00:00
|
|
|
max_key_length = max(len(key) for key in reader.fields.keys())
|
|
|
|
for key, field in reader.fields.items():
|
|
|
|
value = field.parts[field.data[0]]
|
2024-05-03 19:36:41 +00:00
|
|
|
print(f"{key:{max_key_length}} : {value}") # noqa: NP100
|
|
|
|
print("----") # noqa: NP100
|
2024-02-13 17:56:38 +00:00
|
|
|
|
|
|
|
# List all tensors
|
2024-05-03 19:36:41 +00:00
|
|
|
print("Tensors:") # noqa: NP100
|
2024-02-13 17:56:38 +00:00
|
|
|
tensor_info_format = "{:<30} | Shape: {:<15} | Size: {:<12} | Quantization: {}"
|
2024-05-03 19:36:41 +00:00
|
|
|
print(tensor_info_format.format("Tensor Name", "Shape", "Size", "Quantization")) # noqa: NP100
|
|
|
|
print("-" * 80) # noqa: NP100
|
2024-02-13 17:56:38 +00:00
|
|
|
for tensor in reader.tensors:
|
|
|
|
shape_str = "x".join(map(str, tensor.shape))
|
|
|
|
size_str = str(tensor.n_elements)
|
|
|
|
quantization_str = tensor.tensor_type.name
|
2024-05-03 19:36:41 +00:00
|
|
|
print(tensor_info_format.format(tensor.name, shape_str, size_str, quantization_str)) # noqa: NP100
|
2024-02-13 17:56:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) < 2:
|
2024-05-03 19:36:41 +00:00
|
|
|
logger.info("Usage: reader.py <path_to_gguf_file>")
|
2024-02-13 17:56:38 +00:00
|
|
|
sys.exit(1)
|
|
|
|
gguf_file_path = sys.argv[1]
|
|
|
|
read_gguf_file(gguf_file_path)
|