apple / apple/coremltools

CoreML BatchNorm3d crashes process

Open
#2,572 1 comment 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
CoreML BatchNorm3d crashes CoreML process

## To Reproduce
```
import torch

class Model(torch.nn.Module):
def __init__(self):
super().__init__()
self.norm = torch.nn.BatchNorm3d(3)

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

model = Model()
inputs = (
torch.randn(1, 3, 4, 4, 4),
)

eager_outputs = model(*inputs)
print(f"Eager: {eager_outputs.shape} {eager_outputs}")

ep = torch.export.export(model.eval(), inputs)

import coremltools as ct
import numpy as np
ep = ep.run_decompositions({})

mlmodel = ct.convert(ep)

coreml_inputs = mlmodel.get_spec().description.input
coreml_outputs = mlmodel.get_spec().description.output
predict_inputs = {str(ct_in.name): pt_in.detach().cpu().numpy().astype(np.int32) for ct_in, pt_in in zip(coreml_inputs, inputs)}
out = mlmodel.predict(predict_inputs)

print("CoremL", out)
```

Output is:
```
loc("tensor _native_batch_norm_legit_no_training_cast_fp16 = batch_norm(beta = tensor([0, 0, 0]), epsilon = fp16(1.00135803e-05), gamma = tensor([1, 1, 1]), mean = tensor([-0.012588501, 0.0046005249, 0.016494751]), variance = tensor([1.00292969, 1.00195312, 1.01855469]), x = x_to_fp16)[milId = uint64(1), name = string(\22_native_batch_norm_legit_no_training_cast_fp16\22)]; - /private/var/folders/lw/phxpy6k10ll388xs18hyq1cr0000gn/T/tmp63_bi7tb.mlmodelc/model.mil":12:12): error: output type 'tensor<1x3x4x4x4xf16>' and mean type 'tensor<1x0x1x1x601354336xf16>' are not broadcast compatible
LLVM ERROR: Failed to infer result type(s).
zsh: abort python test.py
/opt/miniconda3/envs/op-et/lib/python3.10/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '
```

## System environment (please complete the following information):
- coremltools version: 8.3
- OS (e.g. MacOS version or Linux type): macOS15

Contributor guide

Open the contributing guide

Research direction

Start by running the provided BatchNorm3d reproduction through torch.export.export, run_decompositions({}), and ct.convert on macOS with coremltools 8.3. Inspect the conversion path for the generated batch_norm operation and its incompatible broadcast shape. Done means conversion and prediction complete without crashing and produce the expected 1x3x4x4x4 output.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.