File size: 2,037 Bytes
b2c2640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""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()

# Make local (MiniCPM-o) modules importable without package setup.
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