Finetuning mistral 7b using Unsloth

Overview
The article uses Unsloth to fine-tune Mistral 7B in Google Colab. It introduces Unsloth as a way to fine-tune models such as Llama, Mistral, and Gemma with reduced memory use and training time. A GPU runtime is required.
Install dependencies
1!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"2!pip install --no-deps xformers trl peft accelerate bitsandbytes triton torch
Imports and configuration
1from unsloth import FastLanguageModel2import torch34max_seq_length = 20485dtype = None6load_in_4bit = True
The article explains that dtype=None allows Unsloth to detect a
suitable type for the GPU, while load_in_4bit=True enables 4-bit
loading. The selected quantized model is intended to fit within Colab's
limited GPU memory.
Example model list
1fourbit_models = [2 "unsloth/mistral-7b-v0.3-bnb-4bit",3 "unsloth/mistral-7b-instruct-v0.3-bnb-4bit",4 "unsloth/llama-3-8b-bnb-4bit",5 "unsloth/llama-3-8b-Instruct-bnb-4bit",6 "unsloth/llama-3-70b-bnb-4bit",7 "unsloth/Phi-3-mini-4k-instruct",8 "unsloth/Phi-3-medium-4k-instruct",9 "unsloth/mistral-7b-bnb-4bit",10 "unsloth/gemma-7b-bnb-4bit",11]
Load model and tokenizer
1model, tokenizer = FastLanguageModel.from_pretrained(2 model_name="unsloth/mistral-7b-instruct-v0.2-bnb-4bit",3 max_seq_length=max_seq_length,4 dtype=dtype,5 load_in_4bit=load_in_4bit,6)
A Hugging Face token can also be passed when loading gated models.
Attach LoRA adapters
1model = FastLanguageModel.get_peft_model(2 model,3 r=16,4 target_modules=[5 "q_proj", "k_proj", "v_proj", "o_proj",6 "gate_proj", "up_proj", "down_proj",7 ],8 lora_alpha=16,9 lora_dropout=0,10 bias="none",11 use_gradient_checkpointing="unsloth",12 random_state=3407,13 use_rslora=False,14 loftq_config=None,15)
The example configures LoRA adapters on attention and feed-forward projection modules. It uses Unsloth's gradient-checkpointing option to reduce memory pressure.
Format training examples
The article's example maps an input comment to a target comment. It uses
a prompt template with Input Comment and Target Comment sections,
then appends the tokenizer's end-of-sequence token.
1prompt = (2 "Below given is an input comment that needs to be converted "3 "to a vague but correct target comment.\n"4 "### Input Comment:\n{}\n"5 "### Target Comment:\n{}\n"6)78EOS_TOKEN = tokenizer.eos_token910def formatting_prompts_func(examples):11 inputs = examples["input"]12 targets = examples["target"]13 texts = []1415 for input_text, target_text in zip(inputs, targets):16 text = prompt.format(input_text, target_text) + EOS_TOKEN17 texts.append(text)1819 return {"text": texts}
Read CSV data
The article builds a dictionary from a CSV file:
1data_dict = {}23with open(data_file_path, mode="r", newline="") as input_file:4 csv_reader = csv.reader(input_file)5 data_dict["input"] = []6 data_dict["target"] = []78 for items in csv_reader:9 data_dict["target"].append(items[0])10 data_dict["input"].append(items[1])
In this example, the first CSV field is assigned to target and the
second to input.
Create and format the dataset
1from datasets import Dataset23dataset = Dataset.from_dict(data_dict)4dataset = dataset.map(formatting_prompts_func, batched=True)
Begin configuring supervised fine-tuning
The article begins setting up TRL's SFTTrainer:
1from trl import SFTTrainer2from transformers import TrainingArguments3from unsloth import is_bfloat16_supported45trainer = SFTTrainer(6 model=model,7 tokenizer=tokenizer,8 train_dataset=dataset,9 dataset_text_field="text",10 max_seq_length=max_seq_length,11)
Workflow summary
- Start a GPU-backed Colab runtime.
- Install Unsloth and its supporting libraries.
- Load a 4-bit Mistral model.
- Attach LoRA adapters.
- Format input/target examples.
- Convert CSV data into a Hugging Face Dataset.
- Configure
SFTTrainer.
Source coverage note: The public page excerpt available for this reconstruction continues beyond the trainer setup; later training arguments and the training invocation are not included here.