Skip to content

economize.backends

Serving backends: economize techniques inside inference engines.

Serving backends: economize drafters inside inference engines.

Each backend lives in its own subpackage and imports its engine lazily, so importing economize.backends.<name> works without the engine installed. The backends know no technique. Each drafter technique defines its exported format (typically the target's config.json with a block that describes the drafter, and the drafter-owned tensors in model.safetensors), and an exporter writes it for one engine. The layers a self-drafter shares with the target, the final norm, the embedding and the LM head are never exported: an engine runs the target's own modules, so a policy update of the target reaches the drafter at once.

A drafter is written for a backend by the :class:DrafterExporter registered in :data:EXPORTERS for that (backend, technique) pair (:mod:economize.backends.exporters); :func:export_drafter picks it. Importing this package imports every backend subpackage (found with :func:economize.registry.import_submodules, so no list is edited), which registers the built-in exporters:

  • :mod:economize.backends.vllm: the vllm.general_plugins entry point, the "vllm/*" exporters and the configuration builders for vllm serve, TRL's server mode and verl.
  • :mod:economize.backends.sglang: the sglang.srt.plugins entry point (economize's self-drafters as an EAGLE-3 draft model), the "sglang/*" exporters and the serving plans for sglang.Engine and launch_server.
  • The runtimes that run a whole model on their own kernels (:mod:economize.backends.gguf for llama.cpp, :mod:economize.backends.onnx for ONNX Runtime, :mod:economize.backends.mlx for MLX, :mod:economize.backends.trtllm for TensorRT-LLM): each is an :class:~economize.backends.runtime_export.ExportRuntime with its own technique adapters, reported by :func:economize.backend_support (docs/exports.md).

Built-in exporters register with the :func:register_exporter decorator when their subpackage is imported, and nowhere else (pyproject.toml declares no built-in economize.exporters entry point, so no exporter is registered twice). A new backend is a new subpackage here, or a separate package that registers its exporters through the economize.exporters entry-point group.

DrafterExporter

Bases: ABC, Generic[_T_co]

Writes the drafter of one technique for one serving backend.

Subclasses implement :meth:config and :meth:tensors; :meth:export writes both. The class attributes are set by :func:register_exporter. The type parameter is the exported technique's class (DrafterExporter[JumpTechnique]), which :meth:technique_of returns.

Attributes:

Name Type Description
backend str

The serving backend ("vllm").

technique str

The technique name whose drafter is exported ("early_exit").

config_file str

File name of the configuration in the exported folder.

weights_file str

File name of the tensors in the exported folder.

technique_of

technique_of(model: Module) -> _T_co

The exported technique of an EfficientModel (or model itself if it is that technique).

The technique is the one registered under :attr:technique, so it is an instance of the exporter's type parameter; that is not checked at run time (a technique of another package may stand in for it).

Raises:

Type Description
ConfigError

The model has no such technique.

config abstractmethod

config(model: Module, **options: Unpack[ExportOptions]) -> dict[str, object]

The content of :attr:config_file (written as JSON). options are the backend's export options.

tensors abstractmethod

tensors(model: Module) -> dict[str, Tensor]

The drafter-owned tensors written to :attr:weights_file, by their name in that file.

live_tensors

live_tensors(model: Module) -> dict[str, Tensor]

The tensors of :meth:tensors under the same names, as they live in the model (detached, no copy and no dtype or device change), for a weight sync into a running engine. The default returns :meth:tensors; an exporter whose :meth:tensors converts overrides it.

metadata

metadata() -> dict[str, str]

The safetensors metadata of :attr:weights_file.

export

export(model: Module, path: str | Path, **options: Unpack[ExportOptions]) -> Path

Write :attr:config_file and :attr:weights_file to the folder path (created if missing).

Returns:

Type Description
Path

path as a :class:~pathlib.Path.

UnsupportedBackendError

Bases: EconomizeError, NotImplementedError

A backend is registered for a technique but cannot serve it yet.

export_drafter

export_drafter(model: Module, path: str | Path, *, backend: str, technique: str | None = None, **options: Unpack[ExportOptions]) -> Path

Export the model's drafter for backend to the folder path (see :func:exporter_for).

options go to the exporter (for vLLM: format, num_speculative_tokens, verifier).

exporter_for

exporter_for(model: Module, backend: str, technique: str | None = None) -> DrafterExporter[Technique]

The exporter of the model's drafter technique for backend.

Parameters:

Name Type Description Default
model Module

An EfficientModel (or a technique).

required
backend str

The serving backend.

required
technique str | None

The technique to export; by default the one attached technique with an exporter for backend.

None

Raises:

Type Description
UnknownNameError

technique is given and has no exporter for backend.

ConfigError

No attached technique, or more than one, has an exporter for backend.

exporter_key

exporter_key(backend: str, technique: str) -> str

The :data:EXPORTERS key of a backend and a technique: "<backend>/<technique>".

register_exporter

register_exporter(backend: str, technique: str, *, override: bool = False) -> Callable[[_E], _E]

Class decorator registering a :class:DrafterExporter for technique on backend.