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