Skip to content

feat(dynamicemb): erase_by_score for lru_lfu tables, per named table - #536

Merged
shijieliu merged 6 commits into
NVIDIA:mainfrom
jyuan-sc:jyuan/erase-by-score-lru-lfu
Nov 9, 2026
Merged

shijieliu merged 6 commits into
NVIDIA:mainfrom
jyuan-sc:jyuan/erase-by-score-lru-lfu

Conversation

@jyuan-sc

@jyuan-sc jyuan-sc commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

Summary

erase_by_score_async (#532) gains three things.

  • It works on (TIMESTAMP, LFU) (lru_lfu) tables. The strategy you pass picks the score word compared: TIMESTAMP compares the last-access time, and LFU compares the access count.
  • A table_names filter, so tables that share a batched module can be erased at different thresholds: one call per threshold.
  • strict=False, which skips names the calling rank doesn't hold, so the same call works on every rank under table-wise and table-row-wise sharding. strict defaults to True, so existing callers see no change.

device_timestamp, the device clock a TIMESTAMP threshold is read from, is now exported from dynamicemb.

The motivating use is a per-table time-to-live in online training. At a training checkpoint boundary, keys not accessed within each table's max age are erased, so an id that comes back starts from a fresh embedding. The threshold is device_timestamp() - max_age_ns.

Changes

Four commits, in order:

  1. 6f170ef Erase by score on (TIMESTAMP, LFU) tables, by either word.
    • The kernel already takes num_scores and score_index and clears every score word of an erased slot (fix(dynamicemb): clear every score word in table_erase_kernel #523); only the Python checks were in the way.
    • A (TIMESTAMP, LFU) table now qualifies for both TIMESTAMP and LFU. The word compared comes from get_physical_score_order, so (TIMESTAMP, LFU) and (LFU, TIMESTAMP) behave the same.
    • The word is passed from the module (erase_score_word) through DynamicEmbStorage.erase_by_score to LinearBucketTable.erase_by_score(score_word=...), which hands it to the kernel as score_index.
    • Any other multi-word layout is still refused.
    • The docs note that an LFU-only erase also removes keys that were just admitted, because the count never decays.
  2. ba123d8 Narrow erase_by_score_async to named tables.
    • Both erase_by_score_async(model, ...) and BatchedDynamicEmbeddingTablesV2.erase_by_score_async(...) take table_names: Optional[Sequence[str]] = None.
    • Each module erases only the named tables it holds, and a module that holds none isn't started. None keeps the current behaviour: every table the strategy erases.
    • Bad names are refused at the call, before any module starts: a name in no module (model level) or not in this module (module method), a name of a table the strategy doesn't erase, an empty list, and a bare str (TypeError).
    • DynamicEmb_APIs.md has an example with two tables at two TTLs, one call each.
  3. 1f7308f Docs only: generic table names in that example.
  4. 0c64430 strict=False for the model-level call, and device_timestamp exported from dynamicemb.
    • With strict=False, a name that no module of this rank's model holds is skipped instead of raising. A rank that holds none of the names erases nothing and returns (0, 0). A held name that the strategy doesn't erase still raises. The module method keeps its own check.
    • device_timestamp can be imported from dynamicemb; the docs and the example use it from there. The package imports torch before dynamicemb_extensions, as every submodule does, since the extension needs the libraries torch loads.
    • The docs say how to get counts per table: one call per table; each table is scanned once either way.

The scan, the chunking, the gate, the layouts check and the erase_untracked marking are unchanged. Nothing changes in CUDA.

This PR is independent of #518, which was closed; the same kernel fix landed as #523, which is already on main.

Tests

On an H100:

  • test/unit_tests/erase_async/test_erase_by_score_lru_lfu.py (new, 16 tests): each word compared on its own; both words cleared and the reclaimed slot reused before any live key is evicted; pinned slots spared; both tuple orders behave the same; a model mixing LFU and (TIMESTAMP, LFU) tables.
  • test/unit_tests/erase_async/test_erase_table_filter.py (new, 16 tests): only the named table is erased; two tables at two thresholds back to back; None equals naming every table; bad names refused with nothing erased; at model level each module gets the names it holds and a bad name starts no module; with strict=False names this rank doesn't hold are skipped, a rank holding none erases nothing, and a held name the strategy doesn't erase is still refused; import dynamicemb; dynamicemb.device_timestamp() works in a fresh process without torch's lib directory on LD_LIBRARY_PATH.
  • test_erase_by_score.py and test_erase_api.py are updated for the new acceptance rules.
  • bash test/unit_tests/erase_async/test_erase_async.sh: 180 passed, 0 skipped. test_erase_distributed and test_erase_stress report all cases passed.
  • test/unit_tests/table_operation/test_lru_lfu.py: 36 passed.

The four commits merge cleanly onto the current main (c6e2bb2); nothing on main since 2d1dfcb touches corelib/dynamicemb.

Integration evidence

We integrated this branch into an online-training loop for a production recommendation model.

  • Setup: row-wise sharding, HBM-only storage, lru_lfu tables. At each window boundary, before the checkpoint, one call per TTL table, with table_names=[name] and strict=False. One extra pass right after a checkpoint restore, before the first training step.
  • Short-TTL test, 16 GPUs over 2 nodes, 5 windows. One table (capacity ~100M keys) had a 120 s TTL; another table in the same model had a 90-day TTL.
    • At every boundary, every surviving key of the 120 s table was younger than the TTL. The widest per-rank spread of last-access timestamps after an erase was 114 s.
    • The 90-day table lost no keys, while its keys' timestamps spread to 330 s.
    • Each erase took 0.01–0.06 s, against about 35 s of training per window.
    • Training loss matched an earlier run of the same setup within noise.
  • Short-TTL test, 8 GPUs on one node (60 s TTL).
    • It erased 0, 32.5M and 32.5M keys over 3 windows, with 0 spared. The short-TTL table stayed at about 65M keys; with day-scale TTLs the same table reached its 100M capacity by the third window.
    • Restore: we stopped after window 3, waited 90 s, and resumed from the checkpoint. The post-restore pass erased all 64.9M keys of the short-TTL table before any lookup. The next window erased 0, since every key was fresh, and the one after erased 32.3M.
  • Day-scale TTLs (1 day / 90 days) on the same stream: 0 erased in every window and at restore, as expected.
  • End-to-end unit tests (H100): only stale keys of the TTL tables are erased; erased keys come back at the initializer value; a long-TTL table keeps its trained values; keys loaded from a checkpoint older than the TTL are erased by the post-restore pass before any lookup.

Follow-ups

We plan to send these as separate PRs after this one, and would welcome the maintainers' view on the approach.

  1. Record the keys erase_by_score erases, for incremental dumps and delta updates.
    • Today erase_by_score records nothing about what it erases. A table it removed keys from is marked erase_untracked, incremental_dump reports the mark, and replay_increment refuses the delta. So a delta from a TTL table can't carry the TTL's deletions to a replica or to serving, which then keep the expired keys, and online training can't use a TTL and delta updates together.
    • Proposal: a recording mode for erase_by_score, matching erase_keys(..., EvictedItemMode.RETAIN_KEY). The erased keys, with their slot indices for tables that store rows, are appended to the buffer pop_erased_keys drains; incremental_dump carries them in erased_keys / erased_slot_index; and the table isn't marked erase_untracked, so its deltas stay replayable.
    • What it needs: the scan kernel clears slots in place and only counts them today, so it must also output the keys and slots it clears, for example through an append buffer per chunk.
  2. Timestamps across restore.
    • lru_lfu file checkpoints keep raw device timestamps, and device clocks differ (we measured −14 s to +27 s across GPUs of one node), so loading on a different GPU shifts every key's age. That's negligible for day-scale TTLs but matters for short ones.
    • Proposal, which keeps timestamps absolute so downtime between dump and load still counts: write them as host Unix ns using a per-device offset measured at dump time, mark that in the checkpoint meta, and convert back with the target device's offset on load. Checkpoints without the mark load as today.

Note: erased count vs table size

When a table is at capacity, the drop in table size between checkpoints combines new inserts, capacity evictions and the erase, so the erase count alone doesn't predict the next table size. That's correct behaviour, but a sentence in the docs may save users some confusion.

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

jyuan-sc and others added 4 commits November 3, 2026 14:41
…r word

erase_by_score / erase_by_score_async refused a (TIMESTAMP, LFU) table,
whose keys carry two score words. The kernel already takes num_scores and
score_index and clears every score word of an erased slot; only the Python
checks were in the way.

A (TIMESTAMP, LFU) table now qualifies for both TIMESTAMP and LFU, and the
strategy named picks the word compared: TIMESTAMP word 0 (the last-access
timestamp, an expiry whatever the frequency), LFU word 1 (the raw access
count, whatever the age). The word comes from get_physical_score_order, so
(TIMESTAMP, LFU) and (LFU, TIMESTAMP) behave the same. It is threaded from
the module (erase_score_word) through DynamicEmbStorage.erase_by_score and
_erase_state_by_score to LinearBucketTable.erase_by_score(score_word=...),
which passes it to the kernel as score_index. An LRU_LFU table must name
the word; any other multi-word layout is still refused.

The layouts check, the untracked-erase marking and the chunking are
unchanged. The docs note that an LFU-only erase also removes keys that were
just admitted, since the count never decays.

Tests (test_erase_by_score_lru_lfu.py): each word compared alone (a stale
key erased however frequent, a fresh rare key erased by LFU), both words
cleared and the reclaimed slot taken before any live key, pinned slots
spared, both tuple orders, and erase_by_score_async on a model mixing LFU
and (TIMESTAMP, LFU) tables.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
erase_by_score_async erased every table whose score strategy qualifies, at
one threshold. Tables that share a module but need their own threshold --
a TTL of a day on one and of 90 days on another -- could not be told apart,
and a timestamp-scored table with no TTL was erased too.

Add table_names: Optional[Sequence[str]] = None to the module method and to
the model-level function. None keeps today's behavior. Otherwise the scan is
narrowed to the tables named, in table order; storage and the kernel already
take a table-id list. One call per threshold; the calls on a module queue in
order, each with its own handle.

Everything is still checked before anything starts. An empty list or a bare
str, a name that is not a table of the module (on a model: of any module),
and a table the strategy does not erase are errors. On a model, each module
gets the names it holds, and a module that holds none is not started.

The new erase_table_ids does the narrowing and check_table_names the
argument check. The scan, the kernel, chunking, the gate, the
untracked-erase marking and the lru_lfu word selection are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
…evice_timestamp

erase_by_score_async(model, ..., table_names=...) raised for a name that
no module of this rank's model holds. Under TABLE_ROW_WISE a table lives
on one node's ranks only, so the same call raised on every other rank.
strict=False now skips such names: a rank that holds none of them erases
nothing and its result is (0, 0). A held name that the strategy does not
erase is still an error. strict defaults to True, so nothing changes for
existing callers. The module method keeps its check: every name must be a
table of that module.

device_timestamp, the device clock a TIMESTAMP threshold is read from, is
exported from dynamicemb; the docs and the example use it from there
instead of dynamicemb_extensions. The package now imports torch before
dynamicemb_extensions, as every submodule does: the extension needs
libc10, which torch loads, so "import dynamicemb" in a fresh process
would otherwise fail where torch/lib is not on LD_LIBRARY_PATH. The docs also say how to get counts per
table: one call per table, each table being scanned once either way.

Tests (test_erase_table_filter.py): names a rank does not hold are
skipped, a rank that holds none erases nothing, and a held name the
strategy does not erase is still refused with strict=False.
dynamicemb.device_timestamp() works in a fresh process with torch/lib off
LD_LIBRARY_PATH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
@greptile-apps

greptile-apps Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with a non-blocking correction needed in the TTL example.

Findings

  1. P2 TTL example can raise ▶

Summary

Adds score-based erase for (TIMESTAMP, LFU) tables, with either score word selectable.

  • Score-based erasing can choose either score in (TIMESTAMP, LFU) tables.
  • Callers can target named tables and use different thresholds for each.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["erase_by_score_async"] --> B["Check arguments and requested names"]
    B --> C["Select tables held by each module"]
    C --> D["Check all selected modules"]
    D --> E["Queue one erase per module"]
    E --> F["Choose timestamp or frequency word"]
    F --> G["Scan selected tables in chunks"]
    G --> H["Spare pinned keys; clear every score word of erased keys"]
    H --> I["Return combined counts"]
Loading

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

@jyuan-sc jyuan-sc changed the title [FEA](DynamicEmb) erase_by_score on (TIMESTAMP, LFU) tables, a table_names filter, and strict=False feat(dynamicemb): erase_by_score for lru_lfu tables, per named table Nov 5, 2026
jyuan-sc and others added 2 commits November 6, 2026 09:38
…e, clock-safe tests

- Rename BatchedDynamicEmbeddingTablesV2.erase_table_ids to get_erase_table_ids.
- Remove the score_name parameter of LinearBucketTable.erase_by_score: no caller
  passes it, and on a (TIMESTAMP, LFU) table it could only select the timestamp
  word; score_word names the word to compare.
- The table-filter tests and the model test of test_erase_by_score_lru_lfu.py no
  longer assume the GPU clock has run for hours. They still take their reference
  from device_timestamp(), shifted past it by the oldest age they set, so every
  timestamp they write is at or after the clock and none underflows.

Tests (H100): test_erase_async.sh 180 passed; test_erase_distributed and
test_erase_stress all cases passed; test_lru_lfu.py 36 passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
Comment thread corelib/dynamicemb/DynamicEmb_APIs.md Resolved
@shijieliu
shijieliu merged commit 4c75b48 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]>
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