Skip to content

[CuTeDSL] examples: use the out-of-bounds predicate in elementwise_apply - #3934

Open
zkyue wants to merge 2 commits into
NVIDIA:mainfrom
zkyue:fix/elementwise-apply-use-pred
Open

zkyue wants to merge 2 commits into
NVIDIA:mainfrom
zkyue:fix/elementwise-apply-use-pred

Conversation

@zkyue

@zkyue zkyue commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

elementwise_apply.py computes frgPred ("predicate for out of boundary checks") and then never uses it: the kernel loads and stores through thrInput.load() / thrC.store() unconditionally. For a tensor whose shape is not a multiple of the (64, 256) tile, the edge blocks read and write past the end of the tensor.

Using the predicate also exposed that it was computed from the wrong coordinates: the data tensors are composed with remap_block ((RestM, RestN) -> (RestN, RestM)), the coordinate tensor cC was not, so frgPred was only right when the block grid is square.

Changes, all in the example:

  • load and store through predicated cute.copy(..., pred=frgPred), the same way elementwise_add.py does;
  • give the per-thread tensors the ((AtomV, AtomRest), Rest...) profile cute.copy expects by prepending a unit mode;
  • compose cC with remap_block like the data tensors.

Verification

nvidia-cutlass-dsl 4.9.0.dev1 (cu13), B200. The output is a view of the first M rows of a larger buffer pre-filled with a sentinel, so any sentinel overwritten past row M is an out-of-bounds store. --op add:

M x N before: elements written out of bounds after
4096 x 4096 0 0
100 x 1000 28024 0
65 x 257 16446 0
1000 x 3 325 0

After the change the result is also correct and in bounds for 512x512, 64x2048, 2048x64, 3x1000, 1x7 and 777x1234, and the example's own reference check passes for add, mul, sub, truediv and leaky_relu at 1000x1000.

Performance at the default 4096x4096 (--benchmark --skip_ref_check --iterations 300, four alternating runs on a GPU shared with other jobs): 0.053-0.069 ms before, 0.060-0.076 ms after. The ranges overlap and the run-to-run noise is larger than any difference, so I can't claim it is neutral to better than that.

…edicate

The kernel computed frgPred but never used it, so tensors whose shape is
not a multiple of the tile size were read and written out of bounds.
Load and store through predicated cute.copy, and remap the blocks of the
coordinate tensor like the data tensors so the predicate is correct for
non-square block grids.

@fengxie fengxie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good catch on both the unused frgPred and the missing remap_block composition on cC. I verified on B200 that this eliminates all out-of-bounds reads and writes (0 errors under PYTORCH_NO_CUDA_MEMORY_CACHING=1 compute-sanitizer --tool memcheck on non-tile-aligned shapes).

This branch has not been deployed

No deployments
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.

2 participants