Skip to content

economize.core

Techniques, the recipe that attaches them, the model wrapper and persistence. The names that economize re-exports at the package root, such as TechniqueConfig, EfficientModel and apply, are documented in economize. This page lists the remaining ones.

Techniques, the recipe that attaches them, the model wrapper and persistence.

CAPABILITY_ORDER module-attribute

CAPABILITY_ORDER: tuple[Capability, ...] = (Capability.WEIGHTS, Capability.ACTIVATIONS, Capability.KV_CACHE, Capability.DEPTH, Capability.DRAFTER)

GENERATE_IMPLEMENTATIONS module-attribute

GENERATE_IMPLEMENTATIONS: Registry[GenerateFn] = Registry('generate implementation')

check_teacher module-attribute

check_teacher = check_teacher_fingerprint

GenerateFn

Bases: Protocol

A transformers-compatible generate, called as fn(model, inputs, **generate_kwargs): the token ids, or a transformers generate output when return_dict_in_generate=True.

attach_rank

attach_rank(technique: Technique | type[Technique]) -> tuple[int, int]

Sort key of the recipe: capability group (:data:CAPABILITY_ORDER, NONE last), then :attr:order.

attach_techniques

attach_techniques(model: Module, adapter: ArchAdapter, configs: Sequence[TechniqueConfig], *, states: States | None = None, calibration: object = None, options: Mapping[str, object] | None = None) -> list[Technique]

Build the techniques of configs, load states (before anything attaches) and attach them in order.

Parameters:

Name Type Description Default
model Module

The base model (unwrapped).

required
adapter ArchAdapter

Its architecture adapter.

required
configs Sequence[TechniqueConfig]

One configuration per technique, in any order.

required
states States | None

Saved state per technique name (from a checkpoint). A technique without an entry is built as :func:economize.apply builds it (techniques stacked on a checkpoint).

None
calibration object

Calibration data for techniques that need it (see :class:AttachContext).

None
options Mapping[str, object] | None

Free-form options for third-party techniques.

None

Returns:

Type Description
list[Technique]

The attached techniques in attach order.

A technique that attaches on a simulated path (:meth:Technique.simulation) gives a :class:~economize.SimulatedPathWarning.

If an attach fails, the techniques attached so far are detached in reverse order before the error propagates. Weight transforms cannot be undone; the error then notes that the base model was (or, for the transform that failed, may have been) modified. A detach that fails during this roll-back is noted on the original error.

build_model

build_model(model: Module, configs: Sequence[TechniqueConfig], *, decoding: DecodeConfig | None = None, calibration: object = None, states: States | None = None) -> EfficientModel

:func:apply that also loads saved technique states before the techniques attach (used by :func:economize.attach for checkpoints).

build_techniques

build_techniques(adapter: ArchAdapter, configs: Sequence[TechniqueConfig], states: States | None = None) -> list[Technique]

Construct the techniques of configs in attach order, check that they combine and load states.

Nothing is attached and the base model is not modified. On a model split by tensor parallelism the techniques' replicated parameters are made equal to those of the first process (a collective call, see :func:~economize.modules.sharding.replicate_from_first_rank).

Raises:

Type Description
IncompatibleTechniquesError

A technique appears twice, or a technique's :meth:~Technique.check_compatible rejects the others (two techniques of one exclusive family such as drafting or dynamic depth, an excluded family, a missing required family).

CheckpointFormatError

states do not match the techniques' parameters.

technique_for

technique_for(config: TechniqueConfig) -> type[Technique]

The registered technique class that builds config.