Casting float32 to int32 and back - PyTorch, coremltools 6.0b2
- Dominant language
- Python
- Stars
- 5.4k
- Forks
- 850
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 10
Description
I need to calculate fractional part of tensor, like `torch.frac()`, an operation that is not implemented in the converter. So I've made this simple model:
```
import torch
class Frac(torch.nn.Module):
def forward(self, x):
x_int = x.to(torch.int32)
x_frac = x - x_int
return x_frac
```
It seems to work well on torch side:
```
frac = Frac().eval()
torch.manual_seed(0)
x = 10 * torch.rand((5, 5)) - 5
frac_traced = torch.jit.trace(frac, x, check_trace=True)
y = frac_traced(x)
```
showing the content of x and y, respectively:
```
tensor([[-0.0374, 2.6822, -4.1152, -3.6797, -1.9258],
[ 1.3408, -0.0991, 3.9644, -0.4437, 1.3231],
[-1.5111, -0.9828, -4.7767, -3.3114, -2.0611],
[ 0.1852, 1.9767, 3.0001, -3.3897, -2.1773],
[ 1.8161, 4.1519, -1.0290, 3.7416, -0.8059]])
tensor([[ -0.0374, 0.6822, -0.1152, -0.6797, -0.9258],
[ 0.3408, -0.0991, 0.9644, -0.4437, 0.3231],
[ -0.5111, -0.9828, -0.7767, -0.3114, -0.0611],
[ 0.1852, 0.9767, 0.0001, -0.3897, -0.1773],
[ 0.8161, 0.1519, -0.0290, 0.7416, -0.8059]])
```
However, if I convert it with coremltools 6.0b2 as follows:
```
import numpy as np
import coremltools as ct
mlmodel = ct.convert(
frac_traced,
convert_to="mlprogram",
inputs=[
ct.TensorType(name="x", shape=x.shape, dtype=np.float32),
]
)
mlmodel.save("frac.mlpackage")
```
and run `predict()` function using `frac.mlpackage`, I get tensor full of zeros in the output. The code is simply:
```
import torch
import coremltools as ct
model = ct.models.MLModel('frac.mlpackage')
torch.manual_seed(0)
x = 10 * torch.rand((5, 5)) - 5
y = model.predict({ 'x': x })
```
The problem occurs only if I convert with `convert_to="mlprogram",` parameter. Output is correct if I use `convert_to="neuralnetwork"`.
I guess something wrong hapens already at the `x_int = x.to(torch.int32)` operation. I get zeros tensor also when I try to return `x_int` from my `forward()`.
- coremltools version: 6.0b2
- MacOS 12.5
- PyTorch 1.12.1
Contributor guide
Assessment
This issue has not been assessed yet.