Skip to content

[BUG] [CPU] mx.compile(shapeless=True): crash in Reduce::eval_cpu when reduced axes have size 1 #5019

Backlog

Description

@archidoge0

☑️ I understand it is strictly prohibited to use AI to write issues.

Describe the bug
During the investigation of the CPU CI failure in #5010, I discovered that running a reduction operator under mx.compile(..., shapeless=True) (or the built-in mlx.nn.log_softmax with shapeless=True) under mx.cpu would crash when all the reduced axes had a size of 1.

Both metal/reduce.cpp and cuda/reduce.cu have already performed copy_gpu early termination for in.size() > 0 && out.size() == in.size(), but Reduce::eval_cpu in mlx/backend/cpu/reduce.cpp missed this branch, that's the root cause of the crash.

To Reproduce

import mlx.core as mx

with mx.stream(mx.cpu):
    fn = mx.compile(lambda x: x.sum(axis=0), shapeless=True)
    mx.eval(fn(mx.ones((4, 8))))
    y = fn(mx.ones((1, 8)))
    mx.eval(y)
    print(y)
    # Note: mlx.nn.log_softmax(mx.array([[1.5], [-2.0]]), axis=-1) also crashes directly on mx.cpu

Expected behavior
It doesn't crash; it returns an all-ones array of shape (8,) just like mx.gpu.

Desktop (please complete the following information):

  • OS Version: macOS 15.8.3 (arm64, Apple M3 Max)
  • Version: 0.32.5 (also reproduced on main @ b343960)

Additional context
Credit given to UCSB PLSE.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions