#pragma once #include #include #include #include #include #include namespace strata::ds4::scr_cpu { inline float e4value(unsigned q) { unsigned e = q >> 3, m = q & 7; return e == 0 ? m * 0.001953125f : std::ldexp(1.0f + m / 8.0f, (int) e - 7); } inline uint8_t e4code(float x) { unsigned best = 0; float dist = std::fabs(x); for (unsigned q = 1; q < 127; ++q) { float d = std::fabs(e4value(q) - x); if (d < dist || (d == dist && (q & 1) == 0)) { best = q; dist = d; } } return (uint8_t) best; } inline uint8_t e2code(float x) { const float a = std::fabs(x); const unsigned m = a <= .25f ? 0 : a < .75f ? 1 : a <= 1.25f ? 2 : a < 1.75f ? 3 : a <= 2.5f ? 4 : a < 3.5f ? 5 : a <= 5.f ? 6 : 7; return (uint8_t) (m | (std::signbit(x) ? 8 : 0)); } inline size_t row_bytes(int fmt, int len) { return ((size_t) len / 2 + len / (fmt == 2 ? 16 : 32) + 15) / 16 * 16; } inline void geometry(int fmt, int len, int rd) { if ((fmt != 2 && fmt != 3) || rd <= 0 || rd > len || len % 32 || rd % 32) throw std::invalid_argument("SCR requires whole RoPE quantization groups"); } // Fresh finish without normalization: the input is the F32 activation captured after norm. inline std::vector fresh(std::vector x, int rd, int fmt, int pos, const std::vector& freqs) { const int len = (int) x.size(), group = fmt == 2 ? 16 : 32; geometry(fmt, len, rd); for (int i = 0; i < rd / 2; ++i) { const float angle = (float) pos * freqs.at((size_t) i), c = std::cos(angle), s = std::sin(angle); const size_t k = (size_t) (len - rd + 2 * i); const float a = x[k], b = x[k + 1]; x[k] = std::fma(-b, s, a * c); x[k + 1] = std::fma(a, s, b * c); } std::vector out(row_bytes(fmt, len), 0); for (int k = 0; k < len; k += group) { float a = 0; for (int j = 0; j < group; ++j) a = std::max(a, std::fabs(x[(size_t) k + j])); float scale; uint8_t code; if (fmt == 2) { code = e4code(std::max(a, 6.f * .001953125f) / 6.f); scale = e4value(code); } else { const float v = std::max(a, 6.f * 1.1754943508222875e-38f) * (1.f / 6.f); int e; const float m = std::frexp(v, &e); scale = std::ldexp(1.f, e - (m == .5f ? 1 : 0)); uint32_t bits; std::memcpy(&bits, &scale, 4); code = (uint8_t) (bits >> 23); } out[(size_t) len / 2 + k / group] = code; for (int j = 0; j < group; j += 2) { const uint8_t lo = e2code(std::clamp(x[(size_t) k + j] / scale, -6.f, 6.f)); const uint8_t hi = e2code(std::clamp(x[(size_t) k + j + 1] / scale, -6.f, 6.f)); out[((size_t) k + j) / 2] = (uint8_t) (lo | hi << 4); } } return out; } inline std::vector relocate(std::vector packed, const std::vector& master, int len, int fmt, int pos, const std::vector& freqs) { const int rd = (int) master.size(), group = fmt == 2 ? 16 : 32; geometry(fmt, len, rd); if (packed.size() != row_bytes(fmt, len)) throw std::invalid_argument("SCR packed row size"); auto tail = fresh(master, rd, fmt, pos, freqs); std::copy_n(tail.begin(), rd / 2, packed.begin() + (len - rd) / 2); std::copy_n(tail.begin() + rd / 2, rd / group, packed.begin() + len / 2 + (len - rd) / group); return packed; } } // namespace strata::ds4::scr_cpu