Goal
Bring the Gemma 4 CUDA performance work to other dense models that fully offload via CudaForwardPass — primarily Qwen3-8B Q4_K_M. The batched-prefill orchestration and CUDA-graph machinery are already largely generic; they are just structurally gated behind _isGemma4Like. This issue generalizes them.
Out of scope: MoE/GDN models (separate CudaHybridForwardPass / CudaHybridGdnForwardPass), and Vulkan.
Background
What the Gemma 4 work (#136/#141/#142/#146/#147/#149/#154) splits into:
Two config knobs that looked like blockers are already first-class: attnScale (kernels do <=0 => 1/sqrt(headDim), Gemma hardcodes 1f) and hp.FfnActivation (Silu vs GeluApprox, batched body hardcodes GeluTanhMul).
Work Item A — Generalize batched prefill + flash attention (biggest win, low risk)
Three hard Gemma assumptions in GpuLayerBatchedTrunkGemma4 (CudaForwardPass.cs:2011):
What Q4_K gains: flash-attention prefill (biggest lever, quant-agnostic, head_dim=128 ✓) + launch collapse. NOT the compute-bound trunk GEMM (Q4_K stays on memory-bound GEMM-N; that's Item C).
Work Item B — Decode CUDA graphs for non-Gemma (launch-overhead win)
ForwardGemma4 is split prefix(embed/PLE) → capturable device region → download. The non-Gemma Forward (:890) inlines everything with no capture.
Work Item C — Compute-bound Q4_K prefill (optional, larger; gate on A's profiler)
Only pursue if A's prefill profiling shows trunk matmuls (not attention) dominating after flash lands.
Verification
dotnet build clean (TreatWarningsAsErrors), dotnet test --filter ...ForwardPass
- Argmax-stable parity oracle for A; bit-identical check for B
- Before/after t/s on Qwen3-8B Q4_K_M (model at
C:\p\sharpi\models\ or E:\models\)
Sequencing: A first (biggest win + clean oracle), B second, C gated on A's profiler.
Goal
Bring the Gemma 4 CUDA performance work to other dense models that fully offload via
CudaForwardPass— primarily Qwen3-8B Q4_K_M. The batched-prefill orchestration and CUDA-graph machinery are already largely generic; they are just structurally gated behind_isGemma4Like. This issue generalizes them.Out of scope: MoE/GDN models (separate
CudaHybridForwardPass/CudaHybridGdnForwardPass), and Vulkan.Background
What the Gemma 4 work (#136/#141/#142/#146/#147/#149/#154) splits into:
Two config knobs that looked like blockers are already first-class:
attnScale(kernels do<=0 => 1/sqrt(headDim), Gemma hardcodes1f) andhp.FfnActivation(SiluvsGeluApprox, batched body hardcodesGeluTanhMul).Work Item A — Generalize batched prefill + flash attention (biggest win, low risk)
Three hard Gemma assumptions in
GpuLayerBatchedTrunkGemma4(CudaForwardPass.cs:2011):int layerHd = _hp.LayerHeadDim NPEs on non-Gemma →_hp.LayerHeadDim?[layer] ?? _headDim_gpu.GeluTanhMul(...)(:2103) hardcoded → dispatch on_hp.FfnActivation(SiLuMulforSilu)attnScale: 1fon all 4 attention calls (:2077-2090) → cached field (1fif_isGemma4Likeelse-1f)IsGemma4BatchedPrefillSupported(:1779) — keep real guards (!_isMoE,!_tqEnabled,!_hasAttnBias,IsNeoxRope, batchable dtypes), drop the implicit Gemma assumptionPrefillgate (:1732) from_isGemma4Liketo "dense + batchable"Forwardon Qwen3-8B, argmax-stable (flash/GEMM not byte-exact, matches Gemma 4 GPU prefill is ~70x slower than llama.cpp — adopt cuBLAS GEMM for prefill #141)What Q4_K gains: flash-attention prefill (biggest lever, quant-agnostic, head_dim=128 ✓) + launch collapse. NOT the compute-bound trunk GEMM (Q4_K stays on memory-bound GEMM-N; that's Item C).
Work Item B — Decode CUDA graphs for non-Gemma (launch-overhead win)
ForwardGemma4is split prefix(embed/PLE) → capturable device region → download. The non-GemmaForward(:890) inlines everything with no capture.Forwardinto token-varying prefix (embed) +RunDeviceRegion(position)(layer loop + final norm/output) + download, keeping TQ ring-advance (:1056) and SnapKV capture (:955) OUTSIDE the regionTryBeginGraphCapture/TryEndGraphCaptureAndInstantiate/LaunchGraphForPosition); reuse the_tqEnabled || _kvEvictedCount > 0bail from:1315Work Item C — Compute-bound Q4_K prefill (optional, larger; gate on A's profiler)
MatMulBatchedGemmto dequant Q4_K->fp16->cuBLAS, ORmul_mat_qQ4_K reference)Only pursue if A's prefill profiling shows trunk matmuls (not attention) dominating after flash lands.
Verification
dotnet buildclean (TreatWarningsAsErrors),dotnet test --filter ...ForwardPassC:\p\sharpi\models\orE:\models\)Sequencing: A first (biggest win + clean oracle), B second, C gated on A's profiler.