Skip to content

Commit fa61c2b

Browse files
MLX: add a cummax handler (pytorch#23051)
`aten.cummax` had no handler, and because it sits outside the core ATen opset a graph containing it cannot be lowered at all: the verifier rejects it. Adds the handler, a `CummaxNode` and its interpreter kernel. Only the values output is produced. MLX has no cumulative argmax, so a graph that consumes the indices is rejected rather than silently handed an unfilled tensor. The dead `getitem` that torch.export always emits for the unused half of the tuple does not count as consuming it. `CummaxNode` is appended at the end of the union (137), per the append-only rule in `schema.fbs`, so previously serialized `.pte` still load. Spelled `Cummax` rather than `CumMax` so `generate.py`'s CamelCase-to-SNAKE pass yields `CUMMAX`, matching the neighbouring `CUMSUM`. The partitioner preserves `cummax` through `ops_to_not_decompose`, but exir still runs the core-ATen verifier over the result, so the op has to be excused with `_core_aten_ops_exception_list`. The test does that, and a caller has to do the same. ## Test plan `python -m executorch.backends.mlx.test.run_all_tests cummax`: 4 configs (1D, 2D over each axis, 3D over -1), all passing end to end on an M-series Mac, comparing the delegate's output against PyTorch. ## Scope change since the first version This started as five handlers. Four are dropped: measured against `to_edge`, each op either disappears or becomes something already handled, so the handler was only reachable because registering it made `ops_to_not_decompose` preserve the op. | dropped | what it becomes after `to_edge` | already handled | | --- | --- | --- | | `type_as`, `to.dtype` | `dim_order_ops._to_dim_order_copy` | yes | | `detach` | `aten.alias_copy.default` | yes | | `detach_` | node is folded away | n/a | | `contiguous` | no node even after `torch.export` | n/a | | `masked_fill.Scalar` | `scalar_tensor` + `aten.where.self` | yes, both | `cummax` is the only one that is load-bearing. --------- Co-authored-by: Scott Roy <161522778+metascroy@users.noreply.github.com>
1 parent 575f863 commit fa61c2b

4 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎backends/mlx/ops.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
ConvTranspose3DNode,
7474
CoshNode,
7575
CosNode,
76+
CummaxNode,
7677
CumsumNode,
7778
DequantizeNode,
7879
DivideNode,
@@ -5330,6 +5331,44 @@ def _cumsum_handler(P: MLXProgramBuilder, n: Node) -> Slot:
53305331
return out
53315332

53325333

5334+
@REGISTRY.register(target=[torch.ops.aten.cummax.default])
5335+
def _cummax_handler(P: MLXProgramBuilder, n: Node) -> Slot:
5336+
"""Handle aten.cummax(x, dim) -> (values, indices).
5337+
5338+
Only the values output is produced; MLX has no cumulative argmax, so a
5339+
graph that consumes the indices is rejected rather than silently given an
5340+
unfilled tensor.
5341+
"""
5342+
if 1 in used_getitem_indices(n):
5343+
raise ValueError("aten.cummax indices output (index 1) is not supported")
5344+
5345+
args = P.args(n)
5346+
require_args(args, 2, 2, "aten.cummax")
5347+
require_kwargs(P.kwargs(n), set(), "aten.cummax")
5348+
x = args[0]
5349+
dim = args[1]
5350+
5351+
output_slots = P.make_or_get_slots(n)
5352+
if len(n.args[0].meta["val"].shape) == 0:
5353+
# MLX rejects any axis on a 0-D array; cummax of a scalar is itself.
5354+
P.emit(
5355+
ContiguousNode(
5356+
x=P.slot_to_tid(x),
5357+
out=P.slot_to_tid(output_slots[0]),
5358+
)
5359+
)
5360+
return output_slots
5361+
5362+
P.emit(
5363+
CummaxNode(
5364+
x=P.slot_to_tid(x),
5365+
out=P.slot_to_tid(output_slots[0]),
5366+
axis=dim,
5367+
)
5368+
)
5369+
return output_slots
5370+
5371+
53335372
@REGISTRY.register(target=[torch.ops.aten.stack.default])
53345373
def _stack_handler(P: MLXProgramBuilder, n: Node) -> Slot:
53355374
"""Handle aten.stack - stack tensors along a new axis."""

‎backends/mlx/runtime/MLXInterpreter.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,13 @@ exec_cumsum(const CumsumNode& n, ExecutionState& st, StreamOrDevice s) {
18591859
st.set_tensor(n.out, cumsum(x, n.axis, n.reverse, n.inclusive, s));
18601860
}
18611861

1862+
inline void
1863+
exec_cummax(const CummaxNode& n, ExecutionState& st, StreamOrDevice s) {
1864+
const auto& x = st.const_tensor_ref(n.x);
1865+
st.set_tensor(
1866+
n.out, cummax(x, n.axis, /*reverse=*/false, /*inclusive=*/true, s));
1867+
}
1868+
18621869
inline void
18631870
exec_stack(const StackNode& n, ExecutionState& st, StreamOrDevice s) {
18641871
std::vector<array> tensors;
@@ -2540,6 +2547,9 @@ class Interpreter {
25402547
case OpCode::CUMSUM:
25412548
ops::exec_cumsum(std::get<CumsumNode>(instr.node), st, s);
25422549
break;
2550+
case OpCode::CUMMAX:
2551+
ops::exec_cummax(std::get<CummaxNode>(instr.node), st, s);
2552+
break;
25432553
case OpCode::STACK:
25442554
ops::exec_stack(std::get<StackNode>(instr.node), st, s);
25452555
break;

‎backends/mlx/serialization/schema.fbs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ table CumsumNode {
646646
inclusive: bool = true;
647647
}
648648

649+
table CummaxNode {
650+
x: Tid (required);
651+
out: Tid (required);
652+
axis: int32;
653+
}
654+
649655
table StackNode {
650656
tensors: [Tid] (required);
651657
out: Tid (required);
@@ -1204,7 +1210,8 @@ union OpNode {
12041210
RandomBitsNode,
12051211
UpdateAndAttendNode,
12061212
TruncNode,
1207-
FlipNode
1213+
FlipNode,
1214+
CummaxNode
12081215
// BC: Add new op nodes here (append only)
12091216
}
12101217

‎backends/mlx/test/test_ops.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7601,6 +7601,53 @@ def create_inputs(self) -> Tuple[torch.Tensor, ...]:
76017601
return (torch.randn(self.shape),)
76027602

76037603

7604+
class CummaxModel(nn.Module):
7605+
def __init__(self, dim: int):
7606+
super().__init__()
7607+
self.dim = dim
7608+
7609+
def forward(self, x: torch.Tensor) -> torch.Tensor:
7610+
return torch.cummax(x, dim=self.dim).values
7611+
7612+
7613+
@register_test
7614+
class CummaxTest(OpTestCase):
7615+
name = "cummax"
7616+
rtol = 1e-5
7617+
atol = 1e-5
7618+
7619+
def __init__(self, shape: Tuple[int, ...] = (3, 4), dim: int = 0):
7620+
self.shape = shape
7621+
self.dim = dim
7622+
shape_str = "x".join(str(s) for s in shape) or "scalar"
7623+
self.name = f"cummax_dim{dim}_{shape_str}"
7624+
7625+
@classmethod
7626+
def get_test_configs(cls) -> List["CummaxTest"]:
7627+
return [
7628+
cls(shape=(8,), dim=0),
7629+
cls(shape=(3, 4), dim=0),
7630+
cls(shape=(3, 4), dim=1),
7631+
cls(shape=(2, 3, 4), dim=-1),
7632+
cls(shape=(), dim=0),
7633+
cls(shape=(), dim=-1),
7634+
]
7635+
7636+
def get_edge_compile_config(self) -> Optional[exir.EdgeCompileConfig]:
7637+
# cummax is not in the core ATen opset. The partitioner preserves it
7638+
# through ops_to_not_decompose, but exir still runs the core-ATen
7639+
# verifier over the result, so the op has to be excused explicitly.
7640+
return exir.EdgeCompileConfig(
7641+
_core_aten_ops_exception_list=[torch.ops.aten.cummax.default]
7642+
)
7643+
7644+
def create_model(self) -> nn.Module:
7645+
return CummaxModel(self.dim)
7646+
7647+
def create_inputs(self) -> Tuple[torch.Tensor, ...]:
7648+
return (torch.randn(self.shape),)
7649+
7650+
76047651
class StackModel(nn.Module):
76057652
def __init__(self, dim: int = 0, n: int = 3):
76067653
super().__init__()

0 commit comments

Comments
 (0)