# Quantized LoRA (QLoRA)

Source: https://docs.surogate.ai/engine/guides/qlora/

QLoRA lets you fine-tune with much less memory. The frozen base model weights are quantized, and the LoRA adapters (see [LoRA settings](/engine/reference/config/#lora-settings)) train in higher precision. Surogate supports two modes:

- Online QLoRA: Surogate quantizes the weights at startup from a standard BF16/FP16 checkpoint.
- Pre-quantized models: you load a model that is already quantized (for example, from the Hugging Face Hub). The supported formats are Fine-Grained FP8 (compatible with NVIDIA ModelOpt FP8 exports), ModelOpt NVFP4 (two-level FP8+FP32 block scaling), compressed-tensors NVFP4 (llm-compressor exports, such as NVFP4 experts beside per-channel FP8 projections) and MXFP4 (microscaling FP4, GPT-OSS models). These skip the quantization step at startup and load straight into the quantized format.

Online QLoRA comes in three formats:

| Aspect                 | FP8 QLoRA                      | FP4 QLoRA                    | NF4 QLoRA (BitsAndBytes)          |
| ---------------------- | ------------------------------ | ---------------------------- | --------------------------------- |
| **Format**             | E4M3 (fwd), E5M2 (bwd)         | E2M1 (both)                  | NF4 (4-bit normal float)          |
| **Scaling**            | Per-tensor delayed             | Two-level block (FP8 + FP32) | Per-block absmax (+ double quant) |
| **GPU Requirement**    | SM89+ (Ada, Hopper, Blackwell) | SM100+ (Blackwell only)      | Any CUDA GPU                      |
| **Memory Compression** | ~50% vs FP16                   | ~75% vs FP16                 | ~75% vs FP16                      |

## Pre-Quantized Models

When a Hugging Face model already ships pre-quantized weights (a ModelOpt-quantized checkpoint, for example), Surogate loads the quantized tensors straight from the safetensors files, with no quantization pass at startup. For large models this saves a lot of startup time, and your training run uses exactly the quantized representation of the published checkpoint.

### Supported Formats

| Format                                                                                           | HF `quant_method`  | Data dtype | Scale dtype             | Notes                                                       |
| ------------------------------------------------------------------------------------------------ | ------------------ | ---------- | ----------------------- | ----------------------------------------------------------- |
| [**Fine-Grained FP8**](https://huggingface.co/docs/transformers/en/quantization/finegrained_fp8) | `fp8`              | FP8 E4M3   | FP32 (inverse scale)    | Per-block 2D tile scaling                                   |
| [**ModelOpt NVFP4**](https://huggingface.co/docs/transformers/en/quantization/mxfp4)             | `modelopt` / NVFP4 | FP4 E2M1   | FP8 block + FP32 global | Two-level scaling (FP8 + FP32)                              |
| **compressed-tensors NVFP4**                                                                     | `compressed-tensors` (`nvfp4-pack-quantized` group) | FP4 E2M1 | FP8 block + FP32 global per expert | llm-compressor exports; other modules load as stored (see below) |
| [**MXFP4**](https://huggingface.co/docs/transformers/en/quantization/mxfp4)                      | `mxfp4`            | FP4 E2M1   | FP8 per-block           | GPT-OSS models only; uses `_blocks`/`_scales` tensor naming |

### How It Works

At startup Surogate reads the `quantization_config` in the model's Hugging Face `config.json` and detects the pre-quantized format from it. You don't need any extra config keys.

Hugging Face tensor naming:

| Format           | Data tensor     | Block scale tensor        | Global scale tensor     |
| ---------------- | --------------- | ------------------------- | ----------------------- |
| Fine-Grained FP8 | `{name}.weight` | `{name}.weight_scale_inv` | —                       |
| ModelOpt NVFP4   | `{name}.weight` | `{name}.weight_scale`     | `{name}.weight_scale_2` |
| compressed-tensors NVFP4 | `{name}.weight_packed` | `{name}.weight_scale` | `{name}.weight_global_scale` |
| MXFP4            | `{base}_blocks` | `{base}_scales`           | —                       |

MXFP4 names tensors differently. There is no `.weight` suffix: the packed FP4 data is stored as `{base}_blocks` and the FP8 scales as `{base}_scales`.

In every format, layers listed in the model's `modules_to_not_convert` (usually the embedding and LM head) load at full BF16 precision.

Some fused weight tensors (such as `gate_up_proj`) are stored as separate pieces in pre-quantized checkpoints. Surogate loads, dequantizes, fuses and re-quantizes them for you.

### Constraints

- Requires `lora: true` ([LoRA settings](/engine/reference/config/#lora-settings)). The base weights are frozen and only the LoRA adapters train.
- Can't be combined with online QLoRA ([QLoRA settings](/engine/reference/config/#qlora-settings)): `qlora_fp8`, `qlora_fp4`, and `qlora_bnb` must all be `false`.
- No adapter merging: `adapter_path` is not supported, because there is no BF16 intermediate.

### Example — Fine-Grained FP8

```yaml
model: nvidia/Llama-3.1-8B-Instruct-FP8
lora: true
lora_rank: 16
recipe: bf16
```

You don't need quantization flags. The format is detected from the checkpoint's `quantization_config`.

### Example — ModelOpt NVFP4

```yaml
model: nvidia/Llama-3.1-8B-Instruct-NVFP4
lora: true
lora_rank: 16
recipe: nvfp4
```

### compressed-tensors NVFP4 (llm-compressor exports)

A checkpoint whose `quantization_config` has `quant_method: compressed-tensors` and a `config_groups`
entry of format `nvfp4-pack-quantized` loads as a pre-quantized NVFP4 base. The NVFP4 modules arrive as
E2M1 codes (`weight_packed`), E4M3 block scales (`weight_scale`) and a global scale
(`weight_global_scale`, the encode scale, inverted on load). The trainer quantizes weights only:
`input_global_scale` is not used, and NVFP4 weights are dequantized to BF16 where they are used.

- MoE experts stored one by one (`experts.{e}.{gate,up,down}_proj`, as llm-compressor writes them) are
  stacked for Qwen3.5 / Qwen3.6 MoE models. Each expert keeps its own global scale. The dequant kernel
  applies one decode scale per expert; folding them into one would re-round the E4M3
  block scales (up to ~6 % error).
- Every other module loads as the checkpoint stores it. Per-channel FP8 Linears (in a Qwen3.5-35B-A3B
  export: the attention and Gated DeltaNet projections and the lm-head) stay FP8 (codes + row scales)
  and stream with their stage under `parallelism: dispatch_pp` ([Dispatch Pipeline Parallelism](/engine/guides/dispatch-pp/)). They do not become a BF16 copy on every
  GPU. BF16 modules load at full precision.
- Recipe: `bf16`.

```yaml
model: /path/to/nvfp4-checkpoint   # NVFP4 in compressed-tensors format
lora: true
lora_rank: 16
lora_target_modules: [gate_proj, up_proj, down_proj]
recipe: bf16
```

We checked this on an NVFP4 export of a Qwen3.5-35B-A3B model (8× RTX 5090, `parallelism: dispatch_pp`, zero LoRA delta),
against our serving engine's rollouts of the same checkpoint, as k3 KL per token:

| Comparison | k3 |
| --- | --- |
| vs the engine's decode, 6 rollouts of at most 8k | 0.0050 |
| vs the engine's prefill, 21 rollouts of at most 32k (chunked) | 0.0056 |
| vs the engine with its MoE pinned to W4A16 | 0.0036-0.0047 |
| FP8 base against the FP8 engine, for comparison | 0.0087 |

At 131k with replicated LoRA r8, the peak per card is 27.3 GB, against 29.7 GB with the FP8 export.

### Example — MXFP4 (GPT-OSS)

```yaml
model: nvidia/Nemotron-H-47B-Base-MXFP4
lora: true
lora_rank: 16
recipe: bf16
```

MXFP4 is only supported for GPT-OSS model architectures for now. The format is detected from the checkpoint's `quantization_config`.

---

## QLoRA vs Recipes

QLoRA decides how the frozen base model weights are stored and used in the forward pass. The base weights stay quantized and are never updated.

The [recipe](/engine/guides/precision-and-recipes/) decides the precision of the LoRA adapter computations, activations and gradients during training.

You can pair any QLoRA format with any compatible recipe:

```
QLoRA (base weights) + Recipe (LoRA training) = Full Configuration
```

## FP8 QLoRA

FP8 QLoRA stores the base model weights in FP8, which uses ~50% less memory than FP16/BF16.

### How It Works

Base weights are quantized to FP8 in two formats, one for each pass:

| Format   | Exponent | Mantissa | Max Value | Use Case                             |
| -------- | -------- | -------- | --------- | ------------------------------------ |
| **E4M3** | 4 bits   | 3 bits   | 448       | Forward pass (higher precision)      |
| **E5M2** | 5 bits   | 2 bits   | 57344     | Backward pass (larger dynamic range) |

[Delayed scaling](/engine/guides/precision-and-recipes/#fp8-hybrid) computes scale factors from the previous iteration's abs-max values (over a history window of 1024 by default). This trains more stably than just-in-time scaling.

### Parameters

| Parameter                 | Default | Description                          |
| ------------------------- | ------- | ------------------------------------ |
| `qlora_fp8`               | false   | Enable FP8 QLoRA                     |
| `margin`                  | 0       | Margin for scale factor computation  |
| `amax_history_len`        | 1024    | Length of amax history window        |
| `amax_compute_algo`       | MAX     | Algorithm: MAX or MOST_RECENT        |
| `reduce_amax`             | true    | Reduce amax across distributed group |
| `skip_quant_first_layers` | 0       | Skip quantization for first N layers |
| `skip_quant_last_layers`  | 0       | Skip quantization for last N layers  |

### Recommended Recipe Combinations

| Recipe     | Use Case                                     |
| ---------- | -------------------------------------------- |
| **bf16**   | Maximum LoRA accuracy, any GPU (Recommended) |
| fp8-hybrid | Faster LoRA compute on SM89+ GPUs            |
| nvfp4      | Maximum speed on Blackwell (experimental)    |

### Example

```yaml
qlora_fp8: true
skip_quant_first_layers: 1
skip_quant_last_layers: 2
recipe: bf16
lora: true
lora_rank: 16
```

## FP4 QLoRA

FP4 QLoRA stores the base model weights in NVIDIA's FP4 E2M1 format, which uses ~75% less memory than FP16/BF16. It needs a Blackwell GPU (SM100+).

### How It Works

FP4 E2M1 compresses hard: it has only 8 representable values per sign.

| Property          | Value                                     |
| ----------------- | ----------------------------------------- |
| **Exponent bits** | 2                                         |
| **Mantissa bits** | 1                                         |
| **Values**        | ±{0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0} |
| **Storage**       | 2 values per byte (4 bits each)           |

Two-level block scaling:

- Level 1: FP8 E4M3 scales per block (16 values for activations, 16x16 for weights)
- Level 2: FP32 global amax baked into block scales

Stability techniques:

- Random Hadamard Transform (RHT) spreads outliers out before quantization.
- Stochastic rounding stops quantization bias from accumulating in gradients.
- Four-Over-Six (4/6) adaptive scaling picks the best scale for each block.
- Layer skipping keeps sensitive layers (embedding, lm_head) in BF16 (see [Layer quantization skip options](/engine/reference/config/#layer-quantization-skip-options)).

### Parameters

| Parameter                 | Default | Description                 |
| ------------------------- | ------- | --------------------------- |
| `qlora_fp4`               | false   | Enable FP4 QLoRA            |
| `skip_quant_first_layers` | 0       | Skip FP4 for first N layers |
| `skip_quant_last_layers`  | 0       | Skip FP4 for last N layers  |
| `backend`                 | cutlass | Backend: cudnn or cutlass   |

### Recommended Recipe Combinations

| Recipe     | Use Case                                       |
| ---------- | ---------------------------------------------- |
| **nvfp4**  | Maximum speed, full FP4 pipeline (Recommended) |
| bf16       | Higher LoRA accuracy, slower                   |
| fp8-hybrid | Balance of speed and accuracy                  |

### Example

```yaml
qlora_fp4: true
recipe: nvfp4
lora: true
lora_rank: 16
skip_quant_first_layers: 1
skip_quant_last_layers: 4
```

## NF4 QLoRA (BitsAndBytes)

NF4 QLoRA uses the BitsAndBytes NF4 (NormalFloat4) quantization format. It saves ~75% of memory and runs on almost any GPU.

### How It Works

NF4 is a 4-bit data type designed for normally distributed weights:

| Property           | Value                                             |
| ------------------ | ------------------------------------------------- |
| **Bits per value** | 4                                                 |
| **Storage**        | 2 values per byte                                 |
| **Quantile-based** | 16 levels mapped to normal distribution quantiles |
| **Block size**     | Configurable (default: 64 values per block)       |

Block-wise quantization:

- Weights are divided into blocks (default 64 values)
- Each block stores an FP32 absmax scale factor
- Values are quantized to 4-bit indices into a fixed NF4 lookup table

Double quantization (optional):

- Absmax scales are further quantized to INT8
- Groups of 256 blocks share an FP32 scale and offset
- Reduces scale overhead from 4 bytes to ~1 byte per block

### Memory Layout

For a weight tensor with N elements using block size 64:

| Component     | Size (bytes) | With Double Quant  |
| ------------- | ------------ | ------------------ |
| NF4 data      | N / 2        | N / 2              |
| Absmax scales | (N / 64) × 4 | (N / 64) × 1       |
| Double quant  | —            | (N / 64 / 256) × 8 |

### Parameters

| Parameter                | Default | Description                             |
| ------------------------ | ------- | --------------------------------------- |
| `qlora_bnb`              | false   | Enable BitsAndBytes NF4 QLoRA           |
| `qlora_bnb_block_size`   | 64      | Block size for quantization (64 or 128) |
| `qlora_bnb_double_quant` | true    | Enable double quantization for scales   |

### GPU Compatibility

FP8 and FP4 QLoRA need specific GPU architectures. NF4 QLoRA works on any CUDA GPU, and dequantizes on the fly during the forward and backward passes.

### Recommended Recipe Combinations

| Recipe     | Use Case                                         |
| ---------- | ------------------------------------------------ |
| **bf16**   | Best accuracy, broad compatibility (Recommended) |
| fp8-hybrid | Faster compute on SM89+ GPUs                     |

### Example

```yaml
model: Qwen/Qwen3-4B
lora: true
lora_rank: 16
lora_alpha: 32

qlora_bnb: true
qlora_bnb_block_size: 64
qlora_bnb_double_quant: true

recipe: bf16
```

---

## See also

- [Precision & recipes](/engine/guides/precision-and-recipes/): how to pick the recipe that runs the adapters on top of your quantized base
- [MoE](/engine/guides/moe/#qlora-for-moe-models): QLoRA on mixture-of-experts models, including expert parallelism
- [Config reference](/engine/reference/config/): every QLoRA option with its default
- [Back to docs index](/engine/)
