Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-zdnn/quantize.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-zdnn / quantize.cpp` 58 lines ยท 2286 bytes Raw Blame History 1 #include "ggml.h" 2 #include "quantize.hpp" 3 4 #include 5 #include 6 7 // quantize each group of group_size values per row to int8, plus the rounding error into residuals for ~15 bits 8 void ggml_zdnn_quantize_inputs(int32_t n_threads, 9 const ggml_tensor * inputs, 10 int64_t group_size, 11 const float * channel_scales, 12 int8_t * quants, 13 int8_t * residuals, 14 float * scales) { 15 16 const int64_t ne0 = inputs->ne[0]; 17 const int64_t ne1 = inputs->ne[1]; 18 const int64_t n_groups = (ne0 + group_size - 1) / group_size; 19 20 ggml_zdnn_parallel_for(n_threads, ne1, [&](int64_t i1_start, int64_t i1_end) { 21 for (int64_t i1 = i1_start; i1 < i1_end; i1++) { 22 const float * xi = (const float *)((const char *)inputs->data + i1*inputs->nb[1]); 23 24 for (int64_t g = 0; g < n_groups; g++) { 25 const float * xg = xi + g*group_size; 26 const float * cg = channel_scales + g*group_size; 27 const int64_t n = std::min(group_size, ne0 - g*group_size); 28 29 float amax = 0.0f; 30 for (int64_t i0 = 0; i0 < n; i0++) { 31 amax = std::max(amax, fabsf(xg[i0] * cg[i0])); 32 } 33 34 // the rounding error is at most d/2 35 const float d = amax / 127.0f; 36 const float id = d ? 1.0f/d : 0.0f; 37 const float id_res = 254.0f * id; 38 scales[g*ne1 + i1] = d; 39 40 int8_t * qg = quants + (g*ne1 + i1)*group_size; 41 int8_t * rg = residuals + (g*ne1 + i1)*group_size; 42 43 for (int64_t i0 = 0; i0 < n; i0++) { 44 const float xc = xg[i0] * cg[i0]; 45 const float v = roundf(xc * id); 46 47 qg[i0] = (int8_t)v; 48 rg[i0] = (int8_t)std::clamp(roundf((xc - v * d) * id_res), -127.0f, 127.0f); 49 } 50 51 for (int64_t i0 = n; i0 < group_size; i0++) { 52 qg[i0] = 0; 53 rg[i0] = 0; 54 } 55 } 56 } 57 }); 58 }