Skip to content

[bugfix] fix hang issue when fed empty batch - #342

Merged
shijieliu merged 1 commit into
NVIDIA:mainfrom
gameofdimension:patch-3
Apr 8, 2026
Merged

shijieliu merged 1 commit into
NVIDIA:mainfrom
gameofdimension:patch-3

Conversation

@gameofdimension

Copy link
Copy Markdown
Contributor

fix issue #341

Description

Checklist

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

@greptile-apps

greptile-apps Bot commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR removes the if num_elements == 0: continue early-exit block inside _dedup_indices, which was incorrectly bypassing CUDA dedup operations for empty batches. In distributed training, skipping these operations on some ranks while other ranks continued caused collective-communication asymmetry and hangs. The removal also implicitly fixes two latent bugs in the old empty-path:

  • ctx.frequency_counters was never appended for empty shards when LFU/admit strategy was enabled, causing index misalignment for subsequent shards' frequency counter lookups
  • reverse_idx was hardcoded as dtype=torch.uint64 in the empty path, which may not have matched the actual dtype returned by segmented_unique_cuda

Confidence Score: 5/5

Safe to merge — removes a well-understood faulty early-exit that caused distributed hangs and index misalignment.

The change is a focused one-block removal. It fixes the primary hang by ensuring symmetric CUDA kernel execution across all ranks, and simultaneously eliminates two secondary bugs (missing frequency_counters append and dtype inconsistency for reverse_idx in the empty path). No new logic is introduced. All remaining CUDA functions are documented as asynchronous and naturally handle zero-element inputs. No P0/P1 issues found.

No files require special attention.

Vulnerabilities

No security concerns identified.

Important Files Changed

Filename Overview
corelib/dynamicemb/dynamicemb/shard/embedding.py Removes the empty-batch early-exit in _dedup_indices, ensuring all ranks execute the same CUDA operations and fixing frequency_counters index misalignment when LFU/admit strategy is active.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant _dedup_indices
    participant expand_table_ids_cuda
    participant segmented_unique_cuda
    participant compute_dedup_lengths_cuda

    Caller->>_dedup_indices: input_feature (any batch size)
    Note over _dedup_indices: Old code: if num_elements==0 → continue (skip CUDA ops)<br/>New code: always proceed

    _dedup_indices->>expand_table_ids_cuda: offsets, d_table_offset, table_num, local_batchsize, num_elements
    expand_table_ids_cuda-->>_dedup_indices: table_ids

    _dedup_indices->>segmented_unique_cuda: indices, table_ids, table_num, input_frequencies
    segmented_unique_cuda-->>_dedup_indices: num_uniques, unique_keys, reverse_idx, table_offsets, freq_counters

    _dedup_indices->>compute_dedup_lengths_cuda: table_offsets, d_table_offset, table_num, local_batchsize, total_B
    compute_dedup_lengths_cuda-->>_dedup_indices: new_lengths, new_offsets

    _dedup_indices->>_dedup_indices: ctx.input_features.append(input_feature)
    _dedup_indices->>_dedup_indices: ctx.reverse_indices.append(reverse_idx)
    _dedup_indices->>_dedup_indices: ctx.frequency_counters.append(frequency_counters) [if LFU/admit]
    _dedup_indices-->>Caller: features_by_shards (dedup_features)
Loading

Reviews (2): Last reviewed commit: "Remove empty input handling in embedding..." | Re-trigger Greptile

@gameofdimension

Copy link
Copy Markdown
Contributor Author

@shijieliu please take a look

@shijieliu

Copy link
Copy Markdown
Collaborator

hi @gameofdimension thanks for your contribution! We will try to reproduce this issue first and merge after verify.

@jiashuy jiashuy self-assigned this Apr 7, 2026
@jiashuy jiashuy added the dynamicemb Related with dynamicemb label Apr 7, 2026
@jiashuy

jiashuy commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

@shijieliu this PR fix the [issue](https://github.com/NVIDIA/recsys-examples/issues/341fix issue #341) indeed, I think it can be merged.
I will add test for empty batch to _dedup_indices and check if there any other potential issue regarding this PR

Removed handling for empty input in embedding processing.
@shijieliu
shijieliu merged commit ab25f5d into NVIDIA:main Apr 8, 2026
@gameofdimension
gameofdimension deleted the patch-3 branch July 13, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dynamicemb Related with dynamicemb

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants