Files
ely-eye/backend/tests/test_host_profiles.py
T
ZacharyZhang-NY 9cb46c18d3 Keep every file inside the rules the repo already wrote down
The 500-line rule and the one-line-comment rule were enforced only from
memory, so they drifted. bench/filesize_check.py now checks the budget on
every tracked source file, and it passes.

Split, with behaviour proven unchanged by comparing each moved function body
against HEAD byte for byte (53 training bodies, 21 benchmark bodies, 0
differences):

  db.py        503 -> 396   store_search.py     105
  bench/run.py 609 -> 351   query_set.py         80, report.py 211
  benchmarks.py 619 -> 424  benchmark_scoring.py 262
  training.py 1630 ->  94   training_{common,encode,metrics,datasets,lora,retrieval}

TrainingService stays as a thin facade for the CLI; the facade's parameter
lists are pinned against the module functions by inspect, which is what
catches a leaked self.

Deleted the eight top-level functions no file called, after fixing the scan
that had reported 201 false positives by skipping the defining file. The
v1/v2/v3 sweep found nothing to delete and says so: every remaining version
tag is an on-disk format marker, a migration step, a fixture name, or a
third-party model name.

Collapsed 39 multi-line comment blocks (86 lines) to one line each, keeping
the measured facts and error strings that justify the code.

269 tests pass; ruff clean; mypy 24 errors in 9 files against a 24-in-6
baseline, net zero; pyright 4, the same four HEAD measures.
2026-10-08 08:20:21 -04:00

103 lines
4.5 KiB
Python

"""The launch layer must describe the machine it runs on, not the one it was written on."""
from __future__ import annotations
import os
import stat
from pathlib import Path
import pytest
from ely_eye import profiles
from ely_eye.config import Settings
from ely_eye.proofs.training_trace import cuda_bf16_training
from ely_eye.training_metrics import require_cuda_bf16
def _stub_nvidia_smi(tmp_path: Path, monkeypatch, output: str, exit_code: int = 0) -> None:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
stub = bin_dir / "nvidia-smi"
stub.write_text(f"#!/bin/sh\nprintf '%s\\n' '{output}'\nexit {exit_code}\n", encoding="utf-8")
stub.chmod(stub.stat().st_mode | stat.S_IEXEC)
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}")
def test_a_gpu_that_is_not_the_written_down_card_still_counts(tmp_path: Path, monkeypatch) -> None:
_stub_nvidia_smi(tmp_path, monkeypatch, "GPU 0: NVIDIA GeForce RTX 5090 (UUID: GPU-1234)")
monkeypatch.setattr(profiles, "uses_wsl", lambda: False)
assert profiles.gpu_available(Settings(home=tmp_path / "home")) is True
def test_a_machine_the_driver_cannot_see_reports_no_gpu(tmp_path: Path, monkeypatch) -> None:
_stub_nvidia_smi(tmp_path, monkeypatch, "nvidia-smi: command not found", exit_code=127)
monkeypatch.setattr(profiles, "uses_wsl", lambda: False)
assert profiles.gpu_available(Settings(home=tmp_path / "home")) is False
def test_the_distro_name_comes_from_settings(monkeypatch) -> None:
monkeypatch.setattr(profiles, "uses_wsl", lambda: True)
command = profiles.host_shell("true", Settings(home=Path("/tmp"), wsl_distro="Ubuntu-24.04"))
assert command == ["wsl.exe", "-d", "Ubuntu-24.04", "bash", "-lc", "true"]
def test_inside_wsl_the_local_shell_runs(monkeypatch, tmp_path: Path) -> None:
monkeypatch.setenv("WSL_DISTRO_NAME", "Ubuntu")
monkeypatch.setattr(profiles.shutil, "which", lambda name: "/mnt/c/WINDOWS/system32/wsl.exe")
assert profiles.uses_wsl() is False
monkeypatch.delenv("WSL_DISTRO_NAME")
assert profiles.uses_wsl() is True
assert profiles.host_shell("true", Settings(home=tmp_path / "home", wsl_distro="Ubuntu"))[0] == "wsl.exe"
def test_a_linux_host_is_pointed_at_the_shell_twin(monkeypatch) -> None:
monkeypatch.setattr(profiles, "uses_wsl", lambda: False)
assert profiles.launch_script("scripts/launch-sglang-live.ps1") == "scripts/launch-sglang-live.sh"
monkeypatch.setattr(profiles, "uses_wsl", lambda: True)
assert profiles.launch_script("scripts/launch-sglang-live.ps1") == "scripts/launch-sglang-live.ps1"
def test_the_training_verdict_does_not_name_a_card() -> None:
newer = {"cuda_available": True, "bf16_supported": True, "device_name": "NVIDIA GeForce RTX 5090"}
assert cuda_bf16_training(newer) is True
older = {"cuda_available": True, "bf16_supported": True, "device_name": "NVIDIA GeForce RTX 4080"}
assert cuda_bf16_training(older) is True
without_bf16 = {"cuda_available": True, "bf16_supported": False, "device_name": "NVIDIA GeForce RTX 4090"}
assert cuda_bf16_training(without_bf16) is False
class _FakeCuda:
def __init__(self, available: bool, bf16: bool) -> None:
self._available = available
self._bf16 = bf16
def is_available(self) -> bool:
return self._available
def is_bf16_supported(self) -> bool:
return self._bf16
class _FakeTorch:
def __init__(self, available: bool, bf16: bool) -> None:
self.cuda = _FakeCuda(available, bf16)
def test_training_refuses_a_machine_without_cuda_by_capability_not_card() -> None:
with pytest.raises(RuntimeError) as refused:
require_cuda_bf16(_FakeTorch(available=False, bf16=False))
assert "4090" not in str(refused.value)
require_cuda_bf16(_FakeTorch(available=True, bf16=True))
def test_every_profile_ships_a_script_for_this_host(tmp_path: Path, monkeypatch) -> None:
root = Path(__file__).resolve().parents[2]
monkeypatch.setattr(profiles, "uses_wsl", lambda: False)
linux = profiles.runtime_profile_report(Settings(home=tmp_path / "home"), root)
assert [item.name for item in linux.validations if not item.script_exists] == []
assert all(item.script.endswith(".sh") for item in linux.profiles), linux.profiles
monkeypatch.setattr(profiles, "uses_wsl", lambda: True)
windows = profiles.runtime_profile_report(Settings(home=tmp_path / "home"), root)
assert [item.name for item in windows.validations if not item.script_exists] == []
assert all(item.script.endswith(".ps1") for item in windows.profiles), windows.profiles