| """Shared glue for all MiniCPM-o eval scripts. |
| |
| Loads the CleverHans-Evaluation counterpart scripts under aliased module |
| names (prefixed with `ch_`), so the MiniCPM-o eval scripts can import their |
| data loaders / metric functions without filename shadowing. |
| |
| Usage in an eval script: |
| |
| import _common # noqa: F401 |
| from _common import ch # namespace holding ch_eval_videomme etc. |
| |
| ch_videomme = ch("videomme") |
| data = ch_videomme.load_videomme(...) |
| """ |
| from __future__ import annotations |
|
|
| import importlib.util |
| import os |
| import sys |
| import types |
| from pathlib import Path |
|
|
|
|
| _HERE = Path(__file__).resolve().parent |
| _CLEVERHANS_SCRIPTS = Path( |
| os.environ.get( |
| "CLEVERHANS_SCRIPTS", |
| "/home/ubuntu/CleverHans-Evaluation/scripts", |
| ) |
| ).resolve() |
|
|
| |
| if str(_HERE) not in sys.path: |
| sys.path.insert(0, str(_HERE)) |
|
|
|
|
| _CACHE: dict[str, types.ModuleType] = {} |
|
|
|
|
| def ch(short_name: str) -> types.ModuleType: |
| """Load a CleverHans-Evaluation script by short name (e.g., 'videomme', |
| 'lvbench', 'dpo_sync'). Returns the module object. |
| |
| Loaded under an aliased module name `ch_eval_<short_name>` so it doesn't |
| collide with same-named files in this directory. |
| """ |
| cache_key = short_name |
| if cache_key in _CACHE: |
| return _CACHE[cache_key] |
|
|
| script_path = _CLEVERHANS_SCRIPTS / f"eval_{short_name}.py" |
| if not script_path.is_file(): |
| raise FileNotFoundError( |
| f"CleverHans-Evaluation script not found: {script_path}\n" |
| f"Set CLEVERHANS_SCRIPTS env var to the correct directory." |
| ) |
|
|
| alias = f"ch_eval_{short_name}" |
| spec = importlib.util.spec_from_file_location(alias, str(script_path)) |
| if spec is None or spec.loader is None: |
| raise ImportError(f"Could not create spec for {script_path}") |
| module = importlib.util.module_from_spec(spec) |
| sys.modules[alias] = module |
| spec.loader.exec_module(module) |
| _CACHE[cache_key] = module |
| return module |
|
|