ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-zdnn / utils.cpp
| 1 | #include "ggml.h" |
| 2 | #include "utils.hpp" |
| 3 | |
| 4 | #include <algorithm> |
| 5 | #include <cmath> |
| 6 | #include <cstdlib> |
| 7 | |
| 8 | zdnn_data_types ggml_zdnn_type_mapping(ggml_type type) { |
| 9 | switch (type) { |
| 10 | case GGML_TYPE_F32: |
| 11 | return FP32; |
| 12 | case GGML_TYPE_F16: |
| 13 | return FP16; |
| 14 | case GGML_TYPE_BF16: |
| 15 | return BFLOAT; |
| 16 | case GGML_TYPE_I8: |
| 17 | return INT8; |
| 18 | case GGML_TYPE_I32: |
| 19 | return INT32; |
| 20 | default: |
| 21 | GGML_ABORT("%s: fatal: unable to determine zTensor data type", |
| 22 | __func__); |
| 23 | break; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | void ggml_zdnn_create_tensor(zdnn_tensor_desc & pre_tfm_desc, |
| 28 | zdnn_tensor_desc & tfm_desc, |
| 29 | zdnn_ztensor & ztensor, |
| 30 | const ggml_tensor * src, |
| 31 | const int64_t * ne, |
| 32 | const zdnn_data_layouts layout) { |
| 33 | zdnn_init_pre_transformed_desc( |
| 34 | layout, |
| 35 | ggml_zdnn_type_mapping(src->type), |
| 36 | &pre_tfm_desc, |
| 37 | ne[3], ne[2], ne[1], ne[0] |
| 38 | ); |
| 39 | |
| 40 | ZDNN_CHECK(zdnn_generate_transformed_desc(&pre_tfm_desc, &tfm_desc)); |
| 41 | ZDNN_CHECK(zdnn_init_ztensor_with_malloc(&pre_tfm_desc, &tfm_desc, &ztensor)); |
| 42 | } |
| 43 | |
| 44 | void ggml_zdnn_load_tensor(zdnn_ztensor & ztensor, void * buffer) { |
| 45 | ZDNN_CHECK(zdnn_transform_ztensor(&ztensor, buffer)); |
| 46 | } |
| 47 | |
| 48 | // size of the groups along ne0 that get their own scale, or 0 for one scale per row |
| 49 | // the groups are spread evenly over ne0 and rounded up to 128 values, the last group is zero padded |
| 50 | int64_t ggml_zdnn_q_group_size(const ggml_tensor * tensor) { |
| 51 | // benchmarked on Llama-3.2-1B and Qwen2.5-1.5B Q8_0 with the channel scales: |
| 52 | // 2048 is 15-18% faster than 1024, and 1024 lowers the KL divergence to the CPU by 13-19% |
| 53 | // GGML_ZDNN_HIGH_PREC=1 selects 1024 for users that want the lower loss |
| 54 | // always re-measure with different models to see if different sizes need a dispatching logic |
| 55 | static const bool high_prec = std::getenv("GGML_ZDNN_HIGH_PREC") != nullptr && |
| 56 | std::atoi(std::getenv("GGML_ZDNN_HIGH_PREC")) != 0; |
| 57 | const int64_t group_size_max = high_prec ? 1024 : 2048; |
| 58 | |
| 59 | const int64_t ne0 = tensor->ne[0]; |
| 60 | if (ne0 <= group_size_max) { |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | const int64_t n_groups = (ne0 + group_size_max - 1) / group_size_max; |
| 65 | return GGML_PAD((ne0 + n_groups - 1) / n_groups, 128); |
| 66 | } |
| 67 | |
| 68 | void ggml_zdnn_load_tensor_q(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor) { |
| 69 | const int64_t ne0 = tensor->ne[0]; |
| 70 | const int64_t ne1 = tensor->ne[1]; |
| 71 | |
| 72 | const ggml_to_float_t to_float = ggml_get_type_traits(tensor->type)->to_float; |
| 73 | GGML_ASSERT(to_float != nullptr); |
| 74 | |
| 75 | // zDNN takes one scale per zTensor, so requantize each group of each row to int8 with its own scale |
| 76 | // a group is the whole row, unless the weights are stacked into groups along ne0 |
| 77 | // rows are written transposed as zDNN computes inputs (ne11, ne0) x weights (ne0, ne1) |
| 78 | const bool stacked = buffer->pre_tfm_desc.layout == ZDNN_3DS; |
| 79 | const int64_t n_groups = stacked ? buffer->pre_tfm_desc.dim3 : 1; |
| 80 | const int64_t gs = stacked ? buffer->pre_tfm_desc.dim2 : ne0; |
| 81 | |
| 82 | // benchmarked: 256 rows fill a 256-byte cache line per write, padding each row by a line avoids cache set conflicts |
| 83 | constexpr int64_t n_tile = 256; |
| 84 | const int64_t x_stride = ne0 + 64; |
| 85 | |
| 86 | // the rows past ne0 of the last group stay zero as padding |
| 87 | std::vector<int8_t> q(n_groups * gs * ne1); |
| 88 | std::vector<float> x(n_tile * x_stride); |
| 89 | std::vector<float> id(n_tile * n_groups); |
| 90 | |
| 91 | // a few input channels with larger weights than the rest would set the scale of every group, |
| 92 | // so divide each input channel by its RMS over the rows and multiply the inputs by it instead |
| 93 | // with groups of 1024, this lowered the KL divergence to the CPU by 10-18% on Llama-3.2-1B and Qwen2.5-1.5B Q8_0 at the same speed |
| 94 | std::vector<double> sum_sq(ne0, 0.0); |
| 95 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 96 | to_float((const char *)tensor->data + i1*tensor->nb[1], x.data(), ne0); |
| 97 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 98 | sum_sq[i0] += (double)x[i0] * x[i0]; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | std::vector<float> channel_id(ne0); |
| 103 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 104 | const float c = (float)std::sqrt(sum_sq[i0] / ne1); |
| 105 | buffer->channel_scales[i0] = c ? c : 1.0f; |
| 106 | channel_id[i0] = 1.0f / buffer->channel_scales[i0]; |
| 107 | } |
| 108 | |
| 109 | for (int64_t i1 = 0; i1 < ne1; i1 += n_tile) { |
| 110 | const int64_t nt = std::min(n_tile, ne1 - i1); |
| 111 | |
| 112 | for (int64_t t = 0; t < nt; t++) { |
| 113 | float * xt = x.data() + t*x_stride; |
| 114 | to_float((const char *)tensor->data + (i1 + t)*tensor->nb[1], xt, ne0); |
| 115 | |
| 116 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 117 | xt[i0] *= channel_id[i0]; |
| 118 | } |
| 119 | |
| 120 | for (int64_t g = 0; g < n_groups; g++) { |
| 121 | float amax = 0.0f; |
| 122 | for (int64_t i0 = g*gs; i0 < std::min((g + 1)*gs, ne0); i0++) { |
| 123 | amax = std::max(amax, fabsf(xt[i0])); |
| 124 | } |
| 125 | |
| 126 | const float d = amax / 127.0f; |
| 127 | buffer->scales[g*ne1 + i1 + t] = d; |
| 128 | id[t*n_groups + g] = d ? 1.0f/d : 0.0f; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 133 | const int64_t g = i0 / gs; |
| 134 | int8_t * qi = q.data() + i0*ne1 + i1; |
| 135 | for (int64_t t = 0; t < nt; t++) { |
| 136 | qi[t] = (int8_t)roundf(x[t*x_stride + i0] * id[t*n_groups + g]); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (buffer->ztensor.is_transformed) { |
| 142 | zdnn_reset_ztensor(&buffer->ztensor); |
| 143 | } |
| 144 | ZDNN_CHECK(zdnn_transform_quantized_ztensor(&buffer->ztensor, false, -127, 127, q.data())); |
| 145 | } |
| 146 | |
| 147 | void ggml_zdnn_init_tensor(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor) { |
| 148 | if (ggml_is_quantized(tensor->type)) { |
| 149 | GGML_ASSERT(ggml_is_matrix(tensor)); |
| 150 | |
| 151 | const int64_t gs = ggml_zdnn_q_group_size(tensor); |
| 152 | |
| 153 | if (gs > 0) { |
| 154 | // stack the weights into groups along ne0, each group of each row gets its own scale |
| 155 | const int64_t n_groups = (tensor->ne[0] + gs - 1) / gs; |
| 156 | |
| 157 | zdnn_init_pre_transformed_desc( |
| 158 | ZDNN_3DS, |
| 159 | INT8, |
| 160 | &buffer->pre_tfm_desc, |
| 161 | n_groups, gs, tensor->ne[1] |
| 162 | ); |
| 163 | |
| 164 | buffer->scales.resize(n_groups * tensor->ne[1]); |
| 165 | } else { |
| 166 | zdnn_init_pre_transformed_desc( |
| 167 | ZDNN_2D, |
| 168 | INT8, |
| 169 | &buffer->pre_tfm_desc, |
| 170 | tensor->ne[0], tensor->ne[1] |
| 171 | ); |
| 172 | |
| 173 | buffer->scales.resize(tensor->ne[1]); |
| 174 | } |
| 175 | |
| 176 | buffer->channel_scales.resize(tensor->ne[0]); |
| 177 | |
| 178 | ZDNN_CHECK(zdnn_generate_quantized_transformed_desc(&buffer->pre_tfm_desc, QUANTIZED_WEIGHTS_INT8, &buffer->tfm_desc)); |
| 179 | ZDNN_CHECK(zdnn_init_quantized_ztensor_with_malloc(&buffer->pre_tfm_desc, &buffer->tfm_desc, 1.0f, 0.0f, &buffer->ztensor)); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | switch (tensor->op) { |
| 184 | case GGML_OP_MUL_MAT: |
| 185 | { |
| 186 | zdnn_init_pre_transformed_desc( |
| 187 | ZDNN_2D, |
| 188 | ggml_zdnn_type_mapping(tensor->type), |
| 189 | &buffer->pre_tfm_desc, |
| 190 | tensor->ne[1], tensor->ne[0] |
| 191 | ); |
| 192 | } break; |
| 193 | |
| 194 | default: |
| 195 | { |
| 196 | // For 4D tensors, GGML uses NCHW layout. However, because zDNN |
| 197 | // automatically transforms everything to NHWC, we will use it |
| 198 | // directly to avoid the performance penalty changing the |
| 199 | // layout and reshaping the tensor. |
| 200 | zdnn_init_pre_transformed_desc( |
| 201 | ZDNN_NHWC, |
| 202 | ggml_zdnn_type_mapping(tensor->type), |
| 203 | &buffer->pre_tfm_desc, |
| 204 | tensor->ne[3], tensor->ne[2], tensor->ne[1], tensor->ne[0] |
| 205 | ); |
| 206 | |
| 207 | // TODO: Consider adding a ggml check. |
| 208 | // TODO: If tensor = 4D, use ZDNN_NCHW by default. |
| 209 | // TODO: If tensor = 2D, use ZDNN_NHWC by default. |
| 210 | } break; |
| 211 | } |
| 212 | |
| 213 | ZDNN_CHECK(zdnn_generate_transformed_desc(&buffer->pre_tfm_desc, &buffer->tfm_desc)); |
| 214 | ZDNN_CHECK(zdnn_init_ztensor_with_malloc(&buffer->pre_tfm_desc, &buffer->tfm_desc, &buffer->ztensor)); |
| 215 | } |