apple / apple/coremltools

torch.nn.ReplicationPad3d fails at runtime when there is padding

Open
#2,571 3 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
torch.nn.ReplicationPad3d fails at runtime when there is padding, but ideally it would fail ahead of time.

## To Reproduce
```

import torch

class Model(torch.nn.Module):
def __init__(self):
super().__init__()
self.pad = torch.nn.ReplicationPad3d(padding=2)

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

model = Model()
inputs = (
torch.randn(1, 6, 6, 6, 6),
)

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

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

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)
```

This fails at runtime with the following error, but ideally it would fail ahead of time during mlmodel creation:

```
/opt/miniconda3/envs/op-et/lib/python3.10/site-packages/coremltools/models/model.py:560: RuntimeWarning: You will not be able to run predict() on this Core ML model. Underlying exception message was: Error compiling model: "Failed to parse the model specification. Error: Unable to parse ML Program: in operation pad_cast_fp16: Padding for more than two dimensions only supports `constant` mode".
_warnings.warn(
Traceback (most recent call last):
File "/Users/scroy/Desktop/executorch/test.py", line 158, in
out = mlmodel.predict(predict_inputs)
File "/opt/miniconda3/envs/op-et/lib/python3.10/site-packages/coremltools/models/model.py", line 804, in predict
raise self._framework_error
File "/opt/miniconda3/envs/op-et/lib/python3.10/site-packages/coremltools/models/model.py", line 549, in _get_proxy_and_spec
_MLModelProxy(
RuntimeError: Error compiling model: "Failed to parse the model specification. Error: Unable to parse ML Program: in operation pad_cast_fp16: Padding for more than two dimensions only supports `constant` mode".
```

## 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 with the provided torch.nn.ReplicationPad3d reproduction and follow the ep.run_decompositions({}) to ct.convert(ep) path. Inspect how this padding operation is validated during conversion; done means unsupported non-constant padding for more than two dimensions is rejected during model creation rather than only when predict() runs.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.