Repository navigation
fix(dynamicemb): keep prefetched keys' ref counts through a rehash - #529
Merged
Merged
Conversation
Contributor
[Medium impact] The PR appears safe to merge; no blocking issues were found. SummaryThe PR keeps prefetched keys protected when a later batch expands the tables.
No actionable issues were found. Tests were not run during this review. DiagramsequenceDiagram
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
Reviews (2) · Last reviewed commit: "fix(dynamicemb): assert a table re-looke..." · Reviewed by Greptile |
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
force-pushed
the
fix/rehash-ref-counts-20261030
branch
from
November 9, 2026 08:10
bb2e864 to
e6d373a
Compare
jiashuy
approved these changes
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]>
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]>
This was referenced Nov 10, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):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_slotsanddst_slotsare per table, while_ref_counteris flat over the tables. The counter of a table's slot is attable_bucket_offsets[table] * bucket_capacity + slot, which is howupdate_counter_with_layout_kernelindexes it.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_implwrites each counter at its flat index in the new key map. The basessrc_baseanddst_baseare the table's first bucket times the bucket capacity. It also counts the rehashes inDynamicEmbTableState.rehashes.PrefetchState.rehashes, thenctx.rehashesin 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.Test
New case
test_prefetch_pipeline_keeps_ref_counts_through_expansionintest/test_batched_dynamic_embedding_tables_v2.py(fwd_bwd group oftest/unit_test.sh):Results on Eos (one DGX H100 node, the 26.07 CI image), with main at d5470b8:
assert 1024 == 1536, because the rehash loses the first batch's 512 countstest_batched_dynamic_embedding_tables_v2.pytest_no_eviction_row_indexing.pytest_expansion_value_capacity.pyCompared 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