Abstract
Modern Large Language Models (LLMs) increasingly rely on Gated Linear Units (GLUs) within their Feed-Forward Networks (FFNs) to achieve superior expressivity. However, standard GLU implementations impose a significant "memory tax" by requiring the loading of two distinct weight matrices, which exacerbates the memory-bandwidth bottleneck during inference. We present the Masked Gated Linear Unit (MGLU), a novel architecture that utilizes a single shared weight matrix and learnable binary masks to compute gating and value streams. When paired with our optimized FlashMGLU kernel, this approach achieves up to a 19.7x inference speedup over naive implementations and a 34% improvement over standard GLU kernels, while maintaining accuracy parity with state-of-the-art models.
Introduction
In the Transformer architecture, the Feed-Forward Network (FFN) typically accounts for a vast majority of the model's total parameters. Recent state-of-the-art models, such as Llama 3, have transitioned to using Gated Linear Units—specifically the SwiGLU variant—due to their improved performance on complex linguistic tasks.
Despite their mathematical advantages, GLUs are computationally expensive in memory-bound regimes. Standard GLUs require two separate weight matrices ($W_g$ for the gate and $W_v$ for the value) to be transferred from High-Bandwidth Memory (HBM) to the GPU's SRAM for every token generated. Because LLM inference is primarily limited by data movement rather than raw floating-point operations (FLOPs), this doubling of memory reads directly increases latency and energy consumption. The Masked Gated Linear Unit (MGLU) is designed to eliminate this redundancy by "recycling" weights through element-wise masking.
Methods
Mixture of Element-wise Gating (MoEG)
The core innovation of MGLU is the Mixture of Element-wise Gating (MoEG). Instead of maintaining two full-rank matrices, MGLU employs a single shared weight matrix $W$. To differentiate the gate and value streams, the model applies learnable binary masks ($M$). In its fundamental configuration, the gate stream utilizes $W \odot M$ while the value stream utilizes $W \odot (1-M)$. This complementarity ensures that the model can compute two distinct projections from a single memory load of the weight matrix.
Learnable Binary Masks
Unlike static pruning or random masking, MGLU masks are co-optimized with the weights during the training process. To overcome the non-differentiable nature of binary values, the authors utilize a Straight-Through Estimator (STE). This allows the network to adaptively allocate capacity; for instance, the model might learn to devote 45% of a layer's parameters to the gate and 55% to the value stream, a flexibility not present in standard GLUs.
FlashMGLU Kernel
To translate these theoretical memory savings into wall-clock speedups, the authors developed FlashMGLU, a custom CUDA/Triton kernel. The kernel optimizes performance through several key strategies:
- Mask Packing: Multiple binary masks are packed into single 8-bit integers, minimizing the metadata overhead.
- Unified Loading: The shared weight matrix and the packed masks are retrieved in a single coalesced memory transaction.
- On-chip Computation: All gating and activation functions (such as Swish) are performed entirely within GPU registers before the final result is written back to memory, maximizing arithmetic intensity.
Experimental Evaluation
Training and Datasets
The researchers evaluated MGLU by pre-training models of varying scales. "Small" models (159M parameters) were trained on a 10B token subset of the FineWeb-Edu 100B dataset, while "large" models (1.08B parameters) utilized the full dataset. Performance was measured across six standard NLP benchmarks: ARC (Easy & Challenge), HellaSwag, PiQA, SciQ, and Winogrande.
Results
- Efficiency Gains: On an NVIDIA RTX 5090, FlashMGLU demonstrated a 34% increase in speed and a 47% reduction in memory overhead compared to standard GLU implementations. When compared to a naive PyTorch implementation of the MGLU logic, the custom kernel provided a staggering 19.66x speedup.
- Accuracy Parity: The SwiMGLU variant (utilizing 4 or 8 masks) matched or slightly exceeded the performance of the standard SwiGLU baseline across all downstream tasks. This suggests that the weight-sharing mechanism does not inherently limit the model's representational power.
- Scaling Behavior: The study found that as the number of masks ($n_m$) increases, the model's perplexity consistently improves, indicating that the element-level "Mixture of Experts" approach scales effectively with model size.
Discussion and Limitations
While MGLU offers significant advantages for inference, it introduces additional complexity during the training phase. The backward pass requires more FLOPs to calculate gradients through the masks, making training more computationally intensive than standard GLUs. Furthermore, the current FlashMGLU kernel has not yet been optimized for the latest hardware features of the NVIDIA Hopper (H100) architecture, such as the Tensor Memory Accelerator (TMA). Future iterations utilizing these features are expected to yield an additional 1.2–1.3x speedup.
Conclusion
The Masked Gated Linear Unit represents a significant step toward more efficient LLM architectures. By shifting the focus from reducing FLOPs to optimizing memory bandwidth usage, MGLU provides a practical path for deploying high-performance models on memory-constrained hardware, ranging from data-center GPUs to edge devices.
Summary updated