Skip to content

[FEA](DynamicEmb) Store a value row for every key in LinearBucketTable & DynamicEmbStorage - #515

Merged
shijieliu merged 14 commits into
NVIDIA:mainfrom
jiashuy:feat/dynamicemb-table-rows
Oct 30, 2026
Merged

shijieliu merged 14 commits into
NVIDIA:mainfrom
jiashuy:feat/dynamicemb-table-rows

Conversation

@jiashuy

@jiashuy jiashuy commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A key's value used to live at its hash slot, so the value buffer was exactly as
large as the key map and a key could never move without its value moving with it.
This PR gives every key a value row of its own, kept apart from the hash slot:

  • LinearBucketTable stores an int64 row per slot and hands rows out densely,
    per table.
  • DynamicEmbStorage uses it for every table, so a rehash carries rows instead of
    moving values.
  • NO_EVICTION, which used to smuggle a row into the score word, becomes a plain
    mode of the same mechanism, and the old score-as-row code is deleted.
  • Incremental dump and replay carry the (slot, row) pair, so a replica
    reproduces the source's placement exactly.

Why

  1. Rehash no longer moves values. An expansion used to re-insert every key and copy
    its value to the new slot. Now the key keeps its row; only the key map is
    rebuilt and the value buffer is extended in place.
  2. NO_EVICTION no longer needs a special case. It kept a row in each key's score
    word and a counter of its own, so the score could carry nothing else and every
    path that read a score had to know about it.
  3. The key map and the value buffer can have different sizes. NO_EVICTION keeps its
    key map at twice its value buffer, which only works if a slot is not a row.
  4. Rows are int64, so a table is not limited to 2^31 rows.

What changes

LinearBucketTable (scored_hashtable.py, src/table_operation/)

  • rows_: int64 per slot, -1 until a key first occupies it. row_counters_ and
    row_limits_: int64, one per table.
  • A slot owns its row for the table's lifetime. The first key to occupy a slot
    takes the next row of its table; every later occupant (the key that evicts it,
    or reuses it after an erase) inherits that row.
  • lookup / insert / _find_keys return rows; return_slots=True also returns the
    hash slots, which state keyed by slot (reference counters, score blocks) needs.
  • insert_carrying_rows / carry_rows_from: rehash re-inserts live keys with their
    rows and re-places reclaimed (erased) slots with theirs, so no row is lost and
    no value moves.
  • restore_reclaimed_slots, reserve_rows: used by replay.
  • set_row_limits / row_limits_: see "Safety net".

DynamicEmbStorage (key_value_table.py, batched_dynamicemb_function.py)

  • use_rows is on for every table. Reference counters stay keyed by hash slot; the
    value buffer is addressed by row. The HBM-direct prefetch keeps both.

NO_EVICTION

  • NO_EVICTION alone keeps no score. (NO_EVICTION, CUSTOMIZED) keeps CUSTOMIZED's
    score, and set_score works for it. Other combinations are refused.
  • The value buffer is exactly half the key map from the start, and an expansion
    keeps it so.
  • Expansion is sized by the rows a table has handed out, not by the keys it holds:
    an erased key's slot keeps its row, so rows can outnumber keys.
  • max_load_factor stays ignored, and expansion is not capped by max_capacity.

Incremental dump / replay (incremental_dump.py, batched_dynamicemb_tables.py)

  • For a table that stores rows, slot_index is an [N, 2] int64 tensor (key slot,
    value row); meta["erased_slot_index"] is a list of [M, 2] tensors, one per
    source rank. meta["num_rows"] is carried per source rank too. HybridStorage
    keeps its 1-D slot_index (tier bit + slot).
  • Replay order: erase by key -> restore reclaimed slots -> upserts, so a replica
    ends with the same keys at the same slots owning the same rows.
  • An erase must use EvictedItemMode.RETAIN_KEY; erase_untracked is tracked per
    logical table and combined across ranks into one boolean, otherwise the delta
    is refused.
  • Capacity / row_capacity compatibility is checked before replay; a source rehash
    ends the delta chain, so the target has to be rebuilt from a full checkpoint.
  • incremental_dump skips NO_EVICTION tables with a warning; replay_increment
    refuses them.

Safety net

  • Allocating a row at or past the size of the table's value buffer is asserted on
    the device (table_resolve_rows: row < row_limits[table]), with no host sync.
    Limits are set at creation and after every expansion.

Bug fixes

  • Checkpoint load undercounted the keys it expands for, so a NO_EVICTION table
    could overrun its buffer (1500 keys into a 1024-row table did not expand). It is
    now sized by the keys this rank owns (_count_owned_keys).
  • Row indices of a dumped table lived on CPU and caused an illegal memory access;
    they are moved to the device as int64 in the load/store helpers.
  • HBM-direct prefetch mixed up hash slots and value rows; they are kept apart.
  • restore_reclaimed_slots handles all three states of a slot (empty / reclaimed /
    still holding a live key) and asserts that a reclaimed slot already owns its row.
  • erase_untracked is per logical table and group-wide, not per rank.
  • The deterministic rehash insert refuses a multi-word score table.
  • Row kernels call ::atomicAdd for the 64-bit counters (dyn_emb has its own
    atomicAdd overloads that hide the CUDA one).

Limitations

  • Only DynamicEmbStorage stores rows. HybridStorage, the cache tier (overflow
    table) and the inference table (InferenceLinearBucketTable) still use the hash
    slot as the value row; use_rows is rejected for a cache / enable_overflow table.
  • Modes that cannot be combined:
    • NO_EVICTION with HybridStorage or with enable_overflow (cache).
    • NO_EVICTION with any score strategy except CUSTOMIZED.
    • NO_EVICTION tables with incremental_dump (skipped) / replay_increment
      (refused).
    • A row-storing table whose erase is not RETAIN_KEY: the delta is refused.
    • Replay across a source/target capacity mismatch (e.g. after a source rehash).
  • HybridStorage dump/replay and NO_EVICTION on HybridStorage are follow-up work.
  • The dump format of a row-storing table changed (slot_index and erased_slot_index
    are [N, 2]); checkpoint contents are unchanged.

Tests

New or reworked tests:

  • test_table_rows.py: dense and stable rows, per-table rows, eviction reusing the
    victim's row, an erased slot keeping its row, rehash carrying rows and reclaimed
    slots, restoring reclaimed slots (including a row beyond 2^32), the row-limit
    assertion (in a subprocess, since it kills the CUDA context), and its default.
  • test_no_eviction_row_indexing.py: resident keys re-read correctly; scores for
    NO_EVICTION alone / with CUSTOMIZED; key map exactly twice the value buffer
    through an expansion; unaligned init_capacity; expansion following rows rather
    than keys; erase-and-reinsert never outgrowing the buffer; checkpoint load
    expands for every key in it.
  • incremental_dump/test_replay_increment.py and test_distributed_dynamicemb.py:
    round trips with erased keys, add/erase/add in one window, untracked erases
    (including the group-wide flag on 8 ranks), NO_EVICTION tables skipped / refused,
    [N, 2] slot_index and erased_slot_index.

Test status (commit fccdcf5, fresh git reset --hard + rebuild on two EOS nodes):

  • fwd_bwd: 22 / 22 files pass, 0 failed sub-commands (47 min).
  • load_dump: 5 / 5 files pass, 0 failed sub-commands (1 h 45 min).
  • Not covered: a real 1-rank -> N-rank resharding load, performance, the Hybrid
    load path.
  • test_row_limit_is_asserted_on_the_device triggers a GPU Xid 43 on purpose (the
    device assert kills that subprocess's CUDA context); the test passes.

Suggested reviewing order

  1. scored_hashtable.py: LinearBucketTable rows (rows_, _rows_of, insert,
    insert_carrying_rows, carry_rows_from, set_row_limits).
  2. src/table_operation/table_rows.cu: row allocation, the limit assertion, the
    rehash helpers and the restore kernel.
  3. key_value_table.py: create_table_state, _expand_tables_impl, get_expand_info,
    DynamicEmbStorage.load, _encode_slot_index, the replay write path.
  4. batched_dynamicemb_tables.py / incremental_dump.py: [N, 2] slot_index, per-rank
    metadata, NO_EVICTION skip/refusal.
  5. dynamicemb_config.py: NO_EVICTION as a mode (get_score_part, has_no_eviction).

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 Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The changes since the previous review appear safe to merge.

Summary

The PR separates value rows from hash slots, so growth can rebuild the key map without moving embeddings or optimizer state.

  • Dynamic embedding tables keep values in stable rows as their key maps grow.
  • NO_EVICTION uses value rows instead of storing row numbers in scores.
  • Incremental dumps preserve key slots and value rows for replay.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Key lookup] --> B[Hash slot]
  B --> C[Stored value row]
  C --> D[Embedding and optimizer state]
  B --> E[Scores and reference counters]
  F[Growth] --> G[Rebuild key map]
  G --> C
Loading

Reviews (10) · Last reviewed commit: "[FIX](DynamicEmb) Call the global 64-bit..." · Reviewed by Greptile

@jiashuy jiashuy changed the title [FEA](DynamicEmb) Store a value row for every key in LinearBucketTable [FEA](DynamicEmb) Store a value row for every key in LinearBucketTable & DynamicEmbStorage Oct 28, 2026
@jiashuy

jiashuy commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

All unit tests passed. Next, I will refactor the implementation of the NO_EVICTION policy.

jiashuy and others added 8 commits October 29, 2026 14:08
Until now a key's value row was its hash slot, so a rehash had to move every
value, and NO_EVICTION kept the row in the score word at the cost of the score.
A table built with use_rows now stores a row for each key in its own per-slot
int32 `rows` column and reports that row as the key's index.

A row is allocated from an auto-incrementing per-table counter the first time a
key occupies a slot, and then stays with the slot: the key that evicts the
occupant, or reuses the slot after an erase, inherits it. Rows therefore stay
dense, and a rehash can carry them along instead of moving values.

- table_resolve_rows kernel maps slots to rows (allocating for a slot that has
  none); negative entries pass through, while overflow slots and a missing row
  without `allocate` are asserted.
- LinearBucketTable(use_rows): lookup/insert/insert_and_evict/export report
  rows; erase keeps reporting presence; reset and copy_table_from carry rows.
- Rehash support: insert_carrying_rows re-inserts a key with its row, and
  carry_rows_from re-places the rows of reclaimed (erased) slots into free
  slots of the new table with two small kernels that allocate only O(reclaimed).
- Cache tables keep slot == row (their reference counters are keyed by slot),
  so use_rows is not supported together with overflow.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
DynamicEmbStorage (except NO_EVICTION) now builds its key map with use_rows, so
a key keeps its value row when the table is rehashed: the expansion carries rows
along (insert_carrying_rows, plus carry_rows_from for the rows of reclaimed
slots) instead of moving every value.

Things keyed by slot rather than by row are fixed to use slots: the multi-word
score blocks in export and checkpoint load (LinearBucketTable.slots_of), and
the packed slot_index of an incremental dump, which now carries slot and row
for such a table.

Incremental dump and replay keep the rows of the source:
- meta["num_rows"] is the source's row counter, which the replica adopts.
- An erase that is retained records the packed slot/row it left reclaimed
  (meta["erased_slot_index"]). Replay removes the erased keys, then restores
  those reclaimed slots (restore_reclaimed_slots), then writes the upserts, so
  add A / erase A / add B ends with B at A's slot and row, and a slot nobody took
  stays reclaimed with its row on a replica that never held A.
- An erase without RETAIN_KEY is allowed but marks the table
  (erase_untracked); the dump reports it and replay_increment refuses such a
  delta before writing anything.

Tests: replay round trip with an erased tail, add/erase/add within one window,
a reclaimed slot for a key the replica never held, an unretained erase, and
restore_reclaimed_slots on its own.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ect prefetch

DynamicEmbStorage stores a value row per key, so lookup and insert report rows,
not hash slots. _prefetch_hbm_direct_path still passed those results straight to
increment_counter and saved them as slot_indices for the backward pass. The
reference counters are keyed by hash slot, so a forward protected unrelated
slots and left the rows it was using open to eviction: an overlapping forward
could replace a row before the backward had updated it.

LinearBucketTable.lookup and insert can now also return the hash slots
(return_slots), and _find_keys passes that on. The prefetch path counts and saves
slots for the counters, and keeps the value rows separately for addressing the
value buffer, which is how it already treated NO_EVICTION. A table whose row is
its slot is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
_expand_tables_impl rebuilt the key map without passing use_rows, so the
replacement defaulted to False for a DynamicEmbStorage. The expansion then took
the slot-addressed path and read each key's value at its hash slot, while the
values are stored at the key's row: existing keys got other keys' embeddings and
optimizer state, and the new map lost its rows. Pass the old map's use_rows to
the replacement, so rows are carried and values stay where they are.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…c rehash insert

insert_carrying_rows went into _deterministic_insert without the num_scores_ == 1
check that every other deterministic entry point makes. That helper reads and
writes one score word per key, so on a two-word LRU_LFU table a rehash under
DEMB_DETERMINISM_MODE touched the wrong bucket and score positions. Add the same
assertion.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…reclaimed slot

restore_reclaimed_slots assumed every slot a delta reports as reclaimed is empty
or already reclaimed on the replica. That fails when the source evicts A with B
and erases B before the next dump: the delta holds neither (no live key took the
slot), only B's erase, so the replica, which never held B, still holds A in the
slot and nothing overwrites it.

The kernel now handles the three states a slot can be in, in one pass:
- empty: claimed, reclaimed, and given the row;
- reclaimed: must already own that row (asserted, not overwritten, so a diverged
  replica is not hidden);
- a live key: erased the way the erase kernel does (score words cleared, digest
  emptied, bucket_sizes decremented), then the slot owns the row.
A locked slot, or a lost race for one, means something else is writing to the
table and is asserted.

It is rewritten on the Table/Bucket accessors, like the other slot-level replay
write, instead of the raw key column, so it can touch the digest, scores and
bucket count; the Python side is a single call.

Tests: restore on its own after a real erase, and a replay round trip where the
source evicts a key for one it then erases, comparing the replica's table with
the source's.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The per-key columns of an incremental dump are gathered across the ranks and so
reach every receiver, but num_rows and erased_slot_index stayed local to the rank
that produced the delta. A gathered delta handed to every replica rank (say,
written by rank 0 only) then had each receiver apply another rank's row counter
and reclaimed slots: replay could assert on slots it does not hold, or leave the
counter below rows already restored, so a later insert reused an occupied row.

Both fields are now lists with one entry per source rank, in rank order
(_all_gather_by_rank, which unlike the merging evicted-key gather keeps each
rank's part apart). Replay picks the receiver's own entry before it writes
anything (_for_this_rank): its shard index for a gathered delta, the only entry
for one that was not gathered, and anything else is rejected. They stay in
meta[i], per table, like slot_index.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
erase_untracked was one boolean on the table state, which covers every logical
table of a storage. An erase without RETAIN_KEY on table A set it, and the dump
then reported it for table B as well, so a delta holding only B was refused by
replay for good. Keep the set of logical tables that had such an erase, mark
only the one erased from, and read that table's entry when dumping.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@jiashuy
jiashuy force-pushed the feat/dynamicemb-table-rows branch from 0c60b51 to 6cad828 Compare October 29, 2026 08:54
NO_EVICTION used to keep a value row in each key's score word, with its own
counter, so the score could not carry anything else and every path that read a
score had to know that. Now it is a mode of a table that, like every other
storage, keeps a row per key in the table's rows column.

- NO_EVICTION alone keeps no score (a constant word that is never written);
  (NO_EVICTION, CUSTOMIZED) keeps CUSTOMIZED's. Other combinations are refused.
- The score-as-row machinery is gone: no_eviction_assign_scores and its kernel,
  the next-index counter, and the helpers that decoded rows from scores.
- The value buffer is exactly half the key map from the start (not
  init_capacity, which need not be a whole number of buckets), and an
  expansion keeps it so.
- A NO_EVICTION table is sized for expansion by the rows it has handed out,
  not by the keys it holds: an erased key's slot keeps its row, so rows can
  outnumber keys.
- Allocating a row at or past the value buffer's size is asserted on the
  device (row_limits), kept in step with the buffer through every expansion.
- Loading a checkpoint sizes a table by the keys in the file. It passed only
  the part that overran the value buffer, which undercounted, and a table
  could be left without the rows to hold them.
- incremental_dump and replay_increment refuse tables whose strategy contains
  NO_EVICTION: they have no timestamp to take a delta by, and their erased
  keys are the user's to manage.
- HybridStorage still refuses NO_EVICTION.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
jiashuy and others added 2 commits October 29, 2026 20:15
- incremental_dump: erase_untracked is the dump group's, not the dumping
  rank's. A delta gathers every rank's keys and slots and is handed whole to
  every receiver, so one rank's untracked erase has to taint it whichever
  rank's copy is shipped; the other per-rank fields were already gathered.
- set_score accepts (NO_EVICTION, CUSTOMIZED) in either order: the check
  compared the whole strategy with CUSTOMIZED, so a user score could never be
  set on the table the strategy exists for.
- Loading a checkpoint is sized by the keys this rank keeps, counted from the
  key file, not by every key in it. A checkpoint can hold many ranks' keys and
  each rank keeps its own share; a NO_EVICTION table has no max_capacity to
  stop it growing for the rest.
- replay_increment documents that a source expansion (rehash) ends the delta
  chain: a target's capacity does not change by itself, so replay rejects the
  deltas of a source whose capacity no longer matches.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… docs

- incremental_dump skips a table whose score_strategy contains NO_EVICTION
  with a warning instead of raising. Raising midway through a model dumped
  with an integer threshold lost what the modules before it had already
  drained (retained erased and evicted keys); skipping happens before
  anything is drained. replay_increment still refuses such a table.
- DynamicEmb_APIs.md: describe (NO_EVICTION, CUSTOMIZED) and set_score for it,
  NO_EVICTION's value buffer being half its key map and its expansion by rows
  handed out, loads sized by the keys a rank keeps, the new incremental_dump
  meta (packed slot_index, num_rows, erased_slot_index, erase_untracked), and
  what replay_increment now requires (retained erases, the source growing ends
  the delta chain, no NO_EVICTION tables). Drop what no longer holds.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@jiashuy

jiashuy commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

All unit tests passed. Ready to merge.

A child process is spawned and asserted to throw an error in test_row_limit_is_asserted_on_the_device ; receiving an "EOS" error notification in this case is the expected behavior:
Xid errors were recorded during execution of Job 6396848 on eos0297.eos.clusters.nvidia.com, check your results. XidList: 43

@jiashuy

jiashuy commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

Glance

Features

  • Every key gets its own value row, separate from its hash slot (was: row == slot).
    LinearBucketTable keeps an int32 row per slot(Optional); a slot owns its row for life.
  • DynamicEmbStorage uses rows for all tables; rehash carries rows (and reclaimed
    slots), so values never move on expansion.
  • NO_EVICTION becomes a mode of the same mechanism; old score-as-row code removed.
    NO_EVICTION alone keeps no score; (NO_EVICTION, CUSTOMIZED) keeps CUSTOMIZED's.
    Key map = 2x value buffer; expansion follows rows handed out.
  • Incremental dump/replay carries (slot, row) per source rank and reproduces the
    source's placement; erase needs RETAIN_KEY; a source rehash ends the delta chain.
  • Device-side assert row < row_limits[table] guards the value buffer.

Bug fixes

  • Checkpoint load undercounted keys when expanding, so a NO_EVICTION table could
    overrun its buffer; now sized by the keys this rank owns.
  • CPU row indices after dump caused illegal memory access; now moved to device int64.
  • HBM-direct prefetch mixed up slots and rows; now kept apart.
  • restore_reclaimed_slots now handles empty / reclaimed / live slots.
  • erase_untracked is tracked per logical table and combined across ranks.
  • incremental_dump skips NO_EVICTION tables (replay refuses them); the deterministic
    rehash insert refuses multi-word score tables.

Limitations

  • Only DynamicEmbStorage stores rows. HybridStorage, the cache tier (overflow
    table) and the inference table (InferenceLinearBucketTable) still use the hash
    slot as the value row; use_rows is rejected for a cache / enable_overflow table.
  • Modes that cannot be combined:
    • NO_EVICTION with HybridStorage or with enable_overflow (cache): refused.
    • NO_EVICTION with any score strategy except CUSTOMIZED: refused.
    • NO_EVICTION tables with incremental_dump (skipped) / replay_increment
      (refused).
    • Row-storing tables with an erase that is not RETAIN_KEY: the delta cannot be
      replayed and is refused.
    • Replay across a source/target capacity mismatch (e.g. after a source
      rehash): refused.
  • HybridStorage dump/replay and NO_EVICTION on HybridStorage are follow-up work.

Docs / tests

  • DynamicEmb_APIs.md updated; new test_table_rows.py plus reworked NO_EVICTION,
    replay, distributed, LRU/LFU tests.
  • On db23de6: fwd_bwd 22/22, load_dump 5/5 pass. Not covered: real 1->N rank
    resharding load, performance, Hybrid load path. One test triggers GPU Xid 43 on
    purpose (device assert).

@jiashuy
jiashuy requested a review from shijieliu October 30, 2026 01:03
Switch the per-slot value row, the per-table row counters and the row
limits from int32 to int64, and the row kernels with them. A packed
`slot << 32 | row` cannot hold an int64 row, so the dump's slot_index of
a table that stores rows is now an [N, 2] int64 tensor (key slot, value
row), and so is the erased_slot_index. HybridStorage keeps its 1-D
slot_index.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
jiashuy and others added 2 commits October 30, 2026 07:51
A table that stores rows now reports each erased slot as a (slot, row)
pair, so one erased slot is a [1, 2] tensor and numel() is 2.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
dyn_emb declares its own atomicAdd overloads, which hide the CUDA one for
unsigned long long, so the int64 row counters did not compile.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@jiashuy

jiashuy commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator Author

unit tests passed

@shijieliu
shijieliu merged commit d5470b8 into NVIDIA:main Oct 30, 2026
1 check passed
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