Repository navigation
Description
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:
- Stores known marker values (v=3.0) to flat tables
- Loads them back via
load_from_flat(..., copy_mode=CopyMode.VALUE)(simulating DEFAULT forward) - Applies
update_for_padded_buffer(grads, values, emb_dim=max_emb_dim=8, value_dim=24)(simulating DEFAULT backward) - 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_dimBut 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_dimThe 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 runcommands used
- If method of install is [Docker], provide
- Run
print_env.shfrom the project root and paste the results here
Additional context
- The bug also affects
AdaGradDynamicEmbeddingOptimizerin theory (same NumRegions=2 layout issue), but AdaGrad has only one optimizer state so the practical impact is less severe. - The
NumRegions=3template parameter (which could place m and v at separate aligned offsets) is not instantiated inload_from_flat_table_valueorstore_to_flat_table_value— onlyNumRegions=0,1,2are used. - The fused update path (
fused_update_for_flat_table) is unaffected because it passes per-tableedimto the kernel. - Full reproduction script:
reproduce.py
Activity
shijieliu commentedon Jun 16, 2026
thanks @PWZER for your interest for recsys-examples. @jiashuy could you take a look of this issue?
jiashuy commentedon Jun 17, 2026
Sure, this issue is fixed in this PR : #426 cc @PWZER
fix(dynamicemb): get per-table emb dims via Storage.embedding_dims() …
fix(dynamicemb): pass per-table all_dims_vec4 to padded-buffer optimi…
fix(dynamicemb): validate table_ids/table_emb_dims are CUDA tensors i…
fix(dynamicemb): correct optimizer-state layout in padded-buffer upda…
shijieliu commentedon Jun 30, 2026
fixed in #426
[FEA] Support FTRL optimizer in DynamicEmb (#487)