Skip to content

[BUG] Adam optimizer v-state corruption when tables have different embedding dims under StorageMode.DEFAULT (host-memory / HybridStorage) #419

Closed

Description

@PWZER

reproduce.py

Describe the bug

When using AdamDynamicEmbeddingOptimizer with multiple embedding tables that have different dim values inside the same DynamicEmbTableState, the velocity (v) state for tables with dim < max_emb_dim is corrupted during the backward pass when running under StorageMode.DEFAULT (host-memory storage / HybridStorage, no cache).

Specifically, load_from_flat/store_to_flat (NumRegions=2) places m+v as a single contiguous block starting at max_emb_dim offset:

NumRegions=2 layout:  [w(d), zeros(M-d), m(d), v(d), zeros(...)]
                       0     d           M      M+d

But update_for_padded_buffer expects each optimizer state to occupy the full max_emb_dim width:

Adam kernel layout:    [w(M), m(M), v(M)]
                        0      M     2*M

When d < M: M+d != 2*M, so the kernel reads v from the wrong position. In the vectorized update4 loop (which iterates M/4 times), the second half of the m region overlaps with the actual v buffer, causing v to be updated with the momentum formula (beta1) instead of the velocity formula (beta2).

Steps/Code to reproduce bug

Run the attached reproduce.py:

# Core setup: two tables with dim=4 and dim=8, Adam optimizer, local_hbm_for_values=0
opt = AdamDynamicEmbeddingOptimizer(OptimizerArgs(eps=1e-8, beta1=0.9, beta2=0.999))
state = create_table_state([
    DynamicEmbTableOptions(dim=4, ..., local_hbm_for_values=0),   # table0
    DynamicEmbTableOptions(dim=8, ..., local_hbm_for_values=0),   # table1
], opt)

The script:

  1. Stores known marker values (v=3.0) to flat tables
  2. Loads them back via load_from_flat(..., copy_mode=CopyMode.VALUE) (simulating DEFAULT forward)
  3. Applies update_for_padded_buffer(grads, values, emb_dim=max_emb_dim=8, value_dim=24) (simulating DEFAULT backward)
  4. Stores back and reloads to check v values

Expected behavior

For both tables, v should be updated by the correct Adam velocity formula:

new_v = beta2 * v + (1-beta2) * grad^2

With grad=0: new_v = 0.999 * 3.0 = 2.997

Actual behavior

For table0 (dim=4): v is updated by the momentum formula because the Adam kernel's second update4 iteration reads v's buffer as m:

new = beta1 * v + (1-beta1) * grad = 0.9 * 3.0 + 0 = 2.7  ← WRONG!

Output:

table0 v reload: [2.7000, ...]  <- CORRUPTED by momentum formula (beta1=0.9)
table1 v reload: [2.9973, ...]  <- correct (beta2=0.999)

Root cause

AdamVecOptimizer::update4 in src/optimizer_kernel.cuh uses a fixed input.dim = emb_dim = max_emb_dim for all rows:

weight_t *m_ptr = weight_ptr + input.dim;  // max_emb_dim
weight_t *v_ptr = m_ptr + input.dim;       // 2 * max_emb_dim

But load_from_flat_table_kernel with NumRegions=2 in src/dynamic_emb_op.cu copies all optimizer states as one contiguous region:

copy_region_vec4(src_base + edim, dst_base + max_emb_dim, opt_dim);
// m+v together at max_emb_dim offset — v ends at max_emb_dim+dim, not 2*max_emb_dim

The two layouts only agree when dim == max_emb_dim for all tables, or when the fused update path (fused_update_for_flat_table) is used (which passes per-table edim to the kernel).

Affected configuration: StorageMode.DEFAULT — triggered when local_hbm_for_values=0 (host-only storage) or when HybridStorage is used without caching. This is the only path that calls update_for_padded_buffer with fixed emb_dim=max_emb_dim.

Environment details (please complete the following information):

  • Environment location: [Bare-metal, Docker, Cloud(specify cloud provider)]
  • Method of recsys-examples install: [conda, Docker, pip, or from source]
    • If method of install is [Docker], provide docker pull & docker run commands used
  • Run print_env.sh from the project root and paste the results here

Additional context

  • The bug also affects AdaGradDynamicEmbeddingOptimizer in theory (same NumRegions=2 layout issue), but AdaGrad has only one optimizer state so the practical impact is less severe.
  • The NumRegions=3 template parameter (which could place m and v at separate aligned offsets) is not instantiated in load_from_flat_table_value or store_to_flat_table_value — only NumRegions=0,1,2 are used.
  • The fused update path (fused_update_for_flat_table) is unaffected because it passes per-table edim to the kernel.
  • Full reproduction script: reproduce.py

Activity

  1. added
    bug
    on Jun 15, 2026
  2. shijieliu commented on Jun 16, 2026

    @shijieliu
    Collaborator

    thanks @PWZER for your interest for recsys-examples. @jiashuy could you take a look of this issue?

  3. jiashuy commented on Jun 17, 2026

    @jiashuy
    Collaborator

    thanks @PWZER for your interest for recsys-examples. @jiashuy could you take a look of this issue?

    Sure, this issue is fixed in this PR : #426 cc @PWZER

  4. added this to the 26.06 milestone on Jun 30, 2026
  5. shijieliu commented on Jun 30, 2026

    @shijieliu
    Collaborator

    fixed in #426

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions