File size: 1,837 Bytes
8d0b310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# Convert a GGUF split shard to a standalone GGUF for single-shard quantization.

import sys
from pathlib import Path
import gguf

def make_standalone(src_path: Path, dst_path: Path):
    reader = gguf.GGUFReader(str(src_path))
    arch = bytes(reader.fields["general.architecture"].parts[0]).decode("utf-8")
    writer = gguf.GGUFWriter(str(dst_path), arch)

    for field in reader.fields.values():
        if field.name in ["split.no", "split.count", "general.architecture"]:
            continue
        vtype = field.types[0]
        if vtype == gguf.GGUFValueType.STRING:
            val = bytes(field.parts[field.data[0]]).decode("utf-8", errors="ignore")
            writer.add_string(field.name, val)
        elif vtype in [gguf.GGUFValueType.UINT32, gguf.GGUFValueType.INT32, gguf.GGUFValueType.UINT64, gguf.GGUFValueType.INT64]:
            writer.add_uint32(field.name, int(field.parts[field.data[0]][0]))
        elif vtype in [gguf.GGUFValueType.FLOAT32, gguf.GGUFValueType.FLOAT64]:
            writer.add_float32(field.name, float(field.parts[field.data[0]][0]))
        elif vtype == gguf.GGUFValueType.BOOL:
            writer.add_bool(field.name, bool(field.parts[field.data[0]][0]))

    # Copy tensors from reader to writer
    for tensor in reader.tensors:
        writer.add_tensor(
            tensor.name,
            tensor.data,
            raw_dtype=tensor.tensor_type
        )

    writer.write_header_to_file()
    writer.write_kv_data_to_file()
    writer.write_tensors_to_file()
    writer.close()
    print(f"Successfully created standalone shard at: {dst_path}")

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python3 make_standalone_shard.py <input.gguf> <output.gguf>")
        sys.exit(1)
    make_standalone(Path(sys.argv[1]), Path(sys.argv[2]))