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
1 change: 1 addition & 0 deletions .github/workflows/mlx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ jobs:
backends/mlx/test/test_serialization_dedup.py \
backends/mlx/test/test_slot_recycling.py \
backends/mlx/test/test_sample.py \
backends/mlx/test/test_run_all_tests.py \
examples/models/gemma4_31b/tests/test_mlx_pipeline.py \
examples/models/muse-glimmer/tests/test_mlx_pipeline.py \
examples/models/muse-glimmer/tests/test_mlx_offgraph.py \
Expand Down
5 changes: 3 additions & 2 deletions backends/mlx/llm/source_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def replace_et_kv_cache_with_mlx(
Replace ET's KVCache with MLX-optimized KVCache.

Recursively finds all KVCache instances (from examples/models/llama/attention.py)
and replaces them with KVCache, which uses mlx::kv_cache_update instead of
unsupported index_put operations.
and replaces them with KVCache, which writes the new positions into the
cache in place with mlx::kv_cache_update. A plain index_put lowers to a
scatter that copies the whole cache on every step.

Args:
module: Model to modify (in place)
Expand Down
217 changes: 184 additions & 33 deletions backends/mlx/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
RoundNode,
RsqrtNode,
ScatterAddNode,
ScatterNode,
SdpaNode,
SigmoidNode,
SignNode,
Expand Down Expand Up @@ -2481,33 +2482,16 @@ def _index_handler(P: MLXProgramBuilder, n: Node) -> Slot:
non_none = _constant_mask_as_positions(P, n, non_none)

if len(non_none) == 1:
axis, idx, idx_meta = non_none[0]
ndim_match = (
x_meta is not None
and idx_meta is not None
and len(x_meta.shape) == len(idx_meta.shape)
)
axis, idx, _ = non_none[0]
out = P.make_or_get_slot(n)
if ndim_match:
# Same ndim: use TakeAlongAxisNode (element-wise gather)
P.emit(
TakeAlongAxisNode(
x=P.slot_to_tid(x),
indices=P.slot_to_tid(idx),
out=P.slot_to_tid(out),
axis=axis,
)
)
else:
# Different ndim (e.g. 1D indices into 3D tensor): use TakeNode
P.emit(
TakeNode(
x=P.slot_to_tid(x),
index=IntOrVidOrTid.from_tid(P.slot_to_tid(idx)),
out=P.slot_to_tid(out),
axis=axis,
)
P.emit(
TakeNode(
x=P.slot_to_tid(x),
index=IntOrVidOrTid.from_tid(P.slot_to_tid(idx)),
out=P.slot_to_tid(out),
axis=axis,
)
)
return out

# Multi-index: use GatherNode (maps to mlx::gather)
Expand Down Expand Up @@ -2619,6 +2603,62 @@ def _index_select_handler(P: MLXProgramBuilder, n: Node) -> Slot:
return out


@REGISTRY.register(target=[torch.ops.aten.gather.default])
def _gather_handler(P: MLXProgramBuilder, n: Node) -> Slot:
"""Handle aten.gather: pick elements of x along dim at the index positions.

Maps to mlx::take_along_axis, which broadcasts the index against x on the
other axes. aten.gather instead reads only the first index.size(d) entries
of x there, so x is sliced to the index shape first where the two differ.

Unlike eager, an out-of-range index does not raise: the runtime clamps it
to the first or last element so the read stays inside x.
"""
args = P.args(n)
require_args(args, 3, 3, "aten.gather")
# sparse_grad only changes the backward pass.
require_kwargs(P.kwargs(n), {"sparse_grad"}, "aten.gather")
x, dim, index = args
x_shape = n.args[0].meta["val"].shape
index_shape = n.args[2].meta["val"].shape
if len(x_shape) == 0 or len(index_shape) != len(x_shape):
raise NotImplementedError(
f"aten.gather: input of rank {len(x_shape)} with index of rank "
f"{len(index_shape)} is not supported"
)
require_static_int(dim, "dim", "aten.gather")
axis = dim % len(x_shape)
for d, (x_size, index_size) in enumerate(zip(x_shape, index_shape)):
if d == axis or statically_known_true(x_size == index_size):
continue
if not isinstance(index_size, int):
raise NotImplementedError(
f"aten.gather: index size {index_size} on dim {d} may differ "
f"from the input size {x_size}"
)
_, sliced = P.make_tmp_slot()
P.emit(
SliceNode(
x=P.slot_to_tid(x),
out=P.slot_to_tid(sliced),
axis=IntOrVid.from_literal(d),
start=IntOrVid.from_literal(0),
stop=IntOrVid.from_literal(index_size),
)
)
x = sliced
out = P.make_or_get_slot(n)
P.emit(
TakeAlongAxisNode(
x=P.slot_to_tid(x),
indices=P.slot_to_tid(index),
out=P.slot_to_tid(out),
axis=axis,
)
)
return out


@REGISTRY.register(target=[torch.ops.aten.slice_scatter.default])
def _slice_scatter_handler(P: MLXProgramBuilder, n: Node) -> Slot:
"""Handle aten.slice_scatter: return a copy of self with self[dim][start:end:step] = src."""
Expand Down Expand Up @@ -2666,7 +2706,7 @@ def _scatter_add_handler(P: MLXProgramBuilder, n: Node) -> Slot:

scatter_add(self, dim, index, src) -> Tensor

Maps to mlx::scatter_add(a, indices, updates, axis).
Maps to mlx::scatter_add_axis(a, indices, updates, axis).
"""
args = P.args(n)
require_args(args, 4, 4, "aten.scatter_add")
Expand All @@ -2689,6 +2729,114 @@ def _scatter_add_handler(P: MLXProgramBuilder, n: Node) -> Slot:
return out


@REGISTRY.register(target=[torch.ops.aten.index_put.default])
def _index_put_handler(P: MLXProgramBuilder, n: Node) -> Slot:
"""Handle aten.index_put: write values into self at the indexed positions.

index_put(self, indices, values, accumulate=False) -> Tensor

Maps to mlx::scatter, or mlx::scatter_add when accumulate is set. MLX takes
the updates as broadcast(indices).shape followed by one slice of self. So
the values are broadcast to the shape self[indices] has in PyTorch, the
index dimensions are moved to the front, and a unit dimension is inserted
for each indexed axis.

Unlike eager, an out-of-range index does not raise: the runtime clamps it
to the first or last slice so the write stays inside self.
"""
args = P.args(n)
require_args(args, 3, 4, "aten.index_put")
require_kwargs(P.kwargs(n), set(), "aten.index_put")
x, indices, values = args[:3]
accumulate = args[3] if len(args) > 3 else False
dtype = n.meta["val"].dtype
# MLX's Metal scatter kernels reject 8-byte element types at run time.
if dtype.itemsize == 8:
raise NotImplementedError(f"aten.index_put on {dtype} is not supported")
if accumulate and dtype == torch.bool:
raise NotImplementedError("aten.index_put with accumulate on bool")

index_vals = [i.meta["val"] for i in n.args[1] if i is not None]
axes = [d for d, i in enumerate(n.args[1]) if i is not None]
if not axes:
raise NotImplementedError("aten.index_put: all indices are None")
if any(i.dtype not in (torch.int32, torch.int64) for i in index_vals):
raise NotImplementedError(
"aten.index_put: only int32 and int64 index tensors are supported"
)

# PyTorch puts the index dimensions where the indexed axes were when those
# axes are adjacent, and in front otherwise.
x_shape = list(n.args[0].meta["val"].shape)
index_shape = list(torch.broadcast_shapes(*(i.shape for i in index_vals)))
rest = [size for d, size in enumerate(x_shape) if d not in axes]
lead = axes[0] if axes == list(range(axes[0], axes[-1] + 1)) else 0
target = rest[:lead] + index_shape + rest[lead:]

values_shape = list(n.args[2].meta["val"].shape)
offset = len(target) - len(values_shape)
if offset or not all(
statically_known_true(v == t) for v, t in zip(values_shape, target)
):
shape = []
for d, size in enumerate(target):
if d >= offset and statically_known_true(values_shape[d - offset] == size):
shape.append(IntOrVid.from_literal(-1))
elif isinstance(size, int):
shape.append(IntOrVid.from_literal(size))
else:
raise NotImplementedError(
f"aten.index_put: cannot broadcast values to dynamic size {size}"
)
_, broadcast = P.make_tmp_slot()
P.emit(
BroadcastToNode(
x=P.slot_to_tid(values),
out=P.slot_to_tid(broadcast),
shape=shape,
)
)
values = broadcast

if lead and index_shape:
index_ndim = len(index_shape)
_, transposed = P.make_tmp_slot()
P.emit(
TransposeNode(
x=P.slot_to_tid(values),
out=P.slot_to_tid(transposed),
perm=list(range(lead, lead + index_ndim))
+ list(range(lead))
+ list(range(lead + index_ndim, len(target))),
)
)
values = transposed

for axis in axes:
_, expanded = P.make_tmp_slot()
P.emit(
ExpandDimsNode(
x=P.slot_to_tid(values),
out=P.slot_to_tid(expanded),
axis=len(index_shape) + axis,
)
)
values = expanded

out = P.make_or_get_slot(n)
P.emit(
ScatterNode(
x=P.slot_to_tid(x),
indices=[P.slot_to_tid(i) for i in indices if i is not None],
updates=P.slot_to_tid(values),
out=P.slot_to_tid(out),
axes=axes,
reduce="sum" if accumulate else "none",
)
)
return out


@REGISTRY.register(target=[torch.ops.aten.select.int, torch.ops.aten.select_copy.int])
def _select_handler(P: MLXProgramBuilder, n: Node) -> Slot:
"""
Expand Down Expand Up @@ -3739,20 +3887,17 @@ def _convolution_handler(P: MLXProgramBuilder, n: Node) -> Slot:
Args layout: convolution(input, weight, bias, stride, padding, dilation,
transposed, output_padding, groups)

This op appears when PyTorch doesn't decompose to specific conv ops
(e.g. grouped conv_transpose).
Conv ops without a handler decompose to this op. For example
conv2d(padding="valid") arrives as convolution with zero padding, and
padding="same" arrives as constant_pad_nd + convolution when one side needs
more padding than the other. Grouped conv_transpose arrives here too.
"""
raw_args = n.args
x_node, w_node = raw_args[0], raw_args[1]
bias_node = raw_args[2] if len(raw_args) > 2 else None
transposed = raw_args[6] if len(raw_args) > 6 else False
groups = raw_args[8] if len(raw_args) > 8 else 1

if not transposed:
raise ValueError(
"aten.convolution with transposed=False: use aten.conv{1,2,3}d instead"
)

x_meta = x_node.meta.get("val")
if x_meta is None:
raise ValueError("aten.convolution: input shape metadata required")
Expand All @@ -3761,6 +3906,12 @@ def _convolution_handler(P: MLXProgramBuilder, n: Node) -> Slot:
stride = _normalize_conv_param(raw_args[3] if len(raw_args) > 3 else 1, ndim, 1)
padding = _normalize_conv_param(raw_args[4] if len(raw_args) > 4 else 0, ndim, 0)
dilation = _normalize_conv_param(raw_args[5] if len(raw_args) > 5 else 1, ndim, 1)

if not transposed:
return _emit_conv(
P, n, x_node, w_node, bias_node, stride, padding, dilation, groups, ndim
)

output_padding = _normalize_conv_param(
raw_args[7] if len(raw_args) > 7 else 0, ndim, 0
)
Expand Down
3 changes: 1 addition & 2 deletions backends/mlx/pte_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def show_mlx_instructions(pte_data: bytes) -> None: # noqa: C901
mlx_delegates = _find_mlx_delegates(pte_data)
if not mlx_delegates:
print("No MLX delegates found in this PTE file.", file=sys.stderr)
sys.exit(1)
return

if len(mlx_delegates) > 1:
print(
Expand Down Expand Up @@ -747,7 +747,6 @@ def show_mlx_instructions(pte_data: bytes) -> None: # noqa: C901
except Exception as e:
print(f"Error showing MLX instructions: {e}", file=sys.stderr)
traceback.print_exc()
sys.exit(1)


def main(): # noqa: C901
Expand Down
Loading