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: 4,346 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 | #!/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"]
|