Repository navigation
[FEA] FTRL group sparsity in DynamicEmb - #500
Merged
shijieliu merged 17 commits intoOct 22, 2026
Merged
Conversation
Contributor
[Medium impact] The PR appears safe to merge; the remaining findings are non-blocking test recommendations. Findings
SummaryAdds row-level group sparsity to FTRL through
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Update a touched row] --> B{group_lasso_reg > 0?}
B -->|No| C[Plain FTRL update]
B -->|Yes| D[Update linear and accum]
D --> E[Apply L1 threshold and compute row norm]
E --> F{Row norm exceeds group threshold?}
F -->|No| G[Write zero weights]
F -->|Yes| H[Shrink and write weights]
Reviews (17) · Last reviewed commit: "test(dynamicemb): keep one group dtype/m..." · Reviewed by Greptile |
jiashuy
force-pushed
the
feat/dynamicemb-ftrl-group-sparsity
branch
from
October 19, 2026 03:48
daedbde to
bb0fc04
Compare
Collaborator
Author
|
TODO:
|
Add an optional group-lasso term to FTRL (G-FTRL, Ni et al., CIKM 2019), with one embedding row as the group. When group_lasso_reg > 0 the row's L1-thresholded linear term is shrunk by max(0, 1 - reg / ||.||_2), so a row's weights become exactly zero together. group_lasso_reg = 0 takes the original single-pass kernel and is unchanged. The group path is two passes over the row (advance linear/accum and sum squares, then solve the weights); shared memory is requested only then. The new argument is last everywhere (OptimizerArgs, the batched tables signature, the C++ bindings with a default of 0) so no positional caller shifts, and checkpoints without it load as plain FTRL. Tests: float64 reference match incl. exact-zero rows, fp32/fp16/bf16 and mixed-width tables on both the padded-buffer and flat-table paths (padding slots must stay untouched), group_lasso_reg=0 bit-identical to plain FTRL, flat-table == padded-buffer, validation and checkpoint meta round-trip. The float64 reference returns 0, not NaN, for an untouched coordinate. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
jiashuy
force-pushed
the
feat/dynamicemb-ftrl-group-sparsity
branch
2 times, most recently
from
October 21, 2026 06:52
ac4b62a to
7214c6f
Compare
…, 2019) Bring the group-sparse update in line with Algorithm 1 and Eq. 3 of the paper, now that its text has been read: - The row norm is of the raw linear term; the L1 soft-threshold the first version applied before it is not in the paper's closed form. l1_reg is therefore rejected together with group_lasso_reg > 0 instead of being silently dropped. - The zeroing threshold is lambda * sqrt(d), with d the row's width, and the shrink factor is 1 - lambda * sqrt(d) / ||z||_2. - New group_lasso_gamma (the paper's gamma_g, default 0) adds lambda * gamma to the learning rate's denominator; the step is per element, eta_i = 1 / ((beta + sqrt(n_i)) / lr + lambda * gamma [+ l2_reg]). group_lasso_gamma is last in OptimizerArgs, the batched tables signature and the C++ bindings (default 0), and checkpoints without it load as 0. The float64 test reference is rewritten to the paper's update; docs describe the update, the sqrt(d) scaling, and that a freshly initialized row is the most likely to be zeroed. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
jiashuy
force-pushed
the
feat/dynamicemb-ftrl-group-sparsity
branch
from
October 21, 2026 06:53
7214c6f to
41d1c11
Compare
…p lasso)
Allow l1_reg > 0 together with group_lasso_reg > 0 instead of rejecting
it. The linear term is soft-thresholded by l1_reg first and the row norm
is taken of that, as in Algorithm 3 of Yue et al., arXiv:2107.14432
("Adaptive Optimizers with Sparse Group Lasso for Neural Networks in CTR
Prediction"), which also gives G-FTRL its sqrt(d) group-size factor:
s_i = sign(z_i) * max(|z_i| - l1_reg, 0)
zero the row if ||s||_2 <= lambda * sqrt(d), else
w_i = -eta_i * (1 - lambda * sqrt(d) / ||s||_2) * s_i
With l1_reg = 0 s is z and the update is unchanged from the paper's closed
form (Ni et al., CIKM 2019, Eq. 3), so nothing changes for existing users.
The l1_reg/group_lasso_reg conflict checks in Python and C++ are removed.
Tests: the float64 reference takes the soft-threshold, the reference
comparison runs with and without l1_reg, and every dtype / mixed-width /
padded / flat case now carries a non-zero l1_reg. Docs describe the update.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…dent Drop group_lasso_gamma. It only added lambda * gamma to the denominator that l2_reg already feeds, so it was the same knob under another name; l2_reg = lambda * gamma reproduces it. l1_reg, l2_reg and group_lasso_reg are now the three independent regularization parameters. Document the contract they implement: each step rebuilds a row from its FTRL state as the solution of min_w z.w + 1/2 sum_i a_i w_i^2 + l1 |w|_1 + l2/2 |w|_2^2 + tau |w|_2 a_i = (beta + n_i^(-p)) / lr, tau = group_lasso_reg * sqrt(d) The row is zeroed exactly when ||s||_2 <= tau (s the L1-thresholded linear term); in a non-zero row the update uses the papers' common shrink factor, which is exact when every a_i + l2 in the row is equal and an approximation otherwise. The docs say so, give the L1 / group / L1+group / +L2 configurations, and note that the sqrt(d) factor is a scaling convention that does not give rows of different widths the same sparsity rate. Tests: the reference comparison is parameterized over l1_reg and l2_reg instead of gamma; the gamma validation and checkpoint-meta checks go. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…cument its surroundings Tests (no behaviour change): - multi-step against the float64 reference from a non-zero history, with rows that are zeroed and later brought back; - a run resumed from the optimizer's own meta matches an uninterrupted one bit for bit, so group_lasso_reg in the meta is enough to keep training with the group term; - d = 1 is a plain L1 threshold of l1_reg + group_lasso_reg; - a row exactly at, just above and just below the threshold, in fp32, fp16 and bf16, on the vec4 and scalar paths; - rows wider than a thread block (2048, 3001); - the objective's KKT conditions checked directly, independent of any transcription of the update, on rows whose curvature is shared (where the update is exact); - end to end: a key looked up several times in a step is updated once with the summed gradient. Docs: state that the group threshold is group_lasso_reg * sqrt(d) with d the row's real width and that this is fixed, the order gradients go through before the optimizer (clip, sum over repeated keys, cast), that only rows touched in a step are updated, that the learning rate is not applied retroactively to the accumulated state, that state shares the table's dtype and is rounded to it before the row norm is taken, and that group_lasso_reg in the checkpoint meta is what keeps a resumed run on the group term. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…p-sparse first step The first-step estimate linear = g - |g| * weight / lr only holds for the default learning_rate_power = -0.5 with initial_accumulator_value = 0. With power 0 the two accumulator powers cancel and linear = g whatever the initializer, so the advice on choosing group_lasso_reg from it did not apply. Give the general form, the three cases, and note that ftrl_beta does not enter linear. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…efore the step The threshold-boundary test read the buffer back after the in-place update and compared it with a second read of the same updated buffer, so it could not fail whatever the step did to linear and accum. Keep a copy of the state taken before the step and compare against that, exactly: a zero gradient leaves accum where it was and sigma is zero, so linear is unchanged too. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…oximation, not the exact solution The docs and kernel comment said each step rebuilds a row "as the solution of" the group-lasso objective, but the update uses the papers' common shrink factor, which is the exact minimizer only when every a_i + l2 in the row is equal. The zero-row test is exact either way. Say so: the objective is the reference, the update is the common-shrink form, and it is an approximation with no error bound otherwise. Add a worked example (z=[3,4], h=[1,2], tau=1: update [-2.4,-1.6], exact about [-2.207,-1.695]). Also: group_scale's comment names the L1-thresholded norm ||s||, not ||z||; the class docstring writes accum^(-p) with the default p = -0.5 instead of accum^0.5; the OptimizerArgs comment mentions the composition with l1_reg and l2_reg. Comments and docs only, no behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The update is the papers' common-shrink form, exact only when every h_i in a row is equal. Keep the KKT test for that case and say so in its name and docstring. Add a test for rows whose curvature differs: read the stored (w, z, n) back, without going through ftrl_step, and check the documented rule from them -- a row is zero exactly when ||s|| <= tau, otherwise h_i * w_i = -(1 - tau / ||s||) * s_i with one factor per row, elements L1 has zeroed stay zero, and the rest carry the sign opposite to s. Note in ftrl_step that its shrink factor is the common one. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
It re-derived the same formula ftrl_step already transcribes, so it could not catch anything the reference comparison does not, and its h-ratio and tolerance thresholds were never run. Keep the renamed KKT test, which is independent of ftrl_step and covers the exact case, and reword its docstring now that the test it pointed to is gone. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
It compared two runs of the same kernel path, group_lasso_reg unset and set to 0.0, which is the same default, so it could not fail on anything the reference comparison of plain FTRL does not already cover. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…meta test Dump and load are covered by the checkpoint tests, not by the FTRL optimizer tests, and the argument check it carried is a one-line comparison. The code it exercised is unchanged. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
It checked that a run rebuilt from the optimizer's own checkpoint meta matches an uninterrupted one. That is the dump/load path, covered by the checkpoint tests rather than the FTRL optimizer tests. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…reference test does not Drop four tests that restate what test_ftrl_group_sparsity_matches_reference already checks, or that test the reference rather than the kernel: - multistep reactivation: the state update is shared with plain FTRL, so more steps add no group-specific logic; - d = 1 as a larger L1: a special case of the same formula; - the KKT check under shared curvature: only the equal-curvature case; - the reference function's own zero-state handling. What stays covers the flat entry point, fp16/bf16 and mixed dims, the block-per-row path (dim 2048, 3001), the threshold boundary, and a real backward with repeated keys. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…RL in one test The group variant was the same test with group_lasso_reg set. Parametrize test_ftrl_flat_table_matches_padded_buffer over group_lasso_reg instead and drop the copy. The plain cases are unchanged. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…drop the duplicate-key copy The duplicate-key test repeated test_ftrl_backward_matches_reference's table setup and export comparison, and what it added -- summing a key's gradients before the optimizer sees them -- is done in reduce_grads for every optimizer, not by the group kernel. Add a group_lasso_reg case (with l1_reg and l2_reg) to the end-to-end reference test and drop the copy. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…essing to the padded-buffer tests The flat-table variant ran the same reference comparison through the other entry point; flat and padded giving identical results is already checked in test_ftrl_flat_table_matches_padded_buffer, and both call the same group kernel. Drop it. In the padded-buffer variant, drop the single-width cases (the reference test covers them in fp32) and the sentinel/padding checks: where a row's states sit in a padded buffer is test_padded_buffer_optimizer.py's job. What stays is what only the group path has: the threshold uses each row's own width, and the state is rounded to the table dtype before the row norm. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Collaborator
Author
|
I removed some tests that were already covered by existing ones—for instance, backward tests(reduce_grads) that verify handling of duplicate keys, and dump/load tests that ensure state consistency before and after the operations. |
Collaborator
Author
|
optimizer tests passed for FTRL group sparsity |
shijieliu
approved these changes
Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Scope
For each embedding row in DynamicEmb, support is provided for FTRL state accumulation, as well as composable L1, L2, and Group Lasso regularization.
This PR adds support for Group Lasso regularization.
Details
1. Synmbol
An embedding row is a group.
Notation/Symbol:
If λG > 0, follow the logic for group sparsity; otherwise, fall back to the general FTRL implementation.
2. General FTRL
Step 1 update optimizer states using old weights
Step 2 update weights using updated optimizer states
3. FTRL with group sparsity
Step 1 update optimizer states using old weights ( the same with general FTRL)
Step 2 First, apply L1 soft-thresholding to each element, then calculate the norm of the entire row.
Step 3 update weigths
Checklist