[HIP] ShortConv prefill parallel (tile 16) + update_hist: batched attention brick
k_shortconv4_prefill_par (exact strata k2_shortconv4_prefill_par): one thread per channel processes a tile of 16 contiguous tokens; 3-token history seeded from hist [3,C] (bf16-rounded f32, same ring as decode) for t0<3 else in_x bf16 round-trip. Static 1/sqrt(5) or dynamic taps [B,n_heads,4] 1/3. Replaces B sequential decode calls with one parallel launch (grid C-tiles x ceil(B/16)). k_shortconv4_update_hist: roll history <- last 3 in_x (bf16), shift for B<3. Parity vs B sequential decode steps: B=37 (partial last tile) C=64, output AND history BIT-EXACT (0.0). Note: k_attn_res_mix already supports M>1 (grid nq x M). Additive — decode default unchanged. Remaining batched attention: causal flash M=B + kv_store B positions + batched depth-blocks/controllers/bank, then forward_batch orchestration + serve wiring + EXACT parity. Verified: hip GPU 24 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
1 parent
b6113b549a
commit
ec2ad3fdc5
2 files changed
+140
No files matched your search
@@ -1191,6 +1191,86 @@ extern "C" int vetta_shortconv4_dyn_decode(const float* cur, const float* static
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_shortconv4_prefill_par: parallel causal ShortConv over B tokens ------
|
||||
// Exact strata k2_shortconv4_prefill_par. One thread per channel c processes a
|
||||
// tile of 16 contiguous tokens [t0,t0+16); the 3-token history is seeded from
|
||||
// hist [3,C] (bf16-rounded f32, same ring as decode) for t0<3 else from in_x with
|
||||
// bf16 round-trip. Static scale 1/sqrt(5), or dynamic taps [B,n_heads,4] scale 1/3.
|
||||
// This is the PREFILL ShortConv (M=B parallel), replacing B sequential decode calls.
|
||||
__global__ void __launch_bounds__(256) k_shortconv4_prefill_par(
|
||||
const float* __restrict__ in_x,
|
||||
const float* __restrict__ static_w,
|
||||
const float* __restrict__ dyn_taps,
|
||||
const float* __restrict__ hist,
|
||||
float* __restrict__ out_y,
|
||||
int B, int C, int n_heads) {
|
||||
const int c = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
const int t0 = blockIdx.y * 16;
|
||||
if (c >= C || t0 >= B) return;
|
||||
const float* sw = static_w + (size_t)c * 4;
|
||||
float x0 = (t0 >= 3) ? bf16_round(in_x[(size_t)(t0 - 3) * C + c]) : hist[(size_t)t0 * C + c];
|
||||
float x1 = (t0 >= 2) ? bf16_round(in_x[(size_t)(t0 - 2) * C + c]) : hist[(size_t)(t0 + 1) * C + c];
|
||||
float x2 = (t0 >= 1) ? bf16_round(in_x[(size_t)(t0 - 1) * C + c]) : hist[(size_t)2 * C + c];
|
||||
const bool has_dyn = (dyn_taps != nullptr && n_heads > 0);
|
||||
const int head = has_dyn ? (c / (C / n_heads)) : 0;
|
||||
const int tend = (t0 + 16 < B) ? (t0 + 16) : B;
|
||||
for (int t = t0; t < tend; ++t) {
|
||||
const float x3 = in_x[(size_t)t * C + c];
|
||||
float y = x3 + sw[0] * x0 + sw[1] * x1 + sw[2] * x2 + sw[3] * x3;
|
||||
if (has_dyn) {
|
||||
const float* dt = dyn_taps + ((size_t)t * n_heads + head) * 4;
|
||||
y += dt[0] * x3 + dt[1] * x2 + dt[2] * x1 + dt[3] * x0;
|
||||
y *= (1.0f / 3.0f);
|
||||
} else {
|
||||
y *= 0.44721359549995793f; // 1/sqrt(5)
|
||||
}
|
||||
out_y[(size_t)t * C + c] = y;
|
||||
x0 = x1; x1 = x2; x2 = bf16_round(x3);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_shortconv4_prefill_par(const float* in_x, const float* static_w,
|
||||
const float* dyn_taps, const float* hist, float* out_y,
|
||||
int B, int C, int n_heads, vetta_stream_t stream) {
|
||||
if (B <= 0 || C <= 0) return 1;
|
||||
if (dyn_taps && (n_heads <= 0 || (C % n_heads) != 0)) return 1;
|
||||
dim3 grid((C + 255) / 256, (B + 15) / 16);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_shortconv4_prefill_par<<<grid, 256, 0, s>>>(in_x, static_w, dyn_taps, hist, out_y, B, C, n_heads);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_shortconv4_update_hist: roll the 3-token history after a prefill chunk
|
||||
// Exact strata k2_shortconv4_update_hist: hist <- last 3 in_x (bf16-rounded),
|
||||
// with shift semantics for B<3. hist [3,C] f32 (bf16-rounded values).
|
||||
__global__ void k_shortconv4_update_hist(const float* __restrict__ in_x,
|
||||
float* __restrict__ hist, int B, int C) {
|
||||
const int c = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (c >= C) return;
|
||||
if (B >= 3) {
|
||||
hist[0 * C + c] = bf16_round(in_x[(size_t)(B - 3) * C + c]);
|
||||
hist[1 * C + c] = bf16_round(in_x[(size_t)(B - 2) * C + c]);
|
||||
hist[2 * C + c] = bf16_round(in_x[(size_t)(B - 1) * C + c]);
|
||||
} else if (B == 2) {
|
||||
hist[0 * C + c] = hist[2 * C + c];
|
||||
hist[1 * C + c] = bf16_round(in_x[0 * C + c]);
|
||||
hist[2 * C + c] = bf16_round(in_x[1 * C + c]);
|
||||
} else if (B == 1) {
|
||||
hist[0 * C + c] = hist[1 * C + c];
|
||||
hist[1 * C + c] = hist[2 * C + c];
|
||||
hist[2 * C + c] = bf16_round(in_x[0 * C + c]);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_shortconv4_update_hist(const float* in_x, float* hist, int B, int C, vetta_stream_t stream) {
|
||||
if (B <= 0 || C <= 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_shortconv4_update_hist<<<(C + 255) / 256, 256, 0, s>>>(in_x, hist, B, 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:
|
||||
|
||||
Reference in new issue
Block a user