Skip to content

Instantly share code, notes, and snippets.

@andrewor14
Last active August 29, 2025 20:30
Show Gist options
  • Select an option

  • Save andrewor14/ab650350b69276cf585c008914aaa146 to your computer and use it in GitHub Desktop.

Select an option

Save andrewor14/ab650350b69276cf585c008914aaa146 to your computer and use it in GitHub Desktop.
Repro unsloth full finetuning
# Based on https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.1_(8B)-Alpaca.ipynb
# but with `full_finetuning=True` and without `get_peft_model`
# Output is at the bottom of the gist
import os
from unsloth import FastLanguageModel
from transformers import TextStreamer
import torch
max_seq_length = 2048
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "unsloth/Meta-Llama-3.1-8B",
max_seq_length = max_seq_length,
dtype = torch.bfloat16,
load_in_4bit = False,
full_finetuning = True,
)
# ============
# Data prep |
# ============
alpaca_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.
### Instruction:
{}
### Input:
{}
### Response:
{}"""
EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
def formatting_prompts_func(examples):
instructions = examples["instruction"]
inputs = examples["input"]
outputs = examples["output"]
texts = []
for instruction, input, output in zip(instructions, inputs, outputs):
# Must add EOS_TOKEN, otherwise your generation will go on forever!
text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
texts.append(text)
return { "text" : texts, }
pass
from datasets import load_dataset
dataset = load_dataset("yahma/alpaca-cleaned", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)
# ========
# Train |
# ========
from trl import SFTConfig, SFTTrainer
trainer = SFTTrainer(
model = model,
tokenizer = tokenizer,
train_dataset = dataset,
dataset_text_field = "text",
max_seq_length = max_seq_length,
packing = False,
args = SFTConfig(
per_device_train_batch_size = 2,
gradient_accumulation_steps = 4,
warmup_steps = 5,
max_steps = 60,
learning_rate = 2e-4,
logging_steps = 1,
optim = "adamw_8bit",
weight_decay = 0.01,
lr_scheduler_type = "linear",
seed = 3407,
output_dir = "outputs",
report_to = "none", # Use this for WandB etc
),
)
trainer_stats = trainer.train()
# ===========
# Generate |
# ===========
inputs = tokenizer(
[
alpaca_prompt.format(
"Continue the fibonnaci sequence.", # instruction
"1, 1, 2, 3, 5, 8", # input
"", # output - leave this blank for generation!
)
], return_tensors = "pt").to("cuda")
text_streamer = TextStreamer(tokenizer)
_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 128)
# ==========
# | Output |
# ==========
# ### Instruction:
# Continue the fibonnaci sequence.
#
# ### Input:
# 1, 1, 2, 3, 5, 8
#
# ### Response:
# The fibonnaci sequence is a sequence of numbers that is used in mathematics, computer programs, computer programs, and computer games to solve problems, solve systems of linear equations, and even to generate a sequence of numbers. The sequence is calculated as follows:
#
# 1. Create an empty sequence `a`.
# 2. For every i in range(n):
# for j in range(n):
# 1. For i in range(n):
# 2. For j in range(n):
# 3. For j in range(n):
# 4. For j in range(n):
# 5. For j in range(n):
# 6. For j in range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment