Skip to content

Language models, retrieval, prompting, and language adaptation · Instruction, preference, and parameter-efficient adaptation · LNG068 · Training method

QLoRA

Fine-tunes low-rank adapters while keeping a pretrained base model in a compact quantized representation. The method combines quantization-aware storage choices with LoRA to reduce adaptation memory requirements.

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Fine-tunes low-rank adapters while keeping a pretrained base model in a compact quantized representation. The method combines quantization-aware storage choices with LoRA to reduce adaptation memory requirements.

Question / objective

Can quantized frozen base weights make low-rank adaptation fit a smaller memory budget?

Architecture / computational structure

A quantized base supplies dequantized computation and trainable LoRA updates; NF4/double quantization/paged optimization are specific recipe components. Adapter weights are learned; quantizer format, compute dtype, rank, and optimizer are choices.

Assumptions

Arbitrary four-bit rounding is not NF4 and should not be labeled a faithful QLoRA implementation.

Formula / computational definition

Weff=dequantize⁡(Q(W0))+αrBA(conceptual frozen-base/adaptor computation).W_{\mathrm{eff}}=\operatorname{dequantize}(Q(W_0))+\frac{\alpha}{r}BA\qquad\text{(conceptual frozen-base/adaptor computation).}

A conceptual forward weight is dequantize(Q(W_0))+(alpha/r)BA; the base quantized weights remain frozen while adapter matrices receive gradients. The paper introduces NF4, double quantization, and paged optimizers as specific components.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Quantization type, compute dtype, rank/scaling, target modules, optimizer, device support, and adapter initialization.

Fitting, fine-tuning, and adaptation

Freeze quantized base weights and train adapters with the desired task loss using a supported hardware backend. Tune ranks and learning rates on validation quality, verify finite gradients, and compare against higher precision. Test deployment/merge behavior and general-task retention rather than assuming quantization is lossless.

How to use and calibrate

Compare adaptation quality with higher-precision LoRA, peak memory, throughput, numerical stability, and merged/deployed model behavior. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Fine-tune a model that otherwise exceeds available accelerator memory.

Interpretation / pitfalls

Watch out

Quantized kernels are hardware-dependent; memory savings vary with activations, sequence length, and optimizer choices. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: Use PEFT with a documented supported quantization backend, verifying hardware and package compatibility. R bridges Python via reticulate. The native example intentionally demonstrates generic frozen quantization plus a low-rank update, explicitly not NF4 or a complete QLoRA reproduction.

Example scope: Generic frozen scalar quantization plus rank-one update; explicitly NOT NF4 or a full QLoRA implementation.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Generic frozen scalar quantization plus rank-one update; explicitly NOT NF4 or a full QLoRA implementation.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
base = [[0.13,0.27],[-0.32,0.44]]
quantized = [[round(w/0.1)*0.1 for w in row] for row in base]
b,a = [0.2,-0.1],[0.5,0.3]
updated = [[quantized[i][j]+b[i]*a[j] for j in range(2)] for i in range(2)]
print(updated)
R

Component demo, not a trained model — Generic frozen scalar quantization plus rank-one update; explicitly NOT NF4 or a full QLoRA implementation.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
base <- rbind(c(0.13,0.27),c(-0.32,0.44))
quantized <- round(base/0.1)*0.1
b <- matrix(c(0.2,-0.1),ncol=1); a <- matrix(c(0.5,0.3),nrow=1)
updated <- quantized + b %*% a
print(updated)

Sources

From the CodeSplash reference, entry LNG068.