]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
py: add_array() will not add to kv store if value is an empty array (#8774)
authorBrian <redacted>
Tue, 30 Jul 2024 14:57:03 +0000 (00:57 +1000)
committerGitHub <redacted>
Tue, 30 Jul 2024 14:57:03 +0000 (00:57 +1000)
* gguf_writer.py: add_array() should not add to kv store if empty

* Apply suggestions from code review

I was wondering if there was a specific reason for `if val` but good to hear we can safely use `len(val == 0`

Co-authored-by: compilade <redacted>
---------

Co-authored-by: compilade <redacted>
gguf-py/gguf/gguf_writer.py

index ba6f53cda25a18a55a293782774b2080da385ede..2e0b335eebed11d6a47c6aa40786f84965f9cdb8 100644 (file)
@@ -312,6 +312,8 @@ class GGUFWriter:
         self.add_key_value(key, val, GGUFValueType.STRING)
 
     def add_array(self, key: str, val: Sequence[Any]) -> None:
+        if len(val) == 0:
+            return
         self.add_key_value(key, val, GGUFValueType.ARRAY)
 
     @staticmethod
@@ -845,7 +847,14 @@ class GGUFWriter:
             encoded_val = val.encode("utf-8") if isinstance(val, str) else val
             kv_data += self._pack("Q", len(encoded_val))
             kv_data += encoded_val
-        elif vtype == GGUFValueType.ARRAY and isinstance(val, Sequence) and val:
+        elif vtype == GGUFValueType.ARRAY:
+
+            if not isinstance(val, Sequence):
+                raise ValueError("Invalid GGUF metadata array, expecting sequence")
+
+            if len(val) == 0:
+                raise ValueError("Invalid GGUF metadata array. Empty array")
+
             if isinstance(val, bytes):
                 ltype = GGUFValueType.UINT8
             else: