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
22 changes: 22 additions & 0 deletions mlx/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3776,6 +3776,18 @@ bool GatherQMM::is_equivalent(const Primitive& other) const {
mode_ == qm_other.mode_ && transpose_ == qm_other.transpose_;
}

std::vector<Shape> GatherQMM::output_shapes(const std::vector<array>& inputs) {
const auto& x = inputs[0];
const auto& w = inputs[1];
const auto& lhs_indices =
(mode_ == QuantizationMode::Affine) ? inputs[4] : inputs[3];
int w_outer = transpose_ ? w.shape(-2) : w.shape(-1) * 32 / bits_;
auto out_shape = lhs_indices.shape();
out_shape.push_back(x.shape(-2));
out_shape.push_back(w_outer);
return {out_shape};
}

std::pair<std::vector<array>, std::vector<int>> RandomBits::vmap(
const std::vector<array>& inputs,
const std::vector<int>& axes) {
Expand Down Expand Up @@ -5882,6 +5894,16 @@ bool GatherMM::is_equivalent(const Primitive& other) const {
right_sorted_ == g_other.right_sorted_;
}

std::vector<Shape> GatherMM::output_shapes(const std::vector<array>& inputs) {
const auto& a = inputs[0];
const auto& b = inputs[1];
const auto& lhs_indices = inputs[2];
auto out_shape = lhs_indices.shape();
out_shape.push_back(a.shape(-2));
out_shape.push_back(b.shape(-1));
return {out_shape};
}

bool BlockMaskedMM::is_equivalent(const Primitive& other) const {
const BlockMaskedMM& a_other = static_cast<const BlockMaskedMM&>(other);
return (block_size_ == a_other.block_size_);
Expand Down
2 changes: 2 additions & 0 deletions mlx/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ class GatherMM : public UnaryPrimitive {

DEFINE_NAME(GatherMM)
bool is_equivalent(const Primitive& other) const override;
std::vector<Shape> output_shapes(const std::vector<array>& inputs) override;
auto state() const {
return std::make_pair(left_sorted_, right_sorted_);
}
Expand Down Expand Up @@ -1700,6 +1701,7 @@ class GatherQMM : public UnaryPrimitive {
DEFINE_GRADS()
DEFINE_NAME(GatherQMM)
bool is_equivalent(const Primitive& other) const override;
std::vector<Shape> output_shapes(const std::vector<array>& inputs) override;
auto state() const {
return std::make_tuple(
group_size_, bits_, mode_, transpose_, left_sorted_, right_sorted_);
Expand Down
39 changes: 39 additions & 0 deletions python/tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,45 @@ def ones_fun(x):
self.assertEqual(compiled_zero_like(y).shape, y_shape)
self.assertEqual(compiled_ones_like(y).shape, y_shape)

def test_shapeless_compile_gather_qmm(self):
K, N, num_experts = 64, 32, 4

w = mx.random.normal((num_experts, N, K))
qw, s, b = mx.quantize(w)
mx.eval(qw, s, b)

idx = mx.array([0, 1, 2, 3])
x4 = mx.ones((num_experts, 4, K))
x8 = mx.ones((num_experts, 8, K))

def fn(x):
return mx.gather_qmm(
x, qw, s, b, lhs_indices=idx, rhs_indices=idx, transpose=True
)

cfn = mx.compile(fn, shapeless=True)

self.assertEqual(cfn(x4).shape, fn(x4).shape)
self.assertEqual(cfn(x8).shape, fn(x8).shape)

def test_shapeless_compile_gather_mm(self):
K, N, num_experts = 64, 32, 4

idx = mx.array([0, 1, 2, 3])
b = mx.random.normal((num_experts, K, N))
mx.eval(b)

x4 = mx.ones((num_experts, 4, K))
x8 = mx.ones((num_experts, 8, K))

def fn(x):
return mx.gather_mm(x, b, lhs_indices=idx, rhs_indices=idx)

cfn = mx.compile(fn, shapeless=True)

self.assertEqual(cfn(x4).shape, fn(x4).shape)
self.assertEqual(cfn(x8).shape, fn(x8).shape)

def test_compile_with_constant(self):
# Test float
@partial(mx.compile)
Expand Down
Loading