35
35
36
36
import gguf
37
37
38
- GGML_QK8_0 = 32
39
- GGML_QK4_0 = 32
40
- GGML_QK4_1 = 32
38
+ QK8_0 = gguf . GGML_QUANT_SIZES [ gguf . GGMLQuantizationType . Q8_0 ][ 0 ]
39
+ QK4_0 = gguf . GGML_QUANT_SIZES [ gguf . GGMLQuantizationType . Q4_0 ][ 0 ]
40
+ QK4_1 = gguf . GGML_QUANT_SIZES [ gguf . GGMLQuantizationType . Q4_1 ][ 0 ]
41
41
42
42
43
43
# Heuristic to avoid having to fully parse pickle files.
@@ -125,8 +125,8 @@ def get_weights(fn):
125
125
126
126
def quantize_q8_0 (tensor : torch .Tensor ) -> torch .CharTensor :
127
127
# equivalent to ggml_quantize_q8_0 in ggml.c (modulo rounding away from zero)
128
- assert tensor .shape [1 ] % GGML_QK8_0 == 0
129
- tensor = tensor .reshape (- 1 , GGML_QK8_0 )
128
+ assert tensor .shape [1 ] % QK8_0 == 0
129
+ tensor = tensor .reshape (- 1 , QK8_0 )
130
130
scale = tensor .abs ().max (dim = - 1 , keepdim = True ).values / ((1 << 7 ) - 1 )
131
131
tensor = (tensor / scale ).round ().clamp (min = - 128 , max = 127 ).char ()
132
132
# add scale into each block
@@ -136,8 +136,8 @@ def quantize_q8_0(tensor: torch.Tensor) -> torch.CharTensor:
136
136
137
137
def quantize_q4_0 (tensor : torch .Tensor ) -> torch .CharTensor :
138
138
# equivalent to ggml_quantize_q4_0 in ggml.c (modulo rounding away from zero)
139
- assert tensor .shape [1 ] % GGML_QK4_0 == 0
140
- tensor = tensor .reshape (- 1 , GGML_QK4_0 )
139
+ assert tensor .shape [1 ] % QK4_0 == 0
140
+ tensor = tensor .reshape (- 1 , QK4_0 )
141
141
abs_max_indices = tensor .abs ().max (dim = - 1 , keepdim = True ).indices
142
142
max_values = torch .take_along_dim (tensor , abs_max_indices , dim = - 1 )
143
143
scale = max_values / - 8
@@ -151,8 +151,8 @@ def quantize_q4_0(tensor: torch.Tensor) -> torch.CharTensor:
151
151
152
152
def quantize_q4_1 (tensor : torch .Tensor ) -> torch .CharTensor :
153
153
# equivalent to ggml_quantize_q4_1 in ggml.c (modulo rounding away from zero)
154
- assert tensor .shape [1 ] % GGML_QK4_1 == 0
155
- tensor = tensor .reshape (- 1 , GGML_QK4_1 )
154
+ assert tensor .shape [1 ] % QK4_1 == 0
155
+ tensor = tensor .reshape (- 1 , QK4_1 )
156
156
abs_max_indices = tensor .max (dim = - 1 , keepdim = True ).indices
157
157
max_values = torch .take_along_dim (tensor , abs_max_indices , dim = - 1 )
158
158
abs_min_indices = tensor .min (dim = - 1 , keepdim = True ).indices
@@ -188,7 +188,7 @@ def maybe_quantize_tensor(tensor, ggml_type):
188
188
189
189
def get_dtype_and_ggml_type (name , tensor , ggml_type ):
190
190
if tensor .ndim in (2 , 3 ) and "ffn_gate_inp" not in name :
191
- if tensor .shape [1 ] % GGML_QK8_0 == 0 :
191
+ if tensor .shape [1 ] % QK8_0 == 0 :
192
192
return np .int8 , ggml_type
193
193
else :
194
194
return np .float16 , gguf .GGMLQuantizationType .F16
0 commit comments