1
0

reader.py 1.5 KB

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