| 1973 | // ====================== 5-bit (de)-quantization |
| 1974 | |
| 1975 | void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) { |
| 1976 | assert(k % QK_K == 0); |
| 1977 | const int nb = k / QK_K; |
| 1978 | |
| 1979 | #if QK_K == 256 |
| 1980 | uint8_t L[QK_K]; |
| 1981 | float mins[QK_K/32]; |
| 1982 | float scales[QK_K/32]; |
| 1983 | float weights[32]; |
| 1984 | uint8_t Laux[32]; |
| 1985 | #else |
| 1986 | int8_t L[QK_K]; |
| 1987 | float scales[QK_K/16]; |
| 1988 | #endif |
| 1989 | |
| 1990 | for (int i = 0; i < nb; i++) { |
| 1991 | |
| 1992 | #if QK_K == 256 |
| 1993 | |
| 1994 | float max_scale = 0; // as we are deducting the min, scales are always positive |
| 1995 | float max_min = 0; |
| 1996 | for (int j = 0; j < QK_K/32; ++j) { |
| 1997 | //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f); |
| 1998 | float sum_x2 = 0; |
| 1999 | for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l]; |
| 2000 | float av_x = sqrtf(sum_x2/32); |
| 2001 | for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]); |
| 2002 | scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false); |
| 2003 | float scale = scales[j]; |
| 2004 | if (scale > max_scale) { |
| 2005 | max_scale = scale; |
| 2006 | } |
| 2007 | float min = mins[j]; |
| 2008 | if (min > max_min) { |
| 2009 | max_min = min; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f; |
| 2014 | float inv_min = max_min > 0 ? 63.f/max_min : 0.f; |
| 2015 | for (int j = 0; j < QK_K/32; ++j) { |
| 2016 | uint8_t ls = nearest_int(inv_scale*scales[j]); |
| 2017 | uint8_t lm = nearest_int(inv_min*mins[j]); |
| 2018 | ls = MIN(63, ls); |
| 2019 | lm = MIN(63, lm); |
| 2020 | if (j < 4) { |
| 2021 | y[i].scales[j] = ls; |
| 2022 | y[i].scales[j+4] = lm; |
| 2023 | } else { |
| 2024 | y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4); |
| 2025 | y[i].scales[j-4] |= ((ls >> 4) << 6); |
| 2026 | y[i].scales[j-0] |= ((lm >> 4) << 6); |
| 2027 | } |
| 2028 | } |
| 2029 | y[i].d = GGML_FP32_TO_FP16(max_scale/63.f); |
| 2030 | y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f); |
| 2031 | |
| 2032 | uint8_t sc, m; |
no test coverage detected