﻿# X-LoRA: token-level expert routing and the cost of a second forward pass

Understand how X-LoRA mixes frozen adapters using hidden-state-dependent scaling, and why a small trained gate does not imply cheap inference.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/guide-xlora-dual-pass-expert-routing/
Language: en

X-LoRA combines pretrained LoRA experts using learned scaling values that can vary across tokens and layers. Both the base model and the expert adapters can remain frozen while a classifier learns the mixture. This makes the routing state small relative to retraining every expert, but changes the computation required for an answer.

## Separate experts from the learned router

Start with compatible adapters for the same base architecture and revision. The router decides their contributions; it does not repair incompatible tensor shapes or invent missing expertise. For an illustrative system combining scientific and general-language adapters, check each expert independently before attributing a mixed answer to useful composition.

## Follow the documented dual pass

The PEFT explanation runs the base without adapter contributions to obtain hidden states, then derives scaling values and runs the adapted computation. That extra model work matters even when only a small classifier was trained. Measure prompt processing and generation separately rather than estimating serving cost from the router's parameter count. Runtime optimizations require evidence from the actual implementation.

## Understand dynamic composition

A single fixed weighted merge cannot generally represent input-dependent scaling. Different positions may call on different experts as context changes. Inspect routing traces alongside examples, but avoid treating a large scaling value as a causal explanation of a complete answer. Normalization, other layers and interactions between experts also affect the output.

## Train against a meaningful task mixture

Router training needs examples reflecting the situations where composition should help. Keep specialist-only, mixed-domain and unrelated questions in separate evaluation groups. Otherwise a strong aggregate score can hide a router that mostly chooses one expert. Compare the frozen base, individual adapters and the composed model using the same prompt and decoding rules.

## Package the entire expert inventory

Preserve classifier state, routing configuration, expert order, adapter revisions and the base identity. Reloading just the classifier is insufficient if expert names resolve to different weights. Test the deployed generation path, including cache handling and supported batching, before selecting this method for a service. The original scientific demonstrations establish a research setting; they do not guarantee that an arbitrary expert collection will generalize or meet a production latency target.

## Primary references

[PEFT X-LoRA](https://huggingface.co/docs/peft/package_reference/xlora).

[X-LoRA original paper](https://arxiv.org/abs/2402.07148).

[PEFT X-LoRA implementation](https://github.com/huggingface/peft/blob/main/src/peft/tuners/xlora/model.py).

Related explanations: [task identity in adapter composition](/journal/adapter-composition-task-identity/); [adapter serving capacity](/journal/lora-serving-capacity/).
