Repository navigation
fix(dynamicemb): keep rowwise Adagrad accumulator in fp32 for 16-bit tables - #495
Conversation
|
|
/build |
|
❌ Pipeline #70441528 -- failed
Result: 14/17 jobs passed |
|
@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. |
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 |
|
/build |
|
@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! |
…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]>
b769c47 to
3a52d2d
Compare
|
/build |
|
❌ Pipeline #71421377 -- failed
Result: 14/17 jobs passed |
|
LGTM, merged! |
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]>
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]>
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. Forfloat16tables 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 becomeslr * 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.bfloat16keeps 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_valuesis zero, and the checkpoint meta recordsoptim_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.
src/optimizer_kernel.cuh): a smallRowWiseAdaGradState<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 plainfloat*access could fault. fp32 tables compile to the same code as before.BaseDynamicEmbeddingOptimizer.get_state_dtype(): new hook, defaulting to the table dtype.RowWiseAdaGradDynamicEmbeddingOptimizerreturnstorch.float32and packs/unpacks the value inreset_optimizer_states,states_for_checkpointandstates_from_checkpoint. The 16-byte slot, its width and all capacity accounting are unchanged.optim_state_dtypefromget_state_dtype, and the load path no longer casts checkpoint optimizer state to the table dtype beforestates_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
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_stateswrites the fp32 initial value.test_dump_stores_table_precisionnow expectsoptim_state_dtype == "float32"and a 4-byte state file for rowwise Adagrad.test/unit_tests/optimizer/34 passed;test/test_batched_dynamic_embedding_tables_v2.py383 passed, 4 skipped; the rest oftest/unit_tests/shows the same pass/fail set with and without this change.Checklist
pre-commit run -a(ran the hooks on the changed files)