Skip to content

fix: MNNVL Allreduce uses bitwise sentinel checking to avoid subnormal value issue (#3053) - #3304

Merged
kahyunnam merged 3 commits into
flashinfer-ai:mainfrom
timlee0212:fix/mnnvl-allreduce-ftz-sentinel-hang
May 13, 2026
Merged

kahyunnam merged 3 commits into
flashinfer-ai:mainfrom
timlee0212:fix/mnnvl-allreduce-ftz-sentinel-hang

Conversation

@timlee0212

@timlee0212 timlee0212 commented May 12, 2026

Copy link
Copy Markdown
Contributor

📌 Description

This PR fixed an inconsistency when polling communication buffer and checking for sentinel value.

The old code does FP comparison with 0 and checks the sign bit to determine whether the polled value is -0.0; The caveat is that when the valid input contains a negative subnormal value, the FP comparison could cause FTZ behavior and flush the valid input into negative zero, which collides with the sentinel value and makes the kernel stuck at polling.

The solution is simple. This PR uses bitwise comparison to check if the incoming value is negative zero, avoiding such subnormal flushing from happening before polling.

Verified the mentioned hang issues reported in SGLang and VLLM can be solved with this fix.

🔍 Related Issues

#3053

also related to the issue vllm-project/vllm#35772

🚀 Pull Request Checklist

Thank you for contributing to FlashInfer! 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.

If you are unsure about how to set up pre-commit, see the pre-commit documentation.

🧪 Tests

  • Tests have been added or updated as needed.
  • All tests are passing (unittest, etc.).

Reviewer Notes

Summary by CodeRabbit

  • Bug Fixes

    • Strengthened negative-zero sentinel detection for FP32 by switching to bit-exact matching and addressing GPU flush-to-zero edge cases to avoid accidental sentinel matches and potential allreduce hangs.
  • Tests

    • Added a regression test that injects targeted sentinel patterns across data types and fusion modes to guard against deadlocks and ensure robust sentinel handling.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR makes negative-zero detection in the MNNVL allreduce kernel bit-exact and adds a parametrized regression test that injects specific sentinel trigger bit-patterns into inputs to exercise and guard the Lamport polling logic.

Changes

Sentinel Negative-Zero Fix and Regression Test

Layer / File(s) Summary
Kernel negative-zero constant and bit-exact detection
include/flashinfer/comm/trtllm_mnnvl_allreduce.cuh
The kNEGZERO_FP32 constant (0x80000000) is added, and utils::isNegZero<float> is updated to use __float_as_uint(val) == kNEGZERO_FP32 with an expanded comment about FTZ/subnormal comparison pitfalls.
Test sentinel injection helper and extended test harness
tests/comm/test_trtllm_mnnvl_allreduce.py
Added _inject_sentinel_trigger_patterns which writes int16 patterns (neg-subnormal, -0.0, pos-subnormal) into the rank-0 input. prepare_test_data and run_mnnvl_ar_full gain an inject_sentinel_patterns flag that is propagated through initialization.
Parametrized sentinel regression test
tests/comm/test_trtllm_mnnvl_allreduce.py
New pytest test_mnnvl_allreduce_sentinel_patterns runs the allreduce with inject_sentinel_patterns=True across fusion modes, fp16/bf16, and multiple hidden-size/seq-len configurations.

Sequence Diagram

sequenceDiagram
  participant Test
  participant run_mnnvl_ar_full
  participant prepare_test_data
  participant _inject_sentinel_trigger_patterns
  participant AllreduceKernel

  Test->>run_mnnvl_ar_full: call with inject_sentinel_patterns=True
  run_mnnvl_ar_full->>prepare_test_data: forward inject_sentinel_patterns=True
  prepare_test_data->>_inject_sentinel_trigger_patterns: x_full tensor
  _inject_sentinel_trigger_patterns->>prepare_test_data: injected int16 patterns
  prepare_test_data->>run_mnnvl_ar_full: initialized inputs (broadcast)
  run_mnnvl_ar_full->>AllreduceKernel: execute allreduce (kernel uses isNegZero)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • flashinfer-ai/flashinfer#2118: Prior work touching utils/isNegZero scaffolding and the fused MNNVL allreduce path; closely related to this bit-exact neg-zero refinement.

Suggested reviewers

  • aleozlx
  • yzh119
  • bkryu
  • jimmyzho
  • nv-yunzheq

Poem

🐰 I nibbled bits in the kernel's bed,
Found -0.0 hiding in the FP thread,
I fixed the sentinel with a bit-exact cheer,
Now polling stays awake — no deadlock near! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main fix: using bitwise sentinel checking in MNNVL Allreduce to address a subnormal value issue.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description is comprehensive and well-structured, addressing the problem, solution, and verification.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@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 updates the isNegZero function in the MNNVL allreduce kernel to use bit-exact comparisons for floating-point values, preventing deadlocks caused by Flush-To-Zero (FTZ) behavior on newer hardware where subnormal values could be incorrectly identified as the Lamport sentinel. It also introduces a regression test that injects specific bit patterns to verify the sentinel polling logic. Feedback was provided to use the __float_as_uint intrinsic instead of reinterpret_cast for safer and more idiomatic bit-level conversion in CUDA.

static inline __device__ bool isNegZero(T val) {
if constexpr (std::is_same_v<T, float>) {
return val == 0.F && signbit(val);
return *reinterpret_cast<uint32_t const*>(&val) == kNEGZERO_FP32;

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

Using reinterpret_cast for type punning can lead to undefined behavior due to strict aliasing rules. In CUDA __device__ code, the idiomatic and safe way to perform bit-level conversion from float to uint32_t is using the __float_as_uint intrinsic. This is more readable and ensures the compiler handles the register move correctly without potential memory round-trips.

    return __float_as_uint(val) == kNEGZERO_FP32;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Resolved

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

🧹 Nitpick comments (1)
include/flashinfer/comm/trtllm_mnnvl_allreduce.cuh (1)

122-123: 💤 Low value

Optional: prefer __float_as_uint for the fp32 bit-cast.

The fp16/bf16 branch right below uses the Fp16BitCast union for type punning, while the new fp32 branch uses a pointer reinterpret_cast. The CUDA intrinsic __float_as_uint is the idiomatic device-side bit-cast for float → uint32_t, sidesteps strict-aliasing UB on the pointer cast, and avoids forcing val to memory just to take its address. Functionally equivalent; purely a cleanup.

♻️ Proposed cleanup
   if constexpr (std::is_same_v<T, float>) {
-    return *reinterpret_cast<uint32_t const*>(&val) == kNEGZERO_FP32;
+    return __float_as_uint(val) == kNEGZERO_FP32;
   } else if constexpr (std::is_same_v<T, __nv_bfloat16> || std::is_same_v<T, __nv_half>) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@include/flashinfer/comm/trtllm_mnnvl_allreduce.cuh` around lines 122 - 123,
Replace the pointer-based bitcast in the float branch with the CUDA intrinsic:
in the code path guarded by if constexpr (std::is_same_v<T, float>) (the
comparison against kNEGZERO_FP32), use __float_as_uint(val) instead of
*reinterpret_cast<uint32_t const*>(&val) to perform the float→uint32_t bit-cast;
this matches the fp16/bf16 union approach, avoids
strict-aliasing/memory-addressing issues, and keeps the check against
kNEGZERO_FP32 unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@include/flashinfer/comm/trtllm_mnnvl_allreduce.cuh`:
- Around line 122-123: Replace the pointer-based bitcast in the float branch
with the CUDA intrinsic: in the code path guarded by if constexpr
(std::is_same_v<T, float>) (the comparison against kNEGZERO_FP32), use
__float_as_uint(val) instead of *reinterpret_cast<uint32_t const*>(&val) to
perform the float→uint32_t bit-cast; this matches the fp16/bf16 union approach,
avoids strict-aliasing/memory-addressing issues, and keeps the check against
kNEGZERO_FP32 unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 16b2bf7b-ecf3-455c-aae8-702e24a32a7e

📥 Commits

Reviewing files that changed from the base of the PR and between 6aba765 and c392120.

📒 Files selected for processing (2)
  • include/flashinfer/comm/trtllm_mnnvl_allreduce.cuh
  • tests/comm/test_trtllm_mnnvl_allreduce.py

@timlee0212

Copy link
Copy Markdown
Contributor Author

/bot run

@flashinfer-bot

Copy link
Copy Markdown
Collaborator

@timlee0212 is not authorized to trigger this CI job. cc: @yzh119, @sricketts, @yongwww

@aleozlx

aleozlx commented May 13, 2026

Copy link
Copy Markdown
Member

/bot run

@aleozlx aleozlx added the run-ci label May 13, 2026
@flashinfer-bot

Copy link
Copy Markdown
Collaborator

GitLab MR !665 has been created, and the CI pipeline #51101144 is currently running. I'll report back once the pipeline job completes.

@wenscarl

Copy link
Copy Markdown
Collaborator

Verified to resolve #3053.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants