Skip to content

feat(dynamicemb): hash_bucket dist_type with a fan-out-invariant global bucket layout - #533

Closed
tiankongdeguiji wants to merge 4 commits into
NVIDIA:mainfrom
tiankongdeguiji:feat/global-bucket-layout
Closed

tiankongdeguiji wants to merge 4 commits into
NVIDIA:mainfrom
tiankongdeguiji:feat/global-bucket-layout

Conversation

@tiankongdeguiji

@tiankongdeguiji tiankongdeguiji commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A key's home bucket in a DynamicEmb table is hash % shard_capacity // bucket_capacity, so the set of keys sharing a bucket depends on the number of shards. Reloading a checkpoint at another world size re-deals them: buckets that were full at 8 ranks overflow at 1, and insert evicts the surplus (or drops the key as Busy). On a real 8-rank checkpoint folded onto 1 GPU this lost 1.3–2.0 % of the rows of every in-memory table (2.3 % synthetic for CACHING at bucket_capacity=128), while external storage restored exactly.

Change

A new dist_type="hash_bucket". The existing roundrobin, hash_roundrobin and continuous rules and layouts are untouched (bit-identical kernels, routing, checkpoints and tests); this PR is purely additive.

Under hash_bucket the bucket is global and the shard is derived from it:

G = ceil(N / bc);  C_g = G·bc;  P = ceil(G / S)
h = murmur3(key) & INT64_MAX
g = (h % C_g) // bc           # global bucket — independent of S
shard = g // P                # contiguous bucket range per shard
local = g % P                 # bucket inside the shard's table

Per-shard capacity is unchanged (ceil(ceil(N/bc)/S) == ceil(ceil(N/S)/bc)), so memory and max_capacity stay the same; only the sum over shards may exceed G by up to S-1 unused buckets.

  • Kernels: LinearBucketTable::home_bucket (types.cuh) replaces the six duplicated bucket formulas (lookup / insert / insert_and_evict / erase / scatter_keys_at_slots / bucketize); a per-table global_capacities pointer on the table (0 = own capacity → old formula). EvictParams gains the pointer (appended; cubin + fatbins rebuilt together).
  • Routing twins: DistType::kHashedBucket → hashed_shard in the bucketize kernel, and key_ownership.owning_shard / OwnershipRule apply the same rule from per-feature (global_capacity, shard_capacity), carried by the distributors next to the dist-type codes and validated once on the host. A hash_bucket feature without them is refused, not routed by another modulus.
  • Options / planner: DynamicEmbTableOptions.global_capacity (get_global_table_capacity), set by the planner for hash_bucket tables (fanout * max_capacity under MAX_BUCKET_CAPACITY, which has no whole-table form); init_capacity is raised to max_capacity (no rehash). Fused tables keep their own.
  • Checkpoints: meta records dist_type, global_capacity, bucket_capacity. A matching layout reads only the rank's own shard file; another dist_type (an error before, now a warning; continuous stays an error — its keys are shard-local ids) or another layout reads every file, re-filters by the table's rule and warns once per table — the migration path from roundrobin / hash_roundrobin checkpoints (one-time lossy fold, lossless ever after). replay_increment checks global_capacity too.
  • Scope: lossless for DynamicEmbStorage (HBM-only / host-only) and the CACHING storage tier, any S → S' with the same N and bc, RW ↔ TRW included. HYBRID is out of scope (a key's tier is decided by eviction history, so its two tiers are not a fixed partition of the keys; its layout is unchanged), as are NO_EVICTION and MAX_BUCKET_CAPACITY tables.

Docs: docs/global_bucket_layout_design.md, DynamicEmb_APIs.md, README.md.

Tests

  • New test/unit_tests/test_global_bucket_layout.py (31 tests): table kernels vs an independent oracle (and old buckets kept when unset); routing kernel / host rule / oracle agree key for key for S ∈ {1,2,3,8}; planner derivation; 8→1, 1→2, 2→1, 8→3 reloads of a 97 %-full table lose 0 rows (HBM-only and CACHING), random-key training survivors reload losslessly, the roundrobin control loses 1.5 %; roundrobin and hash_roundrobin checkpoints migrate into a hash_bucket table with a warning; a different recorded layout is read whole.
  • Updated: test_hash_roundrobin_kuairand.py (kernel vs a second CPU implementation, per key, for all three hash rules), test_embedding_dump_load.sh (+ hash_bucket at 1/2 ranks and 2→1 / 1→2; hash_roundrobin cases unchanged), test_table_row_wise.sh (+ hash_bucket parity/jagged/grad and RW↔TRW dump/load), test_replay_increment.py (+ global_capacity mismatch).
  • Passed here (4×A10): table_operation, ref counters, planner, expansion, replay, no_eviction, the original bucketize/int64 tests, alignment at 1 and 4 GPUs, the table-row-wise suite (2 and 4 GPUs), test_embedding_dump_load.py for hash_bucket (1/2 ranks, cross-world) and for unchanged roundrobin / hash_roundrobin. 8-GPU shell variants not run.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JoQS7n2bkEWZws9Rddef49

@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects DynamicEmb table placement, routing, checkpoint loading, and replay.

Fix the first-table buffer placement before merging; it can allocate values on host memory that the planner did not reserve.

Summary

Adds hash_bucket routing and a global bucket layout so supported tables can reload checkpoints at another world size without regrouping keys into overflowing buckets.

  • Hash-bucket tables keep each key’s bucket stable across rank counts.
  • Checkpoint loading follows the recorded bucket layout and re-partitions files when needed.
  • Incremental replay checks the whole-table capacity before writing saved slots.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Sum fused tables' sizes and HBM budgets] --> B{Values fit HBM budget?}
    B -->|Yes| C[Select HBM_ONLY]
    C --> D[Pass original options to create_table_state]
    D --> E{First table's budget is zero?}
    E -->|Yes| F[Allocate every value buffer on host]
    E -->|No| G[Allocate value buffers on GPU]
Loading

Reviews (5) · Last reviewed commit: "revert(dynamicemb): back out the per-tab..." · Reviewed by Greptile

@tiankongdeguiji
tiankongdeguiji force-pushed the feat/global-bucket-layout branch from d51dd5d to 0b66c74 Compare November 3, 2026 09:34
…al bucket layout

A key's home bucket is hash % shard_capacity // bc, so the keys that share a
bucket depend on the number of shards: a checkpoint reloaded at another
world size re-deals them, full buckets overflow and insert evicts the
surplus (1-2 % of the rows of a near-full table on an 8 -> 1 restore).

Add dist_type="hash_bucket": the bucket is the global one,
(murmur3(key) & INT64_MAX) % global_capacity // bc with
global_capacity = ceil(num_embeddings / bc) * bc, and the shard is that
bucket's position, g // ceil(G / S). Per-shard capacity is unchanged
(ceil(ceil(N/bc)/S) == ceil(ceil(N/S)/bc)); only the sum over shards may
exceed the global count by up to S-1 unused buckets. roundrobin,
hash_roundrobin and continuous keep their rules and layouts bit for bit.

- LinearBucketTable::home_bucket (types.cuh) replaces the six duplicated
  bucket formulas; a per-table global capacity pointer on the table, 0 = own
  capacity. The LruLfu cubin ABI gains the pointer (appended).
- DistType::kHashedBucket in the routing kernel (hashed_shard) and
  key_ownership.owning_shard / OwnershipRule apply the rule from per-feature
  (global_capacity, shard_capacity), carried by the distributors next to the
  dist-type codes and validated once on the host.
- DynamicEmbTableOptions.global_capacity, set by the planner for hash_bucket
  tables (fanout * max_capacity under MAX_BUCKET_CAPACITY, which has no
  whole-table form); init_capacity is raised to max_capacity.
- Checkpoint meta records global_capacity / bucket_capacity; a matching
  layout reads only the rank's own shard file, another dist_type (no longer
  an error) or another layout reads every file, re-filters and warns once.
  replay_increment checks global_capacity too.
- HYBRID stays lossy (phase 2 designed in the doc), as do NO_EVICTION,
  MAX_BUCKET_CAPACITY, roundrobin and hash_roundrobin tables.

Tests: test_global_bucket_layout.py (kernel / routing / host oracle,
planner, 8->1, 1->2, 2->1, 8->3 reloads at 97 % fill lose nothing for
HBM-only and CACHING tables, roundrobin control loses 1.5 %, roundrobin and
hash_roundrobin checkpoints migrate with a warning); kuairand kernel oracle
covers all three hash rules; hash_bucket cases in test_embedding_dump_load.sh
(incl. 2->1 / 1->2) and test_table_row_wise.sh; replay global_capacity
mismatch.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JoQS7n2bkEWZws9Rddef49
@tiankongdeguiji
tiankongdeguiji force-pushed the feat/global-bucket-layout branch from 0b66c74 to 06a8e18 Compare November 3, 2026 09:35
@tiankongdeguiji tiankongdeguiji changed the title feat(dynamicemb): fan-out-invariant global bucket layout for hash_roundrobin feat(dynamicemb): hash_bucket dist_type with a fan-out-invariant global bucket layout Nov 3, 2026
…yout

Conflicts (both sides added a keyword argument at the same spot) resolved by
keeping both: `global_capacities` / `global_bucket_layout` from this branch
beside `use_ref_counter` from NVIDIA#532 in create_table_state, get_scored_table,
LinearBucketTable.__init__, _expand_tables_impl and the HybridStorage tiers;
the `table_insert*_single_score` helpers take NVIDIA#532's optional counter and
this branch's global capacity pointer.

Rebuilt and re-ran on the merged tree: test_global_bucket_layout (31),
kuairand (17), replay (36), planner, no_eviction (24), table_rows, expansion,
ref counters, table_operation, the erase_async suite (143) incl. the
distributed case, and multi-GPU hash_bucket / hash_roundrobin / roundrobin
dump-load.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JoQS7n2bkEWZws9Rddef49
- lookup_torch_binding.cu: the forward declaration repeated the defaults
  table.cuh already gives (redefinition of default argument), which broke the
  CMake inference_emb_ops build; drop them from the declaration.
- lookup_meta: _table_lookup_fake takes table_global_capacities, matching the
  op schema the dispatcher hands fake kernels.
- Routing kernel (kHashedBucket): sign-extend the key before hashing. An int32
  key arrived as the kernel's unsigned index and was zero-extended, while the
  table and owning_shard hash the int64 key -- half of the negative int32 keys
  were routed to a rank other than the one a reload would keep them on.
- Load across dist_type: refuse when either side is `continuous`
  (check_dist_type_migration) -- its keys are shard-local ids that no other
  rule can re-partition; keep the warning path for roundrobin/hash_roundrobin.
- Layout warnings are said once per table (shard 0 in the module path, once
  per process for a direct Storage.load), not once per shard file per rank
  (suppress_layout_warnings / _warn_layout_once).
- Design doc: drop the lossless-HYBRID follow-up section; HYBRID is out of
  scope.

Tests: routing twins agree for int32 keys with negatives (8 cases);
continuous checkpoints are refused on both paths; exactly one layout warning
per table on the module path and on a direct storage load; CPU test of the
fake kernel's signature.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JoQS7n2bkEWZws9Rddef49
… (PR NVIDIA#534)

b9ca916 unintentionally carried the runtime half of the per-table HBM
budget change: those edits were sitting uncommitted in this tree while
the hash_bucket review findings were being addressed, and a commit takes
whole files. The change is unrelated to hash_bucket and is under review
as NVIDIA#534, which also carries the planner-side
mirror it needs to stay consistent.

This restores _create_cache_storage's shared cap_scale split,
create_table_state's options[0] host placement, and the
DynamicEmb_APIs.md row, exactly as they were before b9ca916.

Co-Authored-By: Claude Code <[email protected]>
@greptile-apps

greptile-apps Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P1 GPU values land on host corelib/dynamicemb/dynamicemb/key_value_table.py:673 ▶

    create_table_state now puts every value buffer on the host when the first table has local_hbm_for_values=0, even when other fused tables provide enough HBM for the group. _create_cache_storage pools their budgets but passes the first table's zero through unchanged.

    For example, two 1 GiB tables with budgets of 0 and 2 GiB select HBM_ONLY, yet allocate all 2 GiB on the host. The planner reserves no host space for that group, so an accepted plan can exhaust host memory. Choose the buffer location from the group's selected tier, not the first table's budget.

@shijieliu

Copy link
Copy Markdown
Collaborator

merged in #566

@shijieliu shijieliu closed this Nov 19, 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