REAL PYTORCH · CPU TRAINING AND AUTOGRAD

Project: Compare full tuning and LoRA budgets

Compare identical splits and step budgets; measure trainable tensors, AdamW state and export bytes.

Run this Lab in Pythilo

Open Labs, choose its topic and tap Download entire topic, or Download entire category for all its Labs, including the required Python packages, then load the starter or worked answer and tap Trace. Progress, pause, resume and removal are in Downloads. Independent checks execute real CPU training and gradients.

That explicit download includes the shared UCI sentiment dataset/tokenizer and 17.7 MB pretrained BERT weights. Its source stays local. After setup it trains and evaluates offline; no API key is needed.

PyTorch is unavailable in this browser runtime. The code and lessons are readable here; execution and checks run in the app.

Pythilo on the App Store →

Download complete project source →

Read lesson: Project: Compare full tuning and LoRA budgets →

Your task

Compare identical splits and step budgets; measure trainable tensors, AdamW state and export bytes. Implement run_project(output, quick=False). Follow the reference report/artifact schema; the checks run quick=True with actual pretrained weights and real data. The full dataset is the default.

Starter

from pathlib import Path
import torch
import pythilo_finetune as ft


def run_project(output, quick=False):
    raise NotImplementedError("Implement the training, evidence and artifact contract in the task")
Inspect the worked answer
"""Same data, initial classifier, batch order, sequence length and optimizer-step budget."""
from pathlib import Path
import pythilo_finetune as ft
from finetune_projects.full import run_project as full_training
from finetune_projects.lora import run_project as lora_training


def run_project(output, quick=False):
    output = Path(output)
    full = full_training(output / 'full', quick)
    lora = lora_training(output / 'lora', quick)
    if full['split_ids'] != lora['split_ids'] or full['steps'] != lora['steps']:
        raise ValueError('Unfair comparison: data or optimizer-step budget differs')
    # Learning rates differ by method and are declared, not selected on the test.
    report = dict(full=full, lora=lora, full_learning_rate=2e-4, lora_learning_rate=1e-3,
                  trainable_ratio=lora['trainable_parameters'] / full['trainable_parameters'],
                  optimizer_ratio=lora['optimizer_state_bytes'] / full['optimizer_state_bytes'],
                  artifact_ratio=lora['artifact_bytes'] / full['artifact_bytes'],
                  same_step_budget=True, quick=quick,
                  interpretation='Parameter and optimizer tensor bytes are measured. They are not a peak-RAM measurement. Test scores are descriptive; neither method is guaranteed to win.')
    ft.save_json(output / 'comparison.json', report)
    return report

About this algorithm

How Project: Compare full tuning and LoRA budgets works

The idea

Full fine-tuning and LoRA are two ways to adapt the same pretrained model, and comparing them only means something if everything else is held equal. This project runs both on identical train, validation and test splits, with one sequence length, batch order and number of optimizer steps, and refuses to report if the splits or step counts differ.

It then measures three costs instead of estimating them: how many parameters are trainable, how many bytes of state AdamW keeps, and how large the saved file is. For LoRA on BERT-Tiny each comes out under 1% of full fine-tuning. Accuracy is reported for both but not ranked, since neither method is guaranteed to win. The two learning rates are declared up front, not tuned on the test set.

How it works

  1. Run the full fine-tuning project into output / 'full' and the LoRA project into output / 'lora' with the same quick flag.
  2. Raise ValueError if the two reports differ in split_ids or in steps.
  3. Compute trainable_ratio as LoRA's trainable parameters divided by full fine-tuning's.
  4. Compute optimizer_ratio from the measured AdamW state bytes and artifact_ratio from the sizes of adapter.pt and model.pt.
  5. Record both learning rates, 2e-4 for full and 1e-3 for LoRA, together with both complete reports.
  6. Save everything to comparison.json and return it.

What it costs

Trainable
≈4.4M vs ≈4,400 weights
AdamW state
under 1% with LoRA
Saved file
under 1% with LoRA

The ratios come from real tensors and files, not peak RAM; LoRA still runs the whole frozen network on every step, so time per step shrinks far less than the parameter count.

Where it is used

  • Hu and co-authors' 2021 LoRA paper reported cutting GPT-3's trainable parameters by about 10,000 times while matching full fine-tuning quality on its tasks.
  • Hugging Face PEFT models print this comparison with print_trainable_parameters(), which shows trainable against total parameters.
  • Apple's on-device foundation models specialise one shared base model for different features by loading small LoRA adapters.

Watch out for

  • Comparing runs with different data splits or step counts, where a difference in score may come from the budget and not the method.
  • Reading trainable-parameter savings as peak memory savings: activations and the frozen weights still have to be held in memory.
  • Tuning each method's learning rate on test scores and then presenting those same scores as the comparison.