Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
07ed619
Fix conv2d OOB reads for partial output-channel tiles (#4653)
navthings Oct 16, 2026
41615bf
Fix GPU pad for outputs larger than 2^31 elements (#4744)
tivris Oct 23, 2026
6f31adb
Fix flipped bounds check in the Metal large-filter convolution loader…
ssmall256 Oct 26, 2026
6aebef5
Fix strided_scan reading/writing out-of-bounds (#4430)
zcbenz Oct 26, 2026
cd3b4d8
Fix GPU concatenate for outputs larger than 2^31 elements (#4758)
tivris Oct 28, 2026
a315e02
Fix Metal and CUDA affine quantization for all-negative groups (#4775)
lgyStoic Oct 28, 2026
9835ed5
Fix gguf tensor to f16 size overflow (#4764)
turingH Oct 28, 2026
db561a5
[Metal] Fix precision loss in addmm (#4769)
tetsuo Oct 29, 2026
46c7b5c
[CPU] Fix accumulation precision in affine quantized matmul (#4803)
erwinzhang7 Nov 5, 2026
dc2d4bd
metal: MLX_QMM_TILE64=1, 64 x 64 tiles for large affine qmm on GPUs w…
jeethu Nov 15, 2026
b7a0d4a
tests: MLX_QMM_TILE64 route and numerics (numen-tech/gemma4-qat#1695)
jeethu Nov 15, 2026
358204a
tests: an all-negative 8-bit K/V group quantizes to its own range (nu…
jeethu Nov 15, 2026
cbdf97f
tests: the tile64 trace test expects 32 x 32 on an Ultra part at 160 …
jeethu Nov 15, 2026
81e2731
tests: the tile64 trace test restores MLX_QMM_TILE64 and stops the tr…
jeethu Nov 15, 2026
f37bc70
qmm tile64: shorten the gate comment, exclude Apple6 in the trace test
jeethu Nov 16, 2026
12ce20c
Trim the tile64 and K/V test commentary to the non-obvious cases
jeethu Nov 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 79 additions & 37 deletions mlx/backend/cpu/quantized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ array ensure_row_contiguous(
}
};

// Row accumulator for the non-transposed affine kernel, taken from the
// allocator so it is served by the buffer cache. Empty when transposed.
array qmm_accumulator(
const array& out,
bool transposed_w,
cpu::CommandEncoder& encoder) {
array acc({transposed_w ? 0 : out.shape(-1)}, float32, nullptr, {});
acc.set_data(allocator::malloc(acc.nbytes()));
encoder.add_temporary(acc);
encoder.set_input_array(acc);
return acc;
}

const static float FP4_LUT[16] = {
+0.0f,
+0.5f,
Expand Down Expand Up @@ -102,7 +115,8 @@ void _qmm(
const T* biases,
int M,
int N,
int K) {
int K,
float* acc) {
constexpr int bitmask = (1 << bits) - 1;
constexpr int pack_factor = get_pack_factor(bits, 8);
constexpr int bytes_per_pack = get_bytes_per_pack(bits);
Expand All @@ -113,15 +127,15 @@ void _qmm(
const T* scales_local = scales;
const T* biases_local = biases;

std::fill(result, result + N, 0);
std::fill(acc, acc + N, 0.0f);

for (int k = 0; k < K; k++) {
T* result_local = result;
float* result_local = acc;
T xi = *x++;

for (int n = 0; n < N; n += group_size) {
T scale = *scales_local++;
T bias = *biases_local++;
float scale = *scales_local++;
float bias = *biases_local++;
for (int ng = 0; ng < packs_in_group; ng++) {
if constexpr (bits == 3 || bits == 5 || bits == 6) {
T wl[pack_factor];
Expand All @@ -147,6 +161,7 @@ void _qmm(
}
}

std::copy(acc, acc + N, result);
result += N;
}
}
Expand Down Expand Up @@ -174,10 +189,10 @@ void _qmm_t(

for (int n = 0; n < N; n++) {
const T* x_local = x;
T sum = 0;
float sum = 0;
for (int k = 0; k < K; k += group_size) {
T scale = *scales_local++;
T bias = *biases_local++;
float scale = *scales_local++;
float bias = *biases_local++;

for (int kw = 0; kw < packs_in_group; kw++) {
if constexpr (bits == 3 || bits == 5 || bits == 6) {
Expand Down Expand Up @@ -293,7 +308,8 @@ void _qmm_dispatch_transpose(
int M,
int N,
int K,
bool transposed_w) {
bool transposed_w,
float* acc) {
if (transposed_w) {
// the simd size must be a multiple of the number of elements per word
if constexpr (32 % bits == 0 && simd::max_size<T> % (32 / bits) == 0) {
Expand All @@ -302,7 +318,7 @@ void _qmm_dispatch_transpose(
_qmm_t<T, bits, group_size>(result, x, w, scales, biases, M, N, K);
}
} else {
_qmm<T, bits, group_size>(result, x, w, scales, biases, M, N, K);
_qmm<T, bits, group_size>(result, x, w, scales, biases, M, N, K, acc);
}
}

Expand All @@ -317,19 +333,20 @@ void _qmm_dispatch_group(
int N,
int K,
int group_size,
bool transposed_w) {
bool transposed_w,
float* acc) {
switch (group_size) {
case 32:
_qmm_dispatch_transpose<T, bits, 32>(
result, x, w, scales, biases, M, N, K, transposed_w);
result, x, w, scales, biases, M, N, K, transposed_w, acc);
break;
case 64:
_qmm_dispatch_transpose<T, bits, 64>(
result, x, w, scales, biases, M, N, K, transposed_w);
result, x, w, scales, biases, M, N, K, transposed_w, acc);
break;
case 128:
_qmm_dispatch_transpose<T, bits, 128>(
result, x, w, scales, biases, M, N, K, transposed_w);
result, x, w, scales, biases, M, N, K, transposed_w, acc);
break;
default:
throw std::invalid_argument(
Expand All @@ -349,35 +366,36 @@ void _qmm_dispatch_typed(
int K,
int group_size,
int bits,
bool transposed_w) {
bool transposed_w,
float* acc) {
switch (bits) {
case 1:
_qmm_dispatch_group<T, 1>(
result, x, w, scales, biases, M, N, K, group_size, transposed_w);
result, x, w, scales, biases, M, N, K, group_size, transposed_w, acc);
break;
case 2:
_qmm_dispatch_group<T, 2>(
result, x, w, scales, biases, M, N, K, group_size, transposed_w);
result, x, w, scales, biases, M, N, K, group_size, transposed_w, acc);
break;
case 3:
_qmm_dispatch_group<T, 3>(
result, x, w, scales, biases, M, N, K, group_size, transposed_w);
result, x, w, scales, biases, M, N, K, group_size, transposed_w, acc);
break;
case 4:
_qmm_dispatch_group<T, 4>(
result, x, w, scales, biases, M, N, K, group_size, transposed_w);
result, x, w, scales, biases, M, N, K, group_size, transposed_w, acc);
break;
case 5:
_qmm_dispatch_group<T, 5>(
result, x, w, scales, biases, M, N, K, group_size, transposed_w);
result, x, w, scales, biases, M, N, K, group_size, transposed_w, acc);
break;
case 6:
_qmm_dispatch_group<T, 6>(
result, x, w, scales, biases, M, N, K, group_size, transposed_w);
result, x, w, scales, biases, M, N, K, group_size, transposed_w, acc);
break;
case 8:
_qmm_dispatch_group<T, 8>(
result, x, w, scales, biases, M, N, K, group_size, transposed_w);
result, x, w, scales, biases, M, N, K, group_size, transposed_w, acc);
break;
default:
throw std::invalid_argument(
Expand All @@ -394,7 +412,8 @@ void _qmm_dispatch_typed(
const array& biases,
int bits,
int group_size,
bool transposed_w) {
bool transposed_w,
array& acc) {
int K = x.shape(-1);
int M = x.ndim() > 1 ? x.shape(-2) : 1;
int N = out.shape(-1);
Expand All @@ -419,7 +438,8 @@ void _qmm_dispatch_typed(
K,
bits,
group_size,
transposed_w);
transposed_w,
acc.data<float>());
}
}

Expand All @@ -431,19 +451,20 @@ void _qmm_dispatch(
const array& biases,
int bits,
int group_size,
bool transposed_w) {
bool transposed_w,
array& acc) {
switch (x.dtype()) {
case float32:
_qmm_dispatch_typed<float>(
out, x, w, scales, biases, bits, group_size, transposed_w);
out, x, w, scales, biases, bits, group_size, transposed_w, acc);
break;
case float16:
_qmm_dispatch_typed<float16_t>(
out, x, w, scales, biases, bits, group_size, transposed_w);
out, x, w, scales, biases, bits, group_size, transposed_w, acc);
break;
case bfloat16:
_qmm_dispatch_typed<bfloat16_t>(
out, x, w, scales, biases, bits, group_size, transposed_w);
out, x, w, scales, biases, bits, group_size, transposed_w, acc);
break;
default:
throw std::invalid_argument(
Expand Down Expand Up @@ -713,7 +734,8 @@ void _bs_qmm_dispatch_typed(
const array& rhs_indices,
int bits,
int group_size,
bool transposed_w) {
bool transposed_w,
array& acc) {
int K = x.shape(-1);
int M = x.shape(-2);
int N = out.shape(-1);
Expand Down Expand Up @@ -747,7 +769,8 @@ void _bs_qmm_dispatch_typed(
K,
bits,
group_size,
transposed_w);
transposed_w,
acc.data<float>());
}
}

Expand All @@ -761,7 +784,8 @@ void _bs_qmm_dispatch(
const array& rhs_indices,
int bits,
int group_size,
bool transposed_w) {
bool transposed_w,
array& acc) {
switch (x.dtype()) {
case float32:
_bs_qmm_dispatch_typed<float>(
Expand All @@ -774,7 +798,8 @@ void _bs_qmm_dispatch(
rhs_indices,
bits,
group_size,
transposed_w);
transposed_w,
acc);
break;
case float16:
_bs_qmm_dispatch_typed<float16_t>(
Expand All @@ -787,7 +812,8 @@ void _bs_qmm_dispatch(
rhs_indices,
bits,
group_size,
transposed_w);
transposed_w,
acc);
break;
case bfloat16:
_bs_qmm_dispatch_typed<bfloat16_t>(
Expand All @@ -800,7 +826,8 @@ void _bs_qmm_dispatch(
rhs_indices,
bits,
group_size,
transposed_w);
transposed_w,
acc);
break;
default:
throw std::invalid_argument(
Expand Down Expand Up @@ -951,6 +978,7 @@ void QuantizedMatmul::eval_cpu(const std::vector<array>& inputs, array& out) {
}
auto biases = ensure_row_contiguous(inputs[3], encoder, stream());
encoder.set_input_array(biases);
auto acc = qmm_accumulator(out, transpose_, encoder);
if (is_implied_bias(biases)) {
// Implied bias: expand the factor f into a temporary `scales * T(f)`
// for the per-group-bias kernels.
Expand All @@ -964,6 +992,7 @@ void QuantizedMatmul::eval_cpu(const std::vector<array>& inputs, array& out) {
scales = array::unsafe_weak_copy(scales),
factor = array::unsafe_weak_copy(biases),
full_biases = array::unsafe_weak_copy(full_biases),
acc = array::unsafe_weak_copy(acc),
group_size_ = group_size_,
bits_ = bits_,
transpose_ = transpose_]() mutable {
Expand Down Expand Up @@ -1005,7 +1034,15 @@ void QuantizedMatmul::eval_cpu(const std::vector<array>& inputs, array& out) {
"supported with an implied bias.");
}
_qmm_dispatch(
out, x, w, scales, full_biases, group_size_, bits_, transpose_);
out,
x,
w,
scales,
full_biases,
group_size_,
bits_,
transpose_,
acc);
});
return;
}
Expand All @@ -1014,10 +1051,12 @@ void QuantizedMatmul::eval_cpu(const std::vector<array>& inputs, array& out) {
w = array::unsafe_weak_copy(w),
scales = array::unsafe_weak_copy(scales),
biases = array::unsafe_weak_copy(biases),
acc = array::unsafe_weak_copy(acc),
group_size_ = group_size_,
bits_ = bits_,
transpose_ = transpose_]() mutable {
_qmm_dispatch(out, x, w, scales, biases, group_size_, bits_, transpose_);
_qmm_dispatch(
out, x, w, scales, biases, group_size_, bits_, transpose_, acc);
});
} else {
encoder.dispatch([out = array::unsafe_weak_copy(out),
Expand Down Expand Up @@ -1069,13 +1108,15 @@ void GatherQMM::eval_cpu(const std::vector<array>& inputs, array& out) {
if (mode_ == QuantizationMode::Affine) {
auto biases = ensure_row_contiguous_last_dims(inputs[3]);
encoder.set_input_array(biases);
auto acc = qmm_accumulator(out, transpose_, encoder);
encoder.dispatch([out = array::unsafe_weak_copy(out),
x = array::unsafe_weak_copy(x),
w = array::unsafe_weak_copy(w),
scales = array::unsafe_weak_copy(scales),
biases = array::unsafe_weak_copy(biases),
lhs_indices = array::unsafe_weak_copy(lhs_indices),
rhs_indices = array::unsafe_weak_copy(rhs_indices),
acc = array::unsafe_weak_copy(acc),
group_size_ = group_size_,
bits_ = bits_,
transpose_ = transpose_]() mutable {
Expand All @@ -1089,7 +1130,8 @@ void GatherQMM::eval_cpu(const std::vector<array>& inputs, array& out) {
rhs_indices,
group_size_,
bits_,
transpose_);
transpose_,
acc);
});
} else {
encoder.dispatch([out = array::unsafe_weak_copy(out),
Expand Down
2 changes: 1 addition & 1 deletion mlx/backend/cuda/quantized/affine_quantize.cu
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ affine_quantize(const T* w, uint8_t* out, T* scales, T* biases, size_t size) {

float w_thread[values_per_reduce];
float w_min = Limits<float>::max();
float w_max = 0;
float w_max = -Limits<float>::max();

#pragma clang loop unroll(full)
for (int i = 0; i < values_per_reduce; i++) {
Expand Down
11 changes: 8 additions & 3 deletions mlx/backend/cuda/scan.cu
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ __global__ void strided_scan(
U* out,
int32_t axis_size,
int64_t stride,
int64_t stride_blocks) {
int64_t stride_blocks,
int64_t data_size) {
auto grid = cg::this_grid();
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<WARP_SIZE>(block);
Expand All @@ -233,7 +234,10 @@ __global__ void strided_scan(
uint32_t scan_offset_y = warp.thread_rank();
uint32_t scan_offset_x = warp.meta_group_rank() * n_scans;

uint32_t stride_limit = stride - global_index_x;
int64_t stride_limit = cuda::std::min(
stride - global_index_x,
data_size - offset - global_index_x);

in += offset + global_index_x + read_offset_x;
out += offset + global_index_x + read_offset_x;
U* read_into = read_buffer + read_offset_y * BN_pad + read_offset_x;
Expand Down Expand Up @@ -435,7 +439,8 @@ void scan_gpu_inplace(
gpu_ptr<U>(out),
axis_size,
stride,
stride_blocks);
stride_blocks,
int64_t(in.data_size()));
}
});
});
Expand Down
Loading