|
19 | 19 | logger = logging.getLogger("gguf-convert-endian") |
20 | 20 |
|
21 | 21 |
|
| 22 | +def byteswap_noop(tensor, block_offs): |
| 23 | + # this function is used when byteswapping is not needed |
| 24 | + pass |
| 25 | + |
| 26 | + |
22 | 27 | def byteswap_q4_0(tensor, block_offs): |
23 | 28 | # Each block_q4_0 consists of an f16 delta (scaling factor) followed by 16 int8 quantizations. |
24 | 29 |
|
@@ -55,22 +60,11 @@ def byteswap_q6_k(tensor, block_offs): |
55 | 60 |
|
56 | 61 |
|
57 | 62 | byteswap_tensors = { |
58 | | - gguf.GGMLQuantizationType.Q4_0: { |
59 | | - "block_size": 18, # 18 bytes = <f16 delta scaling factor> + 16 * <int8 quant> |
60 | | - "byteswap_func": byteswap_q4_0, |
61 | | - }, |
62 | | - gguf.GGMLQuantizationType.Q8_0: { |
63 | | - "block_size": 34, # 34 bytes = <f16 delta scaling factor> + 32 * <int8 quant> |
64 | | - "byteswap_func": byteswap_q8_0, |
65 | | - }, |
66 | | - gguf.GGMLQuantizationType.Q4_K: { |
67 | | - "block_size": 144, # 144 bytes = 2 * <f16 delta scaling factor> + 140 * <int8 quant> |
68 | | - "byteswap_func": byteswap_q4_k, |
69 | | - }, |
70 | | - gguf.GGMLQuantizationType.Q6_K: { |
71 | | - "block_size": 210, # 210 bytes = <f16 delta scaling factor> + 208 * <int8 quant> |
72 | | - "byteswap_func": byteswap_q6_k, |
73 | | - }, |
| 63 | + gguf.GGMLQuantizationType.Q4_0: byteswap_q4_0, |
| 64 | + gguf.GGMLQuantizationType.Q8_0: byteswap_q8_0, |
| 65 | + gguf.GGMLQuantizationType.Q4_K: byteswap_q4_k, |
| 66 | + gguf.GGMLQuantizationType.Q6_K: byteswap_q6_k, |
| 67 | + gguf.GGMLQuantizationType.MXFP4: byteswap_noop, |
74 | 68 | } |
75 | 69 |
|
76 | 70 |
|
@@ -135,8 +129,8 @@ def convert_byteorder(reader: gguf.GGUFReader, args: argparse.Namespace) -> None |
135 | 129 |
|
136 | 130 | tensor.data.resize(newshape) |
137 | 131 |
|
138 | | - block_size = byteswap_tensors[tensor.tensor_type]["block_size"] |
139 | | - byteswap_func = byteswap_tensors[tensor.tensor_type]["byteswap_func"] |
| 132 | + block_size = gguf.constants.GGML_QUANT_SIZES[tensor.tensor_type][1] |
| 133 | + byteswap_func = byteswap_tensors[tensor.tensor_type] |
140 | 134 |
|
141 | 135 | n_blocks = len(tensor.data) // block_size |
142 | 136 | for block_num in (inner_pbar := tqdm(range(n_blocks), desc="Byte-swapping Blocks", leave=False)): |
|
0 commit comments