Skip to content

fix(rocm): route tensor parallel communication through RCCL - #135

Draft
zihaomu wants to merge 12 commits into
FlashML-org:mainfrom
zihaomu:feat/rocm-rdna4-rccl
Draft

zihaomu wants to merge 12 commits into
FlashML-org:mainfrom
zihaomu:feat/rocm-rdna4-rccl

Conversation

@zihaomu

@zihaomu zihaomu commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Draft follow-up to #132. This branch is based on the #132 head. Until #132 merges, GitHub Files changed also includes the foundation diff; the incremental review scope is three files.

Summary

  • disable the NVIDIA-only custom PyNCCL communicator for multi-GPU ROCm runs;
  • initialize PyTorch distributed with backend nccl, which the ROCm wheel implements through RCCL;
  • retain the existing PyNCCL path on CUDA;
  • make direct PyNCCL module loading fail immediately on ROCm instead of reaching a misleading -lnccl linker failure;
  • validate an actual two-rank collective through Engine._init_communication().

PyNCCL itself is deliberately bypassed, not ported to RCCL.

Incremental review scope

Relative to zihaomu:feat/rocm-rdna3-rdna4-foundation:

  • python/freetoken/engine/engine.py
  • python/freetoken/kernel/pynccl.py
  • tests/engine/test_rocm_communication.py

Diff size: 151 additions, 2 deletions.

Dependency

Validation

  • two AMD Radeon AI PRO R9700 (gfx1201) GPUs, PyTorch 2.11.0+rocm7.14.0, HIP 7.14.60850;
  • tests/engine/test_rocm_communication.py: 4 passed with both GPUs visible;
  • actual Engine._init_communication() selected the default nccl backend (RCCL), created the CPU gloo subgroup, and produced all-reduce result 3.0 on both ranks;
  • unit coverage also proves direct PyNCCL loading fails before native compilation/linking on ROCm.

Known limitation

Full multi-GPU model serving with real weights is not claimed yet. The PR remains Draft until #132 establishes the common ROCm foundation.

@lukascechovic

Copy link
Copy Markdown

Confirming this from a second pair of R9700s (gfx1201, Linux), and adding the number the PR does not
claim: what the torch/RCCL fallback actually costs at a real model's decode shape.

We hit the pynccl break independently while trying to measure the collective through the engine's own
transport, and our reading of it matches yours exactly:

# freetoken/kernel/pynccl.py:29-31
@functools.cache
def _load_nccl_module() -> Module:
    # TODO(ROCm): NCCL -> RCCL migration for multi-GPU tensor parallelism on AMD.
    return load_aot("pynccl", cuda_files=["pynccl.cu"], extra_ldflags=["-lnccl"])

Two independent breaks in that one call: the vendored csrc/include/freetoken/nccl227.h is NVIDIA's
own NCCL 2.27 header with zero __HIP/hip_runtime guards, so the build dies at
fatal error: 'cuda_runtime.h' file not found; and -lnccl has nothing to link against either,
because a ROCm image ships librccl.so.1, not libnccl.so.

⭐ Worth adding to the PR's rationale: the reason this sat unreported is
freetoken/distributed/impl.py:79-80 — enable_pynccl_distributed returns before touching the
builder when tp_info.size == 1. So a TP=1 server parses --use-pynccl, records use_pynccl=True in
ServerArgs, logs it, and never trips on it. Our deployed row does exactly that. The failure only
fires at tp_size = 2, which on AMD nobody had reached. Your "fail immediately on ROCm instead of
reaching a misleading -lnccl linker failure" is the right call — the current error points at a
missing library rather than at an unported header.

The fallback performs fine — measured

Since the PR routes through TorchDistributedImpl / PyTorch's bundled RCCL, here is what that path
costs on two R9700s at the shape that matters. Qwen3.8-Flash-Next decode is one token through 48
layers, so the collective is [1, 2560] bf16 = 5,120 bytes per call:

µs/call x48 layers
back-to-back, 15 banked readings 28.6 – 41.9 1.37 – 2.01 ms/token
host-synced either side (upper bound) 72.4 3.48 ms/token

The back-to-back figure is the honest one — the host-synced arm charges ~30 µs of synchronisation the
engine never pays. A tuning sweep (NCCL_PROTO LL/LL128/Simple, NCCL_ALGO Tree/Ring,
NCCL_P2P_DISABLE) moved nothing: every setting landed inside the same 28–42 µs band, which is
run-to-run jitter, not a tuning win.

RCCL 2.30.4 chose direct peer-to-peer with no host staging, which we did not expect on this board:

Channel 00/0 : 0[3000] -> 1[7000] via P2P/IPC comm 0x17c0dbe0 nRanks 02

⛔ These two cards have no XGMI — they are two hops apart through the AM5 root complex, on x8 gen5
links. torch.cuda.can_device_access_peer(0, 1) is True and RCCL takes it anyway.

⇒ the fallback is not a degraded mode to apologise for. It is fast enough that a per-layer all-reduce
is a real design option on AMD.

One caveat on our numbers

⚠️ 15 of our 16 readings have a banked artifact and span 28.6–41.9 µs. The 27.8 µs low comes from the
tuning table, whose per-setting raw was overwritten; we quote the range rather than the minimum.

Where we are, in case it is useful to your step 5

We serve qwen4_exp (RadixArk/Qwen3.8-Flash-Next-NVFP4, --moe-backend offload) on one of these
two cards under Linux, in production, at a 262,144-token context. So we have some ground truth for the
"full model-serving validation" you have staged as step 5 of #132, and we are glad to run things
against your branches.

⚠️ We have also taken TP=2 for this model as far as it goes today, and it does not load — for reasons
below your layer, not in it. Details in a separate report so as not to derail this PR; the short
version is that qwen4_exp's own tensor-parallel path is unfinished, and separately that replicated
offload banks want 2 × 111.4 GiB of host RAM. Neither is anything a working RCCL collective can fix,
and your PR removes a real obstacle that sits in front of both.

Platform

AMD Radeon AI PRO R9700 x2 (gfx1201), Linux · PyTorch 2.11.0+rocm7.14.0, HIP 7.14.60850, RCCL 2.30.4
· FreeToken main@4b94bdc3 + #132 + #133 + #134 · gfx1201 BLOCK_N clamped to 32 in the QSA attend
kernel (see our separate LDS issue), which does not touch this measurement.

@zihaomu

zihaomu commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

@lukascechovic Thanks for the detailed independent validation. Your observation about TP=1 masking the issue is correct: use_pynccl defaults to true, but enable_pynccl_distributed returns before loading or building PyNCCL when the TP size is one.

I added an explicit regression test for that contract in f75a553. It verifies that the single-rank path stays on Gloo and does not probe the ROCm branch, initialize PyNCCL, or create a second process group.

I also rechecked the communication tests locally on one R9700 (gfx1201) with PyTorch 2.13 nightly and ROCm 7.14:

  • tests/engine/test_rocm_communication.py: 4 passed, 1 skipped
  • the TP=1 engine path initialized Gloo without touching the PyNCCL loader
  • a real single-rank PyTorch nccl process group initialized on ROCm and completed all_reduce successfully
  • direct PyNCCL loading failed early with the intended NVIDIA-only error

The skipped case is the existing real two-rank RCCL test. I was limited to one GPU for this run, so I did not claim an independent two-GPU reproduction here.

I agree that changing only -lnccl to -lrccl would not be a sufficient port: the current custom module also depends on NVIDIA headers, CUDA runtime APIs, and NCCL-specific APIs. Keeping this PR on the PyTorch nccl process-group path backed by RCCL is therefore the appropriately scoped fix.

Your R9700 latency measurements are useful supporting microbenchmark evidence. I will keep the performance claim scoped accordingly: they demonstrate that the RCCL route is viable on that platform, but are not yet an end-to-end TP2 serving result until the model TP path itself is complete.

@lukascechovic

Copy link
Copy Markdown

Following up on the scoping you gave here — you're right that this thread's numbers were transport-only.
The model TP path finished a few hours after your reply, and it serves: +34.1% decode / −39.5% TTFT
at 10k context (+31.5% / −40.4% at 100k) against the same box's TP=1 row, on two R9700s. Full numbers,
the mechanism behind it (your PR #311 disk-PLE was what actually unblocked the host-RAM budget — 111
GiB pinned → 64 GiB), and two things we have not closed (a greedy-text divergence we can't yet
diagnose without logprobs, and an 18-20 min cold load) are on
#62.

No quality claim attached — we don't have a fidelity instrument on this box, which is exactly why the
divergence item is still open. Wanted this on your radar since you're the one who scoped the gap it
fills.

Platform

Same as above: AMD Radeon AI PRO R9700 x2 (gfx1201), Linux, 122 GiB host RAM · PyTorch
2.11.0+rocm7.14.0, HIP 7.14.60850, RCCL 2.30.4 · FreeToken main@4b94bdc3 + #132/#133/#134 + #311 + our
sharding work.

samuelishida pushed a commit to samuelishida/FreeToken that referenced this pull request Sep 5, 2026
What:
- Remove .agents/learnings and .plans/rocm-consolidation files from the branch.
- Remove internal increment and plan-path references from source comments and public installation docs.
- Keep implementation comments that explain correctness, ownership, profiler intent, source attribution, or ROCm safety behavior.
- Clarify public ROCm documentation: gfx1100 has recorded serving smoke on ROCm 7.2.1; the ROCm 7.14.x container is a reference environment, and other target cells remain compile-only until physical serving evidence exists.

Why:
- Keep merge surface focused on code, tests, reproducibility tooling, and user-facing documentation.
- Prevent private planning history, review workflow language, stale plan paths, and local process notes from entering the upstream repository.
- Avoid presenting compile success or a reference container as cross-target serving or performance proof.

Related upstream work informing this branch:
- PR FlashML-org#132: portable ROCm/HIP foundation.
- PR FlashML-org#133: TVM-FFI index/store portability.
- PR FlashML-org#135: RCCL tensor-parallel communication.
- PR FlashML-org#136: native GGUF ROCm build and Q4_0 kernels.
- PR FlashML-org#137: earlier AMD serving bring-up.
- PR FlashML-org#217: source-fork ROCm, Qwen3.5 GGUF, and performance experiments.
- PR FlashML-org#241: gfx1150 build, JIT, Triton, and attention hardening.
- PR FlashML-org#260: gfx1151 validation and fallback/build evidence.
- PR FlashML-org#316: HIP graph-capture-safe expert copies.
- PR FlashML-org#378: CPU/Hybrid MoE graph replay safety.
- Local branch milestones: 436263f, 926c1e8, e1d1856, 8a70c7e, and e5fd30f.

Evidence:
- 170 focused tests passed after cleanup.
- gfx1100 is the only target with end-to-end Qwen3.5 GGUF serving smoke recorded here.
- Remaining matrix targets are compile-only; no new throughput claim is published without a matching A/B manifest.
@zihaomu
zihaomu force-pushed the feat/rocm-rdna4-rccl branch from f75a553 to 500763c Compare September 16, 2026 07:50
@zihaomu

zihaomu commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

Dependency refresh (2026-09-16): rebased the three RCCL-specific commits onto the conflict-resolved #132 head c2af4c6. git range-diff reports all three commits as patch-equivalent. Focused communication tests in the ROCm 7.14 / PyTorch 2.11 container: 4 passed, 1 skipped; the skipped test is the real two-rank RCCL GPU test because no devices were attached. New head: 500763c; GitHub reports clean/mergeable.

LZ-QWQ added a commit to LZ-QWQ/FreeToken that referenced this pull request Sep 19, 2026
bouclem and others added 12 commits September 21, 2026 10:54
- Add hip_compat.h shim mapping CUDA runtime API to HIP equivalents
- Update pinned_tensor.cpp to compile under both nvcc and hipcc
- Add ROCm detection in arch.py (is_rocm, get_rocm_gfx_arch, is_gfx11xx_family)
- Guard NVIDIA arch checks to return None on ROCm
- Skip nvcc version check in _toolchain.py when on ROCm
- Add ROCm build path in setup.py (ROCM_HOME, amdhip64, --offload-arch)
- Add _hip_cflags() in kernel/utils.py for JIT compilation on ROCm
- Add is_rocm() and driver_hip_version() in backend.py
- Add rocm-smi fallback in __main__.py for clangd generation
- Add TODO(ROCm) for NCCL->RCCL, flashinfer/sgl_kernel ROCm builds,
  Triton autotune RDNA3 tuning, PDL equivalent, hiprtc JIT cache
- Add AMD ROCm classifier in pyproject.toml
@zihaomu
zihaomu force-pushed the feat/rocm-rdna4-rccl branch from 500763c to 3675d03 Compare September 21, 2026 03:01
LZ-QWQ pushed a commit to LZ-QWQ/FreeToken that referenced this pull request Sep 22, 2026
LZ-QWQ pushed a commit to LZ-QWQ/FreeToken that referenced this pull request Sep 22, 2026
LZ-QWQ pushed a commit to LZ-QWQ/FreeToken that referenced this pull request Sep 22, 2026
@zihaomu zihaomu mentioned this pull request Sep 23, 2026
2 of 29 tasks
@zhangnju

zhangnju commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

hi @jason-fxz @andy-yang-1 @zihaomu Thanks @zihaomu for tackling this — ROCm TP comm being broken is a real blocker, and routing the collectives through torch.distributed is a clean, low-maintenance way to unblock it.

I hit the same wall independently and took a slightly different route. Sharing it as input rather than a competing change — happy to converge on whatever the maintainers prefer.

My approach: keep a single unified pynccl path for both CUDA and ROCm. Instead of forking ROCm onto torch.distributed, we ported pynccl.cu to build against RCCL (the NVIDIA symmetric-memory window is #if-gated off on RDNA, but the structure stays shared with the CUDA path).

I put it on a self-contained, rebased-on-main, test-included branch so it's easy to review/pull:

Unified-pynccl vs. torch-forked — the tradeoff

#135 (torch.distributed) pynccl→RCCL (ours)
Code paths ROCm forks onto a second comm backend One pynccl path, CUDA + ROCm
Build deps none (torch handles RCCL) custom C++ + JIT (ninja + librccl)
Symmetric-memory window lost on ROCm gated off on RDNA, kept on CUDA
Maintenance simpler (less C++) one codebase, but C++ surface

Correctness — the all_reduce test above, on 2×R9700 (gfx1201)

pynccl all_reduce(sum) is bit-for-bit identical to torch.distributed across shapes/dtypes (the test's oracle):

shape dtype pynccl == torch
(1, 2048) bf16 ✅ exact
(4, 4096) fp16 ✅ exact
(2, 7, 333) bf16 ✅ exact
(8192, 2048) bf16 ✅ exact

1 passed in 9.27s. (pynccl registers the dtypes the TP engine uses — bf16/fp16; fp32 isn't routed through the collective.) The test skips cleanly when <2 GPUs are visible, and runs on CUDA (NCCL) too.

Benchmark — [1, 2048] bf16 all-reduce (decode shape), same 2×R9700

mode pynccl (RCCL) torch-nccl ratio
eager ~9 µs ~26–33 µs ~3× faster
CUDA-graph captured ~12.4 µs ~12.4 µs ~equal

Honest read: the eager 3× gap closes under CUDA-graph capture, and FreeToken captures the decode hot path — so for captured decode the two are effectively equal. The gap matters for un-captured / eager paths.

Either way: TP correctness validation we can contribute

Our TP weight-sharding work is comm-agnostic (goes through DistributedCommunicator), so it rides on #135's backend or ours interchangeably. Validated on RDNA + CUDA:

  • Qwen3.5-9B dense TP=2 byte-identical to TP=1 (greedy); TP=4 coherent
  • Qwen3.6-35B-A3B bf16 TP=4 / TP=8 coherent
  • Qwen3.6-35B-A3B-FP8 TP=1(offload) == TP=2 == TP=4 byte-identical on 5 prompts
  • Same run via --disable-pynccl (the torch.distributed path) → also correct ⇒ confirms fix(rocm): route tensor parallel communication through RCCL #135's backend produces identical results
  • End-to-end fp8 35B-A3B TP=4 with pynccl on 4×R9700 → coherent output
  • CUDA 2×4090: 9B TP=2 correct via both backends, byte-identical to ROCm

So whichever comm backend lands, we can supply a TP correctness matrix to go with it.

It'd be great to settle the comm direction sooner rather than later — our TP sharding work sits on top of it, so an early call unblocks the next steps for everyone. If the maintainers feel the unified-pynccl route is the better long-term fit, the pynccl-rccl-only branch is already rebased on main with a test, and I'm happy to open a standalone PR for it. Either way, glad to help test #135 on our R9700 boxes 🙂

LZ-QWQ added a commit to LZ-QWQ/FreeToken that referenced this pull request Oct 8, 2026
Integrate FlashML-org#133, FlashML-org#134, FlashML-org#135, FlashML-org#378 and FlashML-org#491 before FlashML-org#587. Preserve the tested FlashML-org#378 handshake and runtime-only linkage, and retain the FlashML-org#134 installation document. Exclude CI and closed GGUF PR FlashML-org#136.
LZ-QWQ added a commit to LZ-QWQ/FreeToken that referenced this pull request Oct 9, 2026
Use upstream FlashML-org#638 and FlashML-org#134; integrate incremental FlashML-org#135 and FlashML-org#378 plus the updated standalone FlashML-org#491 before FlashML-org#587. Retain the probed FlashML-org#378 handshake and upstream installation document.
@TashaSkyUp

Copy link
Copy Markdown

Sharing a hardware-validated reference implementation/data point for anyone working on ROCm TP here — not proposing this as an autonomous upstream PR.

I have a fork branch for Qwen3.8-Flash-Next TP=2 on 2× Radeon AI PRO R9700 (gfx1201) using the torch.distributed/RCCL path:

https://github.com/TashaSkyUp/FreeToken/tree/draft/qwen4-tp-precision-r9700

The useful result for this thread is that the communication path itself is not the remaining correctness blocker. With a precision-stable TP profile applied symmetrically to TP1 and TP2, the current branch gets 100/100 exact greedy cross-TP text matches on the audited 1,024-token cohort (440 total responses across the full experiment). The earlier profile was only 77/100, so we traced the divergence to TP-sensitive arithmetic rather than RCCL transport.

On the 60 short prompts with identical text/token count/finish reason across both modes:

config TTFT decode ms/token decode tok/s
TP1 11.134 s 78.929 12.673
TP2 5.825 s 59.179 16.898

Stack: Ubuntu 24.04.4, ROCm SDK 7.2.3 / HIP 7.2.53211, torch 2.14.1+rocm7.2, Triton 3.8.0, Qwen3.8-Flash-Next NVFP4, host offload, --disable-pynccl.

The detailed writeup, exact commands, frozen requests/results, and audit artifacts are here:
https://github.com/TashaSkyUp/FreeToken/blob/draft/qwen4-tp-precision-r9700/docs/qwen4-exp-tp-r9700.md

For anyone implementing upstream: this suggests #135's torch/RCCL fallback is already sufficient for correct Qwen3.8 TP=2; the harder part is keeping TP1/TP2 arithmetic stable through the model-specific sharded attention/GDN/MoE paths. Happy for maintainers/contributors to reuse the measurements or implementation ideas from the fork.

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.

6 participants