Skip to content

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

Closed

Description

@JacoCheung

Summary

PR #312 (align_to_table_size) introduces a bug: when num_embeddings is exactly divisible by world_size, num_aligned_embedding_per_rank is not set, causing max_capacity to be unaligned to bucket_capacity. This leads to CUDA illegal memory access during distributed training.

Root Cause

In _validate_configs() (planner.py), the conditional:

if tmp_config.num_embeddings != int(num_aligned_embedding_per_rank * world_size):
    tmp_constraint.dynamicemb_options.num_aligned_embedding_per_rank = num_aligned_embedding_per_rank

With align_to_table_size, when num_embeddings % world_size == 0 (e.g. 50M / 16 = 3,125,000), the aligned value equals the original, so the condition is False and num_aligned_embedding_per_rank stays None.

This causes max_capacity = local_row = 3,125,000, which is not a multiple of bucket_capacity (128). The C++ hash table rounds up to 3,125,120, breaking the invariant _capacity == max_capacity. When expand() is triggered, it allocates value tables with 3,125,000 rows but the hash table can return indices up to 3,125,119 — causing out-of-bounds access.

(The old _next_power_of_2 always produced a value larger than the original, so the condition was always True.)

Suggested Fix

  1. Always set num_aligned_embedding_per_rank (remove the conditional), and ensure it is rounded up to a multiple of bucket_capacity.
  2. In expand(), use self.key_index_map.capacity() instead of target_capacity when allocating value tables and updating self._capacity.

Related

Activity

  1. added a commit that references this issue on Mar 18, 2026
    804b08d
  2. added a commit that references this issue on Mar 19, 2026
    06f79a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions