Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lightx2v_platform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,40 @@ Currently supported backends include:
For the corresponding Docker environments, see: https://github.com/ModelTC/LightX2V/tree/main/dockerfiles/platforms

For the corresponding usage scripts, see: https://github.com/ModelTC/LightX2V/tree/main/scripts/platforms

## Out-of-tree platform plugins

Besides in-tree backends, a chip backend can be shipped as a separate
pip-installable package and discovered at runtime via entry points — no edits to
this repository required.

A plugin package declares an entry point in the `lightx2v.platform_plugins`
group:

```toml
# plugin package's pyproject.toml
[project.entry-points."lightx2v.platform_plugins"]
my_backend = "my_backend_pkg:register"
```

The referenced callable registers a `Device` class into
`PLATFORM_DEVICE_REGISTER` and its operators into the `PLATFORM_*` op registries:

```python
# my_backend_pkg/__init__.py
def register():
from . import device # @PLATFORM_DEVICE_REGISTER("my_backend")
from .ops import register_ops
register_ops()
```

`set_ai_device()` scans this group before initialising the device and before the
op registries are imported, so plugin registrations reach the framework. Then:

```bash
pip install my-backend-package
PLATFORM=my_backend python lightx2v/infer.py ...
```

When no plugins are installed this scan is a no-op, and a plugin that fails to
load is logged and skipped rather than aborting device setup.
39 changes: 39 additions & 0 deletions lightx2v_platform/set_ai_device.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
import os

from loguru import logger

from lightx2v_platform import *


def _load_platform_plugins():
"""Discover out-of-tree platform backends via entry points.

Third-party packages register under the ``lightx2v.platform_plugins`` entry
point group. Each entry point is a zero-arg callable that registers its
Device class into ``PLATFORM_DEVICE_REGISTER`` and its ops into the
``PLATFORM_*`` op registries.

This runs before ``init_ai_device`` (so a plugin-provided device is visible
to the lookup) and before ``lightx2v_platform.ops`` is imported, i.e. before
``lightx2v.utils.registry_factory`` snapshots the platform registries via
``merge()``. That ordering is what makes plugin registrations reach the
framework-facing registries.

No effect when no plugins are installed; a failing plugin is logged and
skipped rather than aborting device setup.
"""
try:
from importlib.metadata import entry_points
except Exception: # pragma: no cover - importlib.metadata is stdlib on 3.8+
return

try:
eps = entry_points(group="lightx2v.platform_plugins")
except TypeError:
# importlib.metadata < 3.10 returns a dict-like mapping.
eps = entry_points().get("lightx2v.platform_plugins", [])

for ep in eps:
try:
ep.load()()
logger.info(f"Loaded LightX2V platform plugin: {ep.name}")
except Exception as e:
logger.warning(f"Failed to load platform plugin '{ep.name}': {e}")


def set_ai_device():
platform = os.getenv("PLATFORM", "cuda")
_load_platform_plugins()
init_ai_device(platform)
check_ai_device(platform)

Expand Down
Loading