TorchScript compatibility between pytorch versions


I am trying to use libtorch in a C++ application that’s restricted to the gcc-4.8 ABI, which forces me to link against libtorch-1.2.0 (I was told newer version of libtorch aren’t compatible with that ABI).

This application needs to load a pre-trained model. That model was trained and saved from a python script that needs to use pytorch-1.3.0 or newer.

Trying to load the serialized TorchScript model aborts the program with this error:

terminate called after throwing an instance of 'c10::Error'
  what():  [enforce fail at] . PytorchStreamReader failed closing reader: file not found 

This happens both from the C++ and the python API. Is this a compatibility issue?

This is easily reproducible: from an environment with either pytorch-1.3.1 or 1.5.0, run

import torch

class TestModule(torch.nn.Module):
    def __init__(self, N, M):
        self.weight = torch.nn.Parameter(torch.rand(N, M))

    def forward(self, x):

my_module = TestModule(10,20)
test_module = torch.jit.script(my_module)"")

Then from an environment with pytorch-1.2.0:

import torch
m = torch.jit.load('')

If the file was written from 1.5.0, I get

RuntimeError: version_number <= kMaxSupportedFileFormatVersion INTERNAL ASSERT FAILED at ../caffe2/serialize/, please report a bug to PyTorch. Attempted to read a PyTorch file with version 2, but the maximum supported version for reading is 1. Your PyTorch installation may be too old.

which is clearly a compatibility issue. But if the file was written from 1.3.1, I get the “file not found” error above (note this is a process abort, not an exception, from both C++ and python API).

I could not find documentation on TorchScript file compatibility across versions. Is that information available somewhere?


Hi, we maintain backwards compatibility but not forwards compatiblity. New versions will work with older models, but older versions will not work with newer models.