Skip to content

Fix empty batch hang fundamentally - #349

Merged
shijieliu merged 3 commits into
NVIDIA:mainfrom
jiashuy:fix_empty_batch_hang
Apr 9, 2026
Merged

shijieliu merged 3 commits into
NVIDIA:mainfrom
jiashuy:fix_empty_batch_hang

Conversation

@jiashuy

@jiashuy jiashuy commented Apr 8, 2026 •

Copy link
Copy Markdown
Collaborator

Description

There is an issue when fed empty batch to the DMP model on a single rank while other rank's batch are not empty.
It was fixed previously, and this PR provides:

  • A test to reproduce the hang issue: issue
  • Fix the hang issue by provide the same data type of dedup_features.lengths: hang issue is because the data type mismatched due to different code path.

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@jiashuy
jiashuy requested a review from shijieliu April 8, 2026 10:01
@jiashuy

jiashuy commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator Author

CI

@greptile-apps

greptile-apps Bot commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a distributed hang caused by a dtype mismatch in dedup_features.lengths when one rank receives an empty batch while others do not. The fix has two parts: unique_op.cu now allocates new_lengths as int64 (matching new_offsets) instead of int32, and embedding.py adds an early-exit path for empty batches that explicitly casts lengths to torch.int64.

Confidence Score: 5/5

Safe to merge — the dtype fix is correct and the previous review concern about empty-batch lengths casting has been addressed.

Both the CUDA kernel change and the Python empty-batch early-exit are consistent and correct. The only remaining finding is a P2 style issue (missing torch.no_grad() around a test forward pass) that does not affect correctness or reliability.

No files require special attention.

Vulnerabilities

No security concerns identified.

Important Files Changed

Filename Overview
corelib/dynamicemb/src/unique_op.cu Root-cause fix: changes new_lengths tensor dtype from at::kInt (int32) to at::kLong (int64) in both the empty-case early-return and the general allocation path, and updates the DataType dispatch argument passed to get_new_length_and_offsets accordingly. Consistent and correct.
corelib/dynamicemb/dynamicemb/shard/embedding.py Adds an early-return for the empty-batch case (num_elements == 0) that produces a KeyedJaggedTensor with lengths cast to torch.int64, preventing dtype mismatch with the non-empty path when distributed ranks have uneven batches. .to(torch.int64) on lengths aligns with the offsets cast already present.
corelib/dynamicemb/test/unit_tests/test_sequence_embedding_fw.py Adds an extra iteration after num_iterations that gives rank 0 an empty KeyedJaggedTensor (all-zero lengths, no values) while other ranks get normal non-empty features, directly reproducing the multi-rank hang scenario from issue #341.
corelib/dynamicemb/test/test_batched_dynamic_embedding_tables_v2.py Adds a single-rank "all keys missed in eval" corner-case forward pass; minor style issue — the new call runs outside torch.no_grad() while the model is still in eval mode.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[input_feature_splits loop] --> B{num_elements == 0?}
    B -- Yes --> C[Build KJT with lengths cast to int64]
    C --> D[Append empty reverse_indices int64]
    D --> E[features_by_shards.append\ncontinue]
    B -- No --> F[expand_table_ids_cuda]
    F --> G[segmented_unique_cuda]
    G --> H[compute_dedup_lengths_cuda]
    H --> I{new_lengths_size == 0?}
    I -- Yes --> J[Return empty int64 lengths\nzero int64 offsets]
    I -- No --> K[Allocate new_lengths as int64\nAllocate new_offsets as int64]
    K --> L[get_new_length_and_offsets\nDataType::Int64 + DataType::Int64]
    L --> M[Return new_lengths int64\nnew_offsets int64]
    J --> N[Build KJT with int64 lengths]
    M --> N
    N --> E
Loading

Reviews (2): Last reviewed commit: "Cast lengths to int64 explictly in dedup..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants