Repository navigation
fix: align DynamicEmb capacity to bucket_capacity instead of DEMB_TABLE_ALIGN_SIZE - #329
Conversation
…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 SummaryThis PR fixes three related bugs in the Key changes:
One regression to consider: Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
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
Last reviewed commit: "fix: align max_capac..." |
jiashuy
left a comment
There was a problem hiding this comment.
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
Summary
align_to_table_sizenow aligns tobucket_capacity(default 128) instead ofDEMB_TABLE_ALIGN_SIZE(16), matching the C++ hash table's internal alignment._validate_configsto always setnum_aligned_embedding_per_rank, preventingmax_capacityfrom being unaligned whennum_embeddingsis perfectly divisible byworld_size.expand()to usekey_index_map.capacity()(actual hash table capacity) for value table allocation, preventing out-of-bounds CUDA memory access during rehash.Root Cause
The C++
ScoredHashTablerounds capacity up to multiples ofbucket_capacity(128). Butalign_to_table_sizeonly rounded to multiples of 16. Whenceil(num_embeddings / world_size)was already a multiple of 16,_validate_configsskipped settingnum_aligned_embedding_per_rank, somax_capacitywas set to the rawlocal_row(not a multiple of 128). Duringexpand(), value tables were allocated with the smallermax_capacity, while the hash table could return indices up to its actual (larger, bucket-aligned) capacity — causing illegal memory access.Test plan
test_alignment.pyto reflectbucket_capacity-based alignmenttest_align_to_table_size_bucket_capacityvalidating alignment with various bucket capacitiesnum_embeddings=50_000_000, world_size=16(the failing configuration) to confirm the fixFixes #328
Made with Cursor
CI