Repository navigation
Conversation
Reviewer's GuideThis PR optimizes graphless CUDA pass 2 for eligible Llama attention workloads by building host-side trie index lists and running separate root and ancestor-only branch varlen attention passes, merged with log-sum-exp instead of dense masked attention. The path is conservatively gated by backend, model, mode, dtype, torch API, and a 1,024-token threshold, preserving the existing dense implementation for unsupported or smaller workloads; focused CPU/CUDA tests cover numerical equivalence and fallback semantics. Sequence diagram for CUDA cascade pass 2 attentionsequenceDiagram
participant G as GraphBackend
participant T as _varlen_tree
participant M as Model
participant V as varlen_attn
participant L as _merge_attention
G->>T: Build trie index lists from seg and parents
G->>M: _forward_cascade(ids, pos, prefix, tree)
M->>V: Root attention with seqused_k
V-->>M: Root output and log-sum-exp
M->>V: Branch attention over ancestors and own tokens
V-->>M: Branch output and log-sum-exp
M->>L: Merge root and branch results
L-->>G: Pass 2 hidden states
Flow diagram for graphless CUDA pass 2 cascade selectionflowchart TD
A[Graphless pass 2] --> B{Rows reach 1024 tokens?}
B -- No --> D[Dense masked attention]
B -- Yes --> C{CUDA Llama varlen support?
Not lean or Gemma}
C -- No --> D
C -- Yes --> E[_varlen_tree builds trie indices]
E --> F[_forward_cascade]
F --> G[_cascade_varlen]
G --> H[Root and ancestor-only attention]
H --> I[Log-sum-exp merge]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="docs/WHATS_NEW.md" line_range="3-8" />
<code_context>
+# What is new in basal 2.3.4 (not released yet)
+
+- **Compress and many-question requests faster on CUDA** (`--mode fast`, the Llama-based basal-2.0 models): a pass 2
+ without a CUDA graph whose rows hold 1,024 or more tokens (every `compress` request, dozens of questions over a state)
+ attends to the state's keys and values as pass 1 wrote them and, per trie block, to its own ancestors (torch's
+ varlen attention), merged by log-sum-exp, instead of one masked call over concatenated and repeated keys. Compress
+ over 1.5k-6k-token documents 1.14-1.39x faster (basal-2.0-mini and basal-2.0-4.5B, RTX 4090 and A100), 40 questions
+ 1.07-1.11x, 2.1-2.5 GB less GPU memory for the largest of them; smaller requests run as before, bit-identical.
+ Answers within bf16 rounding: against fp32, mean |Δp| 6.9e-3 (the dense bf16 pass: 7.0e-3).
+ Contributed by Milosz (@Butterski).
</code_context>
<issue_to_address>
**Cascade availability is overstated**
When a CUDA installation lacks supported varlen attention or `seqused_k`, `GraphBackend._cascade_why` disables the cascade when the installed torch lacks supported varlen attention or `seqused_k`, and otherwise-ineligible configurations use the dense pass. The note's claim that every compress request gets the cascade-led speedup therefore does not hold for all supported CUDA installations.
Qualify the release note's availability and speedup claims with the varlen-attention, `seqused_k`, and dtype requirements.
</issue_to_address>
### Comment 2
<location path="basal/engine.py" line_range="746-758" />
<code_context>
+def _cascade_varlen(q, k, v, kp, vp, tree, scale):
</code_context>
<issue_to_address>
**Pass-two documentation omits cascade**
When pass 2 is cascade-eligible on CUDA, `GraphBackend`'s class docstring still says pass 2 uses a block mask and runs as a normal forward for other shapes (`basal/engine.py:888-894`). On cascade-eligible CUDA requests, `_cascade_varlen` instead runs separate root and branch varlen attentions without that mask, so the documented pass-two behavior no longer matches the code.
Update the `GraphBackend` class docstring to describe the cascade path and its eligibility alongside the graph and dense paths.
</issue_to_address>A pass 2 without a CUDA graph whose longest row has CASCADE_TOKENS (1024) or more tokens (every compress request, dozens of questions) runs as a cascade on Llama models with torch varlen attention: one varlen_attn over the root key / value buffers pass 1 wrote (seqused_k = each row's root length; no mask, no concatenated or repeated keys), one over each trie block's ancestors and own tokens (causal from the end), merged by log-sum-exp (rkinas#34's _merge_attention). Also the long lane. Below the threshold, in fp32, with --lean and on Gemma 4 nothing changes. RTX 4090 / A100, basal-2.0-mini and 4.5B: compress over 1.5k-6k-token documents 1.14-1.39x faster, 40 questions 1.07-1.11x, 2.1-2.5 GB less GPU memory for the largest requests; the dense path bit-identical to main; against fp32 the cascade's mean |dp| 6.9e-3 (dense bf16 7.0e-3).
…ring and WHATS_NEW
5cb8d9c to
8faed53
Compare
|
Thank you, Miłosz. #39 is in basal 2.3.5, released today: https://github.com/rkinas/basal/releases/tag/v2.3.5. What we measured on an H100 with torch 2.13:
|
The GPU half of #34: the same cascade (root part + per-block branch part, #34's
_merge_attention) for the CUDA graphengine's pass 2 where it runs without a graph.
Problem
In the graph engine (
--mode fast, Llama models) pass 2 of a branched unit runs without a CUDA graph when its rowsare longer than 256 tokens, it has more than 8 rows, or its root is longer than 3,072 tokens: every
compressrequest(one branch per sentence, thousands of tokens below the document) and requests with many questions. That pass
attends through
basal_attentionwithbasal_prefix, per layer:torch.catof the root's keys / values with the branch tokens', then transformers'repeat_kv(6 or 8 copies ofevery key head for SDPA with a mask);
[B, 1, S, L + S]built per call, its root part all zeros;S x (L + S), although a branch token sees only theroot and its own trie ancestors (1.4-2.7% of the
S x Spart on compress requests).Change
basal/engine.py, Llama on CUDA with torch's varlen attention (torch_varlen_available, plusseqused_k), not--lean, not Gemma 4 (_cascade_why). The model check is split out ofcascade_okasattention_only: the CUDApath needs it but not the private CPU flash op that
cascade_okalso checks since 2.3.3.CASCADE_TOKENS= 1024 tokens runs as a cascade(
_forward_cascade->basal_attentionwith a_VarlenTree->_cascade_varlen):varlen_attnover the root key / value buffers pass 1 wrote,seqused_k= each row's root length(no mask, no concatenated or repeated keys);
varlen_attnover, per trie block, its ancestors' and own tokens, causal from the end(
_varlen_treebuilds the index lists on the host from the sameseg/parentsas the dense mask);_merge_attention): the softmax over all keys, summed in another order. A paddingtoken sees the root only, as in the dense mask.
_long_hidden) uses it too.changes; the dense path is bit-identical to
main.Measurements
RTX 4090, torch 2.13.0+cu130,
basal-serve(fast, its defaults) on this branch; per request the pass 2 alternatesbetween dense (threshold off), cascade (threshold 0) and the default threshold (a research launcher that reads the
threshold from a file; not part of this PR), one client, median of 5 (mini) / 3 (4.5B) after a warm-up round. Compress
over synthetic case logs (one unit per ticket line,
keep_ratio0.3), multi-question requests over a 1.5k-token log:With the cascade forced on 5-10 questions it is 0.98-1.02x (two small kernels and the merge against one small dense
call), hence the threshold.
A100 PCIe 40 GB (Ampere), the same runs, median of 3:
There the forced cascade costs 0.83-0.84x at 5 questions, so the threshold matters more. An H100 was not measured
(three rented hosts never finished loading the container).
Re-checked after the rebase on 2.3.3 (with #34, #35, #37), RTX 4060 Ti 16 GB, basal-2.0-mini, median of 3: compress
1.16-1.34x (en 3k: 1447 -> 1077 ms, en 6k: 3750 -> 2874 ms), 40 questions 1.10x (325 -> 293 ms), 5-20 questions as
dense (default threshold); the dense path bit-identical to
main(47ae982) on all six compress requests.GPU memory for the largest of these requests (compress en / pl 6k, 40 questions) on a fresh server, cascade first,
then dense (the allocator keeps its peak). Measured before #35 / #37 were merged; their savings are in the start-up
memory, the cascade's in the per-request part:
Answers: the dense path of this branch against upstream
main(621bc57, and 47ae982 after the rebase) on the sixcompress requests: every probability bit-identical. The cascade changes bf16 rounding only; against basal-2.0-mini in
fp32 (where every mode runs the dense fp32 pass) as the reference, over all ten requests (679 probabilities):
The compress decisions are the top 30% of units, so near-ties at the cut flip with bf16 rounding in both: dense bf16
already keeps a different set than fp32 in 4 of the 6 compress requests.
Tests
test_cascade_pass2_equals_separate(tests/test_engine16.py): the cascade path on the CPU with torch's varlenattention written out (
ref_varlen): several units per forward with different root and branch lengths (padding,stale buffer keys past each root), nested option orders, readouts inside the root; every readout equals its prompt
run alone (fp32, 1e-5), and below the threshold the dense pass runs. Dropping
seqused_k, a non-causal branch part,missing ancestors or the root part alone each fail it.
tests/test_cascade_cuda.py(CUDA sm80+): torch'svarlen_attnagainst the attention written out (itslog-sum-exp layout, causal aligned at the end,
seqused_k), and_cascade_varlenagainst one masked SDPA call overcat(root, branch)with repeated heads (two rows, nested blocks, padding).test_cascade_why: off on the CPU and with--lean; on CUDA it does not need the CPU flash op (with the gate oncascade_ok, a torch without that op would switch the CUDA cascade off; the test fails that way).tests/(withouttest_gguf.py), rebased on 2.3.3: CPU 703 passed, 83 skipped; RTX 4060 Ti 739 passed, 47 skipped(
test_cascade_cuda.py2 passed). Before the rebase (2.3.1 + perf(mps): the CPU branch pass without dense masks (cascade attention), the state once per request #34, the same pass-2 code): RTX 4090 and A100 703passed, 47 skipped each.
Summary by Sourcery
Speed up long uncompiled CUDA pass-2 requests by using a trie-aware varlen attention cascade instead of dense masked attention.
New Features:
Enhancements:
Documentation:
Tests: