1
0

writer.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python3
  2. import sys
  3. from pathlib import Path
  4. import numpy as np
  5. # Necessary to load the local gguf package
  6. sys.path.insert(0, str(Path(__file__).parent.parent))
  7. from gguf import GGUFWriter # noqa: E402
  8. # Example usage:
  9. def writer_example() -> None:
  10. # Example usage with a file
  11. gguf_writer = GGUFWriter("example.gguf", "llama")
  12. gguf_writer.add_block_count(12)
  13. gguf_writer.add_uint32("answer", 42) # Write a 32-bit integer
  14. gguf_writer.add_float32("answer_in_float", 42.0) # Write a 32-bit float
  15. gguf_writer.add_custom_alignment(64)
  16. tensor1 = np.ones((32,), dtype=np.float32) * 100.0
  17. tensor2 = np.ones((64,), dtype=np.float32) * 101.0
  18. tensor3 = np.ones((96,), dtype=np.float32) * 102.0
  19. gguf_writer.add_tensor("tensor1", tensor1)
  20. gguf_writer.add_tensor("tensor2", tensor2)
  21. gguf_writer.add_tensor("tensor3", tensor3)
  22. gguf_writer.write_header_to_file()
  23. gguf_writer.write_kv_data_to_file()
  24. gguf_writer.write_tensors_to_file()
  25. gguf_writer.close()
  26. if __name__ == '__main__':
  27. writer_example()