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.
gemma4-form-report-lora / code /src /common /chat_format.py
dllwa's picture
Initial release
3ca95cd
Raw History Blame Contribute Delete
4.35 kB
#!/usr/bin/env python3
"""ํ•™์Šต๊ณผ ํ‰๊ฐ€๊ฐ€ **๋˜‘๊ฐ™์€** ๋Œ€ํ™” ํ˜•์‹์„ ์“ฐ๋„๋ก ํ•œ ๊ณณ์— ๋ชจ์•„๋‘”๋‹ค.
์™œ ๊ณตํ†ต ๋ชจ๋“ˆ๋กœ ๋นผ๋Š”๊ฐ€:
ํ•™์Šต ๋•Œ์™€ ์ถ”๋ก  ๋•Œ ํ”„๋กฌํ”„ํŠธ ํ˜•์‹์ด ์กฐ๊ธˆ์ด๋ผ๋„ ๋‹ค๋ฅด๋ฉด ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง€๋Š”๋ฐ,
์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š์•„์„œ ์›์ธ์„ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค. ์–‘์ชฝ์— ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด๋‘๋ฉด
ํ•œ์ชฝ๋งŒ ๊ณ ์ณ์ง€๋Š” ์ˆœ๊ฐ„ ๊ทธ ์‚ฌ๊ณ ๊ฐ€ ๋‚œ๋‹ค. ํ•œ ๊ณณ์—์„œ ๊ฐ€์ ธ๋‹ค ์“ฐ๋ฉด ์–ด๊ธ‹๋‚  ์ˆ˜ ์—†๋‹ค.
(docs/05_design_notes.md N-002)
"""
def detect_system_support(tok) -> bool:
"""์ด ํ† ํฌ๋‚˜์ด์ €์˜ chat template์ด system ๋กค์„ ๋ฐ›๋Š”์ง€ ์‹ค์ œ๋กœ ์‹œํ—˜ํ•œ๋‹ค.
Gemma ๊ณ„์—ด์€ ๋ฒ„์ „์— ๋”ฐ๋ผ system ๋กค์„ ๋”ฐ๋กœ ๋‘์ง€ ์•Š๋Š”๋‹ค. ๋ชจ๋ธ ์นด๋“œ๋ฅผ ์ฝ๊ณ 
ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋ฉด ๋ชจ๋ธ์„ ๋ฐ”๊ฟ€ ๋•Œ๋งˆ๋‹ค ๋‹ค์‹œ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๋Ÿฐํƒ€์ž„์— ๋ฌผ์–ด๋ณธ๋‹ค.
(N-003)
"""
try:
tok.apply_chat_template(
[{"role": "system", "content": "x"}, {"role": "user", "content": "y"}],
add_generation_prompt=True, tokenize=False,
)
return True
except Exception:
return False
def build_user_turn(record: dict, system: str | None, supports_system: bool) -> tuple[list, str]:
"""ํ•™์Šต์Œ ํ•˜๋‚˜๋ฅผ (๋ฉ”์‹œ์ง€ ๋ฆฌ์ŠคํŠธ, ์ •๋‹ต) ์œผ๋กœ ๋งŒ๋“ ๋‹ค.
instruction ๊ณผ input ์„ ๋นˆ ์ค„ ํ•˜๋‚˜๋กœ ์ž‡๋Š”๋‹ค. ๋ฐ์ดํ„ฐ์˜ input ์€ ์ƒ๋‹ด ์ „๋ฌธยท
๋ฌธ์„œ ๋ณธ๋ฌธ์ฒ˜๋Ÿผ ๊ธด ๋ฉ์–ด๋ฆฌ๋ผ, ์ง€์‹œ๋ฌธ๊ณผ ๋ถ™์—ฌ ๋‘๋ฉด ๊ฒฝ๊ณ„๊ฐ€ ํ๋ ค์ง„๋‹ค.
"""
user = record["instruction"]
if record.get("input"):
user += "\n\n" + record["input"]
msgs = []
if system:
if supports_system:
msgs.append({"role": "system", "content": system})
else:
user = system + "\n\n" + user
msgs.append({"role": "user", "content": user})
return msgs, record.get("output", "")
def derive_answer_suffix(tok) -> str:
"""์ •๋‹ต ๋’ค์— ๋ถ™๋Š” ํ„ด ์ข…๋ฃŒ ํ‘œ์‹œ๋ฅผ **ํ…œํ”Œ๋ฆฟ์—์„œ ๋ฝ‘์•„๋‚ธ๋‹ค.**
`<turn|>\\n` ๊ฐ™์€ ํ† ํฐ์„ ์ฝ”๋“œ์— ์ ์–ด๋‘๋ฉด ๋ชจ๋ธ์ด ๋ฐ”๋€” ๋•Œ ์กฐ์šฉํžˆ ์–ด๊ธ‹๋‚œ๋‹ค(N-002).
ํ‘œ์‹(sentinel)์„ ์ •๋‹ต ์ž๋ฆฌ์— ๋„ฃ์–ด ๋ Œ๋”๋งํ•œ ๋’ค, ๊ทธ ๋’ค์— ๋‚จ๋Š” ๋ฌธ์ž์—ด์„ ๊ฐ€์ ธ์˜จ๋‹ค.
"""
sentinel = "<<<ANSWER_SLOT>>>"
rendered = tok.apply_chat_template(
[{"role": "user", "content": "x"},
{"role": "assistant", "content": sentinel}],
add_generation_prompt=False, tokenize=False,
)
return rendered.split(sentinel)[-1]
def build_prompt_and_full(tok, msgs, answer, answer_suffix) -> tuple[str, str]:
"""ํ•™์Šต์— ์“ธ (ํ”„๋กฌํ”„ํŠธ, ํ”„๋กฌํ”„ํŠธ+์ •๋‹ต) ๋ฌธ์ž์—ด์„ ๋งŒ๋“ ๋‹ค.
โš ๏ธ Gemma 4 ํŠน์œ ์˜ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ํ•œ๋‹ค.
์ด ๋ชจ๋ธ์˜ **์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋Š” ๋นˆ ์‚ฌ๊ณ (thought) ์ฑ„๋„์„ ์—ฌ๋‹ซ์€ ๋’ค ๋๋‚œ๋‹ค**:
<|turn>model\\n<|channel>thought\\n<channel|>
๊ทธ๋Ÿฐ๋ฐ ํ…œํ”Œ๋ฆฟ์— ์ •๋‹ต๊นŒ์ง€ ๋„ฃ์–ด ๋ Œ๋”๋งํ•˜๋ฉด ์‚ฌ๊ณ  ์ฑ„๋„์ด **์•„์˜ˆ ๋น ์ง„๋‹ค**:
<|turn>model\\n๋‹ต๋ณ€<turn|>\\n
์ฆ‰ ํ…œํ”Œ๋ฆฟ์ด ๋งŒ๋“œ๋Š” ํ•™์Šต์šฉ ๋ฌธ์ž์—ด๊ณผ ์ถ”๋ก  ์‹œ ์‹ค์ œ ์ž…๋ ฅ์ด ์„œ๋กœ ๋‹ค๋ฅด๋‹ค.
(`reasoning_content=""` ๋ฅผ ์ค˜๋„ ์ฑ„๋„์ด ๋‚˜์˜ค์ง€ ์•Š๊ณ , ๊ฐ’์„ ์ฃผ๋ฉด ๊ทธ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ„๋‹ค)
๊ทธ๋Œ€๋กœ ๋‘๋ฉด **ํ•™์Šต ๋•Œ ๋ณธ ์  ์—†๋Š” ํ˜•ํƒœ๋ฅผ ์ถ”๋ก  ๋•Œ ๋งŒ๋‚˜๊ฒŒ ๋œ๋‹ค.** ๊ทธ๋ž˜์„œ
ํ•™์Šต ๋ฌธ์ž์—ด์„ '์ถ”๋ก ๊ณผ ๋˜‘๊ฐ™์€ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ' ๋’ค์— ์ •๋‹ต์„ ์ด์–ด ๋ถ™์—ฌ ๋งŒ๋“ ๋‹ค.
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ”„๋กฌํ”„ํŠธ๊ฐ€ ์ „์ฒด์˜ ์ ‘๋‘์‚ฌ๊ฐ€ ๋˜์–ด ์†์‹ค ๋งˆ์Šคํ‚น๋„ ์ •ํ™•ํ•ด์ง„๋‹ค.
"""
prompt = tok.apply_chat_template(msgs, add_generation_prompt=True, tokenize=False)
return prompt, prompt + answer + answer_suffix
def encode_no_special(tok, text) -> list[int]:
"""chat template ๊ฒฐ๊ณผ๋ฅผ ํ† ํฐํ™”ํ•œ๋‹ค.
`add_special_tokens=False` ์ธ ์ด์œ : ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด์— ์ด๋ฏธ `<bos>` ๊ฐ€ ๋“ค์–ด ์žˆ๋‹ค.
๊ธฐ๋ณธ๊ฐ’(True)์œผ๋กœ ๋‘๋ฉด `<bos>` ๊ฐ€ ๋‘ ๋ฒˆ ๋ถ™๋Š”๋‹ค.
๋˜ํ•œ `apply_chat_template(..., tokenize=True)` ๋ฅผ ์“ฐ์ง€ ์•Š๋Š” ์ด์œ :
transformers 5.x ์—์„œ ์ด ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ **BatchEncoding(dict)** ์„
๋Œ๋ ค์ค€๋‹ค. `len()` ์ด ํ† ํฐ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ํ‚ค ๊ฐœ์ˆ˜(2)๊ฐ€ ๋˜์–ด, ๊ธธ์ด ๋น„๊ต์™€
์Šฌ๋ผ์ด์‹ฑ์ด ์กฐ์šฉํžˆ ์ž˜๋ชป๋œ๋‹ค.
"""
return tok(text, add_special_tokens=False)["input_ids"]