|
| 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). |
0 commit comments