Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
82 lines (59 loc) · 2.44 KB
/
Copy pathconftest.py
File metadata and controls
82 lines (59 loc) · 2.44 KB
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
8F40
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Shared pytest fixtures and collection rules.
Tests are split by dependency weight:
* ``tests/unit`` and ``tests/property`` -- pure Python, no torch import
(except the numerical modules, which are marked ``torch``).
* ``tests/integration`` -- CPU torch.
* ``tests/gpu`` -- marked ``gpu`` and deselected unless CUDA is present.
"""
from __future__ import annotations
import importlib.util
import socket
from collections.abc import Iterator
from pathlib import Path
import pytest
HAS_TORCH = importlib.util.find_spec("torch") is not None
HAS_TRANSFORMERS = importlib.util.find_spec("transformers") is not None
HAS_PEFT = importlib.util.find_spec("peft") is not None
def _cuda_available() -> bool:
if not HAS_TORCH:
return False
import torch
return bool(torch.cuda.is_available())
HAS_CUDA = _cuda_available()
requires_torch = pytest.mark.skipif(not HAS_TORCH, reason="requires the [train] extra (torch)")
requires_transformers = pytest.mark.skipif(
not (HAS_TORCH and HAS_TRANSFORMERS), reason="requires torch + transformers"
)
requires_peft = pytest.mark.skipif(
not (HAS_TORCH and HAS_TRANSFORMERS and HAS_PEFT), reason="requires torch + transformers + peft"
)
def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
"""Auto-skip GPU tests when there is no CUDA device."""
if HAS_CUDA:
return
skip_gpu = pytest.mark.skip(reason="no CUDA device available")
for item in items:
if "gpu" in item.keywords:
item.add_marker(skip_gpu)
@pytest.fixture
def run_dir(tmp_path: Path) -> Iterator[Path]:
"""An isolated output directory for a run."""
target = tmp_path / "runs"
target.mkdir(parents=True, exist_ok=True)
yield target
@pytest.fixture
def toy_tokenizer(): # type: ignore[no-untyped-def]
"""A fresh toy tokenizer."""
from miniverl.models.tokenizers import ToyTokenizer
return ToyTokenizer()
@pytest.fixture
def deny_network(monkeypatch: pytest.MonkeyPatch) -> list[str]:
"""Fail a test at the first attempted socket connection."""
attempts: list[str] = []
def blocked(*args: object, **kwargs: object) -> None:
target = args[0] if args else kwargs.get("address", "unknown")
attempts.append(str(target))
raise AssertionError(f"network access attempted: {target}")
monkeypatch.setattr(socket, "create_connection", blocked)
monkeypatch.setattr(socket.socket, "connect", blocked)
return attempts