Skip to content

GateSkip

At a glance

GateSkip: trained residual gates per layer, and the tokens with the lowest gates skip the layer.

Registered name Family Saves Lossless Trainable Configuration
gateskip dynamic-depth compute or latency no gates GateSkipConfig

Backends (default configuration): transformers: real · gguf: no · mlx: no · onnx: no · sglang: no · trtllm: no · vllm: no

GateSkip (Laitenberger et al., 2026) adds a learned residual gate to the layers of a model and lets the tokens with low gate values skip them. In economize every gated layer l gets one scalar gate g = sigmoid(w . rms(x) + b), computed from the layer's input x. A token that runs the layer leaves it as x + g (layer(x) - x), and a token that skips it leaves it as x, with its K/V at that layer computed from x. Since the gate is known before the layer runs, the decision costs one dot product per token and layer. The saving is the skipped layer applications.

This is a layer-level variant: the paper gates the attention and the MLP branch of each layer separately and skips branches. GateSkip is lossy. The gates scale every layer's update even when no token skips, so the output differs from the base model's from the start, and training keeps that difference small. Training updates the gates only, with the base model frozen.

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

The base model's greedy output is the reference for quality, so generate it before the technique is applied:

reference = base.generate(ids, max_new_tokens=64, do_sample=False)[0, ids.shape[1]:]

The gate bias starts at 5, so every gate starts near 0.993 and no token skips at the default threshold of 0.5. The gates have to be trained before they save anything:

from economize.techniques.dynamic_depth import GateSkipConfig, GateSkipObjective, token_mismatch
from economize.train import fit

model = economize.apply(base, GateSkipConfig(rule="threshold", threshold=0.5))
fit(model, "train.bin", objective=GateSkipObjective(sparsity=0.01), steps=2000, batch_size=8, seq_len=1024, seed=0)

result = model.decode(ids[0], 64)
print(tokenizer.decode(result.tokens))
summary = model.techniques["gateskip"].cost_summary(result)
print(token_mismatch(result.tokens, reference))      # fraction of positions that differ from the base model
print(summary["relative_effective_layer_tokens"])    # layer applications relative to plain decoding
print(summary["executed_fraction_per_layer"])        # fraction of the tokens that ran each layer

fit(model, "train.bin") without an objective uses GateSkipObjective() with its defaults.

Measuring quality and saving

token_mismatch is CALM's token-level consistency loss: the fraction of the positions of the longer generation at which the two generations differ, with a missing token counted as a difference. Greedy decoding compounds every early difference, so it is worth averaging it over prompts from the target workload. relative_effective_layer_tokens divides the layer applications that ran by those of plain decoding of the same output, prompt included. phases["decode"] holds the counts of the decoding steps alone, and kv_fill_layer_tokens counts the skipped layers that still computed K/V.

Training

The objective is the language-model loss of the gated model plus sparsity * mean(g), which pushes the gates down. From anneal_start (a fraction of training) on, only the final_budget fraction of each sequence's tokens with the highest gates keeps its update, with the budget falling linearly from 1 to final_budget by the end of training. kl_weight adds a KL divergence to the base model's distribution, and ce_weight scales the cross-entropy:

model.detach()
model = economize.apply(base, GateSkipConfig(layers=tuple(range(9, 25))))
objective = GateSkipObjective(sparsity=0.05, final_budget=0.7, anneal_start=0.2, kl_weight=1.0)
fit(model, "train.bin", objective=objective, steps=2000, batch_size=8, seq_len=1024, seed=0)

layers restricts the gates to a set of layers, here the upper two thirds. gate_init_std draws random initial gate weights, and a gate_bias of 100 or more makes every gate exactly 1.

Saving and loading

save_pretrained writes the configuration and the gate weights:

model.save_pretrained("gateskip_model")
model.detach()

model = economize.load(base, "gateskip_model")

Decoding options

rule="threshold" skips the tokens whose gate is below threshold, and a higher threshold skips more. rule="budget" keeps the budget fraction of the tokens of each pass with the highest gates, as the paper does. It ranks within a pass, so a single-token decoding step always runs and the rule saves compute only in the prompt and in multi-token passes. Use threshold for decoding. skip_prompt=False runs the prompt through every layer. Trained gates can be decoded with other inference settings by changing the configuration in the checkpoint:

import dataclasses

checkpoint = economize.read_checkpoint("gateskip_model")
checkpoint.configs[0] = dataclasses.replace(checkpoint.configs[0], threshold=0.7, skip_prompt=False)
model.detach()
model = economize.attach(base, checkpoint)
result = model.decode(ids[0], 64)
print(token_mismatch(result.tokens, reference))

Combining with other techniques

Only one dynamic-depth technique is used per model. GateSkip composes with drafters and with KV-cache techniques. The exact pass that verifies a draft follows the gates, so greedy verification reproduces the output of GateSkip alone. Here the drafter is an untrained EAGLE-3 head (train it as in EAGLE-3 before use) and the cache is quantized to 4 bits:

from economize.techniques.eagle3 import Eagle3Config
from economize.techniques.kv_quant.kivi import KIVIConfig

model.detach()
model = economize.apply(base, GateSkipConfig(), KIVIConfig(bits=4), Eagle3Config(),
                        decoding=economize.DecodeConfig(cycle_length=6))
combined = model.decode(ids[0], 64)

Serving

GateSkip runs on the Hugging Face path only, and a model with it decodes without CUDA graphs:

print(economize.backend_support(GateSkipConfig()))

Configuration

GateSkipConfig dataclass

GateSkipConfig(layers: tuple[int, ...] = (), skip_prompt: bool = True, decision_group: int = 1, rule: str = 'threshold', threshold: float = 0.5, budget: float = 1.0, gate_bias: float = 5.0, gate_init_std: float = 0.0)

GateSkip, layer-level variant (residual-gated layer skipping; see the module docstring).

Attributes:

Name Type Description
layers tuple[int, ...]

Gated layers (empty: every layer).

rule str

Inference rule (:data:GATESKIP_RULES).

threshold float

rule="threshold": tokens whose gate is below it skip the layer.

budget float

rule="budget": fraction of each sequence's tokens in a pass that run each gated layer (rounded up, so a single-token decode step always runs; 1: all).

gate_bias float

Initial gate bias. At >= 100 the gates are exactly 1 and the model is unchanged.

gate_init_std float

Standard deviation of the initial gate weights (0: the gate starts at the bias).

skip_prompt bool

Gate the prompt too (False: the prompt runs every layer).

References

  • Laitenberger, F., Kopiczko, D., Snoek, C. G. M., & Asano, Y. M. (2026). What layers when: Learning to skip compute in LLMs with residual gates. In International Conference on Learning Representations. https://doi.org/10.48550/arXiv.2510.13876