[HIP] loop-controller kernels (RMS pack + gain/forget apply) verified

- k_ctrl_pack / vetta_ctrl_pack: exact strata k1_loop_ctrl_rms +
  k1_loop_ctrl_pack_in_bf16 — inv_c=rsqrt(mean(cur^2)+eps),
  inv_m=rsqrt(mean(msg^2)+eps); packed BF16 [2D] = [cur*inv_c ; msg*inv_m].
- k_ctrl_apply / vetta_ctrl_apply: exact k1_loop_ctrl_apply_out —
  out = (1-tanh(forget))*cur + tanh(gain)*msg (gain/forget gating of the loop
  message). The down/gain/forget low-rank projections reuse gemm_bf16.
- loop_controller_parity_vs_cpu: full controller chain (pack -> down GEMM
  [gate_rank] -> cast -> gain/forget GEMM [D] -> apply) vs a CPU oracle:
  max_rel_err 1.6e-7. The loop controller composes correctly.

This is the last kernel needed for the full Inkling forward. All mechanisms
(SWA + full-attn attention, MoE NVFP4, deep-residual block-mix, shortconv
static/dyn, loop controllers) are now verified microkernels.

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (5 files) green; GPU 17/17 (--features hip).
This commit is contained in:
adm committed 2026-12-01 15:07:02 +01:00
1 parent 5ae2072981
commit 38ac957b26
3 files changed
+129

No files matched your search

+8
View File
@@ -121,6 +121,14 @@ int vetta_kv_store(const float* k_in, const float* v_in, float* k_cache,
// k_softcap: y = cap * tanh(x / cap) (final logit softcapping).
int vetta_softcap(const float* x, float* y, size_t n, float cap, vetta_stream_t stream);
// k_ctrl_pack: RMS-normalize cur & msg -> packed BF16 [2D] (loop controller input).
int vetta_ctrl_pack(const float* cur, const float* msg, uint16_t* packed, int D,
float eps, vetta_stream_t stream);
// k_ctrl_apply: out = (1-tanh(forget))*cur + tanh(gain)*msg (loop gating).
int vetta_ctrl_apply(const float* cur, const float* msg, const float* gain,
const float* forget, float* out, size_t n, vetta_stream_t stream);
// Core <15 microkernel entry points (implemented progressively in M1 & M2):
// 1. k_embed_bf16
// 2. k_rms_norm
+57
View File
@@ -856,4 +856,61 @@ extern "C" int vetta_softcap(const float* x, float* y, size_t n, float cap, vett
k_softcap<<<(n + nt - 1) / nt, nt, 0, s>>>(x, y, n, cap);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_ctrl_pack: loop-controller input pack (RMS-normalize cur & msg) -----
// Exact strata k1_loop_ctrl_rms + k1_loop_ctrl_pack_in_bf16: inv_c =
// rsqrt(mean(cur^2)+eps), inv_m = rsqrt(mean(msg^2)+eps); packed_bf16 [2D] =
// [cur*inv_c ; msg*inv_m] (BF16 truncate). One block per token.
__global__ void k_ctrl_pack(const float* __restrict__ cur, const float* __restrict__ msg,
uint16_t* __restrict__ packed, int D, float eps) {
const int tid = threadIdx.x;
const int nt = blockDim.x;
__shared__ float red[256];
__shared__ float inv_c_s, inv_m_s;
float ssc = 0.f, ssm = 0.f;
for (int d = tid; d < D; d += nt) { ssc += cur[d] * cur[d]; ssm += msg[d] * msg[d]; }
red[tid] = ssc; __syncthreads();
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
if (tid == 0) inv_c_s = rsqrtf(red[0] / (float)D + eps);
__syncthreads();
red[tid] = ssm; __syncthreads();
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
if (tid == 0) inv_m_s = rsqrtf(red[0] / (float)D + eps);
__syncthreads();
const float ic = inv_c_s, im = inv_m_s;
for (int d = tid; d < D; d += nt) {
packed[d] = (uint16_t)(__float_as_uint(cur[d] * ic) >> 16);
packed[D + d] = (uint16_t)(__float_as_uint(msg[d] * im) >> 16);
}
}
extern "C" int vetta_ctrl_pack(const float* cur, const float* msg, uint16_t* packed,
int D, float eps, vetta_stream_t stream) {
if (D <= 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_ctrl_pack<<<1, 256, 0, s>>>(cur, msg, packed, D, eps);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_ctrl_apply: out = (1 - tanh(forget))*cur + tanh(gain)*msg -----------
// Exact strata k1_loop_ctrl_apply_out (gain/forget gating of the loop message).
__global__ void k_ctrl_apply(const float* __restrict__ cur, const float* __restrict__ msg,
const float* __restrict__ gain, const float* __restrict__ forget,
float* __restrict__ out, size_t n) {
size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) out[i] = (1.f - tanhf(forget[i])) * cur[i] + tanhf(gain[i]) * msg[i];
}
extern "C" int vetta_ctrl_apply(const float* cur, const float* msg, const float* gain,
const float* forget, float* out, size_t n, vetta_stream_t stream) {
if (n == 0) return 0;
const int nt = 256;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_ctrl_apply<<<(n + nt - 1) / nt, nt, 0, s>>>(cur, msg, gain, forget, out, n);
HIP_CHECK(hipGetLastError());
return 0;
}