Skip to content

CSA compressor: extend the validated ratio envelope to ratio=2 - #710

Merged
Anerudhan merged 1 commit into
NVIDIA:developfrom
zkyue:feat/csa-compressor-ratio2
Aug 24, 2026
Merged

CSA compressor: extend the validated ratio envelope to ratio=2#710
Anerudhan merged 1 commit into
NVIDIA:developfrom
zkyue:feat/csa-compressor-ratio2

Conversation

@zkyue

@zkyue zkyue commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

What

Widen check_support in python/cudnn/csa/compressor/api.py to accept ratio=2 in the
generic-kernel family: the validated envelope becomes ratio in {2, 4, 128} (with
coff in {1, 2} at every ratio, as before).

No kernel code changes. The generic kernels in compressor_sm100.py are
JIT-specialized per (ratio, head_dim, coff) and are ratio-generic; the {4, 128} gate
encoded validation scope, not a kernel limitation. The dedicated ratio=128 family
(compressor_sm100_r128.py) and the ratio-based routing are untouched.

Why

ratio=2 (with coff=2) is the compressor configuration used in production training of
the model family this operation serves (the kernels were ported from Megatron-LM in
#427). With the gate at {4, 128}, integrating the module at ratio=2 requires bypassing
the validation layer and calling compressor_sm100.run_fwd/run_bwd directly; widening
the envelope lets that configuration go through the supported, validated API.

Why ratio=2 needs no kernel work (verified by reading the kernels and by the test
additions below):

  • the window length win = coff * ratio is a compile-time constant; register pressure
    shrinks with ratio (the family is documented register-bound only beyond ratio ~ 32,
    and ratio=4 is already validated);
  • APE indexing (k % ratio), the overlap-window first-block invalidation, the backward
    zero-write ownership classes (seqlen % ratio tails, seqlen < ratio segments, the
    last block's first-half columns at coff=2, token-capacity padding) and the dAPE
    atomic targets are all expressed in ratio with no lower bound;
  • the launch schedules do not depend on ratio (_fwd_schedule is a function of
    head_dim only; the backward uses fixed rows_per_cta/threads);
  • the int32-offset bounds in check_support are checked independently on
    total_tokens * coff * head_dim, total_comp * head_dim and
    ratio * coff * head_dim, so the (relatively larger) total_comp at ratio=2 is
    already covered.

Numerics at ratio=2

B200 (CC 10.0), torch 2.13.0 / CUDA 13.3 / nvidia-cutlass-dsl 4.5.1, seeded inputs from
this suite's generator; fused wrappers vs the suite's fp32-intermediate eager reference
(the numerics-contract comparator):

config out dKV dScore fwd n_diff dAPE max_abs (rel to ref max)
r2 c2 d128 1x8192 bitwise bitwise bitwise 0/524288 2.1e-05 (6.4e-07)
r2 c2 d128 3x8192 bitwise bitwise bitwise 0/1572864 8.0e-05 (1.1e-06)
r2 c2 d512 1x8192 bitwise bitwise bitwise 0/2097152 3.2e-05 (7.5e-07)
r2 c2 d512 3x8192 bitwise bitwise bitwise 0/6291456 9.2e-05 (1.0e-06)
r2 c2 d128 ragged [8191,8192,4093] bitwise bitwise bitwise 0/1310336 4.2e-05 (6.1e-07)
r2 c1 d128 1x8192 bitwise bitwise bitwise 0/524288 3.1e-05 (8.0e-07)
r4 c2 d128 1x8192 (control) 8 elems differ bitwise bitwise 8/262144 1.1e-05 (4.9e-07)
r4 c2 d512 3x8192 (control) 109 elems differ bitwise bitwise 109/3145728 2.9e-05 (7.5e-07)

dKV/dScore carry the generic family's bitwise contract at ratio=2, exactly as at
ratio=4. On every tested ratio=2 config the forward is additionally fully bitwise (the
contract only requires "within one bf16 rounding step on a tiny fraction of elements";
the ratio=4 controls show that documented behavior). dAPE differs at fp32
atomic-order level only (~1e-6 relative), per the documented contract (dAPE is not
run-to-run deterministic in either implementation; the backward still refuses strict
torch.use_deterministic_algorithms(True)).

Performance at ratio=2

nsys pure-kernel times (sum of kernel durations inside a cudaProfilerApi-gated
50-iteration unsynced loop / 50; JIT + fast-launch warmup before the window), ratio=2
coff=2, single B200; eager = the fp32-intermediate reference region (fwd + autograd
bwd, ~93 kernel launches per step):

config fused fwd fused bwd fused fwd+bwd eager fwd+bwd speedup
d128, 1x8192 4.7 us 11.4 us 16.2 us 331.6 us 20.5x
d128, 3x8192 10.4 us 25.6 us 36.1 us 650.5 us 18.0x
d512, 1x8192 13.5 us 27.7 us 41.2 us 785.5 us 19.1x
d512, 3x8192 39.6 us 69.2 us 108.8 us 2104.6 us 19.3x

(The fused ratio=2 kernels run at the same throughput class as the validated ratio=4
configs — same kernel bodies, smaller unrolled window.)

Tests

test/python/fe_api/csa/test_CSA_compressor.py adds ratio=2 cases following the exact
ratio=4 patterns, both window forms (coff 1 and 2):

  • numerics vs the fp32-intermediate eager reference / verbatim upstream eager / fp64
    oracle (12 new _SHAPES cases: single/ragged/short-segment/empty-segment packs, d
    128/512 and the odd d=65 scalar layout), with the bitwise dKV/dScore assertion
    now applied at ratio in {2, 4};
  • static-capacity padding rows (6 new cases, including a padding-only pack — every segment shorter than ratio, positive static capacity);
  • NaN-canary zero-write ownership over every never-consumed slot class (18 new cases;
    the tiny-segment packs use 1-token segments so the seqlen < ratio class is still
    exercised at ratio=2);
  • run-to-run replay determinism (ratio now parametrized over {2, 4});
  • check_support acceptance at ratio=2 and rejection cases (coff 0/3 at ratio=2, a
    total_tokens < ratio case at ratio=2, and the updated {2, 4, 128} rejection
    message).

Module suite (-m "L0 or L1", single B200): 145 passed, 1 skipped
(test_multi_device_launch, single-GPU runner). On the unpatched gate the new ratio=2
cases fail with the expected envelope rejection
(ValueError: CSA compressor is validated for ratio in {4, 128} only, got ratio=2, ...),
so the tests pin the widening in both directions.

Docs: docs/fe-oss-apis/csa.md support surface and numerics-contract sections updated
(ratio in {2, 4} for the generic family; ratio=128 family unchanged).

Summary by CodeRabbit

  • New Features

    • Added support for CSA compression with ratio 2, alongside existing ratios 4 and 128.
    • Added support for coefficient values 1 and 2 with ratios 2 and 4.
  • Bug Fixes

    • Improved validation for unsupported ratio and coefficient combinations.
    • Ensured consistent bitwise results for key outputs across supported generic compression ratios.
  • Documentation

    • Updated API and numerical behavior documentation to describe ratio 2 support and its guarantees.

The generic compressor kernels (compressor_sm100.py) are JIT-specialized
per (ratio, head_dim, coff) and carry no ratio-dependent assumption below
the documented register bound at ratio ~ 32; the {4, 128} envelope in
api.py's check_support encoded validation scope, not a kernel limitation.
Widen the generic-family branch to ratio in {2, 4} -- ratio=2 (coff=2) is
the compressor configuration used in production training of the model
family this operation serves. No kernel changes; the ratio=128 family and
its routing are untouched.

Validation on B200 (CC 10.0, torch 2.13, cutlass-dsl 4.5.1):

- ratio=2 test cases added mirroring every ratio=4 pattern (numerics vs
  the fp32-intermediate eager reference with the bitwise dKV/dScore
  contract now asserted at ratio in {2, 4}, ragged/short/empty-segment
  packs, static-capacity padding, NaN-canary zero-write ownership, replay
  determinism, check_support acceptance/rejection): module suite 145
  passed, 1 skipped (multi-GPU test on a single-GPU runner); the new
  cases fail with the envelope ValueError on the unpatched gate.
- at ratio=2, out/dKV/dScore are bitwise-identical to the fp32 eager
  reference in every tested config; dAPE differs at fp32 atomic-order
  level only (~1e-6 relative), per the documented contract.
- nsys pure-kernel at ratio=2 coff=2 (50-iter mean): fwd 4.7/10.4/13.5/
  39.6 us, bwd 11.4/25.6/27.7/69.2 us for d128/d512 x 1/3x8192-token
  packs -- 18-21x the eager reference region.

Signed-off-by: zky <kaiyue.zhou@z.ai>
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 3dfa21af-e7f5-4ce8-9df4-7796a131a87b

📥 Commits

Reviewing files that changed from the base of the PR and between aded990 and 0c9856a.

📒 Files selected for processing (3)
  • docs/fe-oss-apis/csa.md
  • python/cudnn/csa/compressor/api.py
  • test/python/fe_api/csa/test_CSA_compressor.py

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.


📝 Walkthrough

Walkthrough

The CSA compressor now supports generic ratios 2 and 4 with coff values 1 and 2. API validation, numerics contracts, documentation, determinism, padding, canary, and support-validation tests now cover ratio 2.

Changes

CSA ratio support

Layer / File(s) Summary
Generic ratio contract
python/cudnn/csa/compressor/api.py, docs/fe-oss-apis/csa.md
The API and documentation accept ratios 2 and 4 for the generic kernel. Ratio 128 retains its tolerance-based contract.
Ratio-2 numerical coverage
test/python/fe_api/csa/test_CSA_compressor.py
Numerics, bitwise gradient, forward behavior, comparison, and determinism tests now cover ratio 2 and ratio 4.
Padding, canary, and support validation
test/python/fe_api/csa/test_CSA_compressor.py
Tests cover ratio-2 padding, never-consumed gradient slots, support envelopes, invalid configurations, and minimum input-token validation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 0c985

This PR widens the supported CSA compressor ratio envelope to include ratio=2, with accompanying tests and documentation updates; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: hxbai

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: extending the validated CSA compressor ratio envelope to ratio=2.
Description check ✅ Passed The description thoroughly covers the change, rationale, compatibility impact, documentation, testing, numerical results, and performance results.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@Anerudhan

Copy link
Copy Markdown
Collaborator

@cudnn-ci-bot run oss

@cudnn-ci-bot

cudnn-ci-bot commented Aug 24, 2026

Copy link
Copy Markdown

🏁 Pipeline finished

SHA: 0c9856a
Targets: oss
Branch: cudnn-gh/pr-710-0c9856a
Pipeline: 64308917
Last updated: 2026-08-24 17:51 UTC

@Anerudhan Anerudhan added orig-external Reported or requested by an external user, customer, or community contributor. mod-cutedsl CuTeDSL kernels, generated kernels, examples, or related integration work. cat-enhancements labels Aug 24, 2026
@Anerudhan Anerudhan added this to the Frontend 1.28.0 milestone Aug 24, 2026
@Anerudhan
Anerudhan merged commit 6a14365 into NVIDIA:develop Aug 24, 2026
1 check passed
@Anerudhan Anerudhan mentioned this pull request Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat-enhancements mod-cutedsl CuTeDSL kernels, generated kernels, examples, or related integration work. orig-external Reported or requested by an external user, customer, or community contributor.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants