Fine-Tuning Local Models for the GPU Poor — Teaching Qwen3.8-27B Narrow Tasks via QLoRA

I’ve been running Qwen3.8-27B (Unsloth 4-bit GGUF) on my local RTX 4090 for a few months now. It never occurred to me that local fine-tuning was possible on a single GPU designed for gaming — the model weights themselves take up 16 of my 24 GiB of total VRAM, and I am already running a Q4 quantization of a BF16 model.

While it’s unlikely that fine-tuning a small local model unlocks new general capabilities, we should be able to teach it narrow, stylistic abilities such as specific coding tasks, a writing style, or API-specific knowledge.

I have Blender 5.2 installed on my machine and found that Qwen3.8 does a bad job of writing Blender scripts, probably due to the large amount of training data with older API calls (v4 and below). So let’s fine-tune our local Qwen to create dioramas in Blender 5.2!

This task combines knowledge update (V5 API spec), coding style, and visual style transfer all in one. The technique transfers in a straightforward manner to harness-specific fine-tuning, internal tool usage, or creating a ‘writing like me’ model.

Here’s a before/after of the results (a diorama of the famous shootout scene from Sergio Leone’s Spaghetti Western masterpiece The Good, the Bad and the Ugly), then let’s get into what I learned about fitting a fine-tuning pipeline into a single GPU.

Two Blender renders of the final standoff from The Good, the Bad and the Ugly, both built by Qwen3.8-27B. Left, untuned: a mostly empty tan sky, with a strip of ground, a few bare trees and grave markers along the bottom edge. Right, fine-tuned: a low-poly diorama on a floating sandy disc, with three gunmen around a stone circle, crosses, headstones and a cactus, lit from one side against a dark background. The request is printed underneath: I'd like the final standoff from The Good, the Bad and the Ugly: three gunmen facing each other in the stone circle at the middle of the huge cemetery.

Take Account of Our Inference Mode Memory Usage

First, let’s take account of our existing inference setup: for a 3,000-token context we use about 17 GiB of our 24 GiB total VRAM, with 6.9 GiB free for any fine-tuning setup (less in actuality, since the GPU is also running my desktop).

A 24 GiB RTX 4090 drawn as a box, filled in layers while running Qwen3.8-27B with 3,000 tokens of context: 0.5 GiB kept by the driver, 15.8 GiB of model weights, 0.3 GiB of context, 0.6 GiB of runtime overhead, and 6.9 GiB free. A label notes that the file averages 5.3 bits per weight, not 4.

Quantization gets tricky when you are saving every penny: why is my Q4 model taking up almost 16 GiB of VRAM? 27,000,000,000 × 4 = 108B bits, or 13.5B bytes. It turns out our 4-bit quantization actually uses a number of quantization formats for specific model parts, each averaging anywhere from 4.25 to 6.56 bits per weight (with a very small number of norm weights actually saved in FP32).

The reason for this is both simple and clever: 4 bits can only represent 2^4 unique values, which would be too coarse for weight representation. Therefore, a separate scale value is attached to groups of weights (groups of 32 weights in the case of Q4) that allows us to unpack back to a higher floating-point range at inference time. Here’s a breakdown of the quantization types used in Qwen3.8-27B-UD-Q4_K_XL, along with their average memory usage per weight parameter.

Stacks showing bits per weight. The model as released in BF16 is 16 bits. Inside the GGUF file: Q5_K at 5.5 bits (69% of weights), IQ4_XS at 4.25 bits (21%), Q6_K at 6.56 bits (5%) and Q4_K at 4.5 bits (5%), for an average of 5.24 bits over the whole model. The top slice of each stack is the scales, stored once per block of 256 weights.

Compared to the model’s native BF16, this gives us great memory savings for inference. However, this also means we cannot fine-tune directly on the quantized model — fine-tuning requires small steps in weight adjustments, and the 4-bit quant’s 16-value representation means each learning step is either rounded away or far too large.

Native Full Fine-Tuning

For a full fine-tune we should load the model in BF16 for the precision it provides when applying learning rates in the range of 1e-5 to 1e-3. This puts additional strain on our VRAM budget, since on top of the model weights, during each backpropagation we also need to keep the following:

  • A gradient number per weight, to be multiplied by the learning rate (this needs to be at least BF16 too, ideally FP32, since gradients can be very small and could use the extra precision).
  • A copy of the activations over the entire context length — these are multiplied with the loss gradient of the next layer to calculate the gradient for the current step.
  • The loss over the entire vocabulary over the entire sequence — this allows us to calculate the loss gradient per vocabulary logit and backprop the loss gradient through the model.
  • The AdamW optimizer we use keeps exponentially decaying first and second moment values per model weight, which should also be in higher precision. This helps us learn faster and escape local minima in solution space.

All in all, this means fine-tuning our model in BF16 would require 332 GiB of VRAM, or 14 RTX 4090 cards. And even if we pretend that we can indeed fine-tune the entire model on some version of the Q4 model (suppose we are fine-tuning in NVFP4 on a Blackwell card), we still need 115 GiB of memory, or 5 RTX 4090s.

Two rows of 24 GiB RTX 4090 boxes. Top row, a full fine-tune in BF16: 50.1 GiB of weights, 50.1 GiB of gradients, 100.2 GiB of AdamW optimizer state, about 122 GiB of activations and 9.4 GiB of loss for 3,000 tokens, about 332 GiB or 14 cards. Bottom row, pretending to train entirely in the GGUF's 4-bit format: 16.4 GiB of weights, 16.4 of gradients, 32.8 of optimizer state, about 40 of activations and 9.4 of loss, about 115 GiB or 5 cards.

QLoRA to the Rescue

Some of you have probably been screaming “just use QLoRA” from the jump. Yes, QLoRA, or Quantized Low-Rank Adaptation, is probably the most memory-efficient way to fine-tune a model that barely fits in your GPU. But it’s good to first get a sense of the scale of the full fine-tune task to fully appreciate QLoRA’s memory savings.

In our QLoRA setup, we freeze our base model in a 4-bit format and add a smaller-rank adapter (rank 16 in our case) on every full attention and MLP layer. The adapter output is added to the frozen base model’s output, and only the adapter weights are trained during backpropagation.

As an example, for a single MLP layer’s pre-nonlinearity dense layer of shape 17,408 (MLP hidden dimension) by 5,120 (model dimension), we get 89,128,960 weights. But our low-rank (rank 16) approximation only requires (17,408 × 16) + (5,120 × 16) = 360,448 weights. And since we are only adding adapters on parts of the model, we get 2 orders of magnitude in memory savings even when we are training our adapters in FP32.

Training memory for one MLP layer of Qwen3.8-27B. Fully trained in BF16 it takes 2,040 MiB, split between weights, gradients and optimizer state. With QLoRA it takes 148 MiB: 132 MiB of frozen 4-bit weights plus a 16.5 MiB rank-16 adapter. Above the bars, the frozen weight matrix is drawn as a slab next to the adapter, which squeezes 5,120 inputs down to 16 numbers and expands them back to 17,408 outputs.

Empirically (arXiv:2305.14314), QLoRA performs almost as well as full fine-tuning for the type of task we are working with here (style transfer and domain knowledge update).

Note: there are many techniques that are even more memory-efficient than LoRA (they often rely on weight updates to variants of the singular values from an SVD decomposition of the full attention/MLP matrix), and I plan on experimenting with them in the future.

Yet QLoRA is still not enough to fit our 27B model in our GPU for fine-tuning. We still need to tackle our large activation and loss memory footprints.

Gradient Checkpointing

Because our LoRA is in a higher precision than the base model, we still need to work in high-precision activations (BF16) during forward and backpropagation in order to calculate precise adapter gradients. This means our Qwen 27B still needs about 122 GiB of memory just for saving forward-pass activation values.

Two rows of 24 GiB RTX 4090 boxes. Back row, the naive full fine-tune in BF16: 50.1 GiB of weights, 50.1 of gradients, 100.2 of optimizer state, about 122 of activations and 9.4 of loss for 3,000 tokens, about 332 GiB or 14 cards. Front row, with QLoRA: 16.5 GiB of weights frozen in 4-bit, a 1.2 GiB adapter, the same 122 GiB of activations and 9.4 of loss, about 149 GiB or 7 cards. Past the front row stand empty outlined boxes the size of what was removed: 50.1 GiB of gradients and 100.2 GiB of optimizer state.

The empty boxes above are gradient and optimizer savings: QLoRA saved about six 4090s’ worth of memory needs but still falls way short of fitting everything on a single card. Next, we will apply gradient checkpointing (arXiv:1808.00079), which trades training speed for memory usage.

The idea is simple: instead of saving every activation from every layer in order to calculate our adapter gradients, we only save the per-layer input values and recalculate the full activations layer by layer as we backprop through the model.

Because each layer’s activations depend only on its inputs, we can safely regenerate activations a single layer at a time when we need to calculate gradient values for that layer. The activations are then immediately discarded when backprop moves back to the previous layer. Here’s an animation of gradient checkpointing in action with a toy 3-layer model.

A counterintuitive result for me here is that the checkpointed version didn’t take 1.5x as long through the entire model, as I naively assumed by giving the forward and backward passes equal duration. The checkpointed version only took about 22% longer.

After checkpointing, we managed to squeeze our memory footprint down to 32 GiB — close, yet still no cigar for our 24 GiB card. The largest chunk we have left is our loss for the entire 3,000-token sequence, still requiring 9.4 GiB of memory.

Two rows of RTX 4090 boxes for QLoRA on Qwen3.8-27B at 3,000 tokens. With every activation kept: about 149 GiB, 7 cards. With gradient checkpointing: about 32 GiB, 2 cards.

Loss Chunking

Qwen3.8 is a small model with a large vocabulary: 248,320 tokens, in fact. The loss for each context token requires a 16-bit logit per vocabulary entry that gets cast to 32-bit for the softmax, before the output is saved in tables of 32-bit scores (14 bytes per entry). At a 3,000-token context length this gives us a loss memory requirement of 248,320 × 3,000 × 14 bytes, which is how we ended up needing 9.71 GiB for just our loss measurements even when we are not touching the unembed layer.

There are 2 things we could do here: one is loss chunking (which spreads the work out one section at a time), and the other is using a performant kernel (so we don’t have to hold the logits, the softmax, and the scores in memory at the same time). We’ll just use loss chunking here.

The idea of loss chunking, again, is not too complicated: the memory overhead of our loss computation comes from reducing our per-token, per-vocab loss (3k by 248k) to per-token, per-model-dimension (3k by 5,120) loss gradient at the final residual layer (ignoring any norms before unembed). And this step need not be done with the entire sequence in memory, since each token in the sequence is independent of the other tokens at the unembedding layer (no attention mechanism here).

So instead of doing the entire sequence at once, we do a length-128 section at a time, writing the loss gradient to the final residual stream (or hidden state) a section at a time. Here’s an animation showing chunking at work.

Here again we are trading speed for memory usage: the chunked loss computation is about 40% slower (although the whole training step is only about 3% slower), but uses much less peak memory.

At last, we are able to fit our entire fine-tuning pipeline inside a single GPU.

Left, QLoRA plus gradient checkpointing needs 2 RTX 4090s, about 32 GiB: 16.5 GiB of frozen 4-bit weights, 4.5 GiB of activations, a 1.2 GiB adapter and 9.4 GiB of loss. Right, adding the chunked loss and the fast kernels fits on 1 RTX 4090, about 22 GiB: the loss drops from 9.4 to 0.45 GiB and activations from 4.5 to 3.5 GiB, with the training run's measured peak at 21.5 GiB and about 1.4 GiB free.

A note on kernels: in theory we should be able to use Apple’s Cut Cross-Entropy kernel (arXiv:2411.09009) and get more memory savings. Included in the chart’s memory savings is the Flash Linear Attention kernel for Qwen’s linear attention layers.

Training

Okay, everything fits on one GPU now, but does it actually train? I found existing Blender script datasets on Hugging Face mostly come from the last generation of models. Fine-tuning on them did not improve our model (possibly because they are already in the training data). So I used Sonnet 5.5 to generate 2,000 Blender scripts from non-overlapping location prompts with a style guide for creating diorama-like output. Here’s an example prompt:

A huge airship hangar with half-open doors, a docking rail, a small repair gantry and a thin airship nose peeking out.

And a section of the style guide for the diorama style that Sonnet had access to:

The look: a low-poly diorama with dramatic light and glow

Every picture in this dataset shares one look, so that a model trained on it is recognisable at a glance. Whatever the request, the result is a small low-poly diorama floating in the dark, lit like a stage, with something glowing.

  • Low-poly. Faceted, flat-shaded forms: no smooth shading, no subdivision, no bevels. Few sides (cylinders and cones with 5 to 10, icospheres with 1 or 2 subdivisions). Chunky, slightly exaggerated proportions. Make natural things irregular (rocks, foliage, ground: move their vertices a little with seeded random numbers; vary sizes and turns), and keep made things crisp.
  • Diorama. The scene stands on a base that floats in darkness: a low-poly slab of ground sized to the scene, with a tapered, rough underside, in a ground that suits the place (grass, sand, rock, snow, planks, metal deck). Shape the top of the base where the place calls for it (a hill, steps, a pond, a path). Everything stays inside the base’s outline, so make the base a little larger than the scene. No floor beyond it, no walls around it, no backdrop. An interior is shown as a cutaway: its floor is the base and at most two walls stand, so the camera sees in.
  • Flat colour. Each material is one colour with a rough finish; no texture nodes. A pattern (stripes, tiles, speckles, planks) is built from pieces or faces in different colours. Keep to a small palette, 4 to 7 colours that sit well together, plus the glow.
  • Glow. Something in every scene gives off light: a window, lantern, crystal, screen, fire, molten metal, sign, magic. Give it an emission colour strong enough to read as a light source, and put a point light of the same colour at the same place so the glow spills onto what is around it. If nothing in the request would glow, set a thin glowing line into the rim of the base in an accent colour.
  • Dramatic light, chosen for the place. A near-black background with a hint of colour. One strong key light, low and from one side, throwing long shadows, and one dimmer rim light from behind on the other side to pick out the outline. No flat fill: let the shadows go dark, but keep the subject readable. Choose the colours and directions for the mood of this place and of the request (a moonlit ruin, a forge at night, a foggy swamp, a harbour at dawn should not be lit alike). As a starting point that reads well: a key sun of strength about 5 some 20 degrees above the horizon, a rim sun of about 2, a background near (0.01, 0.01, 0.03), and glow lights of 150 to 2,500 W, more for tall scenes.
  • Camera. A three-quarter view from above, 25 to 35 degrees up, through a long lens (70 to 90 mm), far enough back that the whole scene fits with a small margin: about 2.5 to 3 times the scene’s width away, and more for a tall scene, because the picture is square. The whole base, and everything on or above it (light shafts, rain, smoke, sky), must sit inside the picture with dark background all round it: the checker fails a scene that is cut off by the picture’s edge. Do not overshoot either: the scene should span most of the picture (85 to 90% of its width or height), and the checker warns when it is small.
  • Readable at a glance. Make small things that matter (hands of a clock, coins, tools, signs) larger than life, and keep large surfaces mid to dark in colour so they do not burn out under the key light.

Here’s an example scene that Sonnet created based on the prompt:

The scene Sonnet built for the airship hangar request: a low-poly hangar with half-open doors on a floating base, an airship nose showing inside, lit from one side against a dark background.

You can find the full dataset at ShiminZ/blender-lowpoly-dioramas.

Each Sonnet script ended up around 3,000 tokens long, so the fine-tuning pipeline was only able to fit about 1 example per batch, and 2 epochs took about 6 hours to fine-tune overnight. The loss curve showed some overfitting at 2 epochs but overall held-out loss was lower after 2 (loss of 0.370 to 0.365):

Loss over two passes of 2,000 scenes, 4,000 steps. The held-out loss starts at 0.619 for the untuned model, falls to 0.395 by step 500, reaches about 0.37 at the end of the first pass and 0.365 at the end of the second. The training loss tracks it through the first pass, then drops to about 0.33 in the second pass, opening a gap between scenes the model has seen and scenes it has not.

100 additional diorama examples are held out as test data; you can see 10 random before/after pairs here (note how many untuned scripts errored out):

Ten held-out scene requests picked at random, each shown as a pair of renders with the request printed underneath: untuned Qwen3.8-27B on the left, fine-tuned on the right. Five of the ten untuned scripts raised an error and show an empty or near-empty scene; the others are mostly misframed or sparse. Eight of the ten fine-tuned scripts ran and show lit low-poly dioramas on floating bases: a seaplane base, a fruit stand, a hillside mill, a mouse house, a cinema, a drilling camp, a logging railway and a warship. Two fine-tuned scripts raised an error.

The total number of scripts that ran without errors increased from 42 out of 100 for the base model to 85 out of 100 post tuning. With just a small selection of examples, the QLoRA tuned Qwen3.8 is better at using Blender with fewer script errors, and learned to exhibit the low-poly diorama style not in its test-time prompt.

To see if the style transferred outside of the location shots, I also generated some classic movie scene prompts for our fine-tuned 3.8 model. Here too we see a clear style transfer and better usage, although there are a few scenes (such as the Ghostbusters scene) where I preferred the original.

Ten film scenes as pairs of renders, untuned Qwen3.8-27B on the left and fine-tuned on the right, each with its request printed underneath: Titanic, The Wizard of Oz, Singin' in the Rain, Jaws, The Fellowship of the Ring, Raiders of the Lost Ark, Blade Runner, Spirited Away, Up and Ghostbusters. Every fine-tuned picture is a lit low-poly diorama on a floating base: a ship, a poppy field, a rainy street, a boat at sea, a stone bridge, a temple, a neon city block, a bathhouse, a house under balloons and a block of city buildings. The untuned pictures vary: a yellow road running to an emerald city under a blue sky, a lit stone passage and the Marshmallow Man standing in a night street are closer to the films' shots, while four are black, blank or nearly empty.

Hope this convinced you that even if you are GPU poor like me and have narrow and repeated tasks where prompt improvements aren’t enough — such as our Blender case where including the latest API in the prompt would eat up too much context — there’s no reason not to build an adapter for them. And while a model with a LoRA adapter tends to do worse at tasks it was not fine-tuned for, it’s easy to turn the adapter on and off at task time and fall back to the original general model for everyday work. And since the adapters are both fast to train and toggle, you can have multiple adapters on disk and only use the ones best suited for a particular task.

Lastly, you can check out the code for the fine-tuning setup here: Local-FineTune-Memory. There were a few more tricks that we could’ve implemented to increase training speed, batch size, or example length, such as moving some of the model to CPU RAM, but I left them out in the spirit of fitting everything inside a single GPU.