Skip to content

Commit 75c5627

Browse files
committed
Add shell setup instructions
1 parent 6955d81 commit 75c5627

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

.github/scripts/run_playbook_tests.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@
3737
- continue_on_error: true/false - whether to continue if this test fails (default: false)
3838
- depends_on: Comma-separated list of test IDs that must pass before this test runs
3939
- hidden: true/false - if true, hides the code block from the website (default: false)
40+
- setup: Shell commands to run before the test script (e.g. venv activation).
41+
For Python tests, wraps execution in a shell that runs the setup first:
42+
setup="source llm-env/bin/activate" → bash -c "source llm-env/bin/activate && python <script>"
43+
For shell tests, the setup commands are prepended to the script body.
44+
45+
Setup attribute:
46+
The `setup` attribute lets you specify shell commands (e.g. venv activation)
47+
that run before the test script. This is especially useful for Python code
48+
blocks which are otherwise executed directly with `python <script>`:
49+
50+
<!-- @test:id=verify-imports platform=all setup="source llm-env/bin/activate" -->
51+
```python
52+
import torch
53+
print(f"PyTorch version: {torch.__version__}")
54+
```
55+
<!-- @test:end -->
56+
57+
The runner expands this to: `bash -c "source llm-env/bin/activate && python test_verify-imports.py"`
58+
On Windows, it uses PowerShell instead of bash.
59+
60+
For shell-based tests, the setup commands are prepended to the script body.
4061
4162
Inline #hide marker:
4263
Lines ending with `#hide` inside a code block are executed by the test runner
@@ -81,6 +102,7 @@ class TestBlock:
81102
continue_on_error: bool = False
82103
depends_on: list[str] = field(default_factory=list)
83104
hidden: bool = False
105+
setup: Optional[str] = None
84106
language: str = "bash"
85107
code: str = ""
86108
line_number: int = 0
@@ -185,6 +207,7 @@ def extract_tests(readme_path: Path, target_platform: str) -> list[TestBlock]:
185207
continue_on_error=attrs.get("continue_on_error", False),
186208
depends_on=attrs.get("depends_on", []),
187209
hidden=attrs.get("hidden", False),
210+
setup=attrs.get("setup"),
188211
language=language,
189212
code=code,
190213
line_number=line_number,
@@ -256,6 +279,8 @@ def run_test(
256279
print(f"Timeout: {test.timeout}s")
257280
if test.depends_on:
258281
print(f"Dependencies: {', '.join(test.depends_on)}")
282+
if test.setup:
283+
print(f"Setup: {test.setup}")
259284
print(f"{'='*60}")
260285

261286
# Check dependencies
@@ -317,33 +342,53 @@ def run_test(
317342
# Determine shell and script extension based on language and platform
318343
is_windows = sys.platform == "win32"
319344

345+
# If setup is provided, prepend it to shell-based tests or wrap Python tests
346+
setup_prefix = test.setup if test.setup else None
347+
320348
if test.language in ["bash", "sh", "shell"]:
321349
if is_windows:
322-
# Use PowerShell on Windows for bash-like commands
323350
shell_cmd = ["powershell", "-Command"]
324351
script_content = effective_code
325352
else:
326353
shell_cmd = ["bash", "-c"]
327354
script_content = effective_code
355+
# Prepend setup commands to the shell script body
356+
if setup_prefix:
357+
script_content = f"{setup_prefix}\n{script_content}"
328358
elif test.language in ["cmd", "batch"]:
329359
shell_cmd = ["cmd", "/c"]
330360
script_content = effective_code
361+
if setup_prefix:
362+
script_content = f"{setup_prefix}\n{script_content}"
331363
elif test.language in ["powershell", "pwsh", "ps1"]:
332364
shell_cmd = ["powershell", "-Command"]
333365
script_content = effective_code
366+
if setup_prefix:
367+
script_content = f"{setup_prefix}\n{script_content}"
334368
elif test.language == "python":
335369
# For Python code blocks, write to temp file and execute
336370
script_file = results_dir / f"test_{test.id}.py"
337371
script_file.write_text(effective_code, encoding="utf-8")
338-
shell_cmd = ["python", str(script_file)]
339-
script_content = None
372+
if setup_prefix:
373+
# Wrap in a shell so setup commands (e.g. venv activation) run first
374+
if is_windows:
375+
shell_cmd = ["powershell", "-Command"]
376+
script_content = f'{setup_prefix}; python "{script_file}"'
377+
else:
378+
shell_cmd = ["bash", "-c"]
379+
script_content = f'{setup_prefix} && python "{script_file}"'
380+
else:
381+
shell_cmd = ["python", str(script_file)]
382+
script_content = None
340383
else:
341384
# Default to shell execution
342385
if is_windows:
343386
shell_cmd = ["powershell", "-Command"]
344387
else:
345388
shell_cmd = ["bash", "-c"]
346389
script_content = effective_code
390+
if setup_prefix:
391+
script_content = f"{setup_prefix}\n{script_content}"
347392

348393
# Build the command
349394
if script_content is not None:

playbooks/core/pytorch-rocm-llms/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ The included [run_llm.py](assets/run_llm.py) script shows how to load and genera
9393

9494
Take a look at how prompts are tokenized and sent to the model. Understanding this process lets you adapt LLMs for any text generation or summarization task. Here's a minimal example from the script:
9595

96-
<!-- @test:id=verify-imports platform=all timeout=60 depends_on=install-deps hidden=True -->
96+
<!-- @test:id=verify-imports platform=all timeout=60 depends_on=install-deps hidden=True setup="source llm-env/bin/activate" -->
9797
```python
9898
import torch
9999
from transformers import AutoTokenizer, AutoModelForCausalLM
@@ -104,7 +104,7 @@ print("PASS: All imports successful")
104104
```
105105
<!-- @test:end -->
106106

107-
<!-- @test:id=run-model platform=all timeout=60 depends_on=install-deps -->
107+
<!-- @test:id=run-model platform=all timeout=60 depends_on=install-deps setup="source llm-env/bin/activate" -->
108108
```python
109109
import torch
110110
from transformers import AutoTokenizer, AutoModelForCausalLM

0 commit comments

Comments
 (0)