Skip to content

Guard the MLX convolution's rank at compile time #135

Open

Description

@jessegrabowski

dispatch/mlx/pooling.py guards the rank it can handle and raises at compile time with a message naming the backend. dispatch/mlx/conv.py has no such guard, so a rank-4 convolution compiles cleanly and fails inside MLX on the first call, with a message that names neither the layer nor the backend policy. Every other NotImplementedError in the dispatch tree fires during pytensor.function(); this is the only one that waits until data loading, initialization and mx.compile are all behind you.

import numpy as np
import pytensor
import pytensor.tensor as pt

from pytensor_ml.layers.conv import ConvLayer

X = pt.tensor("X", shape=(1, 4, 4, 4, 4, 2))
W = pt.tensor("W", shape=(2,) * 6)
f = pytensor.function([X, W], ConvLayer((2,) * 4, (1,) * 4, (1,) * 4)(X, W), mode="MLX")
# compiles fine

f(np.zeros((1, 4, 4, 4, 4, 2)), np.zeros((2,) * 6))
# ValueError: [conv] Only works for inputs with 1-3 spatial dimensions.

One rank check mirroring the one already in the sibling pooling module turns this into a compile-time error like every other backend hole.

Activity

  1. changed the title [-]An MLX convolution over more than three spatial axes compiles and then dies on the first batch[/-] [+]Guard the MLX convolution's rank at compile time[/+] on Sep 1, 2026
  2. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Sep 1, 2026
  3. jessegrabowski commented on Sep 1, 2026

    @jessegrabowski
    MemberAuthor

    Nobody's writing a four-dimensional convolution, so this isn't the bug it's filed as. What's worth doing is giving conv the compile-time rank guard dispatch/mlx/pooling.py already has.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpytensorRelating to the pytensor backend

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions