Skip to content

[BUG] Metal corrupts mx.tril output when co-evaluated with a transposed cumsum graph #4419

Closed

Description

@TheDarkchip

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

Hi, during some work on GDN kernels it appears I have found some strange behavior.

Describe the bug
On Metal, joint lazy evaluation corrupts an independent mx.tril result.

Observe the output from the reproduction script:

CPU row sums: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
CPU PASS
Metal row sums: [0.0, 0.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
AssertionError

To Reproduce

import mlx.core as mx


def run(device):
    mx.set_default_device(device)
    x = mx.array([1.0] * 72).reshape(2, 9, 4).transpose(0, 2, 1)
    x = mx.cumsum(x, axis=-1)

    causal = mx.tril(mx.ones((9, 9)))
    relative = x[..., :, None] - x[..., None, :]
    mx.eval(relative, causal)

    return causal.tolist()


def row_sums(x):
    return [sum(row) for row in x]


expected = [[float(column <= row) for column in range(9)] for row in range(9)]

# Run Metal first so the CPU control cannot affect the failing graph.
metal = run(mx.gpu)
cpu = run(mx.cpu)

print("CPU row sums:", row_sums(cpu))
assert cpu == expected
print("CPU PASS")

print("Metal row sums:", row_sums(metal))
assert metal == expected
print("Metal PASS")

Expected behavior
CPU and Metal should both return the lower-triangular matrix, with row sums:

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

Desktop (please complete the following information):

  • MLX: 0.32.2
  • MLX Metal: 0.32.2
  • Python: 3.13.12
  • macOS: 26.5.2 (25F84)
  • Hardware: Apple M2 Pro, 32 GB RAM, 16-core GPU
  • Metal: Metal 4
  • Architecture: applegpu_g14s
  • Also reproduced from clean source commit: 99e45f7

Additional context
Materializing causal early with mx.eval(causal) prevents the corruption. I was able to reproduce the failure in five out of five fresh Metal processes.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions