Closed ndonyapour closed 2 years ago
Could this be because your pytorch and libtorch are not the same version?
I'm using the same version (1.8). I tried to debug the code, and the data type of the triu_index
matrix in the line above the error line becomes torch.Double
during the runtime. Changing the data type of index
is fixing the issue, but I thought this might not be the best solution.
angular_aev.index_add_(0, index.to(dtype=torch.int64), angular_terms_)
Looks like you hit a PyTorch bug. Could you open a bug report at https://github.com/pytorch/pytorch
I tired to reproduce the error using a simple model that uses the index_add_
method. However, there is no error when I'm loading and running this model in c++.
Here is my code
from torch import Tensor
from typing import Tuple
class my_model(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, input_: Tuple[Tensor, Tensor]):
species, coordinates = input_
tensor = torch.tensor([[1, 2, 3], [1, 2, 3],[1, 2, 3]],
dtype=torch.double)
index = torch.tensor([0, 3, 2], dtype=torch.long)
coordinates.index_add_(1, index, tensor)
return coordinates
def save_model():
save_path = './test_model.pt'
model = my_model()
script_module = torch.jit.script(model)
script_module.save(save_path)
def test_saved_model():
save_path = './test_model.pt'
model = torch.jit.load(save_path)
coordinates = torch.rand((1, 8, 3),
dtype=torch.double)
species = torch.ones((1, coordinates.shape[0]),
dtype=torch.int64)
print(model((species, coordinates)))
if __name__ == '__main__':
save_model()
test_saved_model()
Hello @ndonyapour libtorch has a bug where integer tensors are cast to torch::kDouble tensors if you call model.double() in a C++ context. I have reported this bug, however unfortunately it didn't get enough attention at the moment, maybe I failed to make myself clear at the time. The issue can be worked around manually by adding a function in your model that recasts the tensors into integer tensors. I don't really have time right now to provide a full working solution but you can look at BuiltinModel's _recast_long_tensors method for an idea on how to solve this.
I should probably raise the bug to pytorch again one of these days, unfortunately there is not much we can do on our side to fix this
Hello, I'm trying to load the TorchAni model (`compiled_model.pt') in the c++ using libtorch however, it gives the following error
Here is my code