Download scripts/phase5_repetition_split.py from Anupam-Rawat-IITB/code-switch-mtp: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/Anupam-Rawat-IITB/code-switch-mtp/resolve/main/scripts/phase5_repetition_split.py
- Command line
-
hf download hf://Anupam-Rawat-IITB/code-switch-mtp/scripts/phase5_repetition_split.py
-
curl -L -o phase5_repetition_split.py https://huggingface.co/Anupam-Rawat-IITB/code-switch-mtp/resolve/main/scripts/phase5_repetition_split.py
1.93 kB
| """Phase 5 check: do the drafting gains hold on documents whose greedy continuation is not repetitive? | |
| Regenerates the greedy continuations used by phase5_dependent_eval.py (same | |
| documents, same order), measures the share of each continuation covered by a | |
| repeated 4-gram, and writes it per document so tokens per pass can be split | |
| by how repetitive the continuation is. | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| from pathlib import Path | |
| os.environ.setdefault("CUDA_DEVICE_ORDER", "PCI_BUS_ID") | |
| REPO = Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(REPO)) | |
| sys.path.insert(0, str(REPO / "scripts")) | |
| import torch | |
| def main() -> None: | |
| p = argparse.ArgumentParser(description=__doc__) | |
| p.add_argument("--name", required=True) | |
| p.add_argument("--heldout", required=True) | |
| p.add_argument("--out", required=True) | |
| p.add_argument("--spec-docs", type=int, default=256) | |
| args = p.parse_args() | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import phase4_boundary_decoding as pb | |
| from phase5_dependent_eval import BACKBONE | |
| from phase5_shortlist_pilot import DEVICE, repeated_fraction | |
| from src.train.data import load_heldout_texts | |
| tok = AutoTokenizer.from_pretrained(BACKBONE) | |
| backbone = AutoModelForCausalLM.from_pretrained(BACKBONE, dtype=torch.float32).to(DEVICE).eval() | |
| eos = backbone.generation_config.eos_token_id | |
| eos_ids = set(eos if isinstance(eos, list) else [eos]) | |
| texts = load_heldout_texts(args.heldout) | |
| pairs = pb.generate_continuations(backbone, tok, texts[: args.spec_docs], 256, 64 + pb.LOOKAHEAD, 16, eos_ids) | |
| rows = [{"length": len(cont), "repeated_4gram_fraction": repeated_fraction(cont)} for _, cont in pairs] | |
| Path(args.out).write_text(json.dumps({"name": args.name, "docs": rows}, indent=1)) | |
| print(f"{args.name}: {len(rows)} continuations") | |
| if __name__ == "__main__": | |
| main() | |