Skip to content

fix(dynamicemb): keep prefetched keys' ref counts through a rehash - #529

Merged
shijieliu merged 2 commits into
NVIDIA:mainfrom
shijieliu:fix/rehash-ref-counts-20261030
Nov 9, 2026
Merged

shijieliu merged 2 commits into
NVIDIA:mainfrom
shijieliu:fix/rehash-ref-counts-20261030

Conversation

@shijieliu

Copy link
Copy Markdown
Collaborator

Problem

In a prefetch pipeline, the prefetch of a later batch can expand a table before the backward of an earlier batch runs. The earlier batch's keys then lose their ref counts. Its backward also takes 1 off the counters of the slots its keys held before the rehash, and those counters end at -1. A key whose counter is not above 0 counts as not in use, so an insert may evict it while a prefetched batch still uses it.

Two things go wrong in the rehash (_expand_tables_impl):

  • It carries the counters with new_key_index_map._ref_counter[dst_slots].copy_(old_key_index_map._ref_counter[src_slots]). Advanced indexing returns a copy, so the counts go into a temporary and are lost. Also, src_slots and dst_slots are per table, while _ref_counter is flat over the tables. The counter of a table's slot is at table_bucket_offsets[table] * bucket_capacity + slot, which is how update_counter_with_layout_kernel indexes it.
  • The backward of a batch prefetched before the rehash takes the counts off at the slots its prefetch found. After the rehash, those slots belong to other keys or to none.

It showed up in the hierarchical dist PR (#528), whose pipeline test runs tables that rehash. Without the fix, that test ends with 207,981 counters at -1 over 8 ranks, and the same happens with TorchRec's dists. The bug is in main by itself, so the fix gets its own PR.

Fix

  • _expand_tables_impl writes each counter at its flat index in the new key map. The bases src_base and dst_base are the table's first bucket times the bucket capacity. It also counts the rehashes in DynamicEmbTableState.rehashes.
  • The HBM-direct prefetch keeps that count with its slots (PrefetchState.rehashes, then ctx.rehashes in the forward). If the table has rehashed since then, the backward looks the batch's keys up again, read only (_slots_after_rehash), and takes the counts off at their new slots.
  • Value rows do not move: a storage keeps each key's row through a rehash, and a cache does not rehash. The cache path keeps its counts on the cache, so it does not change. In a training loop without a pipeline, a forward's own prefetch does any expansion before it finds its slots, so its backward does not look the keys up again.

Test

New case test_prefetch_pipeline_keeps_ref_counts_through_expansion in test/test_batched_dynamic_embedding_tables_v2.py (fwd_bwd group of test/unit_test.sh):

  • Two HBM tables of 1024 slots each.
  • The first batch (256 keys) is prefetched, then the second batch (512 keys, 128 of them shared). The second prefetch expands both tables to 2048 slots.
  • Then the forward and backward of each batch run.
  • The case checks that the counts add up after the rehash (1536), that no counter goes below 0 after each backward, and that every counter is 0 at the end.

Results on Eos (one DGX H100 node, the 26.07 CI image), with main at d5470b8:

main this PR
new case fails (main with the new test): assert 1024 == 1536, because the rehash loses the first batch's 512 counts passes
counters after each step (a script that runs the same steps) after the rehash: sum 1024, max 1. After the first backward: 172 at -1. At the end: 512 at -1 after the rehash: sum 1536, max 2. Never below 0. At the end: all 0
test_batched_dynamic_embedding_tables_v2.py 390 passed, 4 skipped 391 passed, 4 skipped
test_no_eviction_row_indexing.py 21 passed 21 passed
test_expansion_value_capacity.py 11 passed 11 passed
the other 19 files of the fwd_bwd group all exit 0 (an earlier run of the same main build) all exit 0, with the same pytest counts

Compared test by test, every test that passes on main also passes with this PR. The only new test is the new case. On main with the new test added, the whole file gives 1 failed (the new case), 390 passed and 4 skipped.

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

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

Summary

The PR keeps prefetched keys protected when a later batch expands the tables.

  • Prefetched batches keep their keys pinned through table expansion.

No actionable issues were found. Tests were not run during this review.

Diagram

sequenceDiagram
    participant A as Batch A
    participant T as Storage
    participant B as Batch B
    A->>T: Prefetch keys and pin slots
    B->>T: Prefetch triggers expansion
    T->>T: Carry counts and keep value rows
    A->>T: Backward detects changed generation
    T->>T: Find current slots without changing scores
    A->>T: Update value rows and release current slots
Loading

Reviews (2) · Last reviewed commit: "fix(dynamicemb): assert a table re-looke..." · Reviewed by Greptile

shijieliu and others added 2 commits November 9, 2026 13:10
What goes wrong: in a prefetch pipeline, the prefetch of a later batch
can expand the table before the backward of an earlier batch runs. The
ref counts of the earlier batch's keys are lost, and its backward takes
1 off the counters of the slots its keys held before the rehash. Those
counters end at -1. A key whose counter is not above 0 counts as not in
use, so an insert may evict it while a prefetched batch still uses it.

Why: a prefetch adds 1 to the ref counter of each key of its batch, at
the key's slot, and the batch's backward takes it off at that slot. A
rehash (_expand_tables_impl) moves keys to other slots, and two things
go wrong:
- It copies the counters with
  new._ref_counter[dst_slots].copy_(old._ref_counter[src_slots]).
  Advanced indexing returns a copy, so the counts go into a temporary
  and are lost. Also, the slots are per table, while the counters are
  flat over the tables: the counter of a table's slot s is at
  table_bucket_offsets[table] * bucket_capacity + s.
- The backward of a batch prefetched before the rehash takes the counts
  off at the slots its prefetch found. After the rehash, those slots
  belong to other keys or to none.

The fix:
- The rehash writes each counter at its flat index in the new key map,
  and counts the rehashes of the table state
  (DynamicEmbTableState.rehashes).
- A prefetch on the HBM-direct path keeps that count with its slots
  (PrefetchState.rehashes, then ctx.rehashes). If the table rehashed
  since then, the backward looks the batch's keys up again, read only,
  and takes the counts off at their new slots (_slots_after_rehash).
  Value rows do not move: a storage keeps each key's row through a
  rehash, and a cache does not rehash.

Test: test_prefetch_pipeline_keeps_ref_counts_through_expansion in
test_batched_dynamic_embedding_tables_v2.py prefetches two batches into
two tables, and the second prefetch expands both tables before the
first backward. The counts must add up after the rehash, no counter may
go below 0, and every counter must be 0 after both backwards.

Evidence, on Eos H100, this commit against main (d5470b8):
- The new case fails on main (assert 1024 == 1536: the rehash loses
  the first batch's 512 counts) and passes with this commit. Step by
  step on main, 172 counters are at -1 after the first backward and 512
  after both. With this commit the counts are 1536 after the rehash,
  none goes below 0, and all are 0 at the end.
- test_batched_dynamic_embedding_tables_v2.py: 391 passed, 4 skipped
  (main: 390 passed, 4 skipped). Every test that passes on main passes
  with this commit. test_no_eviction_row_indexing.py (21 passed) and
  test_expansion_value_capacity.py (11 passed) are the same on both.
- The other 19 files of the fwd_bwd group of test/unit_test.sh all
  exit 0, with the same pytest counts as on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lue rows

_slots_after_rehash refreshes only the hash slots (ref counter). That is enough
because DynamicEmbStorage keeps each key's value row through a rehash. A table
whose row is its slot would also have its value rows moved, and the prefetched
batch's forward/backward would read and update stale rows. Fail loudly instead.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@jiashuy
jiashuy force-pushed the fix/rehash-ref-counts-20261030 branch from bb2e864 to e6d373a Compare November 9, 2026 08:10
@shijieliu
shijieliu merged commit 331fcd8 into NVIDIA:main Nov 9, 2026
1 check passed
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]>
jiashuy added a commit to jiashuy/recsys-examples that referenced this pull request Nov 10, 2026
…grad forward releases its pins

A later batch's prefetch may expand the table before an earlier batch's
no-grad forward runs. That forward released its pins at the slots its prefetch
found, which a rehash had moved: other keys' counters went below 0 and its own
new slots stayed pinned. Only backward refreshed the slots (NVIDIA#529).

Move that refresh into refresh_slots_after_rehash, which updates the slots and
the rehash count on the PrefetchState (or the ctx that took it over), and call
it from both the backward and the no-grad forward. release_slot_pins only
decrements. Add a test that expands the tables between two prefetches.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
shijieliu pushed a commit that referenced this pull request Nov 11, 2026
)

* fix(dynamicemb): release slot pins after no-grad training forwards

Signed-off-by: lidongdong.2024 <[email protected]>

* [REF](DynamicEmb) Share release_slot_pins between backward and no-grad forward

The backward and the no-grad forward both undo the ref-counter pins that
prefetch took. Put the choice of counter owner (cache or storage, by the
batch's storage_mode) in one function so the two paths cannot disagree.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Jiashu Yao <[email protected]>

* [TEST](DynamicEmb) Cover layouts, admission and mixed grad/no-grad batches for slot pins

Run the slot-pin lifetime tests on every storage layout (HBM, cache, hybrid,
host-only), with and without a frequency admission that rejects a key on its
first sight (slot -1). Add a case with two batches in flight, one under no_grad
and one with grad, in both orders: releasing the no-grad batch must leave the
other batch's pins until its backward.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Jiashu Yao <[email protected]>

* [FIX](DynamicEmb) Refresh a batch's slots after a rehash before a no-grad forward releases its pins

A later batch's prefetch may expand the table before an earlier batch's
no-grad forward runs. That forward released its pins at the slots its prefetch
found, which a rehash had moved: other keys' counters went below 0 and its own
new slots stayed pinned. Only backward refreshed the slots (#529).

Move that refresh into refresh_slots_after_rehash, which updates the slots and
the rehash count on the PrefetchState (or the ctx that took it over), and call
it from both the backward and the no-grad forward. release_slot_pins only
decrements. Add a test that expands the tables between two prefetches.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Jiashu Yao <[email protected]>

---------

Signed-off-by: lidongdong.2024 <[email protected]>
Signed-off-by: Jiashu Yao <[email protected]>
Co-authored-by: lidongdong.2024 <[email protected]>
Co-authored-by: Claude Sonnet 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