Skip to content

[EP] add initial support for NVSHMEM-based all-to-all - #1569

Closed
tianyu-l wants to merge 1 commit into
mainfrom
nvshmem
Closed

tianyu-l wants to merge 1 commit into
mainfrom
nvshmem

Conversation

@tianyu-l

Copy link
Copy Markdown
Contributor

As titled. This PR also does some refactoring around grouped_mm calling, as NVSHMEM-based all-to-all takes num_tokens_per_expert and prepares offsets.

What works

  • when num_local_experts == 1

What doesn't work and needs debugging

  • when num_local_experts > 1

other TODOs

  • let multiple MoE layers share the same input / output buffer
  • add NVSHMEM-based ExpertTensorParallel support (currently only supports ETP=1)

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 14, 2025
Comment on lines +92 to +93
# TODO: why do we need this clone?
return out.clone()

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.

Can you try removing this clone after we added out_buffer.detach() ?

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.

still erroring out if removing this clone

RuntimeError: Output 0 of AllToAllVDev2dBackward is a view and its base or another view of its base has been modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function.

self.output_splits = None

# performing all-to-all dispatch on the input
def _token_dispatch(self, mod, inputs, device_mesh):

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.

i think this new implementation will get rid of the need of torch._dynamo.config.capture_scalar_outputs, avoiding the need to handle unbacked symints

)

out = func(w1, w2, w3, x, num_tokens_per_expert)
out = func(w1, w2, w3, x, num_tokens_per_expert, offsets)

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.

To make it reusable with GPT-oss implementation, can we make w1, w2, w3 somehow to a list of parameters or kwargs (basically take variable number of weights and bias)? I think what we do in this wrapper is just taking these inputs and then pass it further to func().

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.

Another thing which gpt-oss is not reusable is the ExpertTensorParallel(). I guess for this part, if a model has different mathematical formula and variable number of weights/bias, it's user's responsible to update _partition_fn_2d in ETP, wdyt?

@7huahua

7huahua commented Jul 14, 2026

Copy link
Copy Markdown

Hi @tianyu-l, I'm an engineer working on collective-communication operators. I've been studying your NVSHMEM all_to_all_vdev_2d work in #1569 and the later HybridEP PR #2207.

From the output_splits layout it looks like tokens land grouped by expert, and the NVSHMEM path skips the D2H sync — so my mental model is the grouped GEMM can consume the output almost in place, no second permute. Is that right, or am I missing a permute step?

If so, why move to HybridEP instead of pushing this further — mainly engineering gaps (multi-expert, the clone() TODO in backward, CUDA-graph), or does "write-directly-to-expert-major" have a deeper limit? Thanks!

@tianyu-l

Copy link
Copy Markdown
Contributor Author

@7huahua This was an early attempt on utilizing NVSHMEM to do all-to-all, but I wasn't able to make it fully work. I don't think there are fundamental limit.

Later we migrated to more mature solutions like DeepEP and HybridEP which also does token dedup in communication.

Recently @xmfan added MinimalAsyncEP which is a more transparent, hackable attempt that

  • is cudagraph-able
  • doesn't drop token
  • always recompute so no need to worry about saved activation memory

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

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants