Download code/cp_causal_bootstrap.sh from bgradowhite/rende-nlp-cp: direct link, hf CLI and curl.
- Browser
- Download file 3.91 kB
-
https://huggingface.co/datasets/bgradowhite/rende-nlp-cp/resolve/main/code/cp_causal_bootstrap.sh
- Command line
-
hf download hf://datasets/bgradowhite/rende-nlp-cp/code/cp_causal_bootstrap.sh
-
curl -L -o cp_causal_bootstrap.sh https://huggingface.co/datasets/bgradowhite/rende-nlp-cp/resolve/main/code/cp_causal_bootstrap.sh
3.91 kB
| # Runs ON a RunPod pod. Builds the CLEAN (low-order-faithful) clone corpus with a causal | |
| # factored-attention generator and exact ancestral sampling, then self-terminates. | |
| # | |
| # bash cp_causal_bootstrap.sh train <order> <steps> | |
| # bash cp_causal_bootstrap.sh sample <order> <shard> <n_shards> <n_seqs> | |
| # | |
| # Two modes on purpose: the generator is trained ONCE and its weights uploaded, then many pods | |
| # sample from that single generator. If every pod trained its own, the corpus would be a mixture | |
| # of slightly different distributions -- small, but exactly the kind of contamination this whole | |
| # exercise exists to remove. | |
| set -x | |
| MODE=${1:?train|sample}; ORDER=${2:-9} | |
| case "$ORDER" in 3) NL=2;; 9) NL=4;; 33) NL=6;; *) echo "bad order"; exit 1;; esac | |
| cd /root | |
| apt-get update -qq && apt-get install -y -qq tmux >/dev/null 2>&1 | |
| pip install -q huggingface_hub numpy | |
| python - <<'PY' | |
| import os, shutil | |
| from huggingface_hub import hf_hub_download | |
| os.makedirs("/root/results/nlp_cp", exist_ok=True) | |
| for repo, f, dest in [("bgradowhite/rende-nlp-fig1", "results/train.npy", "train.npy"), | |
| ("bgradowhite/rende-nlp-fig1", "results/test.npy", "test.npy")]: | |
| p = hf_hub_download(repo, f, repo_type="dataset", local_dir="/root/dl") | |
| shutil.copy(p, f"/root/results/nlp_cp/{dest}") | |
| print("data ready") | |
| PY | |
| nvidia-smi --query-gpu=name,ecc.errors.uncorrected.volatile.total --format=csv | |
| python -c "import torch;assert torch.cuda.is_available();print('cuda OK',torch.cuda.get_device_name(0))" || { | |
| echo "GPU UNUSABLE"; runpodctl remove pod "$RUNPOD_POD_ID"; exit 1; } | |
| if [ "$MODE" = "train" ]; then | |
| STEPS=${3:-12000} | |
| python -u causal_factored.py --data /root/results/nlp_cp/train.npy --layers "$NL" \ | |
| --steps "$STEPS" --batch 128 --n-sample 2000 --save /root/causal_k$ORDER.pt \ | |
| --out /root/results/causal 2>&1 | tee /root/train_k$ORDER.log | |
| python - <<PY | |
| from huggingface_hub import HfApi | |
| api = HfApi(); api.create_repo("bgradowhite/rende-nlp-cp", repo_type="dataset", exist_ok=True) | |
| for f, n in [("/root/causal_k$ORDER.pt", "causal_k$ORDER.pt"), | |
| ("/root/train_k$ORDER.log", "logs/causal_train_k$ORDER.log"), | |
| ("/root/results/causal/causal_ladder.json", "logs/causal_ladder_k$ORDER.json")]: | |
| try: | |
| api.upload_file(path_or_fileobj=f, path_in_repo=n, | |
| repo_id="bgradowhite/rende-nlp-cp", repo_type="dataset") | |
| print("uploaded", n) | |
| except Exception as e: | |
| print("upload failed", n, e) | |
| PY | |
| else | |
| SHARD=${3:?shard}; N_SHARDS=${4:?n_shards}; N_SEQS=${5:-400000} | |
| python - "$ORDER" "$SHARD" "$N_SHARDS" "$N_SEQS" <<'PY' | |
| import sys, numpy as np, torch | |
| from huggingface_hub import hf_hub_download, HfApi | |
| sys.path.insert(0, "/root") | |
| from causal_factored import load, DEV, marginal_fidelity | |
| order, shard, n_shards, n_seqs = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]) | |
| w = hf_hub_download("bgradowhite/rende-nlp-cp", f"causal_k{order}.pt", | |
| repo_type="dataset", local_dir="/root/dl2") | |
| m = load(w) | |
| per = n_seqs // n_shards | |
| gen = torch.Generator(device=DEV).manual_seed(1000 + shard) | |
| parts, chunk = [], 2000 | |
| done = 0 | |
| while done < per: | |
| b = min(chunk, per - done) | |
| parts.append(m.sample(b, generator=gen).cpu().numpy().astype(np.int16)) | |
| done += b | |
| np.save(f"/root/clone{order}c_shard{shard}.npy", np.concatenate(parts)) | |
| print(f" {done}/{per} sampled", flush=True) | |
| arr = np.concatenate(parts) | |
| tr = np.load("/root/results/nlp_cp/train.npy") | |
| tv = marginal_fidelity(arr, tr, m.V) | |
| print(f"shard {shard}: {len(arr)} seqs, unigram TV vs real {tv:.4f}", flush=True) | |
| api = HfApi() | |
| api.upload_file(path_or_fileobj=f"/root/clone{order}c_shard{shard}.npy", | |
| path_in_repo=f"clone{order}c_shard{shard}.npy", | |
| repo_id="bgradowhite/rende-nlp-cp", repo_type="dataset") | |
| print("uploaded") | |
| PY | |
| fi | |
| runpodctl remove pod "$RUNPOD_POD_ID" | |