|
| 1 | +"""Utilities for discovering and loading external hls4ml backend plugins.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from collections.abc import Callable |
| 5 | +from importlib import import_module |
| 6 | +from importlib.metadata import entry_points |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from hls4ml.backends.backend import register_backend |
| 10 | +from hls4ml.writer.writers import register_writer |
| 11 | + |
| 12 | +ENTRY_POINT_GROUP = 'hls4ml.backends' |
| 13 | +ENV_PLUGIN_MODULES = 'HLS4ML_BACKEND_PLUGINS' |
| 14 | + |
| 15 | +_plugins_loaded = False |
| 16 | + |
| 17 | + |
| 18 | +def load_backend_plugins() -> None: |
| 19 | + """Discover and register backend plugins. |
| 20 | +
|
| 21 | + This function loads plugins published via Python entry points under the |
| 22 | + ``hls4ml.backends`` group as well as modules listed in the |
| 23 | + ``HLS4ML_BACKEND_PLUGINS`` environment variable. The environment variable |
| 24 | + accepts a separator compatible with :data:`os.pathsep`. |
| 25 | + """ |
| 26 | + global _plugins_loaded |
| 27 | + if _plugins_loaded: |
| 28 | + return |
| 29 | + |
| 30 | + _load_entry_point_plugins() |
| 31 | + _load_env_plugins() |
| 32 | + |
| 33 | + _plugins_loaded = True |
| 34 | + |
| 35 | + |
| 36 | +def _load_entry_point_plugins() -> None: |
| 37 | + group_eps = entry_points().select(group=ENTRY_POINT_GROUP) |
| 38 | + |
| 39 | + for ep in group_eps: |
| 40 | + try: |
| 41 | + obj = ep.load() |
| 42 | + except Exception as exc: |
| 43 | + print(f'WARNING: failed to load backend plugin entry "{ep.name}": {exc}') |
| 44 | + continue |
| 45 | + _register_plugin_object(ep.name, obj) |
| 46 | + |
| 47 | + |
| 48 | +def _load_env_plugins() -> None: |
| 49 | + raw_modules = os.environ.get(ENV_PLUGIN_MODULES, '') |
| 50 | + if not raw_modules: |
| 51 | + return |
| 52 | + |
| 53 | + for module_name in filter(None, raw_modules.split(os.pathsep)): |
| 54 | + try: |
| 55 | + module = import_module(module_name) |
| 56 | + except Exception as exc: |
| 57 | + print(f'WARNING: failed to import backend plugin module "{module_name}": {exc}') |
| 58 | + continue |
| 59 | + |
| 60 | + register_callable: Any = getattr(module, 'register', module) |
| 61 | + _register_plugin_object(module_name, register_callable) |
| 62 | + |
| 63 | + |
| 64 | +def _register_plugin_object(name: str, obj: Any) -> None: |
| 65 | + """Interpret the plugin object and register provided backends.""" |
| 66 | + if callable(obj): |
| 67 | + _invoke_registration_callable(name, obj) |
| 68 | + return |
| 69 | + |
| 70 | + print(f'WARNING: plugin entry "{name}" did not provide a usable backend registration (got {obj!r})') |
| 71 | + |
| 72 | + |
| 73 | +def _invoke_registration_callable(name: str, func: Callable[..., Any]) -> None: |
| 74 | + try: |
| 75 | + func(register_backend=register_backend, register_writer=register_writer) |
| 76 | + return |
| 77 | + except TypeError: |
| 78 | + try: |
| 79 | + func(register_backend, register_writer) |
| 80 | + return |
| 81 | + except Exception as exc: |
| 82 | + print(f'WARNING: backend plugin callable "{name}" failed: {exc}') |
| 83 | + return |
| 84 | + except Exception as exc: |
| 85 | + print(f'WARNING: backend plugin callable "{name}" failed: {exc}') |
| 86 | + return |
0 commit comments