apple / apple/coremltools

torch.where behaves strangely with CoreML via ExecuTorch

Open
#2,544 2 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

When a model has torch.where(x, min, max), the min and max needs to be registered within the model for it to work. Otherwise, the model output is wrong on iOS. When inferencing on iOS with CoreML via ExecuTorch, model produces non-meaningful results compared to python inference.

This won't work:
```
class Model(nn.Module):
def __init__(self, ):
super(Model, self).__init__()
self.conv = nn.Conv2d(3, 3, 3, 1, 1, bias=True)

def forward(self, x):
x = self.conv(x)
x = torch.where(x > 0.5, 0.0, 1.0)
return x

```

This will work:

```
class Model(nn.Module):
def __init__(self, ):
super(Model, self).__init__()
self.conv = nn.Conv2d(3, 3, 3, 1, 1, bias=True)

def forward(self, x):
x = self.conv(x)
x = torch.where(x > 0.5, self.lower, self.upper)
return x

model = Model()
upper = torch.tensor(1.0).to(float_dtype)
lower = torch.tensor(0.0).to(float_dtype)
model.register_buffer("upper", upper)
model.register_buffer("lower", lower)
```

Contributor guide

Open the contributing guide

Research direction

Start with the provided torch.where reproduction and compare its Python output with the iOS CoreML output through ExecuTorch. Trace how scalar min and max operands are represented during conversion, and consider the issue done when the reproduction produces matching, meaningful outputs without requiring registered buffers.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, python, pytorch
Domain
machine-learning, mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.