apple / apple/coremltools

random data is generated when output dtype=np.float16?

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

Description

If module returns a 2D float16 Tensor with **1st dimension size > 5** and **2nd dimension size > 12** and **2nd dimension size is not multiple of 32**, it returns some random data from MLModel.predict. Do I need to set some other options to make output conversion correct?

example script to reproduce it
```python
import torch
import numpy as np
import coremltools as ct

class MyModule(torch.nn.Module):
def __init__(self):
super().__init__()

def forward(self, x: torch.Tensor):
return torch.ones(5, 13)

module = MyModule()
module.eval()

x = torch.ones((1), dtype=torch.float16)

traced_module = torch.jit.trace(module, [x])

mlmodel = ct.convert(
traced_module,
convert_to="mlprogram",
inputs=[ct.TensorType(name="x", shape=x.shape, dtype=np.float16)],
outputs=[ct.TensorType(name="output", dtype=np.float16)],
compute_units=ct.ComputeUnit.ALL,
compute_precision=ct.precision.FLOAT16,
minimum_deployment_target=ct.target.macOS13,
)

torch_output = traced_module(x)
print("torch output:", torch_output)

coreml_output = torch.from_numpy(
mlmodel.predict({'x': x})['output']
)
print(f"coreml output:", coreml_output)

diff = torch.abs(torch_output - coreml_output)
print("diff avg:", torch.mean(diff), ", diff max:",torch.max(diff))
```

example result from console. The result is different in each run.
```
Converting PyTorch Frontend ==> MIL Ops: 88%|███████████████████████▋ | 7/8 [00:00<00:00, 6273.53 ops/s]
Running MIL frontend_pytorch pipeline: 100%|█████████████████████████| 5/5 [00:00<00:00, 17549.39 passes/s]
Running MIL default pipeline: 100%|████████████████████████████████| 64/64 [00:00<00:00, 11410.16 passes/s]
Running MIL backend_mlprogram pipeline: 100%|██████████████████████| 11/11 [00:00<00:00, 67948.96 passes/s]
torch output: tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
coreml output: tensor([[1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00,
1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00,
1.0000e+00],
[1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00,
1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00,
1.0000e+00],
[1.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 6.8528e-36, 1.4013e-45,
1.0693e-09, 1.4013e-45, 4.2039e-45, 0.0000e+00, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[9.0943e-10, 1.4013e-45, 0.0000e+00, 0.0000e+00, 1.0711e-09, 1.4013e-45,
0.0000e+00, 0.0000e+00, 0.0000e+00, 1.4013e-45, 0.0000e+00, 0.0000e+00,
2.5644e-43],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00,
0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00,
0.0000e+00]])
diff avg: tensor(0.5846) , diff max: tensor(1.)
```
---
testing environment:
coremltools 7.0b1
torch 2.0.1
numpy 1.25.0
macOS Ventura 13.4 on Macbook M1 Air

Contributor guide

Open the contributing guide

Research direction

Start by running the provided conversion script and comparing traced_module(x) with mlmodel.predict({'x': x})['output'] for the reported float16 shapes. Trace the conversion and prediction path responsible for the output buffer; done means the converted model consistently returns the expected all-ones 5×13 tensor without random values.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.