Skip to content

Relax alignment requirements(remove pow of 2) in dynamicemb - #312

Merged
shijieliu merged 5 commits into
NVIDIA:mainfrom
jiashuy:relax_alignment
Mar 11, 2026
Merged

shijieliu merged 5 commits into
NVIDIA:mainfrom
jiashuy:relax_alignment

Conversation

@jiashuy

@jiashuy jiashuy commented Feb 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@jiashuy
jiashuy requested a review from shijieliu February 26, 2026 09:14
@jiashuy

jiashuy commented Feb 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

CI

@greptile-apps

greptile-apps Bot commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR replaces the power-of-2 alignment constraint for dynamic embedding table capacities with a lighter DEMB_TABLE_ALIGN_SIZE = 16 alignment. A new align_to_table_size helper function is introduced and exported, _next_power_of_2 is superseded throughout the planning path, and get_constraint_capacity converts its hard assert into a UserWarning to permit graceful recovery from under-provisioned HBM budgets. Documentation, benchmarks, examples, and tests are updated consistently.

Key changes:

  • types.py: Defines DEMB_TABLE_ALIGN_SIZE = 16 as the new alignment constant.
  • dynamicemb_config.py: Adds align_to_table_size, replaces _next_power_of_2 in DynamicEmbTableOptions.__post_init__, and converts the capacity assert to a UserWarning. _next_power_of_2 now has no callers and is effectively dead code.
  • planner/planner.py: _validate_configs and _dyn_emb_table_size_per_rank use align_to_table_size; bucket-capacity floor now calls align_to_table_size(bucket_cap) rather than using the raw value.
  • test/unit_tests/test_alignment.py: New test file covering alignment correctness and memory stats; the _compare_actual_vs_theoretical caching-mode assertion (hbm_ok = actual["local_hbm_for_values"] >= 0) is trivially true and provides no meaningful validation of cache capacity allocation.
  • scored_hashtable.py: Removes a long-commented-out power-of-2 rounding line, confirming the underlying table no longer requires power-of-2 bucket sizes.

Confidence Score: 4/5

  • This PR is safe to merge; changes are well-scoped and documentation, benchmarks, and tests are updated consistently.
  • The core logic change (aligning to multiples of 16 vs. powers of 2) is simple and correct. All call sites are updated, and a new dedicated test file is added. The main concerns are minor: _next_power_of_2 is now dead code but functionally harmless, and one branch of the new test has a trivially-passing assertion that doesn't validate cache HBM capacity.
  • corelib/dynamicemb/dynamicemb/dynamicemb_config.py (dead code) and corelib/dynamicemb/test/unit_tests/test_alignment.py (weak caching-mode assertion)

Important Files Changed

Filename Overview
corelib/dynamicemb/dynamicemb/dynamicemb_config.py Core change: introduces align_to_table_size, replaces _next_power_of_2 in DynamicEmbTableOptions.__post_init__, and converts the get_constraint_capacity assert to a UserWarning. _next_power_of_2 is now dead code with no remaining callers.
corelib/dynamicemb/dynamicemb/planner/planner.py Replaces _next_power_of_2 with align_to_table_size in _validate_configs and _dyn_emb_table_size_per_rank; bucket-floor fallback now calls align_to_table_size(bucket_cap) instead of using the raw bucket capacity directly.
corelib/dynamicemb/test/unit_tests/test_alignment.py New test file verifying alignment and HBM memory stats. The caching-mode HBM check in _compare_actual_vs_theoretical is trivially non-negative and provides no meaningful validation of cache capacity.
corelib/dynamicemb/test/unit_tests/test_embedding_dump_load.py Replaces _next_power_of_2(math.ceil(math.log2(...))) with align_to_table_size; bucket_capacity and max_capacity are now aligned-to-16 values rather than powers of 2.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["EmbeddingConfig.num_embeddings"] --> B["math.ceil(num_embeddings / world_size)"]
    B --> C["align_to_table_size(n)\n⌈n / 16⌉ × 16"]
    C --> D{per-rank capacity\n< bucket_capacity?}
    D -- Yes --> E["align_to_table_size(bucket_cap)"]
    D -- No --> F["num_aligned_embedding_per_rank"]
    E --> F
    F --> G["DynamicEmbTableOptions.max_capacity"]

    H["global_hbm_for_values"] --> I["ceil(global_hbm / world_size)\n= local_hbm_for_values"]
    I --> J{local_hbm\n< 1 bucket?}
    J -- Yes --> K["Warn + round up to\nbucket_size_in_bytes"]
    J -- No --> L["get_constraint_capacity()\n= floor(capacity/bucket_cap) × bucket_cap"]
    K --> L

    G --> M["Hash Table Capacity\n(multiple of 16)"]
    L --> N["Cache HBM Capacity\n(multiple of bucket_cap)"]
Loading

Comments Outside Diff (2)

  1. corelib/dynamicemb/dynamicemb/dynamicemb_config.py, line 538-554 (link)

    _next_power_of_2 is now dead code

    After this PR, _next_power_of_2 has no remaining callers:

    • Its import in planner.py was replaced with align_to_table_size
    • Its usage in DynamicEmbTableOptions.__post_init__ (line 318) was replaced with align_to_table_size

    The function is no longer used anywhere in the codebase. Leaving unreachable helper functions risks confusion — a future reader might not realise the function is superseded by align_to_table_size and accidentally call it. Consider removing it, or at minimum adding a deprecation comment pointing to the replacement.

  2. corelib/dynamicemb/test/unit_tests/test_alignment.py, line 740-746 (link)

    Trivially-passing HBM assertion for caching mode

    The caching branch of _compare_actual_vs_theoretical only checks that local_hbm_for_values is non-negative, which is always true and carries no diagnostic value:

    else:
        hbm_ok = actual["local_hbm_for_values"] >= 0

    The non-caching branch does a meaningful comparison (effective_actual_hbm == hbm_per_rank). The caching case is harder to assert exactly because get_constraint_capacity can round up to a full bucket, but a lower-bound check would be more useful than simply checking >= 0. For example, you could verify that the actual HBM is at least one full bucket:

    else:
        min_hbm = BUCKET_CAPACITY_CACHE * _byte_per_vector()
        hbm_ok = actual["local_hbm_for_values"] >= min_hbm

    As written, a bug that zeroes out local_hbm_for_values in caching mode would pass this check silently.

Last reviewed commit: 1da799d

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

10 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@jiashuy

jiashuy commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator Author

CI

Comment thread corelib/dynamicemb/README.md Resolved
@shijieliu
shijieliu merged commit 30bb6ba into NVIDIA:main Mar 11, 2026
JacoCheung added a commit to JacoCheung/recsys-examples that referenced this pull request Mar 18, 2026
…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
JacoCheung added a commit that referenced this pull request Mar 19, 2026
…LE_ALIGN_SIZE (#329)

* fix: align DynamicEmb capacity to bucket_capacity instead of DEMB_TABLE_ALIGN_SIZE

PR #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 #328

Made-with: Cursor

* style: remove unreachable bucket_cap guard in _validate_configs

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

* fix: align max_capacity to bucket_capacity in DynamicEmbTableOptions.__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
tiankongdeguiji added a commit to tiankongdeguiji/TorchEasyRec that referenced this pull request Apr 1, 2026
…of-2

Adapt to upstream dynamicemb PR (NVIDIA/recsys-examples#312) which relaxes
table capacity alignment from power-of-2 to multiples of DEMB_TABLE_ALIGN_SIZE
(16), reducing memory waste. Replace local _next_power_of_2 with the new
align_to_table_size API and update wheel to 0.0.1+20260331.bea6b4b.

Co-Authored-By: Claude Opus 4.6 (1M context) <[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