"""Generate test passages with a small open model: unmarked, SynthID-Text and KGW. python generate.py prompts.json results/generated.jsonl [--limit N] Both watermarks are the open implementations that ship with Hugging Face transformers. SynthID-Text is the technique Google DeepMind published; KGW is the "green list" scheme of Kirchenbauer et al. The keys below are ours, made up for this test. Nothing here uses, or says anything about, any vendor's keys. The run can be interrupted and restarted: finished ids are skipped. """ import json import sys import time import torch from transformers import ( AutoModelForCausalLM, AutoTokenizer, SynthIDTextWatermarkingConfig, WatermarkingConfig, ) from wm_config import KGW_ARGS, MODEL_NAME, SYNTHID_ARGS, pick_device prompts_path, out_path = sys.argv[1], sys.argv[2] limit = int(sys.argv[sys.argv.index("--limit") + 1]) if "--limit" in sys.argv else None device = pick_device() tok = AutoTokenizer.from_pretrained(MODEL_NAME) model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16 if device != "cpu" else torch.float32) model.to(device).eval() SCHEMES = { "unmarked": None, "synthid": SynthIDTextWatermarkingConfig(**SYNTHID_ARGS), "kgw": WatermarkingConfig(**KGW_ARGS), } prompts = json.load(open(prompts_path)) if limit: prompts = prompts[:limit] try: done = {json.loads(line)["id"] for line in open(out_path) if line.strip()} except FileNotFoundError: done = set() for p_index, prompt in enumerate(prompts): for scheme, cfg in SCHEMES.items(): sample_id = f"p{p_index:02d}-{scheme}" if sample_id in done: continue messages = [ {"role": "system", "content": "You are a professional writer. Write clear, natural prose in full paragraphs. Do not use headings or bullet points."}, {"role": "user", "content": prompt}, ] enc = tok.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(device) torch.manual_seed(1000 + p_index) started = time.time() with torch.no_grad(): out = model.generate( **enc, do_sample=True, temperature=0.8, top_k=40, min_new_tokens=280, max_new_tokens=400, watermarking_config=cfg, ) new_tokens = out[0, enc["input_ids"].shape[1]:] text = tok.decode(new_tokens, skip_special_tokens=True).strip() row = {"id": sample_id, "prompt": p_index, "scheme": scheme, "text": text, "tokens": int(new_tokens.shape[0]), "seconds": round(time.time() - started, 1)} with open(out_path, "a") as f: f.write(json.dumps(row, ensure_ascii=False) + "\n") print(sample_id, row["tokens"], "tokens", row["seconds"], "s", flush=True)