Skip to content

[KDA] Add SM90 fused WY-DqKG backward#109

Open
zheyang0825 wants to merge 1 commit into
inclusionAI:mainfrom
zheyang0825:feat/sm90-wydqkg
Open

[KDA] Add SM90 fused WY-DqKG backward#109
zheyang0825 wants to merge 1 commit into
inclusionAI:mainfrom
zheyang0825:feat/sm90-wydqkg

Conversation

@zheyang0825

Copy link
Copy Markdown
Collaborator

📌 Description

  • Add a Hopper SM90 warp-specialized CuTe DSL implementation of KDA fused WY-DqKG backward.
  • Fuse the backward path producing dq, dk, dv, db, dg, and dA in the same order as FLA.
  • Support fixed-length and ragged variable-length inputs, partial chunks, float32/bfloat16 beta, and selectable BK/BV tile sizes and occupancy.
  • Add numerical comparison, determinism, benchmark-shaped, and compute-sanitizer test coverage.
  • Add a dedicated benchmark against FLA Triton and document the reproduction command.
  • Integrate the kernel into the public cula.kda.chunk_kda autograd path on SM90.
  • Use FLA v0.5.0 Triton for SM90 forward intra/output, recompute W/U, delta-H, and backward intra; preserve the existing SM100/SM103 paths and the SM90 GVA fallback.
  • Correct architecture-only pytest markers so SM90 tests do not run on CPU or non-SM90 CUDA hosts.

This ports the SM90 WY-DqKG implementation from the internal biz/v0.2 line into a focused PR based on the public GitHub main, without carrying unrelated SM100 or build-system changes.

🔍 Related Issues

N/A

🚀 Pull Request Checklist

Thank you for contributing to cuLA! Before we review your pull request, please make sure the following items are complete.

✅ Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).
  • I have installed the hooks with pre-commit install.
  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.

pre-commit was run successfully on every file changed by this PR. The repository-wide command was intentionally not marked complete because existing unrelated and ignored source trees are not fully Ruff-formatted.

🧪 Tests

  • Tests have been added or updated as needed.
  • All tests are passing.

Validation completed locally:

FLA:                 0.5.0
PyTorch:             2.9.1+cu129
nvidia-cutlass-dsl:  4.4.2
ruff check:          passed
pre-commit (PR files): passed
python compileall:   passed
pytest collection:  48 tests collected
SM90 correctness:   5 passed
SM90 determinism:   10 passed (benchmark-sized fixed + varlen cases)
chunk_kda end-to-end: 3 passed (fixed/varlen, recompute/saved intermediates, fp32/bf16 beta)

The numerical tests compare all six outputs with the FLA v0.5.0 Triton reference. The benchmark-sized determinism suite is bitwise exact across repeated runs. The CUDA runtime reports compute capability (9, 0); nvidia-smi labels the provisioned device NVIDIA L20X.

pip install -e . --no-build-isolation was also attempted. The extension build is blocked by the host toolchain mismatch (system NVCC 13.0 versus PyTorch cu129), independent of this Python/CuTe change.

⚡ Performance

Benchmark target:

python benchmarks/bench_kda_bwd_wy_dqkg_sm90.py --mode both --heads 32

The benchmark covers fixed-length B={1,2}, sequence lengths from 256 through 32768, and uniform/random/skewed variable-length workloads. It reports FLA v0.5.0 latency, cuLA SM90 latency, speedup, and accuracy for dq, dk, dv, db, dg, and dA.

Measured with H=32, K=V=128, BT=64, BK=32, BV=64, occupancy 2, bf16, 25 warmup iterations, and 100 timed iterations. The CUDA runtime reports SM90 capability (9, 0); nvidia-smi labels the provisioned device NVIDIA L20X.

Workload Configs FLA v0.5.0 cuLA SM90 Speedup
Fixed length 15 0.239–10.508 ms 0.034–3.628 ms 3.46x avg (2.70x–7.05x)
Variable length 18 0.721–2.731 ms 0.270–0.975 ms 2.77x avg (2.55x–2.94x)
Overall 33 3.08x avg (2.55x–7.05x)

Fixed-length results

B T FLA v0.5.0 cuLA SM90 Speedup Max err_ratio Max rel_max
1 256 0.2423 ms 0.0343 ms 7.05x 0.000467 0.000472
1 512 0.2412 ms 0.0381 ms 6.33x 0.000474 0.000728
1 1024 0.2422 ms 0.0692 ms 3.50x 0.000457 0.000709
1 2048 0.3393 ms 0.1256 ms 2.70x 0.000461 0.000626
1 4096 0.6749 ms 0.2395 ms 2.82x 0.000458 0.000374
1 8192 1.3504 ms 0.4601 ms 2.93x 0.000418 0.000696
1 16384 2.6801 ms 0.9050 ms 2.96x 0.000464 0.000570
1 32768 5.2796 ms 1.8050 ms 2.92x 0.000469 0.000564
2 512 0.2389 ms 0.0693 ms 3.45x 0.000457 0.000709
2 1024 0.3401 ms 0.1261 ms 2.70x 0.000461 0.000626
2 2048 0.6744 ms 0.2382 ms 2.83x 0.000458 0.000374
2 4096 1.3476 ms 0.4604 ms 2.93x 0.000418 0.000696
2 8192 2.6836 ms 0.9073 ms 2.96x 0.000464 0.000570
2 16384 5.2753 ms 1.8023 ms 2.93x 0.000469 0.000564
2 32768 10.5079 ms 3.6280 ms 2.90x 0.000455 0.000765

Variable-length results

Distribution Sequences Total T Length range FLA v0.5.0 cuLA SM90 Speedup Max err_ratio Max rel_max
uniform 10 4096 409–415 0.7259 ms 0.2701 ms 2.69x 0.000444 0.000494
random 10 4096 24–1201 0.7233 ms 0.2750 ms 2.63x 0.000449 0.000651
skewed 10 4096 227–2053 0.7211 ms 0.2785 ms 2.59x 0.000438 0.000496
uniform 20 4096 204–220 0.7806 ms 0.3006 ms 2.60x 0.000420 0.000572
random 20 4096 5–787 0.7638 ms 0.2914 ms 2.62x 0.000432 0.000582
skewed 20 4096 107–2063 0.7245 ms 0.2838 ms 2.55x 0.000443 0.000544
uniform 10 8192 819–821 1.3545 ms 0.4783 ms 2.83x 0.000411 0.000911
random 10 8192 48–2401 1.3983 ms 0.4999 ms 2.80x 0.000407 0.000595
skewed 10 8192 455–4097 1.4010 ms 0.4998 ms 2.80x 0.000411 0.000754
uniform 20 8192 409–421 1.4307 ms 0.5016 ms 2.85x 0.000399 0.000607
random 20 8192 9–1574 1.4254 ms 0.5065 ms 2.81x 0.000398 0.000551
skewed 20 8192 215–4107 1.4431 ms 0.5210 ms 2.77x 0.000403 0.000816
uniform 10 16384 1638–1642 2.6896 ms 0.9269 ms 2.90x 0.000460 0.000458
random 10 16384 95–4802 2.6955 ms 0.9502 ms 2.84x 0.000459 0.000630
skewed 10 16384 910–8194 2.7216 ms 0.9247 ms 2.94x 0.000457 0.000739
uniform 20 16384 819–823 2.6941 ms 0.9341 ms 2.88x 0.000459 0.000467
random 20 16384 19–3147 2.7305 ms 0.9752 ms 2.80x 0.000453 0.000576
skewed 20 16384 431–8195 2.7083 ms 0.9355 ms 2.90x 0.000455 0.000857

For every row, the two accuracy columns are the maxima across dq, dk, dv, db, dg, and dA; both maxima came from dg. Values are rounded to the six decimal places printed by the benchmark.

Reviewer Notes

  • Focus review on TMA/WGMMA pipeline synchronization, partial-chunk masking, and the fused epilogue stores.
  • Verify both fixed and ragged variable-length paths on SM90.
  • Confirm numerical tolerance and bitwise determinism on benchmark-sized cases.
  • Review the SM90 dispatch boundaries in chunk_fwd.py, chunk_intra.py, and chunk_bwd.py; SM100/SM103 behavior should remain unchanged.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces SM90 (Hopper) support for the modular chunk KDA forward and backward kernels, integrating Triton-based adapters and fallback paths alongside the existing SM100 (Blackwell) implementations. It also adds benchmarking scripts, comprehensive numerical correctness tests, and updates the test configuration to properly target SM90 and SM100 devices. The review feedback suggests grouping multiple imports from the same module in tests/test_chunk_wy_dqkg_sm90.py into a single import statement to improve readability.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +26 to +46
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
BK as BENCHMARK_BK,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
BT as BENCHMARK_BT,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
BV as BENCHMARK_BV,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
MIN_OCC as BENCHMARK_MIN_OCC,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
SEED as BENCHMARK_SEED,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
K as BENCHMARK_K,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
V as BENCHMARK_V,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Multiple import statements from the same module benchmarks.bench_kda_bwd_wy_dqkg_sm90 can be grouped into a single import statement. This improves readability and adheres to PEP 8 guidelines.

Suggested change
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
BK as BENCHMARK_BK,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
BT as BENCHMARK_BT,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
BV as BENCHMARK_BV,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
MIN_OCC as BENCHMARK_MIN_OCC,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
SEED as BENCHMARK_SEED,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
K as BENCHMARK_K,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
V as BENCHMARK_V,
)
from benchmarks.bench_kda_bwd_wy_dqkg_sm90 import (
BK as BENCHMARK_BK,
BT as BENCHMARK_BT,
BV as BENCHMARK_BV,
MIN_OCC as BENCHMARK_MIN_OCC,
SEED as BENCHMARK_SEED,
K as BENCHMARK_K,
V as BENCHMARK_V,
)
References
  1. Imports should be grouped and placed at the top of the file, and multiple imports from the same module should be combined. (link)

@zheyang0825
zheyang0825 requested review from icavan and zhouaihui July 22, 2026 02:16
@zheyang0825
zheyang0825 marked this pull request as ready for review July 22, 2026 02:17
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

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