Views
No views yet
apply_chat_template() method:1messages = [
2 {"role": "system", "content": "You are a helpful assistant."},
3 {"role": "user", "content": "Your question here"}
4]
5inputs = tokenizer.apply_chat_template(messages, tokenize=True, return_tensors="pt")| Model | Strict Prompt | Strict Inst | Loose Prompt | Loose Inst |
|---|---|---|---|---|
| Base | 0.4399 | 0.5731 | 0.4787 | 0.6067 |
| Fine-tuned | 0.3050 | 0.4376 | 0.3327 | 0.4700 |
| Δ | ↘ -0.1349 | ↘ -0.1355 | ↘ -0.1460 | ↘ -0.1367 |
| Benchmark | What It Tests | Base | Fine-tuned | Improvement |
|---|---|---|---|---|
| IFEval | Tests ability to follow specific instructions | 43.99% | 30.50% | ↘ -13.49% (-30.7%) |
| GSM8K | Tests math reasoning and chain-of-thought | - | - | - |
| HellaSwag | Tests real-world knowledge and common sense | - | - | - |
| MMLU | Tests broad knowledge retention (detects catastrophic forgetting) | - | - | - |
| TruthfulQA | Tests tendency to generate truthful answers | - | - | - |
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3
4# Load base model
5base_model = AutoModelForCausalLM.from_pretrained(
6 "unsloth/Llama-3.2-1B-Instruct-bnb-4bit",
7 load_in_4bit=True,
8 device_map="auto"
9)
10
11# Load LoRA adapters
12model = PeftModel.from_pretrained(base_model, "path/to/lora")
13tokenizer = AutoTokenizer.from_pretrained("unsloth/Llama-3.2-1B-Instruct-bnb-4bit")
14
15# Generate
16messages = [{"role": "user", "content": "Your question here"}]
17input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
18outputs = model.generate(input_ids, max_new_tokens=256)
19print(tokenizer.decode(outputs[0], skip_special_tokens=True))1from unsloth import FastLanguageModel
2
3model, tokenizer = FastLanguageModel.from_pretrained(
4 model_name="path/to/lora",
5 max_seq_length=2048,
6 dtype=None,
7 load_in_4bit=True,
8)
9
10# For inference
11FastLanguageModel.for_inference(model)
12
13# Generate
14messages = [{"role": "user", "content": "Your question here"}]
15inputs = tokenizer.apply_chat_template(messages, tokenize=True, return_tensors="pt").to("cuda")
16outputs = model.generate(input_ids=inputs, max_new_tokens=256)
17print(tokenizer.decode(outputs[0], skip_special_tokens=True))1from unsloth import FastLanguageModel
2
3# Load model with LoRA
4model, tokenizer = FastLanguageModel.from_pretrained(
5 model_name="path/to/lora",
6 max_seq_length=2048,
7 dtype=None,
8 load_in_4bit=True,
9)
10
11# Save merged 16-bit model
12model.save_pretrained_merged("merged_model", tokenizer, save_method="merged_16bit")
13
14# Or save as GGUF for llama.cpp/Ollama
15model.save_pretrained_gguf("model.gguf", tokenizer, quantization_method="q4_k_m")1@misc{llama_3.2_1b_instruct_bnb_4bit_lima_lora,
2 author = {Farhan Syah},
3 title = {Llama-3.2-1B-Instruct-bnb-4bit-lima Fine-tuned with LoRA},
4 year = {2025},
5 note = {Fine-tuned using Unsloth: https://github.com/unslothai/unsloth},
6 howpublished = {\url{https://github.com/farhan-syah/unsloth-finetuning}}
7}