Skip to content

[FIX](DynamicEmb) Treat int32 keys as the int64 keys a table stores, under every dist_type and index dedup - #544

Merged
shijieliu merged 6 commits into
NVIDIA:mainfrom
jiashuy:cherry-pick-pr524-pr525
Nov 11, 2026
Merged

shijieliu merged 6 commits into
NVIDIA:mainfrom
jiashuy:cherry-pick-pr524-pr525

Conversation

@jiashuy

@jiashuy jiashuy commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Summary

A DynamicEmb table stores a key widened to int64 with its sign, and a
checkpoint or an incremental-dump delta holds that int64 key. An int32 key from
a KJT is therefore the same key as the int64 key with the same value, and must
get the same rank, the same row and the same dedup result. Three places did not
treat it that way. This PR fixes them and pins the invariant with tests.

This branch carries the commits of #524 and #525, rebased onto the current
main, plus one commit that closes a gap left between them.

Changes

  1. Bucketize by the stored key (fix(dynamicemb): bucketize int32 keys by the int64 key a table stores #524)
    block_bucketize_sparse_features read an int32 key in 32 bits for the
    roundrobin and hash_roundrobin rules. A negative key was another number,
    and a hash cut to 32 bits left another remainder at any world size that is
    not a power of two, so an int32 key went to a different rank than the int64
    key it equals (the one a checkpoint load or a delta replay puts it on).
    Both rules now widen the key with its sign, then read it as unsigned
    (cyclic_bucket in sparse_block_bucketize_features.cu), the same way
    dynamicemb/key_ownership.py does.

  2. Index dedup for int32 keys (fix(dynamicemb): deduplicate int32 keys under index dedup #525)
    segmented_unique_cuda takes 64-bit keys. Under index dedup an int32 indices
    tensor is now deduplicated as int64, the keys the tables store, and the unique
    keys are returned in the input dtype, so the input dist sees the same dtype
    as without dedup and as on a rank whose input is empty.

  3. The continuous (kBlock) rule reads the stored key as well (new)
    The two commits above left continuous reading an int32 key in 32 bits, so a
    negative int32 key still went to another rank than its int64 twin under that
    rule alone. block_bucket() now does the arithmetic in 64 bits on the stored
    key (kernel1 and kernel2 share it). The rewritten key is still written in the
    input's dtype, which keeps the low bits of the 64-bit result, so an int32
    key stays an int32.
    int64 keys and non-negative int32 keys are unchanged. Only a negative int32
    key under continuous goes to a different rank than before.

Comments in key_ownership.py and utils.h now say that every rule, continuous
included, reads the stored key. DynamicEmb_APIs.md and
docs/replay_increment_design.md describe the roundrobin / hash_roundrobin
reading and int32 under index dedup; they do not cover continuous, which
key_ownership does not support.

Tests

  • test_hash_roundrobin_kuairand.py
    • test_bucketize_kernel_matches_key_ownership: the kernel's rank equals
      key_ownership.owning_shard for int32 and int64 keys, at world sizes
      1..255, on keys next to 0, 231, 232, 2**63 and random keys.
    • test_bucketize_kernel_continuous_reads_stored_key: replaces
      ..._continuous_keeps_key_width, which pinned the 32-bit reading. Checks
      rank and rewritten key against a CPU reference written apart from the kernel.
    • test_bucketize_kernel_int32_goes_where_int64_goes (new): under
      continuous, roundrobin and hash_roundrobin, an int32 key and the
      int64 key with the same value get the same rank.
  • int32-key variants of existing end-to-end tests: test_embedding_dump_load
    (negative keys, except -4..-1, the hash table's reserved keys once widened),
    test_embedding_admission, test_lfu_scores (with and without index dedup
    and a cache; the score must equal the true access count),
    test_sequence_embedding_fw (the same keys as int32 must look up the same
    embeddings as int64) and test_twin_module (a sequence-int32 case on the
    sequence collection, checked against the int64 run).

Test results (EOS, 8x H100)

  • Full unit_test.sh run, fwd_bwd and load_dump groups, two nodes: all 33
    test files exit 0, no failing sub-command. This run was on the head before
    the rebase and before change 3.
  • On the final head (rebased onto main 665ffd9, with change 3):
    test_hash_roundrobin_kuairand.py 127 passed (the new tests included, all
    three dist_types) and test_sequence_embedding.sh exit 0.
  • The full unit_test.sh was not re-run on the final head.

Known limits

  • An int32 key from -4 to -1 is widened to the hash table's reserved keys. It
    was so before and nothing rejects it here; the tests avoid those four values.
  • The block_bucketize_pos path (explicit block boundaries) of the block
    bucketizer still reads a key in its own width. Nothing in DynamicEmb uses it.
  • A table whose index_type is int32 is not supported by the table kernels
    (DISPATCH_KEY_TYPE covers int64/uint64 only), so "a table stores an int64
    key" holds for every supported configuration.
  • An int32 input under index dedup costs two extra copies (to int64 and back).

Related: #524, #525.

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 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge; no blocking issue was found.

Summary

This PR makes int32 embedding keys follow the stored int64 key’s routing rule and preserves the input dtype after index deduplication.

  • Int32 keys reach the same shard as their stored 64-bit keys.
  • Index dedup keeps int32 keys in the input dtype.

Reviews (3) · Last reviewed commit: "[FIX](DynamicEmb) Read keys as int64 und..." · Reviewed by Greptile

shijieliu and others added 5 commits November 11, 2026 08:51
A table stores an int32 key widened to int64 with its sign, and a
checkpoint or a delta holds that int64 key. key_ownership places each
key by it when a checkpoint is loaded or a delta is replayed. The
bucketize places the int32 key of the KJT. The two agree only if the
bucketize gives an int32 key the rank of the int64 key with the same
value. These tests check that.

- test_hash_roundrobin_kuairand.py
  - test_bucketize_kernel_matches_cpu_reference also runs with int32
    keys, and also at 3 and 7 ranks.
  - test_bucketize_kernel_matches_key_ownership (new) gives the
    bucketize the keys next to 0, 2**31, 2**32 and 2**63 and 4096
    random keys of the dtype, one key per sample, at 1, 2, 3, 5, 6, 7,
    8, 12, 16, 100 and 255 ranks, for int64 and int32 keys and both
    rules. Each key's rank has to equal key_ownership.owning_shard's for
    the key widened to int64, and the rank from this file's own
    reference. Kernel2 has to write each key, unchanged and in its own
    dtype, where kernel1 counted it.
  - test_bucketize_kernel_continuous_keeps_key_width (new): continuous
    reads a key in its own width, and its rewrite of an int32 key stays
    an int32.
  - The reference reads a key as an unsigned 64-bit number under both
    rules, as the rules do. numpy's % on a negative key gives the
    remainder of the signed value, which is another number.
- test_embedding_dump_load.py: --key-dtype int32 draws int32 keys,
  negative ones included but not -4 to -1, which are the hash table's
  reserved keys as int64. --batch-size lets 7 ranks feed equal shares,
  and a batch size that is not a multiple of the world size raises.
- test_embedding_dump_load.sh: int32 keys dumped and loaded under
  hash_roundrobin at 8 and at 7 ranks, and under roundrobin at 7 ranks.
  The loaded model has to hold every key its rank owns and return the
  rows the dumped model had.

On this commit, with the bucketize of d0dbc08 (Eos, H100):
- test_hash_roundrobin_kuairand.py: 16 failed, 78 passed. All 16 are
  int32 cases: matches_cpu_reference at 3 and 7 ranks, for both
  features, and matches_key_ownership under hash_roundrobin at 2, 3, 5,
  6, 7, 8, 12, 16, 100 and 255 ranks and under roundrobin at 7 and 100
  ranks. Under hash_roundrobin at 8 ranks, for example, 1786 of 4110
  int32 keys get another rank from the bucketize than from
  key_ownership. Every int64 case and every continuous case passes.
- The int32 dump/load cases: the three dumps pass, and the three loads
  fail on every rank with "Key <k> must exist in table of rank <r>".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
What goes wrong: with int32 KJTs, a key that a checkpoint load or a
delta replay puts on one rank is looked up on another rank, and nothing
raises. After a load the model misses those keys and returns fresh rows
for them.

Why: a table stores an int32 key widened to int64 with its sign, and a
checkpoint or a delta holds that int64 key. key_ownership places each
key by it, and that is all a loader has: nothing records that the input
is int32. The bucketize reads an int32 key in 32 bits:

- roundrobin: rank (uint32)key % W, where key_ownership gives
  (uint64)key % W. The two differ for a negative key when 2**32 and
  2**64 leave different remainders modulo W, as at W = 7 or 100.
- hash_roundrobin: the hash of the zero-extended 32-bit key, cut to its
  low 32 bits, modulo W, where key_ownership hashes the sign-extended
  key and takes the whole hash modulo W. The two differ for about
  (W - 1)/W of the negative keys at any W above 1, and for most keys
  when W is not a power of two.

The fix: both bucketize kernels take the rank of a roundrobin or
hash_roundrobin key from one helper, cyclic_bucket. It widens the key to
64 bits with its sign and works in 64-bit arithmetic, which is exactly
key_ownership's rule. Kernel1 counts the lengths per rank and kernel2
writes the keys, both with this rank. The key written stays the input
key, in its own dtype. int64 keys keep their ranks bit for bit.
continuous keeps its rule: key_ownership has none for it, and its
rewrite of an int32 key stays an int32.

The other places that compute a rank from a key agree already:
key_ownership.owning_shard and murmur3_fmix64 widen a key with its sign,
and the checkpoint load, the admission counter load, replay and the twin
module all go through owned_key_mask. The comments and docs that state
the rules say how an int32 key is read: DistType in utils.h,
key_ownership.py, DynamicEmb_APIs.md and replay_increment_design.md.

Evidence, on Eos H100, this commit against the one before it:
- test_hash_roundrobin_kuairand.py: 94 passed (before: 16 failed, all
  int32, and 78 passed).
- The int32 dump/load cases of test_embedding_dump_load.sh: 6 of 6 runs
  pass (before: the 3 loads fail on every rank).
- The same before and after: test_twin_module.sh 513 passed;
  test_sequence_embedding.sh 18 runs and test_pooled_embedding.sh 11
  runs, all passing; test_table_row_wise.sh 26 runs, all passing;
  test_incremental_dump.sh 189 passed.
- test_embedding_dump_load.sh, run with the one-GPU invocations eight at
  a time: all 228 invocations pass (before: 225, the 3 int32 loads
  fail). The other 222, all with int64 keys, pass on both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With use_index_dedup=True, ShardedDynamicEmbeddingCollection._dedup_indices
hands the KJT values to segmented_unique_cuda, which takes int64 and uint64
keys only. int32 keys raise "Unsupported key dtype: must be int64 or uint64"
on the first forward. Without index dedup they work.

Extend the index dedup tests with int32 keys:

- test_twin_module: int32 keys on the sequence collection, with and without
  index dedup. The DynamicEmb model reads int32 keys, and its outputs over
  10 training iterations must match the TorchRec model, which reads the same
  keys as int64. This checks the forward, the gradients and the table
  updates. The key dtype joins the (is_pooled, pooling_mode, max_bag_size)
  parameter, so the pooled collection, which has no index dedup, gets no
  int32 items.
- test_sequence_embedding_fw (--key_dtype int32), run by
  test_sequence_embedding.sh on 2 and 8 GPUs: each batch is also looked up
  as int32 keys. The embeddings must equal the int64 lookup's, and each
  row of a DynamicEmb table must hold the DEBUG initializer's value,
  key % 100000. That is the value the lookup returns without index dedup,
  and the check does not depend on the dedup of int64 keys, which the int64
  lookup and the twin reference also use. The last batch leaves rank 0
  without keys, so the empty branch of the dedup runs next to ranks that
  dedup int32 keys.
- test_lfu_scores (--key-dtype int32), run by test_lfu_scores.sh on 1 and 8
  GPUs: the LFU score of an int32 key must equal its true access count, with
  and without index dedup, with and without a cache.
- test_embedding_admission: cases int32-keys-{hbm-direct,hybrid,cache} on 1
  and 8 GPUs, which count admission frequency through the dedup. The cache
  case uses a threshold of 2: its keys spread over 8000 values per table,
  where a threshold of 4 admits one key of all 8 tables and 2 admits 365.
- validate_admission_keys checks both directions, for every case: each key
  in a table reached the threshold, and each key a rank owns that reached
  the threshold is in its table.

test_sequence_embedding_bw is not extended: it drives
BatchedDynamicEmbeddingTablesV2 directly, whose forward casts the keys to
index_type and never reaches _dedup_indices. Nor is
test_multi_embedding_collection_fw: each of its collections takes the same
_dedup_indices path as test_sequence_embedding_fw.

On main (d0dbc08), on one 8-GPU H100 node, every int32 run with index dedup
fails with that error, raised at the segmented_unique_cuda call in
_dedup_indices: the 12 int32 test_twin_module items with index dedup at 1
GPU and at 8 GPUs (12 failed, 69 passed, 64 skipped), both
test_sequence_embedding_fw runs (2 and 8 GPUs), the 4 LFU runs and the 6
admission cases. The int32 runs without index dedup pass: the other 12 twin
items, the 2-GPU forward run with its DEBUG value check, and the 4 LFU
runs. All 70 int64 admission cases pass the two-sided check.

Two broken versions of the next commit's fix show what the new checks
catch. Frequency counters of 1 for int32 keys fail all 6 int32 admission
cases, each with keys that reached the threshold and were not admitted.
Reverse indices shifted by one fail test_sequence_embedding_fw on the DEBUG
value check at the first batch, while its int32 and int64 lookups agree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With use_index_dedup=True, ShardedDynamicEmbeddingCollection._dedup_indices
passes the KJT values to segmented_unique_cuda as they come. The kernel
takes int64 and uint64 keys only, so int32 keys raise "Unsupported key
dtype: must be int64 or uint64" on the first forward. Without index dedup
int32 keys work, because the lookup widens them to int64
(features.values().long()).

_dedup_indices widens int32 keys to int64 for segmented_unique_cuda and
returns the unique keys in the input dtype. So:

- the dedup compares the same 64-bit keys the tables store, made by the
  same sign extension as in the lookup;
- the bucketize, the input all-to-all and the lookup see the dtype they see
  without index dedup;
- a rank with empty input, which passes its keys on unchanged, sends the
  same dtype as the ranks that dedup.

The reverse indices and the LFU and admission frequency counters do not
depend on the key dtype. int64 and uint64 keys are not copied: they reach
segmented_unique_cuda as they are, and .to() returns the same tensor.

Why not int32 support inside segmented_unique_cuda: it would add kernel
instantiations for this one caller, since the lookup's own dedup
(batched_dynamicemb_function.segmented_unique) only sees index_type keys.
Widening in Python keeps the kernel's key types the same as the tables'.
The cost is two copies, small next to the dedup and the all-to-all: the
int32 keys widened to int64, and the unique keys narrowed back to int32.

Tests, on one 8-GPU H100 node (CI image 26.07, TorchRec 1.8.0):

- The int32 runs added by the previous commit all pass: 24 test_twin_module
  items at 1 GPU and at 8 GPUs, 3 test_sequence_embedding_fw runs, 8 LFU
  runs and 6 admission cases. Without this fix those with index dedup fail
  with the error above: 12 twin items at each GPU count, 2 forward runs, 4
  LFU runs and 6 admission cases.
- Related suites with the previous commit's tests, main -> this branch:
  test_twin_module.sh 12 failed, 69 passed, 64 skipped -> 81 passed, 64
  skipped, at 1 GPU and at 8 GPUs; test_sequence_embedding.sh 19/21 ->
  21/21 runs pass; test_pooled_embedding.sh 11/11 -> 11/11;
  test_lfu_scores.sh 56/60 -> 60/60; test_embedding_admission.sh 13 passed
  and 70/76 runs -> 13 passed and 76/76 runs. On main only the int32 runs
  with index dedup fail; on this branch nothing fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Say in the DynamicEmbeddingCollectionSharder section what use_index_dedup
does and which key dtypes it takes: int32 and int64, with int32 keys
deduplicated as int64 and passed on as int32.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jiashuy
jiashuy force-pushed the cherry-pick-pr524-pr525 branch from 250a383 to c48fd0a Compare November 11, 2026 03:26
roundrobin and hash_roundrobin read a key as a table stores it: widened to
64 bits with its sign, then unsigned. continuous still read it in the
input's own width, so a negative int32 key went to another rank than the
int64 key with the same value.

The block bucketize now widens the key the same way, so an int32 key gets
the rank of the int64 key it equals under every dist_type. The rewritten key
is still written in the input's dtype, which keeps the low bits of the 64-bit
result. int64 keys and non-negative int32 keys are unchanged.

The test that pinned the old behaviour now pins the new one, and a new test
checks that int32 and int64 go to the same rank under each dist_type.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@jiashuy jiashuy changed the title Cherry pick pr524 pr525 [FIX](DynamicEmb) Treat int32 keys as the int64 keys a table stores, under every dist_type and index dedup Nov 11, 2026
@shijieliu
shijieliu merged commit 476cdc0 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]>
shijieliu added a commit to shijieliu/recsys-examples that referenced this pull request Nov 12, 2026
NVIDIA#544 has DynamicEmb's bucketize place an int32 key as the int64 key of
its value, the key a table stores and a checkpoint holds, as
key_ownership already did. The hierarchical dist's bucketize reads a key
in its own width: a negative int32 key as 2^32 + key under roundrobin,
and the low 32 bits of the hash of the zero-extended key under
hash_roundrobin. So after the merge of main an int32 key could get one
owner on the hierarchical dist and another on TorchRec's path and in a
checkpoint load or a delta replay: under hash_roundrobin most keys at a
world size that is not a power of two and most negative keys at any,
under roundrobin negative keys at world sizes such as 7 or 100.

hier_forward now gives the op int32 keys as int64, so the op places
every key by the 64-bit rule, which for int64 keys is the rule it had.
No C++ changes: the op still takes int32 keys and reads them in 32 bits,
which no call through the sharding reaches now. The widened keys are
held with the call's other inputs until the second wait.

- test_hier_bucketize.py gives the bucketize hook the keys as the
  sharding gives them to the op, int32 ones as int64, and compares it as
  before with DynamicEmb's bucketize of the int32 KJT, now NVIDIA#544's.
- The docs and docstrings that stated the 32-bit rule, or that a node's
  GPUs must give the dist keys of one dtype, say what holds now: the
  design doc's sections 4.2, 5.2, 9, 10.1 and 19, DynamicEmb_APIs.md,
  DynamicEmbTableOptions.output_dist, hier_needs and the dist test's
  bucketized_owners. With every key int64 at the op, a node's GPUs may
  give the dist keys of different dtypes; TorchRec's dists still need
  one dtype on every rank.

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.

2 participants