writer.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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_architecture()
  13. gguf_writer.add_block_count(12)
  14. gguf_writer.add_uint32("answer", 42) # Write a 32-bit integer
  15. gguf_writer.add_float32("answer_in_float", 42.0) # Write a 32-bit float
  16. gguf_writer.add_custom_alignment(64)
  17. tensor1 = np.ones((32,), dtype=np.float32) * 100.0
  18. tensor2 = np.ones((64,), dtype=np.float32) * 101.0
  19. tensor3 = np.ones((96,), dtype=np.float32) * 102.0
  20. gguf_writer.add_tensor("tensor1", tensor1)
  21. gguf_writer.add_tensor("tensor2", tensor2)
  22. gguf_writer.add_tensor("tensor3", tensor3)
  23. gguf_writer.write_header_to_file()
  24. gguf_writer.write_kv_data_to_file()
  25. gguf_writer.write_tensors_to_file()
  26. gguf_writer.close()
  27. if __name__ == '__main__':
  28. writer_example()