apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.44k stars 643 forks source link

Numpy 2.x compatibility issue w `issubclass` #2304

Closed vade closed 2 months ago

vade commented 3 months ago

🐞Describing the bug

CoreMLTools 8.0 b1 is incompatible with a subtle change in Numpy 2.x

Stack Trace

AttributeError: np.issubclass_ was removed in the NumPy 2.0 release. Use issubclass builtin instead.

  _check_trace(
Tuple detected at graph output. This will be flattened in the converted model.
Traceback (most recent call last):
  File "/Users/vade/Documents/Repositories/SyphonDeepFace/GFPGAN/gfpgan_convert.py", line 32, in <module>
    model_ml_program = model = ct.convert(traced_model,
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/_converters_entry.py", line 635, in convert
    mlmodel = mil_convert(
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 188, in mil_convert
    return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 212, in _mil_convert
    proto, mil_program = mil_convert_to_proto(
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 288, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 108, in __call__
    return load(*args, **kwargs)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 84, in load
    return _perform_torch_convert(converter, debug)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 118, in _perform_torch_convert
    prog = converter.convert()
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 1162, in convert
    input_var = mb.cast(x=input_var, dtype="fp32")
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/ops/registry.py", line 183, in add_op
    return cls._add_op(op_cls_to_add, **kwargs)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/builder.py", line 173, in _add_op
    kwargs.update(cls._create_vars(
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/builder.py", line 156, in _create_vars
    var = cls._add_const(val, new_var_name, before_op)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/builder.py", line 96, in _add_const
    output_var = cls.const(val=val, name=const_name,
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/ops/registry.py", line 183, in add_op
    return cls._add_op(op_cls_to_add, **kwargs)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/builder.py", line 202, in _add_op
    new_op.type_value_inference()
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/operation.py", line 257, in type_value_inference
    output_types = self.type_inference()
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/ops/defs/iOS15/control_flow.py", line 171, in type_inference
    builtin_type, _ = self._get_type_val(self.val.val)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/ops/defs/iOS15/control_flow.py", line 221, in _get_type_val
    _, builtin_type = numpy_val_to_builtin_val(value)
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/types/type_mapping.py", line 496, in numpy_val_to_builtin_val
    ret_type = type_to_builtin_type(type(npval))
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/coremltools/converters/mil/mil/types/type_mapping.py", line 480, in type_to_builtin_type
    if np.issubclass_(type, bool):
  File "/Users/vade/miniforge3/envs/coremltools/lib/python3.10/site-packages/numpy/__init__.py", line 397, in __getattr__
    raise AttributeError(
AttributeError: `np.issubclass_` was removed in the NumPy 2.0 release. Use `issubclass` builtin instead.

To Reproduce

Install numpy 2.x Attempt any model conversion.

System environment (please complete the following information):

Additional context

DawerG commented 3 months ago

We are looking into adding the support for numpy 2.0. Until then, please try to build from the source which pins the version of numpy to be less than 2.0.

jakesabathia2 commented 3 months ago

This issue should be fixed in 8.0b2, @vade please give it a try :)

DawerG commented 2 months ago

Since the issue is resolved, let me close the issue.

@vade Please try with the latest coremltools, pip install coremltools==8.0b2

If the issue still persists, please feel free to re-open the issue.