Skip to content

fix(dynamicemb): keep rowwise Adagrad accumulator in fp32 for 16-bit tables - #495

Merged
shijieliu merged 3 commits into
NVIDIA:mainfrom
jyuan-sc:bug-rowwise-adagrad-fp32-state
Oct 4, 2026
Merged

shijieliu merged 3 commits into
NVIDIA:mainfrom
jyuan-sc:bug-rowwise-adagrad-fp32-state

Conversation

@jyuan-sc

Copy link
Copy Markdown
Contributor

Description

Rowwise Adagrad (EXACT_ROWWISE_ADAGRAD) stores its per-row accumulator in the optimizer-state slot of the table's value row, and that slot takes the table's dtype. For float16 tables the accumulator, a running sum of mean squared gradients, easily falls below fp16's smallest subnormal (~6e-8): with per-element gradients around 1e-7 the per-update increment is ~1e-14. The store then rounds it to zero on every step, so each update becomes lr * g / (|g| + eps), a constant-size step that never decays. Rows that are updated often keep taking full-size steps and their norms grow without bound, while rarely updated rows look normal. bfloat16 keeps the range but loses increments smaller than ~1/256 of the running sum.

Reproduction: train an fp16 table with rowwise Adagrad and small gradients, then dump it. Every value in *_emb_opt_values is zero, and the checkpoint meta records optim_state_dtype: float16. The same run on an fp32 table has nonzero accumulators.

Change

Keep the rowwise Adagrad accumulator in fp32 regardless of the table dtype.

  • Kernel (src/optimizer_kernel.cuh): a small RowWiseAdaGradState<weight_t> helper loads and stores the accumulator as fp32. For 16-bit tables it is held as two raw 16-bit words, so the access is only 2-byte aligned: on flat tables the state slot starts at the per-table embedding dim, which need not be even, so a plain float* access could fault. fp32 tables compile to the same code as before.
  • BaseDynamicEmbeddingOptimizer.get_state_dtype(): new hook, defaulting to the table dtype. RowWiseAdaGradDynamicEmbeddingOptimizer returns torch.float32 and packs/unpacks the value in reset_optimizer_states, states_for_checkpoint and states_from_checkpoint. The 16-byte slot, its width and all capacity accounting are unchanged.
  • Dump/load: the meta records optim_state_dtype from get_state_dtype, and the load path no longer casts checkpoint optimizer state to the table dtype before states_from_checkpoint (that cast would round the fp32 accumulator back to zero on resume). Checkpoints written before this change, with the accumulator in the table dtype, still load.

Other optimizers (Adam, Adagrad, FTRL) also keep their state in the table dtype; they are out of scope here.

Tests

  • New test/unit_tests/optimizer/test_rowwise_adagrad_state_precision.py: for fp16 and bf16 tables, after 50 updates with ~1e-5 gradients the accumulator is positive and bit-identical to an fp32 table's for the same gradients, on both the vec4 and scalar kernel paths and at embedding dims 7 and 13 (2-byte-aligned state slot); checkpoint round trip is exact; a legacy table-dtype accumulator loads; reset_optimizer_states writes the fp32 initial value.
  • test_dump_stores_table_precision now expects optim_state_dtype == "float32" and a 4-byte state file for rowwise Adagrad.
  • Ran on an L4 (sm_89) with torch 2.10 / torchrec 1.4: test/unit_tests/optimizer/ 34 passed; test/test_batched_dynamic_embedding_tables_v2.py 383 passed, 4 skipped; the rest of test/unit_tests/ shows the same pass/fail set with and without this change.
  • End to end: in a multi-node training job with fp16 tables, all accumulators were zero before the change and match an fp32 run after it (median same-key ratio 0.996); the hot-row norm blow-up disappears, and the small but consistent AUC deficit of fp16 storage versus fp32 goes away.

Checklist

  • Signed-off commits (DCO)
  • New and updated unit tests
  • pre-commit run -a (ran the hooks on the changed files)

@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Changes optimizer state storage precision for 16-bit embedding tables.

The PR appears safe to merge; no outstanding finding or new actionable issue remains.

Summary

The PR keeps row-wise Adagrad accumulators in fp32 by default for 16-bit tables, adds a configurable state-precision option and checkpoint conversion, and extends precision coverage to flat tables. Since the previous review, it also revalidates incremental-replay layouts after a cache flush and adds a regression test for that path.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  D[Replay delta] --> P[Validate layout and columns]
  P --> F[Flush target cache]
  F --> R[Revalidate backing layout]
  R -->|Compatible| A[Apply source-slot writes]
  R -->|Changed layout| E[Reject replay]
Loading

Reviews (4) · Last reviewed commit: "feat(dynamicemb): add optimizer_state_dt..."

@shijieliu

Copy link
Copy Markdown
Collaborator

/build

@JacoCheung

JacoCheung commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #70441528 -- failed

Job Status Log
pre_check ❌ failed view
train_build_x86 ✅ success view
train_build_arm64 ✅ 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 ✅ success view
unit_test_1gpu_h100 ✅ success 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 ✅ success view
inference_test_nve_2605_1gpu ❌ failed view

Result: 14/17 jobs passed

View full pipeline

@shijieliu

Copy link
Copy Markdown
Collaborator

@jyuan-sc do you think it makes sense to keep the optimizer states in FP32 for all optimizers for fp16 table, or should we make the optimizer-state dtype configurable—for example, via an optimizer_dtype option? Could Adam and other optimizers safely use FP16 or BF16 states?

@jyuan-sc

Copy link
Copy Markdown
Contributor Author

@jyuan-sc do you think it makes sense to keep the optimizer states in FP32 for all optimizers for fp16 table, or should we make the optimizer-state dtype configurable—for example, via an optimizer_dtype option? Could Adam and other optimizers safely use FP16 or BF16 states?

Thanks, @shijieliu I'd make it configurable, but default to FP32 whenever the table is FP16/BF16. 16-bit state isn't safe to use by default for any of the optimizers. With sparse embedding gradients these values fall well below FP16's smallest subnormal (~6e-8). That's exactly what we saw with row-wise Adagrad: the accumulator was stored as 0 on every step, so the learning rate never decayed, and we lost ~0.001 AUC on our L2 model. So I'd suggest an optimizer_state_dtype option that defaults to FP32 for 16-bit tables, with FP16/BF16 as an opt-in for users who need the memory and have validated it on their model.

For this PR, I'd keep it to row-wise Adagrad. Its accumulator fits in the existing 16-byte state slot, so the row layout doesn't change. For Adam, Adagrad and FTRL, the state is sized in table-dtype elements, so FP32 state changes the row layout, memory sizing and checkpoint format. That seems better as a follow-up once we agree on the design: FP32 state inside the row, or a separate FP32 state buffer.

@shijieliu

Copy link
Copy Markdown
Collaborator

@jyuan-sc Sure. I’ll scope this PR to adding an optimizer_state_dtype option and making the row-wise Adagrad accumulator dtype configurable, defaulting to FP32 for FP16/BF16 tables. @jiashuy can follow up with support for the other optimizers.

@jyuan-sc

Copy link
Copy Markdown
Contributor Author

@jyuan-sc Sure. I’ll scope this PR to adding an optimizer_state_dtype option and making the row-wise Adagrad accumulator dtype configurable, defaulting to FP32 for FP16/BF16 tables. @jiashuy can follow up with support for the other optimizers.

Thanks, @shijieliu. @jiashuy. I added the option DynamicEmbTableOptions.optimizer_state_dtype. Row-wise Adagrad accepts torch.float32 (the default when None) or the table's embedding_dtype, which stores the accumulator at table precision as before. Other optimizers accept only their table's dtype for now and raise ValueError otherwise. please help to check

@jyuan-sc

Copy link
Copy Markdown
Contributor Author

/build

@jyuan-sc

jyuan-sc commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

@shijieliu @jiashuy I’ve added an optimizer_state_dtype option in this PR. Could you please help review it? If there are no issues, we can merge the PR. Our work is currently blocked on this change.

Thanks!

jyuan-sc and others added 3 commits October 2, 2026 13:18
…tables

Rowwise Adagrad stores its accumulator in the optimizer-state slot of the
table's value row, and the slot took the table's dtype. For fp16 tables the
accumulator -- a running sum of mean squared gradients -- routinely sits below
fp16's smallest subnormal (6e-8) for embedding gradients, so it rounded to
zero on every store. Each update then became lr * g / (|g| + eps): a
constant-size step with no decay, and frequently updated rows grew without
bound. bf16 tables kept the range but lost increments below 1/256 of the sum.

Keep the accumulator in fp32 regardless of the table dtype:

- kernel: RowWiseAdaGradState loads/stores the accumulator as fp32. 16-bit
  tables hold it as two raw 16-bit words, so the access is 2-byte aligned for
  any embedding width (flat tables start the slot at the per-table emb dim,
  which need not be even). fp32 tables are unchanged.
- optimizer: BaseDynamicEmbeddingOptimizer.get_state_dtype(); rowwise Adagrad
  returns fp32 and packs/unpacks it in reset_optimizer_states,
  states_for_checkpoint and states_from_checkpoint. The 16-byte slot and its
  capacity accounting are unchanged.
- dump records optim_state_dtype from get_state_dtype, and load no longer casts
  the checkpoint state to the table dtype before states_from_checkpoint (which
  would round the fp32 accumulator away again). Checkpoints that stored the
  accumulator in the table dtype still load.

Tests: fp16/bf16 accumulators are bit-identical to an fp32 table's for the same
gradients on both kernel paths and at odd embedding widths; checkpoint round
trip is exact; legacy checkpoints load; reset writes fp32.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
…table path

Add flat-table cases for fused_update_for_flat_table with odd embedding
dims (state offset not 4-byte aligned) for fp16 and bf16: accumulator is
bit-identical to an fp32 table, neighbouring rows are untouched, and a
table rebuilt from a checkpoint resumes bit-exactly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
…ise Adagrad accumulator

Add DynamicEmbTableOptions.optimizer_state_dtype. Row-wise Adagrad accepts
float32, the default, or the table's own dtype, which stores the
accumulator at table precision as before the fp32 fix. Other optimizers
accept only the table's dtype for now and reject anything else when the
table is constructed.

It is a per-table option rather than a fused_params entry because
fused_params also reaches the tables FBGEMM shards, whose TBE constructor
does not accept it. It is part of the grouping key, so tables that differ
in it get separate storage.

The kernel takes the state dtype as a template parameter, dispatched once
per launch to the table's type or float, so neither the row layout nor
the per-element loop changes. Dumps record the configured dtype as
optim_state_dtype and checkpoints load across the two settings. The dump
and load docs now describe the optimizer-state precision correctly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
@jyuan-sc
jyuan-sc force-pushed the bug-rowwise-adagrad-fp32-state branch from b769c47 to 3a52d2d Compare October 2, 2026 20:19
@shijieliu

Copy link
Copy Markdown
Collaborator

/build

@JacoCheung

JacoCheung commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #71421377 -- failed

Job Status Log
pre_check ❌ failed view
train_build_x86 ✅ success view
train_build_arm64 ✅ 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 ✅ success view
unit_test_1gpu_h100 ✅ success 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 ✅ success view
inference_test_nve_2605_1gpu ❌ failed view

Result: 14/17 jobs passed

View full pipeline

@shijieliu

Copy link
Copy Markdown
Collaborator

LGTM, merged!

@shijieliu
shijieliu merged commit 6b983fc into NVIDIA:main Oct 4, 2026
1 of 2 checks passed
jiashuy added a commit to jiashuy/recsys-examples that referenced this pull request Oct 16, 2026
NVIDIA#495 added test_rowwise_adagrad_state_precision.py and did not register it,
so its 42 cases -- packed fp32 accumulators, odd embedding dims, checkpoint
conversion, configurable state precision -- have not run in CI since. It is
the only unregistered entry point in test/; the other two files in
test/unit_tests/optimizer/ are both listed.

The defect it covers is silent: an fp16 accumulator rounds to zero on every
store, the update degenerates to a constant step with no decay, and training
quietly goes wrong.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
shijieliu pushed a commit to shijieliu/recsys-examples that referenced this pull request Oct 19, 2026
NVIDIA#495 added test_rowwise_adagrad_state_precision.py and did not register it,
so its 42 cases -- packed fp32 accumulators, odd embedding dims, checkpoint
conversion, configurable state precision -- have not run in CI since. It is
the only unregistered entry point in test/; the other two files in
test/unit_tests/optimizer/ are both listed.

The defect it covers is silent: an fp16 accumulator rounds to zero on every
store, the update degenerates to a constant step with no decay, and training
quietly goes wrong.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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