#include "format/planes.h" #include #include #include #include "common/base.h" #include "common/numeric.h" namespace hk { const float kE2M1[8] = {0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f}; void planes_set_mxfp4(PlanesDesc& d) { d.nplanes = 4; d.group = 32; d.scale = ScaleKind::E8M0; for (int i = 0; i < 8; ++i) d.lut[i] = kE2M1[i]; } void planes_decode_row(const PlanesDesc& d, const PlanesView& v, int r, float* out) { const int words = d.cols / 32; const int np = d.nplanes; const int rpb = d.row_plane_bytes(); const int words_per_group = d.group / 32; const uint8_t* pl[4]; for (int p = 0; p < np; ++p) pl[p] = v.planes[p] + (int64_t)r * rpb; const uint8_t* sc = v.scales + (int64_t)r * d.row_scales() * (d.scale == ScaleKind::E8M0 ? 1 : 2); for (int w = 0; w < words; ++w) { int g = w / words_per_group; float s = d.scale == ScaleKind::E8M0 ? e8m0_to_f32(sc[g]) : f16_to_f32(load_le16(sc + 2 * g)); uint32_t sign = load_le32(pl[0] + 4 * w); uint32_t m1 = np > 1 ? load_le32(pl[1] + 4 * w) : 0; uint32_t m2 = np > 2 ? load_le32(pl[2] + 4 * w) : 0; uint32_t m3 = np > 3 ? load_le32(pl[3] + 4 * w) : 0; float* o = out + 32 * w; // magnitude code: plane 1 is the most significant bit switch (np) { case 1: for (int j = 0; j < 32; ++j) o[j] = ((sign >> j) & 1u) ? -d.lut[0] * s : d.lut[0] * s; break; case 2: for (int j = 0; j < 32; ++j) { float mag = d.lut[(m1 >> j) & 1u] * s; o[j] = ((sign >> j) & 1u) ? -mag : mag; } break; case 3: for (int j = 0; j < 32; ++j) { float mag = d.lut[(((m1 >> j) & 1u) << 1) | ((m2 >> j) & 1u)] * s; o[j] = ((sign >> j) & 1u) ? -mag : mag; } break; default: for (int j = 0; j < 32; ++j) { float mag = d.lut[(((m1 >> j) & 1u) << 2) | (((m2 >> j) & 1u) << 1) | ((m3 >> j) & 1u)] * s; o[j] = ((sign >> j) & 1u) ? -mag : mag; } } } } // v[b] packs, for one stored byte (two nibbles), two bits for each of the four planes: // bits [2p, 2p+1] = plane p bit of the low nibble, then of the high nibble. Built at // compile time: the converter calls this from several threads, so nothing may be // filled in at run time. namespace { struct NibbleTable { uint8_t v[256]; constexpr NibbleTable() : v() { for (int b = 0; b < 256; ++b) { const int lo = b & 0xF, hi = b >> 4; int t = 0; for (int p = 0; p < 4; ++p) { const int bit = 3 - p; // plane 0 = sign = nibble bit 3 t |= (((lo >> bit) & 1) | (((hi >> bit) & 1) << 1)) << (2 * p); } v[b] = (uint8_t)t; } } }; constexpr NibbleTable kNibbleTable{}; // a wrong entry is a build error, not a wrong store static_assert(kNibbleTable.v[0x00] == 0x00 && kNibbleTable.v[0xFF] == 0xFF && kNibbleTable.v[0x08] == 0x01 && kNibbleTable.v[0x80] == 0x02 && kNibbleTable.v[0x01] == 0x40 && kNibbleTable.v[0x10] == 0x80, "the nibble table is wrong"); } // namespace void mxfp4_nibbles_to_planes(const uint8_t* packed, int rows, int cols, uint8_t* p0, uint8_t* p1, uint8_t* p2, uint8_t* p3) { HK_CHECK(cols % 8 == 0, "MXFP4 matrix width %d is not a multiple of 8", cols); const uint8_t* tab = kNibbleTable.v; uint8_t* out[4] = {p0, p1, p2, p3}; const int64_t nbytes = (int64_t)rows * (cols / 8); // output bytes per plane; 4 input bytes each for (int64_t i = 0; i < nbytes; ++i) { const uint8_t* in = packed + 4 * i; uint8_t t0 = tab[in[0]], t1 = tab[in[1]], t2 = tab[in[2]], t3 = tab[in[3]]; for (int p = 0; p < 4; ++p) { int sh = 2 * p; out[p][i] = (uint8_t)(((t0 >> sh) & 3) | (((t1 >> sh) & 3) << 2) | (((t2 >> sh) & 3) << 4) | (((t3 >> sh) & 3) << 6)); } } } void mxfp4_planes_to_nibbles(const uint8_t* p0, const uint8_t* p1, const uint8_t* p2, const uint8_t* p3, int rows, int cols, uint8_t* packed) { const int64_t n = (int64_t)rows * cols; memset(packed, 0, (size_t)(n / 2)); for (int64_t j = 0; j < n; ++j) { int bit = (int)(j & 7); int64_t byte = j >> 3; int code = (((p0[byte] >> bit) & 1) << 3) | (((p1[byte] >> bit) & 1) << 2) | (((p2[byte] >> bit) & 1) << 1) | ((p3[byte] >> bit) & 1); packed[j >> 1] |= (uint8_t)(code << ((j & 1) ? 4 : 0)); } } void mxfp4_accumulate_hist(const uint8_t* packed, const uint8_t* scales, int rows, int cols, double hist[8]) { const int groups = cols / 32; uint32_t count[8]; for (int r = 0; r < rows; ++r) { for (int g = 0; g < groups; ++g) { float s = e8m0_to_f32(scales[(int64_t)r * groups + g]); if (s == 0.0f) continue; const uint8_t* in = packed + ((int64_t)r * cols + 32 * g) / 2; memset(count, 0, sizeof(count)); for (int i = 0; i < 16; ++i) { ++count[in[i] & 7]; ++count[(in[i] >> 4) & 7]; } double s2 = (double)s * (double)s; for (int c = 0; c < 8; ++c) hist[c] += s2 * count[c]; } } } void mxfp4_truncated_lut(const double hist[8], int nplanes, float lut[8]) { HK_CHECK(nplanes >= 1 && nplanes <= 4, "nplanes must be 1..4"); for (int i = 0; i < 8; ++i) lut[i] = 0.0f; int kept = nplanes - 1; // magnitude bits kept int drop = 3 - kept; // trailing magnitude bits dropped int classes = 1 << kept; for (int t = 0; t < classes; ++t) { double num = 0, den = 0; for (int c = t << drop; c < ((t + 1) << drop); ++c) { num += hist[c] * kE2M1[c]; den += hist[c]; } if (den > 0) { lut[t] = (float)(num / den); } else { // no data: plain midpoint of the class double sum = 0; for (int c = t << drop; c < ((t + 1) << drop); ++c) sum += kE2M1[c]; lut[t] = (float)(sum / (1 << drop)); } } } // nearest E2M1 magnitude for x >= 0. Midpoints: .25 .75 1.25 1.75 2.5 3.5 5. static inline int e2m1_nearest(float x) { if (x < 0.25f) return 0; if (x < 0.75f) return 1; if (x < 1.25f) return 2; if (x < 1.75f) return 3; if (x < 2.5f) return 4; if (x < 3.5f) return 5; if (x < 5.0f) return 6; return 7; } void mxfp4_quantize_row(const float* w, int cols, uint8_t* scales, uint8_t* packed) { HK_CHECK(cols % 32 == 0, "MXFP4 needs a width that is a multiple of 32, got %d", cols); memset(packed, 0, (size_t)(cols / 2)); for (int g = 0; g < cols / 32; ++g) { const float* x = w + 32 * g; float amax = 0; for (int j = 0; j < 32; ++j) amax = std::max(amax, std::fabs(x[j])); if (!(amax > 0) || !std::isfinite(amax)) { scales[g] = 0; continue; } // Smallest power of two with amax / scale <= 6, then one step finer too (clipping the // largest weight can lower the total error). int e_hi = (int)std::ceil(std::log2((double)amax / 6.0)); int best_e = e_hi; double best_err = -1; for (int e = e_hi; e >= e_hi - 1; --e) { int biased = e + 127; if (biased < 1 || biased > 254) continue; float s = e8m0_to_f32((uint8_t)biased); double err = 0; for (int j = 0; j < 32; ++j) { float q = kE2M1[e2m1_nearest(std::fabs(x[j]) / s)] * s; double d = (double)std::fabs(x[j]) - q; err += d * d; } if (best_err < 0 || err < best_err) { best_err = err; best_e = e; } } int biased = std::min(254, std::max(1, best_e + 127)); scales[g] = (uint8_t)biased; float s = e8m0_to_f32((uint8_t)biased); for (int j = 0; j < 32; ++j) { int code = e2m1_nearest(std::fabs(x[j]) / s); if (x[j] < 0 && code != 0) code |= 8; int idx = 32 * g + j; packed[idx >> 1] |= (uint8_t)(code << ((idx & 1) ? 4 : 0)); } } } } // namespace hk