Skip to content

[training, test] feat: add unit tests for padding_utils helpers - #3786

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

[training, test] feat: add unit tests for padding_utils helpers#3786
lonexreb wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
lonexreb:training/test-padding-utils

Conversation

@lonexreb

Copy link
Copy Markdown
Contributor

Summary

`src/megatron/bridge/training/utils/padding_utils.py` exports three pure-tensor helpers that align variable-length batches to a target sequence length with an upper cap:

  • `pad_or_truncate_2d_to_len` — 2D (batch, seq_len) tokens/labels/loss_mask
  • `pad_or_truncate_pos_to_len` — position ids (monotonic extension)
  • `pad_or_truncate_attn_to_len` — 2D or 4D attention masks

They are CPU-only, no torch.distributed dependencies, and previously had no direct unit test coverage.

What this PR adds

20 tests in `tests/unit_tests/training/utils/test_padding_utils.py`:

`TestPadOrTruncate2dToLen` (6)

  • `None` passthrough, padding to target, custom pad_value, truncation to max_cap, identity passthrough, float-dtype pad value

`TestPadOrTruncatePosToLen` (6)

  • `None` passthrough, monotonic-range extension shape and values, batch broadcasting, dtype preservation, truncation, identity passthrough

`TestPadOrTruncateAttnToLen` (8)

  • `None` passthrough
  • 2D bool mask padded with `False`, 2D int mask padded with `0`
  • 2D truncation and passthrough
  • 4D square-mask padding in both seq dims with original block intact at top-left
  • 4D truncation and passthrough
  • 3D mask raises `ValueError`

Why

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

Test plan

  • All 20 tests sit under `tests/unit_tests/` so they run under `Launch_Unit_Tests_Core` (no functional / GPU slot)
  • CPU tensors only; no `.cuda()`, no `torch.distributed`
  • No production code changed
  • CI `Launch_Unit_Tests_Core` passes

🤖 Generated with Claude Code

padding_utils.py exports three small, pure-tensor helpers used to
align variable-length batches to a target sequence length with an
upper cap:

  pad_or_truncate_2d_to_len
  pad_or_truncate_pos_to_len
  pad_or_truncate_attn_to_len

They operate on CPU tensors only and have no GPU or distributed
dependencies, but were uncovered by unit tests.

This commit adds 20 tests across three classes covering:

- 2D pad: None passthrough, padding to target with default and
  custom pad_value, truncation to max_cap, identity pass-through
  between target and cap, and float-dtype pad value.
- Position-id pad: None passthrough, monotonic extension shape and
  values, batch broadcasting, dtype preservation, truncation to
  max_cap, and identity pass-through.
- Attention mask pad: None passthrough, 2D bool/int mask padding
  with correct pad value, 2D truncation and pass-through, 4D
  square-mask padding in both seq dims, 4D truncation and
  pass-through, and a 3D-mask ValueError.

Follows the pattern of recently-merged unit-test PRs NVIDIA-NeMo#3650 and NVIDIA-NeMo#3695.
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