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.
Instructions to use dllwa/gemma4-form-report-lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use dllwa/gemma4-form-report-lora with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("google/gemma-4-31B-it") model = PeftModel.from_pretrained(base_model, "dllwa/gemma4-form-report-lora") - Notebooks
- Google Colab
- Kaggle
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()
|