bugger.

How Unsloth works: architecture & execution flow

Training8 MIN READ

Unsloth

GitHub

Unsloth adapts model loading and training operations around the PyTorch, Transformers, PEFT and TRL ecosystem. Follow a supervised LoRA or QLoRA run to see where the base model stays frozen, where gradients flow, and why specialized kernels help.

LoRAQLoRATritonPyTorchPEFTTRL
Explore the components or trace execution
SYSTEM ARCHITECTURE SELECT A COMPONENT TO EXPLORE
Orchestration Compute State & storage┄ Feedback / repeat
Representative single-device, decoder-only LoRA/QLoRA training path. These are cooperating Python modules and GPU operations, not separate network services. Full fine-tuning, distributed training and Studio inference use additional paths.

Training examples

A tokenizer turns formatted examples into token IDs. The collator builds batches, attention masks and labels. Label masking determines which positions contribute to supervised loss; a wrong chat template or mask changes the task the model learns.

  • Training examples Trainer loopBatched tensors

One pass through the system.

4 steps

Select a step to highlight the components involved in the diagram.

Why it is built this way.

Memory savings come from several different places

Quantization shrinks stored base weights; LoRA shrinks trainable state; checkpointing reduces saved activations. They solve different problems. Longer sequences or larger microbatches can still exhaust device memory, and checkpointing adds recomputation.

Fast paths have compatibility boundaries

Fused operations work for supported models and settings. Changing adapter targets, precision, hardware or library versions may change the selected implementation. Validate numerical behavior and throughput on the actual training configuration.

Smaller updates still need good evaluation

Adapter rank is a capacity choice, not a quality guarantee. Data leakage, incorrect labels and overfitting remain possible. Keep a held-out set and compare the exported artifact against the in-training model.

Read the implementation.

unsloth/models/loader.py

Start at model loading, architecture selection and compatibility checks.

unsloth/kernels/fast_lora.py

Follow custom autograd functions for low-rank projection operations.

unsloth/save.py

Inspect saving, merging and supported export paths.

Go deeper with the official docs

Educational diagrams, synthesized from the linked project documentation. Components and boundaries vary by version and deployment.

More AI systems under the hood.

Practice AI debugging
Inference · 8 min read

vLLM

An inference engine and serving stack for language models. Its central problem is sharing finite GPU memory and compute among requests whose prompts and response lengths are different.

Inference · 8 min read

SGLang

A language-model serving runtime with scheduling, attention kernels and reusable prefix state. Its RadixAttention idea is especially useful when conversations or related prompts repeatedly share an initial token sequence.

Inference · 7 min read

Ray Serve

A model-serving framework built on Ray actors. It routes requests, composes deployments and scales replicas; a replica can run a PyTorch model, ordinary Python code or an LLM inference engine such as vLLM.

Training · 7 min read

PyTorch

PyTorch provides tensor operations, automatic differentiation and the building blocks for neural networks. Its central trick is to let a normal Python program describe a computation while the runtime selects hardware kernels and records how to differentiate it.

Models & tooling · 6 min read

Hugging Face Transformers

Hugging Face is an ecosystem; Transformers is its open-source model library. It connects pretrained checkpoints, model-specific implementations, preprocessing and generation APIs. Here we follow decoder-only text generation using the PyTorch backend.

Training · 7 min read

DeepSpeed

DeepSpeed wraps a PyTorch model with a training engine that coordinates precision, optimization and distributed execution. Its ZeRO optimizer removes replicated training state across data-parallel workers, enabling configurations that would otherwise exceed device memory.

Inference · 8 min read

llama.cpp

llama.cpp provides C/C++ model inference across CPU and accelerator backends. Its core connects a model file, tokenization, a computation graph, device buffers and a sampling loop. A server can wrap this library, but HTTP is not required for local generation.