Skip to content

[FEA](DynamicEmb) fp8 table storage with stochastic rounding for row-wise Adagrad - #548

Merged
shijieliu merged 12 commits into
NVIDIA:mainfrom
jiashuy:cherry-pick-pr499
Nov 16, 2026
Merged

shijieliu merged 12 commits into
NVIDIA:mainfrom
jiashuy:cherry-pick-pr499

Conversation

@jiashuy

@jiashuy jiashuy commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds torch.float8_e4m3fn as a DynamicEmb table embedding_dtype: one byte per
weight, a quarter of an fp32 table. Builds on #499 (the first two commits are its
feature and perf commits, rebased onto current main); the rest are fixes and tests
found while reviewing it.

How fp8 storage works:

  • A weight is stored as the e4m3 code of weight * 256, so e4m3's range moves to
    [2^-17, 1.75], which is where trained embeddings live. Weights are held only in
    [-1.75, 1.75]; a larger one saturates to the bound.
  • Every write rounds stochastically. An optimizer step is often smaller than half an
    e4m3 step, and round-to-nearest would discard it and freeze rows.
  • Only EXACT_ROWWISE_ADAGRAD is supported. The accumulator stays fp32, in the
    16-byte state slot at the end of the row. Other optimizers are refused at
    construction.
  • A checkpoint is written as fp16, which holds every fp8 value exactly.

What this PR adds on top of #499

  • Gradient precision (gradient_dtype): an fp8 table takes fp32 gradients whatever
    the gradient dtype is; every other table takes the gradient as it is, never lowered.
    The fused flat-table path used to cast to the table dtype (fp16 for fp8), which
    flushes gradients below ~6e-8 to zero, and the padded-buffer path did not cast at all.
    Both paths now use the same expression. fp16/bf16 tables no longer have their
    gradients cast down on the fused path (more accurate, slightly different numbers from
    main).
  • 64-bit row offsets in the optimizer kernels: row * stride was a 32-bit product
    and wrapped at 232 elements, which 220 unique rows of width 4096 already reach;
    later rows then read the gradient of, and in the padded buffer wrote the weights of,
    a row 2**20 before them. Six sites in the four update kernels now compute the offset
    in int64. Only the rows the optimizer addresses are changed here.
  • Warning for initializers that can generate weights beyond +-1.75, issued once per
    fp8 table at construction (a default NORMAL with std_dev=1.0, UNIFORM or
    TRUNCATED_NORMAL with wider bounds, an out-of-range CONSTANT; NORMAL is judged on
    |mean| + 3 std_dev). Warning only; nothing else changes.
  • Sequence evaluation through output_alloc (the hierarchical dist's eval-mode
    lookup) copied an fp8 table's rows with copy_ / index_copy_ before the fp8 decode,
    so the rows left the lookup as stored codes, 256x the weights. The decode now runs
    before the allocator branch, always (a no-op .to for other tables). The training
    forward decodes inside its kernels and was not affected.
  • output_dtype=torch.float8_e4m3fn is refused at construction. The lookup
    kernels write an fp8 output as stored codes (256x), so fp8 is only a table storage
    dtype; the output is fp32, fp16 or bf16, as before.
  • Docs: the embedding_dtype docstring now describes fp8: the 256x encoding, the
    +-1.75 range, the optimizer restriction, the fp16 checkpoint, unseeded rounding. It
    used to say a checkpoint is written at the table dtype and a precision mismatch warns,
    which is not true for fp8. A comment in src/utils.h no longer claims on-grid values
    are stored "exactly" (a subnormal one moves up a step with probability ~1e-7).
  • Tests registered: test_fp8_table_storage.py and test_optimizer_row_offsets.py
    are added to the fwd_bwd group of unit_test.sh (the first was not run by CI).

Tests

  • test_fp8_table_storage.py: encode/decode, the saturation bound, rounding is
    unbiased, steps smaller than half an fp8 step still move the weights, odd dims,
    gradient dtype rule, the initializer risk rule.
  • test_optimizer_row_offsets.py (new): SGD on rows addressed past 2**32 elements,
    for gradients only, weights only, and both, flat and padded paths, vec4 and scalar.
    Fails on the old kernels, passes on the fixed ones. Needs up to 17 GB of GPU memory
    and skips on a GPU without room.
  • test_batched_dynamic_embedding_tables_v2.py:
    • test_fp8_table_trains_like_fp32_table: an fp8 and an fp32 table start bit-equal
      (12/256 is an e4m3 value), train on the same batches; per-step output error stays
      below 9% (SUM) / 16% (NONE) of the reference RMS, and the net drift matches.
      Measured: 5.8% / 10.5%.
    • test_fp8_table_learns_from_gradients_fp16_would_flush: gradients scaled to ~1e-8;
      asserts the fp8 table receives fp32 gradients and still drifts like the fp32 table.
      Fails when the fp16 cast is put back (drift ratio 1.03 vs 0.000).
    • test_fp8_table_warns_of_an_initializer_that_can_saturate.
    • test_fp8_sequence_lookup_through_output_alloc_returns_real_weights: evaluation and
      training forward, rows in order and permuted, through an allocator.
    • test_fp8_is_refused_as_an_output_dtype.

Verification on EOS (H100, container distributed-recommender:devel_latest):

  • Before the final rebase: fwd_bwd group, all 31 files passed; load_dump group, all
    items passed; the tightened error limits passed 20/20 repeats (the rounding is
    unseeded, so they were repeated).
  • On the rebased head (current main, including [FEA] Support configurable indexing and NVE layers for exportable embedding collections #478), built from source in the same
    container:
    • load_dump group (commit 3e064a71): 5/5 files passed.
    • fwd_bwd group (commit 3e064a71): 30/31 files passed; the one failure was a
      missing import in a test of that commit (NameError, 2 cases, the other 401 tests
      of that file passed), fixed by the commit after it.
    • On the head (1d91c82f): test_batched_dynamic_embedding_tables_v2.py in full
      (409 passed, 4 skipped), test_fp8_table_storage.py (25 passed) and
      test_optimizer_row_offsets.py (6 passed).

Known limitations (left as they are)

  • Weights beyond +-1.75 saturate silently; only the initializer is warned about. Loading
    an fp32/bf16 checkpoint with larger values, or training that grows a weight past the
    bound, is not reported.
  • A fp16 checkpoint trained as a fp16 table loads into an fp8 table without a warning:
    it cannot be told from an fp8 table's own dump.
  • Stochastic rounding is not seeded, so two runs are not bit-identical.
  • No multi-rank or int32-key fp8 test. The only fp8-specific multi-rank code is the
    checkpoint all_gather (dtypes of empty and non-empty ranks agree by reading the code).

🤖 Generated with Claude Code

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@greptile-apps

greptile-apps Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The changes since the previous review appear safe to merge.

Summary

Adds fp8 table storage with stochastic rounding for row-wise Adagrad, while keeping accumulators in fp32 and checkpoints in fp16.

  • DynamicEmb tables store weights as scaled fp8 values and decode them for lookups.
  • Optimizer updates keep gradients at their incoming precision unless fp8 needs fp32.
  • Optimizer kernels address rows beyond 2³² elements without wrapping offsets.

No new actionable issues found in the changes since the previous review.

Reviews (4) · Last reviewed commit: "[TEST](DynamicEmb) Cover the training fo..." · Reviewed by Greptile

jyuan-sc and others added 10 commits November 16, 2026 07:48
…wise Adagrad

Add torch.float8_e4m3fn as a table dtype. An fp8 table stores e4m3 codes of
value * 256, which moves e4m3's range to [2^-17, 1.75] (exponent bias 15, the
format FBGEMM's FP8 comms use): trained embeddings sit around 1e-3..0.4, where
plain e4m3fn would leave a third of the values subnormal and flush everything
below 2^-9.

- kernel: dyn_fp8_t storage type with float conversions; every write rounds
  stochastically, because a row-wise Adagrad step is often below half an e4m3
  step and round-to-nearest would freeze rows after a few dozen updates. Values
  already on the grid are stored exactly. Same-type copies move raw bytes, so
  the fp32 accumulator packed in the state slot is never reinterpreted as fp8.
  A value-type dispatch adds fp8 to the lookup, initializer, load/store and
  row-wise Adagrad paths only; gradients and other optimizers are unchanged.
- optimizer: row-wise Adagrad keeps an fp32 accumulator in fp8 tables (four raw
  bytes in the 16-byte slot); every other optimizer, and a table-dtype
  accumulator, is rejected for fp8. Gradients reach the fused update as fp16.
- checkpoints: fp8 tables dump and export embeddings as fp16, which holds every
  fp8 value exactly, so any loader reads them; loads and delta replay convert
  back. Placeholder parameters stay fp16.

Tests: accumulator bit-identical to an fp32 table at odd widths; weights track
an fp32 table with zero mean error; rounding is unbiased; sub-step updates
accumulate; fp8 trains like fp32 through HBM, cache and host storage, pooled
and sequence; fp16 dump is lossless and round-trips byte for byte.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
Load and store four fp8 codes, and the fp32 row-wise Adagrad accumulator in an
fp8 row, as one aligned 32-bit access instead of four byte accesses, falling
back to bytes for unaligned or partial vectors. Host-resident tables pay a PCIe
transaction per access, so the byte loop made fp8 slower than fp16 there.

L4, three tables (dims 64/32/896), batch 4096 x 8 ids, forward + backward with
row-wise Adagrad, ms/step:

  storage  fp32   fp16   fp8 before  fp8 after
  host     34.4   23.7   61.4        17.0
  HBM       8.7    9.0    8.1         8.1

The fp8 weight-tracking test now bounds the mean error by four standard
errors: the stochastic rounding noise is not seeded, so a fixed bound flaked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jingping Yuan <[email protected]>
The fp8 storage tests were added without being listed in any group, so CI
never ran them. Add them to the fwd_bwd group next to the other optimizer
tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…izer

The fused update cast every gradient to the table's dtype, and the fp8 commit
made that fp16 for fp8 tables: fp16 flushes gradients below 6e-8 to zero and
keeps few bits below 6e-5, so a row-wise Adagrad step is lost exactly when the
gradient is small. The padded-buffer path passed the gradient as it was, so one
fp8 table took different precision depending on which path it went through.

The optimizer kernels take the gradient and the weight at independent dtypes
and convert every gradient to fp32 to compute, so converting a gradient to the
weight's dtype can only lose precision. gradient_dtype(table_dtype, grad_dtype)
now returns fp32 for an fp8 table, whatever the gradient is, and the gradient's
own dtype for every other table; both paths use it the same way.

fp16 and bf16 tables no longer have their gradients rounded to the table dtype
on the fused path.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The optimizer kernels located a row's gradient, and in the padded buffer its
weights, as row * stride with both operands 32-bit, so the product wrapped at
2**32 elements. A batch of 2**20 unique rows of width 4096 reaches it. From
there on every row read the gradient of the row 2**20 before it and, in the
padded buffer, wrote its weights there too, which trains the wrong rows without
any error. The flat-table weight pointer was already computed in 64 bits; the
flat-table gradient pointer and both padded-buffer pointers were not.

Widen the row to 64 bits in the four kernels that compute these offsets.

The gradients and the weights are addressed by separate products, so the new
test gives each a case where only it passes 2**32 elements: large gradients with
small weights (flat table), and large weights with small gradients (padded
buffer, whose value rows carry optimizer state). A third case has both large,
the only way to push a padded-buffer gradient address past 2**32, since a value
row is never narrower than its gradient row. The gradient of row r differs from
its wrapped neighbour's, so a wrapped read or write cannot pass as the right
one. Each case needs up to 17 GB and is skipped on a GPU without room for it.
The test is registered in the fwd_bwd group.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Row-wise Adagrad's step is lr * g / sqrt(accumulated g^2), so it does not
depend on how large the gradients are: an fp32 table takes full steps on
gradients of 1e-8. fp16 flushes everything below 3e-8 to zero, so an fp8 table
whose gradients are lowered to fp16 does not move at all. No test could see
that: the optimizer unit tests pass fp32 gradients straight to the padded
buffer kernels, and the end-to-end test trains on gradients of order one, which
fp16 holds without loss.

The new test trains an fp8 and an fp32 table on gradients of a few 1e-8, with
eps far below them, and checks that the fp8 table receives fp32 gradients on
every update it takes, through whichever of the fused and padded paths it goes,
and that its rows move at least half as far as the fp32 table's. Measured on an
H100, the fp8 table moves 1.03x as far as the fp32 table; with the fp16 cast
put back it does not move at all.

The end-to-end test and the new one build and train the same two tables, so
that code, and the comparison of their exported rows, is now shared by both.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The fp8 tests start every row at 12 / 256, written out in the table builder and
again in the function that measures how far the rows moved. The value is not
arbitrary: an fp8 table stores e4m3 codes of value * 256, and 12 is exactly an
e4m3 value (1.5 * 2**3), so the row is stored without rounding and the fp8 and
the fp32 table start equal bit for bit. Give it a name and say so, so the two
places cannot drift apart.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The end-to-end fp8 test allowed every step's output to be 25% off the fp32
table's, RMS over RMS. Measured on an H100 over 10 runs each, the worst step is
5.8% for SUM pooling (at most 5.9%) and 10.5% for NONE (at most 10.7%), and the
unseeded rounding moves those by about 1%, so 25% left room for an fp8 table
that is badly wrong to pass: one that learns at half the rate is 10.2% off with
SUM pooling.

Allow 0.09 for SUM and 0.16 for NONE, about 1.5x the largest measured value, for
other hardware.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…eights beyond +-1.75

An fp8 table stores e4m3 codes of weight * 256, so it holds weights only up to
+-1.75 and saturates a larger one without a word. Warn at construction when a
table's initializer (a default NORMAL, a UNIFORM or TRUNCATED_NORMAL with wider
bounds, an out-of-range CONSTANT) can generate such weights, and document the
fp8 range, optimizer, and checkpoint behavior in the embedding_dtype docstring.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
jiashuy and others added 2 commits November 16, 2026 09:19
…hem, and refuse an fp8 output dtype

A sequence evaluation that goes through output_alloc (the hierarchical dist's
eval-mode lookup) copied the looked-up rows with copy_ / index_copy_, which only
convert dtypes, before the fp8 decode ran, so an fp8 table's rows left the lookup
as their stored codes, 256x the weights. Decode before the allocator branch, and
always: for any other table that is a no-op `.to`.

The lookup kernels write an fp8 output as stored codes too, so fp8 is only a
table storage dtype: reject output_dtype=torch.float8_e4m3fn at construction.

Tests: an fp8 table's sequence evaluation through output_alloc (in order and with
an out_rows permutation) returns the real weights; fp8 output is refused for fp8
and fp16 tables. Import FP8_STORAGE_DTYPE in the test module, which the previous
commit's initializer warning test used without importing.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… test

Run the allocated-output check in training mode as well as in evaluation: a
training forward decodes inside its kernels, so the real weights must come out
of the allocator's buffer there too.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@shijieliu
shijieliu merged commit 725f33c into NVIDIA:main Nov 16, 2026
1 check passed
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