Key Techniques for Long-Context and Sparse MoE Training

    This article explains two techniques for training large language models with long contexts and sparse mixture-of-experts (MoE) architectures. The first keeps chunked linear attention numerically stable with bounded decay. The second balances expert load without an auxiliary loss, using routing bias adjustment. It also covers the trade-offs of each method and the engineering checks to run during training.

    1. Background: Shared Challenges of Long Context and Sparse Experts

    Large language models are moving toward long contexts and sparse expert architectures for two main reasons. First, agent applications need to process hundreds of thousands, or even millions, of tokens of history within a single task, such as code repositories, long documents, and records of multi-turn tool calls. Second, mixture-of-experts (MoE) architectures keep the compute per inference step under control as parameter counts grow. However, as the number of experts increases, routing imbalance and training instability also grow.

    The compute cost of standard attention grows quadratically with sequence length, so the bottleneck in long-sequence training first appears in the attention operator. Linear attention reduces complexity to approximately linear in sequence length, but chunked parallel implementations run into numerical problems. The following three sections discuss three groups of techniques, each targeting a different bottleneck.

    2. Numerical Stability of Linear Attention

    2.1 The Reciprocal Problem in Chunked Computation

    Linear attention is usually computed in chunks. Within a chunk, the computation requires the reciprocal of the cumulative decay factor. If the log decay has no lower bound, the cumulative product can become extremely small, and its reciprocal becomes extremely large. Low-precision formats such as BF16 have a limited representable range, so results can easily overflow or underflow. To avoid this, implementers often use a separate branch that handles position pairs one by one for diagonal blocks. This prevents all blocks from using the dense matrix multiplication path, which reduces computational efficiency.

    2.2 Bounded Decay Parameterization

    One approach is to constrain the log decay to a finite interval. Let g_min be a finite negative number. The log decay can then be written as follows:

    1. # Illustrative code: bounded log decay
    2. def log_decay(x, g_min=-5.0):
    3. return g_min * sigmoid(x) # result lies within (g_min, 0)

    The decay factor exp(g) then falls within (exp(g_min), 1). With g_min = -5, the single-step decay is no less than about 0.0067. Within a 16-token chunk, the minimum cumulative decay is about exp(-80), and its reciprocal is about exp(80). Both values fall within the exponent range of BF16, so in-chunk computation does not fail due to overflow or underflow.

    This constraint also has a cost. The decay cannot approach 0, so the model cannot represent a state of complete forgetting. The value of g_min must be balanced between numerical stability and expressive capacity, and it is usually determined through ablation experiments.

    2.3 Benefits and Validation

    With bounded decay, the scaling factor range of each block is controlled. Diagonal and off-diagonal blocks can then use the same dense matrix multiplication path, which removes the special branch. The throughput gain from this unification depends on the specific operator implementation and hardware platform, and this article does not provide throughput figures. Whether bounded decay helps length extrapolation in settings without positional encoding must be verified with long-context evaluations. Theoretical reasoning alone is not enough.

    3. Auxiliary-Loss-Free Expert Load Balancing

    3.1 Side Effects of Auxiliary Losses

    Traditional approaches add a load-balancing term to the training loss to encourage each expert to receive roughly equal numbers of tokens. This term competes with the language modeling objective for gradients. If its weight is too large, model quality drops. If its weight is too small, some experts degrade and traffic concentrates on a few experts.

    3.2 Quantile-Based Bias Adjustment

    Another approach removes load balancing from the loss and instead adds a dynamically updated bias to the routing scores. At each training step, the load of each expert is measured, and the bias is adjusted relative to a target quantile:

    1. # Illustrative code: quantile-based routing bias update
    2. def update_routing_bias(bias, load, step_size=1e-3, q=0.5):
    3. # load[i]: share of tokens received by expert i in this step
    4. target = quantile(load, q) # use the quantile as the reference
    5. for i in range(len(bias)):
    6. if load[i] > target:
    7. bias[i] -= step_size # overloaded expert: lower its score
    8. else:
    9. bias[i] += step_size # underloaded expert: raise its score
    10. return bias

    The bias affects only the selection of top-k experts and does not participate in the loss computation, so it does not directly change the gradient direction of language modeling. The quantile, step size, and update frequency must be tuned according to the number of experts and the batch size. A step size that is too large causes routing oscillation, while one that is too small leads to slow convergence.

    3.3 Engineering Checklist

    • Monitor per-expert load histograms, not just average load.
    • If capacity limits are used, record the dropped