Skip to content

Hierarchical drafting

At a glance

Speculative decoding with a separate draft LM that runs economize techniques of its own.

Registered name Family Saves Lossless Trainable Configuration
draft_model_hier drafting latency yes no HierDraftModelConfig

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

draft_model_hier is two-model speculative decoding with a draft LM that is itself accelerated by economize techniques. The base model verifies as usual, so greedy decoding returns the base model's own tokens and the rejection verifier keeps its distribution. What changes is how the draft LM produces a cycle's drafts. A technique that only changes its weights, such as int4 quantization, makes every draft step cheaper. A drafting technique on the draft LM, such as an EAGLE-3 head or a trained early exit, gives it a drafter of its own, which speeds up the draft LM in the same way a self-drafter speeds up a base model.

With an inner drafter, every cycle starts with an exact catch-up of the draft LM over the committed tokens it has not run through all its layers. This pass gives the first draft, the token the plain draft LM would propose, and the exact hidden states that the inner drafter conditions on. The inner drafter's cheap steps then give the remaining drafts of the cycle. A rejected draft leaves nothing behind, since the next catch-up recomputes every position after the last exact one. The inner techniques are attached to the draft LM with economize.apply (given as configurations) or economize.load (given as the directory of an economize checkpoint trained on the draft LM).

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)

Quick start

The page drafts for Qwen2.5-7B-Instruct with Qwen2.5-0.5B-Instruct and quantizes the draft LM's weights to int4. Nothing is trained:

from economize.techniques.draft_model_hier import HierDraftModelConfig
from economize.techniques.quant import QuantConfig

big = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-7B-Instruct", dtype=torch.bfloat16, device_map="cuda")
model = economize.apply(
    big,
    HierDraftModelConfig(model="Qwen/Qwen2.5-0.5B-Instruct", dtype="bfloat16",
                         inner=(QuantConfig(format="int4", method="rtn", group_size=128),)),
    decoding=economize.DecodeConfig(cycle_length=5),
)

result = model.decode(ids[0], 64)
print(tokenizer.decode(result.tokens))
print(result.mean_accepted)            # accepted drafts per verification

The quantized draft LM drafts exactly like a plain draft LM, so this row differs from draft_model only in the cost of a draft step and in the drafts the quantized weights produce.

A trained drafter inside the draft LM

A drafter for the draft LM is trained on the draft LM alone, as in the EAGLE-3 tutorial, and saved as an economize checkpoint. inner then names that directory:

from economize.techniques.eagle3 import Eagle3Config
from economize.train import fit

draft = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct", dtype=torch.bfloat16, device_map="cuda")
fit(economize.apply(draft, Eagle3Config()), "train.bin", steps=2000, batch_size=8, seq_len=1024,
    output_dir="runs/draft_eagle3")

model.detach()
model = economize.apply(
    big,
    HierDraftModelConfig(model="Qwen/Qwen2.5-0.5B-Instruct", dtype="bfloat16", inner="runs/draft_eagle3/final"),
    decoding=economize.DecodeConfig(cycle_length=5),
)

The inner technique can also be given as a configuration, e.g. inner=(EarlyExitConfig(exit_layer=16, adaptation="full"),), in which case its trainable parts start from their initialization. In a configuration string (the tech: lines of the benchmark scripts) inner is either a checkpoint path or a list of techniques written as name:key=value;key=value|name2:..., with JSON values.

The call log keeps the inner drafter's events and moves them to the draft LM's networks: calls of the draft LM's own layers are logged under the module draft_model, and calls of a separate network of the inner technique under draft_model.<module>, such as draft_model.eagle3. A cost model prices the latter with an entry of that name.

Saving and loading

The checkpoint holds the configuration, and loading reads the draft LM again from model and attaches the inner techniques again (from their own checkpoint when inner names one):

model.save_pretrained("hier_draft")
model.detach()

model = economize.load(big, "hier_draft", decoding=economize.DecodeConfig(cycle_length=5))

Serving

vLLM and SGLang run a draft model as one plain forward per draft token, so they refuse this technique. A quantized draft LM alone is served as draft_model from its quantized checkpoint.

Configuration

HierDraftModelConfig dataclass

HierDraftModelConfig(model: str = '', revision: str | None = None, dtype: str = 'auto', attn_implementation: str = 'sdpa', inner: tuple[TechniqueConfig, ...] = (), checkpoint: str = '')

Two-model speculative decoding whose draft LM runs with economize techniques of its own (hierarchical drafting).

Every cycle the draft LM first processes, with all its layers, the tokens the model committed since the last cycle (the prompt in the first cycle). That pass yields the first draft exactly as :class:~economize.techniques.draft_model.DraftModelConfig would, and it hands its hidden states to the inner drafter (an EAGLE-3 module, an early exit, ... trained on the draft LM), which makes the remaining drafts of the cycle with its cheap steps. The model verifies every draft, so the output stays the model's own; the inner technique only changes how fast and how well the draft LM drafts.

Inner techniques without a drafter (weight quantization, pruning) change the draft LM itself, which then drafts every token with all its (quantized, pruned) layers.

Attributes:

Name Type Description
inner tuple[TechniqueConfig, ...]

The techniques applied to the draft LM, as configurations, their JSON form ({"technique": name, "config": fields}), or one string: the directory of an economize checkpoint saved for the draft LM (it then goes to :attr:checkpoint), or specs "name:key=value;key=value" joined by "|" (values are parsed as JSON, e.g. "early_exit:exit_layer=16;adaptation="full"").

checkpoint str

Directory of an economize checkpoint (e.g. runs/<name>/final) whose techniques were trained on the draft LM; it is loaded onto the draft LM whenever the technique attaches. Exclusive with :attr:inner.

validate

validate(adapter: ArchAdapter) -> None

Nothing to check against the model: the inner techniques are validated against the draft LM when it loads (see :class:~economize.techniques.draft_model_hier.HierDraftModelTechnique).