Skip to content

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

Closed
lidongdongbuaa wants to merge 5 commits into
NVIDIA:mainfrom
lidongdongbuaa:bug-dynamicemb-no-grad-slot-pins
Closed

lidongdongbuaa wants to merge 5 commits into
NVIDIA:mainfrom
lidongdongbuaa:bug-dynamicemb-no-grad-slot-pins

Conversation

@lidongdongbuaa

Copy link
Copy Markdown
Contributor

Description

Repeated training-mode forwards under torch.no_grad() gather their output but retain embedding slot pins: there is no autograd graph to release the pins in backward. Release those pins on the current stream after the gather when the result does not require gradients. Grad-enabled forwards keep their pins until backward.

Split from #538 following the review request. This PR is based directly on main@4c75b48 and has no dependency on the Docker or build changes in #538.

  • Add eight GPU regression cases covering cache/HBM storage, sequence/SUM outputs, repeated no-grad forwards, and pin retention through backward.
  • Register the regression file in the existing unit_test.sh fwd_bwd suite.
  • Run the sequence and pooled forward-only test programs under torch.no_grad().

Validation

  • Pre-commit checks passed on all five changed files: isort, autoflake, codespell, Black, and the repository's configured mypy target.
  • Python syntax checks, bash -n corelib/dynamicemb/test/unit_test.sh, and git diff --check passed.
  • The three test files are byte-for-byte identical to the previously validated 03865e2 snapshot, and the runtime forward() implementation matches that snapshot. On that earlier combined build/runtime snapshot, all eight lifecycle cases passed on GB300, alongside table/JIT tests (108 cases total, no failures/errors/skips).
  • GPU tests have not been rerun on this standalone PR's current-main base. No new GPU or CI result is claimed for this commit.

Run in a CUDA environment with DynamicEmb built and installed:

python3 -m pytest -q --import-mode=importlib \
  corelib/dynamicemb/test/unit_tests/test_no_grad_slot_lifetime.py
# Full registered forward/backward suite:
cd corelib/dynamicemb
bash test/unit_test.sh fwd_bwd

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 Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches DynamicEmb training forwards that run without gradients.

Refresh stale slots in the no-grad release path before merging.

Findings

  1. P1 Growth releases the wrong pins ▶

Summary

Releases embedding slot pins after training forwards that build no gradient graph.

  • No-grad training forwards release embedding slot pins after gathering.

GPU tests were not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Prefetch batch A and pin its slots] --> B[Prefetch batch B grows the key map]
  B --> C[Keys and pin counts move to new slots]
  C --> D[Batch A runs without gradients]
  D --> E[Release uses old slot numbers]
  E --> F[Wrong pin counts change]
Loading

Reviews (3) · Last reviewed commit: "Merge branch 'main' into bug-dynamicemb-..." · Reviewed by Greptile

jiashuy and others added 4 commits November 10, 2026 07:21
…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]>
…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]>
Comment on lines +1438 to +1443
release_slot_pins(
self._cache,
self._storage,
prefetch_state.storage_mode,
prefetch_state.slot_indices,
prefetch_state.unique_table_ids,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Growth releases the wrong pins

A queued HBM prefetch can grow the key map before an earlier batch runs under torch.no_grad(). Growth moves each key's pin count to its new slot, but this release still uses prefetch_state.slot_indices from the old map. For a NO_EVICTION table, value rows stay valid, so forward succeeds while the release decrements the wrong counters. This leaves pins stuck and can remove another batch's protection against erase.

Refresh the slots when key_map_generation changes, as backward already does.

@jiashuy

jiashuy commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

closed as merged by #547

@jiashuy jiashuy closed this Nov 11, 2026
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