Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion mlx/backend/cuda/quantized/quantized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,16 @@ void GatherQMM::eval_gpu(const std::vector<array>& inputs, array& out) {
auto& s = stream();
auto& encoder = cu::get_command_encoder(s);

if (mode_ != QuantizationMode::Affine && inputs.size() == 6) {
throw std::runtime_error(
"[GatherQMM] Global scale is only supported on the Metal backend.");
}

array x = ensure_row_contiguous(inputs[0], encoder, s);
const array& w = inputs[1];
const array& scales = inputs[2];
std::optional<array> biases;
if (inputs.size() == 6) {
if (mode_ == QuantizationMode::Affine) {
biases = inputs[3];
}
array lhs_indices =
Expand Down
84 changes: 58 additions & 26 deletions mlx/backend/metal/jit_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,28 +1054,44 @@ MTL::ComputePipelineState* get_gather_qmm_kernel(
int bk,
int wm,
int wn,
bool transpose) {
bool transpose,
bool has_global_scale) {
const auto& lib_name = kernel_name;
auto lib = d.get_library(lib_name, [&]() {
std::string kernel_source;
concatenate(
kernel_source, metal::utils(), metal::quantized_utils(), metal::gemm());
bool is_affine = mode == "affine";
// Only the fp kernel takes a global scale.
auto template_def = is_affine ? get_template_definition(
lib_name,
"affine_gather_qmm_rhs",
get_type_string(x.dtype()),
group_size,
bits,
bm,
bn,
bk,
wm,
wn,
transpose)
: get_template_definition(
lib_name,
"fp_gather_qmm_rhs",
get_type_string(x.dtype()),
group_size,
bits,
bm,
bn,
bk,
wm,
wn,
transpose,
has_global_scale);
concatenate(
kernel_source,
is_affine ? metal::quantized() : metal::fp_quantized(),
get_template_definition(
lib_name,
(is_affine ? "affine" : "fp") + std::string("_gather_qmm_rhs"),
get_type_string(x.dtype()),
group_size,
bits,
bm,
bn,
bk,
wm,
wn,
transpose));
template_def);
return kernel_source;
});
return d.get_kernel(kernel_name, lib, hash_name, func_consts);
Expand Down Expand Up @@ -1255,7 +1271,8 @@ MTL::ComputePipelineState* get_gather_qmm_nax_kernel(
int bk,
int wm,
int wn,
bool transpose) {
bool transpose,
bool has_global_scale) {
const auto& lib_name = kernel_name;
auto lib = d.get_library(lib_name, [&]() {
std::string kernel_source;
Expand All @@ -1265,21 +1282,36 @@ MTL::ComputePipelineState* get_gather_qmm_nax_kernel(
metal::gemm_nax(),
metal::quantized_utils());
bool is_affine = mode == "affine";
// Only the fp kernel takes a global scale.
auto template_def = is_affine ? get_template_definition(
lib_name,
"affine_gather_qmm_rhs_nax",
get_type_string(x.dtype()),
group_size,
bits,
bm,
bn,
bk,
wm,
wn,
transpose)
: get_template_definition(
lib_name,
"fp_gather_qmm_rhs_nax",
get_type_string(x.dtype()),
group_size,
bits,
bm,
bn,
bk,
wm,
wn,
transpose,
has_global_scale);
concatenate(
kernel_source,
is_affine ? metal::quantized_nax() : metal::fp_quantized_nax(),
get_template_definition(
lib_name,
(is_affine ? "affine" : "fp") + std::string("_gather_qmm_rhs_nax"),
get_type_string(x.dtype()),
group_size,
bits,
bm,
bn,
bk,
wm,
wn,
transpose));
template_def);
return kernel_source;
});
return d.get_kernel(kernel_name, lib, hash_name, func_consts);
Expand Down
6 changes: 4 additions & 2 deletions mlx/backend/metal/kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ MTL::ComputePipelineState* get_gather_qmm_kernel(
int bk,
int wm,
int wn,
bool transpose);
bool transpose,
bool has_global_scale);

MTL::ComputePipelineState* get_steel_gemm_fused_nax_kernel(
metal::Device& d,
Expand Down Expand Up @@ -400,7 +401,8 @@ MTL::ComputePipelineState* get_gather_qmm_nax_kernel(
int bk,
int wm,
int wn,
bool transpose);
bool transpose,
bool has_global_scale);

MTL::ComputePipelineState* get_steel_attention_kernel(
metal::Device& d,
Expand Down
Loading
Loading