Skip to content

Commit 2e616b5

Browse files
Merge pull request #47 from CalculatedContent/agent/enforce-tmp-nanogpt-root
Enforce /tmp-only nanoGPT runtime paths
2 parents fe5d0e3 + 54e792f commit 2e616b5

4 files changed

Lines changed: 81 additions & 8 deletions

File tree

baseline/nanogpt_one_head/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ cd baseline/nanogpt_one_head
230230
# Install this package and any missing dependencies into the active conda env.
231231
python -m pip install -e .
232232

233-
# Keep all corpus caches and experiment outputs outside the git checkout.
234-
export RG_NANOGPT_ONE_HEAD_ROOT="$HOME/rg-nanogpt-one-head"
233+
# Keep all corpus caches and experiment outputs under /tmp.
234+
export RG_NANOGPT_ONE_HEAD_ROOT="/tmp/rg-nanogpt-one-head"
235235

236236
# Allow unsupported individual MPS operations to fall back to CPU when needed.
237237
export PYTORCH_ENABLE_MPS_FALLBACK=1

baseline/nanogpt_one_head/src/rg_nanogpt_one_head/config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
import yaml
1111

1212
SUPPORTED_OPTIMIZERS = ("sgd_momentum", "adamw", "muon")
13-
DEFAULT_ROOT = Path.home() / "rg-nanogpt-one-head"
13+
DEFAULT_ROOT = Path("/tmp/rg-nanogpt-one-head")
1414

1515

1616
def roots() -> dict[str, Path]:
17-
root = Path(os.environ.get("RG_NANOGPT_ONE_HEAD_ROOT", DEFAULT_ROOT)).expanduser()
17+
root = Path(os.environ.get("RG_NANOGPT_ONE_HEAD_ROOT", DEFAULT_ROOT))
1818
return {
1919
"root": root,
2020
"data": Path(
2121
os.environ.get("RG_NANOGPT_ONE_HEAD_DATA_ROOT", root / "data")
22-
).expanduser(),
22+
),
2323
"results": Path(
2424
os.environ.get("RG_NANOGPT_ONE_HEAD_RESULTS_ROOT", root / "results")
25-
).expanduser(),
25+
),
2626
"plots": Path(
2727
os.environ.get("RG_NANOGPT_ONE_HEAD_PLOTS_ROOT", root / "plots")
28-
).expanduser(),
28+
),
2929
}
3030

3131

baseline/nanogpt_one_head/src/rg_nanogpt_one_head/data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ def prepare_fineweb_edu(
257257
except ImportError as exc:
258258
raise RuntimeError(
259259
"data preparation requires datasets and tiktoken; "
260-
"run scripts/setup_mac.sh"
260+
"install dependencies into the active conda environment with "
261+
"`python -m pip install -e .`"
261262
) from exc
262263

263264
dataset_cfg = cfg["dataset"]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
import sys
5+
6+
7+
EXPERIMENT_ROOT = Path(__file__).resolve().parents[1]
8+
REPO_ROOT = EXPERIMENT_ROOT.parents[1]
9+
sys.path.insert(0, str(EXPERIMENT_ROOT / "src"))
10+
11+
from rg_nanogpt_one_head.config import DEFAULT_ROOT, roots
12+
13+
14+
ROOT_ENV_VARS = (
15+
"RG_NANOGPT_ONE_HEAD_ROOT",
16+
"RG_NANOGPT_ONE_HEAD_DATA_ROOT",
17+
"RG_NANOGPT_ONE_HEAD_RESULTS_ROOT",
18+
"RG_NANOGPT_ONE_HEAD_PLOTS_ROOT",
19+
)
20+
FORBIDDEN_HOME_TOKENS = (
21+
"$HOME",
22+
"${HOME}",
23+
"Path.home(",
24+
".expanduser(",
25+
"/home/",
26+
"~/",
27+
)
28+
29+
30+
def test_default_root_is_tmp(monkeypatch):
31+
for name in ROOT_ENV_VARS:
32+
monkeypatch.delenv(name, raising=False)
33+
34+
assert DEFAULT_ROOT == Path("/tmp/rg-nanogpt-one-head")
35+
resolved = roots()
36+
assert resolved == {
37+
"root": Path("/tmp/rg-nanogpt-one-head"),
38+
"data": Path("/tmp/rg-nanogpt-one-head/data"),
39+
"results": Path("/tmp/rg-nanogpt-one-head/results"),
40+
"plots": Path("/tmp/rg-nanogpt-one-head/plots"),
41+
}
42+
43+
44+
def test_nanogpt_sources_docs_and_notebooks_do_not_reference_home():
45+
paths = [EXPERIMENT_ROOT / "README.md"]
46+
paths.extend((EXPERIMENT_ROOT / "src").rglob("*.py"))
47+
paths.extend((EXPERIMENT_ROOT / "notebooks").glob("*.ipynb"))
48+
paths.extend((EXPERIMENT_ROOT / "configs").glob("*.yaml"))
49+
50+
violations = []
51+
for path in paths:
52+
text = path.read_text(encoding="utf-8")
53+
for token in FORBIDDEN_HOME_TOKENS:
54+
if token in text:
55+
violations.append(f"{path.relative_to(REPO_ROOT)}: {token}")
56+
57+
assert not violations, "home-directory references found:\n" + "\n".join(violations)
58+
59+
60+
def test_repo_shell_scripts_never_reference_home():
61+
violations = []
62+
for path in REPO_ROOT.rglob("*.sh"):
63+
text = path.read_text(encoding="utf-8")
64+
for token in FORBIDDEN_HOME_TOKENS:
65+
if token in text:
66+
violations.append(f"{path.relative_to(REPO_ROOT)}: {token}")
67+
68+
assert not violations, "home-directory references found in shell scripts:\n" + "\n".join(violations)
69+
70+
71+
def test_removed_nanogpt_wrapper_scripts_do_not_return():
72+
assert not (EXPERIMENT_ROOT / "scripts").exists()

0 commit comments

Comments
 (0)