Experiment Run Scripts (Fast execution + robust authoring)
Goals
- Run efficiently: launch parallel workers (often per-GPU) with clean logs and deterministic inputs.
- Write maintainable scripts: keep scripts as orchestrators (resource selection, sharding, logging, chaining), with the experiment logic living in Python modules/configs.
Non-negotiable conventions
- Run from repo root (or make paths robust): treat configs/data/outputs as repo-relative by default.
- Config-driven: the script takes a single
CONFIG path (YAML/JSON) and passes it through; avoid editing code to switch datasets/params.
- Stable outputs: results and logs always land in a predictable
outputs/<run-name>/... folder (or a config-defined output dir).
- Resume-first: rerunning the same command should be safe. Prefer idempotent “skip if output exists” behavior in the underlying program.
Operator quickstart (copy/paste)
Environment
- Python deps: install with the project’s dependency manager (
requirements.txt, pyproject.toml, conda env, etc.).
- GPU deps (optional): if
nvidia-smi is available, the script can shard across GPUs; otherwise it must fallback to CPU single-shard.
Common pattern: batch parallelism over seeds/tasks
Typical invocation:
bash scripts/run_experiment.sh path/to/config.yaml
Behavioral requirements:
- Parallel model: launch N background jobs, then
wait for the batch to finish.
- Per-unit logs: one log per seed/task/shard, named predictably.
- Final aggregation: after all workers finish, optionally run a merge/aggregate step.
Common pattern: GPU sharding with CPU fallback
Run:
bash scripts/run_experiment.sh path/to/config.yaml
Logs (example):
outputs/<run-name>/logs/shard_*.log
Failure handling:
- Any shard fails: rerun the same script. If resume is implemented, completed work is skipped automatically.
- Insufficient GPU memory: the script should refuse to launch and print actionable diagnostics (free memory, processes).
Post-processing chaining
Typical sequence (names are project-specific; keep the pattern):
python -m package.module --mode merge --config path/to/config.yaml
python -m package.module --mode postprocess --config path/to/config.yaml
python -m package.module --mode analyze --config path/to/config.yaml
Quality checks & debugging (preferred: dedicated scripts)
Spot-check outputs (human-auditable report)
Requirements for a good spotcheck tool:
- Accept
--input_dir / --out_dir, --n, --seed, optional --stratify.
- Produce CSV + HTML (or Markdown) so humans can audit correctness quickly.
Diagnose “feature extraction / peak detection / metrics not found”
Requirements for a good diagnostics tool:
- Never re-run expensive inference; scan existing artifacts.
- Save a summary JSON plus a sweep CSV for threshold sensitivity.
How to author new runner scripts (the agent must follow)
0) Provide one stable entrypoint
- Naming:
scripts/run_<pipeline>.sh (one script per pipeline).
- Usage:
bash scripts/run_<pipeline>.sh [config_path] with a sensible default config.
- Inputs: accept one primary argument (
CONFIG) and keep the rest as constants or environment-variable overrides.
1) Required Bash skeleton
The script must include:
set -euo pipefail
CONFIG=${1:-"path/to/default.yaml"}
LOG_DIR="outputs/<run-name>/logs" and mkdir -p "$LOG_DIR"
- GPU detection / fallback:
- If
nvidia-smi is missing: run single-shard and tee logs to shard_0.log.
- If
nvidia-smi exists: select GPUs (by free memory or explicit allowlist) and set TOTAL_SHARDS=${#AVAILABLE_GPUS[@]}.
- Shard launch: for
shard_idx=0..TOTAL_SHARDS-1, set CUDA_VISIBLE_DEVICES=$gpu_id and pass:
--shard "$shard_idx"
--total_shards "$TOTAL_SHARDS"
- redirect logs to
"$LOG_DIR/shard_${shard_idx}.log"
- Wait + failure summary: store
PIDS[], wait each PID, count failures, exit non-zero if any failed.
- Post steps: run merge/aggregate and optional analysis steps.
2) Logging & outputs (must be enforceable)
- One log per shard:
shard_${shard_idx}.log.
- Output dir is a parameter: do not hardcode file names for downstream artifacts; print where to find results.
- Print grep-friendly banners: config path, start/end time, GPU list, per-shard PID, and failing shard log paths.
3) Minimum reproducibility bar
- No code edits for runs: dataset/size/output must be configurable.
- Safe to rerun: reruns should not corrupt completed artifacts; prefer atomic writes and “skip-if-exists”.
4) Common pitfalls to avoid
- Confusing physical GPU id with shard id: shard indices should be
0..TOTAL_SHARDS-1 even if physical GPU ids are [2,5,7].
- Brittle paths: avoid assuming the current directory unless explicitly enforced.
- Silent failures: always surface exit codes and point to the exact failing log file.
1---2name: experiment-scripts-skills3description: Experiment Run Scripts (Fast execution + robust authoring)4---56# Experiment Run Scripts (Fast execution + robust authoring)78## Goals910- **Run efficiently**: launch parallel workers (often per-GPU) with clean logs and deterministic inputs.11- **Write maintainable scripts**: keep scripts as orchestrators (resource selection, sharding, logging, chaining), with the *experiment logic* living in Python modules/configs.1213## Non-negotiable conventions1415- **Run from repo root** (or make paths robust): treat configs/data/outputs as repo-relative by default.16- **Config-driven**: the script takes a single `CONFIG` path (YAML/JSON) and passes it through; avoid editing code to switch datasets/params.17- **Stable outputs**: results and logs always land in a predictable `outputs/<run-name>/...` folder (or a config-defined output dir).18- **Resume-first**: rerunning the same command should be safe. Prefer idempotent “skip if output exists” behavior in the underlying program.1920## Operator quickstart (copy/paste)2122### Environment2324- **Python deps**: install with the project’s dependency manager (`requirements.txt`, `pyproject.toml`, conda env, etc.).25- **GPU deps (optional)**: if `nvidia-smi` is available, the script can shard across GPUs; otherwise it must **fallback to CPU single-shard**.2627### Common pattern: batch parallelism over seeds/tasks2829Typical invocation:3031```bash32bash scripts/run_experiment.sh path/to/config.yaml33```3435Behavioral requirements:36- **Parallel model**: launch N background jobs, then `wait` for the batch to finish.37- **Per-unit logs**: one log per seed/task/shard, named predictably.38- **Final aggregation**: after all workers finish, optionally run a merge/aggregate step.3940### Common pattern: GPU sharding with CPU fallback4142Run:4344```bash45bash scripts/run_experiment.sh path/to/config.yaml46```4748Logs (example):4950```bash51outputs/<run-name>/logs/shard_*.log52```5354Failure handling:55- **Any shard fails**: rerun the same script. If resume is implemented, completed work is skipped automatically.56- **Insufficient GPU memory**: the script should refuse to launch and print actionable diagnostics (free memory, processes).5758### Post-processing chaining5960Typical sequence (names are project-specific; keep the *pattern*):6162```bash63python -m package.module --mode merge --config path/to/config.yaml64python -m package.module --mode postprocess --config path/to/config.yaml65python -m package.module --mode analyze --config path/to/config.yaml66```6768## Quality checks & debugging (preferred: dedicated scripts)6970### Spot-check outputs (human-auditable report)7172Requirements for a good spotcheck tool:73- Accept `--input_dir` / `--out_dir`, `--n`, `--seed`, optional `--stratify`.74- Produce **CSV + HTML** (or Markdown) so humans can audit correctness quickly.7576### Diagnose “feature extraction / peak detection / metrics not found”7778Requirements for a good diagnostics tool:79- Never re-run expensive inference; scan existing artifacts.80- Save a summary JSON plus a sweep CSV for threshold sensitivity.8182## How to author new runner scripts (the agent must follow)8384### 0) Provide one stable entrypoint8586- **Naming**: `scripts/run_<pipeline>.sh` (one script per pipeline).87- **Usage**: `bash scripts/run_<pipeline>.sh [config_path]` with a sensible default config.88- **Inputs**: accept *one primary argument* (`CONFIG`) and keep the rest as constants or environment-variable overrides.8990### 1) Required Bash skeleton9192The script must include:93- `set -euo pipefail`94- `CONFIG=${1:-"path/to/default.yaml"}`95- `LOG_DIR="outputs/<run-name>/logs"` and `mkdir -p "$LOG_DIR"`96- **GPU detection / fallback**:97 - If `nvidia-smi` is missing: run **single-shard** and `tee` logs to `shard_0.log`.98 - If `nvidia-smi` exists: select GPUs (by free memory or explicit allowlist) and set `TOTAL_SHARDS=${#AVAILABLE_GPUS[@]}`.99- **Shard launch**: for `shard_idx=0..TOTAL_SHARDS-1`, set `CUDA_VISIBLE_DEVICES=$gpu_id` and pass:100 - `--shard "$shard_idx"`101 - `--total_shards "$TOTAL_SHARDS"`102 - redirect logs to `"$LOG_DIR/shard_${shard_idx}.log"`103- **Wait + failure summary**: store `PIDS[]`, `wait` each PID, count failures, exit non-zero if any failed.104- **Post steps**: run merge/aggregate and optional analysis steps.105106### 2) Logging & outputs (must be enforceable)107108- **One log per shard**: `shard_${shard_idx}.log`.109- **Output dir is a parameter**: do not hardcode file names for downstream artifacts; print *where to find results*.110- **Print grep-friendly banners**: config path, start/end time, GPU list, per-shard PID, and failing shard log paths.111112### 3) Minimum reproducibility bar113114- **No code edits for runs**: dataset/size/output must be configurable.115- **Safe to rerun**: reruns should not corrupt completed artifacts; prefer atomic writes and “skip-if-exists”.116117### 4) Common pitfalls to avoid118119- **Confusing physical GPU id with shard id**: shard indices should be `0..TOTAL_SHARDS-1` even if physical GPU ids are `[2,5,7]`.120- **Brittle paths**: avoid assuming the current directory unless explicitly enforced.121- **Silent failures**: always surface exit codes and point to the exact failing log file.122