Skip to content

Commit 304786d

Browse files
committed
[Training] feat: add memory estimator breakdown
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
1 parent 84ad990 commit 304786d

6 files changed

Lines changed: 800 additions & 136 deletions

File tree

‎docs/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ training/training-loop-settings.md
4747
training/optimizer-scheduler.md
4848
training/logging.md
4949
training/profiling.md
50+
training/memory-estimator.md
5051
training/checkpointing.md
5152
training/megatron-fsdp.md
5253
training/resiliency.md

‎docs/training/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This directory contains comprehensive documentation for training and customizing
5252
|----------|---------|--------------|
5353
| **[Logging](logging.md)** | Logging configuration and TensorBoard/WandB integration | Monitoring training progress |
5454
| **[Profiling](profiling.md)** | Performance profiling and analysis | Identifying bottlenecks |
55+
| **[Theoretical Memory Estimator](memory-estimator.md)** | Formula-based per-GPU memory planning | Sizing dense and MoE training configs |
5556
| **[Resiliency](resiliency.md)** | Handling failures and recovery | Building robust training pipelines |
5657

5758
### Advanced Features

‎docs/training/memory-estimator.md‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Theoretical Memory Estimator
2+
3+
Megatron Bridge includes a formula-based training memory estimator for GPT-like
4+
model providers. It is intended for early configuration planning and for the
5+
training-time theoretical memory report printed alongside CUDA memory metrics.
6+
7+
The estimator is available as a Python API:
8+
9+
```python
10+
from megatron.bridge.training.utils.theoretical_memory_utils import (
11+
estimate_training_memory,
12+
format_training_memory_estimate,
13+
)
14+
15+
estimate = estimate_training_memory(cfg, num_microbatches=num_microbatches)
16+
print(format_training_memory_estimate(estimate, unit="GB"))
17+
```
18+
19+
`estimate_training_memory` returns a structured result with:
20+
21+
- dense and embedding parameter memory on the most-loaded GPU shard
22+
- routed MoE expert parameter memory when `num_moe_experts` is set
23+
- activation memory when requested
24+
- total global parameter count covered by the estimator
25+
- assumptions attached to the estimate
26+
27+
The training loop continues to use the same report hook:
28+
29+
```text
30+
Theoretical memory footprints: weight and optimizer=..., activation=..., total=...
31+
```
32+
33+
## What It Covers
34+
35+
The model-state estimate covers weights, gradients, FP32 master weights, and
36+
Adam optimizer states. It uses the same byte model as the legacy utility:
37+
38+
- `18` bytes per parameter when the distributed optimizer is disabled
39+
- `6 + 12 / shard_size` bytes per parameter when the distributed optimizer is enabled
40+
41+
For dense parameters, `shard_size` is `data_parallel_size * context_parallel_size`.
42+
For routed MoE experts, expert parameters are divided by
43+
`expert_model_parallel_size * expert_tensor_parallel_size`, and optimizer state
44+
uses the expert data-parallel shard size, including context parallel ranks.
45+
46+
The activation estimate follows the existing Megatron-LM theoretical formula and
47+
adds Bridge-aware accounting for MoE active expert width and context parallel
48+
partitioning. It is most meaningful when sequence parallelism and selective
49+
activation recomputation are enabled, which is also the condition used by the
50+
training-time report.
51+
52+
## Assumptions
53+
54+
The estimate reports the most-loaded GPU shard. Embeddings are assigned to the
55+
first and last pipeline stages; untied input/output embeddings both land on the
56+
same GPU only when `pipeline_model_parallel_size == 1`.
57+
58+
The estimator does not model runtime allocator fragmentation, CUDA kernel
59+
workspace, NCCL buffers, CUDA graph static buffers, token-routing imbalance,
60+
dispatcher workspace, CPU offloading, or full activation recomputation. Use
61+
profiling and CUDA memory reports to validate final launch configurations.
62+
63+
## MoE Notes
64+
65+
MoE layer count follows `moe_layer_freq`, including list patterns. Routed expert
66+
parameters are counted separately from dense attention, dense MLP, shared expert,
67+
normalization, and embedding parameters. This keeps expert parallelism effects
68+
visible in the returned component breakdown.
69+
70+
For memory tuning decisions, use this estimator as a planning signal together
71+
with the runtime guidance in [CPU Offloading](cpu-offloading.md),
72+
[Activation Recomputation](activation-recomputation.md), and
73+
[Megatron FSDP](megatron-fsdp.md).

‎skills/perf-memory-tuning/SKILL.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ Beyond fragmentation, actual peak memory is determined by:
3131
micro-batch size
3232
- **Temporary / workspace memory** — CUDA kernels, NCCL buffers, CUDA graphs
3333

34+
For configuration planning, use the Bridge theoretical estimator before launching
35+
large jobs:
36+
37+
```python
38+
from megatron.bridge.training.utils.theoretical_memory_utils import estimate_training_memory
39+
40+
estimate = estimate_training_memory(cfg, num_microbatches=num_microbatches)
41+
```
42+
43+
The estimator reports the most-loaded GPU shard and separates dense/embedding,
44+
routed MoE expert, and activation components. It does not include allocator
45+
fragmentation, CUDA/NCCL workspace, CUDA graph buffers, token imbalance, or
46+
dispatcher workspace, so validate final configs with runtime memory metrics.
47+
3448
## Quick Decision
3549

3650
When a training run OOMs or is close to the memory limit:
@@ -208,14 +222,16 @@ model_config = GPTModelProvider(
208222
|---|---|---|---|
209223
| OOM on a single rank despite headroom on others | Memory fragmentation | check if `expandable_segments:True` is set | set `PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True` |
210224
| OOM with `expandable_segments` already set | Genuine capacity limit | check `nvidia-smi` for param/optimizer memory | increase PP, use distributed optimizer, or add recompute |
225+
| Estimated memory exceeds GPU capacity before launch | model state or activations genuinely too large | run `estimate_training_memory` and inspect the largest component | adjust PP/TP/CP/EP, distributed optimizer, or recompute before launching |
211226
| `ValueError: PP + CPU offloading` | using cpu_offloading with PP > 1 | check PP config | disable CPU offloading or set PP=1 |
212227
| `RuntimeError` with `--use-nccl-ub` + expandable segments | NCCL UB incompatible with expandable allocator | check env vars | remove `expandable_segments:True` or disable `--use-nccl-ub` |
213228

214229
## Known Limitations
215230

216231
- CPU offloading is blocked when PP > 1
217232
- Parallelism resizing (TP/PP) often has significant throughput costs
218-
- No automatic memory profiling to recommend the optimal strategy
233+
- The theoretical estimator is formula-based and does not replace runtime
234+
profiling or CUDA memory reports
219235

220236
## Verification
221237

0 commit comments

Comments
 (0)