Skip to content

Commit 4fcd87c

Browse files
gguf-py : skip endian-conversion of MXFP4 data (#17523)
* gguf_convert_endian.py: skip MXFP4 data * Use gguf.constants.GGML_QUANT_SIZES to determine block sizes
1 parent b78db3b commit 4fcd87c

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

gguf-py/gguf/scripts/gguf_convert_endian.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
logger = logging.getLogger("gguf-convert-endian")
2020

2121

22+
def byteswap_noop(tensor, block_offs):
23+
# this function is used when byteswapping is not needed
24+
pass
25+
26+
2227
def byteswap_q4_0(tensor, block_offs):
2328
# Each block_q4_0 consists of an f16 delta (scaling factor) followed by 16 int8 quantizations.
2429

@@ -55,22 +60,11 @@ def byteswap_q6_k(tensor, block_offs):
5560

5661

5762
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,
7468
}
7569

7670

@@ -135,8 +129,8 @@ def convert_byteorder(reader: gguf.GGUFReader, args: argparse.Namespace) -> None
135129

136130
tensor.data.resize(newshape)
137131

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]
140134

141135
n_blocks = len(tensor.data) // block_size
142136
for block_num in (inner_pbar := tqdm(range(n_blocks), desc="Byte-swapping Blocks", leave=False)):

0 commit comments

Comments
 (0)