Skip to content

BaseBatch dense tensor shape convention inconsistency with padded fields (num_candidates) #361

Closed

Description

@JacoCheung

Problem

pad_and_allgather_batch in batch_allgather.py assumes all dense tensor fields in BaseBatch follow the convention: dim-0 = actual_batch_size, so it computes eps = numel // actual_batch_size to determine the per-sample element count and pads to batch_size * eps.

However, some fields like HSTUBatch.num_candidates are already padded to batch_size by the dataloader (via pad_tensor() in hstu_sequence_dataset.py). When actual_batch_size < batch_size (incomplete batch), numel // actual_batch_size gives the wrong eps, producing an incorrectly sized tensor that causes all_gather_into_tensor to hang (size mismatch across ranks).

Example

  • num_candidates shape = [128] (padded to batch_size=128)
  • actual_batch_size = 11
  • eps = 128 // 11 = 11 (wrong, should be 1)
  • pad_size = 128 * 11 - 128 = 1280
  • Padded tensor = [1408] — other ranks send [128] → NCCL AllGather deadlock

Root Cause

There is no clear contract in BaseBatch for dense tensor fields:

  1. Should dim-0 always equal actual_batch_size (unpadded)?
  2. Or can dim-0 equal batch_size (pre-padded by dataloader)?

Currently both conventions coexist:

  • num_candidates: pre-padded to [batch_size] by dataloader
  • Dense labels in test: [actual_batch_size, num_features] (unpadded)

This ambiguity also affects:

  • _strip_dense_padding in batch_shuffler.py — uses t.reshape(full_bs, -1)[:actual_bs].reshape(-1) which assumes unpadded dim-0
  • BaseBatch.index_select — uses t.reshape(self.actual_batch_size, -1) which assumes dim-0 = actual_batch_size
  • KK algorithm and TorchRec require padded KJT lengths (no empty tensors), but the dense tensor convention is undefined

Workaround

Add a guard before padding: skip if numel % batch_size == 0 (tensor is already correctly sized):

if pad_dense and tensor_or_kjt.numel() % batch.batch_size != 0:
    eps = _elems_per_sample(tensor_or_kjt, batch.actual_batch_size)
    ...

Suggested Fix

Standardize dense tensor convention in BaseBatch:

  • Option A: All dense tensors must have dim-0 = batch_size (dataloader pads). pad_and_allgather_batch and _strip_dense_padding skip tensors already at batch_size.
  • Option B: All dense tensors must have dim-0 = actual_batch_size (unpadded). Dataloader must not pad dense fields. pad_and_allgather_batch handles all padding.
  • Option C: Add a metadata field to BaseBatch indicating which dense fields are pre-padded.

Affected Code

  • examples/commons/distributed/batch_allgather.py — allgather_field, _pad_dense
  • examples/commons/distributed/batch_shuffler.py — _strip_dense_padding
  • examples/commons/sequence_batch/batch.py — BaseBatch.index_select
  • examples/commons/datasets/hstu_sequence_dataset.py — pad_tensor for num_candidates

Reproduction

# 4 GPU movielen ranking with shuffler — hangs at eval's last incomplete batch
cd examples/hstu
torchrun --nproc_per_node 4 --master_addr localhost --master_port 6000   training/pretrain_gr_ranking.py --gin-config-file ./training/configs/movielen_ranking.gin

Activity

  1. added a commit that references this issue on Apr 14, 2026
    ea0d0bf
  2. added a commit that references this issue on Apr 17, 2026
    d9dbe30
  3. added a commit that references this issue on Apr 17, 2026
    3db734f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions