from __future__ import annotations import importlib.util import sys from pathlib import Path from types import ModuleType def load_encoder(model_name: str) -> ModuleType: """Import the chat encoder that ships inside the checkpoint directory.""" from examples._config import resolve_model_path path = Path(resolve_model_path(model_name)) / "encoding" / "encoding.py" if not path.is_file(): raise SystemExit(f"checkpoint has no encoding/encoding.py: {path}") mod_name = "ckpt_encoding" if mod_name in sys.modules: return sys.modules[mod_name] spec = importlib.util.spec_from_file_location(mod_name, path) module = importlib.util.module_from_spec(spec) sys.modules[mod_name] = module spec.loader.exec_module(module) return module