Skip to content

fix(dynamicemb): correct optimizer-state layout in padded-buffer update - #426

Merged
shijieliu merged 8 commits into
NVIDIA:mainfrom
jiashuy:fix/issue-419-adam-padded-buffer-vstate
Jun 30, 2026
Merged

shijieliu merged 8 commits into
NVIDIA:mainfrom
jiashuy:fix/issue-419-adam-padded-buffer-vstate

Conversation

@jiashuy

@jiashuy jiashuy commented Jun 17, 2026 •

Copy link
Copy Markdown
Collaborator

The contiguous padded-buffer optimizer kernels (StorageMode.DEFAULT / HybridStorage without caching) assumed each optimizer state occupied the full max_emb_dim width. For tables with dim < max_emb_dim the Adam velocity (v) slot fell inside the kernel's momentum (m) region and was updated with the beta1 rule instead of beta2, corrupting v; the embedding update also read uninitialized padding as v.

Make the padded-buffer kernels layout-aware: optimizer states start at a fixed max_emb_dim offset with per-row dim. Add OptimizierInput.state_offset (flat-table path keeps state_offset == dim; padded path uses max_emb_dim) and thread per-row table_ids/table_emb_dims through the padded-buffer update path (CUDA kernels, pybind, optimizer.py, and the backward caller).

Add test/unit_tests/test_padded_buffer_optimizer.py covering Adam with mixed embedding dims.

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.

…te (NVIDIA#419)

The contiguous padded-buffer optimizer kernels (StorageMode.DEFAULT / HybridStorage without caching) assumed each optimizer state occupied the full max_emb_dim width. For tables with dim < max_emb_dim the Adam velocity (v) slot fell inside the kernel's momentum (m) region and was updated with the beta1 rule instead of beta2, corrupting v; the embedding update also read uninitialized padding as v.

Make the padded-buffer kernels layout-aware: optimizer states start at a fixed max_emb_dim offset with per-row dim. Add OptimizierInput.state_offset (flat-table path keeps state_offset == dim; padded path uses max_emb_dim) and thread per-row table_ids/table_emb_dims through the padded-buffer update path (CUDA kernels, pybind, optimizer.py, and the backward caller).

Add test/unit_tests/test_padded_buffer_optimizer.py covering Adam with mixed embedding dims.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@jiashuy

jiashuy commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator Author

/build

@JacoCheung

JacoCheung commented Jun 17, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #55101861 -- failed

Job Status Log
pre_check ✅ success view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ❌ failed view
dynamicemb_test_load_dump_8gpus ❌ failed view
unit_test_1gpu_a100 ❌ failed view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ❌ failed view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 10/17 jobs passed

View full pipeline

@greptile-apps

greptile-apps Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a state-layout corruption in the contiguous padded-buffer optimizer kernels (StorageMode.DEFAULT / HybridStorage without caching). When a table's edim < max_emb_dim, the old kernels used max_emb_dim as dim for all rows, causing Adam's v slot to be written at weight_ptr + 2*max_emb_dim instead of weight_ptr + max_emb_dim + edim, corrupting v with the beta1 rule.

  • Adds state_offset to OptimizierInput (flat-table path keeps state_offset == dim; padded-buffer path sets it to max_emb_dim) and threads per-row table_ids/table_emb_dims through all four padded-buffer CUDA kernels, their pybind wrappers, optimizer.py, and the backward caller.
  • Fixes the incorrect all_dims_vec4 = (emb_dim % 4 == 0) computation inside each host function, replacing it with the correctly pre-computed per-table flag from storage.all_dims_vec4().
  • Adds a new unit test exercising Adam with mixed embedding dims over both the scalar and vec4 kernel paths, manually constructing the padded buffer in a way that matches the load_from_flat_table two-region copy (confirmed by reading the load/store kernels in dynamic_emb_op.cu).

Confidence Score: 5/5

Safe to merge. The fix is mechanically correct and well-motivated: the load/store kernel layout in dynamic_emb_op.cu confirms that optimizer states land at [max_emb_dim, max_emb_dim+2*edim) in the padded buffer, which is exactly what the new state_offset=max_emb_dim + dim=edim parameterisation produces.

The core corruption (v slot written with beta1 rule when edim < max_emb_dim) is definitively fixed and verified against the actual load/store kernel layout. The previously flagged all_dims_vec4 = (emb_dim % 4 == 0) issue is also resolved. The new test exercises both the vec4 and scalar Adam paths. The only gap is missing regression tests for AdaGrad and RowWiseAdaGrad, which share the same state_offset infrastructure.

test/unit_tests/test_padded_buffer_optimizer.py — AdaGrad and RowWiseAdaGrad are not covered; a future regression could re-introduce the state pointer bug for those optimizers without failing any existing test.

Important Files Changed

Filename Overview
corelib/dynamicemb/src/optimizer_kernel.cuh Core fix: adds state_offset to OptimizierInput; padded-buffer kernels now look up per-row edim via table_ids/table_emb_dims and pass max_emb_dim as state_offset, matching the load_from_flat_table layout.
corelib/dynamicemb/src/optimizer.cu All four padded-buffer host functions now accept table_ids and table_emb_dims tensors with CUDA checks, drop the per-function all_dims_vec4 = (emb_dim % 4 == 0) computation, and forward the correct per-row pointers to the kernel.
corelib/dynamicemb/dynamicemb/batched_dynamicemb_function.py Backward pass now queries storage.embedding_dims(on_device=True) and storage.all_dims_vec4() before calling update_for_padded_buffer; unique_table_ids is already available at line 1231.
corelib/dynamicemb/dynamicemb/key_value_table.py Adds table_emb_dims_host to DynamicEmbTableState; implements embedding_dims() and all_dims_vec4() on both DynamicEmbStorage and HybridStorage (correctly returning HBM-tier dims for the hybrid case).
corelib/dynamicemb/dynamicemb/types.py Adds abstract embedding_dims() and all_dims_vec4() to Storage base class with well-documented contracts.
corelib/dynamicemb/dynamicemb/optimizer.py All four padded-buffer optimizer implementations updated to accept and forward table_ids, table_emb_dims, all_dims_vec4; clean mechanical change.
corelib/dynamicemb/test/unit_tests/test_padded_buffer_optimizer.py New Adam regression test covers both vec4 (dims=[8,4]) and scalar (dims=[8,6]) paths with correct buffer layout matching load_from_flat_table; SGD/AdaGrad/RowWiseAdaGrad optimizers are not tested.
corelib/dynamicemb/test/test_batched_dynamic_embedding_tables_v2.py PyDictStorage mock updated with embedding_dims() and all_dims_vec4() implementations matching the real storage contract.

Reviews (4): Last reviewed commit: "perf(dynamicemb): return persistent host..." | Re-trigger Greptile

Comment thread corelib/dynamicemb/src/optimizer.cu Resolved
jiashuy and others added 5 commits June 23, 2026 07:55
…in padded update

The NVIDIA#419 fix referenced storage._state.table_emb_dims in the padded-buffer backward path, but HybridStorage has no _state (it uses _hbm/_host tiers), raising AttributeError under StorageMode.DEFAULT with HybridStorage (e.g. test_lfu_scores).

Add a Storage.embedding_dims(on_device=False) interface returning per-table embedding dims as an int64 tensor (CPU via table_emb_dims_cpu without a device sync, or the existing CUDA tensor). Implemented by DynamicEmbStorage (_state) and HybridStorage (_hbm tier, which produces the value buffer in find()). Backward now calls storage.embedding_dims(on_device=True) instead of reaching into a storage-specific attribute.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…zer kernels

The padded-buffer host functions derived all_dims_vec4 from emb_dim % 4 (max_emb_dim only). After switching the kernels to per-row edim, a table whose edim is not a multiple of 4 but routed through the vec4 kernel (because max_emb_dim is) would have its m_vec.store spill past the m region into v with the beta1 rule — the same corruption NVIDIA#419 fixes. Only manifests with a vec4-aligned max_emb_dim plus a non-aligned table dim.

Thread the per-table-correct flag (state.all_dims_vec4, which checks every dim and value_dim) into the four *_update_for_padded_buffer host functions / pybind args / optimizer.py wrappers, exposed via a new Storage.all_dims_vec4() interface (DynamicEmbStorage._state, HybridStorage._hbm). Parametrize the regression tests over an aligned mix [8,4] (vec4 path) and a misaligned mix [8,6] (scalar path).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…n padded update

The four padded-buffer host functions checked grads/values with TORCH_CHECK but not the table_ids/table_emb_dims tensors added for the NVIDIA#419 fix. A CPU tensor passed accidentally (e.g. embedding_dims(on_device=False)) would be dereferenced as device memory inside the kernel, causing an opaque illegal-memory-access at runtime. Add explicit TORCH_CHECK(...is_cuda()) for a clear error at the entry point.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The '# adam state = 2 * max_emb_dim' comment could read as if each optimizer slot holds max_emb_dim live values. Clarify that the row stride is emb + m + v (each slot reserved max_emb_dim wide) while only the row's own edim values are live per slot, matching the module layout diagram.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
pre-commit (autoflake) removes the pass following the docstring in the new embedding_dims/all_dims_vec4 abstract methods; the docstring is a valid body.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@jiashuy
jiashuy force-pushed the fix/issue-419-adam-padded-buffer-vstate branch from c3363f3 to ac46393 Compare June 23, 2026 03:18
@jiashuy

jiashuy commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator Author

/build

@JacoCheung

JacoCheung commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #55518095 -- failed

Job Status Log
pre_check ✅ success view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ❌ failed view
dynamicemb_test_load_dump_8gpus ✅ success view
unit_test_1gpu_a100 ❌ failed view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ✅ success view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 12/17 jobs passed

View full pipeline

…age test ref

The new Storage abstract methods were not implemented on PyDictStorage (the pure-Python reference Storage in test_batched_dynamic_embedding_tables_v2.py), so every test using it failed to instantiate the abstract class. Implement both from its existing _emb_dims/_value_dims.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@jiashuy

jiashuy commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator Author

/build

@jiashuy
jiashuy requested a review from shijieliu June 23, 2026 06:37
@JacoCheung

JacoCheung commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #55531091 -- failed

Job Status Log
pre_check ✅ success view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ✅ success view
dynamicemb_test_load_dump_8gpus ✅ success view
unit_test_1gpu_a100 ❌ failed view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ✅ success view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 13/17 jobs passed

View full pipeline

…g_dims

embedding_dims(on_device=False) previously built a fresh CPU tensor each call. Add a persistent table_emb_dims_host tensor on DynamicEmbTableState (built once in create_table_state) and return it; on_device=True returns the existing GPU table_emb_dims. table_emb_dims_cpu stays a List[int] for the per-table int lookups. PyDictStorage gets equivalent persistent _emb_dims_dev/_emb_dims_host members.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@jiashuy

jiashuy commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator Author

/build

@JacoCheung

JacoCheung commented Jun 29, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #56181018 -- failed

Job Status Log
pre_check ❌ failed view
train_build_x86 ❌ failed view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ⏩ skipped view
build_whl ⏩ skipped view
dynamicemb_test_fwd_bwd_8gpus ⏩ skipped view
dynamicemb_test_load_dump_8gpus ⏩ skipped view
unit_test_1gpu_a100 ⏩ skipped view
unit_test_1gpu_h100 ⏩ skipped view
unit_test_4gpu ⏩ skipped view
unit_test_tp_4gpu ⏩ skipped view
L20_unit_test_1gpu ⏩ skipped view
inference_unit_test_1gpu ⏩ skipped view
inference_test_1gpu ⏩ skipped view

Result: 4/6 jobs passed

View full pipeline

@jiashuy

jiashuy commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator Author

/build

@shijieliu
shijieliu merged commit 5dc46a2 into NVIDIA:main Jun 30, 2026
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.

3 participants