Skip to content

fix(dynamicemb): deduplicate int32 keys under index dedup - #525

Closed
shijieliu wants to merge 3 commits into
NVIDIA:mainfrom
shijieliu:fix/index-dedup-int32-20261026
Closed

shijieliu wants to merge 3 commits into
NVIDIA:mainfrom
shijieliu:fix/index-dedup-int32-20261026

Conversation

@shijieliu

Copy link
Copy Markdown
Collaborator

Description

With use_index_dedup=True, a sharded DynamicEmb embedding collection fails on its first forward when its keys are int32. The user sees ValueError: Unsupported key dtype: must be int64 or uint64. The same int32 keys work without index dedup.

The index dedup (_dedup_indices) passes the keys to segmented_unique_cuda as they come, and that kernel takes int64 and uint64 keys only. Without index dedup, the lookup widens the keys to int64 first, so the kernel never sees int32 keys.

Fix: _dedup_indices widens int32 keys to int64 for segmented_unique_cuda, the same widening the lookup does, and returns the unique keys as int32. The input all-to-all and the lookup see the same dtype as without index dedup, also on a rank with no keys. int64 and uint64 keys are not copied. The docs say which key dtypes use_index_dedup takes.

Tests:

  • New int32 runs. With and without index dedup: the twin-module test (int32 DynamicEmb model against the int64 TorchRec model over 10 training iterations), the sequence forward test (the int32 lookup must equal the int64 lookup and the DEBUG initializer's values; one batch leaves rank 0 without keys) and the LFU score test (with and without a cache). With index dedup: 3 admission cases at 1 and 8 GPUs. The admission check covers both directions: no key below the threshold is in a table, and every key that reaches it is.
  • On main (d0dbc08), every int32 run with index dedup fails with the error above: 12 twin items at 1 GPU and at 8 GPUs, 2 forward runs, 4 LFU runs and 6 admission cases. The int32 runs without index dedup pass.
  • With this PR all of them pass, and so do the related suites: test_twin_module.sh 81 passed, 64 skipped (1 and 8 GPUs), test_sequence_embedding.sh 21/21, test_pooled_embedding.sh 11/11, test_lfu_scores.sh 60/60, test_embedding_admission.sh 13 passed and 76/76.
  • Measured on one 8-GPU H100 node, CI image 26.07, TorchRec 1.8.0.

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

shijieliu and others added 3 commits October 29, 2026 11:03
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]>
@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Fixes int32 keys with use_index_dedup=True by widening them for segmented_unique_cuda, then restoring their dtype before input distribution.

  • Sharded DynamicEmb lookups accept int32 keys with index dedup.

No actionable issues were found. GPU tests were not run during this review.

Reviews (1) · Last reviewed commit: "docs(dynamicemb): int32 keys under index..." · Reviewed by Greptile

@jiashuy

jiashuy commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

closed as merged in #544

@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