Skip to content

Commit 912c98f

Browse files
ikawrakowIwan Kawrakow
andauthored
Fix requatizing from row-interleaved quants (ikawrakow#992)
Co-authored-by: Iwan Kawrakow <[email protected]>
1 parent e919c00 commit 912c98f

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

src/llama-quantize.cpp

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ struct quantize_state_internal {
6666
{}
6767
};
6868

69+
static std::pair<ggml_type, int> interleaved_properties(ggml_type type) {
70+
static std::unordered_map<ggml_type, std::pair<ggml_type, int>> k_map = {
71+
{ GGML_TYPE_Q4_0_4_4, { GGML_TYPE_Q4_0, 4} },
72+
{ GGML_TYPE_Q4_0_4_8, { GGML_TYPE_Q4_0, 4} },
73+
{ GGML_TYPE_Q4_0_8_8, { GGML_TYPE_Q4_0, 8} },
74+
{ GGML_TYPE_Q4_0_R8, { GGML_TYPE_Q4_0, 8} },
75+
{ GGML_TYPE_Q5_0_R4, { GGML_TYPE_Q5_0, 4} },
76+
{ GGML_TYPE_Q6_0_R4, { GGML_TYPE_Q6_0, 4} },
77+
{ GGML_TYPE_Q8_0_R8, { GGML_TYPE_Q8_0, 8} },
78+
{ GGML_TYPE_Q2_K_R4, { GGML_TYPE_Q2_K, 4} },
79+
{ GGML_TYPE_Q3_K_R4, { GGML_TYPE_Q3_K, 4} },
80+
{ GGML_TYPE_Q4_K_R4, { GGML_TYPE_Q4_K, 4} },
81+
{ GGML_TYPE_Q5_K_R4, { GGML_TYPE_Q5_K, 4} },
82+
{ GGML_TYPE_Q6_K_R4, { GGML_TYPE_Q6_K, 4} },
83+
{ GGML_TYPE_IQ2_XXS_R4, { GGML_TYPE_IQ2_XXS, 4} },
84+
{ GGML_TYPE_IQ2_XS_R4, { GGML_TYPE_IQ2_XS, 4} },
85+
{ GGML_TYPE_IQ2_S_R4, { GGML_TYPE_IQ2_S, 4} },
86+
{ GGML_TYPE_IQ3_XXS_R4, { GGML_TYPE_IQ3_XXS, 4} },
87+
{ GGML_TYPE_IQ3_S_R4, { GGML_TYPE_IQ3_S, 4} },
88+
{ GGML_TYPE_IQ4_XS_R8, { GGML_TYPE_IQ4_XS, 8} },
89+
{ GGML_TYPE_IQ4_NL_R4, { GGML_TYPE_IQ4_NL, 4} },
90+
{ GGML_TYPE_IQ1_S_R4, { GGML_TYPE_IQ1_S, 4} },
91+
{ GGML_TYPE_IQ1_M_R4, { GGML_TYPE_IQ1_M, 4} },
92+
{ GGML_TYPE_IQ2_BN_R4, { GGML_TYPE_IQ2_BN, 4} },
93+
{ GGML_TYPE_IQ2_K_R4, { GGML_TYPE_IQ2_K, 4} },
94+
{ GGML_TYPE_IQ3_K_R4, { GGML_TYPE_IQ3_K, 4} },
95+
{ GGML_TYPE_IQ4_K_R4, { GGML_TYPE_IQ4_K, 4} },
96+
{ GGML_TYPE_IQ4_KS_R4, { GGML_TYPE_IQ4_KS, 4} },
97+
{ GGML_TYPE_IQ5_KS_R4, { GGML_TYPE_IQ5_KS, 4} },
98+
{ GGML_TYPE_IQ5_K_R4, { GGML_TYPE_IQ5_K, 4} },
99+
{ GGML_TYPE_Q8_KV_R8, { GGML_TYPE_Q8_KV, 8} },
100+
{ GGML_TYPE_Q8_K_R8, { GGML_TYPE_Q8_0, 8} },
101+
{ GGML_TYPE_BF16_R16, { GGML_TYPE_BF16, 16} },
102+
};
103+
if (auto it = k_map.find(type); it != k_map.end()) return it->second;
104+
return {type, 1};
105+
}
106+
69107
static void llama_tensor_dequantize_internal(
70108
struct ggml_tensor * tensor, std::vector<no_init<float>> & output, std::vector<std::thread> & workers,
71109
const size_t nelements, const int nthread
@@ -101,10 +139,11 @@ static void llama_tensor_dequantize_internal(
101139
auto row_size = ggml_row_size(tensor->type, tensor->ne[0]);
102140
int nrows = ggml_nrows(tensor);
103141
auto qsrc = (const char *)tensor->data;
104-
for (int row = 0; row < nrows; ++row) {
105-
qtype.to_float(qsrc, f32_output, tensor->ne[0]);
106-
qsrc += row_size;
107-
f32_output += tensor->ne[0];
142+
auto num_rows = interleaved_properties(tensor->type).second;
143+
for (int row = 0; row < nrows; row += num_rows) {
144+
qtype.to_float(qsrc, f32_output, num_rows*tensor->ne[0]);
145+
qsrc += num_rows*row_size;
146+
f32_output += num_rows*tensor->ne[0];
108147
}
109148
} else {
110149
GGML_ABORT("fatal error"); // unreachable
@@ -235,44 +274,6 @@ static ggml_type change_type_if_necessary(ggml_type new_type, int nx, int ny) {
235274
return new_type;
236275
}
237276

238-
static std::pair<ggml_type, int> interleaved_properties(ggml_type type) {
239-
static std::unordered_map<ggml_type, std::pair<ggml_type, int>> k_map = {
240-
{ GGML_TYPE_Q4_0_4_4, { GGML_TYPE_Q4_0, 4} },
241-
{ GGML_TYPE_Q4_0_4_8, { GGML_TYPE_Q4_0, 4} },
242-
{ GGML_TYPE_Q4_0_8_8, { GGML_TYPE_Q4_0, 8} },
243-
{ GGML_TYPE_Q4_0_R8, { GGML_TYPE_Q4_0, 8} },
244-
{ GGML_TYPE_Q5_0_R4, { GGML_TYPE_Q5_0, 4} },
245-
{ GGML_TYPE_Q6_0_R4, { GGML_TYPE_Q6_0, 4} },
246-
{ GGML_TYPE_Q8_0_R8, { GGML_TYPE_Q8_0, 8} },
247-
{ GGML_TYPE_Q2_K_R4, { GGML_TYPE_Q2_K, 4} },
248-
{ GGML_TYPE_Q3_K_R4, { GGML_TYPE_Q3_K, 4} },
249-
{ GGML_TYPE_Q4_K_R4, { GGML_TYPE_Q4_K, 4} },
250-
{ GGML_TYPE_Q5_K_R4, { GGML_TYPE_Q5_K, 4} },
251-
{ GGML_TYPE_Q6_K_R4, { GGML_TYPE_Q6_K, 4} },
252-
{ GGML_TYPE_IQ2_XXS_R4, { GGML_TYPE_IQ2_XXS, 4} },
253-
{ GGML_TYPE_IQ2_XS_R4, { GGML_TYPE_IQ2_XS, 4} },
254-
{ GGML_TYPE_IQ2_S_R4, { GGML_TYPE_IQ2_S, 4} },
255-
{ GGML_TYPE_IQ3_XXS_R4, { GGML_TYPE_IQ3_XXS, 4} },
256-
{ GGML_TYPE_IQ3_S_R4, { GGML_TYPE_IQ3_S, 4} },
257-
{ GGML_TYPE_IQ4_XS_R8, { GGML_TYPE_IQ4_XS, 8} },
258-
{ GGML_TYPE_IQ4_NL_R4, { GGML_TYPE_IQ4_NL, 4} },
259-
{ GGML_TYPE_IQ1_S_R4, { GGML_TYPE_IQ1_S, 4} },
260-
{ GGML_TYPE_IQ1_M_R4, { GGML_TYPE_IQ1_M, 4} },
261-
{ GGML_TYPE_IQ2_BN_R4, { GGML_TYPE_IQ2_BN, 4} },
262-
{ GGML_TYPE_IQ2_K_R4, { GGML_TYPE_IQ2_K, 4} },
263-
{ GGML_TYPE_IQ3_K_R4, { GGML_TYPE_IQ3_K, 4} },
264-
{ GGML_TYPE_IQ4_K_R4, { GGML_TYPE_IQ4_K, 4} },
265-
{ GGML_TYPE_IQ4_KS_R4, { GGML_TYPE_IQ4_KS, 4} },
266-
{ GGML_TYPE_IQ5_KS_R4, { GGML_TYPE_IQ5_KS, 4} },
267-
{ GGML_TYPE_IQ5_K_R4, { GGML_TYPE_IQ5_K, 4} },
268-
{ GGML_TYPE_Q8_KV_R8, { GGML_TYPE_Q8_KV, 8} },
269-
{ GGML_TYPE_Q8_K_R8, { GGML_TYPE_Q8_0, 8} },
270-
{ GGML_TYPE_BF16_R16, { GGML_TYPE_BF16, 16} },
271-
};
272-
if (auto it = k_map.find(type); it != k_map.end()) return it->second;
273-
return {type, 1};
274-
}
275-
276277
static ggml_type llama_tensor_get_type(quantize_state_internal & qs, ggml_type new_type, const ggml_tensor * tensor, llama_ftype ftype) {
277278
const std::string name = ggml_get_name(tensor);
278279

0 commit comments

Comments
 (0)