Repository navigation
[CuTeDSL] RMSNorm example: 64-bit tile offsets for tensors past 2**31 elements - #3926
bhargavchintam wants to merge 1 commit into
Conversation
… elements The kernel builds x and out with the static row stride N and takes each block's tile with local_tile(mX, tiler, (bidx, cluster_y)), where bidx is the Int32 block index. With a static stride the tile offset bidx * rows_per_block * N is computed in 32 bits. With one row per CTA it is sign-extended, so the kernel faults once a tile starts at 2**31 elements. The other configs zero-extend it, so the tile at 2**32 elements wraps to row 0 and its own rows are never written. Pass Int64(bidx) as the row-tile coordinate for mX and mO, so local_tile computes the offset in 64 bits. Add large-case regression tests at the first tile past 2**31 and 2**32 elements, for 1, 2 and 4 rows per CTA and the 2-CTA cluster path.
|
Hi @fengxie, since you reviewed #3824 on the same RMSNorm example, could you take a look at this one when you have time? It computes the tile offsets in 64 bits, so inputs past |
fengxie
left a comment
There was a problem hiding this comment.
Thanks for fixing. It's a clean, minimal fix and thorough validation.
Thanks a lot! Reviewed and approved. @Junkai-Wu to help merge. |
Fixes #3925.
RMSNormKernel.__call__inexamples/python/CuTeDSL/cute/blackwell/kernel/rmsnorm/rmsnorm.pybuildsmXandmOwithcute.make_layout((M, cfg.N), stride=(cfg.N, 1)), and the kernel takes its tile withcute.local_tile(mX, tiler_mn, (bidx, cluster_y)), wherebidxis the Int32 block index. With an Int32 coordinate and a static stride, the tile offsetbidx * rows_per_block * Nis computed in 32 bits. In the PTX for N = 8192 (one row per CTA) it isshl.b32 %r74, %r3, 13and thencvt.s64.s32, so a tile at 231 elements gets a negative offset and the kernel faults. For N = 4096 (two rows per CTA) the offset is built with 32-bit ops and zero-extended bymul.wide.u32 %rd6, %r82, 2, so it wraps at 232 elements: the last tile rewrites row 0 and its own row is never written. N = 2048 (four rows per CTA) and the 2-CTA cluster path (N = 32768) wrap the same way.On #3607 (the same 32-bit offset with a static stride, in
domain_offset), BlueArchive-Sensei saiddomain_offsetshould not widen coordinates on its own, and that a caller that needs 64-bit offset arithmetic should ask for it when it builds the tensor, with a 64-bit stride. The same reply lists an i64 coordinate with a static stride as another way to get i64 arithmetic. I tried both in the example. The 64-bit row stride (cute.assume(Int64(cfg.N), divby=cfg.vec_size)in__call__) passed the four shapes I ran it on (262145 x 8192, 1048577 x 4096, 2097153 x 2048, 131073 x 32768), but the stride becomes a kernel argument (mad.lo.s64in the address) and it was 7% slower at 4096 x 16384 in the timing below. So this PR keeps the static stride and passesInt64(bidx)as the row-tile coordinate tolocal_tileformXandmO. The tile base for N = 8192 is then onemul.wide.u32 %rd6, %r3, 16384.cXkeeps the Int32bidx, because its values are row indices belowM.New
test/examples/CuTeDSL/sm_100a/test_rmsnorm_large.py: five BFloat16 cases through the example'srun(), at the first tile past 231 elements (N = 8192 and 16384, one row per CTA) and past 232 elements (N = 4096 and 2048 with two and four rows per CTA, and N = 32768 on the cluster path).run()peaks at 16.00 bytes per element (32 GiB and 64 GiB), so the cases carry the repo'slarge_casemarker: they run only with--only-large-case, and they skip when the GPU has less than 17 bytes per element free.cutlass_cicd/test.pydoes not pass--only-large-case, so the public L0 suite skips them.Validation
B200 (SM100), driver 580.196.07, CUDA 13.0 image, Python 3.12, torch 2.14.1+cu130, nvidia-cutlass-dsl 4.9.0.dev1, pytest 9.0.2 (the version in
cutlass_cicd/cutedsl/requirements-test-cu13.txt). Trees: main f8acf1a with only the new test file added, and this branch.New test,
python -m pytest --target-cc 100 -p no:cacheprovider -v --only-large-case "<test id>"from the repo root, one process per case on main (a fault leaves the CUDA context unusable):test_rmsnorm_past_int32_offsets[...]262145-8192torch.AcceleratorError: CUDA error: an illegal memory access was encountered131073-16384torch.AcceleratorError: CUDA error: an illegal memory access was encountered1048577-4096AssertionError: Tensor-likes are not close!,Mismatched elements: 3943 / 4294971392, greatest difference at row 10485762097153-2048AssertionError: Tensor-likes are not close!,Mismatched elements: 1962 / 4294969344, greatest difference at row 2097152131073-32768AssertionError: Tensor-likes are not close!,Mismatched elements: 31626 / 4295000064, greatest difference at row 131072The whole file in one process with this change:
5 passed, 5 warnings in 2.03s. With this change merged with #3824:5 passed, 5 warnings in 2.04s.The example's own command (
python rmsnorm.py --M <M> --N <N>, BFloat16 unless named):--M 262144 --N 8192(exactly 2**31 elements)--M 524289 --N 4096--M 262145 --N 8192 --dtype Float32torch.AcceleratorError: CUDA error: an illegal memory access was encountered--M 262145 --N 8192 --dtype Float16 --no_weighttorch.AcceleratorError: CUDA error: an illegal memory access was encountered--M 2097153 --N 2048AssertionError: Tensor-likes are not close!--M 131073 --N 32768AssertionError: Tensor-likes are not close!To see which rows go wrong, I filled
outwith NaN before one kernel call. On main exactly one row stays NaN for 1048577 x 4096 (row 1048576), 2097153 x 2048 (row 2097152) and 131073 x 32768 (row 131072), and row 0 still matches the reference. With this change no row stays NaN for 1048577 x 4096.python -m pytest --target-cc 100 -p no:cacheprovider -v test/examples/CuTeDSL/sm_100a/test_rmsnorm.pygives79 passedon main. With this change, run together with the new file (then at three cases), it gives79 passed, 3 skipped: without--only-large-casethe new cases are skipped. With this change merged with #3824: 93 passed and 3 failed, the threetest_rmsnorm_shared_padding_initcheckcases that #3824 adds. They fail the same way on #3824 alone on this image, where compute-sanitizer 2025.3.1 saysunrecognised option '--initcheck-address-space'.Kernel time, BFloat16 with weight, main and this change compiled in one process:
cutlass.cute.testing.benchmarkwith CUDA graphs, 200 iterations, rotating workspaces of at least 512 MiB so L2 stays cold, 10 rounds with the variants interleaved; median per variant:The 64-bit stride variant measured 8.69, 42.70, 42.56, 45.29, 43.99, 42.24 and 41.32 us in the same runs. SASS from
CUTE_DSL_KEEP_CUBIN=1: 480 instructions on both for N = 8192, 488 to 496 for N = 4096, 984 to 1008 for N = 32768.black --check test/examples/CuTeDSL/sm_100a/test_rmsnorm_large.pygives1 file would be left unchanged.ruff check(0.14.10) on the two changed files reports onlyF401 ctypes imported but unusedandF401 math imported but unusedinrmsnorm.py, the same two as on main.git merge-tree --write-tree HEAD <pr head>is clean with #3824, #3826, #3827 and #3771.Not run: Hopper (the example also supports SM90), and the full
L0_oss_dsl_testsuite on a GPU.python3 cutlass_cicd/test.py --source-dir . --workspace /tmp/cutlass-ci-dryrun-rmsnorm --package-dir . --sm 100 --test L0_oss_dsl_test --dry-runpasses.