Skip to content

[TP] Look up a vocab-parallel embedding on plain tensors - #51248

Open
SunMarc wants to merge 2 commits into
mainfrom
tp-embedding-plain-lookup
Open

SunMarc wants to merge 2 commits into
mainfrom
tp-embedding-plain-lookup

Conversation

@SunMarc

@SunMarc SunMarc commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

A model split with tensor parallelism fails as soon as it runs in a second thread:

  File ".../torch/distributed/tensor/placement_types.py", line 1047, in _reduce_value
    assert self.mask_buffer.data is not None
AssertionError

The embedding with a split vocabulary goes through DTensor's lookup, which keeps some state per thread, and torch reuses it across threads.

Instead, we do the lookup on plain tensors instead. Each rank looks up the ids it owns, returns zeros for the others, and the results are summed. Works for training and inference.

Feel free to push on top of this branch, I didn't gave time to check the code yet.

Reproducer

# torchrun --nproc-per-node 2 repro.py
import os, threading, torch
from transformers import AutoModelForCausalLM, DistributedConfig

model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen2.5-0.5B-Instruct", distributed_config=DistributedConfig(tp_size=int(os.environ["WORLD_SIZE"])), dtype="auto"
)
input_ids = torch.tensor([[9, 11, 18, 0]], device=model.device)

def forward():
    with torch.no_grad():
        model(input_ids)

forward()
thread = threading.Thread(target=forward)  # AssertionError in this thread on main
thread.start()
thread.join()

Continuous batching hits it in practice: every generate_batch call runs the model in a new generation thread, so the second call on a tensor-parallel model returns empty outputs.

Tests

Added test_forward_in_another_thread in tests/tensor_parallel/test_tensor_parallel.py (tiny Llama config, 2 processes): fails on main with the assertion above, passes here.

On 2x H100, torch 2.10.0:

main this PR
tests/tensor_parallel 19 passed
ContinuousBatchingTensorParallelTest (RUN_SLOW=1) 2 passed, 6 failed 8 passed
test_tp_forward, test_tp_backward, test_tp_generation for Qwen3 (CPU) passed

Speed

Embedding alone, 2x H100, median ms per call, main → this PR:

Qwen2.5-0.5B Qwen3-8B
inference 0.52 → 0.30 0.52 → 0.30
forward + backward 1.47 → 1.20 1.75 → 1.44

A full model step is unchanged on Qwen2.5-0.5B (forward 31.9 → 32.1 ms, forward + backward 220.7 → 220.0 ms).

DTensor's embedding lookup keeps the mask of the ids on a placement that torch caches for the whole process but fills per thread, so a forward in a second thread fails with 'assert self.mask_buffer.data is not None'. Each rank now looks up the ids of its rows on plain tensors, gives zeros for the others, and the ranks' outputs are summed.
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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.

2 participants