Skip to content

fix(dynamicemb): send frequency weights for an empty input under index dedup - #527

Merged
shijieliu merged 4 commits into
NVIDIA:mainfrom
shijieliu:fix/index-dedup-empty-rank-20261027
Nov 9, 2026
Merged

shijieliu merged 4 commits into
NVIDIA:mainfrom
shijieliu:fix/index-dedup-empty-rank-20261027

Conversation

@shijieliu

@shijieliu shijieliu commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Description

With use_index_dedup=True and an LFU score or an admission strategy, each rank sends how often each deduplicated key occurs, as the KJT weights. A rank with no keys for a sharding sends that KJT without weights, while the other ranks send weights. The job then hangs in TorchRec's KJT all-to-all. When the first of two shardings is empty on every rank (for example, a data-parallel TorchRec table, declared first, that gets no keys), nothing hangs, but the LFU scores fall short of the access counts.

_dedup_indices adds no counters for an empty input (its empty branch came with #349), and input_dist gives entry i of ctx.frequency_counters to sharding i only when the list is long enough. So an empty input gets no weights, and an empty first sharding takes the counters of the second.

Fix:

  • For an empty input, _dedup_indices appends an empty counter tensor. The list then has one entry per sharding, in sharding order.
  • input_dist gives entry i to sharding i without the length check. Every rank sends weights for every sharding.
  • create_context is cleaned up, and DynamicEmb_APIs.md says how the counts are sent.

Tests:

  • New test options give rank 0 no keys at one iteration (--empty-rank-iteration), or add a data-parallel table first, with no keys on rank 0 (--data-parallel-table). Four new runs, all with index dedup: LFU with an empty rank (8 GPUs), LFU with the data-parallel table (1 and 8 GPUs), and the admission case empty-rank (8 GPUs).
  • On main (d0dbc08), all four fail. The three 8-GPU runs hang in the KJT all-to-all until a 300 s timeout kills them. The 1-GPU run fails with Table embedding_collection_0_0, Key 35: Expected frequency: 7, Actual score: 5.
  • With this PR, the four runs pass. test_lfu_scores.sh: 55/55 runs pass (on main, 52/52). test_embedding_admission.sh: 13 tests and 71/71 runs pass (on main, 13 tests and 70/70).
  • Measured on one 8-GPU H100 node, CI image 26.07, TorchRec 1.8.0.

Note: this PR and #525 both add runs to test_embedding_admission.sh, and they conflict only in its run-count comment. Whichever lands second needs a rebase there.

Checklist

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

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

The PR keeps frequency weights present when a rank has no keys under index dedup.

  • Empty deduplicated inputs keep frequency weights aligned across shards.

No actionable issues were found. This was a source review; GPU tests were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Input for each sharding] --> B{Needs frequency counts?}
  B -->|No| C[Append None]
  B -->|Yes| D{Has keys?}
  D -->|Yes| E[Append counts for unique keys]
  D -->|No| F[Append an empty counter tensor]
  C --> G[Read this sharding's counter entry]
  E --> G
  F --> G
  G --> H[Attach weights when the entry is not None]
  H --> I[Distribute keys and weights]
Loading

Reviews (3) · Last reviewed commit: "Merge branch 'main' into fix/index-dedup..." · Reviewed by Greptile

shijieliu and others added 3 commits November 9, 2026 14:28
…ssion

With use_index_dedup=True and an LFU score or an admission strategy, each
rank sends how often each deduplicated key occurs, as the weights of the
KJT of each sharding. But _dedup_indices adds no counters for a sharding
whose input on this rank is empty, and input_dist gives entry i of
ctx.frequency_counters to sharding i only when the list is long enough.
So:

1. A rank with no keys sends its KJT without weights, while the other
   ranks send weights. The job hangs in TorchRec's KJT all-to-all.
2. In a collection with two shardings, when the first one is empty on a
   rank, the first gets the counters of the second, and the second gets
   none. If other ranks send weights, the job hangs as in 1. If the first
   sharding is empty on every rank, nothing hangs, but no rank sends
   counters for the second one, and the LFU scores of its tables fall
   short of the access counts.

Case 2 happens when a TorchRec table sharded data parallel (named in
get_planner's data_parallel_embedding_table_names) comes before the
DynamicEmb tables of a collection: the shardings come in the order in
which the tables first use them.

The tests:
- test_lfu_scores and test_embedding_admission get --empty-rank-iteration:
  at that iteration, rank 0 has no keys.
- test_lfu_scores gets --data-parallel-table: each collection gets a
  data-parallel TorchRec table first, with no keys on rank 0.
  create_model in test_embedding_dump_load gets data_parallel_table for
  it.
- test_lfu_scores.sh gets three runs, all with index dedup: rank 0 empty
  at iteration 1 on 8 GPUs, and the data-parallel table on 1 GPU and on
  8 GPUs. On 1 GPU that sharding is empty on every rank.
- test_embedding_admission.py gets the case empty-rank: rank 0 empty at
  iteration 1 on 8 GPUs, with the timestamp score, so that only admission
  turns the counters on (test_lfu_scores covers LFU).
The checks stay as they are: every LFU score equals the access count, and
no key below the threshold is admitted.

On main (d0dbc08), on one 8-GPU H100 node (CI image 26.07, TorchRec
1.8.0), each run under a 300 s shell timeout:
- The three 8-GPU runs hang in TorchRec's KJT all-to-all. The timeout
  kills them at 300 s.
- The 1-GPU run fails its check: "Table embedding_collection_0_0, Key 35:
  Expected frequency: 7, Actual score: 5".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…x dedup

With use_index_dedup=True and an LFU score or an admission strategy, each
rank sends how often each deduplicated key occurs, as the weights of the
KJT of each sharding. _dedup_indices added no counters for a sharding
whose input on this rank is empty, and input_dist gave entry i of
ctx.frequency_counters to sharding i only when the list was long enough.
A rank with no keys for a sharding then sent that KJT without weights,
while the other ranks sent weights, and the job hung in TorchRec's KJT
all-to-all. In a collection with two shardings, an empty first sharding
also shifted the counters: the first got those of the second, and the
second got none.

Now, for an empty input, _dedup_indices appends an empty counter tensor
(uint64, on the module's device, like the others). With LFU or admission,
ctx.frequency_counters then has one entry per sharding, in sharding
order, and input_dist gives entry i to sharding i without the length
check. Every rank sends weights for every sharding, and empty weights for
an empty input: KJTAllToAll sends the weights of a KJT whenever they are
not None, empty ones too. When no input is empty, the weights are the
same tensors as before.

The comment in create_context said it pre-allocates frequency_counters
to "ensure all ranks have the same structure", but both of its branches
gave an empty list. create_context now makes the context with empty
lists, and its comment says that _dedup_indices fills
frequency_counters. Remove the commented-out copy of create_context.

On one 8-GPU H100 node (CI image 26.07, TorchRec 1.8.0):
- The four runs added by the previous commit pass.
- test_lfu_scores.sh: 55/55 runs pass. On main, 52/52 pass; the three
  more are the new runs.
- test_embedding_admission.sh: 13 tests and 71/71 runs pass. On main, 13
  tests and 70/70 runs pass; the one more is the new case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Say in the DynamicEmbeddingCollectionSharder section that with index dedup
and LFU or admission each rank sends how often each deduplicated key occurs,
as the KJT weights.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jiashuy
jiashuy force-pushed the fix/index-dedup-empty-rank-20261027 branch from 31ef5f0 to 5706694 Compare November 9, 2026 08:59
@jiashuy

jiashuy commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Rebased onto the latest main (aa0556a, includes #534) and force-pushed; head is now 5706694.

Note on the rebase: #530 already made frequency_counters per-sharding (one entry per sharding, None when a sharding needs no counters), so the index-misalignment part of this fix was already covered upstream. What was still missing is that a sharding with an empty input on a rank appended None, so that rank sent no weights while the other ranks did, and the KJT all-to-all disagreed. The fix is now just that: an empty sharding that needs counters (LFU or admission) appends an empty uint64 tensor, so every rank sends weights. input_dist and create_context follow upstream as they are.

Tested on 8x H100 (EOS) with the wheel built from this head:

  • admission: test_embedding_admission.py --case empty-rank (8 GPUs) -- passed
  • full test_lfu_scores.sh, including the new cases (empty rank at iteration 1 on 8 GPUs; data-parallel table first, no keys on rank 0, on 1 and 8 GPUs) -- passed
  • control: the same admission empty-rank case with upstream's shard/embedding.py (without this fix) hangs and times out after 600 s, so the new case does catch the bug

Not run: test_embedding_dump_load.sh, and the rest of the admission suite beyond empty-rank.

@shijieliu
shijieliu merged commit 68d6e78 into NVIDIA:main Nov 9, 2026
shijieliu added a commit to shijieliu/recsys-examples that referenced this pull request Nov 9, 2026
Merge main up to d03198c (NVIDIA#536, NVIDIA#534, NVIDIA#529, NVIDIA#527, NVIDIA#526).

- NVIDIA#527 sends empty weights for an empty input under index dedup, the
  change this branch made in shard/embedding.py: the merge keeps main's
  docstring and comments and this branch's needs_counters, the same
  condition.
- NVIDIA#529 adds PrefetchState.key_map_generation and _slots_after_rehash,
  and looks a prefetched batch's slots up again in its backward when a
  later prefetch rehashed the table. Git put _slots_after_rehash between
  PrefetchState's fields and this branch's record_stream, which then fell
  out of the class without a conflict; the merge puts it back. The
  lookup's row map (out_rows, grad_rows) only says where rows go, and the
  forward's load_from_flat_by reads value rows, which a storage keeps
  through a rehash.
- NVIDIA#534 (each table's HBM budget in fused groups), NVIDIA#536 (erase_by_score
  for lru_lfu tables, per named table) and NVIDIA#526 (VMMTensor frees after
  the queued work) merge without conflicts.

Every file of test/unit_test.sh passes on one node, each group's .sh
files with its DYNAMICEMB_TEST_GROUP, and so does the HSTU end-to-end
test on one node and on 2. On 2 nodes test_hier_dist.py (122),
test_hier_overflow.py (79) and test_hier_pipeline.py (45) pass, and on 4
nodes test_hier_dist.py (122).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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