apple / apple/coremltools

Concatenation support PyTorch -> CoreML

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

Description

## 🐞Describe the bug
Unexpected behavior with 0 size tensors concatenation from PyTorch

## To Reproduce
- If a python script can reproduce the error, please paste the code snippet
```
x = torch.rand((0, 100, 120))
y = torch.rand((0, 100, 100))
torch.cat([x, y], dim=2)
Out: tensor([], size=(0, 100, 220))
```
```
@mb.program(input_specs=[mb.TensorSpec(shape=(0, 100, 100)), mb.TensorSpec(shape=(0, 100, 120))])
def prog(x, y):
# MIL operation takes named inputs (instead of positional inputs).
# Here `name` argument is optional.
return mb.concat(values = (x, y), axis=2)

model = ct.convert(prog)

# Make a prediction with CoreML
prediction = model.predict({
'x': np.random.rand(0, 100, 100).astype(np.float32),
'y': np.random.rand(0, 100, 120).astype(np.float32)
})

prediction
```

Running MIL optimization passes: 100%|██████████| 17/17 [00:00<00:00, 7773.16 passes/s]
Translating MIL ==> MLModel Ops: 100%|██████████| 3/3 [00:00<00:00, 6594.82 ops/s]
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in
10 prediction = model.predict({
11 'x': np.random.rand(0, 100, 100).astype(np.float32),
---> 12 'y': np.random.rand(0, 100, 120).astype(np.float32)
13 })
14

~/.pyenv/versions/3.7.0/envs/twilio-test/lib/python3.7/site-packages/coremltools/models/model.py in predict(self, data, useCPUOnly, **kwargs)
365 else:
366 if self._framework_error:
--> 367 raise self._framework_error
368 else:
369 raise Exception(

~/.pyenv/versions/3.7.0/envs/twilio-test/lib/python3.7/site-packages/coremltools/models/model.py in _get_proxy_and_spec(filename, use_cpu_only)
111
112 try:
--> 113 return (_MLModelProxy(filename, use_cpu_only), specification, None)
114 except RuntimeError as e:
115 _warnings.warn(

RuntimeError: Error compiling model: "compiler error: Blob with zero size found: x".

## System environment (please complete the following information):
- coremltools version '4.0b4':
- torch version 1.6.0

Contributor guide

Open the contributing guide

Research direction

Start by running the provided PyTorch and coremltools reproduction through mb.concat, ct.convert, and model.predict. Trace where the zero-size tensor is rejected during model compilation, then verify that concatenating the empty inputs produces the expected shape without the zero-size blob error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.