apple / apple/coremltools

Value inference errors out while creating scalars

Open
#1,811 0 comments 0 reactions 0 assignees View on GitHub
bug triaged
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## 🐞Describing the bug
- coremltools value inference sometime (when output is scalar) creates builtin types (float) and propagates the same.
- When creating op e.g. `squeeze`, it errors out expecting rank-0 tensor
```
ValueError: Types should have zero-rank ndarray input, got 0.7357164025306702 instead.
```

## Stack Trace
```
File "~/repro.py", line 25, in
def prog1(x):
File "/lib/python3.9/site-packages/coremltools/converters/mil/mil/builder.py", line 237, in wrapper
outputs = main_block(*input_vars)
File "~/repro.py", line 28, in prog1
y = mb.squeeze(x=y)
File "/lib/python3.9/site-packages/coremltools/converters/mil/mil/ops/registry.py", line 176, in add_op
return cls._add_op(op_cls_to_add, **kwargs)
File "/lib/python3.9/site-packages/coremltools/converters/mil/mil/builder.py", line 182, in _add_op
new_op.type_value_inference()
File "/lib/python3.9/site-packages/coremltools/converters/mil/mil/operation.py", line 256, in type_value_inference
output_vals = self._auto_val(output_types)
File "/lib/python3.9/site-packages/coremltools/converters/mil/mil/operation.py", line 403, in _auto_val
builtin_val.val = v
File "/lib/python3.9/site-packages/coremltools/converters/mil/mil/types/type_double.py", line 42, in val
raise ValueError(
ValueError: Types should have zero-rank ndarray input, got 0.7357164025306702 instead.
```

## To Reproduce
- Please add a minimal code example that can reproduce the error when running it.
```
import coremltools as ct
from coremltools.converters.mil import Builder as mb
import numpy as np

input_shape = (1,)
input_data = { 'x' : np.random.randn(*input_shape) }

def test_program(prog):
print(prog)
model = ct.convert(prog, source="milinternal", convert_to="mlprogram")

output = model.predict(input_data)
print(output)

t1 = np.random.rand(1)
@mb.program(input_specs=[mb.TensorSpec(input_shape)])
def prog1(x):
y = mb.concat(values=[t1, x], axis=0)
y, _ = mb.split(x=y, num_splits=2, axis=0)
y = mb.squeeze(x=y)
return y

test_program(prog1)
```
- If the model conversion succeeds, but there is a numerical mismatch in predictions, please include the code used for comparisons.

## System environment (please complete the following information):
- coremltools version: 6.2
- OS (e.g. MacOS version or Linux type): macOS 12.6 (21G115)
- Any other relevant version information (e.g. PyTorch or TensorFlow version): n/a

Contributor guide

Open the contributing guide

Research direction

Start with the value-inference path in converters/mil/mil/operation.py and the scalar validation in converters/mil/mil/types/type_double.py; the stack trace also identifies builder.py and the squeeze operation as entry points. Run the supplied program with coremltools 6.2 and confirm that scalar outputs are handled as rank-0 tensors and the squeeze conversion completes without the ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.