Hidden Transfer¶
At a glance
Hidden Transfer: trained linear maps turn an intermediate hidden state into pseudo states of future tokens, which the upper layers decode into drafts.
| Registered name | Family | Saves | Lossless | Trainable | Configuration |
|---|---|---|---|---|---|
hidden_transfer |
drafting | latency | yes | transfers | HiddenTransferConfig |
Backends (default configuration): transformers: real · gguf: no · mlx: no · onnx: no · sglang: no · trtllm: no · vllm: real
Hidden Transfer (Wu et al., 2024) drafts future tokens from pseudo hidden states at intermediate layers. After a
forward pass that ended at position n, a linear map W_i (one d x d matrix per future step i = 1..k) turns the
base model's hidden state of n after layer t_i into a pseudo state of the token at n + i, which is not known
yet. The pseudo state continues through layers t_i + 1..L and the LM head, attending to the cache, and drafts token
n + i + 1. Later transfer layers sit deeper, so each step runs fewer layers than a full forward pass. The base
model verifies the drafts in one pass, so greedy decoding is lossless.
The maps start at the identity, so an untrained step i is the early exit of layer t_i's state through the
remaining layers. They are trained on a frozen base model with the KL divergence from the base model's distribution
of each future token. By default the transfer layers sit at 62.5, 75 and 87.5 percent of the depth, the paper's
layers 25, 30 and 35 of 40.
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¶
from economize.techniques.dynamic_depth import HiddenTransferConfig
model = economize.apply(base, HiddenTransferConfig())
decoding = model.techniques["hidden_transfer"].decode_config()
result = model.decode(ids[0], 64, decoding=decoding)
print(tokenizer.decode(result.tokens))
print(result.mean_accepted) # accepted drafts per verification
print(result.cost_summary())
decode_config() gives cycles of k + 1 tokens, one verified token and one draft per map. On the 24 layers of
Qwen2.5-0.5B the default fractions give the transfer layers 15, 18 and 21. transfer_layers sets them directly:
model.detach()
model = economize.apply(base, HiddenTransferConfig(transfer_layers=(12, 16, 20)),
decoding=economize.DecodeConfig(cycle_length=4))
Training¶
fit trains the maps on the frozen base model. The default objective hidden_transfer_kl is the paper's
distillation loss over the k steps:
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/hidden_transfer")
print(result.eval) # teacher-forced KL and agreement per draft step
In training, a pseudo state attends to the real states of every earlier position, as in the paper.
train_context="draft" lets it attend to the exact states up to its source position and to the earlier pseudo
states instead, which is what it sees during decoding. HiddenTransferDistill takes the options of StepDistill,
such as an extra cross-entropy on the true token:
from economize.techniques.dynamic_depth import HiddenTransferDistill
model.detach()
model = economize.apply(base, HiddenTransferConfig(train_context="draft"),
decoding=economize.DecodeConfig(cycle_length=4))
fit(model, "train.bin", HiddenTransferDistill(ce_weight=0.1), steps=2000, batch_size=8, seq_len=1024)
Saving and loading¶
model.save_pretrained("hidden_transfer_maps")
model.detach()
model = economize.load(base, "hidden_transfer_maps", decoding=economize.DecodeConfig(cycle_length=4))
Decoding options¶
lossless=False keeps every draft without checking it, so every cycle emits k + 1 tokens whatever the base
model would have chosen. The output then differs from the base model's:
lossy = model.techniques["hidden_transfer"].decode_config(lossless=False)
model.decode(ids[0], 64, decoding=lossy)
Combining with other techniques¶
The maps compose with techniques that are not drafters, such as a 4-bit KV cache:
from economize.techniques.kv_quant.kivi import KIVIConfig
model.detach()
model = economize.apply(base, KIVIConfig(bits=4), HiddenTransferConfig(),
decoding=economize.DecodeConfig(cycle_length=4))
model.decode(ids[0], 64)
Serving¶
economize's vLLM plugin runs the maps and the remaining layers as the draft step, and vLLM verifies the chain. SGLang has no proposer for it.
from economize.backends.vllm import serving
plan = serving.to_vllm(model, "out/qwen-hidden-transfer", num_speculative_tokens=3)
llm = plan.llm()
Configuration¶
HiddenTransferConfig
dataclass
¶
HiddenTransferConfig(transfer_layers: tuple[int, ...] | None = None, transfer_fractions: tuple[float, ...] = (0.625, 0.75, 0.875), train_context: str = 'teacher', param_dtype: str = 'float32')
Hyperparameters of Hidden Transfer.
Attributes:
| Name | Type | Description |
|---|---|---|
transfer_layers |
tuple[int, ...] | None
|
|
transfer_fractions |
tuple[float, ...]
|
Relative depths of the transfer layers when :attr: |
train_context |
str
|
An entry of :data: |
param_dtype |
str
|
Storage and compute dtype of the maps ( |
resolved_layers
¶
resolved_layers(num_layers: int) -> tuple[int, ...]
t_1..t_k for a model of num_layers layers.
Raises:
| Type | Description |
|---|---|
ConfigError
|
A layer outside |