reader.py 1.4 KB

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