Skip to content

fix/test(moe_ep): self-bootstrap 1-rank process group in dg mega oracle test - #4221

Merged
mhoqueanik merged 1 commit into
mainfrom
fix-dg-oracle-test-selfboot-dist
Jul 30, 2026
Merged

mhoqueanik merged 1 commit into
mainfrom
fix-dg-oracle-test-selfboot-dist

Conversation

@mhoqueanik

@mhoqueanik mhoqueanik commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

test_deep_gemm_mega_kernel_matches_torch_reference (added in #3980) fails under plain pytest with "ValueError: ... environment variable RANK expected, but not set": its WORLD_SIZE guard only skips when the variable is set and != 1, so without torchrun it falls through to dist.init_process_group(backend="nccl"), whose default env:// rendezvous requires torchrun's RANK/MASTER_* variables.

This is only reachable on CI jobs that combine plain-pytest discovery of tests/moe_ep, a cu13 image with the EP stack (deep_gemm) installed, and a capability-10 GPU — i.e. the B300 cu130 unit-test job, where it currently errors on every run.

Fix: when RANK is absent, self-bootstrap a 1-rank NCCL group via an explicit tcp://127.0.0.1: init (rank=0, world_size=1) instead of env://. Deliberately avoids os.environ mutation so RANK/MASTER_* don't leak to later tests in the same pytest process; teardown is unchanged (conftest.pytest_sessionfinish destroys the group). The torchrun path is preserved and run_tests.sh still exercises it.

Verified on GB200 (single GPU): both launch modes pass with rel_l2=0.0027 vs the torch oracle; the plain-pytest mode was confirmed with RANK/WORLD_SIZE/MASTER_ADDR/MASTER_PORT/LOCAL_RANK explicitly unset.

AI-assisted (Claude Code).

📌 Description

🔍 Related Issues

🚀 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

  • Tests
    • Improved test compatibility by allowing the deep-gemm oracle test to run directly with standard pytest.
    • Added automatic single-process distributed setup when launched outside torchrun.
    • Preserved support for torchrun-based execution and clarified the available test modes.

test_deep_gemm_mega_kernel_matches_torch_reference (added in #3980) fails
under plain pytest with "ValueError: ... environment variable RANK
expected, but not set": its WORLD_SIZE guard only skips when the variable
is set and != 1, so without torchrun it falls through to
dist.init_process_group(backend="nccl"), whose default env:// rendezvous
requires torchrun's RANK/MASTER_* variables.

This is only reachable on CI jobs that combine plain-pytest discovery of
tests/moe_ep, a cu13 image with the EP stack (deep_gemm) installed, and a
capability-10 GPU — i.e. the B300 cu130 unit-test job, where it currently
errors on every run.

Fix: when RANK is absent, self-bootstrap a 1-rank NCCL group via an
explicit tcp://127.0.0.1:<free-port> init (rank=0, world_size=1) instead
of env://. Deliberately avoids os.environ mutation so RANK/MASTER_* don't
leak to later tests in the same pytest process; teardown is unchanged
(conftest.pytest_sessionfinish destroys the group). The torchrun path is
preserved and run_tests.sh still exercises it.

Verified on GB200 (single GPU): both launch modes pass with
rel_l2=0.0027 vs the torch oracle; the plain-pytest mode was confirmed
with RANK/WORLD_SIZE/MASTER_ADDR/MASTER_PORT/LOCAL_RANK explicitly unset.

AI-assisted (Claude Code).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@mhoqueanik mhoqueanik changed the title test(moe_ep): self-bootstrap 1-rank process group in dg mega oracle test fix/test(moe_ep): self-bootstrap 1-rank process group in dg mega oracle test Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The deep-gemm oracle test now supports plain pytest execution by bootstrapping a one-rank NCCL process group, while retaining torchrun’s env:// initialization path.

Changes

Deep-GEMM test execution

Layer / File(s) Summary
Dual process-group initialization
tests/moe_ep/test_deep_gemm_mega_kernel_vs_reference.py, tests/moe_ep/run_tests.sh
The test documents and implements separate initialization paths for torchrun and plain pytest, using an ephemeral local TCP rendezvous for the latter; runner commentary reflects both modes.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Pytest
  participant Socket
  participant NCCL
  Pytest->>NCCL: Use env:// initialization when RANK is set
  alt Plain pytest
    Pytest->>Socket: Bind an available local port
    Socket-->>Pytest: Return TCP rendezvous address
    Pytest->>NCCL: Initialize rank 0, world size 1 via tcp://
  end
Loading

Suggested labels: op: comm

Suggested reviewers: yzh119, jiahanc, aleozlx

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly names the main change: self-bootstrapping a 1-rank process group for the DeepGEMM oracle test.
Description check ✅ Passed The PR description explains the bug, fix, scope, and verification; only optional template items like related issues/checklist are incomplete.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-dg-oracle-test-selfboot-dist

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.

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

Actionable comments posted: 1

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

Inline comments:
In `@tests/moe_ep/test_deep_gemm_mega_kernel_vs_reference.py`:
- Around line 244-262: Expand the existing try/finally in the test setup to
begin immediately after the conditional process-group bootstrap, covering
torch.cuda.set_device(), _make_problem(), and symmetric-buffer creation. Track
whether this test initialized the group (including the plain-pytest path), and
in finally destroy it only when owned by this test, while preserving externally
managed torchrun groups.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 299bb828-76f2-4852-9c04-415052150bef

📥 Commits

Reviewing files that changed from the base of the PR and between 1d97cbb and 964f229.

📒 Files selected for processing (2)
  • tests/moe_ep/run_tests.sh
  • tests/moe_ep/test_deep_gemm_mega_kernel_vs_reference.py

Comment on lines +244 to +262
if "RANK" in os.environ:
# torchrun launch: use its env:// rendezvous.
dist.init_process_group(backend="nccl", timeout=_PG_TIMEOUT)
else:
# Plain-pytest launch (CI unit-test jobs): self-bootstrap a 1-rank
# group with an explicit tcp:// store instead of mutating the
# RANK/MASTER_* env vars, which would leak to later tests in the
# same pytest process. The freed port can theoretically be
# snatched before torch rebinds it; acceptable for a test.
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
port = s.getsockname()[1]
dist.init_process_group(
backend="nccl",
init_method=f"tcp://127.0.0.1:{port}",
rank=0,
world_size=1,
timeout=_PG_TIMEOUT,
)

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.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Ensure bootstrap failures cannot leak the process group.

The group is initialized before torch.cuda.set_device(), _make_problem(), and symmetric-buffer creation, but the cleanup boundary starts afterward. If any of those operations fails, the plain-pytest process group remains initialized for subsequent tests. Move the existing try/finally to cover all post-bootstrap setup and destroy the group only when this test created it.

🤖 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 `@tests/moe_ep/test_deep_gemm_mega_kernel_vs_reference.py` around lines 244 -
262, Expand the existing try/finally in the test setup to begin immediately
after the conditional process-group bootstrap, covering torch.cuda.set_device(),
_make_problem(), and symmetric-buffer creation. Track whether this test
initialized the group (including the plain-pytest path), and in finally destroy
it only when owned by this test, while preserving externally managed torchrun
groups.

@aleozlx aleozlx added the run-ci label Jul 29, 2026
@mhoqueanik
mhoqueanik merged commit 4b964ec into main Jul 30, 2026
68 of 72 checks passed
@mhoqueanik
mhoqueanik deleted the fix-dg-oracle-test-selfboot-dist branch July 30, 2026 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants