AWS Trainium 50 Exercises #2: Fundamentals of Trainium
Chapter 2: Fundamentals of Trainium
This chapter assumes the following prerequisites:
- Basic understanding of the Transformer architecture
- Basic understanding of HuggingFace Models
Exercises (8–10)
Trainium is a “chip specialized for deep learning” (hardware accelerator) developed by AWS around 2021. Like GPUs, it excels at performing matrix operations quickly, but it offers even better cost performance. However, as mentioned in the previous chapter, it requires compilation of the computation graph, which introduces certain constraints.
Node, Core, and Chip
The main instances equipped with the current latest AWS Trainium chip,
Trn1, are listed below. Their cost and memory capacity are as follows (on-demand pricing inus-east-2as of 2025-05-19):
| Instance | Total Trn Cores | Total Trn Memory | CPU Memory | Cost/h |
|---|---|---|---|---|
| trn1.2xlarge | 2 | 32GB | 32GB | $1.34 |
| trn1.32xlarge | 32 | 512GB | 512GB | $21.50 |
※ The above costs are for on-demand usage. By using EC2 Capacity Blocks, which involve pre-reserving capacity and prepayment, it is possible to reduce costs further.
In a
Trn1instance, the number in{num}xlargematches the total number of cores.
- How much memory (in GB) does each core of
Trn1have? Use the table above to calculate and memorize it.Show answer
Each Trn1 core has 16GB of memory.
Additional notes:
Although not shown in the table, one chip = two cores (32GB).
About trn1n instances:
- Compared with non-
nversions, these have enhanced inter-node networking (EFA) and are recommended for large-scale training that involves more than 100 nodes simultaneously.About next-generation Trainium chips:
Trn2was released in December 2024, offering higher performance thanTrn1(details).Trn3is scheduled for release in the latter half of 2025.About Inferentia2:
- Separate from Trainium (1 or 2), there is also the Inferentia2 chip. It is designed more for inference than for training, but it can still perform training. In fact, the main difference between Trainium1 and Inferentia2 lies in network bandwidth (Trainium1 offers faster communication).
-
You want to host an inference demo server of Qwen2.5-7B-Instruct on a
Trn1instance with a maximum sequence length of 8K. Estimate the required memory and determine which instance would be optimal. Assume that the memory required for inference of a standard Transformer-based LLM can be estimated as:
parameter_count × 2Bytes + ((vocab_size) + 2 × (key-value head dimension) × (number of key-value heads per layer) × (number of layers)) × batch_size × sequence_length × 2Bytes
(reference)Show answer
The following excerpt is from config.json:
{ "hidden_size": 3584, // hidden dimension (= head_dim × attention_heads_per_layer) "num_attention_heads": 28, // number of attention heads per layer "num_hidden_layers": 28, // number of layers "num_key_value_heads": 4, // number of key-value heads per layer "vocab_size": 152064 // vocabulary size }From these values, substituting gives:
7G × 2 + (152064 + 2 × 128 × 4 × 28) × 1 × 8K × 2 = 14G + 3G = 17G. Therefore, it can be hosted on atrn1.2xlarge(32GB Trn memory). However, since a single core (16GB) is insufficient, some form of model parallelism is required. -
You want to train Qwen2.5-7B-Instruct on a single
Trn1node. The micro-batch size is 1, and the maximum sequence length is 8K. Estimate the required memory and determine which instance is optimal. Assume that the memory required for training a standard Transformer-based LLM can be estimated as:
3 × parameter_count × 2Bytes + 2 × (total hidden dimension per token across all layers) × batch_size × sequence_length × 2Bytes
(reference)
To simplify estimation, approximate the total hidden dimension per token asnumber_of_layers × 10 × hidden_size.Show answer
Substituting gives:
3 × 7G × 2 + 2 × (28 × 10 × 3584) × 1 × 8K × 2 = 42G + 32G = 74GB. Therefore, training is not possible on a singletrn1.2xlargenode (with 32GB Trn memory). However, it is possible on a singletrn1.32xlargenode (under appropriate model parallelism).
To perform training or inference on Trn/Inf, the model (computation graph) must be compiled and deployed to Trn/Inf. This development effort is required for each model architecture. For popular model architectures (e.g.,
LlamaForCausalLM), official support may already exist. However, for others, this development can be the most time-consuming part—often requiring 2–3 person-months of work.
-
We would like to thank Mr. Tokoyo from AWS for his supervision of this material. ↩︎
Discussion