File size: 18,437 Bytes
29fc577 | 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | #!/usr/bin/env python3
"""
data/generate_repetition_preference.py β Self-play preference data targeting repetition.
Generates (prompt, chosen, rejected) pairs by:
- rejected: greedy decoding (temp=0, rep_penalty=1.0) β tends to repeat
- chosen: sampling with repetition penalty (temp=0.7, rep_penalty=1.2) β cleaner
Only keeps pairs where rejected has strictly higher 3-gram repetition rate than chosen.
Usage:
python3 data/generate_repetition_preference.py \
--checkpoint checkpoints/3b_dpo/checkpoint-slerp
python3 data/generate_repetition_preference.py \
--checkpoint checkpoints/3b_dpo/checkpoint-slerp \
--output data/preference/repetition_preference.jsonl \
--num_prompts 100 \
--max_tokens 256
"""
from __future__ import annotations
import argparse
import json
import math
import os
import sys
import time
from pathlib import Path
from typing import List, Optional
import torch
import torch.nn.functional as F
_PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(_PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(_PROJECT_ROOT))
from model import LLM # noqa: E402
from tokenizers import Tokenizer # noqa: E402
# ---------------------------------------------------------------------------
# Korean prompt bank β 100+ diverse prompts
# ---------------------------------------------------------------------------
# 15 existing eval prompts (completion style β wrapped in chat template)
_EVAL_PROMPTS = [
"λνλ―Όκ΅μ μλλ μ΄λμΈμ§ μ€λͺ
ν΄μ£ΌμΈμ.",
"μΈκ³΅μ§λ₯μ΄λ 무μμΈμ§ μμΈν μ€λͺ
ν΄μ£ΌμΈμ.",
"νκ΅μ μ ν΅ μμ μ€μμ λνμ μΈ κ²λ€μ μκ°ν΄μ£ΌμΈμ.",
"μ§κ΅¬ μ¨λνμ μ£Όμ μμΈμ 무μμΈκ°μ?",
"νλ‘κ·Έλλ°μ λ°°μ°λ €λ©΄ μ΄λ»κ² μμν΄μΌ νλμ?",
"μ‘°μ μλμλ μ΄λ€ μΌλ€μ΄ μμλμ?",
"물리νμμ μλμ§λ 무μμΈμ§ μ€λͺ
ν΄μ£ΌμΈμ.",
"νκ΅μ΄λ μΈκ³μμ μ΄λ€ νΉμ§μ κ°μ§κ³ μλμ?",
"κ²½μ μ±μ₯μ μν΄μλ 무μμ΄ νμνκ°μ?",
"μ°μ£Ό νμ¬μ μμ¬λ₯Ό κ°λ¨ν μ€λͺ
ν΄μ£ΌμΈμ.",
"λ¨Έμ λ¬λκ³Ό λ₯λ¬λμ μ°¨μ΄λ 무μμΈκ°μ?",
"νκ΅ λ¬Ένμ λνμ μΈ μνμΌλ‘λ μ΄λ€ κ²λ€μ΄ μλμ?",
"μμ μ»΄ν¨ν°λ 무μμΈμ§ μ€λͺ
ν΄μ£ΌμΈμ.",
"건κ°ν μμ΅κ΄μ μν΄μλ μ΄λ»κ² ν΄μΌ νλμ?",
"μΈκ³ 2μ°¨ λμ μ΄ν μΈκ³λ μ΄λ»κ² λ³νλμ?",
]
# Additional diverse prompts (~85 more)
_EXTRA_PROMPTS = [
# μΌμ λν
"μ€λ λ μ¨κ° μ’μλ° λ νλ©΄ μ’μκΉμ?",
"μ£Όλ§μ λ νλ©΄ μ’μμ§ μΆμ²ν΄μ£ΌμΈμ.",
"μ’μ ν루λ₯Ό μμνλ λ°©λ²μ μλ €μ£ΌμΈμ.",
"μ§μμ ν μ μλ μ·¨λ―Έ νλμ μΆμ²ν΄μ£ΌμΈμ.",
"μΉκ΅¬μ μΈμ μ λ μ΄λ»κ² νν΄νλ©΄ μ’μκΉμ?",
"μΈλ‘μμ λλ λ μ΄λ»κ² 극볡ν μ μλμ?",
"μκ° κ΄λ¦¬λ₯Ό μ νλ λ°©λ²μ μλ €μ£ΌμΈμ.",
"μμΉ¨ μΌμ° μΌμ΄λλ μ΅κ΄μ λ§λ€λ €λ©΄ μ΄λ»κ² ν΄μΌ νλμ?",
"μλ‘μ΄ λμλ‘ μ΄μ¬νμ λ μ μνλ λ°©λ²μ?",
"μΉ΄νμμ νΌμ μκ° λ³΄λ΄λ κ²μ μ₯μ μ 무μμΈκ°μ?",
# μ§μ β κ³Όν
"DNAκ° λ¬΄μμΈμ§ μ€λͺ
ν΄μ£ΌμΈμ.",
"λΈλνμ΄λ 무μμΈκ°μ?",
"μ§νλ‘ μ΄λ 무μμΈμ§ κ°λ¨ν μ€λͺ
ν΄μ£ΌμΈμ.",
"κΈ°ν λ³νκ° μνκ³μ λ―ΈμΉλ μν₯μ 무μμΈκ°μ?",
"μΈμ²΄μ λ©΄μ μμ€ν
μ μ΄λ»κ² μλνλμ?",
"λΉμ μλλ μ μ€μνκ°μ?",
"μμμ λΆμμ μ°¨μ΄μ μ 무μμΈκ°μ?",
"κ΄ν©μ±μ΄λ 무μμΈμ§ μ€λͺ
ν΄μ£ΌμΈμ.",
"μ€λ ₯νλ 무μμΈκ°μ?",
"μ€κΈ°μΈν¬ μΉλ£λ 무μμ΄λ©° μ΄λ»κ² νμ©λλμ?",
# μ§μ β μμ¬Β·μ¬ν
"νκ΅μ μμ¬μμ κ°μ₯ μ€μν μ¬κ±΄μ 무μμΈκ°μ?",
"λ―Όμ£Όμ£Όμλ 무μμΈμ§ μ€λͺ
ν΄μ£ΌμΈμ.",
"μ°μ
νλͺ
μ΄ μΈκ³μ λ―ΈμΉ μν₯μ 무μμΈκ°μ?",
"λμ μ΄λ 무μμ΄μλμ?",
"νκ΅ μ μμ μμΈκ³Ό κ²°κ³Όλ₯Ό μ€λͺ
ν΄μ£ΌμΈμ.",
"μΈκ³νλ 무μμ΄λ©° μ΄λ€ μν₯μ λ―ΈμΉλμ?",
"μΈκΆμ΄λ 무μμ΄λ©° μ μ€μνκ°μ?",
"μ€ν¬λ‘λκ° μμ¬μ μΌλ‘ μ€μν μ΄μ λ 무μμΈκ°μ?",
"λ₯΄λ€μμ€ μλλ μ΄λ€ μκΈ°μλμ?",
"νκ΅μ λ
립μ΄λμ λν΄ μ€λͺ
ν΄μ£ΌμΈμ.",
# μ‘°μΈ β μ§μ
Β·νμ΅
"μ·¨μ
λ©΄μ μ 보λ λ°©λ²μ 무μμΈκ°μ?",
"μ΄λ ₯μλ₯Ό μ μ°λ λ°©λ²μ μλ €μ£ΌμΈμ.",
"λν μνμ μμ°¨κ² λ³΄λ΄λ λ°©λ²μ?",
"κ³΅λΆ μ§μ€λ ₯μ λμ΄λ λ°©λ²μ μλ €μ£ΌμΈμ.",
"μΈκ΅μ΄λ₯Ό λΉ λ₯΄κ² λ°°μ°λ λ°©λ²μ 무μμΈκ°μ?",
"μ§μ₯μμ μμ¬μ μ μ§λ΄λ λ°©λ²μ?",
"ν리λμλ‘ μΌνλ©΄ μ΄λ€ μ₯λ¨μ μ΄ μλμ?",
"μκΈ°μκ°μλ₯Ό μ μ°λ νμ μλ €μ£ΌμΈμ.",
"λ
μ μ΅κ΄μ κΈ°λ₯΄λ λ°©λ²μ 무μμΈκ°μ?",
"μνμ μνκΈ° μν κ³΅λΆ λ°©λ²μ?",
# μ‘°μΈ β 건κ°Β·μ¬λ¦¬
"μ€νΈλ μ€ ν΄μ λ°©λ²μ μλ €μ£ΌμΈμ.",
"μ°μΈκ°μ 극볡νλ λ°©λ²μ 무μμΈκ°μ?",
"κ·μΉμ μΈ μ΄λ μ΅κ΄μ λ§λλ λ°©λ²μ?",
"μλ©΄μ μ§μ λμ΄λ λ°©λ²μ μλ €μ£ΌμΈμ.",
"λ²μμμ μλ°©νλ λ°©λ²μ 무μμΈκ°μ?",
"λ§μμ ννλ₯Ό μ°Ύλ λ°©λ²μ?",
"μμ‘΄κ°μ λμ΄λ λ°©λ²μ μλ €μ£ΌμΈμ.",
"λͺ
μμ μμνλ €λ©΄ μ΄λ»κ² ν΄μΌ νλμ?",
"건κ°ν 체μ€μ μ μ§νλ λ°©λ²μ?",
"λμ§νΈ μ€λ
μ 극볡νλ λ°©λ²μ μλ €μ£ΌμΈμ.",
# μ°½μ
"μ§§μ λνλ₯Ό νλ λ§λ€μ΄μ£ΌμΈμ.",
"λ΄μ λν μλ₯Ό μ¨μ£ΌμΈμ.",
"λ―Έλ λμλ₯Ό λ°°κ²½μΌλ‘ ν μ§§μ μ΄μΌκΈ°λ₯Ό μ¨μ£ΌμΈμ.",
"λ°λ€μ κ΄ν μ§§μ μνμ μ¨μ£ΌμΈμ.",
"κ³ μμ΄λ₯Ό μ£ΌμΈκ³΅μΌλ‘ ν μ§§μ μ΄μΌκΈ°λ₯Ό λ§λ€μ΄μ£ΌμΈμ.",
"κ°μ νκ²½μ λ¬μ¬νλ κΈμ μ¨μ£ΌμΈμ.",
"μ°μ μ κ΄ν μ§§μ μλ₯Ό μ¨μ£ΌμΈμ.",
"μλ§μκ² λ³΄λ΄λ νΈμ§λ₯Ό μ¨μ£ΌμΈμ.",
"λ―Έλμ λμκ² μ°λ νΈμ§λ₯Ό μμ±ν΄μ£ΌμΈμ.",
"μ΄λ¦° μμ μΆμ΅μ κ΄ν μ§§μ κΈμ μ¨μ£ΌμΈμ.",
# κΈ°μ Β·IT
"ν΄λΌμ°λ μ»΄ν¨ν
μ΄λ 무μμΈκ°μ?",
"λΈλ‘체μΈμ΄ 무μμΈμ§ μ€λͺ
ν΄μ£ΌμΈμ.",
"μ¬μ΄λ² 보μμ΄ μ μ€μνκ°μ?",
"λΉ
λ°μ΄ν°λ 무μμ΄λ©° μ΄λ»κ² νμ©λλμ?",
"5G κΈ°μ μ΄ κ°μ Έμ¬ λ³νλ 무μμΈκ°μ?",
"μΈν°λ· κ²μ μμ§μ μ΄λ»κ² μλνλμ?",
"μ€λ§νΈν°μ΄ μνμ λ―ΈμΉ μν₯μ 무μμΈκ°μ?",
"κ°μνμ€κ³Ό μ¦κ°νμ€μ μ°¨μ΄λ 무μμΈκ°μ?",
"μμ¨μ£Όν μλμ°¨ κΈ°μ μ μ΄λκΉμ§ μλμ?",
"μ€νμμ€ μννΈμ¨μ΄λ 무μμΈκ°μ?",
# λ¬ΈνΒ·μμ
"K-νμ΄ μΈκ³μ μΌλ‘ μΈκΈ°λ₯Ό μ»μ μ΄μ λ 무μμΈκ°μ?",
"νκ΅ μνκ° μΈκ³ μμ₯μμ μ£Όλͺ©λ°λ μ΄μ λ?",
"μ ν΅ μμ κ³Ό νλ μμ μ μ°¨μ΄λ 무μμΈκ°μ?",
"μμ
μ΄ κ°μ μ λ―ΈμΉλ μν₯μ 무μμΈκ°μ?",
"λ
μκ° μΆμ λ―ΈμΉλ κΈμ μ μΈ μν₯μ?",
"λ―Έμ κ°μμ μ νλ λ°©λ²μ μλ €μ£ΌμΈμ.",
"νκ΅ μ ν΅ μμ
μΈ κ΅μ
μ νΉμ§μ 무μμΈκ°μ?",
"μν λΉνμ μ μ°λ λ°©λ²μ?",
"μ¬νμ΄ μ¬λμ μ±μ₯μν€λ μ΄μ λ 무μμΈκ°μ?",
"μ¬μ§ μ°κΈ°λ₯Ό μ νλ νμ μλ €μ£ΌμΈμ.",
# νκ²½Β·μ¬ν
"νκ²½ 보νΈλ₯Ό μν΄ κ°μΈμ΄ ν μ μλ μΌμ?",
"μ¬νμ©μ μ€μμ±κ³Ό λ°©λ²μ μ€λͺ
ν΄μ£ΌμΈμ.",
"μ±μμ£Όμμ μ₯λ¨μ μ 무μμΈκ°μ?",
"λλ¬Ό 볡μ§λ 무μμ΄λ©° μ μ€μνκ°μ?",
"μ§μ κ°λ₯ν λ°μ μ΄λ 무μμΈκ°μ?",
"λ
Έλ Ήν μ¬νκ° κ°μ Έμ€λ λ¬Έμ μ μ 무μμΈκ°μ?",
"κ΅μ‘ λΆνλ±μ ν΄μνλ λ°©λ²μ?",
"λΉκ³€ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν λ°©λ²μ?",
"λ€λ¬Έν μ¬νμμ 곡쑴νλ λ°©λ²μ?",
"λ΄μ¬ νλμ΄ μ¬νμ λ―ΈμΉλ μν₯μ 무μμΈκ°μ?",
]
ALL_PROMPTS = _EVAL_PROMPTS + _EXTRA_PROMPTS # 15 + 85 = 100
CHAT_TEMPLATE = "<|user|>\n{prompt}\n<|assistant|>\n"
EOS_TOKEN_ID = 2
# ---------------------------------------------------------------------------
# Repetition metric
# ---------------------------------------------------------------------------
def compute_ngram_repetition_rate(tokens: List[int], n: int = 3) -> float:
"""Fraction of n-gram positions that are repeats of an earlier occurrence."""
if len(tokens) < n:
return 0.0
ngrams = [tuple(tokens[i: i + n]) for i in range(len(tokens) - n + 1)]
if not ngrams:
return 0.0
seen: set = set()
repeated = 0
for ng in ngrams:
if ng in seen:
repeated += 1
seen.add(ng)
return repeated / len(ngrams)
# ---------------------------------------------------------------------------
# Generation
# ---------------------------------------------------------------------------
@torch.inference_mode()
def generate(
model: torch.nn.Module,
input_ids: torch.Tensor,
max_new_tokens: int,
temperature: float,
repetition_penalty: float,
eos_token_id: int,
) -> List[int]:
"""Auto-regressive generation with optional repetition penalty.
Args:
model: LLM instance already on device
input_ids: (1, T) prompt token ids
max_new_tokens: max tokens to generate
temperature: sampling temperature (0 = greedy)
repetition_penalty: penalty > 1 reduces prob of previously seen tokens
eos_token_id: stop generation when this token is produced
Returns:
List of generated token ids (not including the prompt).
"""
device = input_ids.device
generated: List[int] = []
current_ids = input_ids.clone() # (1, T)
for _ in range(max_new_tokens):
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
logits, _ = model(current_ids) # (1, T, V)
next_logits = logits[0, -1, :].float() # (V,)
# Repetition penalty: discount logits for already-generated tokens
if repetition_penalty != 1.0:
all_seen_ids = current_ids[0].tolist() + generated
for token_id in set(all_seen_ids):
if token_id < next_logits.shape[0]:
if next_logits[token_id] < 0:
next_logits[token_id] *= repetition_penalty
else:
next_logits[token_id] /= repetition_penalty
# Sample / greedy
if temperature == 0.0:
next_token = int(next_logits.argmax())
else:
next_logits = next_logits / temperature
probs = F.softmax(next_logits, dim=-1)
next_token = int(torch.multinomial(probs, num_samples=1).item())
generated.append(next_token)
if next_token == eos_token_id:
break
# Append to context
next_tensor = torch.tensor([[next_token]], dtype=torch.long, device=device)
current_ids = torch.cat([current_ids, next_tensor], dim=1)
return generated
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Generate self-play repetition preference data"
)
parser.add_argument(
"--checkpoint",
type=Path,
default=Path("checkpoints/3b_dpo/checkpoint-slerp"),
help="Path to model checkpoint directory",
)
parser.add_argument(
"--output",
type=Path,
default=Path("data/preference/repetition_preference.jsonl"),
help="Output JSONL path",
)
parser.add_argument(
"--num_prompts",
type=int,
default=None,
help="How many prompts to use (default: all ~100)",
)
parser.add_argument(
"--max_tokens",
type=int,
default=256,
help="Max new tokens per generation",
)
parser.add_argument(
"--tokenizer",
type=Path,
default=None,
help="Path to tokenizer.json (default: auto-resolve)",
)
parser.add_argument(
"--device",
type=str,
default="cuda:0",
help="Torch device string",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Random seed for reproducibility",
)
parser.add_argument(
"--min_rep_diff",
type=float,
default=0.0,
help="Minimum difference (rejected_rep - chosen_rep) to keep a pair (default: >0)",
)
return parser.parse_args()
def _resolve_tokenizer(args: argparse.Namespace) -> Path:
if args.tokenizer is not None:
return Path(args.tokenizer)
# Try checkpoint dir first
ckpt_tok = args.checkpoint / "tokenizer.json"
if ckpt_tok.exists():
return ckpt_tok
# Fall back to project default
default_tok = _PROJECT_ROOT / "tokenizer" / "korean_sp" / "tokenizer.json"
if default_tok.exists():
return default_tok
raise FileNotFoundError(
"Cannot find tokenizer.json β specify with --tokenizer"
)
def main() -> None:
args = parse_args()
# Reproducibility
torch.manual_seed(args.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
# Prompts
prompts = ALL_PROMPTS
if args.num_prompts is not None:
prompts = prompts[: args.num_prompts]
print(f"[INFO] Using {len(prompts)} prompts")
# Device
device = torch.device(args.device if torch.cuda.is_available() else "cpu")
print(f"[INFO] Device: {device}")
# Tokenizer
tokenizer_path = _resolve_tokenizer(args)
print(f"[INFO] Loading tokenizer from {tokenizer_path}")
tokenizer = Tokenizer.from_file(str(tokenizer_path))
# Model
checkpoint_path = _PROJECT_ROOT / args.checkpoint if not args.checkpoint.is_absolute() else args.checkpoint
if not checkpoint_path.exists():
raise FileNotFoundError(f"Checkpoint not found: {checkpoint_path}")
print(f"[INFO] Loading model from {checkpoint_path} ...")
t0 = time.perf_counter()
model = LLM.from_pretrained(checkpoint_path)
model = model.to(device=device, dtype=torch.bfloat16)
model.eval()
print(f"[INFO] Model loaded in {time.perf_counter() - t0:.1f}s")
# Output dir
output_path = _PROJECT_ROOT / args.output if not args.output.is_absolute() else args.output
output_path.parent.mkdir(parents=True, exist_ok=True)
# Stats
valid_pairs = 0
skipped = 0
total_rejected_rep = 0.0
total_chosen_rep = 0.0
t_start = time.perf_counter()
with open(output_path, "w", encoding="utf-8") as fout:
for idx, prompt_text in enumerate(prompts):
prompt_str = CHAT_TEMPLATE.format(prompt=prompt_text)
# Tokenize prompt
encoding = tokenizer.encode(prompt_str)
prompt_ids = encoding.ids
if not prompt_ids:
print(f" [{idx+1}/{len(prompts)}] SKIP: empty tokenization for prompt")
skipped += 1
continue
input_ids = torch.tensor([prompt_ids], dtype=torch.long, device=device)
# --- Generate REJECTED: greedy, no rep penalty ---
rej_tokens = generate(
model=model,
input_ids=input_ids,
max_new_tokens=args.max_tokens,
temperature=0.0,
repetition_penalty=1.0,
eos_token_id=EOS_TOKEN_ID,
)
# --- Generate CHOSEN: sampling + rep penalty ---
cho_tokens = generate(
model=model,
input_ids=input_ids,
max_new_tokens=args.max_tokens,
temperature=0.7,
repetition_penalty=1.2,
eos_token_id=EOS_TOKEN_ID,
)
# Decode (strip EOS)
rej_clean = [t for t in rej_tokens if t != EOS_TOKEN_ID]
cho_clean = [t for t in cho_tokens if t != EOS_TOKEN_ID]
rej_text = tokenizer.decode(rej_clean)
cho_text = tokenizer.decode(cho_clean)
# Compute 3-gram repetition rates on generated tokens
rej_rep = compute_ngram_repetition_rate(rej_clean, n=3)
cho_rep = compute_ngram_repetition_rate(cho_clean, n=3)
# Filter: only keep if rejected is more repetitive than chosen
diff = rej_rep - cho_rep
if diff <= args.min_rep_diff:
status = "SKIP"
skipped += 1
else:
status = "KEEP"
valid_pairs += 1
total_rejected_rep += rej_rep
total_chosen_rep += cho_rep
record = {
"prompt": prompt_str,
"chosen": cho_text,
"rejected": rej_text,
}
fout.write(json.dumps(record, ensure_ascii=False) + "\n")
elapsed = time.perf_counter() - t_start
print(
f" [{idx+1:3d}/{len(prompts)}] {status:4s} "
f"rej_rep={rej_rep:.3f} cho_rep={cho_rep:.3f} diff={diff:+.3f} "
f"| rej_len={len(rej_clean)} cho_len={len(cho_clean)} "
f"| elapsed={elapsed:.1f}s"
)
# Summary
elapsed_total = time.perf_counter() - t_start
print()
print("=" * 60)
print(f"Generation complete in {elapsed_total:.1f}s")
print(f" Total prompts processed : {len(prompts)}")
print(f" Valid pairs kept : {valid_pairs}")
print(f" Skipped (rep filter) : {skipped}")
if valid_pairs > 0:
avg_rej = total_rejected_rep / valid_pairs
avg_cho = total_chosen_rep / valid_pairs
print(f" Avg rejected 3-gram rep : {avg_rej:.4f}")
print(f" Avg chosen 3-gram rep : {avg_cho:.4f}")
print(f" Avg improvement : {avg_rej - avg_cho:+.4f}")
print(f" Output saved to : {output_path}")
print("=" * 60)
if __name__ == "__main__":
main()
|