economize.registry¶
The generic registry with lazy discovery of third-party plugins.
A small generic registry with lazy discovery of third-party plugins.
Everything in economize that users may extend (techniques, architectures, checkpoint formats, quantization
formats, verifiers, draft-length policies, attention masks) lives in a :class:Registry. A new entry is added by
registration, never by editing an economize module::
from economize.techniques.quant import QUANT_FORMATS, QuantGrid
class MyGrid(QuantGrid):
...
QUANT_FORMATS.register("my_format", lambda symmetric: MyGrid())
Installed packages can also contribute entries through a Python entry-point group, e.g. in pyproject.toml::
[project.entry-points."economize.quant_formats"]
my_format = "my_pkg.formats:my_format"
Entry points are loaded lazily, the first time a name is missing or the full listing is requested. A plugin that
fails to load is logged and recorded in :attr:Registry.unavailable, which the error of an unknown name lists.
Built-in entries register when their module is imported; a package of them calls :func:import_submodules in its
__init__, so adding a module adds its entries and no list is edited.
Registry
¶
Registry(kind: str, entry_point_group: str | None = None)
Bases: Generic[T]
Maps names to objects of one kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
Human-readable name of the kind of entry, used in error messages. |
required |
entry_point_group
|
str | None
|
Optional entry-point group scanned for plugins on the first lookup miss. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
unavailable |
dict[str, str]
|
Sources of entries that could not be loaded (a broken plugin, a module with a missing optional dependency), mapped to the reason. Loaders outside the registry add their own failures here; the error of an unknown name lists them. |
register
¶
register(name: str, obj: None = None, *, override: bool = False) -> Callable[[T], T]
register(name: str, obj: T, *, override: bool = False) -> T
register(name: str, obj: T | None = None, *, override: bool = False)
Register obj under name. Without obj this returns a decorator.
Raises:
| Type | Description |
|---|---|
DuplicateNameError
|
|
unregister
¶
unregister(name: str) -> T
Remove and return the entry name (plugins are loaded first, so that a plugin entry can be removed and
stays removed).
get
¶
get(name: str) -> T
Return the entry name, loading plugins first if it is not registered yet.
Raises:
| Type | Description |
|---|---|
UnknownNameError
|
no entry of that name exists; the message lists the known names. |
import_submodules
¶
import_submodules(package: str, path: Iterable[str], *, packages_only: bool = True, unavailable: dict[str, str] | None = None) -> list[str]
Import the submodules of package in name order (names with a leading underscore hold shared code and are
skipped), so that each registers its entries, and return the names of those imported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package
|
str
|
The package's qualified name ( |
required |
path
|
Iterable[str]
|
The package's |
required |
packages_only
|
bool
|
Import subpackages only, not plain modules. |
True
|
unavailable
|
dict[str, str] | None
|
Where a submodule that fails to import is recorded (name -> reason) and skipped: a missing
third-party module is logged at INFO level, any other error at WARNING level with its traceback. |
None
|