1
0

reader.py 1.6 KB

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