Skip to content

perf(cublaslt): cache the BF16 matmul plan per shape - #77

Closed
AzeezIsh wants to merge 2 commits into
mainfrom
perf/cublaslt-plan-cache
Closed

AzeezIsh wants to merge 2 commits into
mainfrom
perf/cublaslt-plan-cache

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

bf16_gemm_act_weight_t built a matmul descriptor, three matrix layouts and a preference, ran cublasLtMatmulAlgoGetHeuristic, launched, then destroyed everything — on every call. None of it depends on a data pointer; it is a pure function of (m, n, k, op) and the fixed workspace. This is the per-call cost that made ATLAS_CUBLAS_GEMM=1 measure marginally slower than the tile kernels it replaces (0.4858 vs 0.4797 s at ctx~124, qwen4_exp), and it now sits on hot paths: 27B GDN projections routed by machine fill, and (#68) the mHC skinny GEMMs on every Flash-Next prefill layer.

  • New cublaslt/bf16_plan.rs: plans cached per shape behind a Mutex<HashMap>, bounded at 4096 and cleared on overflow (prefill m follows the chunk length).
  • Output: the heuristic is deterministic for identical inputs, so the cached pick is the algorithm the per-call path chose — same kernel, same bits.

Validation (queued on reiner, job 255)

  • release build + clippy -D warnings on spark-runtime (CUDA; the macOS check compiles the stub)
  • cutlass::tests::bf16 GPU tests — CUTLASS vs cuBLASLt on repeated shapes (cache hits)
  • 27B A/B vs main66: greedy byte-identity (A == P, A == A2 control) + TTFT at ~160 / 2.6k / 10k tokens

🤖 Generated with Claude Code

bf16_gemm_act_weight_t created a matmul descriptor, three layouts and a
preference, ran cublasLtMatmulAlgoGetHeuristic, launched, and destroyed
all of it on every call. None of that depends on a data pointer; it is a
pure function of (m, n, k, op) and the fixed workspace. Plans are now
cached per shape (bf16_plan.rs), bounded at 4096 and cleared on overflow
(prefill m follows the chunk length). The heuristic is deterministic for
identical inputs, so the cached pick is the algorithm the per-call path
chose: same kernel, same output, minus the host setup that made
ATLAS_CUBLAS_GEMM=1 measure slower than the tile kernels.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Lets a serve prove the plan-cache path actually fires
(RUST_LOG=spark_runtime::cublaslt=debug); one line per new shape.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Validation, attempt 1 (reiner job 255): did not exercise this PR. It is not evidence either way.

  • Build and clippy: clean.
  • GPU tests: 0 ran. They sit behind #[cfg(all(test, atlas_cutlass))], and reiner has no CUTLASS_HOME, so none compiled ("0 passed; 287 filtered out").
  • 27B A/B: outputs byte-identical, TTFT equal (e.g. 10.2k: main66 15.08 / 15.01 s, perf(cublaslt): cache the BF16 matmul plan per shape #77 15.04 / 15.02 s). But no arm set ATLAS_CUBLAS_GEMM=1 or ATLAS_FP8_ROWWISE=1, and those are the only routes into bf16_gemm_act_weight_t for this model. The equal numbers are therefore expected and say nothing about the cache.

Attempt 2 (job 262), on f2f4f587d:

  • The commit adds a debug line per plan built, so a serve log can prove the path fired.
  • The job first probes each routing flag on the 27B and counts plan builds.
  • It then runs the main66-vs-perf(cublaslt): cache the BF16 matmul plan per shape #77 A/B under the first flag that fires: greedy byte-identity, TTFT, and repeats.
  • If no flag routes this pack through cuBLASLt BF16, it reports NOT EXERCISED rather than a pass.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Attempt 2 (job 262): the route is confirmed, and a pre-existing OOM hazard showed up.

  • With ATLAS_CUBLAS_GEMM=1, the 27B does route through this PR's cache: cuBLASLt BF16 plan built m=15 n=16384 k=5120 op_a=1.
  • The serve then dropped to 2.1 GB MemAvailable, and the job's watchdog killed it before the box froze.

Cause (not this PR). cublas_bf16_proj → dequant_fp8_bf16_cached lazily materialises a BF16 copy of every FP8 projection the first time it runs. That memory is not in the KV budget. At util 0.88 the plan was 54.2 GB pre-KV + 49.9 GB KV, so the first prefill's copies went past physical memory.

Anyone enabling ATLAS_CUBLAS_GEMM=1 on a unified-memory box at the default util can hang the machine. Two options: reserve the BF16 copies up front, or refuse the flag when they do not fit. Tracked on #71.

Attempt 3 (job 266): same job at util 0.70, which leaves ~36 GB for the copies.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Result (job 266, util 0.70, ATLAS_CUBLAS_GEMM=1, 27B): correct, and no measurable gain. I recommend closing this PR.

2.6k TTFT 10.2k TTFT greedy
main66 (A / A2) 2.527 / 2.540 s 9.73 / 9.82 s —
this PR (P / P2) 2.537 / 2.528 s 9.86 / 9.92 s byte-identical ×4

Why there is no gain:

  • The route does fire; the new debug line logged 10 plans.
  • Every prefill builds a new plan: m is the prompt length (2621, 2622, 2628, 2631, …) and m is part of the key. Prefill never hits the cache.
  • The per-call descriptor and heuristic setup is too small to see in TTFT anyway.
  • Keying without m is not sound, because the heuristic's algorithm choice depends on m. Bucketing m would change which algorithm runs, so the output would no longer be identical.

What this validation did produce: the OOM hazard (lazily cached BF16 copies of FP8 weights outside the KV budget under ATLAS_CUBLAS_GEMM=1), tracked on #71. That is the actionable item here.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Closing, per the result of job 266. The change is correct but has no measurable effect.

  • Correct: with ATLAS_CUBLAS_GEMM=1 on the 27B, greedy output was byte-identical to main66 in 4 of 4 comparisons, and the debug line confirms the cached path fired (10 plans built).
  • No gain: 2.6k TTFT was 2.537 / 2.528 s vs main66's 2.527 / 2.540 s; 10.2k TTFT was 9.86 / 9.92 s vs 9.73 / 9.82 s.
  • Why: the plan key has to include m, because the heuristic's algorithm choice depends on it. Prefill m is the prompt length, so every prefill builds a new plan and the cache never hits. Bucketing m would change which algorithm runs and break output identity.

The real finding here is the hazard this validation exposed. ATLAS_CUBLAS_GEMM=1 lazily materialises BF16 copies of the FP8 projections, and those copies sit outside the KV/memory budget. At util 0.88 that took the box to 2.1 GB MemAvailable (job 262). It is tracked on #71 and will be fixed in its own PR: either reserve the copies up front or refuse the flag when they do not fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant