Fine-Tuning, and When Not ToLesson 52 of 84

Module 6 · The mechanics of a run

A working config, line by line

In this lesson

  • 10 min read
  • 5 reading sections
  • Knowledge check
  • Lesson 5 of 10 in this chapter

This chapter’s goalConfigure and launch a fine-tuning job with the correct chat template, masking and tokenizer handling, and diagnose from the logs alone whether a run is not learning, diverging, or memorising.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 6Free · no account needed
Lesson steps

The whole thing, then the reasons

This is a complete supervised fine-tuning setup for an 8B model on a 24 GB card, using TRL, PEFT and bitsandbytes, all free, all open source.

Python
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import LoraConfig
from trl import SFTConfig, SFTTrainer
import torch

MODEL = "meta-llama/Llama-3.1-8B-Instruct"

bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",
                         bnb_4bit_use_double_quant=True,
                         bnb_4bit_compute_dtype=torch.bfloat16)

tok = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForCausalLM.from_pretrained(
    MODEL, quantization_config=bnb, torch_dtype=torch.bfloat16,
    attn_implementation="flash_attention_2", device_map={"": 0})

peft_cfg = LoraConfig(
    r=16, lora_alpha=32, lora_dropout=0.05, bias="none",
    target_modules="all-linear", task_type="CAUSAL_LM")

args = SFTConfig(
    output_dir="out/triage-v3",
    num_train_epochs=2,
    per_device_train_batch_size=2,
    gradient_accumulation_steps=8,
    learning_rate=2e-4,
    lr_scheduler_type="cosine",
    warmup_ratio=0.03,
    max_grad_norm=1.0,
    bf16=True,
    optim="paged_adamw_8bit",
    gradient_checkpointing=True,
    gradient_checkpointing_kwargs={"use_reentrant": False},
    max_length=2048,
    packing=False,
    group_by_length=True,
    assistant_only_loss=True,
    logging_steps=10,
    eval_strategy="steps", eval_steps=100,
    save_steps=200, save_total_limit=3,
    seed=0,
    report_to="tensorboard")

trainer = SFTTrainer(model=model, args=args, peft_config=peft_cfg,
                     train_dataset=train, eval_dataset=val,
                     processing_class=tok)
trainer.train()

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Running Models Yourself exam.

Stuck on something?Ask Rafael, your AI tutor

How to cite: Addaly (2026) ‘A working config, line by line’, in Fine-Tuning, and When Not To. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/fine-tuning/a-config-line-by-line (Accessed: 29 November 2026).