Skip to content

Commit

Permalink
py: add_array() will not add to kv store if value is an empty array (#…
Browse files Browse the repository at this point in the history
…8774)

* 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 <[email protected]>

---------

Co-authored-by: compilade <[email protected]>
  • Loading branch information
mofosyne and compilade authored Jul 30, 2024
1 parent 7c27a19 commit 7e72aa7
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion gguf-py/gguf/gguf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def add_string(self, key: str, val: str) -> None:
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
Expand Down Expand Up @@ -845,7 +847,14 @@ def _pack_val(self, val: Any, vtype: GGUFValueType, add_vtype: bool) -> bytes:
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:
Expand Down

0 comments on commit 7e72aa7

Please sign in to comment.