forked from ggerganov/llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new GGUFValueType.OBJ virtual type
The content of the OBJ type is actually a list of all key names of the object. This change includes several improvements and additions to the codebase: * GGUFWriter: * Added `def add_kv(self, key: str, val: Any) -> None` method: Automatically determines the appropriate value type based on val. * Added `def add_dict(self, key: str, val: dict) -> None` method: add object(dict) key-value * constants: * Revised `GGUFValueType.get_type(val)`: Added support for numpy's integers and floating point numbers, and appropriately selected the number of digits according to the size of the integer. * gguf_reader * Added `ReaderField.get()` method: get the value of this ReaderField * Unit tests have been added to cover these changes. Related Issues: ggerganov#4868, ggerganov#2872
- Loading branch information
Showing
4 changed files
with
149 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,52 @@ | ||
import gguf # noqa: F401 | ||
import sys | ||
from pathlib import Path | ||
import numpy as np | ||
import unittest | ||
|
||
# TODO: add tests | ||
# Necessary to load the local gguf package | ||
sys.path.insert(0, str(Path(__file__).parent.parent)) | ||
|
||
from gguf import GGUFWriter, GGUFReader | ||
|
||
def test_write_gguf() -> None: | ||
pass | ||
class TestGGUFReaderWriter(unittest.TestCase): | ||
|
||
def test_rw(self) -> None: | ||
# Example usage with a file | ||
gguf_writer = GGUFWriter("test_writer.gguf", "llama") | ||
|
||
# gguf_writer.add_architecture() | ||
gguf_writer.add_block_count(12) | ||
gguf_writer.add_uint32("answer", 42) # Write a 32-bit integer | ||
gguf_writer.add_float32("answer_in_float", 42.0) # Write a 32-bit float | ||
gguf_writer.add_dict("dict1", {"key1": 2, "key2": "hi", "obj": {"k": 1}}) | ||
gguf_writer.add_custom_alignment(64) | ||
|
||
tensor1 = np.ones((32,), dtype=np.float32) * 100.0 | ||
tensor2 = np.ones((64,), dtype=np.float32) * 101.0 | ||
tensor3 = np.ones((96,), dtype=np.float32) * 102.0 | ||
|
||
gguf_writer.add_tensor("tensor1", tensor1) | ||
gguf_writer.add_tensor("tensor2", tensor2) | ||
gguf_writer.add_tensor("tensor3", tensor3) | ||
|
||
gguf_writer.write_header_to_file() | ||
gguf_writer.write_kv_data_to_file() | ||
gguf_writer.write_tensors_to_file() | ||
|
||
gguf_writer.close() | ||
|
||
gguf_reader = GGUFReader("test_writer.gguf") | ||
self.assertEqual(gguf_reader.alignment, 64) | ||
v = gguf_reader.get_field("dict1") | ||
self.assertIsNotNone(v) | ||
self.assertListEqual(v.get(), ['key1', 'key2', 'obj']) | ||
v = gguf_reader.get_field("dict1.key1") | ||
self.assertEqual(v.get(), 2) | ||
v = gguf_reader.get_field("dict1.key2") | ||
self.assertEqual(v.get(), "hi") | ||
v = gguf_reader.get_field("dict1.obj") | ||
self.assertListEqual(v.get(), ['k']) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |