To fine-tune a model using Unsloth, it's essential to prepare your dataset meticulously. Here's a step-by-step guide to help you through the process:
1. Understand Model Requirements
Different models have specific input formats. For instance, QwN-E2.5 uses an instruction-following format, while LLaMA employs prompts with special tokens. Familiarize yourself with the target model's expected input structure by consulting its official documentation[1].
2. Collect and Clean Data
Gather Relevant Data: Obtain datasets pertinent to your application, either from repositories like Hugging Face or by curating your own.
Remove Duplicates: Ensure your dataset doesn't contain duplicate entries to prevent bias and inefficiency.
Eliminate Irrelevant Data: Filter out any content that doesn't align with your training objectives, such as spam or incomplete entries.
Ensure Consistent Formatting: Maintain a uniform structure throughout your dataset to facilitate better learning[1].
3. Format the Data
Structure your data according to the model's requirements. For example, for QwN-E2.5, you might use:
code
### Instruction:
What is the capital of France?
### Response:
The capital of France is Paris.
For LLaMA:
code
<s>[INST] What is the capital of France? [/INST] The capital of France is Paris. </s>
Utilize templates to ensure consistency. Automating this process with scripts can enhance efficiency[1].
4. Validate the Data
Consistency Checks: Verify that all entries adhere to the prescribed format.
Token Length Verification: Ensure input sequences don't exceed the model's maximum token limit, typically 2048 tokens.
Quality Assurance: Manually review a subset of your dataset to confirm its quality and relevance[1].
5. Structure Data for Unsloth
Unsloth typically works with CSV or JSON formats. Organize your data accordingly:
code
instruction,response
"What is the capital of France?","The capital of France is Paris."
Upload your formatted file to the environment where Unsloth is running, such as Google Colab or Kaggle[1].
6. Load and Standardize the Dataset
Use Unsloth's functions to load and standardize your dataset. For example, to load a ShareGPT dataset:
python
from unsloth.chat_templates import standardize_sharegpt
from datasets import load_dataset
dataset = load_dataset("Sulav/mental_health_counseling_conversations_sharegpt", split="train")
Then, format the dataset for training:
python
def formatting_prompts_func(examples):
convos = examples["conversations"]
texts = [tokenizer.apply_chat_template(convo, tokenize=False, add_generation_prompt=False) for convo in convos]
return {"text": texts}
dataset = dataset.map(formatting_prompts_func, batched=True)
```[2]
**7. Set Up the Trainer for Fine-Tuning**
Configure the trainer with appropriate parameters:
```python
from trl import SFTTrainer
from transformers import TrainingArguments, DataCollatorForSeq2Seq
from unsloth import is_bfloat16_supported
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
dataset_text_field="text",
max_seq_length=max_seq_length,
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),
dataset_num_proc=2,
packing=False,
args=TrainingArguments(
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
warmup_steps=5,
max_steps=60,
learning_rate=2e-4,
fp16=not is_bfloat16_supported(),
bf16=is_bfloat16_supported(),
logging_steps=1,
optim="adamw_8bit",
weight_decay=0.01,
lr_scheduler_type="linear",
seed=3407,
output_dir="outputs",
report_to="none",
),
)
```[2]
By following these steps, you'll prepare your dataset effectively for fine-tuning with Unsloth, ensuring optimal model performance.