Fine-tuning and using a large language model on Isambard-AI
Set up → Fine-tuning? → Base model → LoRA and QLoRA
Did it work? → vLLM → Prompting → Headlines → Wrap up
Optional at the end: profiling with Nsight Systems
How do I get a GPU with the model and data ready?
apps.isambard.ac.uk/aip2-jupyter
| Field | Choose |
|---|---|
| Project | the course project |
| Job duration | until the end of the course |
| Number of GH200s | 1 |
| Partition | leave empty |
| Reservation | interactive |
When your time runs out, the session ends straight away, with no warning. Save regularly.
In JupyterLab, go to File → New → Terminal
Terminal
Reload the page, open your copy of hpc-for-ai-2.ipynb, and check the kernel in the top right says Fine-tuning + Nsight
| Course files | $PROJECTDIR/hpc-for-ai-2 |
| Model | data/hf-cache |
| Datasets | data/alpaca, data/headlines |
| If you fall behind | data/fallback |
| Your results | $PROJECTDIR/$USER/llm-fine-tuning |
The model and data were downloaded before the course, so nobody downloads 28 GB on the day
None of these are backed up
What does a base model do, and what will fine-tuning change?
Fine-tuning starts from a model that has already been trained, and trains it a little further on a smaller, specific dataset
Starts from Mistral 7B, trained only to predict the next word
Trains on 3,200 Alpaca examples: an instruction, then its answer
Learns To answer an instruction and stop, training under 1% of the parameters
It needs far less data and compute than training from scratch, because the model keeps most of what it already knows
Text goes in as tokens: words or pieces of words
The model outputs a probability for every possible next token
To generate text, pick a token, add it to the input, and run again
7.3 billion
parameters in Mistral 7B
A base model only continues text. It hasn’t been trained to answer questions.
| Training from scratch | Fine-tuning | RAG | |
|---|---|---|---|
| What changes | All the weights | Some weights of a trained model | No weights: text added to the prompt |
| Data | Vast | Hundreds to thousands of labelled examples | Your documents |
| Compute | A very large cluster | One GPU, minutes to hours | Mostly at question time |
| Good for | New foundation models | A format, style or narrow skill | Facts the model doesn’t have |
Try prompting first. Use RAG for missing facts. Fine-tune for a format, style or skill.
What does the model do before any fine-tuning?
hpc-for-ai-2.ipynb
Look at how each answer ends. Does it stop, or carry on and write a new ### Instruction: of its own?
How do you fine-tune a 7-billion-parameter model on one GPU?
16 bytes per parameter Weights, gradients, a 32-bit master copy and two optimiser values
× 7.3 billion About 117 GB
One GH200 96 GB
Only about 42 million of 7.3 billion parameters train: 0.58%
hpc-for-ai-2.ipynb
About 8 minutes. The loss is printed every 10 steps and should fall.
| LoRA | QLoRA | |
|---|---|---|
| Training time | 8.0 min | 8.8 min |
| Peak GPU memory | 16.1 GiB | 5.8 GiB |
| Final loss | 0.903 | 0.908 |
QLoRA is slower because it unpacks its 4-bit weights before every multiplication, which takes 18% of GPU time. It takes about 10% longer and uses about 64% less memory.
Use LoRA if the model fits in GPU memory, and QLoRA if it doesn’t.
Has the model learned to answer?
hpc-for-ai-2.ipynb
Seen in training The first question is Alpaca’s first example
Unseen The others show whether it learned to answer in general
Falling loss alone doesn’t prove a model is better
Add a question of your own to QUESTIONS and ask both models.
Can you find a question where the fine-tuned model is clearly worse?
It trained on 3,200 examples for 8 minutes, so it learned the question-and-answer format but no new facts.
How do we get answers fast enough to use in a program?
Batching Work on all the questions at once: 32 questions become one matrix with 32 rows
CUDA graphs Record the GPU work for one step once, then replay it, instead of Python launching every kernel
15 minutes
Start the vLLM cell before you go
hpc-for-ai-2.ipynb
With Transformers, Python sends the GPU one small job at a time, for each question and each word, so the GPU spends most of its time waiting. vLLM sends the work in batches.
How do you use a model on a list of inputs, and trust what comes back?
hpc-for-ai-2.ipynb
def run(instruction, inputs, settings=loop_settings, lora_request=adapter):
"""Ask one instruction for each input, all in one batch."""
prompts = [TEMPLATE_WITH_INPUT.format(instruction=instruction, input=text) for text in inputs]
outputs = llm.generate(prompts, settings, lora_request=lora_request)
return [out.outputs[0].text.strip() for out in outputs]Give it the whole list at once, so vLLM can batch it
Clean → Check → Retry failures → Mark as None
Clean Lower case, first word, no punctuation
Retry Only the failures, with temperature=0.7
Mark as None Rather than guess, so you can count failures
Has each UK news outlet given more or less of its front page to the environment?
29,823
headlines
| One at a time | All at once | |
|---|---|---|
| Headlines | 50 | 29,823 |
| Time per headline | 28 ms | 1.7 ms |
| GPU busy | 66% | 98% |
Gather the inputs into a list and classify them in one call
CLASSIFY, or use the base model (lora_request=None). How many labels change, and which do you trust more?How far can we trust this?
The classifier It was never taught what counts as “the environment”
Lead stories only Not everything each outlet published
Partial years 2024 starts in September, 2026 ends in October
Sampling Every 6 hours misses short-lived leads
Save regularly, and check your time left with squeue (the command is in the notes)
Share one copy of large model files in the project folder
Nothing on Isambard-AI is backed up: copy anything important somewhere safe
Please stop your server
File → Hub Control Panel → Stop My Server
Jean Golding Institute © CC-BY 4.0