We are analyzing a typical Transformer block involving two large matrices:
- B∈RD×F, with D=8192, F=32768
- C∈RF×D
- Input x∈RB×D, batch size B=128
- All data in bfloat16 (2 bytes per element)
- Running on a TPUv5e 2x2 slice → 4 TPU chips
- Each TPU has 300 MB of free memory
- Goal: Minimize total time (FLOPs + communication) while staying under memory limits
We consider the forward pass of a Transformer block that computes:
y=C⋅(B⋅x)
This is typical of feed-forward networks (FFNs) in Transformers (e.g., W2(W1(x))), where B is the up-projection and is the down-projection.
1. Memory Constraints
Each TPU has 300 MB = 300 × 1024² bytes = 314,572,800 bytes
In bfloat16, 1 element = 2 bytes → max 157,286,400 elements per TPU
Let’s compute the size of each tensor:
| Tensor | Shape | Elements | Size (MB) |
|---|
| B | (8192, 32768) | 268,435,456 | 536.87 MB |
| C | (32768, 8192) | 268,435,456 | 536.87 MB |
| x | (128, 8192) | 1,048,576 | 2.00 MB |
| z=Bx | (128, 32768) | 4,194,304 | 8.00 MB |
|
Problem:
- B and C are 537 MB each, but each TPU only has 300 MB free → cannot fit even one of them on a single TPU.
- So we must shard both B and C across the 4 TPU devices.
2. Strategy: Sharding Plan
We have 4 devices, and want to minimize:
- Memory usage per device
- Communication cost
- Computation (FLOPs) per device
We consider 2D sharding (tensor + batch parallelism) and pipeline vs fully parallel.
But since the matrices are large and memory is tight, we must shard the weights and possibly the batch.
Let’s analyze options for B⋅x and C⋅z.
✅ Optimal Sharding: 2D Tensor + Batch Parallelism (Hybrid Sharding)
We use 2D sharding inspired by Megatron-LM and TPU best practices, combining:
- Row-wise sharding of B → splits output dimension F
- Column-wise sharding of C → splits input dimension F
- Batch sharding (data parallelism) to reduce per-device memory
But memory is tight, so we must shard both B and C across all 4 devices, and shard the batch.
Let’s go with:
Plan: 2D Sharding (2×2 Grid)
Divide the 4 TPUs into a 2×2 grid:
- 2 devices along F dimension (32768) → split F
- 2 devices along batch dimension (128) → split batch
We’ll use tensor model parallelism (TP) along F and data parallelism (DP) along batch.
But to minimize comms, we use 2D sharding with collective communication optimized for TPU topology.
Sharding Strategy:
| Component | Sharding | Per-device Shape | Per-device Size (MB) |
|---|
| x | Batch-sharded (DP) | (64, 8192) | 1.00 MB |
| B | Row-sharded (TP) | (8192, 16384) | (8192 × 16384) × 2 / 1e6 = 268.4 MB |
| C | Column-sharded (TP) | (16384, 8192) | (16384 × 8192) × 2 / 1e6 = 268.4 MB |
| z=Bx | |
Why this works:
- B: split along F → each device stores F/2=16384 rows → 268.4 MB ✅ < 300 MB
- C: split along F → each device stores rows (same as 's output dim) → 268.4 MB ✅
Total per-device memory:
- B: 268.4 MB
- C: 268.4 MB → but can be stored in different memory banks, or streamed if needed
- x: 1.0 MB
- z: 2.05 MB
- y: 1.0 MB
- Workspace: ~10–20 MB (for activations, gradients, etc.)
⚠️ But 268.4 + 268.4 = 536.8 MB > 300 MB → cannot keep both B and C in memory at the same time!
🔴 Problem: We cannot store both B and C on the same device if they are both 268.4 MB.
✅ Solution: Overlap computation and memory — stream C or B
But better: recompute or pipeline, or use a 3D sharding where B and C are on different devices?
Wait — we have 4 devices. Can we assign B and C to different devices?
No, because the output of Bx must be all-gathered to compute Cz, and C must be on the same device group as z.
But we can optimize by not storing both matrices at the same time.
✅ Best approach: Recompute or pipeline the FFN layers — but we only have one block. So instead, stream the matrices or **use **memory-efficient sharding.
✅ Revised Plan: Overlap B and C computation with memory streaming
We cannot keep both B and C in HBM at the same time on a device.
But:
- B is used in first GEMM: z=Bx
- C is used in second GEMM: y=Cz
- After is computed, is no longer needed (unless backward pass, but we’re doing forward only here)
✅ So we can:
- Load B → compute z=Bx → free B
- Load C → compute y →
Thus, peak memory per device:
- B: 268.4 MB (during first GEMM)
- z: 2.05 MB
- x: 1.0 MB
- Workspace: ~20 MB
→ Total: ~291.5 MB ✅ < 300 MB
Then after B is freed, load C: 268.4 MB + z: 2.05 MB + workspace → ~290.5 MB ✅
✅ Memory feasible with streaming!
🔑 Key insight: Do not keep both B and C in memory simultaneously — stream them in stages.
This is standard in memory-efficient training and inference engines.
3. Communication Patterns
We now analyze communication during the two GEMMs.
(1) GEMM 1: z=Bx
- B: row-sharded (split along F), so each device has B(i)∈R
Compute: z(i)=B(i)⋅x
But we need all F-dim values to proceed to Cz. So we must all-gather along the F-dimension.
🔁 Communication: All-Gather along TP group (2 devices)
- Each device sends z(i)∈R64×16384 → 2.05 MB
- All-gather → each device receives full z∈ → 4.1 MB
📡 Comm volume per device: 2.05 MB (sent), 2.05 MB (received) → 4.1 MB total transferred
🔗 Link: TPUv5e uses high-speed mesh interconnect (bidirectional, ~100–200 GB/s per link)
Assume 2D torus, 2 devices per TP group → point-to-point or ring all-gather
Bandwidth: Assume 150 GB/s per link → effective all-gather bandwidth ~150 GB/s for 2-device group
⏱️ Time:
tcomm1=150×1
(We send 2.05 MB, but all-gather requires 2× data transfer → 4.1 MB × 2 = 8.2 MB total, but per device bandwidth is 150 GB/s, so time is based on max send/receive rate)
Actually: all-gather of 2.05 MB over 2 devices → each sends 2.05 MB → time = 2.05 MB / 150 GB/s ≈ 13.7 μs
✅ Use bidirectional ring: time ≈ bandwidthdata=1.5×10
🔁 t_comm1 ≈ 14 μs
(2) GEMM 2: y=Cz
- C: column-sharded → each device has C(i)∈RF/2×D
- : now → on each device
Compute: y(i)=C(i)⋅z∈R
But y(i) is partial — we need to sum across devices (since C is column-sharded, outputs are additive).
🔁 Communication: Reduce-Scatter or All-Reduce along TP group
We want y=∑iC(i)z → so after GEMM, we all-reduce the outputs.
- Each device computes y(i)∈R64×8192 → 1.00 MB
- All-reduce across 2 devices → each gets full y∈
📡 Comm volume: 1.00 MB sent, 1.00 MB received → 2.00 MB total
⏱️ Time: tcomm2=1.5×1011
✅ t_comm2 ≈ 7 μs
🔁 Total comm time: 14 + 7 = 21 μs
💡 Note: We could use reduce-scatter to avoid storing full y, but since we need it for next layer, all-reduce is fine.
4. Computation (FLOPs)
(1) GEMM 1: z=Bx
- B∈R8192×16384, x∈R64×8192
- Output:
(2) GEMM 2: y=Cz
- C∈R16384×8192, z∈R64×32768
- Output:
🔁 Total FLOPs per device:
1.718×1010+3.436×1010=5.154× FLOPs
💻 TPUv5e performance:
- TPUv5e has ~125 TFLOPS (bf16) per chip
- But sustained GEMM performance in practice is ~70–90% → use 100 TFLOPS = 10^{11} FLOPS
⏱️ Computation time:
tflops=1011
✅ t_flops ≈ 515 ms
🔍 Note: This is dominated by GEMM2 (down-projection), which is larger because F≫D
5. Total Time and Bottlenecks
| Component | Time |
|---|
| FLOPs (compute) | 515 ms |
| Comm (all-gather + all-reduce) | 21 μs ≈ 0.021 ms |
| Memory streaming / load | Negligible (overlapped with compute) |
| Total time | ≈ 515 ms |
🚀 Comm is negligible (0.004%) → compute-bound
✅ Optimal sharding: 2D hybrid (2×2) with:
- Tensor parallelism (2 devices) along F (split B and C)
- Data parallelism (2 devices) along batch (split x)
- All-gather after Bx, all-reduce after
🔧 TPU-specific optimizations:
- Use XLA to fuse GEMMs and communication
- Use async collectives to overlap comms with computation (but here comms are tiny, so not needed)
- 2D mesh layout matches TPU topology → efficient all-gather/reduce
6. Can We Do Better?
Option 1: 4-way TP (no batch sharding)
- Split B into 4 parts: F/4=8192
- Each B(i): (8192, 8192) → 134.2 MB
- : (8192, 8192) → 134.2 MB
✅ Memory: 134.2 + 134.2 + 2 + 2 = 272.4 MB → still need streaming, but easier
FLOPs per device:
- Bx: 2×128×8192×8192=1.718×10
But: total FLOPs is same, just distributed → time: 3.436×1010/1011=343.6 ms
📡 Comm volume:
- All-gather z: (128, 8192) = 2.05 MB → over 4 devices → 3× send → 6.15 MB → time ≈ 41 μs
- All-reduce y: (128, 8192) = 2.05 MB → 3× send → 6.15 MB → 41 μs
- Total comm: 82 μs
✅ Total time: 343.6 ms + 0.082 ms ≈ 343.7 ms
✅ Better! 34% faster
🔴 But: Can we store both B and C? 134.2 × 2 = 268.4 MB → plus x, z, y → ~275 MB → ✅ fits without streaming!
✅ No need to stream → simpler, faster memory access
✅ Winner: 4-way TP (no DP)
Why? Because FLOPs scale down, and comm is still tiny, and memory fits
🔁 Comm pattern: 4-device all-gather and all-reduce → use ring or recursive doubling → efficient on 2×2 mesh
✅ Optimal: 4-way tensor parallelism (TP=4), no batch sharding
But: loss of batch parallelism → if we had more batch, we could scale further
With B=128, 4-way TP is fine.
🔄 Alternative: 3D sharding (TP × DP × PP), but overkill for 4 devices.
✅ Final Recommendation: 4-Way Tensor Parallelism
| Parameter | Value |
|---|
| Sharding | 4-way tensor parallelism (split F into 4) |
| B per device | (8192, 8192) → 134.2 MB |
| C per device | (8192, 8192) → 134.2 MB |
| x per device | (128, 8192) → 2.0 MB |
| z after GEMM1 | (128, 8192) → 2.05 MB → all-gather to (128, 32768) |
| after GEMM2 |
🚀 This is optimal for 4 TPUv5e chips with 300 MB each
🔧 Implementation notes:
- Use XLA with
sharding={device_mesh=[2,2], dims=[0,1]} for 2D mesh
- Use all-gather and all-reduce with collective_permute or ring
- No need for recomputation or streaming — both matrices fit
Summary
| Metric | Value |
|---|
| Sharding | 4-way tensor parallelism (split F) |
| Memory per device | ~275 MB (fits 300 MB) |
| FLOPs per device | 34.36 G |
| Compute time | 343.6 ms |
| Communication time | 82 μs (all-gather + all-reduce) |
| Total time | ≈ 344 ms |
| Efficiency | Compute-bound (99.98%) |
| Scalability | Can increase batch size up to ~256 before memory pressure |
✅ This minimizes time while respecting memory limits.
🔮 For larger batches, add data parallelism (DP) — but with 4 devices, TP=4 is best for this F≫D case.
📌 Rule of thumb: When F≫D, prefer tensor parallelism over data parallelism for FFNs.