Estimating GPU Memory

batch_size decides how much GPU memory training uses, and the only sure way to find the largest value that fits is to try it and see whether it runs out of memory. memory_profiler gives you a good starting point without trying. It reads a training config, estimates how much memory the busiest GPU needs, and suggests the largest batch_size that should fit. It only does arithmetic on the config, so it needs no GPU and no data, and finishes in seconds.

python -m mammoth.scripts.memory_profiler config.yaml 64

The second argument is the memory of one GPU in GB: 64 for one MI250X GCD on LUMI, 96 for a GH200 on Roihu.

How the estimate works

Think of the GPU as a suitcase. Some things always go in, whatever the trip: the model weights and the optimizer state. What is left is space for the batch, and every token in the batch takes the same amount of it. The profiler measures both parts and divides.

  1. The busiest GPU. In MAMMOTH, each GPU holds only the components its tasks use. The profiler reads node_gpu, enc_sharing_group and dec_sharing_group from the tasks section, and finds the GPU with the most encoders, decoders and embeddings. All the numbers below are for that GPU.

  2. Fixed cost. Parameters of those components times the bytes each parameter needs with its gradient and optimizer state (8 bytes for bf16/fp16 with Adam, 16 for fp32 with Adam). MAMMOTH trains a bf16 model fully in bf16, without an fp32 copy of the weights, so the Adam state is bf16 too. The dtype is model_dtype in the config (bf16 if unset, the training default).

  3. Cost per token. The activations stored for the backward pass in every encoder and decoder layer, plus the output logits, which grow with the vocabulary size.

  4. Budget. Only 80% of the GPU counts as usable, and the total is multiplied by 1.5 for allocator fragmentation and communication buffers. The 1.5 was measured on LUMI MI250X, where it matched real out-of-memory errors.

Reading the report

The report for the 21-language, 5-node config from Training MAMMOTH 101, europarl_21langs_5nodes.yaml, with batch_size: 20000, src_seq_length_max / tgt_seq_length_max: 480 and valid_batch_size: 16, ends like this (64 GB GPU, bf16, 210,000-token vocabulary):

──────────────────────────────────────────────────────────────
  GPU Memory Budget
──────────────────────────────────────────────────────────────
  GPU total            : 64 GB
  Usable (80% headroom) : 51.2 GB
  Allocator overhead   : ×1.50  (fragmentation + NCCL/RCCL buffers + DDP buckets, empirical on LUMI)
  Safe active budget   : 34.1 GB
  Weight + opt         : 5.46 GB
  Available for acts   : 28.67 GB

──────────────────────────────────────────────────────────────
  Batch Size Table  (per-forward-pass, seq_length=480)
──────────────────────────────────────────────────────────────
    batch_size    Seqs    Act (GB)   Peak* (GB)  Status
  ────────────  ──────  ──────────  ───────────  ──────
         5,000      10        6.45        17.87  likely OK
         8,000      16       10.32        23.68  likely OK
        11,000      22       14.19        29.48  likely OK
        14,000      29       18.06        35.28  likely OK
        17,000      35       21.93        41.09  likely OK
        20,000      41       25.80        46.89  likely OK
        23,000      47       29.67        52.70  may OOM

  * Peak = (weight+opt + activation) × 1.50 overhead

  Suggested batch_size : ~22,225 tokens  (46 seqs × 480)  [rough estimate]
  Est. grad batch      : ~444,500 tokens  (× accum 1 × 20 GPUs)

──────────────────────────────────────────────────────────────
  Config Validation
──────────────────────────────────────────────────────────────
  batch_size=20000 tokens  (per forward pass per GPU)
  accum_count=1  (gradient accumulation steps, no memory impact)
  → forward-pass tokens : 20,000  (41 seqs)
  Active memory (est.)  : 31.26 GB
  Peak incl. overhead   : 46.89 GB  →  likely OK
  GPU utilization       : 73.3%  (tight)

──────────────────────────────────────────────────────────────
  Validation  (valid_batch_size)
──────────────────────────────────────────────────────────────
  valid_batch_size=16  →  counted in SENTENCES (validation ignores batch_type=tokens)
  valid files      : 60 task(s), largest has 1,000 lines
  → sents per batch : 16  (worst case, padded to tgt_seq_length_max=480)
  Logits peak/token : 2,461 KB  (logits + reshape copy + fp32 log_softmax, V=210000)
  Logits peak/batch : 18.02 GB
  Peak incl. overhead : 35.23 GB  →  likely OK
  Suggested valid_batch_size : ≤ 25 sentences  [worst-case length]
  • Batch Size Table: the estimated peak memory for increasing batch_size values, until one may not fit.

  • Suggested batch_size: the largest batch (in tokens, per GPU) that fits the budget.

  • Est. grad batch: how many tokens one optimizer step sees, over all GPUs and accumulation steps.

  • Config Validation: the estimate for the batch_size currently in the config. Here 20,000 tokens use about 73% of the GPU.

  • Validation: the estimate for valid_batch_size, shown when the config has validation data (path_valid_src). See the next section.

Treat the suggestion as an upper bound. Set batch_size a little below it (here 20000, a bit under the suggested 22,225) and watch the memory use in the first steps of training.

Validation batch size

valid_batch_size works differently from batch_size, and it is easy to get wrong. Validation always counts it in sentences, even when training uses batch_type: tokens. So valid_batch_size: 16 means 16 sentences per batch, not 16 tokens.

Think of validation as grading a pile of exams. Training keeps notes for every page (the activations for the backward pass), so its cost grows with the model. Validation keeps no notes, but for every word in the batch it writes out a score for every word in the vocabulary (the logits). With 210,000 tokens in the vocabulary, that is about 2.4 MB per target token, because the logits exist in several copies at once: the bf16 output, a bf16 reshaped copy, and two fp32 copies for the loss. A batch is padded to its longest target sentence. The filtertoolong transform also runs on validation data and drops pairs longer than tgt_seq_length_max, so in the worst case one sentence costs tgt_seq_length_max × 2.4 MB, about 1.2 GB at 480 tokens. This bound holds only for tasks that list filtertoolong in their transforms. Indexed datasets (data_type: indexed) ignore transforms, so their validation sentences are not limited.

The profiler reads the validation files (path_valid_tgt, or path_valid_src if unset) to count their lines. A batch can never hold more sentences than the file has lines, so the worst case is min(valid_batch_size, lines in the largest validation file). If it cannot read the files, it assumes full batches.

With the old value valid_batch_size: 4096, training fits but validation does not, and the profiler warns:

  valid_batch_size=4096  →  counted in SENTENCES (validation ignores batch_type=tokens)
  valid files      : 60 task(s), largest has 1,000 lines
  → sents per batch : 1,000  (worst case, padded to tgt_seq_length_max=480)
  Logits peak/token : 2,461 KB  (logits + reshape copy + fp32 log_softmax, V=210000)
  Logits peak/batch : 1,126.53 GB
  Peak incl. overhead : 1,697.99 GB  →  may OOM
  Suggested valid_batch_size : ≤ 25 sentences  [worst-case length]

  *** WARNING: validation will likely OOM even though training fits. ***
  Lower valid_batch_size to ~25 (sentences).

  Note: valid_batch_size=4096 looks like a token count, but validation
  batches by sentences. It is NOT tied to batch_type: tokens.

Such a run trains normally and then crashes at the first validation step, after valid_steps steps. Set valid_batch_size below the suggestion (the 21-language config uses 16). The suggestion assumes every sentence is tgt_seq_length_max tokens long, so it is safe but pessimistic. Validation speed matters little, so there is no need to push it to the limit.

Things to keep in mind

  • batch_size is per GPU and per forward pass (with batch_type: tokens). accum_count adds forward passes before each optimizer step but does not change memory.

  • valid_batch_size is always counted in sentences (default 32), whatever batch_type says. See Validation batch size.

  • The vocabulary size is read from the tokenizer files in src_vocab / tgt_vocab, so run the profiler from the directory where those paths exist. If it cannot read them, it assumes 32,000. A larger vocabulary makes the output layer bigger and the suggested batch smaller.

  • The sequence length is the larger of src_seq_length_max and tgt_seq_length_max from the config (200 if unset, the filtertoolong default), shown as seq_length in the table title. It only converts tokens to sentences in the Seqs column, and adds a small attention term per token. To choose these limits, see Choosing Sequence Lengths.

  • The FFN size follows ff_activation and ff_mult like training does: if ff_mult is unset it is 2.67 for swiglu (the default) and 4.0 for gelu. The output layer (to_logits) is always counted separately from the target embedding, because MAMMOTH does not tie them. With share_encoder_decoder_embeddings: true, a target language that is also a source language on the same GPU reuses the source embedding and is not counted again.

  • Attention is assumed to run as a fused kernel (flash or memory-efficient), which does not store the attention matrix. If your GPU falls back to the plain math kernel, add --no_fused_attn.

  • The estimate is per token and does not model padding. Long sentences next to short ones in the same batch use more memory than the estimate.

  • A bigger batch changes the training dynamics, so the learning rate and warmup may need adjusting. See Training tips.

Options

Option

Effect

config

The training YAML (positional).

gpu_memory_gb

Memory of one GPU in GB (positional).

--vocab_size N

Use this vocabulary size instead of reading the tokenizer files.

--no_fused_attn

Also count the attention matrix, for when attention does not run as a fused kernel.

--headroom F

Fraction of GPU memory treated as usable (default 0.80).

--allocator_overhead F

Multiplier for allocator and communication overhead (default 1.50).