Deploying and Evaluating a Fine-Tuned Model: What the GPU Taught Me
In February I wrote about building a fine-tuning pipeline with Dagster. This post picks up where that one stopped: evaluating the model, putting it on a GPU, and profiling it. I did all of it on a 16 GB T4, the smallest GPU most clouds offer, which makes every resource decision visible.
1. Predict memory before you run anything
A model's weights take parameters times bytes per parameter.
| Precision | Bytes per parameter | Gemma-2B (2.5 billion parameters) |
|---|---|---|
| 32-bit | 4 | 10 GB |
| 16-bit | 2 | 5 GB |
| 4-bit | 0.5 | about 1.3 GB |
On top of the weights comes working memory, which grows with batch size and input length. I predicted 5 GB for serving in 16-bit. It measured 5.05 GB after loading and 5.07 GB at peak while generating. A prediction you make first and check afterwards is worth more than a number you read off a dashboard, because it tells you whether you understand the system.
2. Fine-tune within the budget
To train on the same card, I loaded the model in 4-bit and used LoRA, which freezes the weights and trains small added ones. Think of a printed textbook with sticky notes in the margins. That was 9.8 million trainable parameters out of 2.5 billion, or 0.39%. Training on 4,486 cleaned recipes took about 45 minutes.
3. Evaluate with a set that made no decisions
| Epoch | Training loss | Validation loss |
|---|---|---|
| 1 | 1.419 | 1.395 |
| 2 | 1.284 | 1.388 |
| 3 | 1.176 | 1.413 |
Training loss kept falling while validation loss turned upward after epoch 2: the model had started to memorize. I kept epoch 2. The third epoch cost 15 minutes of GPU time and produced a worse model, which is its own small lesson in resource use.
Because validation chose the checkpoint, I scored the result on a separate test set of 596 recipes, next to the original model.
| Model | Test perplexity |
|---|---|
| Base Gemma-2B | 5.80 |
| Fine-tuned | 4.39 (24% lower) |
4. Profile the deployment: cold against warm
I served the model on a T4 with Modal, behind a small web page, set to scale to zero when idle.
| Measurement | Value |
|---|---|
| First request (cold) | 82.7 s |
| of which loading the model | 61.0 s |
| of which generating | 9.7 s |
| Warm request | 8.2 s |
| Throughput | About 22 tokens per second |
A cold request was ten times slower than a warm one, and nearly all of the difference was getting 5 GB of weights onto the GPU. That frames the main decision in serving:
| Option | Cold starts | Cost |
|---|---|---|
| Scale to zero | After every quiet spell | Only while answering |
| Stay warm for a while after each request | Once per session | A few idle GPU minutes |
| Keep one container always on | Never | The GPU bills around the clock |
For a demo that a few people use occasionally, I chose scale to zero. For anything people wait on, I'd pay for warmth.
5. Find where the GPU's time really goes
A GPU has its own memory, separate from the machine's main memory. Picture two rooms with a corridor between them. Work is fast inside the GPU's room, and everything that crosses the corridor has to be copied.
To see how much that costs, I ran a separate experiment on the same kind of card: downsampling a 4K video three ways.
| Run | Time | Speed-up |
|---|---|---|
| Everything on the CPU | 70.4 s | 1.0x |
| GPU encoding only | 19.1 s | 3.7x |
| Decode, scale and encode all on the GPU | 10.5 s | 6.7x |
Moving the slowest step to the GPU gave 3.7x. Keeping the frames in GPU memory from start to finish nearly doubled that again. Timing a single resize made the reason plain: the GPU's work took 30 ms, and with the copies in and out it took 208 ms. 86% of the time was the corridor.
Having a GPU available doesn't mean your code uses it, and using it doesn't mean you're using it well. Find the bottleneck, then count the trips between the two memories. It wasn't free, either: the GPU-encoded files were about 10% larger, and the fully-GPU run scored slightly lower on quality.
6. The evaluation that mattered
I sent the link to my husband, who was planning Hanukkah dinner. He asked for sea bass. A reasonable recipe came back, followed by this:
- -Recipe by:
- Linda T.,
- Lakeview, MN
- Submitted to
- Food-n-Family-Fun.com.
- 07/19/2005.
- 07/19/2005
- 07/19/2005
Two failures. The model had learned author credits that my data cleaning missed, because my pattern allowed one leading dash and some credits had two. And it didn't know where a recipe ends, so it looped.
Perplexity, 24% better, had shown neither. It also has a resource cost: every request was spending GPU seconds generating junk up to the token limit. My quick fix, a filter that cuts the output at the first credit or repeated line, hides the junk and still pays for it. The proper fix is cleaner data and an end marker on each training example, so the model stops early and each request gets cheaper.
What I'd tell myself at the start
- Predict memory from the parameter count, then measure the peak.
- Hold out a test set that makes no decisions, and score the base model on it too.
- Measure cold and warm separately. They're different problems.
- Before optimizing, find the bottleneck, then count the copies.
- Check quality whenever you make something faster.
- Read the outputs. An average score can look fine while individual outputs are broken, and wasted tokens are wasted GPU time.