Skip to content

ignition/sampling: add <=16-SM probability-space top-k fallback - #72

Draft
FujitsuPolycom wants to merge 1 commit into
mainfrom
jetson/pr-c-sm16-flashinfer-sampling
Draft

FujitsuPolycom wants to merge 1 commit into
mainfrom
jetson/pr-c-sm16-flashinfer-sampling

Conversation

@FujitsuPolycom

@FujitsuPolycom FujitsuPolycom commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Why

ignition.components.flashinfer_sampling.filter_probabilities_out calls torch.ops.flashinfer.top_k_mask_logits_out before softmax and top_p_renorm_probs_out. In FlashInfer (include/flashinfer/topk.cuh and csrc/renorm.cu), RadixTopKMaskLogitsMultiCTA rejects multi-CTA launches with cudaErrorNotSupported whenever multi_processor_count <= 16 and the vocabulary exceeds one CTA's shared memory (~24K FP32 entries at 99 KiB opt-in shared memory). That aborts native core export and CUDA-graph capture on low-SM GPUs such as the 8-SM Jetson Orin Nano even when the required CTAs per row fit within the available SMs (for example, 65,536 tokens requiring 3 CTAs per row on 8 SMs).

Conversely, RadixTopKRenormProbMultiCTA has no ctas_per_group <= multi_processor_count launch guard, so unconditionally calling it for vocabularies that require more CTAs per row than SMs (such as 248,320 tokens on 8 SMs) would deadlock its inter-CTA software barrier (AdvanceRadixGroupBarrier). Routing only multi-CTA vocabularies whose FP32 post-softmax probabilities fit within multi_processor_count to an in-place, torch.library.custom_op-registered deterministic top_k_renorm_probs_out after softmax—keeping single-CTA vocabularies on top_k_mask_logits_out and failing fast when FP32 prob_ctas > multi_processor_count—unblocks low-SM GPUs cleanly while preserving compile_forward / aot_forward tracing and leaving > 16-SM GPUs unchanged.

What changed

Before this change, filter_probabilities_out unconditionally called torch.ops.flashinfer.top_k_mask_logits_out on every CUDA device.

  • ignition/components/flashinfer_sampling.py:
    • Before: Always executed top_k_mask_logits_out -> softmax -> top_p_renorm_probs_out.
    • After:
      • Registers torch.library.custom_op("flare::top_k_renorm_probs_out", mutates_args=("renorm_probs", "row_states_buffer")) with a register_fake kernel, forwarding directly to cached sampling.gen_sampling_module().build_and_load().top_k_renorm_probs(...) in FlashInfer's exact C++ argument order (probs, renorm_probs, maybe_top_k_arr, top_k_val, is_deterministic, row_states_buffer). Returning None with a fake kernel allows make_fx(..., tracing_mode="fake"), _lower_storage, and aot_forward's _serialized_invocations to trace, lower, and serialize the op cleanly.
      • Adds _use_prob_space_top_k(logits). For > 16-SM CUDA devices and non-CUDA tensors, it returns False (unchanged top_k_mask_logits_out path). For <= 16-SM CUDA devices, it computes CTA counts from shared_memory_per_block_optin (falling back to shared_memory_per_block, reserving 4096 bytes for fixed dynamic and static CUB shared-memory overhead):
        • Single-CTA logit vocabularies (logit_ctas <= 1, using logits.element_size()) return False and stay on barrier-free top_k_mask_logits_out.
        • Because module.softmax unconditionally widens logits to FP32, the multi-CTA residency check computes prob_ctas using 4-byte FP32 elements: when prob_ctas <= multi_processor_count, filter_probabilities_out runs module.softmax(...) -> torch.ops.flare.top_k_renorm_probs_out(probabilities, probabilities, top_k, 0, row_states, True) -> torch.ops.flashinfer.top_p_renorm_probs_out(...) in place (zero extra [rows, vocab] FP32 intermediate buffer, using deterministic fixed-point renormalization in existing row_states).
        • When prob_ctas > multi_processor_count, _use_prob_space_top_k raises RuntimeError immediately instead of launching a deadlocking multi-CTA barrier.
  • ignition/components/sampling.py:
    • Updates PreparedSampling's docstring to note that multi-CTA top-k runs in probability space after softmax on <= 16-SM GPUs.
  • tests/components/test_flashinfer_sampling_dispatch.py:
    • Adds CPU unit tests verifying SM-count and CTA-budget dispatch (8/16 SMs with 65,536 and 154,880 vocabularies; single-CTA 16,384 on 8 SMs; 17/170 SMs), exact non-zero cuda:1 device forwarding, fail-fast RuntimeError when vocab_size = 248,320 exceeds 8 SMs for both FP32 and BF16/FP16 logits, non-CUDA handling, exact C++ argument-order forwarding in torch.ops.flare.top_k_renorm_probs_out (tensor and None maybe_top_k_arr), and real make_fx(..., tracing_mode="fake") + _lower_storage + _serialized_invocations tracing.
  • Compatibility & defaults: Existing signed cores and > 16-SM (SM120, SM121, SM103) or single-CTA exports are identical. No new workspace buffers or PreparedSampling ABI changes are introduced.

Claimed benefit and result

  • Expected capability on <= 16-SM GPUs (not yet GPU-verified on 8bd243e6): Unblocks native export, compile_forward / aot_forward tracing, and CUDA-graph capture on low-SM GPUs for multi-CTA vocabularies whose FP32 probabilities satisfy prob_ctas <= multi_processor_count (such as 65,536 and 154,880 tokens on an 8-SM Jetson Orin Nano) without allocating a duplicate [rows, vocab] FP32 buffer, while failing fast instead of deadlocking when prob_ctas > multi_processor_count.
  • Numerical semantics: Probability-space softmax -> top_k_renorm_probs(..., is_deterministic=True) is monotone with logit-space top-k up to FP32 softmax underflow/rounding ties on extreme negative logits and FlashInfer's 2^44 fixed-point probability renormalization.
  • Impact on > 16-SM GPUs: Zero code-path or performance change (SM120, SM121, SM103 remain on top_k_mask_logits_out).

Supporting evidence and limitations

Tested on clean git archive 8bd243e6f82e614c2d7494197bddf33a8ac258a6 atop NVIDIA/hyperflare-inference main (7bbc6707792aa1cb8633794ee189b7592091cc45):

  • Layer 1 — CPU lint, dispatch, tracing, and boundary tests (passed on 8bd243e6f82e614c2d7494197bddf33a8ac258a6):
    • ruff==0.12.8 check ignition/components/flashinfer_sampling.py ignition/components/sampling.py tests/components/test_flashinfer_sampling_dispatch.py: All checks passed!.
    • CUDA_VISIBLE_DEVICES="" pytest tests/components/test_flashinfer_sampling_dispatch.py tests/preparation/test_core_boundary.py -v: 16 passed (12/12 dispatch, FP32/BF16 residency-guard, C++ argument-order forwarding, and real make_fx + _lower_storage + _serialized_invocations tracing tests; 4/4 core boundary checks).
    • Layers 2 and 3 (scheduling simulator, TLA+, Kani) do not apply because this change is confined to Python preparation-time sampling dispatch.
  • Unrun GPU checks & limitations (required before Ready / upstream):
    • Live unmocked CUDA execution (FLARE_RUN_CUDA_TESTS=1 pytest tests/components/test_sampling_flashinfer.py covering eager, compile_forward=True, and compile_forward='aot', CUDA-graph capture/replay, in-place probs == renorm_probs aliasing verification, row_states workspace reset across replays, and numerical parity comparison against top_k_mask_logits_out -> softmax) has not yet been run on 8bd243e6f82e614c2d7494197bddf33a8ac258a6 on either > 16-SM (SM120/SM121) or <= 16-SM (SM87) hardware.
    • Vocabularies requiring prob_ctas > multi_processor_count (such as 248,320 tokens on an 8-SM GPU with 99 KiB opt-in shared memory) intentionally raise RuntimeError rather than deadlocking RadixTopKRenormProbMultiCTA.

Discord: Dooner

@FujitsuPolycom
FujitsuPolycom force-pushed the jetson/pr-c-sm16-flashinfer-sampling branch from 32e7121 to 31c47f6 Compare December 4, 2026 21:57
@FujitsuPolycom FujitsuPolycom changed the title ignition/sampling: use probability-space top-k fallback on <=16-SM GPUs ignition/sampling: add <=16-SM probability-space top-k fallback Dec 4, 2026
FlashInfer's RadixTopKMaskLogitsMultiCTA rejects multi-CTA launches with
cudaErrorNotSupported whenever multi_processor_count <= 16 and the
vocabulary exceeds one CTA's shared memory (~24K FP32 entries at 99 KiB
opt-in SMEM). That aborts native core export and graph capture on low-SM
GPUs such as the 8-SM Jetson Orin Nano even when the row group fits in
available SMs (for example, 65,536 tokens using 3 CTAs per group).

- ignition/components/flashinfer_sampling.py:
  - Before: filter_probabilities_out unconditionally called
    torch.ops.flashinfer.top_k_mask_logits_out before softmax.
  - After: registers torch.library custom op flare::top_k_renorm_probs_out
    (-> None, mutates_args=("renorm_probs", "row_states_buffer"), with
    register_fake so make_fx, _lower_storage, and aot_forward
    _serialized_invocations trace and serialize it cleanly), forwarding
    directly to cached sampling.gen_sampling_module().build_and_load().
    On <=16-SM CUDA devices, _use_prob_space_top_k computes CTA counts
    from shared_memory_per_block_optin (reserving 4096 B overhead):
    single-CTA logit vocabularies stay on barrier-free
    top_k_mask_logits_out; multi-CTA vocabularies whose FP32 post-softmax
    probabilities fit within multi_processor_count run in-place
    deterministic flare::top_k_renorm_probs_out after softmax (zero extra
    [rows, vocab] FP32 buffer); vocabularies whose FP32 probabilities
    exceed multi_processor_count fail fast with RuntimeError instead of
    deadlocking RadixTopKRenormProbMultiCTA's inter-CTA barrier. Devices
    with > 16 SMs keep the unchanged top_k_mask_logits_out path.
- ignition/components/sampling.py:
  - Updates PreparedSampling docstring for <=16-SM probability-space order.
- tests/components/test_flashinfer_sampling_dispatch.py:
  - Verifies SM-count and CTA-budget dispatch, device forwarding on
    cuda:1, fail-fast deadlock prevention for both FP32 and BF16/FP16
    logits (vocab 248,320 on 8 SMs), non-CUDA handling, exact C++
    argument-order forwarding in flare::top_k_renorm_probs_out, and CPU
    make_fx + _lower_storage + _serialized_invocations tracing.
- Compatibility: no core format or workspace changes; > 16-SM and
  single-CTA exports are identical.

Result:
- Expected to unblock native core export and graph capture on <=16-SM
  GPUs for multi-CTA vocabularies up to multi_processor_count CTAs/row
  without allocating an extra probability tensor or hanging on oversized
  vocabularies.

Evidence:
- Verified on clean git archive via ruff==0.12.8 check and
  CUDA_VISIBLE_DEVICES="" pytest
  tests/components/test_flashinfer_sampling_dispatch.py
  tests/preparation/test_core_boundary.py (including custom-op argument
  forwarding and real make_fx, _lower_storage, and
  _serialized_invocations checks).
- Limitations: live CUDA execution (FLARE_RUN_CUDA_TESTS=1 pytest
  tests/components/test_sampling_flashinfer.py) and numerical parity on
  both >16-SM and <=16-SM (SM87) GPUs remain unrun on this revision.

Discord: Dooner

Design-Signed-Off-By: Antigravity (Gemini 4 Neon); design-review=2e5b
Signed-off-by: Dooner <dooner@users.noreply.github.com>
@FujitsuPolycom
FujitsuPolycom force-pushed the jetson/pr-c-sm16-flashinfer-sampling branch from 31c47f6 to 8bd243e Compare December 4, 2026 22:04
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