okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-dsv41-rel (0be62ccad) into v0.2.3.1: DeepSeek V4.1-Flash on top of the released 0.2.3 line28ce98bf1

main
1.8 KiB33 linesraw
1// src/kernels/cpu/tq_computed.hpp - the LUT-free ("computed") codebook of the experimental trellis types TQK5X / TQK4X
2// (version 2): a state's four weights come from two 32-bit LCG words instead of the hashed 2048-point table.
3//
4//   r_i = ((s * A_i + B_i) & 0x8FFF8FFF) ^ 0x3B603B60        i = 0, 1   (s = the 15-bit state)
5//   weights 0, 1 = the low and high fp16 of r_0, weights 2, 3 = those of r_1
6//
7// (QTIP's 3INST word, Tseng et al., Cornell-RelaxML/qtip lib/codebook/bitshift.py; the variant "3single" of the
8// f-dsv41-cudalab study, which measured it at the SQNR of the table on real DeepSeek experts and a GEMV without
9// shared-memory lookups.)  The mask fixes the top exponent bits, so |w| lies in [0.125, 2) and the int16 table value
10// round(w * 2^13) used by the CPU kernels is an exact integer (1024..16383): decoder and encoder agree to the bit.
11// The existing types (TQ2_T, TQK6, TQK7) and the table-based K5 / K4 (ids 226 / 227, "version 1") do not use it.
12#pragma once
13
14#include <cstdint>
15
16#include "tq_states.hpp"
17
18namespace strata::kernels::cpu::tq_detail {
19
20inline constexpr uint32_t kCompA[2] = {89226354u, 0x9E3779B1u};
21inline constexpr uint32_t kCompB[2] = {64248484u, 0x7F4A7C15u};
22inline constexpr uint32_t kCompMask = 0x8FFF8FFFu, kCompXor = 0x3B603B60u;
23
24inline uint32_t comp_word(uint32_t s, int i) { return ((s * kCompA[i] + kCompB[i]) & kCompMask) ^ kCompXor; }
25
26/// The four weights of state s at scale 2^13 (int16, exact).
27inline void comp_state4(uint32_t s, int16_t out[4]) {
28    const uint32_t r0 = comp_word(s & 0x7fffu, 0), r1 = comp_word(s & 0x7fffu, 1);
29    const uint16_t h[4] = {(uint16_t) (r0 & 0xffffu), (uint16_t) (r0 >> 16), (uint16_t) (r1 & 0xffffu), (uint16_t) (r1 >> 16)};
30    for (int j = 0; j < 4; ++j) out[j] = (int16_t) (tq_half_to_float(h[j]) * 8192.0f);
31}
32
33}  // namespace strata::kernels::cpu::tq_detail