Upload gguf.py
Browse files
gguf.py
CHANGED
|
@@ -325,11 +325,23 @@ class GGUFReader:
|
|
| 325 |
"""Raw packed label channel bytes for ``name`` (undecoded KV value)."""
|
| 326 |
for key, vtype, raw in self.kv_pairs:
|
| 327 |
if key == SEG_KV_PREFIX + name:
|
| 328 |
-
if vtype
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
return None
|
| 334 |
|
| 335 |
def seg_labels(self, name: str) -> tuple[np.ndarray, int] | None:
|
|
@@ -897,7 +909,10 @@ def write_quantized_gguf(
|
|
| 897 |
key = SEG_KV_PREFIX + ti.name
|
| 898 |
content_len = label_channel_bytes(ti.n_elements, seg_k)
|
| 899 |
seg_reserve[key] = content_len
|
| 900 |
-
|
|
|
|
|
|
|
|
|
|
| 901 |
|
| 902 |
output_path = Path(output_path)
|
| 903 |
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
@@ -925,7 +940,7 @@ def write_quantized_gguf(
|
|
| 925 |
if len(packed) != seg_reserve.get(key, -1):
|
| 926 |
raise RuntimeError(f"{key}: label channel size {len(packed)} != reserved")
|
| 927 |
pos = fout.tell()
|
| 928 |
-
fout.seek(seg_patch_offsets[key] +
|
| 929 |
fout.write(packed)
|
| 930 |
fout.seek(pos)
|
| 931 |
for ti, out_type, out_size, offset in out_infos:
|
|
|
|
| 325 |
"""Raw packed label channel bytes for ``name`` (undecoded KV value)."""
|
| 326 |
for key, vtype, raw in self.kv_pairs:
|
| 327 |
if key == SEG_KV_PREFIX + name:
|
| 328 |
+
if vtype == 8:
|
| 329 |
+
# legacy: string value, u64 length prefix then raw bytes
|
| 330 |
+
if len(raw) < 8:
|
| 331 |
+
raise ValueError(f"{name}: corrupt label channel KV")
|
| 332 |
+
return raw[8:]
|
| 333 |
+
if vtype == 9:
|
| 334 |
+
# uint8 array: u32 element type + u64 count + data
|
| 335 |
+
if len(raw) < 12:
|
| 336 |
+
raise ValueError(f"{name}: corrupt label channel KV")
|
| 337 |
+
if struct.unpack("<I", raw[:4])[0] != 0:
|
| 338 |
+
raise ValueError(f"{name}: label channel array is not uint8")
|
| 339 |
+
n = struct.unpack("<Q", raw[4:12])[0]
|
| 340 |
+
if len(raw) < 12 + n:
|
| 341 |
+
raise ValueError(f"{name}: truncated label channel array")
|
| 342 |
+
return raw[12 : 12 + n]
|
| 343 |
+
raise ValueError(f"{name}: label channel KV has bad type {vtype}")
|
| 344 |
+
return None
|
| 345 |
return None
|
| 346 |
|
| 347 |
def seg_labels(self, name: str) -> tuple[np.ndarray, int] | None:
|
|
|
|
| 909 |
key = SEG_KV_PREFIX + ti.name
|
| 910 |
content_len = label_channel_bytes(ti.n_elements, seg_k)
|
| 911 |
seg_reserve[key] = content_len
|
| 912 |
+
# GGUF uint8 array: u32 element type + u64 count + zeros
|
| 913 |
+
kv_pairs.append(
|
| 914 |
+
(key, 9, struct.pack("<I", 0) + struct.pack("<Q", content_len) + bytes(content_len))
|
| 915 |
+
)
|
| 916 |
|
| 917 |
output_path = Path(output_path)
|
| 918 |
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 940 |
if len(packed) != seg_reserve.get(key, -1):
|
| 941 |
raise RuntimeError(f"{key}: label channel size {len(packed)} != reserved")
|
| 942 |
pos = fout.tell()
|
| 943 |
+
fout.seek(seg_patch_offsets[key] + 12) # skip u32 array type + u64 count
|
| 944 |
fout.write(packed)
|
| 945 |
fout.seek(pos)
|
| 946 |
for ti, out_type, out_size, offset in out_infos:
|