Early exit and LayerSkip¶
At a glance
Self-speculative early exit: draft tokens run the model's first r layers and exit there.
| Registered name | Family | Saves | Lossless | Trainable | Configuration |
|---|---|---|---|---|---|
early_exit |
drafting | latency | yes | readout, path | EarlyExitConfig |
Backends (default configuration): transformers: real · gguf: no · mlx: no · onnx: no · sglang: real · trtllm: no · vllm: real
An early-exit drafter drafts with the first exit_layer layers of the model. A draft step runs layers 1..r on
the last token and reads the next token's logits at layer r, either through a small trained readout adapter
followed by the model's final norm and LM head (head="readout"), or through the final norm and LM head alone
(head="shared"). A draft therefore costs r of the model's L layers and no separate network. The model verifies
the drafts in its next forward pass and keeps the longest prefix it agrees with, so greedy decoding is lossless.
What is trained depends on the configuration: the readout adapter, LoRA updates of layers 1..r
(adaptation="lora"), or trainable copies of these layers that only the drafter uses (adaptation="full"). The
model's own weights stay unchanged.
LayerSkip (Elhoushi et al., 2024) is the variant with the shared head. It fine-tunes the model itself with layer
dropout and an early-exit loss so that its intermediate layers predict tokens well through the final head, and then
drafts with an early exit of the fine-tuned model. economize trains this with the layerskip objective. Unlike the
paper, which reuses the draft's K/V of layers 1..r during verification, economize recomputes every drafted position
with the full model in the next exact pass.
Setup¶
Every tutorial starts from the same model and prompt:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import economize
device = "cuda" if torch.cuda.is_available() else "cpu"
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B", dtype=torch.bfloat16, device_map=device)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")
ids = tokenizer("Natalia sold clips to 48 of her friends in April, and then she sold half as many clips in May. "
"How many clips did Natalia sell altogether?", return_tensors="pt").input_ids.to(device)
Training data is a flat file of uint32 token ids, the format of nanoGPT-style pretraining shards, and calibration
data is a [N, S] tensor of token ids. Both are made here from GSM8K:
import numpy as np
from datasets import load_dataset
texts = [f"{row['question']}\n{row['answer']}" for row in load_dataset("openai/gsm8k", "main", split="train")]
tokens = np.concatenate([tokenizer(t + tokenizer.eos_token).input_ids for t in texts]).astype(np.uint32)
tokens[: len(tokens) * 9 // 10].tofile("train.bin")
tokens[len(tokens) * 9 // 10 :].tofile("val.bin")
calibration = torch.from_numpy(tokens[: 128 * 2048].astype(np.int64)).view(128, 2048)
Quick start¶
An exit after layer 8 of the 24 layers of Qwen2.5-0.5B, with a new readout adapter, attaches with economize.apply:
from economize.techniques.early_exit import EarlyExitConfig
model = economize.apply(base, EarlyExitConfig(exit_layer=8), decoding=economize.DecodeConfig(cycle_length=4))
result = model.decode(ids[0], 64)
print(tokenizer.decode(result.tokens))
print(result.mean_accepted) # accepted drafts per verification
print(result.cost_summary()) # exact passes, draft steps and layer-tokens of both
cycle_length=4 drafts three tokens per verification. The output is the model's greedy output whatever the
drafter proposes, since every token is verified. model.generate takes the usual transformers arguments:
out = model.generate(ids, max_new_tokens=64)
Training¶
fit trains the readout adapter (and the LoRA updates or layer copies, if configured) on the frozen model. The
default objective is early_exit_kl, the KL divergence from the model's next-token distribution to the exit's:
from economize.train import fit, fixed_windows
result = fit(model, "train.bin", steps=2000, batch_size=8, seq_len=1024, seed=0,
eval_data=fixed_windows("val.bin", 64, 1024), output_dir="runs/exit8")
print(result.eval)
LoRA on the draft layers gives the exit more capacity than the readout alone. EarlyExitKL also takes a
cross-entropy term on the data:
from economize.techniques.early_exit import EarlyExitKL
model.detach()
model = economize.apply(base, EarlyExitConfig(exit_layer=12, adaptation="lora", lora_rank=16),
decoding=economize.DecodeConfig(cycle_length=4))
fit(model, "train.bin", EarlyExitKL(divergence="kl", ce_weight=0.1), steps=2000, batch_size=8, seq_len=1024)
Saving and loading¶
model.save_pretrained("exit12_lora")
model.detach()
model = economize.load(base, "exit12_lora", decoding=economize.DecodeConfig(cycle_length=4))
Decoding options¶
min_confidence ends a cycle early when the drafter's top probability falls below the threshold, which saves
draft steps that would likely be rejected. Sampling stays lossless with the rejection verifier:
model.decode(ids[0], 64, decoding=economize.DecodeConfig(cycle_length=6, min_confidence=0.5))
sampling = economize.DecodeConfig(cycle_length=4, verifier="rejection")
sampled = model.generate(ids, max_new_tokens=64, do_sample=True, temperature=0.7, top_p=0.9, decoding=sampling)
LayerSkip¶
The shared head without adaptation adds no parameters, so the layerskip objective trains the base model instead.
It skips layer l with a probability that grows exponentially to dropout_max at the last layer and adds the
cross-entropy of the enabled exits with the paper's weights. The rotational curriculum enables every rotation-th
exit per step:
from economize.techniques.early_exit import LayerSkip
model.detach()
model = economize.apply(base, EarlyExitConfig(exit_layer=8, head="shared"),
decoding=economize.DecodeConfig(cycle_length=4))
fit(model, "train.bin", LayerSkip(exit_scale=0.1, dropout_max=0.1, curriculum="rotational", rotation=4),
steps=2000, batch_size=8, seq_len=1024, output_dir="runs/layerskip")
result = model.decode(ids[0], 64)
The objective trains the base weights in place (or the PEFT adapters of a PEFT model). The run writes them to
runs/layerskip/final/base_parameters.safetensors and a plain transformers copy to runs/layerskip/final/base/,
which loads with economize as economize.load(AutoModelForCausalLM.from_pretrained("runs/layerskip/final/base"),
"runs/layerskip/final"). Training describes these files.
Combining with other techniques¶
An early exit composes with every technique that is not itself a drafter, for example int8 weights and a 4-bit KV cache:
from economize.techniques.kv_quant.kivi import KIVIConfig
from economize.techniques.quant import QuantConfig
print(economize.compatible("early_exit", "quant"), economize.compatible("early_exit", "eagle3")) # True False
model.detach()
model = economize.apply(
AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B", dtype=torch.bfloat16, device_map="cuda"),
QuantConfig(format="int8"),
KIVIConfig(bits=4),
EarlyExitConfig(exit_layer=8),
calibration=calibration,
decoding=economize.DecodeConfig(cycle_length=4),
)
Serving¶
vLLM and SGLang serve every exit configuration through economize's plugins. The draft model shares layers 1..r
with the served model (or holds their LoRA updates or copies) and ends in the readout:
from economize.backends.vllm import serving
plan = serving.to_vllm(model, "out/qwen-early-exit", num_speculative_tokens=3)
llm = plan.llm()
Configuration¶
EarlyExitConfig
dataclass
¶
EarlyExitConfig(exit_layer: int, head: str = 'readout', adaptation: str = 'frozen', lora_rank: int = 16, lora_alpha: float | None = None, copy_dtype: str = 'float32')
Hyperparameters of an early-exit drafter.
A draft step runs the model's first exit_layer (r) layers on the last token and reads the next-token
logits there. head="shared" with adaptation="frozen" is the self-speculative decoding of LayerSkip
(Elhoushi et al., 2024), whose base model is trained with the layerskip objective so that its intermediate
layers can exit. head="readout" adds a trainable readout adapter before the model's norm and head
(Kangaroo's adapter plays the same role).
Attributes:
| Name | Type | Description |
|---|---|---|
exit_layer |
int
|
|
head |
str
|
|
adaptation |
str
|
|
lora_rank |
int
|
Rank of the LoRA updates. |
lora_alpha |
float | None
|
LoRA scaling numerator (default |
copy_dtype |
str
|
Storage dtype of the trainable copies ( |
References¶
- Elhoushi, M., Shrivastava, A., Liskovich, D., Hosmer, B., Wasti, B., Lai, L., Mahmoud, A., Acun, B., Agarwal, S., Roman, A., Aly, A. A., Chen, B., & Wu, C.-J. (2024). LayerSkip: Enabling early exit inference and self-speculative decoding. In Annual Meeting of the Association for Computational Linguistics. https://doi.org/10.48550/arXiv.2404.16710