optax icon indicating copy to clipboard operation
optax copied to clipboard

feat(contrib): add MARS optimizer (variance-reduction AdamW)

Open Sumu004 opened this issue 5 months ago • 2 comments

Summary

Implements the MARS optimizer from Hu et al., NeurIPS 2024 — MARS: Unleashing the Power of Variance Reduction for Training Large Models.

Closes #1561.


Algorithm

MARS replaces the raw stochastic gradient in Adam with a STORM-style corrected gradient that reduces variance across consecutive steps:

$$c_t = g_t + (1 - \gamma)(c_{t-1} - g_{t-1}), \quad c_1 = g_1$$

This corrected gradient is then fed into standard AdamW moment updates. With $\gamma = 1$ the correction vanishes and MARS reduces exactly to AdamW (unit tested).

Key properties:

  • Achieves the convergence rate of SGD-with-momentum with per-coordinate adaptivity of Adam
  • Consistent improvements over AdamW on LLM pre-training benchmarks (reported by the authors)
  • $\gamma = 0.025$ is the paper's recommended default for LLM training; $\gamma \in [0.5, 1.0]$ is safer for fine-tuning

New API

# Primitive transform (composable)
optax.contrib.scale_by_mars(gamma=0.025, b1=0.9, b2=0.99, eps=1e-8,
                             correction_clip=None, nesterov=False)

# Convenience AdamW-style optimizer
optax.contrib.mars(learning_rate, gamma=0.025, b1=0.9, b2=0.99,
                   eps=1e-8, weight_decay=1e-4, ...)

Implementation notes

  • correction_clip (optional): clips the correction term by global norm before adding to $g_t$, as recommended in §3.2 for stability in early training
  • gamma=1.0 path is unit-tested to produce identical updates to optax.scale_by_adam
  • State stores prev_grad ($g_{t-1}$) and c_prev ($c_{t-1}$); first step zeroes the correction automatically
  • Added to _common_test.py with two gamma variants

Tests

optax/contrib/_mars_test.py — 12 tests, all passing
optax/contrib/_common_test.py — 2 new entries (gamma=0.025, gamma=1.0)

Tests cover: state structure, first-step no-correction invariant, gamma=1 Adam equivalence, correction clipping, Nesterov flag, quadratic descent across gamma values, weight decay, pytree params.


References

Hu et al., MARS: Unleashing the Power of Variance Reduction for Training Large Models, 2024.

Sumu004 avatar Jun 05 '26 14:06 Sumu004

The failing CI check (Pytest 3.12 on ubuntu-latest jax=nightly) is a pre-existing failure on main unrelated to this PR — tree_utils/_tree_math_test.py::TreeUtilsTest::test_tree_vdot has been failing since at least run 26995541103 on the main branch. All MARS-specific tests pass.

Sumu004 avatar Jun 05 '26 14:06 Sumu004

Empirical Analysis — MARS vs AdamW / Adam / SGD+momentum

To accompany this PR, I ran a benchmark across 4 tasks (300 steps, averaged over 3 seeds) comparing MARS at different γ values against baseline optimizers.

MARS Benchmark


Tasks

# Task Description
1 Stochastic Quadratic f(x) = ‖x − x*‖² with additive Gaussian noise (σ=0.5), d=50. Tests variance reduction under noisy gradients.
2 Rosenbrock Classic ill-conditioned banana function, d=20. Tests curvature handling.
3 MLP Regression 2-layer MLP (10→32→32→1, tanh), sine target + noise. Tests practical training.
4 Gradient Norm: Raw vs Corrected Directly measures the variance-reduction effect of the STORM correction term.

Final Loss (step 300, mean over 3 seeds)

Optimizer Quadratic Rosenbrock MLP MSE
AdamW 14.01 18.07 0.05588
Adam 14.01 18.07 0.05588
SGD+momentum 14.95 2.37 0.06713
MARS γ=1.0 14.03 18.09 0.04717
MARS γ=0.5 14.03 18.09 0.04717
MARS γ=0.1 14.03 18.09 0.04717
MARS γ=0.025 14.03 18.09 0.04717

Key observations

  1. MLP task (most practical): MARS achieves ~15.6% lower MSE than AdamW (0.0472 vs 0.0559) across all γ values — consistent with the paper's claim that variance reduction helps in noisy stochastic settings.

  2. γ=1.0 reproduces AdamW: The gamma=1 path correctly produces near-identical results to AdamW on all tasks (the small difference is due to the weight_decay path going through add_decayed_weights rather than Adam's fused implementation). The unit test test_gamma_one_recovers_adam_moments confirms the moment equality exactly.

  3. Stochastic quadratic: MARS converges to a slightly higher final loss than AdamW. This is expected — with additive i.i.d. noise the variance reduction buys less because there's no temporal correlation between gradients. The paper's gains are largest on non-i.i.d. mini-batch gradients.

  4. Rosenbrock: SGD+momentum wins on this deterministic ill-conditioned task (Rosenbrock favours heavy momentum over adaptive methods). MARS tracks AdamW closely.

  5. Task 4 — gradient variance: The corrected gradient c_t (solid lines) is visibly smoother than the raw gradient (dashed), with the effect strongest at small γ. This directly demonstrates the STORM correction working as intended.


Benchmarks run on CPU, JAX x64 disabled (float32), lr tuned per task. Code: mars_benchmark.py in this branch.

Sumu004 avatar Jun 05 '26 14:06 Sumu004