ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-zdnn / mmq.cpp
| 1 | #include "ggml.h" |
| 2 | #include "mmq.hpp" |
| 3 | #include "quantize.hpp" |
| 4 | #include "utils.hpp" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <future> |
| 8 | #include <vector> |
| 9 | |
| 10 | // the weights are stacked into groups along ne00, and each group of each row of the inputs and weights has its own scale |
| 11 | // zDNN multiplies the groups in stacked calls, then the partial results of each group are scaled and summed here |
| 12 | static void ggml_zdnn_mul_mat_q_grouped(ggml_backend_zdnn_context * ctx, |
| 13 | int32_t n_threads, |
| 14 | const ggml_tensor * inputs, |
| 15 | const ggml_backend_zdnn_buffer * weights_extra, |
| 16 | ggml_tensor * output) { |
| 17 | const int64_t ne10 = inputs->ne[0]; |
| 18 | const int64_t ne11 = inputs->ne[1]; |
| 19 | const int64_t ne0 = output->ne[0]; |
| 20 | |
| 21 | const int64_t n_groups = weights_extra->pre_tfm_desc.dim3; |
| 22 | const int64_t gs = weights_extra->pre_tfm_desc.dim2; |
| 23 | |
| 24 | // the last group is zero padded when gs does not divide ne10 |
| 25 | GGML_ASSERT(n_groups * gs >= ne10 && (n_groups - 1) * gs < ne10); |
| 26 | |
| 27 | // the rounding errors of the inputs are stacked after the inputs, see ggml_zdnn_quantize_inputs |
| 28 | ggml_zdnn_pool_alloc<int8_t> inputs_q_alloc(ctx->pool, 2 * n_groups * gs * ne11); |
| 29 | int8_t * inputs_q = inputs_q_alloc.get(); |
| 30 | std::vector<float> inputs_scales(n_groups * ne11); |
| 31 | ggml_zdnn_quantize_inputs(n_threads, inputs, gs, weights_extra->channel_scales.data(), |
| 32 | inputs_q, inputs_q + n_groups * gs * ne11, inputs_scales.data()); |
| 33 | |
| 34 | zdnn_tensor_desc inputs_pre_tfm_desc; |
| 35 | zdnn_tensor_desc inputs_tfm_desc; |
| 36 | zdnn_ztensor inputs_ztensor; |
| 37 | ggml_zdnn_pool_alloc<uint8_t> inputs_ztensor_alloc; |
| 38 | |
| 39 | zdnn_init_pre_transformed_desc(ZDNN_3DS, INT8, &inputs_pre_tfm_desc, 2 * n_groups, ne11, gs); |
| 40 | ZDNN_CHECK(zdnn_generate_quantized_transformed_desc(&inputs_pre_tfm_desc, QUANTIZED_INT8, &inputs_tfm_desc)); |
| 41 | ggml_zdnn_init_scratch_ztensor(ctx, &inputs_pre_tfm_desc, &inputs_tfm_desc, &inputs_ztensor, inputs_ztensor_alloc); |
| 42 | ZDNN_CHECK(zdnn_transform_quantized_ztensor(&inputs_ztensor, false, -127, 127, inputs_q)); |
| 43 | |
| 44 | // each group of the partial results is read back as its own 2D ztensor |
| 45 | zdnn_tensor_desc group_pre_tfm_desc; |
| 46 | zdnn_tensor_desc group_tfm_desc; |
| 47 | |
| 48 | zdnn_init_pre_transformed_desc(ZDNN_2D, FP32, &group_pre_tfm_desc, ne11, ne0); |
| 49 | ZDNN_CHECK(zdnn_generate_transformed_desc(&group_pre_tfm_desc, &group_tfm_desc)); |
| 50 | |
| 51 | const uint64_t group_bytes = zdnn_getsize_ztensor(&group_tfm_desc); |
| 52 | |
| 53 | // stacked calls with a large output are much slower per group, so run the groups in chunks |
| 54 | // 8 MiB per chunk was benchmarked to be the best on Llama-3.2-1B Q8_0 |
| 55 | // always re-measure with different models to see if different sizes need a dispatching logic |
| 56 | constexpr int64_t chunk_bytes = 8*1024*1024; |
| 57 | |
| 58 | const int64_t n_chunk = std::clamp<int64_t>(chunk_bytes / (int64_t)group_bytes, 1, n_groups); |
| 59 | |
| 60 | // all zeros, so it is already the pre-computed bias for every chunk |
| 61 | const zdnn_ztensor & bias_ztensor = ggml_zdnn_zero_bias(ctx, n_chunk, ne0); |
| 62 | |
| 63 | // two partial buffers, so the next chunk runs on the NNPA while this one is summed |
| 64 | // each holds the results of a chunk of inputs, followed by the results of their rounding errors |
| 65 | zdnn_tensor_desc partial_pre_tfm_desc; |
| 66 | zdnn_tensor_desc partial_tfm_desc; |
| 67 | zdnn_ztensor partial_ztensor[2]; |
| 68 | ggml_zdnn_pool_alloc<uint8_t> partial_ztensor_alloc[2]; |
| 69 | |
| 70 | zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &partial_pre_tfm_desc, 2 * n_chunk, ne11, ne0); |
| 71 | ZDNN_CHECK(zdnn_generate_quantized_transformed_desc(&partial_pre_tfm_desc, QUANTIZED_DLFLOAT16, &partial_tfm_desc)); |
| 72 | GGML_ASSERT(zdnn_getsize_ztensor(&partial_tfm_desc) == (uint64_t)(2 * n_chunk) * group_bytes); |
| 73 | for (int32_t i = 0; i < 2; i++) { |
| 74 | ggml_zdnn_init_scratch_ztensor(ctx, &partial_pre_tfm_desc, &partial_tfm_desc, &partial_ztensor[i], partial_ztensor_alloc[i]); |
| 75 | } |
| 76 | |
| 77 | const float * weights_scales = weights_extra->scales.data(); |
| 78 | ggml_zdnn_pool_alloc<float> partial_alloc(ctx->pool, 2 * ne11 * ne0); |
| 79 | float * partial = partial_alloc.get(); |
| 80 | float * partial_res = partial + ne11 * ne0; |
| 81 | |
| 82 | const auto sum_chunk = [&](const zdnn_ztensor & chunk, int64_t g0, int64_t n) { |
| 83 | zdnn_ztensor group_ztensor; |
| 84 | zdnn_init_ztensor(&group_pre_tfm_desc, &group_tfm_desc, &group_ztensor); |
| 85 | |
| 86 | for (int64_t g = g0; g < g0 + n; g++) { |
| 87 | group_ztensor.buffer_size = group_bytes; |
| 88 | group_ztensor.is_transformed = true; |
| 89 | |
| 90 | group_ztensor.buffer = (char *)chunk.buffer + (g - g0)*group_bytes; |
| 91 | ZDNN_CHECK(zdnn_transform_origtensor(&group_ztensor, partial)); |
| 92 | |
| 93 | group_ztensor.buffer = (char *)chunk.buffer + (n_chunk + g - g0)*group_bytes; |
| 94 | ZDNN_CHECK(zdnn_transform_origtensor(&group_ztensor, partial_res)); |
| 95 | |
| 96 | const float * sw = weights_scales + g*ne0; |
| 97 | for (int64_t i1 = 0; i1 < ne11; i1++) { |
| 98 | const float sa = inputs_scales[g*ne11 + i1]; |
| 99 | const float * p = partial + i1*ne0; |
| 100 | const float * pr = partial_res + i1*ne0; |
| 101 | float * y = (float *)output->data + i1*ne0; |
| 102 | |
| 103 | if (g == 0) { |
| 104 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 105 | y[i0] = sa * sw[i0] * (p[i0] + pr[i0] * (1.0f/254.0f)); |
| 106 | } |
| 107 | } else { |
| 108 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 109 | y[i0] += sa * sw[i0] * (p[i0] + pr[i0] * (1.0f/254.0f)); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | zdnn_tensor_desc view_pre_tfm_desc[6]; |
| 117 | zdnn_tensor_desc view_tfm_desc[6]; |
| 118 | std::future<void> sum_task; |
| 119 | |
| 120 | for (int64_t c = 0, g0 = 0; g0 < n_groups; c++, g0 += n_chunk) { |
| 121 | const int64_t n = std::min(n_chunk, n_groups - g0); |
| 122 | |
| 123 | const zdnn_ztensor inputs_view = ggml_zdnn_stack_view(&view_pre_tfm_desc[0], &view_tfm_desc[0], inputs_ztensor, 2 * n_groups, g0, n); |
| 124 | const zdnn_ztensor inputs_res_view = ggml_zdnn_stack_view(&view_pre_tfm_desc[1], &view_tfm_desc[1], inputs_ztensor, 2 * n_groups, n_groups + g0, n); |
| 125 | const zdnn_ztensor weights_view = ggml_zdnn_stack_view(&view_pre_tfm_desc[2], &view_tfm_desc[2], weights_extra->ztensor, n_groups, g0, n); |
| 126 | const zdnn_ztensor bias_view = ggml_zdnn_stack_view(&view_pre_tfm_desc[3], &view_tfm_desc[3], bias_ztensor, n_chunk, 0, n); |
| 127 | |
| 128 | // the partial buffer of chunk c - 2 is summed by now, as sum_task is waited on before starting the next sum |
| 129 | zdnn_ztensor & chunk = partial_ztensor[c % 2]; |
| 130 | chunk.pre_transformed_desc = &partial_pre_tfm_desc; |
| 131 | chunk.transformed_desc = &partial_tfm_desc; |
| 132 | chunk.is_transformed = false; |
| 133 | |
| 134 | zdnn_ztensor chunk_view = ggml_zdnn_stack_view(&view_pre_tfm_desc[4], &view_tfm_desc[4], chunk, 2 * n_chunk, 0, n); |
| 135 | zdnn_ztensor chunk_res_view = ggml_zdnn_stack_view(&view_pre_tfm_desc[5], &view_tfm_desc[5], chunk, 2 * n_chunk, n_chunk, n); |
| 136 | ZDNN_CHECK(zdnn_quantized_matmul_op(&inputs_view, &weights_view, &bias_view, |
| 137 | MATMUL_OP_ADDITION, -127, 127, true, false, true, |
| 138 | nullptr, &chunk_view)); |
| 139 | ZDNN_CHECK(zdnn_quantized_matmul_op(&inputs_res_view, &weights_view, &bias_view, |
| 140 | MATMUL_OP_ADDITION, -127, 127, true, false, true, |
| 141 | nullptr, &chunk_res_view)); |
| 142 | |
| 143 | if (sum_task.valid()) { |
| 144 | sum_task.get(); |
| 145 | } |
| 146 | |
| 147 | if (n_threads > 1) { |
| 148 | sum_task = std::async(std::launch::async, sum_chunk, chunk, g0, n); |
| 149 | } else { |
| 150 | sum_chunk(chunk, g0, n); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (sum_task.valid()) { |
| 155 | sum_task.get(); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
| 160 | void ggml_zdnn_mul_mat_q( |
| 161 | ggml_backend_zdnn_context * ctx, |
| 162 | const ggml_tensor * src0, |
| 163 | const ggml_tensor * src1, |
| 164 | ggml_tensor * dst) { |
| 165 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 166 | |
| 167 | const enum ggml_type type = src0->type; |
| 168 | |
| 169 | GGML_ASSERT(ne0 == ne01); |
| 170 | GGML_ASSERT(ne1 == ne11); |
| 171 | GGML_ASSERT(ne2 == ne12); |
| 172 | GGML_ASSERT(ne3 == ne13); |
| 173 | |
| 174 | // we don't support permuted src0 or src1 |
| 175 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 176 | GGML_ASSERT(nb10 == ggml_type_size(src1->type)); |
| 177 | |
| 178 | // dst cannot be transposed or permuted |
| 179 | GGML_ASSERT(nb0 == sizeof(float)); |
| 180 | GGML_ASSERT(nb0 <= nb1); |
| 181 | GGML_ASSERT(nb1 <= nb2); |
| 182 | GGML_ASSERT(nb2 <= nb3); |
| 183 | |
| 184 | const ggml_tensor * weights = src0; |
| 185 | const ggml_tensor * inputs = src1; |
| 186 | ggml_tensor * output = dst; |
| 187 | |
| 188 | GGML_ASSERT(inputs->type == GGML_TYPE_F32); |
| 189 | |
| 190 | ggml_backend_zdnn_buffer * weights_extra = (ggml_backend_zdnn_buffer *)weights->extra; |
| 191 | ggml_backend_zdnn_buffer * output_extra = (ggml_backend_zdnn_buffer *)output->extra; |
| 192 | |
| 193 | // weights read straight into a host buffer skip set_tensor, so transform them on first use |
| 194 | if (!weights_extra->ztensor.is_transformed) { |
| 195 | ggml_zdnn_load_tensor_q(weights_extra, weights); |
| 196 | } |
| 197 | |
| 198 | // starting threads costs more than it saves for small inputs, e.g. during token generation |
| 199 | const int32_t n_threads = ne10*ne11 >= 65536 ? ctx->n_threads : 1; |
| 200 | |
| 201 | if (weights_extra->pre_tfm_desc.layout == ZDNN_3DS) { |
| 202 | GGML_ASSERT(weights_extra->pre_tfm_desc.dim1 == weights->ne[1] && "weights_extra->pre_tfm_desc.dim1 must match weights->ne[1]"); |
| 203 | |
| 204 | ggml_zdnn_mul_mat_q_grouped(ctx, n_threads, inputs, weights_extra, output); |
| 205 | |
| 206 | // the result is written to output->data directly, so the output ztensor is stale |
| 207 | output_extra->ztensor.is_transformed = false; |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | ggml_backend_zdnn_buffer * bias_extra = (ggml_backend_zdnn_buffer *)output_extra->extra; |
| 212 | |
| 213 | GGML_ASSERT(weights_extra->pre_tfm_desc.dim2 == weights->ne[0] && "weights_extra->pre_tfm_desc.dim2 must match weights->ne[0]"); |
| 214 | GGML_ASSERT(weights_extra->pre_tfm_desc.dim1 == weights->ne[1] && "weights_extra->pre_tfm_desc.dim1 must match weights->ne[1]"); |
| 215 | |
| 216 | // zDNN takes one scale per zTensor, so quantize each row of the inputs with its own scale |
| 217 | // the rounding errors of the inputs are stacked after the inputs, see ggml_zdnn_quantize_inputs |
| 218 | ggml_zdnn_pool_alloc<int8_t> inputs_q_alloc(ctx->pool, 2 * ne10 * ne11); |
| 219 | int8_t * inputs_q = inputs_q_alloc.get(); |
| 220 | std::vector<float> inputs_scales(ne11); |
| 221 | ggml_zdnn_quantize_inputs(n_threads, inputs, ne10, weights_extra->channel_scales.data(), |
| 222 | inputs_q, inputs_q + ne10 * ne11, inputs_scales.data()); |
| 223 | |
| 224 | zdnn_tensor_desc inputs_pre_tfm_desc; |
| 225 | zdnn_tensor_desc inputs_tfm_desc; |
| 226 | zdnn_ztensor inputs_ztensor; |
| 227 | ggml_zdnn_pool_alloc<uint8_t> inputs_ztensor_alloc; |
| 228 | |
| 229 | zdnn_init_pre_transformed_desc(ZDNN_3DS, INT8, &inputs_pre_tfm_desc, 2, ne11, ne10); |
| 230 | ZDNN_CHECK(zdnn_generate_quantized_transformed_desc(&inputs_pre_tfm_desc, QUANTIZED_INT8, &inputs_tfm_desc)); |
| 231 | ggml_zdnn_init_scratch_ztensor(ctx, &inputs_pre_tfm_desc, &inputs_tfm_desc, &inputs_ztensor, inputs_ztensor_alloc); |
| 232 | ZDNN_CHECK(zdnn_transform_quantized_ztensor(&inputs_ztensor, false, -127, 127, inputs_q)); |
| 233 | |
| 234 | zdnn_tensor_desc partial_pre_tfm_desc; |
| 235 | zdnn_tensor_desc partial_tfm_desc; |
| 236 | zdnn_ztensor partial_ztensor; |
| 237 | ggml_zdnn_pool_alloc<uint8_t> partial_ztensor_alloc; |
| 238 | |
| 239 | zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &partial_pre_tfm_desc, 2, ne11, ne0); |
| 240 | ZDNN_CHECK(zdnn_generate_quantized_transformed_desc(&partial_pre_tfm_desc, QUANTIZED_DLFLOAT16, &partial_tfm_desc)); |
| 241 | ggml_zdnn_init_scratch_ztensor(ctx, &partial_pre_tfm_desc, &partial_tfm_desc, &partial_ztensor, partial_ztensor_alloc); |
| 242 | |
| 243 | // both stacks are multiplied by the same unstacked weights in one broadcast call |
| 244 | // the bias is all zeros, so it is already the pre-computed bias |
| 245 | ZDNN_CHECK(zdnn_quantized_matmul_op(&inputs_ztensor, &weights_extra->ztensor, &bias_extra->ztensor, |
| 246 | MATMUL_OP_ADDITION, -127, 127, true, false, true, |
| 247 | nullptr, &partial_ztensor)); |
| 248 | |
| 249 | // each stack of the partial results is read back as its own 2D ztensor |
| 250 | zdnn_tensor_desc stack_pre_tfm_desc; |
| 251 | zdnn_tensor_desc stack_tfm_desc; |
| 252 | |
| 253 | zdnn_init_pre_transformed_desc(ZDNN_2D, FP32, &stack_pre_tfm_desc, ne11, ne0); |
| 254 | ZDNN_CHECK(zdnn_generate_transformed_desc(&stack_pre_tfm_desc, &stack_tfm_desc)); |
| 255 | |
| 256 | const uint64_t stack_bytes = zdnn_getsize_ztensor(&stack_tfm_desc); |
| 257 | GGML_ASSERT(partial_ztensor.buffer_size == 2*stack_bytes); |
| 258 | |
| 259 | ggml_zdnn_pool_alloc<float> partial_res_alloc(ctx->pool, ne11 * ne0); |
| 260 | float * partial[2] = { (float *)output->data, partial_res_alloc.get() }; |
| 261 | |
| 262 | ggml_zdnn_parallel_for(n_threads, 2, [&](int64_t s_start, int64_t s_end) { |
| 263 | zdnn_ztensor stack_ztensor; |
| 264 | zdnn_init_ztensor(&stack_pre_tfm_desc, &stack_tfm_desc, &stack_ztensor); |
| 265 | |
| 266 | for (int64_t s = s_start; s < s_end; s++) { |
| 267 | stack_ztensor.buffer = (char *)partial_ztensor.buffer + s*stack_bytes; |
| 268 | stack_ztensor.buffer_size = stack_bytes; |
| 269 | stack_ztensor.is_transformed = true; |
| 270 | ZDNN_CHECK(zdnn_transform_origtensor(&stack_ztensor, partial[s])); |
| 271 | } |
| 272 | }); |
| 273 | |
| 274 | // the result is written to output->data directly, so the output ztensor is stale |
| 275 | output_extra->ztensor.is_transformed = false; |
| 276 | |
| 277 | const float * weights_scales = weights_extra->scales.data(); |
| 278 | ggml_zdnn_parallel_for(n_threads, ne1, [&](int64_t i1_start, int64_t i1_end) { |
| 279 | for (int64_t i1 = i1_start; i1 < i1_end; i1++) { |
| 280 | const float sa = inputs_scales[i1]; |
| 281 | const float * pr = partial[1] + i1*ne0; |
| 282 | float * y = partial[0] + i1*ne0; |
| 283 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 284 | y[i0] = sa * weights_scales[i0] * (y[i0] + pr[i0] * (1.0f/254.0f)); |
| 285 | } |
| 286 | } |
| 287 | }); |
| 288 | } |