|
| 1 | +"""Tests for the pet env's hub-resolve bypass. |
| 2 | +
|
| 3 | +UPETCalculator(model=..., version=...) resolves the name by listing the hub |
| 4 | +repo — an uncached API call that fails on workers (HF_HUB_OFFLINE=1) and on |
| 5 | +nodes without internet. The env fetches the pinned file itself via |
| 6 | +hf_hub_download (a cache hit needs no network) and passes checkpoint_path, |
| 7 | +which skips the resolve. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import importlib.util |
| 13 | +import sys |
| 14 | +import types |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +_CONFIGS_DIR = ( |
| 20 | + Path(__file__).parent.parent.parent / "sample_model_configurations" / "nvidia_configs" |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _load_env_module(): |
| 25 | + spec = importlib.util.spec_from_file_location("pet_env", _CONFIGS_DIR / "pet.py") |
| 26 | + module = importlib.util.module_from_spec(spec) |
| 27 | + spec.loader.exec_module(module) |
| 28 | + return module |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def stubbed_libs(monkeypatch): |
| 33 | + """Stub huggingface_hub and upet; capture what setup() passes to each.""" |
| 34 | + captured = {} |
| 35 | + |
| 36 | + hf = types.ModuleType("huggingface_hub") |
| 37 | + |
| 38 | + def hf_hub_download(**kwargs): |
| 39 | + captured["download"] = kwargs |
| 40 | + return "/shared/cache/models/stub.ckpt" |
| 41 | + |
| 42 | + hf.hf_hub_download = hf_hub_download |
| 43 | + |
| 44 | + upet = types.ModuleType("upet") |
| 45 | + upet_calculator = types.ModuleType("upet.calculator") |
| 46 | + |
| 47 | + class UPETCalculator: |
| 48 | + def __init__(self, **kwargs): |
| 49 | + captured["calculator"] = kwargs |
| 50 | + |
| 51 | + upet_calculator.UPETCalculator = UPETCalculator |
| 52 | + upet.calculator = upet_calculator |
| 53 | + |
| 54 | + monkeypatch.setitem(sys.modules, "huggingface_hub", hf) |
| 55 | + monkeypatch.setitem(sys.modules, "upet", upet) |
| 56 | + monkeypatch.setitem(sys.modules, "upet.calculator", upet_calculator) |
| 57 | + return captured |
| 58 | + |
| 59 | + |
| 60 | +def test_setup_downloads_pinned_filename(stubbed_libs): |
| 61 | + env = _load_env_module() |
| 62 | + env.setup("pet-oam-xl", device="cuda") |
| 63 | + assert stubbed_libs["download"] == { |
| 64 | + "repo_id": "lab-cosmo/upet", |
| 65 | + "filename": "pet-oam-xl-v1.0.0.ckpt", |
| 66 | + "subfolder": "models", |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | +def test_setup_passes_checkpoint_path_not_model_name(stubbed_libs): |
| 71 | + """model=/version= would trigger the hub-listing resolve — never pass them.""" |
| 72 | + env = _load_env_module() |
| 73 | + env.setup("pet-omatpes-l", device="cpu") |
| 74 | + calc_kwargs = stubbed_libs["calculator"] |
| 75 | + assert calc_kwargs == { |
| 76 | + "checkpoint_path": "/shared/cache/models/stub.ckpt", |
| 77 | + "device": "cpu", |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +def test_every_checkpoint_maps_to_parseable_filename(): |
| 82 | + """upet parses (model, size, version) out of the filename — every pinned |
| 83 | + entry must render to the {model}-{size}-v{version}.ckpt shape.""" |
| 84 | + env = _load_env_module() |
| 85 | + for upstream in env.CHECKPOINTS.values(): |
| 86 | + model, version = upstream.split("@", 1) |
| 87 | + filename = f"{model}-v{version}.ckpt" |
| 88 | + assert filename.endswith(f"-v{version}.ckpt") |
| 89 | + assert "@" not in filename |
0 commit comments