Back to blog

    Deploying and Evaluating a Fine-Tuned Model: What the GPU Taught Me

    GPULLMDeploymentEvaluationProfilingMLOps

    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.

    PrecisionBytes per parameterGemma-2B (2.5 billion parameters)
    32-bit410 GB
    16-bit25 GB
    4-bit0.5about 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

    EpochTraining lossValidation loss
    11.4191.395
    21.2841.388
    31.1761.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.

    ModelTest perplexity
    Base Gemma-2B5.80
    Fine-tuned4.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.

    MeasurementValue
    First request (cold)82.7 s
    of which loading the model61.0 s
    of which generating9.7 s
    Warm request8.2 s
    ThroughputAbout 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:

    OptionCold startsCost
    Scale to zeroAfter every quiet spellOnly while answering
    Stay warm for a while after each requestOnce per sessionA few idle GPU minutes
    Keep one container always onNeverThe 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.

    RunTimeSpeed-up
    Everything on the CPU70.4 s1.0x
    GPU encoding only19.1 s3.7x
    Decode, scale and encode all on the GPU10.5 s6.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

    1. Predict memory from the parameter count, then measure the peak.
    2. Hold out a test set that makes no decisions, and score the base model on it too.
    3. Measure cold and warm separately. They're different problems.
    4. Before optimizing, find the bottleneck, then count the copies.
    5. Check quality whenever you make something faster.
    6. Read the outputs. An average score can look fine while individual outputs are broken, and wasted tokens are wasted GPU time.