Automatic Tensor Parallelism (Training)
This tutorial covers Automatic Tensor Parallelism for combining tensor parallelism with ZeRO optimization during training. For inference-only tensor parallelism, see Automatic Tensor Parallelism (Inference).
Contents
- Introduction
- Quick Start
- HuggingFace tp_plan Support
- Vocabulary-parallel LM Loss
- Custom Layer Specifications
- Limitations
Introduction
The AutoTP Training API enables hybrid parallelism by combining:
- Tensor Parallelism (TP): Split model weights across GPUs within a node
- Data Parallelism (DP): Replicate model across GPU groups
- ZeRO Optimization: Memory-efficient optimizer states (Stage 0, 1, or 2)
Tensor parallelism (TP) splits the computations and parameters of large layers across multiple GPUs so each rank holds only a shard of the weight matrix. This is an efficient way to train large-scale transformer models by reducing per-GPU memory pressure while keeping the layer math distributed across the TP group.
Quick Start
Basic Usage
AutoTP training can be enabled entirely through the DeepSpeed config. When
tensor_parallel is set in the config, deepspeed.initialize(...) applies
AutoTP sharding during engine initialization, so the training loop itself does
not change.
import torch
import deepspeed
# 1. Create your model
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B")
# 2. Define the DeepSpeed config with tensor_parallel settings
ds_config = {
"train_micro_batch_size_per_gpu": 1,
"zero_optimization": {"stage": 2},
"bf16": {"enabled": True},
"tensor_parallel": {"autotp_size": 4},
}
# 3. Initialize DeepSpeed with AutoTP + ZeRO
engine, optimizer, _, _ = deepspeed.initialize(
model=model,
optimizer=optimizer,
config=ds_config,
mpu=mpu # Model parallel unit (optional if you provide tp_group elsewhere)
)
# 4. Train as usual
for batch in dataloader:
outputs = engine(input_ids=batch["input_ids"], labels=batch["labels"])
engine.backward(outputs.loss)
engine.step()
Compatibility note: For backward compatibility, you can still call
set_autotp_mode(training=True) and deepspeed.tp_model_init(...), but they
are not required when the DeepSpeed config provides the necessary
tensor_parallel settings.
Preset-based Sharding
If your model matches a built-in preset, set tensor_parallel.preset_model in the DeepSpeed config:
{
"train_batch_size": 8,
"train_micro_batch_size_per_gpu": 1,
"bf16": { "enabled": true },
"zero_optimization": { "stage": 2 },
"tensor_parallel": {
"autotp_size": 4,
"preset_model": "llama"
}
}
For the list of available presets, see supported models.
HuggingFace tp_plan Support
Many HuggingFace models (e.g. Llama, Qwen, Gemma2) ship with a built-in
base_model_tp_plan in their model config that describes how each layer
should be partitioned for tensor parallelism. DeepSpeed can automatically
detect and use this plan, so you do not need to configure preset_model or
partition_config for these models.
When tensor_parallel is set in the DeepSpeed config, the initialization
follows this priority:
- Custom
partition_config(highest): User-defined regex patterns. - HuggingFace
tp_plan: Automatically extracted frommodel._tp_planormodel.config.base_model_tp_plan. - AutoTP heuristics (lowest): Built-in parser based on module structure.
For models that define a tp_plan, you only need a minimal config:
{
"train_micro_batch_size_per_gpu": 1,
"zero_optimization": { "stage": 2 },
"bf16": { "enabled": true },
"tensor_parallel": { "autotp_size": 4 }
}
DeepSpeed will read the model’s tp_plan at initialization and convert it to
internal partition rules. The supported types are colwise, rowwise,
and colwise_gather_output(colwise_rep). The gathered column styles shard
the linear weight along its output dimension and AllGather the local output
shards so every tensor-parallel rank receives the complete output. For untied
output layers, the output dimension does not need to be divisible by
autotp_size; DeepSpeed uses uneven local shards and gathers back to the
original logical output size.
Gathered column parallelism currently supports untied output layers. If an
output layer such as lm_head shares the same runtime Parameter object with
an embedding, DeepSpeed leaves both modules replicated and applies tensor
parallelism to the remaining matched layers. This preserves the tie without
silently cloning the weight, but does not reduce the embedding or output-layer
memory footprint. A coupled vocabulary-parallel embedding is required to shard
the tied weight and is not yet implemented. This fallback uses actual Parameter
identity rather than model configuration metadata such as tie_word_embeddings.
Additional HuggingFace types such as local_colwise and local_rowwise are
not yet handled and fall back to AutoTP preset-based partitioning.
For an lm_head or embed_out, an explicit row partition rule also
supports eager training with a replicated input. The head slices the input to
match its weight shard, reduces the complete output, and reconstructs the full
input gradient across tensor-parallel ranks. Bias stays replicated; uneven
hidden dimensions and both flattened and sequence-shaped inputs are supported.
The default training output-head layout remains column parallel. A head tied
to its input embedding (tie_word_embeddings) is also supported: DeepSpeed
shards every tied nn.Embedding along the same hidden dimension, so the pair
keeps reading from and accumulating gradients into a single per-rank weight
shard, and all-gathers each token’s embedding slices into the full hidden
activation. A conflicting explicit spec on the tied embedding is superseded
with a warning. The embedding options max_norm and sparse, custom
embedding forwards other than Gemma3’s scaled embedding, weights tied to
modules other than nn.Embedding, and reshaped/non-input-dimension shards are
rejected rather than silently breaking a parameter tie. Deferred DeepCompile
collectives are not supported for this output-head path. Note that a
row-parallel head all-reduces the complete vocabulary logits; for tied models,
vocab_parallel_lm_head avoids that and keeps logits sharded.
If you need to override the model’s built-in tp_plan, provide a
partition_config in the DeepSpeed config – it takes precedence.
Vocabulary-parallel LM Loss
Causal language models normally gather the complete lm_head output before
computing cross entropy. To keep an output vocabulary sharded, enable
vocab_parallel_lm_head:
{
"train_micro_batch_size_per_gpu": 1,
"zero_optimization": { "stage": 2 },
"tensor_parallel": {
"autotp_size": 4,
"vocab_parallel_lm_head": true
}
}
DeepSpeed then keeps lm_head (or embed_out) local to each TP rank and
installs a pure-PyTorch distributed causal-LM loss through the model’s
loss_function hook. The loss computes a numerically stable distributed
log-sum-exp and target lookup without gathering vocabulary logits. Uneven
vocabulary shards are supported.
For optional fused CE, install liger-kernel>=0.8.1 and set
tensor_parallel.vocab_parallel_ce_backend to "liger" alongside
vocab_parallel_lm_head. The default remains "torch", with no Liger dependency.
The Liger backend accepts nonempty accelerator logits in FP32, FP16, or BF16 with
equal vocabulary shards behind an explicit tensor-parallel group, on any accelerator
whose Triton support the DeepSpeed accelerator reports. Uneven shards, a missing
tensor-parallel group, and unsupported layouts use the PyTorch backend on every TP
rank. This fuses CE, not the linear projection: local logits are still
materialized. Liger supports one first-order backward per forward; retained-graph
second backward and higher-order gradients raise an error rather than reusing
its overwritten gradient buffer. Select "torch" for those workloads.
To avoid materializing the local logits as well, select "fused_linear", which
fuses the head projection into the loss; see
Fused Projection and Loss.
Only an nn.Linear whose final name segment is lm_head or embed_out is
supported. With vocab_parallel_lm_head: true, if no such head exists,
initialization fails instead of quietly falling back to an ordinary gathered
head. When partition_config or a
HuggingFace tp_plan also describes that head, vocab_parallel_lm_head takes
precedence and a warning names the superseded partitioning.
Models that tie the output head to the input embedding (tie_word_embeddings)
are also supported: DeepSpeed detects the shared nn.Embedding and vocabulary-
shards it jointly with lm_head, including all registered aliases of the
embedding and distinct embedding modules sharing the same weight. These modules
keep reading from and accumulating gradients into a single physical, per-rank-sharded weight
Parameter instead of falling back to a gathered, replicated head. This also
covers the HuggingFace tp_plan embedding_rowwise style that newer
transformers releases inject for tied-embedding models. That style
automatically enables the same vocabulary-parallel embedding/head path when
vocab_parallel_lm_head is omitted or null, so no separate enable flag is
required. This returns rank-local outputs.logits; set
vocab_parallel_lm_head: false to opt out and keep the replicated tied
embedding/head with full-vocabulary logits. Tied models whose plan
does not contain embedding_rowwise retain the previous replicated behavior
unless the flag is enabled explicitly. A conflicting explicit
partition_config spec on the tied embedding is likewise superseded, with a
warning.
Vocabulary-parallel embeddings preserve padding_idx gradient suppression and
Gemma3’s scaled-embedding forward behavior. Other custom embedding forwards and
the max_norm, scale_grad_by_freq, and sparse options are unsupported.
Implicit automatic sharding checks compatibility before mutating weights; an
unsupported embedding, missing or unwritable loss hook, empty vocabulary shard,
or ambiguous output head retains the replicated embedding/head and logs a
warning. Explicit true requests raise instead of falling back. Tied embeddings
retain the full vocabulary shape in universal-checkpoint metadata, including
uneven shards.
With compile.deepcompile: true and "autotp" in compile.passes, automatic
sharding from embedding_rowwise is disabled: the tied embedding and output
head remain replicated with gathered logits, while the other supported layers
are still tensor-parallel. An explicit vocab_parallel_lm_head: true request
is not downgraded; vocabulary-parallel embeddings are not supported by the
AutoTP compile pass.
This option requires a model with a writable loss_function hook. The head’s
vocabulary must also produce a nonempty shard on every TP rank; smaller
vocabularies or a grain size that leaves empty shards trigger the compatibility
behavior above instead of leaving ranks with empty logits. An explicit true
flag or a supported tied HuggingFace embedding_rowwise plan entry triggers this path. A
plain colwise lm_head specification with local output keeps the previous
behavior of returning rank-local logits without installing the distributed
loss.
The lower-level vocab_parallel_cross_entropy API also accepts an explicit
sequence-parallel group. With reduction="none", callers may return local token
losses or gather them along sequence dimension 0. sum and mean reduce over
the supplied SP group. TP and SP may be combined with explicit orthogonal
process groups, but AutoTP does not currently construct a combined TP x SP mesh
automatically.
Gathered sequence losses have two backward conventions. The general
vocab_parallel_cross_entropy(..., gather_sequence_loss=True) API sums the
gradient contributions from every SP rank before returning each rank’s local
slice. The compatibility wrapper vocab_sequence_parallel_cross_entropy
preserves its legacy behavior and returns only the corresponding local slice of
the gathered loss gradient. New callers that consume or reduce the gathered
loss on every SP rank should use the general API; existing callers can retain
the wrapper without changing their gradient scale. Both gathered forms require
an explicit sp_group.
Under DeepSpeed’s Ulysses sequence-parallel engine, the installed loss must keep
its default sequence-parallel settings (no sp_group): the engine aggregates
each shard’s mean itself, weighted by the shard’s valid-token count, so an
additional SP reduction would double-count tokens. Pass an sp_group only when
this loss is the sole aggregation over a manually constructed TP x SP mesh.
Untied Input Embedding
vocab_parallel_lm_head shards an input embedding only when it is tied to the
output head. An untied input embedding stays replicated on every TP rank (for
example Qwen2.5-72B’s 152,064 x 8,192 embedding weight is 12% of each GPU’s
parameters at TP 8). Set vocab_parallel_embedding to shard it along the
vocabulary as well:
"tensor_parallel": {
"autotp_size": 8,
"vocab_parallel_embedding": true
}
Each rank keeps 1/tp of the rows, looks up the tokens that fall in its rows, and
the partial results are summed over the tensor-parallel group. The model must
return an nn.Embedding from get_input_embeddings(). The option applies on all
three partitioning paths (HuggingFace tp_plan, partition_config or
preset_model, and the heuristic path) and to models built under
deepspeed.zero.Init.
A tied embedding is sharded with its output head instead. When the head is
vocabulary-parallel (vocab_parallel_lm_head) or row-parallel, the embedding
already shares the head’s shard and the option changes nothing. When the head
stays replicated, the option raises an error rather than untie the embedding from
its output head.
The option also raises an error in two cases: when a partition_config spec
already partitions the embedding, and when the DeepCompile autotp pass is
enabled, because that pass does not support vocabulary-parallel embeddings.
Fused Projection and Loss
The vocabulary-parallel loss above still materializes [tokens, vocab / tp]
logits on every rank. vocab_parallel_linear_cross_entropy fuses the LM-head
projection into the loss. Forward processes tokens in chunks and keeps only
per-token softmax statistics; backward recomputes the logits one vocabulary block
at a time. Only one block of logits is alive at a time:
from deepspeed.sequence.linear_cross_entropy import vocab_parallel_linear_cross_entropy
head = model.lm_head # VocabParallelLinear
hidden = model.model(input_ids).last_hidden_state[:, :-1]
loss = vocab_parallel_linear_cross_entropy(hidden,
head.weight,
labels[:, 1:],
bias=head.bias,
tp_group=head.mp_group,
vocab_start_index=head.vocab_start_index,
vocab_end_index=head.vocab_end_index)
The call replaces both head(hidden) and the loss: it all-reduces the hidden
gradient over the TP group in backward, as the column-parallel head would. It
supports none, sum and mean reductions. Tokens whose target is
ignore_index (prompt and padding positions) are dropped before the
projection, so they cost neither GEMMs nor collectives. chunk_size (tokens per
forward chunk; backward uses vocabulary blocks of the same size) trades peak
memory for speed; by default each FP32 logits block is about the size of the
hidden states, but at least 256 tokens so that the projection GEMMs stay efficient.
Using the fused loss from a HuggingFace forward
Set vocab_parallel_ce_backend to "fused_linear" to use this loss for the
HuggingFace causal-LM forward:
{
"bf16": { "enabled": true },
"zero_optimization": { "stage": 1 },
"tensor_parallel": {
"autotp_size": 2,
"vocab_parallel_lm_head": true,
"vocab_parallel_ce_backend": "fused_linear"
}
}
When the model is called in training mode with labels, the vocabulary-parallel
head hands its input to the installed loss_function instead of projecting it,
and the loss runs the fused projection. outputs.loss is unchanged
(num_items_in_batch is honored), but outputs.logits has a last dimension of
0 because no logits are materialized. In eval mode, or without labels, the
head returns the rank-local logits as with the "torch" backend.
The HuggingFace Trainer needs no code changes: it reads
tensor_parallel.autotp_size from the DeepSpeed config passed as
TrainingArguments(deepspeed=...) and calls deepspeed.tp_model_init before
deepspeed.initialize, so the vocabulary-parallel options above apply. For
Qwen2.5-0.5B (151,936-token vocabulary, tied embeddings) with Trainer, TP=2,
ZeRO-1, BF16, gradient checkpointing, 4 x 2,048 tokens per micro-batch with
half of each sequence masked as prompt, on two GPUs:
tensor_parallel setting |
Peak memory per rank | Step time (2x NVIDIA L20) | Step time (2x Intel Arc Pro B70) |
|---|---|---|---|
vocab_parallel_lm_head: false (gathered logits) |
18,860 MiB | 558 ms | 715 ms |
vocab_parallel_ce_backend: "torch" |
13,201 MiB | 514 ms | 653 ms |
vocab_parallel_ce_backend: "liger" |
8,456 MiB | 477 ms | - |
vocab_parallel_ce_backend: "fused_linear" |
5,200 MiB | 467 ms | 590 ms |
Peak memory is from the L20 runs; the B70 runs are within 20 MiB. Loss and gradient norms match across all settings.
Limitations of the "fused_linear" backend:
- Code that reads
outputs.logitsfrom a training forward withlabelsreceives an empty tensor. Compute such metrics in eval mode, or use the"torch"backend. - Models that soft-cap or rescale the logits after the head (for example
final_logit_softcapping,logit_scaleorlogits_scalingin the config) are rejected, because the fused loss never sees those logits. - ZeRO stage 3 and the DeepCompile AutoTP pass are rejected.
- Under DeepSpeed Ulysses / ALST, the model is called with
shift_labelsinstead oflabels, so the forward returns local logits and the sequence-parallel loss path applies.
With any vocabulary-parallel backend, outputs.logits (when present) holds only
this rank’s vocabulary shard. Callers that compute their own loss from the
logits or from lm_head.weight therefore see partial vocabularies. This
includes Trainer with label_smoothing_factor, and TRL trainers that patch
the forward with their own fused LM head (SFTTrainer and GRPOTrainer since
TRL 1.15). Use the vocabulary-parallel ops below, or keep
vocab_parallel_lm_head: false, for those workloads.
Log-probabilities, Entropy, KL, and JSD on Sharded Logits
RL and on-policy distillation losses can consume the local logits shard
directly through deepspeed.sequence.vocab_parallel_ops, so they never
all-gather the full vocabulary:
from deepspeed.sequence.vocab_parallel_ops import (vocab_parallel_entropy, vocab_parallel_jsd, vocab_parallel_kl_div,
vocab_parallel_logprobs)
head = model.lm_head # VocabParallelLinear installed by vocab_parallel_lm_head
shard = dict(tp_group=head.mp_group)
ids = dict(vocab_start_index=head.vocab_start_index, vocab_end_index=head.vocab_end_index, **shard)
student_logits = model(input_ids).logits # [batch, seq, local_vocab]
# Policy-gradient losses: log-probabilities of the sampled tokens, plus entropy.
token_logprobs = vocab_parallel_logprobs(student_logits, sampled_ids, **ids)
token_entropy = vocab_parallel_entropy(student_logits, **shard)
# Dense distillation against an identically sharded teacher.
token_kl = vocab_parallel_kl_div(student_logits, teacher_logits.detach(), reverse=True, **shard)
# Generalized JSD over the full vocabulary, without gathering its shards.
token_jsd = vocab_parallel_jsd(student_logits, teacher_logits.detach(), beta=0.5, **shard)
# Distillation from a teacher that only provides top-k (logprob, id) pairs.
student_topk = vocab_parallel_logprobs(student_logits, teacher_topk_ids, **ids)
token_kl = (teacher_topk_logprobs.exp() * (teacher_topk_logprobs - student_topk)).sum(-1)
loss = (token_kl * mask).sum() / mask.sum()
Every op returns an fp32 per-token tensor that is identical on all TP ranks;
masking and reduction are left to the caller. vocab_parallel_logprobs accepts
one token id per position or a trailing top-k dimension, and returns 0 for
ignore_index. vocab_parallel_kl_div computes KL(teacher || student) by
default and KL(student || teacher) with reverse=True; apply any temperature
to both logits before calling. Teacher and student logits must use the same TP
group and vocabulary sharding.
vocab_parallel_jsd computes (1-beta) * KL(student || mixture) + beta * KL(teacher || mixture),
where mixture = (1-beta) * student + beta * teacher refers to globally normalized probabilities.
For compatibility with generalized_jsd_loss, beta=0 instead selects forward KL and beta=1
selects reverse KL; these endpoint conventions are not the continuous limits of the interior
JSD expression. Gradients flow to both models unless the teacher logits are detached.
Temperature is applied to the input logits by the caller; no temperature-squared factor is
added automatically. Top-k restriction and pointwise clipping are not supported.
Each op saves only its inputs and per-token statistics, and recomputes the
softmax in bounded row chunks during backward. Its peak activation memory is
therefore a small multiple of the local logits shard instead of the several
fp32 copies that an equivalent composition of PyTorch ops keeps alive.
When Triton supports the logits’ device,
each pass over the logits runs in a fused Triton kernel that keeps no fp32 copy of the shard; other devices, or
deepspeed.HAS_TRITON = False, use an eager implementation with the same
semantics. The entropy and KL forward passes read each logit once, and a slice
along the sequence, such as the next-token shift logits[:, :-1], is read in
place rather than copied.
Interior-beta JSD has fused Triton statistics, divergence and backward kernels, with
bounded eager row chunks as fallback. Its statistics kernel reads student and teacher
together; after global normalization a second pass computes both KL-to-mixture
expectations, packed into one SUM all-reduce. Unlike KL, the nonlinear mixture requires
this second forward pass. Backward recomputes probabilities in registers and writes
only the requested gradients, without extra TP communication.
JSD also enables these kernels on XPU when an active Intel Triton backend is installed,
without changing the accelerator-wide Triton flag or the dispatch of existing ops.
It still consumes materialized local logits; this is not fused-linear JSD and does not
automatically integrate with TRL or the OPSD example.
Custom Patterns
If you are training a custom model, define regex-based patterns and partition rules in tensor_parallel.partition_config:
{
"tensor_parallel": {
"autotp_size": 4,
"partition_config": {
"use_default_specs": false,
"layer_specs": [
{
"patterns": [".*\\.o_proj\\.weight$", ".*\\.down_proj\\.weight$"],
"partition_type": "row"
},
{
"patterns": [".*\\.[qkv]_proj\\.weight$"],
"partition_type": "column"
},
{
"patterns": [".*\\.gate_up_proj\\.weight$"],
"partition_type": "column",
"shape": [2, -1],
"partition_dim": 0
}
]
}
}
}
Custom Layer Specifications
For models not covered by presets, define custom layer specs:
{
"tensor_parallel": {
"autotp_size": 4,
"partition_config": {
"use_default_specs": false,
"layer_specs": [
{
"patterns": [".*\\.o_proj\\.weight$", ".*\\.down_proj\\.weight$"],
"partition_type": "row"
},
{
"patterns": [".*\\.[qkv]_proj\\.weight$"],
"partition_type": "column"
},
{
"patterns": [".*\\.gate_up_proj\\.weight$"],
"partition_type": "column",
"shape": [2, -1],
"partition_dim": 0
}
]
}
}
}
Fused Layers with Unequal Sub-parameters (GQA)
For Grouped Query Attention with different Q/K/V sizes:
{
"tensor_parallel": {
"partition_config": {
"layer_specs": [
{
"patterns": [".*\\.qkv_proj\\.weight$"],
"partition_type": "column",
"shape": [[q_size, kv_size, kv_size], -1],
"partition_dim": 0
}
]
}
}
}
Limitations
-
Ranks beyond the key/value head count stay idle: Attention heads are distributed whole, and the distribution may be uneven – 6 key/value heads over 4 ranks becomes 2/2/1/1, and a fused QKV weight is cut on the same head boundaries rather than inside a head. With more ranks than key/value heads, for example an 8-head model at
autotp_size=16, the surplus ranks receive no attention weights at all. The result is still correct, because those ranks contribute zeros to the row-parallel all-reduce, but they do no attention work; AutoTP logs a warning instead of replicating heads to fill them. Hidden and vocabulary dimensions do not need to be divisible by the tensor parallel size: uneven shards are carried through save, conversion and restore via per-TP-rank shapes and widths. -
Vocabulary-parallel LM loss requires a writable
loss_functionhook:vocab_parallel_lm_headrequires a model with a writableloss_functionhook, and a vocabulary at least as large as the tensor-parallel size. Both tied and untiedlm_head/embed_outheads are supported; a tied embedding is jointly vocabulary-sharded with its output head. -
Combined TP and SP orchestration: The vocab-parallel loss supports explicit orthogonal TP and SP groups, but AutoTP does not currently construct a combined TP x SP process mesh automatically.
-
Cross-topology universal restore: Loading a universal checkpoint back into a topology with a different tensor-parallel degree goes through DeepSpeed’s Megatron-style model-state loader, which is not AutoTP-aware; prefer same-topology restore when changing world size.