Fine-tuning
Collection
Collection of fine-tuned LLMs.
•
25 items
•
Updated
•
1
This project fine-tunes the meta-llama/Llama-4-Scout-17B-16E-Instruct
model using a medical reasoning dataset (FreedomIntelligence/medical-o1-reasoning-SFT
) with 4-bit quantization for memory-efficient training.
pip install -U datasets accelerate peft trl bitsandbytes
pip install transformers==4.51.0
pip install huggingface_hub[hf_xet]
Make sure your Hugging Face token is stored in an environment variable:
export HF_TOKEN=your_huggingface_token
The notebook will automatically log you in using this token.
Load the Model and Tokenizer
The script downloads the Llama 4 Scout model and applies 4-bit quantization with BitsAndBytesConfig
for efficient memory usage.
Prepare the Dataset
FreedomIntelligence/medical-o1-reasoning-SFT
(first 500 samples).Fine-tuning
Push Fine-tuned Model
Here is the training notebook: Fine_tuning_llama_4
meta-llama/Llama-4-Scout-17B-16E-Instruct
nvidia-smi
check is included).Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
### Instruction:
You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning.
Please answer the following medical question.
### Question:
<medical question>
### Response:
<think>
<chain of thought>
</think>
<final answer>
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel
import torch
# Base model (original model from Meta)
base_model_id = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
# Your fine-tuned LoRA adapter repository
lora_adapter_id = "kingabzpro/Llama-4-Scout-17B-16E-Instruct-Medical-ChatBot"
# Load the model in 4-bit
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=False,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
# Load base model
base_model = AutoModelForCausalLM.from_pretrained(
base_model_id,
device_map="auto",
torch_dtype=torch.bfloat16,
quantization_config=bnb_config,
trust_remote_code=True,
)
# Attach the LoRA adapter
model = PeftModel.from_pretrained(
base_model,
lora_adapter_id,
device_map="auto",
trust_remote_code=True,
)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
# Inference example
prompt = """Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
### Instruction:
You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning.
Please answer the following medical question.
### Question:
What is the initial management for a patient presenting with diabetic ketoacidosis (DKA)?
### Response:
<think>
"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=500)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)
Base model
meta-llama/Llama-4-Scout-17B-16E