Download train_lora_test2.py from starcrow/sft: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/starcrow/sft/resolve/main/train_lora_test2.py
- Command line
-
hf download hf://starcrow/sft/train_lora_test2.py
-
curl -L -o train_lora_test2.py https://huggingface.co/starcrow/sft/resolve/main/train_lora_test2.py
2.67 kB
| import os | |
| import torch | |
| from datasets import load_dataset | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig | |
| from peft import LoraConfig, get_peft_model | |
| from trl import SFTTrainer, SFTConfig | |
| from peft import prepare_model_for_kbit_training | |
| # โ ๋์ "์๋ณธ HF" Gemma3 ๊ฒฝ๋ก๋ก ๋ฐ๊ฟ๋ผ (EXL3 ํด๋ ๋ง๊ณ !) | |
| BASE_MODEL = r"C:\models\gemma-3-27b" # ์์ | |
| OUT_DIR = r".\loras\my_lora_01" | |
| bnb = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_quant_type="nf4", | |
| bnb_4bit_compute_dtype=torch.float16, # 3090์ด๋ฉด fp16 ์์ | |
| bnb_4bit_use_double_quant=True, | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True) | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| model = AutoModelForCausalLM.from_pretrained( | |
| BASE_MODEL, | |
| device_map="auto", | |
| quantization_config=bnb, | |
| dtype=torch.float16, | |
| ) | |
| # โ ์ฌ๊ธฐ์ k-bit ํ์ต ์ค๋น (LoRA ๋ถ์ด๊ธฐ ์ ์!) | |
| model = prepare_model_for_kbit_training(model) | |
| # Gemma ๊ณ์ด์์ ๋ณดํต ์ ๋จน๋ ํ๊น | |
| lora_cfg = LoraConfig( | |
| r=8, | |
| lora_alpha=16, | |
| lora_dropout=0.05, | |
| bias="none", | |
| task_type="CAUSAL_LM", | |
| target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"], | |
| ) | |
| model = get_peft_model(model, lora_cfg) | |
| # โ ํ์ต๋๋(=requires_grad) ํ๋ผ๋ฏธํฐ๋ฅผ FP32๋ก ๊ณ ์ ํด์ ์์น ์์ ์ฑ/ํธํ์ฑ ํ๋ณด | |
| for n, p in model.named_parameters(): | |
| if p.requires_grad: | |
| p.data = p.data.float() | |
| # (์ ํ) ์ง์ง๋ก ํ์ต ํ๋ผ๋ฏธํฐ dtype ํ์ธ | |
| print("trainable dtypes:", {p.dtype for p in model.parameters() if p.requires_grad}) | |
| ds = load_dataset("json", data_files="data_lora_test.jsonl")["train"] | |
| def format_chat(example): | |
| # messages -> ๋ชจ๋ธ chat template๋ก ๋ฌธ์์ด ์์ฑ | |
| return tokenizer.apply_chat_template(example["messages"], tokenize=False) | |
| cfg = SFTConfig( | |
| output_dir=OUT_DIR, | |
| max_length=512, | |
| per_device_train_batch_size=1, | |
| gradient_accumulation_steps=8, | |
| learning_rate=2e-4, | |
| num_train_epochs=1, | |
| logging_steps=1, | |
| save_steps=50, | |
| save_total_limit=2, | |
| fp16=False, | |
| bf16=False, | |
| report_to=[], | |
| ) | |
| trainer = SFTTrainer( | |
| model=model, | |
| processing_class=tokenizer, # <-- ์ฌ๊ธฐ๋ง ๋ฐ๋ | |
| train_dataset=ds, | |
| args=cfg, | |
| formatting_func=format_chat, | |
| ) | |
| trainer.train() | |
| # โ ์ด ์ ์ฅ์ด adapter_model.safetensors / adapter_config.json ๋ง๋ ๋ค | |
| trainer.model.save_pretrained(OUT_DIR, safe_serialization=True) | |
| tokenizer.save_pretrained(OUT_DIR) | |
| print("Saved LoRA to:", OUT_DIR) |