Skip to content

FIX](DynamicEmb) Release slot pins after no-grad training forwards - #547

Merged
shijieliu merged 4 commits into
NVIDIA:mainfrom
jiashuy:fix/dynamicemb-no-grad-slot-pins
Nov 11, 2026
Merged

shijieliu merged 4 commits into
NVIDIA:mainfrom
jiashuy:fix/dynamicemb-no-grad-slot-pins

Conversation

@jiashuy

@jiashuy jiashuy commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Supersedes #541 (its commit is kept, authored by @lidongdongbuaa ).

A training-mode forward under torch.no_grad() never runs backward, so the ref-counter pins that prefetch took on its slots were never released and eventually filled the cache/bucket.

  • Release the pins in BatchedDynamicEmbeddingTablesV2.forward when the output has no autograd graph (release_slot_pins, shared with backward).
  • Refresh a batch's slots first if a later batch's prefetch rehashed the table since (refresh_slots_after_rehash, shared with backward; it replaces _slots_after_rehash from fix(dynamicemb): keep prefetched keys' ref counts through a rehash #529). Without it a no-grad forward released the stale slots: other keys' counters went below 0 and its own new slots stayed pinned.
  • Tests (test_no_grad_slot_lifetime.py, 46 cases): HBM / cache / hybrid / host-only layouts, with and without frequency admission, two batches in flight (grad and no-grad, both orders), and an expansion between two prefetches. On Eos H100 they pass with the fix; without it the no-grad cases on HBM/cache fail (pins left) and the rehash case fails with a counter below 0.

Original fix by lidongdong.2024 (commit 4bc4f25).

Checklist

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

lidongdongbuaa and others added 4 commits November 11, 2026 07:24
…d 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]>
…tches 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]>
…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]>
Signed-off-by: Jiashu Yao <[email protected]>
@greptile-apps

greptile-apps Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Releases slots held by training forwards that have no autograd graph.

  • Training forwards without an autograd graph release their slot pins.

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

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Prefetch pins slots] --> B[Forward copies embeddings]
  B --> C{Output requires grad?}
  C -->|No| D[Refresh slots after table growth]
  C -->|Yes| E[Backward updates embeddings]
  E --> F[Refresh slots after table growth]
  D --> G[Release this batch's pins]
  F --> G
Loading

Reviews (2) · Last reviewed commit: "[FIX](DynamicEmb) Refresh a batch's slot..." · Reviewed by Greptile

@shijieliu
shijieliu merged commit 665ffd9 into NVIDIA:main Nov 11, 2026
1 check passed
shijieliu added a commit to shijieliu/recsys-examples that referenced this pull request Nov 12, 2026
Merge main up to 476cdc0: NVIDIA#544 and NVIDIA#547 in DynamicEmb; NVIDIA#535, NVIDIA#537,
NVIDIA#539 and NVIDIA#540 outside it.

- NVIDIA#544 deduplicates int32 keys as int64 in _dedup_indices and returns
  them in the input's dtype, as this branch did. The merge keeps main's
  widening (indices_64bit) and drops this branch's copy of it, so the
  keys are widened once. It keeps this branch's structure: a sharding on
  the hierarchical dist keeps its unique keys sized on the device, and
  TorchRec's dists slice them to the count. The docstring says both.
- NVIDIA#544 also bucketizes an int32 key as the int64 key of its value. The
  hierarchical dist's own bucketize still reads a key in its own width;
  the next commit gives it int32 keys as int64.
- DynamicEmb_APIs.md: main's paragraph on how the rules read a key, then
  this branch's line that the hierarchical dist takes roundrobin and
  hash_roundrobin only.
- test_sequence_embedding_fw.py and test_twin_module.py: main's int32
  runs (NVIDIA#544) and no-grad forward (NVIDIA#547), with this branch's checks that
  a sharding took the dist it was given (check_path, check_totals).
- NVIDIA#547 (a no-grad training forward releases its slot pins, after
  refreshing its slots past a rehash) merges without conflicts next to
  this branch's row map and load_from_flat_by.

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.

3 participants