Skip to content

[training, test] feat: add unit tests for theoretical_memory_utils - #3785

Closed
lonexreb wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
lonexreb:training/test-theoretical-memory-utils
Closed

[training, test] feat: add unit tests for theoretical_memory_utils#3785
lonexreb wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
lonexreb:training/test-theoretical-memory-utils

Conversation

@lonexreb

Copy link
Copy Markdown
Contributor

Summary

`src/megatron/bridge/training/utils/theoretical_memory_utils.py` exports three public helpers that compute model memory footprint analytically from a `ConfigContainer`:

  • `compute_weight_and_optimizer_memory`
  • `compute_activation_memory`
  • `report_theoretical_memory`

They do pure arithmetic on dataclass fields — no GPU, no model construction, no checkpoints — so they're a natural unit-test target that was previously uncovered.

What this PR adds

19 tests across four classes in `tests/unit_tests/training/utils/test_theoretical_memory_utils.py`:

`TestComputeWeightAndOptimizerMemory` (11)

  • Returns positive float
  • Non-distributed optimizer uses 18 bytes/param
  • Distributed optimizer scales with `data_parallel_size` (6 + 12/dp)
  • Tensor parallel reduces per-shard memory
  • Separate (non-shared) embeddings increase param count
  • Gated linear + SiLU adds MLP params (and the gated-linear flag alone, without SiLU, is correctly not treated as gated)
  • More layers / more MoE experts increase memory
  • GQA (`num_query_groups < num_attention_heads`) reduces attention params
  • Verbose mode prints layer counts
  • PP > 1 verbose mode logs "other shards"

`TestComputeActivationMemory` (7)

  • Returns positive float
  • Layer / TP / micro-batch sensitivity
  • Virtual-PP interleaved schedule memory penalty
  • Non-interleaved PP microbatch discount (`min(1, num_microbatches/pp)`)
  • PP=1 output-layer / CE loss term executes
  • Verbose mode prints interleaved penalty / microbatch info

`TestReportTheoreticalMemory` (3)

  • Skips activation breakdown when `sequence_parallel=False`
  • Prints full `weight=… activation=… total=…` when seq-parallel + selective recompute
  • Skips activation breakdown when `recompute_granularity != "selective"`

`MegatronMIMOProvider` is patched out so the early-return MIMO check doesn't short-circuit normal configs.

`TestNumBytesInMegabyteConstant` (1)

  • Sanity check on the public byte constant

Why

Follows the recently-merged unit-test pattern from #3650 (pg_utils) and #3695 (FLOPs calculator) — small, GPU-free, high-signal coverage of an analytic helper.

Test plan

  • 19 new tests, all in `tests/unit_tests/` so they run under `Launch_Unit_Tests_Core` (no functional / GPU slot)
  • Mocks use `SimpleNamespace` and `unittest.mock.patch` — no real model build
  • No production code changed
  • CI `Launch_Unit_Tests_Core` passes

🤖 Generated with Claude Code

theoretical_memory_utils.py exports three public helpers
(compute_weight_and_optimizer_memory, compute_activation_memory,
report_theoretical_memory) and one private vocab helper. All four
operate on a ConfigContainer-shaped object and do pure arithmetic
— no GPU, no model construction, no checkpoints — so they are a
natural unit-test target.

This commit adds 19 tests across four classes that exercise:

- weight-and-optimizer memory: positive output, distributed vs.
  non-distributed optimizer accounting, data-parallel scaling,
  tensor-parallel sharding, separate-vs-shared embeddings, gated
  linear (silu vs. non-silu), layer count, MoE expert count, GQA
  vs. MHA, and verbose-mode logging
- activation memory: positivity, layer/TP/batch sensitivity,
  interleaved VPP penalty, non-interleaved PP microbatch discount,
  PP=1 output-layer term, and verbose logging
- report_theoretical_memory: sequence-parallel + selective recompute
  prints the full breakdown; otherwise weight-only; full recompute
  also prints weight-only. MegatronMIMOProvider is patched out so
  the early-return MIMO check does not short-circuit normal configs.
- NUM_BYTES_IN_MEGABYTE constant

Follows the pattern of recently-merged unit-test PRs NVIDIA-NeMo#3650 (pg_utils)
and NVIDIA-NeMo#3695 (FLOPs calculator). No production changes.

Signed-off-by: lonexreb <reach2shubhankar@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented May 12, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added area:training Training loop, callbacks, and runtime integration ci CI, automation, test queue, or workflow infrastructure work needs-review PR is ready for code review and waiting on a reviewer labels May 12, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label May 14, 2026
@yaoyu-33 yaoyu-33 removed the needs-review PR is ready for code review and waiting on a reviewer label May 17, 2026
@yaoyu-33 yaoyu-33 closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:training Training loop, callbacks, and runtime integration ci CI, automation, test queue, or workflow infrastructure work community-request waiting-on-maintainers Waiting on maintainers to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants