Building with AI

Fine-tuning a 27B model on a single RTX 4090 with QLoRA

A developer demonstrates how to fine-tune Qwen3.8-27B for specific coding tasks on a consumer GPU using QLoRA, gradient checkpointing, and loss chunking.

A graphics card with small gold adapters connecting to a large ice cube.
Illustration generated for this article

Shimin Zhang successfully fine-tuned the Qwen3.8-27B language model on a single NVIDIA RTX 4090 graphics card, proving that specialized local model training is feasible for developers with limited hardware. By combining quantized low-rank adaptation with aggressive memory optimization techniques, the project targeted narrow stylistic and API-specific knowledge updates rather than general capability expansion.

What happened

Zhang aimed to teach Qwen3.8-27B how to write scripts for Blender 5.2, a task where the base model struggled due to outdated training data focused on older API versions. The goal was not to make the model smarter in a general sense, but to instill a specific visual style and technical knowledge set: creating low-poly dioramas with dramatic lighting. This required updating the model’s understanding of the V5 API while enforcing a strict aesthetic guide involving flat shading, glowing elements, and specific camera angles.

The primary obstacle was memory. Running the model in its standard 4-bit quantized format for inference already consumed 16 GiB of the card’s 24 GiB VRAM, leaving little room for the additional buffers required during training. Standard full fine-tuning in BF16 precision would have required approximately 332 GiB of VRAM, equivalent to fourteen RTX 4090 cards. Even attempting to fine-tune directly on the quantized weights was impossible because the coarse 4-bit representation cannot support the tiny weight adjustments needed for learning.

To solve this, Zhang implemented a pipeline that froze the base model and trained only small adapter layers. Through a combination of QLoRA, gradient checkpointing, and loss chunking, the total memory footprint was reduced enough to fit within the remaining VRAM headroom. The result was a functional pipeline that could train on synthetic data generated by other models, producing Blender scripts that adhered to the desired diorama style.

How it works

The core technique used is Quantized Low-Rank Adaptation (QLoRA). Instead of updating all 27 billion parameters of the model, the base weights are frozen in their 4-bit format. Small, trainable adapter matrices are inserted into the attention and MLP layers. For a typical layer with millions of weights, the adapter might only contain a few hundred thousand parameters. This reduces the number of trainable weights by two orders of magnitude, significantly cutting down the memory needed for gradients and optimizer states.

However, QLoRA alone was insufficient. The forward pass still requires high-precision activations to calculate accurate gradients for the adapters. To manage this, Zhang employed gradient checkpointing. Rather than storing every activation from every layer in memory during the forward pass, the system only saves the input to each layer. During backpropagation, it recalculates the activations for one layer at a time, discarding them immediately after use. This trades computational speed for memory savings, increasing training time by only about 22 percent.

The final bottleneck was the loss computation. With a vocabulary of over 248,000 tokens and a context length of 3,000, storing the logits for the entire sequence required nearly 10 GiB of VRAM. Zhang applied loss chunking, which processes the sequence in small sections of 128 tokens. Since tokens are independent at the unembedding stage, the system can compute the loss gradient for one chunk, write it to the residual stream, and then discard the intermediate data before moving to the next chunk. This reduced peak memory usage enough to fit the entire process on a single GPU.

Key details

  • Hardware: Single NVIDIA RTX 4090 with 24 GiB VRAM.
  • Model: Qwen3.8-27B in 4-bit GGUF format (Unsloth).
  • Technique: QLoRA with rank 16 adapters on attention and MLP layers.
  • Memory Optimization: Gradient checkpointing reduced activation memory; loss chunking handled the large vocabulary output.
  • Performance Cost: Gradient checkpointing added ~22% to training time; loss chunking added ~3% to the total step time.
  • Data Source: 2,000 synthetic Blender scripts generated by Sonnet 5.5 using a detailed style guide.

Why it matters

This approach democratizes access to model customization for engineers who do not have access to enterprise-grade GPU clusters. It shows that specialized tasks, such as adapting a model to a new software API or enforcing a corporate writing style, can be performed locally. This reduces dependency on cloud services for fine-tuning and keeps sensitive data or proprietary style guides on-premise.

For developers building AI-assisted tools, the ability to inject narrow domain knowledge without retraining the entire model is crucial. It allows for rapid iteration on specific features, such as code generation for internal frameworks or customer support bots with specific tone guidelines. The trade-off between speed and memory is often acceptable for these targeted updates, especially when the alternative is renting expensive cloud infrastructure.

What you can do

  • Assess your VRAM budget by calculating the memory usage of your base model in inference mode before planning any fine-tuning.
  • Use QLoRA to freeze base weights and train only low-rank adapters, drastically reducing the number of parameters that need optimization.
  • Implement gradient checkpointing if activation memory is a bottleneck, accepting a moderate increase in training time for significant memory savings.
  • Apply loss chunking for models with large vocabularies to avoid storing massive logit tables in memory during the backward pass.
  • Generate high-quality synthetic training data using existing powerful models if domain-specific datasets are unavailable or outdated.
  • Focus on narrow, well-defined tasks like API updates or style transfer rather than attempting to improve general reasoning capabilities locally.
  • #qlora
  • #fine-tuning
  • #local-llm
  • #gpu-optimization
  • #blender

Tools from the Bytechap store

$89

DocBento

Self-hosted document management that reads every scan and answers with page citations.

Live demo

Keep reading

All stories