Skip to content
Open
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
57 changes: 56 additions & 1 deletion backends/mlx/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from __future__ import annotations

import operator
from typing import Any, Dict, List, Optional, Set, Tuple, Union
from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, Union

import torch
from executorch.backends.mlx.builder.op_helpers import (
Expand Down Expand Up @@ -5696,13 +5696,23 @@ def _sort_handler(P: MLXProgramBuilder, n: Node) -> Slot:
require_kwargs(P.kwargs(n), set(), "aten.sort")
x = args[0]
dim = args[1] if len(args) > 1 else -1
descending = bool(args[2]) if len(args) > 2 else False

# torch.sort returns (values, indices) - 2 outputs
output_slots = P.make_or_get_slots(n)
values_slot, indices_slot = output_slots

used = used_getitem_indices(n)

if descending:
targets: List[Tuple[type, Slot]] = []
if 0 in used:
targets.append((SortNode, values_slot))
if 1 in used:
targets.append((ArgsortNode, indices_slot))
_emit_descending_sort(P, n, x, dim, targets)
return output_slots

if 0 in used:
P.emit(
SortNode(
Expand All @@ -5723,6 +5733,46 @@ def _sort_handler(P: MLXProgramBuilder, n: Node) -> Slot:
return output_slots


def _emit_descending_sort(
P: MLXProgramBuilder,
n: Node,
x: Slot,
dim: int,
targets: Sequence[Tuple[type, Slot]],
) -> None:
"""Emit each ``(node_cls, out_slot)`` target as a descending sort/argsort.

Emits an ascending ``SortNode``/``ArgsortNode`` into a tmp slot, then
reverses along ``dim`` with a step=-1 slice (the same trick
``_topk_handler`` uses) to obtain descending order.
"""
x_meta = n.args[0].meta.get("val")
if x_meta is None:
raise ValueError("Input tensor metadata not found for aten.sort")
norm_axis = dim if dim >= 0 else dim + len(x_meta.shape)
dim_size = x_meta.shape[norm_axis]

for node_cls, out_slot in targets:
_, sort_tmp = P.make_tmp_slot()
P.emit(
node_cls(
x=P.slot_to_tid(x),
out=P.slot_to_tid(sort_tmp),
axis=dim,
)
)
P.emit(
SliceNode(
x=P.slot_to_tid(sort_tmp),
out=P.slot_to_tid(out_slot),
axis=P.to_int_or_vid(dim),
start=P.to_int_or_vid(dim_size - 1),
stop=P.to_int_or_vid(-(dim_size + 1)),
step=-1,
)
)


@REGISTRY.register(target=[torch.ops.aten.argsort.default])
def _argsort_handler(P: MLXProgramBuilder, n: Node) -> Slot:
"""Handle aten.argsort - indices that sort elements along an axis."""
Expand All @@ -5731,8 +5781,13 @@ def _argsort_handler(P: MLXProgramBuilder, n: Node) -> Slot:
require_kwargs(P.kwargs(n), set(), "aten.argsort")
x = args[0]
dim = args[1] if len(args) > 1 else -1
descending = bool(args[2]) if len(args) > 2 else False

out = P.make_or_get_slot(n)
if descending:
_emit_descending_sort(P, n, x, dim, [(ArgsortNode, out)])
return out

P.emit(
ArgsortNode(
x=P.slot_to_tid(x),
Expand Down
62 changes: 47 additions & 15 deletions backends/mlx/test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8218,32 +8218,35 @@ def create_inputs(self) -> Tuple[torch.Tensor, ...]:


class SortModel(nn.Module):
def __init__(self, dim: int):
def __init__(self, dim: int, descending: bool = False):
super().__init__()
self.dim = dim
self.descending = descending

def forward(self, x: torch.Tensor) -> torch.Tensor:
# Only return sorted values
return torch.sort(x, dim=self.dim)[0]
return torch.sort(x, dim=self.dim, descending=self.descending)[0]


class SortIndicesModel(nn.Module):
def __init__(self, dim: int):
def __init__(self, dim: int, descending: bool = False):
super().__init__()
self.dim = dim
self.descending = descending

def forward(self, x: torch.Tensor) -> torch.Tensor:
# Only return sort indices
return torch.sort(x, dim=self.dim)[1]
return torch.sort(x, dim=self.dim, descending=self.descending)[1]


class SortBothModel(nn.Module):
def __init__(self, dim: int):
def __init__(self, dim: int, descending: bool = False):
super().__init__()
self.dim = dim
self.descending = descending

def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
values, indices = torch.sort(x, dim=self.dim)
values, indices = torch.sort(x, dim=self.dim, descending=self.descending)
return values, indices


Expand All @@ -8258,12 +8261,15 @@ def __init__(
shape: Tuple[int, ...] = (3, 4),
dim: int = -1,
output: str = "values",
descending: bool = False,
):
self.shape = shape
self.dim = dim
self.output = output
self.descending = descending
shape_str = "x".join(str(s) for s in shape)
self.name = f"sort_{output}_dim{dim}_{shape_str}"
desc_str = "_desc" if descending else ""
self.name = f"sort_{output}_dim{dim}{desc_str}_{shape_str}"

@classmethod
def get_test_configs(cls) -> List["SortTest"]:
Expand All @@ -8272,20 +8278,34 @@ def get_test_configs(cls) -> List["SortTest"]:
cls(shape=(3, 4), dim=-1, output="values"),
cls(shape=(3, 4), dim=0, output="indices"),
cls(shape=(2, 3, 4), dim=1, output="both"),
# descending=True: the delegate must reverse the ascending sort.
cls(shape=(8,), dim=0, output="values", descending=True),
cls(shape=(3, 4), dim=-1, output="values", descending=True),
cls(shape=(3, 4), dim=0, output="indices", descending=True),
cls(shape=(2, 3, 4), dim=1, output="both", descending=True),
]

def create_model(self) -> nn.Module:
if self.output == "values":
return SortModel(self.dim)
return SortModel(self.dim, self.descending)
elif self.output == "indices":
return SortIndicesModel(self.dim)
return SortIndicesModel(self.dim, self.descending)
else:
return SortBothModel(self.dim)
return SortBothModel(self.dim, self.descending)

def create_inputs(self) -> Tuple[torch.Tensor, ...]:
return (torch.randn(self.shape),)

def get_expected_node_counts(self) -> Optional[Dict[str, int]]:
if self.descending:
# Each output is an ascending sort/argsort into a tmp slot,
# followed by a step=-1 SliceNode.
if self.output == "values":
return {"SortNode": 1, "SliceNode": 1}
elif self.output == "indices":
return {"ArgsortNode": 1, "SliceNode": 1}
else:
return {"SortNode": 1, "ArgsortNode": 1, "SliceNode": 2}
if self.output == "values":
return {"SortNode": 1, "ArgsortNode": 0}
elif self.output == "indices":
Expand All @@ -8295,12 +8315,13 @@ def get_expected_node_counts(self) -> Optional[Dict[str, int]]:


class ArgsortModel(nn.Module):
def __init__(self, dim: int):
def __init__(self, dim: int, descending: bool = False):
super().__init__()
self.dim = dim
self.descending = descending

def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.argsort(x, dim=self.dim)
return torch.argsort(x, dim=self.dim, descending=self.descending)


@register_test
Expand All @@ -8309,22 +8330,33 @@ class ArgsortTest(OpTestCase):
rtol = 0.0
atol = 0.0

def __init__(self, shape: Tuple[int, ...] = (3, 4), dim: int = -1):
def __init__(
self,
shape: Tuple[int, ...] = (3, 4),
dim: int = -1,
descending: bool = False,
):
self.shape = shape
self.dim = dim
self.descending = descending
shape_str = "x".join(str(s) for s in shape)
self.name = f"argsort_dim{dim}_{shape_str}"
desc_str = "_desc" if descending else ""
self.name = f"argsort_dim{dim}{desc_str}_{shape_str}"

@classmethod
def get_test_configs(cls) -> List["ArgsortTest"]:
return [
cls(shape=(8,), dim=0),
cls(shape=(3, 4), dim=-1),
cls(shape=(3, 4), dim=0),
# descending=True: the delegate must reverse the ascending argsort.
cls(shape=(8,), dim=0, descending=True),
cls(shape=(3, 4), dim=-1, descending=True),
cls(shape=(2, 3, 4), dim=1, descending=True),
]

def create_model(self) -> nn.Module:
return ArgsortModel(self.dim)
return ArgsortModel(self.dim, self.descending)

def create_inputs(self) -> Tuple[torch.Tensor, ...]:
return (torch.randn(self.shape),)
Expand Down
Loading