Files
foxhunt/docs
jgrusewski 326c133782 perf(dqn): Phase H — fuse cublasLt BIAS epilogue into 4 attention forward projections
Collapses each `cublasLtMatmul + add_bias_f32_kernel` pair in the attention
forward path (Q, K, V, O projections) into a single fused `cublasLtMatmul`
with `CUBLASLT_EPILOGUE_BIAS`. Saves 4 kernel launches per attention forward
× per training step (target + online), targeting the L40S deploy hot-spot
where the per-epoch training phase is 99.7% of wall time.

Three code changes in `crates/ml/src/cuda_pipeline/gpu_attention.rs`:
  1. `create_attn_gemm_desc` extended with `epilogue: Option<cublasLtEpilogue_t>`
     parameter — when `Some(BIAS)`, descriptor is configured with
     `EPILOGUE = BIAS` + `BIAS_DATA_TYPE = CUDA_R_32F`; when `None`, stays
     at `EPILOGUE_DEFAULT` (the 8 backward dW/dX GEMMs unchanged).
  2. New `lt_matmul_with_bias_ex` helper writes the per-call bias pointer via
     `set_matmul_desc_attribute(BIAS_POINTER, …)` immediately before each
     `cublasLtMatmul` (mirrors the existing pattern in batched_forward.rs).
     The 4 forward projection sites in `forward(...)` switch from the prior
     `lt_matmul_ex(...)` + `launch_bias_add_ex(...)` pair to a single
     `lt_matmul_with_bias_ex(...)` call.
  3. Orphans pruned: `launch_bias_add_ex` and `launch_bias_add` deleted from
     `gpu_attention.rs` (their only callers were the 4 fused-away sites).
     Shared `add_bias_f32_kernel` retained — still used by
     `batched_forward.rs::launch_add_bias_f32_raw` (VSN Linear_2 logit output,
     no activation).

Determinism preserved: the deterministic-algo cache (`cublas_algo_deterministic.rs`)
already keys on epilogue via `ShapeKey::with_epilogue`, so first-call selection
runs the full `AlgoGetIds → AlgoInit → AlgoCheck` loop with the new descriptor
and subsequent calls reuse the cached `(types, shape, epilogue)` algo. Bit-
deterministic when the algo is fixed under `CUBLAS_WORKSPACE_CONFIG=:4096:8`.

Site #2 (DRELU_BGRAD on the trunk Linear→Bias→ReLU backward) deferred — the
forward-side aux-buffer plumbing crosses three modules (BatchedForward →
BatchedBackward → value-FC site) and the determinism contract verified by
the Phase G smoke is non-trivial to preserve. Tracked for follow-up.

Verified: cargo check workspace clean at 11 warnings (baseline preserved).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 19:27:48 +02:00
..