Run this model with HF Jobs

Run dllwa/gemma4-form-report-lora on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/dllwa/gemma4-form-report-lora. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/dllwa/gemma4-form-report-lora on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
File size: 12,593 Bytes
3ca95cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""ํ‰๊ฐ€์šฉ ์ƒ์„ฑ โ€” base ๋ชจ๋ธ๊ณผ LoRA ์–ด๋Œ‘ํ„ฐ์— **๊ฐ™์€ ์ž…๋ ฅ**์„ ๋˜์ ธ ๋‹ต์„ ๋ฐ›์•„๋‘”๋‹ค.

์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ์ฑ„์ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ƒ์„ฑ(GPU๊ฐ€ ํ•„์š”, ๋А๋ฆผ)๊ณผ ์ฑ„์ (CPU, ๋น ๋ฆ„)์„
๋ถ„๋ฆฌํ•ด ๋‘๋ฉด, ์ฑ„์  ๊ธฐ์ค€์„ ๊ณ ์น  ๋•Œ๋งˆ๋‹ค ๋‹ค์‹œ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์‚ฌ์šฉ (๊ฐ™์€ ๋ฐ์ดํ„ฐยท๊ฐ™์€ ํ‘œ๋ณธ์œผ๋กœ ๋‘ ๋ฒˆ ๋Œ๋ฆฐ๋‹ค):
  python3 src/eval/generate.py --model <base> --data val.jsonl --n 200 --out base.jsonl
  python3 src/eval/generate.py --model <base> --adapter <runs/.../adapter> --data val.jsonl --n 200 --out tuned.jsonl
"""
import argparse
import json
import os
import random
import sys

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
from common.chat_format import build_user_turn, detect_system_support  # noqa: E402
from common.gpu_guard import require_burst_lock_if_needed  # noqa: E402


def cap_gpu_memory(fraction: float):
    """โ›” ๊ณต์šฉ GPU ๋ณดํ˜ธ. ํ‰๊ฐ€๋„ ํ•™์Šต๊ณผ ๊ฐ™์€ ์ƒํ•œ์„ ๊ฑด๋‹ค.

    *(2026-08-14: ๋„ํ‚น์ŠคํŠธ๋ง์ด `N-004`(0.19)๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ๊ทธ๊ฑด **D-022 ๋กœ ๋Œ€์ฒด๋๋‹ค.**
      ํ˜„ํ–‰์€ ๋ฝ์„ ์ฅ” ์ฑ„ `0.46`.)*
    """
    if fraction and fraction > 0:
        for i in range(torch.cuda.device_count()):
            torch.cuda.set_per_process_memory_fraction(fraction, i)
        print(f"[mem] GPU๋‹น ์ƒํ•œ {fraction:.0%}")


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--model", required=True, help="base ๋ชจ๋ธ ๊ฒฝ๋กœ/ID")
    p.add_argument("--adapter", default=None, help="LoRA ์–ด๋Œ‘ํ„ฐ ๊ฒฝ๋กœ (์—†์œผ๋ฉด base ๊ทธ๋Œ€๋กœ)")
    p.add_argument("--data", required=True, help="ํ‰๊ฐ€ํ•  jsonl (๋ณดํ†ต val)")
    p.add_argument("--out", required=True)
    p.add_argument("--n", type=int, default=200, help="ํ‰๊ฐ€ ํ‘œ๋ณธ ์ˆ˜")
    p.add_argument("--seed", type=int, default=42, help="ํ‘œ๋ณธ ์ถ”์ถœ ์‹œ๋“œ โ€” base/tuned ๋™์ผํ•ด์•ผ ํ•œ๋‹ค")
    p.add_argument("--max_new_tokens", type=int, default=512)
    p.add_argument("--batch", type=int, default=8)
    p.add_argument("--mem_fraction", type=float, default=0.46,
                   help="D-022 ํ˜„ํ–‰๊ฐ’. ๋ฝ(flock /tmp/lora_tune.lock)์„ ์ฅ๊ณ  ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. "
                        "๋ฝ ์—†์ด ๋Œ๋ฆฌ๋ ค๋ฉด 0.223 ์ดํ•˜.")
    p.add_argument("--no_repeat_ngram", type=int, default=0,
                   help="n-gram ๋ฐ˜๋ณต ๊ธˆ์ง€(0=๋”). ๋ชฉํ‘œ 2 ์ž์œ  ์ƒ์„ฑ์˜ ๋ฐ˜๋ณต ํญ์ฃผ ๋Œ€์‘ [9/23]")
    p.add_argument("--ban_chars", default="",
                   help="์ƒ์„ฑ ๊ธˆ์ง€ ๋ฌธ์ž๋“ค(์˜ˆ: 'โ–ฒ'). ๊ทธ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•œ ํ† ํฐ ์ „๋ถ€๋ฅผ bad_words ๋กœ ๋ง‰๋Š”๋‹ค. "
                        "์šฉ์ฒ˜: ์›๋ณธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ธ์ •๋ณด ๋งˆ์Šคํ‚น ๋ฌธ์ž(โ–ฒ)๋Š” ์‹ค์‚ฌ์šฉ ์ถœ๋ ฅ์— ์ •๋‹นํ•œ ์šฉ์ฒ˜๊ฐ€ ์—†๋Š”๋ฐ "
                        "์–ด๋Œ‘ํ„ฐ๊ฐ€ ๊ณผํ•™์Šตํ•ด ํญ์ฃผ ์”จ์•—์ด ๋œ๋‹ค(๋ชฉํ‘œ 3 2026-09-30 ์‹ค์ธก โ€” ๊ณจ๋“œ 69%๊ฐ€ โ–ฒ ๋ณด์œ ๋ผ "
                        "๋ฐ์ดํ„ฐ ์ œ์™ธ๋Š” ๋ถˆ๊ฐ€, ๋””์ฝ”๋”ฉ ์ฐจ๋‹จ์ด ์›๋ณธ ๋ถˆ๋ณ€ยท๊ตฌ์กฐ์  ํ•ด๋ฒ•).")
    p.add_argument("--repetition_penalty", type=float, default=1.0,
                   help="1.0=๋”(๊ธฐ๋ณธ, ์˜› ๋™์ž‘ ์œ ์ง€). ๋ฐ˜๋ณต ํญ์ฃผ๋ฅผ ์–ต์ œํ•œ๋‹ค. "
                        "**base ์™€ tuned ์— ๋ฐ˜๋“œ์‹œ ๊ฐ™์€ ๊ฐ’์„ ์ค˜์•ผ ๋น„๊ต๊ฐ€ ์„ฑ๋ฆฝํ•œ๋‹ค.** "
                        "no_repeat_ngram_size ๋ฅผ ์•ˆ ์“ฐ๋Š” ์ด์œ : '๋ณด์กฐ๊ธˆ ๊ด€๋ฆฌ์— ๊ด€ํ•œ ๋ฒ•๋ฅ ' ์ฒ˜๋Ÿผ "
                        "์ •๋‹นํ•˜๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ๋ฒ•๋ น๋ช…๊นŒ์ง€ ๋ง‰๋Š”๋‹ค.")
    p.add_argument("--system", default=None)
    p.add_argument("--max_prompt_len", type=int, default=None,
                   help="ํ”„๋กฌํ”„ํŠธ ์ ˆ๋‹จ ํ•œ๋„. ๊ธฐ๋ณธ 2048, --system ์ด ์žˆ์œผ๋ฉด 4096 โ€” 2026-10-15 ๋ฐœ๊ฒฌ: 1,514์ž ์ง€์‹œ๋ฌธ์„ ์–น์€ ์ฑ„ 2048 ๋กœ ๋‘์–ด "
                        "val ์˜ ๊ธด ์žฌ๋ฃŒ 2๊ฑด(idx 89ยท157)์ด ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ž˜๋ ค '์žฌ๋ฃŒ ์ด์–ด์“ฐ๊ธฐ' ์ถœ๋ ฅ์ด ๋๊ณ , 14์ฐจpg~17์ฐจ+pg4 ์˜ "
                        "'๊ธด ๋ฒ•๋ น ๋ฐ˜๋ณตยท์ดˆ์•ˆ ์‹คํŒจ'๋Š” ์ „๋ถ€ ๊ทธ 2๊ฑด์ด์—ˆ๋‹ค. ๋ช…์‹œํ•˜๋ฉด ๊ทธ ๊ฐ’์„ ์“ด๋‹ค.")
    p.add_argument("--attn", default="eager",
                   help="์–ดํ…์…˜ ๊ตฌํ˜„. ๊ธฐ๋ณธ 'eager'(๊ธฐ์กด ์‹คํ—˜ ์žฌํ˜„์šฉ โ€” ๋ฐ”๊พธ์ง€ ์•Š๋Š”๋‹ค). "
                        "๊ธด ํ”„๋กฌํ”„ํŠธ(์ˆ˜์ฒœ ํ† ํฐโ†‘)์—์„œ๋Š” eager ๊ฐ€ ์–ดํ…์…˜ ํ–‰๋ ฌ์„ ํ†ต์งธ๋กœ ๋งŒ๋“ค์–ด "
                        "OOM ๋‚œ๋‹ค(2026-09-08 ์‚ฌ์‹ค๊ด€๊ณ„ํ‘œ 12k ํ† ํฐ์—์„œ 16.8 GiB ์‹œ๋„) โ€” "
                        "๊ทธ๋•Œ๋Š” 'sdpa'. ํ•™์Šต ์ชฝ N-017 ๊ณผ ๊ฐ™์€ ์˜ต์…˜์ด๋ฉฐ, "
                        "**๋น„๊ต ์‹คํ—˜์˜ ๋‘ ์กฐ๊ฑด์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ฐ™์€ ๊ฐ’์„ ์ค€๋‹ค.**")
    p.add_argument("--stream", action="store_true",
                   help="๋ฐฐ์น˜๋งˆ๋‹ค ๊ฒฐ๊ณผ๋ฅผ ํŒŒ์ผ์— ์ด์–ด ์“ด๋‹ค. ์ˆ˜ ์‹œ๊ฐ„์งœ๋ฆฌ ์ž‘์—…์—์„œ ๋„์ค‘์— ์ฃฝ์–ด๋„ "
                        "๊ฑฐ๊ธฐ๊นŒ์ง€๋Š” ๋‚จ๋Š”๋‹ค. ํ‰๊ฐ€์šฉ ๊ธฐ๋ณธ ๋™์ž‘(๋์— ํ•œ ๋ฒˆ ์“ฐ๊ธฐ)์€ ๋ฐ”๊พธ์ง€ ์•Š๋Š”๋‹ค.")
    args = p.parse_args()

    # โ›” D-022: ๊ณต์กด์„ ์„ ๋„˜๋Š” ์ƒํ•œ์ด๋ฉด ๋ฝ ์—†์ด๋Š” ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค (ํ•™์Šต๊ณผ ๋™์ผ)
    require_burst_lock_if_needed(args.mem_fraction)
    cap_gpu_memory(args.mem_fraction)

    tok = AutoTokenizer.from_pretrained(args.model)
    if tok.pad_token_id is None:
        tok.pad_token = tok.eos_token
    # ๋””์ฝ”๋” ์ „์šฉ ๋ชจ๋ธ์˜ ๋ฐฐ์น˜ ์ƒ์„ฑ์€ **์™ผ์ชฝ ํŒจ๋”ฉ**์ด์–ด์•ผ ํ•œ๋‹ค.
    # ์˜ค๋ฅธ์ชฝ์— ํŒจ๋”ฉ์ด ๋ถ™์œผ๋ฉด ์ƒ์„ฑ์ด ํŒจ๋”ฉ ๋’ค์—์„œ ์‹œ์ž‘ํ•ด ์ถœ๋ ฅ์ด ๋ง๊ฐ€์ง„๋‹ค.
    tok.padding_side = "left"

    supports_system = detect_system_support(tok)

    model = AutoModelForCausalLM.from_pretrained(
        args.model, dtype=torch.bfloat16, device_map="auto",
        attn_implementation=args.attn,
    )
    if args.adapter:
        from peft import PeftModel
        model = PeftModel.from_pretrained(model, args.adapter)
        print(f"[์–ด๋Œ‘ํ„ฐ] {args.adapter}")
    else:
        print("[์–ด๋Œ‘ํ„ฐ] ์—†์Œ โ€” base ๋ชจ๋ธ")
    # ์ƒ์„ฑ๋ฌผ์˜ ์ถœ์ฒ˜๋ฅผ log ์— ๋‚จ๊ธด๋‹ค โ€” base/tuned ์— ๊ฐ™์€ ๊ฐ’์„ ์ผ๋Š”์ง€ ๋‚˜์ค‘์— ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค
    bad_words_ids = None
    if args.ban_chars:
        # ์–ดํœ˜ ์ „์ฒด์—์„œ ๊ธˆ์ง€ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ํ† ํฐ์„ ์ฐพ๋Š”๋‹ค โ€” โ–ฒ ๋Š” ๋‹จ๋…ยท์—ฐ์†ยท์กฐํ•ฉ ํ† ํฐ์ด ์—ฌ๋Ÿฟ์ด๋‹ค.
        bad_words_ids = [[tid] for t, tid in tok.get_vocab().items()
                         if any(c in tok.convert_tokens_to_string([t]) for c in args.ban_chars)]
    print(f"[๋””์ฝ”๋”ฉ] greedy, repetition_penalty={args.repetition_penalty}, no_repeat_ngram={args.no_repeat_ngram}, "
          f"ban_chars={args.ban_chars!r}({len(bad_words_ids) if bad_words_ids else 0}ํ† ํฐ), attn={args.attn}")
    print(f"[์‹œ์Šคํ…œ] {'์—†์Œ' if not args.system else repr(args.system[:60]) + f' โ€ฆ ์ด {len(args.system)}์ž'}")

    # ํ•™์Šต ๋•Œ๋Š” ๊ป์ง€๋งŒ(N-018) ์ƒ์„ฑ์—์„œ๋Š” ๋ฐ˜๋“œ์‹œ ์ผ ๋‹ค. ๋„๋ฉด ๋งค์šฐ ๋А๋ ค์ง„๋‹ค.
    model.config.use_cache = True
    model.eval()

    rows = [json.loads(l) for l in open(args.data, encoding="utf-8")]
    random.seed(args.seed)                      # base/tuned๊ฐ€ ๊ฐ™์€ ํ‘œ๋ณธ์„ ๋ณด๋„๋ก
    picks = random.sample(rows, min(args.n, len(rows)))
    print(f"[๋ฐ์ดํ„ฐ] {args.data} ์ „์ฒด {len(rows):,} ์ค‘ {len(picks)}๊ฑด (seed={args.seed})")

    results = []
    written = 0                 # --stream ์—์„œ ์ด๋ฏธ ํŒŒ์ผ์— ๊ธฐ๋กํ•œ ๊ฑด์ˆ˜
    if args.stream:
        os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True)
        open(args.out, "w").close()      # ์ด์–ด์“ฐ๊ธฐ ์ „์— ๋น„์šด๋‹ค
    n_trunc = 0                 # ํ•œ๋„๋ฅผ ๋„˜๊ฒจ **์˜ค๋ฅธ์ชฝ์ด ์ž˜๋ฆฐ** ํ”„๋กฌํ”„ํŠธ ์ˆ˜
    lim = args.max_prompt_len or (4096 if args.system else 2048)
    print(f"[ํ”„๋กฌํ”„ํŠธ ํ•œ๋„] {lim}" + (" (--system ์žˆ์Œ โ†’ ๊ธฐ๋ณธ 4096)" if args.max_prompt_len is None and args.system else ""))
    for i in range(0, len(picks), args.batch):
        chunk = picks[i: i + args.batch]
        prompts = []
        for r in chunk:
            msgs, _ = build_user_turn(r, args.system, supports_system)
            # ์ด์–ด์ง€๋Š” ๋Œ€ํ™” ์‹œ์ œ(`src/data/make_followup_probe.py`)๋งŒ ์“ฐ๋Š” ์„ ํƒ ํ•„๋“œ.
            # ์—†์œผ๋ฉด ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค โ€” ๊ธฐ์กด ๋ฐ์ดํ„ฐ์˜ ํ”„๋กฌํ”„ํŠธ๋Š” ํ•œ ๊ธ€์ž๋„ ์•ˆ ๋ฐ”๋€๋‹ค.
            msgs += r.get("history") or []
            prompts.append(tok.apply_chat_template(
                msgs, add_generation_prompt=True, tokenize=False))

        # โš ๏ธ ์•„๋ž˜ `truncation=True` ๋Š” **์˜ค๋ฅธ์ชฝ**์„ ์ž๋ฅธ๋‹ค. ํ”„๋กฌํ”„ํŠธ๊ฐ€ ๊ธธ๋ฉด ์ƒ์„ฑ ์‹œ์ž‘
        #    ํ† ํฐ(`<start_of_turn>model`)๊นŒ์ง€ ๋‚ ์•„๊ฐ€ ์ถœ๋ ฅ์ด ๋ฌด๋„ˆ์ง„๋‹ค.
        #    ์˜คํ”ˆ๋ถ ๋ฐ์ดํ„ฐ๋Š” p99 1,631 ยท ์ตœ๋Œ€ 5,838 ํ† ํฐ์ด๋ผ ์‹ค์ œ๋กœ ๊ฑธ๋ฆฐ๋‹ค.
        #    **๋ช‡ ๊ฑด์ด ๊ฑธ๋ ธ๋Š”์ง€ ๋ชจ๋ฅด๋ฉด ๊ฒฐ๊ณผ๋ฅผ ํ•ด์„ํ•  ์ˆ˜ ์—†์–ด** ์„ธ์„œ ์ฐ๋Š”๋‹ค (`docs/12` 3-5์ ˆ).
        # โš ๏ธ 2026-09-04: ํ•œ๋„๋ฅผ ์ธ์ž๋กœ ๋บ๋‹ค. ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋ถ™์—ฌ base ์™€ ๊ฒฌ์ฃผ๋Š” ์‹คํ—˜์—์„œ
        #    ํ•œ๋„๊ฐ€ ๊ณ ์ •์ด๋ฉด **๊ธด ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋ฐ›์€ ์ชฝ๋งŒ ์ž˜๋ ค** ๋น„๊ต๊ฐ€ ์กฐ์ž‘๋œ๋‹ค.
        n_trunc += sum(1 for p in prompts
                       if len(tok(p, add_special_tokens=False)["input_ids"]) > lim)

        enc = tok(prompts, return_tensors="pt", padding=True,
                  truncation=True, max_length=lim,
                  # ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด์— ์ด๋ฏธ <bos>๊ฐ€ ์žˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’(True)์ด๋ฉด ๋‘ ๋ฒˆ ๋ถ™๋Š”๋‹ค.
                  add_special_tokens=False).to(model.device)
        with torch.no_grad():
            out = model.generate(
                **enc,
                max_new_tokens=args.max_new_tokens,
                # ๊ทธ๋ฆฌ๋”” ๋””์ฝ”๋”ฉ: base์™€ tuned๋ฅผ ๊ณต์ •ํ•˜๊ฒŒ ๋น„๊ตํ•˜๋ ค๋ฉด ๋ฌด์ž‘์œ„์„ฑ์ด
                # ์—†์–ด์•ผ ํ•œ๋‹ค. ์ƒ˜ํ”Œ๋ง์„ ์“ฐ๋ฉด ๊ฐ™์€ ๋ชจ๋ธ๋„ ์‹คํ–‰๋งˆ๋‹ค ๋‹ต์ด ๋‹ฌ๋ผ์ ธ
                # ์ฐจ์ด๊ฐ€ ๋ชจ๋ธ ๋•Œ๋ฌธ์ธ์ง€ ์šด ๋•Œ๋ฌธ์ธ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.
                do_sample=False,
                # โš ๏ธ **greedy ๋Š” ๊ตฌ์กฐ์ ์œผ๋กœ ๋ฐ˜๋ณต์— ์ทจ์•ฝํ•˜๋‹ค.** ๋งค ์Šคํ… 1๋“ฑ ํ† ํฐ๋งŒ ๊ณ ๋ฅด๋ฏ€๋กœ,
                #    ํ•œ ๋ฒˆ ์–ด๋–ค ๊ตฌ์ ˆ์„ ๋ฑ‰์œผ๋ฉด ๋‹ค์Œ ์Šคํ…์˜ ๋ฌธ๋งฅ์ด ๊ฑฐ์˜ ๊ฐ™์•„ ๋˜ ๊ฐ™์€ ๊ฑธ ๊ณ ๋ฅธ๋‹ค โ†’ ๋ฃจํ”„.
                #    2026-08-14 ํ‰๊ฐ€์—์„œ tuned 200๊ฑด ์ค‘ 38๊ฑด์ด ์ด๋ ‡๊ฒŒ ๋ฌด๋„ˆ์กŒ๋‹ค
                #    (ํ•œ ๊ฑด์€ ๊ฐ™์€ 12๊ธ€์ž๊ฐ€ **63ํšŒ**, 84์ž์งœ๋ฆฌ ๋‹ต์ด 1015์ž๋กœ ํญ๋ฐœ).
                #    base ๋„ ๊ฐ™์€ greedy ์—์„œ 9๊ฑด ๋‚ฌ์œผ๋ฏ€๋กœ **๋””์ฝ”๋”ฉ๋งŒ์˜ ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์ง€๋งŒ**,
                #    ๋ง‰๋Š” ์žฅ์น˜๊ฐ€ ํ•˜๋‚˜๋„ ์—†์—ˆ๋˜ ๊ฒƒ์€ ์šฐ๋ฆฌ ์„ค์ • ๋ฌธ์ œ๋‹ค.
                #    ๊ธฐ๋ณธ๊ฐ’ 1.0 = ๋”(์˜› ๋™์ž‘ ๊ทธ๋Œ€๋กœ). ์ผค ๋•Œ๋งŒ ๋ช…์‹œํ•œ๋‹ค.
                repetition_penalty=args.repetition_penalty,
                no_repeat_ngram_size=(args.no_repeat_ngram or None),
                bad_words_ids=bad_words_ids,
                pad_token_id=tok.pad_token_id,
            )
        gen = out[:, enc["input_ids"].shape[1]:]      # ํ”„๋กฌํ”„ํŠธ ๋ถ€๋ถ„ ์ž˜๋ผ๋ƒ„
        texts = tok.batch_decode(gen, skip_special_tokens=True)

        for r, t in zip(chunk, texts):
            results.append({
                "source": r.get("source"),
                "subtype": r.get("subtype"),
                "instruction": r["instruction"],
                "input": r.get("input", ""),
                "reference": r.get("output", ""),
                "prediction": t.strip(),
            })
        if args.stream:
            # โš ๏ธ ๊ธด ์ž‘์—…(์ˆ˜ ์‹œ๊ฐ„)์€ ๋์— ํ•œ ๋ฒˆ ์“ฐ๋ฉด ๋„์ค‘์— ์ฃฝ์„ ๋•Œ ์ „๋ถ€ ์žƒ๋Š”๋‹ค
            #    (FACTS ยง2 ์˜ ๊ฐ„ํ— CUDA ์˜ค๋ฅ˜). ๋ฐฐ์น˜๋งˆ๋‹ค ์ด์–ด ๋ถ™์—ฌ ์‚ด์•„๋‚จ๊ฒŒ ํ•œ๋‹ค.
            with open(args.out, "a", encoding="utf-8") as f:
                for r in results[written:]:
                    f.write(json.dumps(r, ensure_ascii=False) + "\n")
            written = len(results)
        print(f"  {min(i + args.batch, len(picks))}/{len(picks)}", flush=True)

    os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True)
    if args.stream:
        pass                        # ์ด๋ฏธ ๋ฐฐ์น˜๋งˆ๋‹ค ๊ธฐ๋กํ–ˆ๋‹ค
    else:
        with open(args.out, "w", encoding="utf-8") as f:
            for r in results:
                f.write(json.dumps(r, ensure_ascii=False) + "\n")
    print(f"[์ €์žฅ] {args.out} ({len(results)}๊ฑด)")
    print(f"[์ ˆ๋‹จ] ํ”„๋กฌํ”„ํŠธ๊ฐ€ {lim} ์„ ๋„˜๊ฒจ ์ž˜๋ฆฐ ๊ฑด: {n_trunc}/{len(picks)} "
          f"({n_trunc / max(len(picks), 1) * 100:.1f}%)"
          + ("  โš ๏ธ ์ด ๊ฑด๋“ค์€ ์ถœ๋ ฅ์ด ๋ฌด๋„ˆ์กŒ์„ ์ˆ˜ ์žˆ๋‹ค" if n_trunc else ""))


if __name__ == "__main__":
    main()