Skip to main content

solvers.TrainingMemoryModel

solvers.TrainingMemoryModel()

Decomposes per-accelerator training memory into teachable components.

This model answers a different question than SingleNodeModel. Roofline feasibility asks whether a workload’s inference weights fit; training feasibility must also account for gradients, optimizer state, activations, and communication buffers. The accounting follows the common mixed-precision state breakdown used by Megatron-LM and ZeRO.

Literature Source: 1. Shoeybi et al. (2019), “Megatron-LM” (tensor/pipeline parallel state). 2. Rajbhandari et al. (2020), “ZeRO” (data-parallel state sharding). 3. Korthikanti et al. (2023), activation recomputation accounting.

Formula contract: - parameter states are sharded by TP * PP * EP before ZeRO sharding. - ZeRO-1 shards optimizer state, ZeRO-2 also shards gradients, and ZeRO-3 also shards weights across DP. - activation memory is based on local microbatch, layers per pipeline stage, hidden dimension, and the actual bytes per element for the precision.

Methods

Name Description
solve Estimate per-accelerator training memory from local state and activations.

solve

solvers.TrainingMemoryModel.solve(
    model,
    hardware,
    batch_size,
    seq_len=2048,
    precision='fp16',
    optimizer='adam',
    activation_checkpointing='selective',
    tp_size=1,
    pp_size=1,
    dp_size=1,
    ep_size=1,
    zero_stage=0,
    gradient_accumulation_steps=1,
    trainable_fraction=1.0,
    communication_buffer_fraction=0.05,
    sequence_parallel=True,
)

Estimate per-accelerator training memory from local state and activations.

Parameters

Name Type Description Default
model TransformerWorkload Parameter count, layer count, hidden width, and attention-head geometry. required
hardware HardwareNode Device memory capacity used for the residency comparison. required
batch_size int Global sequence count per update; must be at least one. required
seq_len int Tokens per sequence; must be at least one. 2048
precision str Parameter and gradient storage precision, resolved to bytes/element. 'fp16'
optimizer str Optimizer storage convention supported by optimizer_state_bytes. 'adam'
activation_checkpointing ('none', 'selective', 'full') Activation retention strategy passed to the activation-memory model. 'none'
tp_size int Tensor-parallel degree, at least one. 1
pp_size int Pipeline-parallel degree, at least one. 1
dp_size int Data-parallel degree, at least one. 1
ep_size int Expert-parallel degree, at least one. 1
zero_stage (0, 1, 2, 3) Cumulative DP sharding: optimizer state, then gradients, then weights. 0
gradient_accumulation_steps int Positive number of sequential microbatches per update. 1
trainable_fraction float Fraction in [0, 1] receiving gradients and optimizer state. 1.0
communication_buffer_fraction float Gradient staging buffer as a fraction in [0, 1] of local gradients. 0.05
sequence_parallel bool If true, split stored activations across the TP group. If false, retain the unsharded activation estimate as a conservative bound. True

Returns

Name Type Description
TrainingMemoryResult Weights, gradients, optimizer state, activations, buffers, and total memory in GB. feasible compares that modeled total with device capacity; it excludes unmodeled runtime reserves.

See Also

mlsysim.physics.memory.calc_activation_memory : Activation retention model. mlsysim.physics.memory.optimizer_state_bytes : Optimizer storage convention.

Notes

Equation: total = weights + gradients + optimizer + activations + communication_buffers. Parameter state first shards via model.sharded_parameter_count(tp_size, pp_size, ep_size) (splitting shared MoE parameters by TP * PP and routed experts by TP * PP * EP); ZeRO then divides selected terms by DP. Local microbatch is max(1, ceil(global_batch / (DP * accumulation_steps))). Layers per stage are max(1, ceil(layers / PP)). Activation bytes follow calc_activation_memory and divide by TP when sequence parallelism is enabled. Buffers contain a gradient fraction plus send/receive pipeline boundary activations.

Assumptions: balanced parameter sharding and uniform layer geometry. The precision byte count rescales the activation model’s FP16 baseline. ZeRO stages are cumulative. Trainable fraction affects gradients and optimizer state but not resident weights.

Excludes: allocator reserves, implementation workspaces, fragmentation, uneven expert placement, and detailed interleaved pipeline lifetimes. A capacity pass does not guarantee a runnable deployment.

References

Rajbhandari et al. (2020), “ZeRO: Memory Optimizations Toward Training Trillion Parameter Models.” https://arxiv.org/abs/1910.02054 (bibliography key: rajbhandari2020). Korthikanti et al. (2023), “Reducing Activation Recomputation in Large Transformer Models.” https://arxiv.org/abs/2205.05198 (bibliography key: korthikanti2023).

Examples

>>> from mlsysim import Models, Hardware
>>> result = TrainingMemoryModel().solve(Models.Language.Llama3_8B,
...     Hardware.Cloud.H100, batch_size=1, seq_len=128)
>>> result.total_memory.to("GB").magnitude > 0
True
>>> result.feasible == (result.total_memory <= result.available_memory)
True
Back to top