Skip to content

fix: align DynamicEmb capacity to bucket_capacity instead of DEMB_TABLE_ALIGN_SIZE - #329

Merged
JacoCheung merged 3 commits into
NVIDIA:mainfrom
JacoCheung:junzhang/fix_align_to_table_size
Mar 19, 2026
Merged

JacoCheung merged 3 commits into
NVIDIA:mainfrom
JacoCheung:junzhang/fix_align_to_table_size

Conversation

@JacoCheung

@JacoCheung JacoCheung commented Mar 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Fix capacity alignment bug introduced in PR Relax alignment requirements(remove pow of 2) in dynamicemb #312: align_to_table_size now aligns to bucket_capacity (default 128) instead of DEMB_TABLE_ALIGN_SIZE (16), matching the C++ hash table's internal alignment.
  • Fix _validate_configs to always set num_aligned_embedding_per_rank, preventing max_capacity from being unaligned when num_embeddings is perfectly divisible by world_size.
  • Fix expand() to use key_index_map.capacity() (actual hash table capacity) for value table allocation, preventing out-of-bounds CUDA memory access during rehash.

Root Cause

The C++ ScoredHashTable rounds capacity up to multiples of bucket_capacity (128). But align_to_table_size only rounded to multiples of 16. When ceil(num_embeddings / world_size) was already a multiple of 16, _validate_configs skipped setting num_aligned_embedding_per_rank, so max_capacity was set to the raw local_row (not a multiple of 128). During expand(), value tables were allocated with the smaller max_capacity, while the hash table could return indices up to its actual (larger, bucket-aligned) capacity — causing illegal memory access.

Test plan

  • Updated existing unit tests in test_alignment.py to reflect bucket_capacity-based alignment
  • Added new test test_align_to_table_size_bucket_capacity validating alignment with various bucket capacities
  • Run e2e training with num_embeddings=50_000_000, world_size=16 (the failing configuration) to confirm the fix

Fixes #328

Made with Cursor

CI

…LE_ALIGN_SIZE

PR NVIDIA#312 introduced `align_to_table_size` which rounds capacity up to
multiples of DEMB_TABLE_ALIGN_SIZE (16). However the C++ hash table
internally rounds capacity to multiples of `bucket_capacity` (default
128). When `num_embeddings / world_size` is already a multiple of 16
but not 128, `_validate_configs` skipped setting
`num_aligned_embedding_per_rank`, causing `max_capacity` to be
unaligned. During `expand()`, value tables were allocated with the
unaligned `max_capacity` while the hash table could return indices up
to its actual (larger) capacity, leading to out-of-bounds CUDA memory
access.

Changes:
- `align_to_table_size` now accepts an `alignment` parameter (default
  unchanged for backward compatibility).
- `_validate_configs` always sets `num_aligned_embedding_per_rank` and
  aligns to `bucket_capacity`.
- `_dyn_emb_table_size_per_rank` takes `bucket_capacity` for alignment.
- `expand()` uses `key_index_map.capacity()` for value table allocation
  to match the actual hash table capacity.
- Updated unit tests to reflect bucket_capacity-based alignment.

Fixes NVIDIA#328

Made-with: Cursor
@greptile-apps

greptile-apps Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes three related bugs in the DynamicEmb capacity-alignment pipeline, all rooted in the C++ ScoredHashTable rounding capacity up to multiples of bucket_capacity (128) while the Python side was using a coarser DEMB_TABLE_ALIGN_SIZE (16). The three-part fix — aligning the planner to bucket_capacity, always writing num_aligned_embedding_per_rank, and recording the actual C++-reported capacity after rehash — is correct for all flows that go through the DynamicEmbeddingShardingPlanner.

Key changes:

  • align_to_table_size gains an explicit alignment parameter (default kept for backwards-compat) so planners and callers can pass bucket_capacity.
  • _validate_configs unconditionally sets num_aligned_embedding_per_rank, eliminating the divide-evenly edge case that left max_capacity unaligned.
  • expand() allocates value tables and tracks self._capacity using key_index_map.capacity() (the bucket-rounded value) rather than the requested target_capacity, preventing the out-of-bounds CUDA write.
  • __post_init__ now also aligns a user-supplied max_capacity to bucket_capacity with a warning.

One regression to consider: batched_dynamicemb_compute_kernel.py's else branch (line 278) still assigns max_capacity = local_row post-construction, bypassing __post_init__. After this PR, self._capacity is always bucket-aligned while an unaligned max_capacity means the expand() early-exit guard (self._capacity == self.options.max_capacity) can never fire in the non-planner path, causing repeated pointless rehashing. See the inline comment on key_value_table.py:201 for details.

Confidence Score: 3/5

  • Safe to merge for production planner-based flows; one non-planner regression needs addressing before broader adoption.
  • The three core fixes are well-reasoned and correct for any code path that goes through DynamicEmbeddingShardingPlanner (the primary production path). The e2e test on the failing 50M/16-rank config confirms the main bug is resolved. The score is reduced because the change to self._capacity = actual_capacity in expand() introduces a regression in the non-planner path: max_capacity can still be set to an unaligned local_row post-construction in batched_dynamicemb_compute_kernel.py, causing the expand() early-exit guard to never fire and leading to repeated rehashing. This path is reachable by users who construct BatchedDynamicEmbeddingBag directly without the planner.
  • corelib/dynamicemb/dynamicemb/key_value_table.py — the expand() early-exit regression; and the corresponding unmodified corelib/dynamicemb/dynamicemb/batched_dynamicemb_compute_kernel.py line 278 which needs to align max_capacity to bucket_capacity as well.

Important Files Changed

Filename Overview
corelib/dynamicemb/dynamicemb/key_value_table.py Core fix: expand() now uses key_index_map.capacity() (actual bucket-aligned capacity) for value table allocation, fixing the OOB CUDA memory access. But the new self._capacity = actual_capacity assignment introduces a regression for the non-planner path where max_capacity can be set unaligned post-construction.
corelib/dynamicemb/dynamicemb/planner/planner.py Planner now unconditionally sets num_aligned_embedding_per_rank aligned to bucket_capacity, fixing the original divide-evenly edge case. _dyn_emb_table_size_per_rank and its callers correctly thread bucket_capacity through.
corelib/dynamicemb/dynamicemb/dynamicemb_config.py align_to_table_size now accepts an explicit alignment parameter (defaulting to DEMB_TABLE_ALIGN_SIZE for backwards compatibility). post_init now also aligns max_capacity to bucket_capacity, closing the user-facing config gap.
corelib/dynamicemb/test/unit_tests/test_alignment.py Tests updated to use bucket_capacity-based alignment throughout; new test_align_to_table_size_bucket_capacity covers the fixed alignment function. Redundant max(aligned, BUCKET_CAPACITY_NORMAL) guards remain but are harmless no-ops.

Sequence Diagram

sequenceDiagram
    participant Planner as DynamicEmbeddingShardingPlanner
    participant Config as _validate_configs
    participant Kernel as _get_dynamicemb_options_per_table
    participant Table as DynamicEmbeddingTable
    participant CPP as ScoredHashTable (C++)

    Planner->>Config: align num_emb/world_size to bucket_capacity
    Config-->>Planner: num_aligned_embedding_per_rank (bucket-aligned)
    Planner->>Kernel: pass dynamicemb_options
    Kernel->>Kernel: max_capacity = num_aligned_embedding_per_rank
    Kernel-->>Table: DynamicEmbTableOptions (max_capacity aligned)
    Table->>CPP: get_scored_table(capacity=init_capacity)
    CPP-->>Table: actual_capacity = ceil_bucket(init_capacity)
    Table->>Table: self._capacity = actual_capacity

    Note over Table,CPP: On expand() trigger
    Table->>CPP: get_scored_table(capacity=min(max_capacity, 2*_capacity))
    CPP-->>Table: actual_capacity = ceil_bucket(target)
    Table->>Table: allocate value tables sized actual_capacity
    Table->>Table: self._capacity = actual_capacity
    Note over Table: Guard: _capacity == max_capacity → stop expanding
Loading

Last reviewed commit: "fix: align max_capac..."

@JacoCheung
JacoCheung requested a review from jiashuy March 18, 2026 15:01

@jiashuy jiashuy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for fixing it

align_to_table_size(n, alignment=bucket_cap) always returns >= bucket_cap
for positive n, so the floor check is dead code.

Made-with: Cursor
…__post_init__

Without this, a misaligned max_capacity causes the expand() early-exit
guard (self._capacity == self.options.max_capacity) to never fire, since
the C++ hash table silently rounds capacity up to bucket_capacity.

Made-with: Cursor

@jiashuy jiashuy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JacoCheung
JacoCheung merged commit 06f79a4 into NVIDIA:main Mar 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.

[Bug] align_to_table_size causes CUDA illegal memory access when num_embeddings is exactly divisible by world_size

2 participants