Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/transformers/distributed/tensor_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

if is_torch_distributed_available():
import torch.distributed as dist
from torch.distributed._functional_collectives import all_to_all_single
from torch.distributed._functional_collectives import all_to_all_single, wait_tensor

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it safe for torch 2.6+?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yepp

from torch.distributed.tensor import DTensor, Partial, Replicate, Shard, distribute_tensor
from torch.distributed.tensor.placement_types import _StridedShard

Expand Down Expand Up @@ -891,6 +891,9 @@ def ep_forward(hidden_states, top_k_index, top_k_weights):
tokens, expert_ids, order, send_sizes, recv_sizes = self._dispatch_tokens(
hidden_states, top_k_index, module.num_experts, ep_group, ep_size
)
# hand the experts the received rows, not the all-to-all's pending result (whose pointer is null until
# waited on): https://github.com/huggingface/transformers/pull/49421
tokens = wait_tensor(tokens)
expert_output = self._run_local_experts(experts_forward, tokens, expert_ids)
output = self._combine_tokens(
expert_output, top_k_weights, order, send_sizes, recv_sizes, ep_group
Expand Down
Empty file.
26 changes: 26 additions & 0 deletions tests/tensor_parallel/test_tensor_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
from unittest.mock import patch

import torch
from torch.distributed.device_mesh import init_device_mesh

from transformers import AutoModelForCausalLM
from transformers.distributed import tensor_parallel
from transformers.distributed.sharding_utils import DtensorShardOperation
from transformers.distributed.tensor_parallel import (
ALL_PARALLEL_STYLES,
ColwiseParallel,
EpDispatchExpertsParallel,
PackedColwiseParallel,
PackedRowwiseParallel,
RowwiseParallel,
)
from transformers.testing_utils import TestCasePlus, is_tensor_parallel_test

from ..test_tensor_parallel_mixin import _init_distributed


@is_tensor_parallel_test
class TestTensorParallelProperties(TestCasePlus):
Expand Down Expand Up @@ -365,3 +369,25 @@ def test_sharding_does_not_create_unrelated_module_attributes(self):

self.assertEqual(module.random_attr, 123)
self.assertFalse(hasattr(module, "num_experts"))

def test_dispatch_hands_experts_the_received_rows(self):
"""The all-to-all's pending result (`AsyncCollectiveTensor`) holds a null pointer until a torch op waits on
it, and experts that launch kernels on raw pointers would read it: the dispatch waits before any experts run."""
_init_distributed(tp=2, backend="gloo")(_dispatch_hands_experts_the_received_rows)()


class _DummyExperts(torch.nn.Module):
def __init__(self):
super().__init__()
self.num_experts = 2

def forward(self, tokens, expert_ids, weights):
assert type(tokens) is torch.Tensor, type(tokens)
return tokens


# Worker functions for the expert-parallel layer tests, spawned through `_init_distributed`.
def _dispatch_hands_experts_the_received_rows(rank):
hidden_states = torch.arange(32.0).view(4, 8) + 100 * rank
experts = EpDispatchExpertsParallel().install_forward(_DummyExperts(), init_device_mesh("cpu", (2,)))
experts(hidden_states, torch.tensor([[0, 3], [1, 2], [2, 1], [3, 0]]), torch.ones(4, 2))
Loading