apple / apple/coremltools

Convering a model with some stride values never finnishes

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

Description

## 🐞Describing the bug
Hi I noticed that for some `stride` values on a PyTorch `Conv2d` layer conversion takes much longer or never finishes at all. I made a table with my results from trying some values twice.

| Stride | Try # 1 | Try # 2 |
|-----------|------------|------------|
| stride=1 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=2 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=3 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=4 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=5 | 🟥 NOK (Timeout 5min) | 🟥 NOK (Timeout 5min) |
| stride=6 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=7 | 🟥 NOK (Timeout 5min) | 🟥 NOK (Timeout 5min) |
| stride=8 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=9 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=10 | 🟨 OK (30s) | 🟨 OK (30s)
| stride=11 | 🟨 OK (30s) | 🟨 OK (30s)
| stride=12 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=13 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=14 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=15 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=16 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=17 | 🟩 OK (<10s) | 🟩 OK (<10s) |
| stride=18 | 🟩 OK (<10s) | 🟩 OK (<10s) |

## To Reproduce
My test code:
```python
import torch
import numpy as np
import coremltools

class DummyModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv = torch.nn.Conv2d(
in_channels=3,
out_channels=3,
kernel_size=1,
stride=stride,
)

def forward(self, x):
return self.conv(x)

with torch.no_grad():
model = DummyModel()
model.eval()
image = torch.randn((1, 3, 1920, 1080))
pytorch_output = model(image)
traced_model = torch.jit.trace(model, example_inputs=[image])

coreml_model = coremltools.converters.convert(
model=traced_model,
inputs=[coremltools.TensorType(name="image", shape=image.shape, dtype=np.float16)],
outputs=[coremltools.TensorType(name="output", dtype=np.float16)],
minimum_deployment_target=coremltools.target.iOS18,
compute_precision=coremltools.precision.FLOAT16,
compute_units=coremltools.ComputeUnit.ALL,
)

print("Comparing")
coreml_output = coreml_model.predict({"image": image.half().numpy()})["output"]
assert np.allclose(pytorch_output.numpy(), coreml_output, atol=5e-3)
print("Done")
```

## System environment (please complete the following information):
- coremltools version: 8.2
- OS: MacOS 15.2
- PyTorch version: 2.5.0

Contributor guide

Open the contributing guide

Research direction

Use the supplied PyTorch Conv2d script as the reproduction entry point, first testing stride=5 and stride=7 under the stated coremltools 8.2, macOS 15.2, and PyTorch 2.5.0 environment. Compare conversion behavior with the neighboring stride values and inspect the conversion path reached by those cases. Done means conversion completes reliably without the five-minute timeout while preserving the reported model output comparison.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, 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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.