Skip to content

cumsum with exclusive=True crashes when constant-folded (TypeError: Cannot construct a dtype from an array) #2759

Closed

Description

@winklemad

Description

Constant-folding a cumsum op with exclusive=True crashes at graph-construction time.

Reproduction

from coremltools.converters.mil.mil import Builder as mb
import numpy as np

@mb.program(input_specs=[])
def prog():
    x = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
    return mb.cumsum(x=x, axis=0, exclusive=True)
File ".../mil/ops/defs/iOS15/tensor_operation.py", line 158, in value_inference
    data = np.concatenate((np.zeros(zero_shape, data)), axis=axis)
TypeError: Cannot construct a dtype from an array

Cause

In cumsum.value_inference, np.zeros(zero_shape, data) passes the data ndarray as np.zeros' dtype argument, and (np.zeros(...)) is a single array rather than a tuple for np.concatenate. So any exclusive=True cumsum on a constant-foldable input aborts conversion — this is reachable from real frontends (e.g. tf.cumsum(..., exclusive=True) on a foldable tensor). Even setting the crash aside, the exclusive prefix sum is never computed: data (the inclusive cumsum) is returned without being shifted.

Expected

Exclusive cumsum of [1, 2, 3, 4] is [0, 1, 3, 6] (out[0] = 0, out[i] = sum(x[:i])).

I'll open a PR that mirrors the exclusive semantics and adds value-inference coverage across axis/reverse combinations.

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