coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

NumPy 2.4.0 breaks CoreML export with TypeError: only 0-dimensional arrays can be converted to Python scalars

Open onuralpszr opened this issue 11 months ago • 3 comments

Hello; 👋

After upgrading to NumPy 2.4.0, CoreML export fails consistently with a TypeError during PyTorch → MIL conversion. The issue appears during constant casting in the Torch frontend (_int op), where NumPy arrays are no longer implicitly convertible to Python scalars.

Downgrading NumPy (e.g. to <2.4.0) resolves the issue and restores successful export.

Action URL: https://github.com/ultralytics/ultralytics/actions/runs/20410420128/job/58646364488

requirements: AutoUpdate success ✅ 3.1s
WARNING ⚠️ requirements: Restart runtime or rerun command for updates to take effect

scikit-learn version 1.7.1 is not supported. Minimum required version: 0.17. Maximum required version: 1.5.1. Disabling scikit-learn conversion API.
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/keras/src/export/tf2onnx_lib.py:8: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar.
  if not hasattr(np, "object"):
TensorFlow version 2.19.0 has not been tested with coremltools. You may run into unexpected errors. TensorFlow 2.12.0 is the most recent version that has been tested.
Torch version 2.9.1 has not been tested with coremltools. You may run into unexpected errors. Torch 2.7.0 is the most recent version that has been tested.
Skipping import of cpp extensions due to incompatible torch version 2.9.1 for torchao version 0.14.0         Please see GitHub issue #2919 for more info
W1221 13:23:05.377000 25817 site-packages/torch/distributed/elastic/multiprocessing/redirects.py:29] NOTE: Redirects are currently not supported in Windows or MacOs.

CoreML: starting export with coremltools 9.0...

Converting PyTorch Frontend ==> MIL Ops:   0%|          | 0/362 [00:00<?, ? ops/s]
Converting PyTorch Frontend ==> MIL Ops:  52%|█████▏    | 187/362 [00:00<00:00, 1867.87 ops/s]
ERROR ❌ CoreML: export failure 23.2s: only 0-dimensional arrays can be converted to Python scalars

ERROR - converting 'int' op (located at: '9/m/0/attn/463'):


Converting PyTorch Frontend ==> MIL Ops:  80%|███████▉  | 289/362 [00:00<00:00, 2299.87 ops/s]
Traceback (most recent call last):
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/utils/benchmarks.py", line 169, in benchmark
    filename = model.export(
               ^^^^^^^^^^^^^
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/engine/model.py", line 709, in export
    return Exporter(overrides=args, _callbacks=self.callbacks)(model=self.model)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/engine/exporter.py", line 570, in __call__
    f[4] = self.export_coreml()
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/engine/exporter.py", line 248, in outer_func
    raise e
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/engine/exporter.py", line 240, in outer_func
    f = inner_func(*args, **kwargs)  # exported file/dir or tuple of (file/dir, *)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/engine/exporter.py", line 955, in export_coreml
    ct_model = ct.convert(
               ^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/_converters_entry.py", line 646, in convert
    mlmodel = mil_convert(
              ^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 186, in mil_convert
    return _mil_convert(
           ^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 218, in _mil_convert
    proto, mil_program = mil_convert_to_proto(
                         ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 294, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/converter.py", line 106, in __call__
    return load(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 87, in load
    return _perform_torch_convert(converter, debug)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 150, in _perform_torch_convert
    prog = converter.convert()
           ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 1392, in convert
    convert_nodes(self.context, self.graph, early_exit=not has_states)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 118, in convert_nodes
    raise e     # re-raise exception
    ^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 113, in convert_nodes
    convert_single_node(context, node)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 174, in convert_single_node
    add_op(context, node)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 3066, in _int
    _cast(context, node, int, "int32")
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 3048, in _cast
    res = mb.const(val=dtype(x.val), name=node.name)
                       ^^^^^^^^^^^^
TypeError: only 0-dimensional arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/cfg/__init__.py", line 1026, in <module>
    entrypoint(debug="")
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/cfg/__init__.py", line 985, in entrypoint
    getattr(model, mode)(**overrides)  # default args from model
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/engine/model.py", line 655, in benchmark
    return benchmark(
           ^^^^^^^^^^
  File "/Users/runner/work/ultralytics/ultralytics/ultralytics/utils/benchmarks.py", line 202, in benchmark
    assert type(e) is AssertionError, f"Benchmark failure for {name}: {e}"
           ^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Benchmark failure for CoreML: only 0-dimensional arrays can be converted to Python scalars
Error: Process completed with exit code 1.

To Reproduce

Force install numpy latest and do export via cli or python code like in here

from ultralytics import YOLO

# Load the YOLO11 model
model = YOLO("yolo11n.pt")

# Export the model to CoreML format
model.export(format="coreml")  # creates 'yolo11n.mlpackage'

# Load the exported CoreML model
coreml_model = YOLO("yolo11n.mlpackage")

# Run inference
results = coreml_model("https://ultralytics.com/images/bus.jpg")

System environment (please complete the following information):

  • coremltools version: 9.0
  • OS (e.g. MacOS version or Linux type): macos tahoe 26.1
  • Any other relevant version information (e.g. PyTorch or TensorFlow version): torch 2.9.1

Additional context

  • Add anything else about the problem here that you want to share.

onuralpszr avatar Dec 21 '25 13:12 onuralpszr

Can you give us a minimal standalone example, which doesn't require installing third party software?

TobyRoseman avatar Dec 22 '25 17:12 TobyRoseman

I was able to reproduce this consistently and the root cause appears to be a NumPy ≥ 2.4.0 behaviour change.

During Torch → MIL conversion, the _int op goes through _cast() in coremltools/converters/mil/frontend/torch/ops.py, which currently does: mb.const(val=dtype(x.val), name=node.name)

With NumPy ≥ 2.4.0, this fails when x.val is a NumPy array with ndim > 0, since NumPy no longer allows implicit array → scalar conversion and raises: TypeError: only 0-dimensional arrays can be converted to Python scalars

Downgrading NumPy (<2.4.0) avoids the issue, which suggests this is not model-specific. A safe fix seems to be explicitly extracting a Python scalar from x.val before casting (e.g. using .item() for 0-D arrays or flattening first), which should be compatible with both older and newer NumPy versions.

Happy to open a PR if this approach makes sense.

Arjav1512 avatar Jan 02 '26 12:01 Arjav1512

@Arjav1512 - the issue should be fixed by #2632.

TobyRoseman avatar Jan 02 '26 18:01 TobyRoseman