File size: 1,926 Bytes
3bbd5dd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | """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
@torch.no_grad()
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()
|