Repository navigation
Commit 9d15a59
Pass gradients through compressed-tensors activation quantization so W8A8 checkpoints train with LoRA (#11585)
* Pass gradients through compressed-tensors activation fake quantization so W8A8 checkpoints train with LoRA
* Trim comments
* Return early from the activation STE wrapper when grad mode is off
Inference runs forward_quantize under no_grad or inference_mode, so check
torch.is_grad_enabled() before unpacking arguments and type checks. Cuts the
added per-call cost on the decode path roughly in half.
* Keep the quantized activation exactly in the STE forward
value + (out - value).detach() rounds in bf16, so a static W8A8 scale that
saturates changed the forward values the checkpoint was calibrated for. A small
autograd function now returns the quantized tensor itself and passes the
gradient straight through. Adds a saturating static-scale test.
* Run compressed-tensors FP8 Linears on Unsloth's FP8 kernels so LoRA trains without decompressing
Keep FP8 weights and scales as loaded, route each forward through fp8_linear (on the fly
dequantization, identity input gradient) and drop the first-forward decompression hook.
Fix weight_dequant on a transposed square per-row view, read the block size from the scale,
and fall back to dequant + matmul in the per-row path when FBGEMM is absent.
* Add the bias once in single-token FP8 decode and wait for a zoo that keeps FP8 inputs
fast_linear_forward passed the bias into fp8_linear, whose per-channel path adds it, then added it
again. Compressed-tensors FP8 checkpoints with q/k/v biases (Qwen2.5 FP8-dynamic) now reach that path.
Route compressed-tensors FP8 Linears to the FP8 kernels only when the installed unsloth_zoo compiled
LoRA forward no longer casts inputs to the FP8 weight dtype; older zoos keep the decompress path.
* Decompress compressed-tensors checkpoints at load, outside inference mode
Checkpoints that do not route to the FP8 kernels (INT8 W8A8, or FP8 with an older unsloth_zoo) were
decompressed by compressed-tensors on their first forward. PEFT's direct .forward calls can skip that
hook, and when the first forward is a generate() it runs under inference_mode, so every weight becomes
an inference tensor and the next LoRA backward fails with 'Inference tensors cannot be saved for
backward'. Decompress once after load instead, and swap the lazy hook for one that leaves inference
mode, so the fallback is safe too. FP8 routing is now all or nothing so the two never mix.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Run the load-decompress test without importing unsloth
CPU CI runners have no unsloth_zoo, so importing unsloth failed collection. The function only needs
torch; the test now takes it straight from loader_utils.py.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Unwrap compressed-tensors 0.19 offload caches and add an FP8 decode GEMV
compressed-tensors 0.19 decompress_model wraps every module's parameters in an OffloadCache even with
nothing offloaded; its data pointer comparison breaks Dynamo, so FastModel failed on every
decompressed checkpoint with 'Unsupported: Data pointer comparison' (main included). Same-device
caches are unwrapped after decompression; real offloading is left alone.
Single-token decode on per-row FP8 weights dequantized the whole weight every step when FBGEMM is
absent. A split-K Triton GEMV now reads the FP8 weight once and applies the row scale in the
reduction, deterministically, for up to four rows with grad off; everything else is unchanged.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Make the FP8 decode GEMV a single launch for one token
The split-K version spent more host time on its reduction, cast and launch math than the GPU spent on
the matmul, so end-to-end decode stayed slower than bf16. One launch that accumulates a full tile and
reduces once matches or beats a bf16 matmul on every Qwen2.5-3B layer shape, and it now runs for a
single token only: from four rows a bf16 matmul wins, since each row re-reads the weight.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Route compressed-tensors FP8 to the FP8 kernels only on GPU weights
The FP8 kernels are triton, so a checkpoint loaded on CPU would have been routed to kernels that cannot
run there; main decompresses it and trains. Weights off the GPU keep that path.
* Trim comments in compressed-tensors changes
* Keep block FP8 compressed-tensors on the decompress path, route per-tensor scales per row
The block FP8 kernel trains about 2.5x slower and decodes about 4x slower than the
decompressed bf16 model on B200, so block routing is opt-in via
UNSLOTH_COMPRESSED_TENSORS_FP8_BLOCK_KERNELS=1. A per-tensor scale is broadcast to one
scale per row, which takes the faster and more accurate rowwise path.
* Make compressed-tensors FP8 kernel routing opt-in
Keeping FP8-dynamic weights in FP8 saves about 20% peak training memory on Qwen3-8B but
dequantizing per step trains 35-65% slower and decodes 18-40% slower than the decompressed
bf16 model on B200. Default to decompressing once at load; UNSLOTH_COMPRESSED_TENSORS_FP8_KERNELS=1
opts into the FP8 kernels.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Read the FP8 multi-device check once so compiled FP8 kernels do not graph break
torch.cuda.device_count() inside a compiled region returns a non-Tensor and breaks the graph;
the single-token FP8 GEMV hit it three times under torch.compile(fullgraph=True).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Keep the per-call FP8 device count outside the decode GEMV
Reading the multi-device check once let Dynamo trace all of FP8BlockQuantLinear, and torch
2.11 compiled its backward to a zero input gradient. Only the forward-only GEMV uses the
import-time count now; the other FP8 kernels behave as on main.
* Shorten FP8 device-count comment
* Dequantize rowwise FP8 fallback to the input dtype, save routed scales in checkpoint shape
The rowwise dequant fallback now also serves missing FBGEMM and pre-sm89 GPUs, where fp16
inputs met a bf16 weight and failed in torch.matmul. A routed compressed-tensors module saves
its weight_scale in the shape the quantization config declares, not the per-row broadcast.
* Drop the compressed-tensors decompress hook inside the decompress callback
compressed-tensors 0.19.0 decompress_model leaves ct_decompress_hook registered, so after a
failed load-time attempt the retry hook ran decompression again on every forward.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Expand saved compressed-tensors scales back to the routed shape on load
The state_dict hook saves weight_scale in the checkpoint shape; the matching load pre-hook lets a
routed module load its own state_dict again.
* Decompress compressed-tensors FP8 for full finetuning instead of routing
Routed FP8 weights are frozen, so full_finetuning=True with the opt-in FP8 kernels left every
base weight untrained; full finetuning now always takes the decompressed bf16 weights.
* Trim comments after review
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent ef63a38 commit 9d15a59
11 files changed
Lines changed: 1070 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
0 commit comments