Train Your Own AI

Fine-Tune Your First LLM with LoRA on a $0.50/hr GPU

By Tyler Babin · FlopSource Research

Training your own AI does not take a data center. With LoRA fine-tuning, one rented GPU that costs less than a cup of coffee per hour, and about sixty minutes, you can teach a 7-billion-parameter model to follow your instructions on your data. This is the companion tutorial to Train Your Own AI, Episode 1 — every command below is verified against current documentation. Total compute cost for the run: under a dollar.

What you're building

A base model like Qwen 2.5 knows a little about everything and isn't great at anything specific. Fine-tuning teaches it your task — your support docs, your coding style, your product's voice. The old way meant retraining all seven billion parameters. LoRA (Low-Rank Adaptation) instead freezes the base model completely and trains a tiny adapter bolted onto the side: a few million trainable parameters instead of seven billion, roughly one tenth of one percent.

QLoRA adds quantization: the frozen base model is stored in 4-bit NormalFloat, a format designed so quantization barely dents accuracy, while gradients still flow in higher precision during training. The whole thing fits in about 5 GB of VRAM. A rented RTX 4090 has 24 GB.

The GPU: rent, don't buy

You don't buy anything — you rent by the second. Current market rates for an RTX 4090 with 24 GB of VRAM:

This run needs the GPU for roughly one to two hours. That's under a dollar, total. Prices move daily, which is why the FlopSource directory tracks GPU pricing across providers — always check it before you spin up a box.

Your dataset matters more than hyperparameters

The model learns exactly what you feed it. The format is instruction / input / output triples:

{
  "instruction": "Classify the sentiment of this review.",
  "input": "This GPU is a steal.",
  "output": "Positive"
}

One thousand clean, consistent examples will beat fifty thousand noisy ones every single time. Good sources: public datasets on the Hugging Face Hub for warm-up (the Alpaca cleaned dataset is the standard starting point), your own logs reformatted into triples, and synthetic data generated by a bigger model and curated by hand. Dedupe aggressively and eyeball at least a hundred examples yourself.

The critical detail: examples must be wrapped in the model's chat template — the special tokens marking where the user message ends and the assistant answer begins. Get this wrong and the model trains on garbage. Unsloth's trainer handles it with one line:

from datasets import Dataset

dataset = Dataset.from_list([
    {"text": tokenizer.apply_chat_template(row["messages"], tokenize=False)}
    for row in rows
])

Install and load the model

Spin up a PyTorch-template pod, open a terminal, and install Unsloth from the official install script:

curl -fsSL https://unsloth.ai/install.sh | sh

Then load the model in 4-bit. We use Qwen 2.5 7B Instruct because it's fully open — no access request, no waiting on approvals like some gated models:

from unsloth import FastLanguageModel

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "Qwen/Qwen2.5-7B-Instruct",
    max_seq_length = 2048,
    load_in_4bit = True,
)

LoRA config and the trainer

Bolt on the adapter. Rank 16 is the sweet spot for a first run — bigger rank means more capacity but slower training and more VRAM, and you rarely need more than 32:

model = FastLanguageModel.get_peft_model(
    model,
    r = 16,
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                      "gate_proj", "up_proj", "down_proj"],
    lora_alpha = 16,
    lora_dropout = 0,
    bias = "none",
    use_gradient_checkpointing = "unsloth",
    random_state = 3407,
)

Then configure the trainer. Memorize these four numbers: r=16, alpha=16, learning rate 2e-4, 60 steps. They carry you through 90 percent of LoRA runs:

from trl import SFTTrainer, SFTConfig

trainer = SFTTrainer(
    model = model,
    train_dataset = dataset,
    args = SFTConfig(
        dataset_text_field = "text",
        max_seq_length = 2048,
        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",
        output_dir = "outputs",
    ),
)
trainer.train()

Watch the loss curve: it should fall fast, then flatten. Sixty steps on a thousand examples is roughly two epochs — for a first fine-tune, one to three epochs is the norm. More than that and the model starts memorizing instead of learning. Save checkpoints along the way so you can roll back without paying for another run.

Test, save, and three pitfalls

Switch to inference mode, test against the base model, and save the adapter — it's a few dozen megabytes versus 14 GB for the full model:

FastLanguageModel.for_inference(model)
inputs = tokenizer(["Classify the sentiment: This GPU is a steal."], return_tensors = "pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens = 64, use_cache = True)
print(tokenizer.batch_decode(outputs))

model.save_pretrained("lora_model")  # a few dozen MB — that's the whole fine-tune

Three pitfalls that ruin first runs:

Always hold out a few examples to test on — training loss going down means nothing if the model can't handle prompts it hasn't seen.

What it costs, and what's next

You fine-tuned a 7-billion-parameter model for less than the price of a coffee, on a GPU you rented by the second and handed back when you were done. When you're ready to run it, don't overpay for the box — check the FlopSource directory for the cheapest available hardware first.

Next in the series: building your dataset with synthetic data, for tasks where no dataset exists yet.

Find the cheapest GPU for your next run

Live pricing, hardware specs, and availability across 30 GPU clouds, neoclouds, and bare-metal providers.

Open the Directory