Skip to content

[CuTeDSL] clc_response: query get_first_ctaid only when is_canceled is true - #3937

Open
builtin-pb wants to merge 1 commit into
NVIDIA:mainfrom
builtin-pb:cute-dsl-clc-response-guard
Open

builtin-pb wants to merge 1 commit into
NVIDIA:mainfrom
builtin-pb:cute-dsl-clc-response-guard

Conversation

@builtin-pb

Copy link
Copy Markdown
Contributor

Summary. cute.arch.clc_response issues clusterlaunchcontrol.query_cancel.get_first_ctaid whether or not the try_cancel request succeeded, but the PTX ISA leaves that query undefined after a failed request, and every persistent CTA decodes a failed response once no cluster is left to cancel. This PR issues the query only under the is_canceled predicate, in one inline PTX block like the C++ sm100 tile scheduler's, and returns 0 coordinates when the request failed.

Problem

  • clc_response (python/CuTeDSL/cutlass/cute/arch/clc.py) loads the 16-byte response, then emits four NVVM queries in a row: is_canceled, then get_first_cta_id_x, _y and _z. Nothing makes the last three depend on the first; callers test is_valid afterwards.
  • The CuTe DSL's CLC schedulers go through it: cutlass.utils.ClcDynamicPersistentTileScheduler.work_tile_info_from_clc_response, its copy in examples/python/CuTeDSL/helpers/dynamic_persistent_tile_scheduler.py, examples/python/CuTeDSL/helpers/fmha_helpers.py and python/CuTeDSL/cutlass/experimental/task_scheduling/resources.py.
  • In the PTX of the CLC GEMM example dense_gemm_persistent_dynamic.py (v4.8.1, sm_100a, 2×1 cluster, 2-CTA MMA), two of the four decode sites read ::x and ::y before the branch on is_canceled. At the other two, LLVM placed the reads after the branch, on the fall-through path, which runs when the request failed. The second site, with the { .reg .b128 %clc_handle; ... } wrappers elided:
ld.shared.v2.b64 {%rd6, %rd7}, [%r10];
clusterlaunchcontrol.query_cancel.is_canceled.pred.b128 %p52, %clc_handle;
fence.proxy.async.shared::cta;
...
@%p52 bra $L__BB0_43;      // canceled: back to the loop
clusterlaunchcontrol.query_cancel.get_first_ctaid::x.b32.b128 %r176, %clc_handle;
add.s32 %r529, %r176, %r5;
clusterlaunchcontrol.query_cancel.get_first_ctaid::y.b32.b128 %r530, %clc_handle;
  • FlashAttention 4's CLC forward kernel, which uses ClcDynamicPersistentTileScheduler, has five such sites in a head-dim-128 build and five in a head-dim-256 2-CTA build.
  • Not covered here: examples/python/CuTeDSL/experimental/rubin/kernel/slice_k/sm107_slicek_gemm_wdm.py (ClcPersistentScheduler.get_task_info) issues the same four queries itself, through the experimental 1:1 primitives, in the same order. We left that example alone.

Change

clc_response keeps its signature and its 128-bit load, and decodes the response in one PTX block:

mov.b128 clc_result, {lo, hi};
mov.b32 x, 0; mov.b32 y, 0; mov.b32 z, 0;
clusterlaunchcontrol.query_cancel.is_canceled.pred.b128 p, clc_result;
selp.b32 valid, 1, 0, p;
@p clusterlaunchcontrol.query_cancel.get_first_ctaid.v4.b32.b128 {x, y, z, _}, clc_result;

A failed request now returns (0, 0, 0, 0) instead of undefined coordinates; is_valid keeps its meaning. The docstring says so.

Why this is correct

PTX ISA 9.4, §9.7.15.19 (clusterlaunchcontrol.query_cancel): "If the request fails the behavior of clusterlaunchcontrol.query_cancel.get_first_ctaid is undefined."

  • The same section calls the .v4 form's fourth element unspecified, so it separates an unspecified value from undefined behavior, and a decode after a failed request is the latter.
  • Its Examples show the predicated .v4 form used here, and include/cutlass/gemm/kernel/sm100_tile_scheduler.hpp:416–428 does the same with @p1. The Examples also list the scalar ::x, ::y and ::z forms without a predicate, but those lines show syntax; the Description's sentence above defines the behavior.
  • Why inline PTX and not a DSL if: LLVM defines these intrinsics with NVVMPureIntrinsic, which adds IntrNoMem and IntrSpeculatable (IntrinsicsNVVM.td at 109bda6, L3965–L3974). LLVM already moves these queries freely: at two of the GEMM's sites above it sank them past the branch. IntrSpeculatable also lets it hoist a query out of an if on is_canceled. With the predicate inside the asm block, no transformation can separate the query from its guard.

Cost and alternatives

  • Cost: per fetched tile, one predicated .v4 query instead of one to three scalar ones, plus three movs and a selp.
  • A caller that ignores the coordinates now also gets the predicated query, which LLVM used to drop as dead code: one extra predicated instruction at that site.
  • Alternative: an if on is_valid around the three NVVM ops. Shorter, but it relies on LLVM not moving speculatable intrinsics out of the branch.
  • Not in this PR: whether LLVM should drop IntrSpeculatable from get_first_ctaid. This change doesn't depend on it.

Testing

  • Ran, CPU only: nvidia-cutlass-dsl 4.8.1 with this PR's clc.py loaded over the installed one, target sm_100a. The 4.8.1 file differs from main's only by keyword-only markers.
    • examples/python/CuTeDSL/cute/blackwell/kernel/dense_gemm/dense_gemm_persistent_dynamic.py at v4.8.1, through its compile_bmm with fake tensors (fp16, 1024×1024×512, MMA tile 256×256, 2×1 cluster, 2-CTA). It compiles, and all 4 decode sites become is_canceled plus @p get_first_ctaid.v4. Before: 4 sites with unguarded ::x and ::y.
    • FlashAttention 4 forward at Dao-AILab/flash-attention b038b49, in two builds that pick the CLC scheduler by default:
      • causal, fp16, seqlen 512/768, head dim 128: 6 decode sites, all predicated. Before: 5 unguarded sites and 1 that reads only is_canceled.
      • causal, head dim 256, 32 heads, seqlen 1024, 2-CTA with multicast try_cancel: 11 sites, all predicated. Before: 5 unguarded sites and 6 that read only is_canceled.
    • ptxas 13.4 (-arch=sm_100a) assembles every PTX, before and after.
  • Evidence: latent. We found it by checking the generated PTX against the ISA and have not seen a failure.
  • Not run: no GPU run of the examples or tests.

…s true

PTX leaves clusterlaunchcontrol.query_cancel.get_first_ctaid undefined
when the try_cancel request failed, but clc_response issued the three
get_first_ctaid queries unconditionally, and every persistent CTA decodes
a failed response once no cluster is left to cancel.

Issue the queries in one inline PTX block, with get_first_ctaid.v4
predicated on is_canceled as in the C++ sm100 tile scheduler, and return
0 coordinates when the request failed. A branch around the NVVM ops would
not be enough: LLVM marks the query intrinsics speculatable, so it may
hoist them above the branch.

Signed-off-by: Bo Peng <builtin.pb@gmail.com>

@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.

Thanks for fixing. Approved.

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