File size: 2,309 Bytes
9a46c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
import os
from pathlib import Path
import subprocess


ROOT = Path(__file__).parents[1]


def test_run_one_exports_project_src_to_method_environment(tmp_path: Path) -> None:
    """Catch manual single-task runs losing dlb package imports after conda env switch."""

    fake_python = tmp_path / "fake-python"
    fake_python.write_text(
        """#!/usr/bin/env python3
import sys

if sys.argv[1:3] == ["-m", "dlb.runner"] and "--validate-only" in sys.argv:
    print("dlb-candi")
    raise SystemExit(0)
raise SystemExit(99)
""",
        encoding="utf-8",
    )
    fake_python.chmod(0o755)

    conda_log = tmp_path / "conda.jsonl"
    fake_conda = tmp_path / "fake-conda"
    fake_conda.write_text(
        """#!/usr/bin/env python3
import json
import os
from pathlib import Path
import sys

Path(os.environ["FAKE_CONDA_LOG"]).write_text(
    json.dumps({
        "argv": sys.argv[1:],
        "pythonpath": os.environ.get("PYTHONPATH"),
        "cuda_visible_devices": os.environ.get("CUDA_VISIBLE_DEVICES"),
    }) + "\\n",
    encoding="utf-8",
)
raise SystemExit(0)
""",
        encoding="utf-8",
    )
    fake_conda.chmod(0o755)

    environment = {
        **os.environ,
        "DLB_PYTHON": str(fake_python),
        "DLB_CONDA": str(fake_conda),
        "FAKE_CONDA_LOG": str(conda_log),
        "CUDA_VISIBLE_DEVICES": "6",
    }
    environment.pop("PYTHONPATH", None)

    completed = subprocess.run(
        [
            "bash",
            str(ROOT / "scripts" / "run_one.sh"),
            "--model",
            "candi",
            "--dataset",
            "lm1b",
            "--steps",
            "2",
            "--num-samples",
            "1",
            "--seed",
            "42",
            "--results-root",
            str(ROOT / "results" / "smoke"),
        ],
        cwd=ROOT,
        env=environment,
        text=True,
        capture_output=True,
    )

    assert completed.returncode == 0, completed.stderr
    record = json.loads(conda_log.read_text(encoding="utf-8"))
    assert record["argv"][:4] == ["run", "-n", "dlb-candi", "env"]
    assert record["argv"][4] == f"PYTHONPATH={ROOT / 'src'}"
    assert record["argv"][5:8] == ["python", "-m", "dlb.runner"]
    assert record["pythonpath"] is None
    assert record["cuda_visible_devices"] == "6"