Optimizer Techniques for Long-Context LLM Training: From SGD to Matrix-Level Updates

    This article explains why optimizers become a bottleneck when LLM context length grows from thousands to millions of tokens, and walks through the technical progression from plain SGD to momentum, adaptive learning rates, and matrix-level methods such as Muon. It covers the three core problems optimizers must solve, the practical constraints of orthogonalized updates, and key engineering practices for long-context training, including layer-wise optimizer design, learning rate scheduling, memory budgeting, numerical stability, observability, and experimentation.

    1. Background: Why Long Contexts Make the Optimizer the Bottleneck

    When the context length of a model grows from a few thousand tokens to the million-token range, training dynamics change noticeably. The sequence dimension of each sample becomes much longer, the memory and communication cost of attention rises, gradients become less evenly distributed across time steps and feature dimensions, and the loss surface develops more narrow, valley-shaped structures. At this point, the optimizer is no longer just an auxiliary component that “pushes parameters downhill.” It directly determines whether training can converge within an acceptable cost.

    The optimizer is often described as the navigation strategy of the training process. Consider an intuitive analogy: you are blindfolded and placed in an undulating mountain range, and your goal is to reach the lowest valley. The loss function tells you your current altitude, the gradient (obtained from backpropagation) tells you which way is downhill, and the optimizer decides two things: the direction of each step and the step size. Direction determines whether you oscillate against walls, and step size determines convergence speed and stability. Missing any one of the three causes training to fail.

    2. Three Core Problems the Optimizer Must Solve

    2.1 Step Size Adaptation: Learning Rate Control

    If the step is too large, parameters overshoot the valley and oscillate back and forth on the opposite slope, showing up as loss spikes, sudden increases in gradient norm, or even NaN. If the step is too small, the training cycle is stretched out and the compute cost becomes unacceptable. An ideal optimizer should accelerate in flat regions and slow down in steep regions or near the valley bottom. This capability is usually delivered by learning rate scheduling together with adaptive scaling.

    2.2 Escaping Local Minima and Saddle Points

    The loss surface contains many small pits halfway up the slope and saddle points where the gradient is close to zero. Relying only on the instantaneous gradient direction makes it easy to misjudge that the global optimum has been reached. Introducing historical information (momentum) or introducing randomness (mini-batch sampling noise, parameter perturbation) are two common escape mechanisms.

    2.3 Matching the Structure of the Update

    Traditional element-wise updates treat a parameter matrix as a pile of independent scalars and ignore the spectral structure of the weight matrix. Matrix-level optimization instead treats the update as a whole matrix and applies orthogonalization or spectral normalization to it, making the scale of the update more balanced across directions and therefore allowing a larger global learning rate.

    3. Technical Progression: From Stochastic Gradient to Matrix-Level Updates

    3.1 Plain Stochastic Gradient Descent

    The idea can be traced back to 19th-century research on finding extrema. Its physical picture is a “massless particle” sliding on a surface: it can start and stop instantly, which clearly violates the intuition of inertia and explains its inefficiency in narrow terrain, where it repeatedly hits the walls.

    3.2 Momentum: Giving the Particle Mass

    In the 1960s, the Soviet mathematician Boris Polyak proposed the Heavy Ball Method while studying optimization theory, and rigorously proved that on strongly convex quadratic problems with a large condition number, adding an inertia term can significantly improve the convergence rate. In engineering terms, momentum takes an exponential moving average of historical gradients, which is equivalent to “velocity”:

    1. # Pseudocode: momentum update
    2. v = beta * v + grad # velocity accumulation, beta typically 0.9-0.99
    3. param -= lr * v

    The intuitive effect is that the back-and-forth components in the vertical direction cancel each other out, while the component along the valley direction is accumulated and amplified. Oscillation is reduced and forward progress speeds up.

    3.3 Adaptive Learning Rates: Scaling by Each Parameter’s Own History

    Later approaches maintain second-order gradient statistics for each parameter and normalize the current gradient by their square root, achieving “a different step size for different parameters.” These methods are notably robust in scenarios with sparse gradients and heavy noise, at the cost of maintaining an extra state of the same order as the parameter count, which increases memory usage.

    3.4 Matrix-Level Optimizers: Treating the Update as a Matrix

    The Muon family of optimizers, which has recently drawn attention, follows this route. Its core operation is approximate orthogonalization of the momentum update for each weight matrix (commonly a polynomial approximation using Newton-Schulz iterations), which drives the singular values of the update matrix toward being uniform. The direct benefits are that the update is balanced in scale across directions, a larger learning rate can be used than with element-wise methods, and a better loss curve is achieved in the same number of training steps. Engineering constraints to note include: it mainly applies to two-dimensional weight matrices; embedding layers, normalization layers, biases, and gating scalars are usually still handled by adaptive methods; and the orthogonalization iterations add compute cost, so a trade-off between throughput and convergence is required.

    Illustrative implementation (structure only, not a specific framework API):

    1. def orthogonalize(G, steps=5):
    2. # Normalize first to avoid numerical overflow during iteration
    3. G = G / (G.norm() + 1e-7)
    4. X = G
    5. for _ in range(steps):
    6. # Newton-Schulz polynomial iteration for approximate orthogonalization
    7. X = a * X + b * (X @ X.T @ X) + c * (X @ X.T @ X @ X.T @ X)
    8. return X
    9. # Maintain momentum separately for each weight matrix
    10. buf = beta * buf + grad
    11. update = orthogonalize(buf)
    12. param -= lr * update

    4. Practical Guidelines for Long-Context Scenarios

    1. Design the optimizer in layers: use matrix-level methods for matrix parameters and adaptive methods for vector and scalar parameters, avoiding the scale mismatch caused by handling everything uniformly.
    2. Couple the learning rate with the global batch size: matrix-level updates allow a larger step, but they need warmup and cosine or linear decay to prevent early gradient noise from being amplified.
    3. Account for the memory budget: first-order momentum, second-order statistics, and intermediate tensors from orthogonalization all consume memory. Under long sequences, plan them together with techniques such as activation recomputation and sequence parallelism.
    4. Numerical stability: normalization of the norm must be done before orthogonalization iterations, and under mixed precision, monitor the ratio of gradient norm to update norm.
    5. Build observability: bring loss, gradient norm, update norm, learning rate, and the parameter distribution of each layer into logging and alerting. When a spike appears, roll back to a checkpoint first rather than blindly tuning hyperparameters.
    6. Experimentation method: run ablations at small scale with short context first, confirm the optimizer combination works, and only then scale up to long context to avoid costly trial and error.

    5. Conclusion

    The evolution of optimizers is essentially a main line of “introducing more reasonable priors”: momentum introduces an inertia prior, adaptive methods introduce a per-parameter scale prior, and matrix-level methods introduce a weight spectral structure prior. For ultra-long-context training, the choice among them depends on the balance of three factors: convergence quality, memory and compute budget, and implementation complexity. With general-purpose capabilities such as the container platform, logging service, and monitoring and alerting offered by platforms like Baidu AI Cloud, teams can build a reproducible training experiment loop faster and turn optimizer selection from experience-based judgment into a data-driven engineering decision.