Repository navigation
Description
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_candidatesshape =[128](padded tobatch_size=128)actual_batch_size = 11eps = 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:
- Should dim-0 always equal
actual_batch_size(unpadded)? - 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_paddinginbatch_shuffler.py— usest.reshape(full_bs, -1)[:actual_bs].reshape(-1)which assumes unpadded dim-0BaseBatch.index_select— usest.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_batchand_strip_dense_paddingskip tensors already atbatch_size. - Option B: All dense tensors must have dim-0 =
actual_batch_size(unpadded). Dataloader must not pad dense fields.pad_and_allgather_batchhandles all padding. - Option C: Add a metadata field to
BaseBatchindicating which dense fields are pre-padded.
Affected Code
examples/commons/distributed/batch_allgather.py—allgather_field,_pad_denseexamples/commons/distributed/batch_shuffler.py—_strip_dense_paddingexamples/commons/sequence_batch/batch.py—BaseBatch.index_selectexamples/commons/datasets/hstu_sequence_dataset.py—pad_tensorfornum_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
fix: unify dense tensor padding convention (dim-0 == batch_size)
fix: unify dense tensor padding convention (dim-0 == batch_size)
fix: unify dense tensor padding convention (dim-0 == batch_size) (NVI…