|
| 1 | +defmodule EMLX.Quantization.ModuleTest do |
| 2 | + @moduledoc """ |
| 3 | + Tests for the EMLX.Quantization module - the primary user-facing API |
| 4 | + for quantized tensor operations. |
| 5 | +
|
| 6 | + These tests verify the high-level API that users should prefer over |
| 7 | + the lower-level EMLX.quantize/EMLX.dequantize/EMLX.quantized_matmul functions. |
| 8 | + """ |
| 9 | + use EMLX.Case |
| 10 | + |
| 11 | + alias EMLX.Quantization |
| 12 | + |
| 13 | + describe "Quantization.quantize/2" do |
| 14 | + test "quantizes an Nx.Tensor" do |
| 15 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 16 | + weight = Nx.backend_transfer(weight, {EMLX.Backend, device: :gpu}) |
| 17 | + |
| 18 | + {q_weight, scales, biases} = Quantization.quantize(weight) |
| 19 | + |
| 20 | + # Returns EMLX device refs |
| 21 | + assert is_tuple(q_weight) |
| 22 | + assert is_tuple(scales) |
| 23 | + assert is_tuple(biases) |
| 24 | + |
| 25 | + # Quantized weights are uint32 (packed int4) |
| 26 | + {_dev, ref} = q_weight |
| 27 | + assert EMLX.scalar_type({:gpu, ref}) == :uint32 |
| 28 | + end |
| 29 | + |
| 30 | + test "quantizes with custom group_size" do |
| 31 | + weight = Nx.iota({128, 128}, type: :f32) |> Nx.divide(100) |
| 32 | + weight = Nx.backend_transfer(weight, {EMLX.Backend, device: :gpu}) |
| 33 | + |
| 34 | + {_q_weight, scales, _biases} = Quantization.quantize(weight, group_size: 128) |
| 35 | + |
| 36 | + # With group_size=128, scales shape is [128, 128/128] = [128, 1] |
| 37 | + {_dev, s_ref} = scales |
| 38 | + assert EMLX.shape({:gpu, s_ref}) == {128, 1} |
| 39 | + end |
| 40 | + |
| 41 | + test "accepts EMLX device ref directly" do |
| 42 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 43 | + emlx_ref = EMLX.Backend.from_nx(weight) |
| 44 | + |
| 45 | + {q_weight, scales, biases} = Quantization.quantize(emlx_ref) |
| 46 | + |
| 47 | + assert is_tuple(q_weight) |
| 48 | + assert is_tuple(scales) |
| 49 | + assert is_tuple(biases) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + describe "Quantization.tensor/5" do |
| 54 | + test "creates quantized Nx.Tensor with {:s, 4} type" do |
| 55 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 56 | + emlx_weight = EMLX.Backend.from_nx(weight) |
| 57 | + {q_weight, scales, biases} = EMLX.quantize(emlx_weight, 64, 4) |
| 58 | + |
| 59 | + qt = Quantization.tensor(q_weight, scales, biases, {64, 64}) |
| 60 | + |
| 61 | + assert %Nx.Tensor{} = qt |
| 62 | + assert Nx.type(qt) == {:s, 4} |
| 63 | + assert Nx.shape(qt) == {64, 64} |
| 64 | + end |
| 65 | + |
| 66 | + test "creates tensor with 8-bit quantization" do |
| 67 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 68 | + emlx_weight = EMLX.Backend.from_nx(weight) |
| 69 | + {q_weight, scales, biases} = EMLX.quantize(emlx_weight, 64, 8) |
| 70 | + |
| 71 | + qt = Quantization.tensor(q_weight, scales, biases, {64, 64}, bits: 8) |
| 72 | + |
| 73 | + assert Nx.type(qt) == {:s, 8} |
| 74 | + end |
| 75 | + |
| 76 | + test "stores group_size in backend struct" do |
| 77 | + weight = Nx.iota({128, 128}, type: :f32) |> Nx.divide(100) |
| 78 | + emlx_weight = EMLX.Backend.from_nx(weight) |
| 79 | + {q_weight, scales, biases} = EMLX.quantize(emlx_weight, 128, 4) |
| 80 | + |
| 81 | + qt = Quantization.tensor(q_weight, scales, biases, {128, 128}, group_size: 128) |
| 82 | + |
| 83 | + opts = Quantization.options(qt) |
| 84 | + assert opts.group_size == 128 |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "Quantization.dequantize/4" do |
| 89 | + test "converts quantized weights back to float" do |
| 90 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 91 | + emlx_weight = EMLX.Backend.from_nx(weight) |
| 92 | + |
| 93 | + {q_weight, scales, biases} = Quantization.quantize(emlx_weight) |
| 94 | + dequantized = Quantization.dequantize(q_weight, scales, biases) |
| 95 | + |
| 96 | + # Returns EMLX device ref |
| 97 | + {_dev, d_ref} = dequantized |
| 98 | + assert EMLX.shape({:gpu, d_ref}) == {64, 64} |
| 99 | + end |
| 100 | + |
| 101 | + test "roundtrip preserves approximate values" do |
| 102 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(10) |
| 103 | + emlx_weight = EMLX.Backend.from_nx(weight) |
| 104 | + |
| 105 | + {q_weight, scales, biases} = Quantization.quantize(emlx_weight) |
| 106 | + dequantized = Quantization.dequantize(q_weight, scales, biases) |
| 107 | + |
| 108 | + original = EMLX.Backend.to_nx(emlx_weight) |
| 109 | + recovered = EMLX.Backend.to_nx(dequantized) |
| 110 | + |
| 111 | + # 4-bit is lossy, but mean should be in ballpark |
| 112 | + original_mean = Nx.mean(original) |> Nx.to_number() |
| 113 | + recovered_mean = Nx.mean(recovered) |> Nx.to_number() |
| 114 | + |
| 115 | + assert abs(original_mean - recovered_mean) / abs(original_mean) < 0.5 |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + describe "Quantization.quantized?/1" do |
| 120 | + test "returns true for quantized tensors" do |
| 121 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 122 | + emlx_weight = EMLX.Backend.from_nx(weight) |
| 123 | + {q_weight, scales, biases} = EMLX.quantize(emlx_weight, 64, 4) |
| 124 | + |
| 125 | + qt = Quantization.tensor(q_weight, scales, biases, {64, 64}) |
| 126 | + |
| 127 | + assert Quantization.quantized?(qt) |
| 128 | + end |
| 129 | + |
| 130 | + test "returns false for regular tensors" do |
| 131 | + tensor = Nx.iota({4, 4}, type: :f32) |
| 132 | + tensor = Nx.backend_transfer(tensor, {EMLX.Backend, device: :gpu}) |
| 133 | + |
| 134 | + refute Quantization.quantized?(tensor) |
| 135 | + end |
| 136 | + |
| 137 | + test "returns false for non-tensors" do |
| 138 | + refute Quantization.quantized?(nil) |
| 139 | + refute Quantization.quantized?(%{}) |
| 140 | + refute Quantization.quantized?("not a tensor") |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + describe "Quantization.options/1" do |
| 145 | + test "returns options map for quantized tensor" do |
| 146 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 147 | + emlx_weight = EMLX.Backend.from_nx(weight) |
| 148 | + {q_weight, scales, biases} = EMLX.quantize(emlx_weight, 64, 4) |
| 149 | + |
| 150 | + qt = Quantization.tensor(q_weight, scales, biases, {64, 64}) |
| 151 | + |
| 152 | + opts = Quantization.options(qt) |
| 153 | + |
| 154 | + assert is_map(opts) |
| 155 | + assert Map.has_key?(opts, :scales) |
| 156 | + assert Map.has_key?(opts, :biases) |
| 157 | + assert Map.has_key?(opts, :group_size) |
| 158 | + assert opts.group_size == 64 |
| 159 | + end |
| 160 | + |
| 161 | + test "returns nil for regular tensors" do |
| 162 | + tensor = Nx.iota({4, 4}, type: :f32) |
| 163 | + tensor = Nx.backend_transfer(tensor, {EMLX.Backend, device: :gpu}) |
| 164 | + |
| 165 | + assert Quantization.options(tensor) == nil |
| 166 | + end |
| 167 | + |
| 168 | + test "returns nil for non-tensors" do |
| 169 | + assert Quantization.options(nil) == nil |
| 170 | + assert Quantization.options(%{}) == nil |
| 171 | + end |
| 172 | + end |
| 173 | + |
| 174 | + describe "Nx.dot integration" do |
| 175 | + test "Nx.dot automatically dispatches to quantized_matmul" do |
| 176 | + # Create input tensor |
| 177 | + input = Nx.iota({1, 4, 64}, type: :f32) |> Nx.divide(100) |
| 178 | + input = Nx.backend_transfer(input, {EMLX.Backend, device: :gpu}) |
| 179 | + |
| 180 | + # Create and quantize weight |
| 181 | + weight = Nx.iota({128, 64}, type: :f32) |> Nx.divide(1000) |
| 182 | + {q_weight, scales, biases} = Quantization.quantize(weight) |
| 183 | + qt = Quantization.tensor(q_weight, scales, biases, {128, 64}) |
| 184 | + |
| 185 | + # Nx.dot should work transparently |
| 186 | + result = Nx.dot(input, [2], qt, [1]) |
| 187 | + |
| 188 | + assert Nx.shape(result) == {1, 4, 128} |
| 189 | + end |
| 190 | + |
| 191 | + test "quantized dot produces reasonable results" do |
| 192 | + input = Nx.iota({1, 4, 64}, type: :f32) |> Nx.divide(100) |
| 193 | + input = Nx.backend_transfer(input, {EMLX.Backend, device: :gpu}) |
| 194 | + |
| 195 | + weight = Nx.iota({64, 64}, type: :f32) |> Nx.divide(100) |
| 196 | + weight_gpu = Nx.backend_transfer(weight, {EMLX.Backend, device: :gpu}) |
| 197 | + |
| 198 | + # Full precision reference |
| 199 | + expected = Nx.dot(input, [2], Nx.transpose(weight_gpu), [1]) |
| 200 | + |
| 201 | + # Quantized path |
| 202 | + {q_weight, scales, biases} = Quantization.quantize(weight_gpu) |
| 203 | + qt = Quantization.tensor(q_weight, scales, biases, {64, 64}) |
| 204 | + result = Nx.dot(input, [2], qt, [1]) |
| 205 | + |
| 206 | + # Both should produce positive values of similar magnitude |
| 207 | + expected_mean = Nx.mean(expected) |> Nx.to_number() |
| 208 | + result_mean = Nx.mean(result) |> Nx.to_number() |
| 209 | + |
| 210 | + assert expected_mean > 0 |
| 211 | + assert result_mean > 0 |
| 212 | + assert result_mean / expected_mean > 0.1 |
| 213 | + assert result_mean / expected_mean < 10 |
| 214 | + end |
| 215 | + end |
| 216 | + |
| 217 | + describe "end-to-end workflow" do |
| 218 | + test "complete quantization workflow" do |
| 219 | + # 1. Create a weight matrix (using iota for determinism) |
| 220 | + weight = Nx.iota({256, 128}, type: :f32) |> Nx.divide(1000) |
| 221 | + weight = Nx.backend_transfer(weight, {EMLX.Backend, device: :gpu}) |
| 222 | + |
| 223 | + # 2. Quantize it |
| 224 | + {q_weight, scales, biases} = Quantization.quantize(weight, group_size: 64, bits: 4) |
| 225 | + |
| 226 | + # 3. Create quantized tensor for Nx operations |
| 227 | + qt = Quantization.tensor(q_weight, scales, biases, {256, 128}, group_size: 64, bits: 4) |
| 228 | + |
| 229 | + # 4. Verify it's marked as quantized |
| 230 | + assert Quantization.quantized?(qt) |
| 231 | + assert Nx.type(qt) == {:s, 4} |
| 232 | + |
| 233 | + # 5. Use with Nx.dot |
| 234 | + input = Nx.iota({1, 8, 128}, type: :f32) |> Nx.divide(100) |
| 235 | + input = Nx.backend_transfer(input, {EMLX.Backend, device: :gpu}) |
| 236 | + |
| 237 | + result = Nx.dot(input, [2], qt, [1]) |
| 238 | + assert Nx.shape(result) == {1, 8, 256} |
| 239 | + |
| 240 | + # 6. Optionally dequantize for debugging |
| 241 | + dequantized = Quantization.dequantize(q_weight, scales, biases, group_size: 64, bits: 4) |
| 242 | + dequant_nx = EMLX.Backend.to_nx(dequantized) |
| 243 | + assert Nx.shape(dequant_nx) == {256, 128} |
| 244 | + end |
| 245 | + |
| 246 | + test "LLM-style inference pattern" do |
| 247 | + # Simulate a transformer linear layer: |
| 248 | + # hidden_states @ weight.T where weight is quantized |
| 249 | + |
| 250 | + batch_size = 1 |
| 251 | + seq_len = 4 |
| 252 | + hidden_dim = 128 |
| 253 | + output_dim = 256 |
| 254 | + |
| 255 | + # Hidden states from previous layer (using iota for determinism) |
| 256 | + hidden = Nx.iota({batch_size, seq_len, hidden_dim}, type: :f32) |> Nx.divide(100) |
| 257 | + hidden = Nx.backend_transfer(hidden, {EMLX.Backend, device: :gpu}) |
| 258 | + |
| 259 | + # Quantized projection weight |
| 260 | + weight = Nx.iota({output_dim, hidden_dim}, type: :f32) |> Nx.divide(1000) |
| 261 | + weight = Nx.backend_transfer(weight, {EMLX.Backend, device: :gpu}) |
| 262 | + |
| 263 | + {q_weight, scales, biases} = Quantization.quantize(weight) |
| 264 | + qt = Quantization.tensor(q_weight, scales, biases, {output_dim, hidden_dim}) |
| 265 | + |
| 266 | + # Forward pass: hidden @ weight.T |
| 267 | + output = Nx.dot(hidden, [2], qt, [1]) |
| 268 | + |
| 269 | + assert Nx.shape(output) == {batch_size, seq_len, output_dim} |
| 270 | + assert Nx.type(output) == {:f, 32} # Output is float, not quantized |
| 271 | + end |
| 272 | + end |
| 273 | +end |
0 commit comments