File size: 2,332 Bytes
4ffdd01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
# SPDX-License-Identifier: Apache-2.0
"""pytest settings of Raon-Speech-9B (no dependency on tt-metal's own conftest).

- Tests marked ``device`` are skipped when ttnn is missing or ``TT_VISIBLE_DEVICES=none`` (host-only runs). On the
  shared workspace box run device tests through the lock and name the test files:
  ``bin/devrun -t 1800 -- python -m pytest -q -s code/tt_raon/tests/test_device_<name>.py``.
- Host tests (``tests/test_host_*.py``) never open a device; run them on the fake ttnn:
  ``PYTHONPATH=<workspace>/research/packaging/scripts:code TT_VISIBLE_DEVICES=none python -m pytest -p
  fake_ttnn_plugin code/tt_raon/tests/test_host_*.py``.
- ``--device-id N`` (or ``TT_DEVICE_ID``) selects the chip for tests that open one.
"""
from __future__ import annotations

import gc
import importlib.util
import os

import pytest


def pytest_addoption(parser):
    try:
        parser.addoption("--device-id", action="store", default=None, help="chip id (default $TT_DEVICE_ID or 0)")
    except ValueError:  # already registered by another conftest
        pass


def pytest_configure(config):
    config.addinivalue_line("markers", "device: needs a Tenstorrent chip (skipped on host-only runs)")
    config.addinivalue_line("markers", "slow: CPU-heavy reference test (runs with RAON_SLOW=1)")


def _no_device_reason():
    if os.environ.get("TT_VISIBLE_DEVICES", "").lower() == "none":
        return "TT_VISIBLE_DEVICES=none (host-only run)"
    if importlib.util.find_spec("ttnn") is None:
        return "ttnn is not installed"
    return None


def pytest_collection_modifyitems(config, items):
    reason = _no_device_reason()
    slow = os.environ.get("RAON_SLOW", "0") == "1"
    for item in items:
        if reason and "device" in item.keywords:
            item.add_marker(pytest.mark.skip(reason=reason))
        if not slow and "slow" in item.keywords:
            item.add_marker(pytest.mark.skip(reason="slow CPU reference test (set RAON_SLOW=1)"))


@pytest.fixture(autouse=True)
def _gc_between_tests():
    gc.collect()


@pytest.fixture(scope="session")
def checkpoint():
    from tt_raon.weights import Checkpoint

    return Checkpoint()


@pytest.fixture(scope="session")
def tokenizer(checkpoint):
    from tt_raon.reference.tokenizer import RaonTokenizer

    return RaonTokenizer(checkpoint.dir)