Low-Level Optimization for Large Language Models: Attention, MoE Load Balancing, and Residual Connections
This article covers three architecture-level optimizations for large language models: chunked linear attention, load balancing in Mixture-of-Experts (MoE) routing, and residual connections in deep networks. For each one, it explains the problem being solved, key implementation details, how to measure gains, and the conditions under which the method may fail. The formulas and code are illustrative sketches based on method descriptions and should be checked against the original papers or official implementations before use.
The parameter counts and context lengths of large language models keep growing. As models get larger, compute utilization and the cost of training and inference more directly determine whether a project can move forward. Architecture changes usually focus on three areas: attention computation, routing and load in Mixture-of-Experts (MoE) layers, and residual connections in deep networks. This article discusses each direction in turn, covering the problem each approach solves, key implementation points, how to measure gains, and the conditions under which it may fail. The formulas and code are illustrative versions compiled from method descriptions. Check them against the original paper or official implementation before use.
1. Bounded Decay Parameterization: Making Chunked Linear Attention Numerically Controllable
1.1 The Problem
Linear attention removes the softmax, so the computation along the sequence dimension can be written in recurrent form. During training, the sequence is usually split into chunks. Within each chunk, computation runs in parallel. Between chunks, a recurrence passes the state forward. Each position carries a decay factor, and these factors accumulate within a chunk. Let gi be the log-decay at position i, and let the cumulative decay within a chunk be Γ_t = exp(Σ{i≤t} g_i). Chunked algorithms often need the reciprocal of Γ to align contributions between different positions in the chunk.
This computation has two problems. First, the stronger the decay, the closer Γ_t gets to zero. Its reciprocal then becomes very large and may fall outside the representable range of floating-point numbers. Second, the reciprocal requires a separate element-wise path and cannot be written directly as a dense matrix multiplication. Tensor Cores are efficient at regular GEMM operations. When diagonal blocks need separate handling, implementers either add extra branches or lose throughput. As a result, CUDA and CUTLASS kernels must maintain two sets of logic, which raises development and debugging costs.
BF16 has the same exponent width as FP32, so its numerical range is not narrow. Overflow and underflow mainly occur when chunks are long and decay is strong. BF16 precision itself does not cause them.
1.2 Bounded Decay Parameterization
The original method replaces the unbounded Negative-Softplus with a bounded, scaled sigmoid:
g_t^h = g_min · σ(a_t^h)
Here a_t^h is the gate pre-activation for head h, obtained by a linear projection of the input x_t. σ is the sigmoid function, and g_min is a negative constant lower bound, for example -5. Because σ(·) takes values in (0, 1), g_t^h is confined to (g_min, 0).
This constraint sets bounds on the values within a chunk. For C positions, the cumulative log-decay satisfies Σg ≥ C·g_min. The lower bound of Γ is therefore exp(C·g_min), and the upper bound of its reciprocal is exp(C·|g_min|). With g_min = -5 and C = 16, the log lower bound is -80. The lower bound of Γ is about 1.8×10^-35, and the upper bound of its reciprocal is about 5.5×10^34. The maximum finite value of BF16 is about 3.4×10^38, so the reciprocal does not overflow.
This gives one constraint: C·|g_min| must stay below about 88, which is ln(3.4×10^38). Above this value, the reciprocal may overflow, or Γ may underflow to zero. With g_min = -5, a chunk length above 17 can exceed the limit. A 32-token chunk therefore does not satisfy the condition, while a 16-token tile does. In practice, recalculate this constraint using the actual g_min and chunk length.
g_min is also a trade-off parameter. Because g lies in (g_min, 0), the per-step retention exp(g) is higher than exp(g_min). A larger |g_min| lets the model forget more in a single step, but it lowers the maximum chunk length and leaves less numerical headroom. If |g_min| is too small, for example 1, the retention is at least 0.37. The model then struggles to discard irrelevant history quickly.
The code below sketches the numerical path. It omits the delta rule update and shows only how cumulative decay and its reciprocal are handled:
```
Input: q, k: [C, d], v: [C, d], g: [C], with g_min < g < 0
logG = cumsum(g) # cumulative log-decay within the chunk
G = exp(logG) # Γ_t, lower bound exp(C * g_min)
G_inv = exp(-logG) # reciproc
