🧩

AWS Trainium 50 Exercises #5: Fundamentals of Distributed Training

に公開

Chapter 5: Fundamentals of Distributed Training

This chapter assumes the following knowledge:

  • Basic understanding of AWS Trainium (covered in Chapter 2)
  • Fundamental understanding of neural networks

Problems (33–37)

Training using multiple memory devices in parallel is called distributed training. (Device: here refers to the smallest unit that executes training. In the case of Trainium, this refers to a Neuron core. Trainium2 has both physical and logical Neuron cores; here we refer to logical Neuron cores.)

Distributed training becomes necessary when: (1) training on a single device is too slow, or (2) the model is too large to fit on a single device.

Category Subcategory Overview Solves (a)? Solves (b)?
Data Parallel Data Parallel (DP) Splits data within a mini-batch across multiple devices Yes No
Model Parallel Tensor Parallel (TP) Splits parameter tensors (row-wise or column-wise) across devices Yes Yes
Pipeline Parallel (PP) Assigns different model layer ranges to different devices (e.g., layers ●–▲ to device 1, ▲–■ to device 2, etc.) Yes Yes

These are the three primary parallel strategies, and they can be combined. It is important to note that training large models that do not fit on a single device requires model parallelism (TP or PP). For Trainium (Trn), any model that cannot fit in 1 core = 16GB (which applies to most LLMs) will require model parallelism.

In the previous chapter, the training configuration was DP=1, TP=32, PP=1. (The notation DP, TP, PP may also refer to the number of parallel devices used for each method.)

  1. You want to train with DP=2, TP=32, PP=8. How many Neuron cores are required, and how many trn1.32xlarge nodes will you need?
    Answer

    2 × 32 × 8 = 512 cores are required. Since one trn1.32xlarge node contains 32 Neuron cores, 512 ÷ 32 = 16 nodes are required.

Data Parallelism (DP)

In data parallelism, each device processes a different subset of data from the same mini-batch. The model weights are replicated across all devices, while each device performs forward and backward computation on its assigned micro-batch, after which gradients are synchronized to ensure consistent weight updates. This is the most fundamental and commonly used parallelization strategy when training with large batch sizes. The global batch size refers to the total number of samples processed per optimization step across all devices (i.e., the algorithmic mini-batch size, ignoring hardware). The micro-batch size refers to the number of samples assigned to each individual device.

  1. You want to train using only data parallelism, with a global batch size of 256. You have 2 trn1.32xlarge nodes available. What is the DP value, and what is the micro-batch size?
    Answer

    A trn1.32xlarge instance has 32 Neuron cores, and 1 core typically maps to 1 device. With 2 nodes, DP = 32 × 2 = 64. Therefore, the micro-batch size is 256 ÷ 64 = 4.

If the micro-batch size is too large to fit in device memory, gradient accumulation can be used to reduce per-device memory usage by accumulating gradients over multiple steps before performing a weight update. When using gradient accumulation, a single forward-backward execution is sometimes called a micro step, and the weight update step is called a global step.

  1. Training with the micro-batch size calculated above caused an OOM error. You decide to set the micro-batch size to 1 using gradient accumulation. How many gradient accumulation steps are required?
    Answer

    The original global batch size is 256, and DP = 64. With a micro-batch size of 1, the required gradient accumulation steps are 256 ÷ 64 ÷ 1 = 4. That is, each device performs 4 micro steps before a weight update to preserve the original global batch size.

The relationship can be summarized as: Global batch size = Micro-batch size × Gradient accumulation steps × DP.

Tensor Parallelism (TP)

Tensor parallelism splits a single parameter tensor across multiple devices. For example, when computing a large matrix multiplication Y = XA, if A is too large to fit on one device, it can be split into p partitions. If A = [A_1, A_2, ..., A_p] along columns, each device computes Y_i = X A_i, and the outputs are concatenated: Y = [Y_1, ..., Y_p] (Column Parallel). If A = [A_1, ..., A_p]^T along rows, X is also split as [X_1, ..., X_p], each device computes Y_i = X_i A_i, and the results are summed: Y = Y_1 + ... + Y_p (Row Parallel). In a 2-layer MLP, using column parallel for the first layer and row parallel for the second can eliminate unnecessary communication between layers.

2-layer MLP: Details

Consider a model that computes the following for input \mathbf{X} \in \mathbb{R}^{B \times d} (B: batch size):

\mathbf{H} = \phi(\mathbf{X} \mathbf{W}_1^\top), \quad \mathbf{Y} = \mathbf{H} \mathbf{W}_2^\top

where:

  • \mathbf{W}_1 \in \mathbb{R}^{h \times d} is the 1st layer weight (output dimension h, input dimension d)
  • \phi is an activation function such as ReLU (applied element-wise per batch)
  • \mathbf{W}_2 \in \mathbb{R}^{o \times h} is the 2nd layer weight (output dimension o)
  • \mathbf{H} \in \mathbb{R}^{B \times h}, \mathbf{Y} \in \mathbb{R}^{B \times o}

1st layer (Column Parallel)
Split weight \mathbf{W}_1 into 2 along the output dimension (row direction):

\mathbf{W}_1 = \begin{bmatrix} \mathbf{W}_1^{(0)} \ \mathbf{W}_1^{(1)} \end{bmatrix}, \quad \mathbf{W}_1^{(i)} \in \mathbb{R}^{(h/2) \times d}

Each device shares the entire input \mathbf{X} and computes:

\mathbf{H}^{(i)} = \phi(\mathbf{X} (\mathbf{W}_1^{(i)})^\top), \quad \mathbf{H}^{(i)} \in \mathbb{R}^{B \times (h/2)}

Then, concatenate \mathbf{H}^{(0)}, \mathbf{H}^{(1)} across devices in the column direction (horizontal) to obtain \mathbf{H} (this concatenation can be omitted in practical implementations by fusing it with the input partitioning of the next layer (※)):

\mathbf{H} = \left[ \mathbf{H}^{(0)} ;; \mathbf{H}^{(1)} \right]

2nd layer (Row Parallel)
Split \mathbf{W}_2 into 2 along the input dimension (column direction):

\mathbf{W}_2 = \left[ \mathbf{W}_2^{(0)} ;; \mathbf{W}_2^{(1)} \right], \quad \mathbf{W}_2^{(i)} \in \mathbb{R}^{o \times (h/2)}

Also split \mathbf{H} accordingly (※), and each device computes:

\mathbf{Y}^{(i)} = \mathbf{H}^{(i)} (\mathbf{W}_2^{(i)})^\top, \quad \mathbf{Y}^{(i)} \in \mathbb{R}^{B \times o}

Finally, sum (AllReduce) the outputs across devices to obtain the final output \mathbf{Y}:

\mathbf{Y} = \mathbf{Y}^{(0)} + \mathbf{Y}^{(1)}

💡 For further details, refer to this excellent article by Turing Inc.: https://zenn.dev/turing_motors/articles/0e6e2baf72ebbc

Notes: (1) The TP degree must divide the hidden dimension exactly (e.g., 4096 → TP must be a divisor of 4096). (2) For transformer models, TP must also divide the number of attention heads. (3) With GQA, TP must divide (KV heads × KV_REPLICATOR). Nexus Distributed (NxD) provides KV_REPLICATOR to replicate KV heads so that TP can scale beyond the KV head count limit.

  1. The Qwen/Qwen3-8B config.json contains "hidden_size": 4096, "num_attention_heads": 32, "num_key_value_heads": 8. What TP configuration allows the largest valid TP value?
    Answer

    TP must divide 32 and (8 × KV_REPLICATOR). To maximize TP, set KV_REPLICATOR = 4, which allows TP = 32.

Pipeline Parallelism (PP)

Pipeline parallelism splits model layers across multiple devices and processes micro-batches in a staged pipeline. However, depending on the number of stages and micro-batches, idle time known as pipeline “bubbles” may reduce hardware utilization.

  1. Qwen2.5-VL-32B-Instruct consists of a 32-layer visual encoder and a 64-layer text decoder, connected sequentially. You want to train it using DP=4, overall TP=8, PP(visual)=4, PP(text)=16. How many trn1.32xlarge nodes are required?
    Answer

    The required number of cores is 4 × 8 × (4 + 16) = 640. Since each trn1.32xlarge node has 32 cores, the number of nodes needed is 640 ÷ 32 = 20 nodes.

By combining DP, TP, and PP, it becomes possible to accelerate training and train models that cannot fit on a single device.

  • ⚠️ Notes: Trainium cores are not fully all-to-all connected; only certain communication patterns are supported (details). Therefore, not all DP/TP/PP combinations are valid. For example, DP=1, TP=1, PP=32 cannot run on a single trn1.32xlarge node because communication between core 7 and core 8 is not supported. Additionally, when KV_REPLICATOR > 1, extra inter-core communication is required, and some settings may be disallowed for the same reason.

Appendix: Sequence Parallelism

As an extension to tensor parallelism, sequence parallelism shards both model parameters and activations. Instead of storing a full activation tensor on each device, each device stores only a shard of the sequence dimension. This reduces memory usage, although inter-device communication becomes more complex. In NxD, this is supported transparently. In this mode: (1) each device stores only a 1/TP sequence shard, (2) activation tensors are kept in shape (sequence, batch, ...) instead of (batch, sequence, ...), and (3) the per-device tensor shape becomes [q_len//TP, batch, ...] rather than [batch, q_len, ...].

脚注
  1. We would like to thank Mr. Tokoyo from AWS for his supervision of this material. ↩︎

KARAKURI Techblog

Discussion