Your First Training Run: Step-by-Step

Your First Training Run: Step-by-Step

Mission Control: Ignition. Follow this complete, end-to-end recipe to launch your first successful fine-tuning job and produce your first custom model checkpoint.

Your First Training Run: Mission Control

You have the data. You have the GPU. You have the environment. Now, it is time to launch.

In this final lesson of Module 8, we will pull every concept we have learned so far—tokenization, loss masking, hyperparameters, and monitoring—into a single, executable Python script. This is the "Hello World" of fine-tuning.

Follow this step-by-step recipe to produce your first custom model checkpoint.


The Pre-Flight Checklist

Before you run the script, ensure:

  1. GPU Check: Run nvidia-smi in your terminal. You should see a GPU with at least 16GB of VRAM (preferably 24GB or more).
  2. Dataset Check: Your train.jsonl file should be in the same folder.
  3. Packages Check: You should have transformers, datasets, peft, bitsandbytes, and accelerate installed.

STEP 1: Load the Model and Tokenizer

We will use Mistral-7B as our base. It is powerful, efficient, and fits on most prosumer GPUs.

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "mistralai/Mistral-7B-v0.3"

# 1. Load Tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token # Essential for Mistral

# 2. Load Model 
# We use 'device_map' to automatically place it on the GPU
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16, 
    device_map="auto"
)

STEP 2: Load and Prepare your Dataset

We use the pipeline technique we built in Module 7.

from datasets import load_dataset

# Load your 'Golden Dataset'
dataset = load_dataset("json", data_files="train.jsonl", split="train")

def tokenize_step(example):
    # Instruction/Response mapping
    full_text = f"USER: {example['instruction']}\nASSISTANT: {example['response']}"
    return tokenizer(full_text, truncation=True, max_length=512)

tokenized_dataset = dataset.map(tokenize_step, remove_columns=dataset.column_names)

STEP 3: Configure the Trainer

This is where you set the Hyperparameters we learned in Lesson 3.

from transformers import TrainingArguments, Trainer

training_args = TrainingArguments(
    output_dir="./first-model-checkpoints",
    per_device_train_batch_size=4,
    gradient_accumulation_steps=4, # Total Batch Size = 16
    
    learning_rate=2e-4, # The sweet spot
    num_train_epochs=3,   # 3 passes for the 100 examples
    
    logging_steps=10,
    save_strategy="epoch", # Save a checkpoint after every lap
    
    fp16=True, # Half-precision for speed
    report_to="none" # Turn off wandb for the first simple run
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset,
    data_collator=lambda data: tokenizer.pad_batch(data, return_tensors="pt")
)

STEP 4: Ignition (The Training Loop)

This is the moment of truth.

print("--- Starting Training ---")
trainer.train()

# Save the final result
trainer.save_model("./my-final-fine-tuned-model")
print("--- Mission Complete! ---")

What to Observe During the Run

  1. The Progress Bar: You will see a progress bar move through the "Steps."
  2. The Loss Value: On your monitor, the "Loss" should start around 2.0-3.0 and slowly drop toward 0.5-0.8.
  3. VRAM Usage: Use a separate terminal to run watch -n 1 nvidia-smi. You should see the memory "filling up" as the training starts.

Visualizing the Successful Run

graph TD
    A["Script Launch"] --> B["Weights Loaded into VRAM"]
    B --> C["Data Streamed to GPU"]
    C --> D["Forward Pass (Prediction)"]
    D --> E["Loss Calculation"]
    E --> F["Backward Pass (Updates)"]
    F --> G["Loop (Epoch 1, 2, 3)"]
    G --> H["Final Checkpoint Saved"]
    
    subgraph "The 'Active' Training"
    D
    E
    F
    end

Troubleshooting Common First-Run Failures

  • OOM (Out of Memory): Your max_length or batch_size is too high. Decrease batch_size to 1 and increase gradient_accumulation_steps to compensate.
  • Model isn't learning: Your learning_rate might be set to the wrong order of magnitude (e.g., 2e-1 instead of 2e-4).
  • Tokenizer Error: Ensure you set tokenizer.pad_token = tokenizer.eos_token.

Summary and Key Takeaways

  • Recipe: Fine-tuning is a sequence of loading the model, processing data, and configuring the trainer.
  • Stability: Start with simple settings (low batch size, no monitoring) before adding complexity.
  • Checkpoints: Always save your model after every epoch in case the computer crashes or the power goes out.
  • Ignition: Once the first run is complete, you are officially an AI fine-tuning practitioner!

Congratulations! You have completed Module 8. You have trained your first model. But wait—that run probably required a very expensive GPU. What if we want to train on a $500 gaming card?

In Module 9, we will explore Parameter-Efficient Fine-Tuning (PEFT), where we learn how to train models using only 1% of the weights.


Reflection Exercise

  1. In Step 3, why did we set save_strategy="epoch"? What would happen if the training took 10 hours and your internet cut out at hour 9?
  2. Look at Step 4. Why is trainer.save_model() separate from trainer.train()?

SEO Metadata & Keywords

Focus Keywords: Fine-tuning step by step tutorial, your first SFT run, Mistral 7B fine-tuning code, Hugging Face trainer ignition, launching AI training job. Meta Description: Take the leap from theory to practice. Follow this complete Python guide to launching your first successful fine-tuning run with Mistral-7B, from data loading to saving your first checkpoint.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn