[HIP] AttnRes block-mix kernel — Inkling deep-residual parity (last defining mechanism)
- k_attn_res_mix / vetta_attn_res_mix: exact strata k1_attn_res_mix semantics. For each learned query q and token m, mix n_states = n_blocks+1 depth states (block snapshots + current): per state inv_rms = rsqrt(mean(x^2)+eps), score = (x.q)*inv_rms/sqrt(D); softmax over scores -> w; final weight = w*inv_rms; out = sum_s weight[s]*state_s (RMS-normalized depth mix). This is Inkling's deep residual that replaces the standard additive residual stream. Contiguous blocks [n_blocks,M,D] snapshot buffer (FFI-friendly vs ptr array). - Parity test vs CPU oracle (nb=3, M=2, nq=2, D=64): max_rel_err 1.0e-5. With this, ALL defining Inkling mechanisms are verified kernels: ShortConv4, flash SWA GQA (window+relpos+sink+gate), AttnRes block-mix, NVFP4 experts + SiTU + sigmoid router. The kernel parity foundation is complete; what remains is integration (lowering the full graph + wiring a complete layer + loop controllers + KV store + projections). Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; lean proofs (4 files, 42 theorems) green; GPU 13/13 (--features hip).
This commit is contained in:
1 parent
aea1c437d4
commit
61c0b21a93
3 files changed
+142
No files matched your search
@@ -611,4 +611,80 @@ extern "C" int vetta_shortconv4_decode(const float* cur, const float* static_w,
|
||||
k_shortconv4_decode<<<grid, 256, 0, s>>>(cur, static_w, hist, out, slot_ids, M, C);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_attn_res_mix: Inkling AttnRes deep-residual block-mix (parity) ------
|
||||
// Exact strata k1_attn_res_mix: for each query q and token m, mix n_states =
|
||||
// n_blocks+1 depth states (block snapshots + current). Per state s:
|
||||
// inv_rms[s] = rsqrt(mean(x^2) + eps); score[s] = (x.q) * inv_rms[s] / sqrt(D)
|
||||
// softmax over scores -> w[s]; final weight = w[s] * inv_rms[s];
|
||||
// out[m,q,:] = sum_s weight[s] * state_s[m,:] (RMS-normalized depth mix).
|
||||
// blocks [n_blocks, M, D] contiguous snapshots; cur [M, D]; queries [nq, D].
|
||||
|
||||
__global__ void k_attn_res_mix(const float* __restrict__ blocks,
|
||||
const float* __restrict__ cur,
|
||||
const float* __restrict__ queries,
|
||||
float* __restrict__ out,
|
||||
int n_blocks, int M, int n_queries, int D, float eps) {
|
||||
const int q = blockIdx.x;
|
||||
const int m = blockIdx.y;
|
||||
if (q >= n_queries || m >= M) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const int n_states = n_blocks + 1;
|
||||
const float inv_sqrt_d = rsqrtf((float)D);
|
||||
const float* qv = queries + (size_t)q * D;
|
||||
__shared__ float red[256];
|
||||
__shared__ float scores[16];
|
||||
__shared__ float inv_rms[16];
|
||||
|
||||
for (int st = 0; st < n_states; ++st) {
|
||||
const float* sv = (st < n_blocks) ? (blocks + (size_t)st * M * D + (size_t)m * D)
|
||||
: (cur + (size_t)m * D);
|
||||
float ss = 0.f, dot = 0.f;
|
||||
for (int d = tid; d < D; d += nt) { float x = sv[d]; ss += x * x; dot += x * qv[d]; }
|
||||
red[tid] = ss; __syncthreads();
|
||||
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
|
||||
const float tss = red[0]; __syncthreads();
|
||||
red[tid] = dot; __syncthreads();
|
||||
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
|
||||
const float tdot = red[0]; __syncthreads();
|
||||
if (tid == 0) {
|
||||
const float ir = rsqrtf(tss / (float)D + eps);
|
||||
inv_rms[st] = ir;
|
||||
scores[st] = tdot * ir * inv_sqrt_d;
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
if (tid == 0) {
|
||||
float mx = -1e30f;
|
||||
for (int st = 0; st < n_states; ++st) mx = fmaxf(mx, scores[st]);
|
||||
float sum = 0.f;
|
||||
for (int st = 0; st < n_states; ++st) { scores[st] = __expf(scores[st] - mx); sum += scores[st]; }
|
||||
const float inv = 1.f / fmaxf(sum, 1e-30f);
|
||||
for (int st = 0; st < n_states; ++st) scores[st] = scores[st] * inv * inv_rms[st];
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
float* dst = out + ((size_t)m * n_queries + q) * D;
|
||||
for (int d = tid; d < D; d += nt) {
|
||||
float acc = 0.f;
|
||||
for (int st = 0; st < n_states; ++st) {
|
||||
const float* sv = (st < n_blocks) ? (blocks + (size_t)st * M * D + (size_t)m * D)
|
||||
: (cur + (size_t)m * D);
|
||||
acc += scores[st] * sv[d];
|
||||
}
|
||||
dst[d] = acc;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_attn_res_mix(const float* blocks, const float* cur, const float* queries,
|
||||
float* out, int n_blocks, int M, int n_queries, int D,
|
||||
float eps, vetta_stream_t stream) {
|
||||
if (M <= 0 || n_queries <= 0 || D <= 0 || n_blocks < 0 || n_blocks + 1 > 16) return 1;
|
||||
dim3 grid(n_queries, M);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_attn_res_mix<<<grid, 256, 0, s>>>(blocks, cur, queries, out, n_blocks, M, n_queries, D, eps);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user