Inferensys

Glossary

Hard Routing

Hard routing is a deterministic routing paradigm in Mixture of Experts models where each input token is discretely assigned to one or more specific expert networks, as opposed to a weighted blend.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
MIXTURE OF EXPERTS INFERENCE

What is Hard Routing?

A core routing mechanism in Mixture of Experts (MoE) architectures that determines which specialized sub-networks process each input.

Hard routing is a paradigm in Mixture of Experts models where each input token is discretely assigned to a specific, sparse subset of experts. Unlike soft routing which blends expert outputs using continuous weights, hard routing uses a deterministic selection mechanism like top-k gating or argmax to send each token exclusively to the k experts with the highest router scores. This creates a conditional computational graph where only the selected experts' parameters are activated, enabling massive model scale with manageable inference cost.

The primary advantage of hard routing is computational sparsity, as it activates only a fraction of the model's total parameters per token. Key implementation challenges include load balancing to prevent expert underutilization and managing all-to-all communication overhead in expert parallelism. Systems like Switch Transformers (top-1) and Mixtral (top-2) employ hard routing, requiring optimized fused MoE kernels and careful capacity factor tuning to minimize dropped tokens and router latency during inference.

MIXTURE OF EXPERTS INFERENCE

Key Characteristics of Hard Routing

Hard routing is a deterministic, discrete assignment mechanism in Mixture of Experts (MoE) models. It contrasts with soft routing by enforcing sparse, non-fuzzy expert selection, which has distinct system-level implications.

01

Discrete Token Assignment

Hard routing makes a deterministic selection of one or more experts per token, typically via an argmax or top-k operation on the router's logits. This creates a sparse computational graph where only the weights of the selected experts are activated. For example, in a top-2 routing policy, each token's hidden state is processed by exactly two expert networks, and their outputs are combined (e.g., summed). This is fundamentally different from soft routing, where outputs might be blended using continuous attention-like scores.

02

Implementation via Top-k Gating

The most common hard routing mechanism is Top-k Gating. A lightweight gating network (router) computes a score for each expert. For each input token, the router selects the k experts with the highest scores. In practice, k is small (1 or 2) to maintain sparsity. The process is:

  • Compute router logits for the token.
  • Select top k experts.
  • Route the token's hidden state only to those experts.
  • Compute and combine the expert outputs. This ensures a fixed, predictable FLOPs cost per token, proportional to k, not the total number of experts.
03

System Efficiency & Challenges

Hard routing enables conditional computation, activating only a fraction of the model's total parameters. This allows for gigantic models (e.g., trillion-parameter) with feasible inference costs. However, it introduces significant system complexity:

  • Load Balancing: The router can develop favorites, causing some experts to be overloaded while others are idle. This is mitigated by auxiliary loss functions during training.
  • All-to-All Communication: In expert-parallel distributed training and inference, tokens must be scattered to the devices hosting their assigned experts and gathered afterward, creating a communication bottleneck.
  • Expert Capacity: Each expert has a fixed capacity (number of tokens it can process per batch). Tokens exceeding an expert's capacity are dropped, potentially harming model quality.
04

Expert Specialization

Through training, hard routing encourages token-expert affinity, where experts naturally specialize in processing specific types of tokens. For instance, in a language model, one expert might specialize in mathematical symbols, another in proper nouns, and another in syntactic function words. This specialization emerges because the router learns to assign semantically or syntactically similar tokens to the same subset of experts, making the overall model more compositional and efficient. The discrete nature of the assignment reinforces this specialization more sharply than soft routing.

05

Contrast with Soft Routing

Hard routing is defined in opposition to soft routing. Key differences:

  • Hard Routing: Discrete, sparse selection (e.g., top-k). Outputs are combined via a weighted sum based on the router scores for the selected experts only.
  • Soft Routing: Continuous, dense blending. All experts receive the token, and their outputs are combined using a full set of attention-like weights, resulting in a denser, more computationally expensive operation. Hard routing is preferred in large-scale MoE (e.g., Switch Transformer, Mixtral) for its computational sparsity. Soft routing is more common in older, smaller mixture-of-experts models and is analogous to multi-head attention.
06

Key Architectural Examples

Several landmark models exemplify hard routing:

  • Switch Transformer: Uses top-1 routing (Switch Routing), where each token is sent to exactly one expert. This maximizes sparsity and simplifies system design.
  • Mixtral 8x7B: Employs top-2 routing. Each layer has 8 expert FFNs, and each token is processed by 2, giving it the effective capacity of a ~47B parameter dense model for only ~13B active parameters.
  • GLaM (Google): A generalist language model using top-2 routing with a massive 1.2 trillion total parameters, demonstrating the scalability of the paradigm. These models rely on fused MoE kernels and optimized expert parallelism to make the routing and sparse computation efficient on GPU clusters.
MIXTURE OF EXPERTS INFERENCE

How Hard Routing Works: Mechanism and Implementation

A technical overview of the deterministic routing mechanism used in Mixture of Experts models, contrasting it with soft routing and detailing its core computational implementation.

Hard routing is a deterministic gating mechanism in Mixture of Experts (MoE) models where each input token is discretely assigned to one or a fixed number of top-scoring experts, as selected by a gating network (router). This contrasts with soft routing, where expert outputs are blended using continuous weights. The primary implementations are top-k gating, where the router selects the k experts with the highest scores, and its special case top-1 (or Switch) routing, where each token is sent to exactly one expert. This discrete assignment creates a sparse computational graph, activating only a subset of the model's total parameters per token.

Implementation requires efficient handling of the all-to-all communication pattern in distributed settings, where tokens are scattered to devices hosting their assigned experts. Optimized fused MoE kernels combine routing logic and sparse matrix multiplication to minimize overhead. A critical hyperparameter is the expert capacity, which sets a limit on tokens per expert to enable batched computation; tokens exceeding an expert's capacity are dropped. This paradigm enables massive model scale with conditional computation, but introduces router latency and load balancing challenges during training.

HARD ROUTING IN PRACTICE

Examples and Real-World Applications

Hard routing is a foundational technique for conditional computation, enabling massive model scale with sparse activation. Its deterministic nature makes it critical for high-performance, predictable inference in production systems.

02

Mixtral 8x7B (Top-2 Routing)

Mistral AI's Mixtral 8x7B is a state-of-the-art open-source model that uses hard top-2 routing. For each token, the router selects the two highest-scoring experts out of eight per layer. Despite having 47B total parameters, it activates only about 13B parameters per token, matching the inference cost of a much smaller dense model. This architecture demonstrates how hard routing enables high-quality, cost-effective inference for open-weight LLMs.

  • Model Family: Mixtral 8x7B, Mixtral 8x22B
  • Routing Policy: Top-2
  • Key Benefit: Dense 13B model latency with 47B parameter knowledge.
05

Load Balancing via Auxiliary Loss

A core challenge in training hard-routed MoEs is load imbalance, where the router may collapse to always selecting the same few experts. To prevent this, an auxiliary load balancing loss is added during training. For top-2 routing, this loss encourages the mean routing probability across experts to be uniform and minimizes the squared coefficient of variation of the number of tokens assigned to each expert. This technique is critical for ensuring all 47B parameters in a model like Mixtral are effectively utilized.

06

Hard vs. Soft Routing in Production

The choice between hard and soft routing has direct engineering implications:

Hard Routing (e.g., Top-k)

  • Pros: Sparse, deterministic computation; efficient expert parallelism; lower memory bandwidth (no blending coefficients).
  • Cons: Discontinuous gradients; requires load balancing losses; risk of dropped tokens if expert capacity is exceeded.

Soft Routing (e.g., Mixture)

  • Pros: Continuous, differentiable operation; natural load balancing.
  • Cons: Dense computation (all experts are weighted); higher memory and compute cost; complex to scale.

In production LLM serving, hard routing dominates due to its superior computational efficiency and predictable latency.

ROUTING PARADIGMS

Hard Routing vs. Soft Routing: A Technical Comparison

A comparison of the two primary paradigms for distributing tokens to experts in a Mixture of Experts (MoE) architecture, focusing on implementation, performance, and system characteristics.

Feature / MetricHard RoutingSoft Routing

Routing Decision

Discrete (e.g., Top-k, Argmax)

Continuous (Weighted Blending)

Token Assignment

Each token assigned to 1 or k specific experts.

Each token's output is a weighted sum of all expert outputs.

Computational Sparsity

High. Only the selected experts' parameters are activated.

Low or None. All experts are computed, then outputs are blended.

Inference Throughput

Higher. Enables conditional computation and expert parallelism.

Lower. Requires full forward pass through all experts.

Router Latency Overhead

Low. Simple selection logic (e.g., sorting).

Higher. Requires computing scores for and blending from all experts.

Model Specialization

Encouraged. Experts can learn distinct, specialized functions.

Discouraged. Experts learn blended, generalized representations.

Training Stability

Requires careful tuning (load balancing, auxiliary loss).

More stable, akin to training a dense network.

Gradient Flow

Sparse. Gradients only flow to the selected experts per token.

Dense. Gradients flow to all experts for every token.

Memory Access Pattern

Sparse, irregular. Requires efficient all-to-all communication.

Dense, regular. Predictable memory access.

System Complexity

High. Requires expert parallelism, token permutation, capacity management.

Low. Similar to serving a standard dense model.

Representative Architectures

Switch Transformer, Mixtral, GShard

Early MoE models, Mixture-of-Soft-Experts variants

HARD ROUTING

Frequently Asked Questions

Hard routing is a fundamental paradigm in Mixture of Experts (MoE) models that determines how computational workload is distributed. These questions address its core mechanics, trade-offs, and implementation.

Hard routing is a deterministic, discrete assignment mechanism in a Mixture of Experts (MoE) model where each input token is routed to a specific, sparse subset of experts based on a selection function like top-k or argmax. Unlike soft routing, which blends expert outputs using continuous weights, hard routing activates only the chosen experts for computation, creating a conditional execution graph. This paradigm is central to achieving sparse activation, where a model can have a massive total parameter count (e.g., hundreds of billions) while only using a small fraction (e.g., 2 out of 8 experts per token) during any single forward pass, dramatically reducing computational cost.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.