Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
67 changes: 52 additions & 15 deletions mlx/backend/metal/kernels/quantized.h
Original file line number Diff line number Diff line change
Expand Up @@ -1932,26 +1932,27 @@ template <typename T, int group_size, int bits, int WK, int BN, int TM>
static_assert(BN % 8 == 0, "BN should be divisible by 8");
static_assert(TM == 1 || TM == 2, "qmv_mma takes at most 16 rows");
static_assert(
(bits == 4 || bits == 8) &&
(bits >= 2 && bits <= 8 && bits != 7) &&
(group_size == 32 || group_size == 64 || group_size == 128),
"qmv_mma reads 4 and 8 bits at group sizes 32, 64 and 128");
"qmv_mma reads 2 to 8 bits (not 7) at group sizes 32, 64 and 128");

using Frag = mlx::steel::BaseMMAFrag<float, 8, 8>;
using frag_t = typename Frag::frag_type;
constexpr int BK = 128;
constexpr short TN = BN / 8;
constexpr int pack_factor = 8 / bits;
constexpr short loads = bits / 4;
// The lane's 32 values take bits words
constexpr short words = bits;
constexpr uint mask = (1 << bits) - 1;

const short2 coord = Frag::get_coord(simd_lid);
const short fn = coord.x;
const short fm = coord.y;
const int row_bytes = K / pack_factor;
const int row_bytes = K / 8 * bits;
const int row_scales = K / group_size;

const int y_col = tid.x * BN;
auto wl = (const device uint8_t*)w +
(y_col + fm) * static_cast<int64_t>(row_bytes) + 16 * fn / pack_factor;
(y_col + fm) * static_cast<int64_t>(row_bytes) + 2 * fn * bits;
const int64_t s_off =
(y_col + fm) * static_cast<int64_t>(row_scales) + 16 * fn / group_size;
scales += s_off;
Expand Down Expand Up @@ -1986,16 +1987,34 @@ template <typename T, int group_size, int bits, int WK, int BN, int TM>
}
}
}
uint4 ws[TN][loads];
uint ws[TN][words];
float sc[TN];
float bs[TN];
STEEL_PRAGMA_UNROLL
for (short t = 0; t < TN; t++) {
const device uint8_t* wt =
wl + 8 * t * static_cast<int64_t>(row_bytes) + k / pack_factor;
STEEL_PRAGMA_UNROLL
for (short l = 0; l < loads; l++) {
ws[t][l] = *(const device uint4*)(wt + 16 * l);
wl + 8 * t * static_cast<int64_t>(row_bytes) + k / 8 * bits;
if constexpr (words % 4 == 0) {
STEEL_PRAGMA_UNROLL
for (short l = 0; l < words; l += 4) {
const uint4 v = *(const device uint4*)(wt + 4 * l);
ws[t][l] = v.x;
ws[t][l + 1] = v.y;
ws[t][l + 2] = v.z;
ws[t][l + 3] = v.w;
}
} else if constexpr (words % 2 == 0) {
STEEL_PRAGMA_UNROLL
for (short l = 0; l < words; l += 2) {
const uint2 v = *(const device uint2*)(wt + 4 * l);
ws[t][l] = v.x;
ws[t][l + 1] = v.y;
}
} else {
STEEL_PRAGMA_UNROLL
for (short l = 0; l < words; l++) {
ws[t][l] = ((const device uint*)wt)[l];
}
}
const int64_t si =
8 * t * static_cast<int64_t>(row_scales) + k / group_size;
Expand All @@ -2021,19 +2040,37 @@ template <typename T, int group_size, int bits, int WK, int BN, int TM>
// each half reads as 1024 + q, and taking 1024 off again is exact.
uint p[4];
if constexpr (bits == 4) {
const uint lo = ws[t][0][g / 2];
const uint hi = ws[t][0][2 + g / 2];
const uint lo = ws[t][g / 2];
const uint hi = ws[t][2 + g / 2];
const uint q = (g % 2) == 0 ? (lo & 0xffff) | (hi << 16)
: (lo >> 16) | (hi & 0xffff0000);
STEEL_PRAGMA_UNROLL
for (short i = 0; i < 4; i++) {
p[i] = (q >> (4 * i)) & 0x000f000f;
}
} else if constexpr (bits == 8) {
STEEL_PRAGMA_UNROLL
for (short i = 0; i < 4; i++) {
p[i] = ((ws[t][g] >> (8 * i)) & 0xff) |
(((ws[t][4 + g] >> (8 * i)) & 0xff) << 16);
}
} else {
// Values 4g to 4g+3 and 16+4g to 16+4g+3, which can span two words
uint q[2];
STEEL_PRAGMA_UNROLL
for (short h = 0; h < 2; h++) {
const short bit = (16 * h + 4 * g) * bits;
const short word = bit / 32;
const short shift = bit % 32;
q[h] = ws[t][word] >> shift;
if (shift + 4 * bits > 32) {
q[h] |= ws[t][word + 1] << (32 - shift);
}
}
STEEL_PRAGMA_UNROLL
for (short i = 0; i < 4; i++) {
p[i] = ((ws[t][0][g] >> (8 * i)) & 0xff) |
(((ws[t][1][g] >> (8 * i)) & 0xff) << 16);
p[i] = ((q[0] >> (bits * i)) & mask) |
(((q[1] >> (bits * i)) & mask) << 16);
}
}
STEEL_PRAGMA_UNROLL
Expand Down
4 changes: 4 additions & 0 deletions mlx/backend/metal/kernels/quantized.metal
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@
instantiate_quantized_all(type, group_size, 5) \
instantiate_quantized_all(type, group_size, 6) \
instantiate_quantized_all(type, group_size, 8) \
instantiate_qmv_mma_all(type, group_size, 2) \
instantiate_qmv_mma_all(type, group_size, 3) \
instantiate_qmv_mma_all(type, group_size, 4) \
instantiate_qmv_mma_all(type, group_size, 5) \
instantiate_qmv_mma_all(type, group_size, 6) \
instantiate_qmv_mma_all(type, group_size, 8)

#define instantiate_quantized_all_groups(type) \
Expand Down
9 changes: 5 additions & 4 deletions mlx/backend/metal/quantized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,11 @@ inline bool use_qmv_mma(
if (d.get_architecture().back() == 'd') {
return false;
}
// Only 4 and 8 bits are instantiated; affine pays off from a smaller N *
// K than fp.
return (bits == 4 || bits == 8) && (N >= 512) && (K >= 512) &&
(int64_t(N) * K >= (int64_t(1) << 21)) && (N % 16 == 0);
// qmv_mma reads 2 to 8 bits (not 7). Affine pays off from a smaller
// N * K than fp, and from 2^20 on gen 15+ (M4).
int nk_log2 = d.get_architecture_gen() >= 15 ? 20 : 21;
return (bits >= 2 && bits != 7) && (N >= 512) && (K >= 512) &&
(int64_t(N) * K >= (int64_t(1) << nk_log2)) && (N % 16 == 0);
}
} else { // fp quantization
if (nax) {
Expand Down
3 changes: 2 additions & 1 deletion python/tests/test_quantized.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,10 @@ def rel_err(got, expected):
[
(4224, 4128),
(512, 4096), # smallest K: one K step per simdgroup
(1024, 1024), # smallest N * K on gen 15+
],
[3, 8, 9],
[4, 8],
[2, 3, 4, 5, 6, 8],
[32, 64, 128],
[mx.bfloat16, mx.float16, mx.float32],
):
Expand Down
Loading