Repository: pytorch
Issue: Fail to load torch script model
Labels: oncall: jit,triaged

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->
When following this tutorial: https://pytorch.org/tutorials/advanced/cpp_export.html

Step 3, executing this command with my model: 

./example-app <path_to_model>/traced_resnet_model.pt 

produces an error when trying to load model.

## To Reproduce

Steps to reproduce the behavior:

1. Follow tutorial steps exactly, except
2. Change load model line to: std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]); (found in another issue because this tutorial is outdated)
3. Save mobilenet model from pytorch as traced model using torch.jit.trace
4. comment out try catch block in script to see what exactly the error is when loading model

fails with this error:

libc++abi.dylib: terminating with uncaught exception of type c10::Error: istream reader failed: reading file. (validate at /Users/ishaghodgaonkar/pytorch/caffe2/serialize/istream_adapter.cc:32)
frame #0: c10::Error::Error(c10::SourceLocation, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 64 (0x10dcf24e0 in libc10.dylib)
frame #1: caffe2::serialize::IStreamAdapter::validate(char const*) const + 166 (0x10b7f8256 in libcaffe2.dylib)
frame #2: caffe2::serialize::IStreamAdapter::read(unsigned long long, void*, unsigned long, char const*) const + 159 (0x10b7f833f in libcaffe2.dylib)
frame #3: caffe2::serialize::PyTorchStreamReader::init() + 222 (0x10b7f43be in libcaffe2.dylib)
frame #4: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::__1::unique_ptr<caffe2::serialize::ReadAdapterInterface, std::__1::default_delete<caffe2::serialize::ReadAdapterInterface> >) + 109 (0x10b7f51cd in libcaffe2.dylib)
frame #5: torch::jit::load(std::__1::unique_ptr<caffe2::serialize::ReadAdapterInterface, std::__1::default_delete<caffe2::serialize::ReadAdapterInterface> >, c10::optional<c10::Device>) + 522 (0x1098ad9da in libtorch.1.dylib)
frame #6: torch::jit::load(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, c10::optional<c10::Device>) + 93 (0x1098adbdd in libtorch.1.dylib)
frame #7: main + 127 (0x109123f4f in example-app)
frame #8: start + 1 (0x7fff765b73d5 in libdyld.dylib)
frame #9: 0x0 + 2 (0x2 in ???)

Abort trap: 6

## Expected behavior

Load the model without runtime error, print 'ok'

## Environment

PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A

OS: Mac OSX 10.14.6
GCC version: Could not collect
CMake version: version 3.13.4

Python version: 2.7
Is CUDA available: N/A
CUDA runtime version: Could not collect
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] numpy==1.17.2
[pip] torch==1.4.0
[pip] torch-extension==0.1
[pip] torchsummary==1.5.1
[pip] torchvision==0.5.0
[conda] blas                      1.0                         mkl  
[conda] mkl                       2019.1                      144  
[conda] mkl-include               2019.4                      233  
[conda] mkl-service               1.1.2            py37hfbe908c_5  
[conda] mkl_fft                   1.0.6            py37h27c97d8_0  
[conda] mkl_random                1.0.2            py37h27c97d8_0  
[conda] pytorch                   1.4.0                   py3.7_0    pytorch
[conda] torch                     1.1.0a0+866c4e3          pypi_0    pypi
[conda] torchvision               0.1.7                    pypi_0    pypi

## Additional context

I also tried using torch::jit::script instead of torch::jit::trace as mentioned in another issue. Same error.

Code:
#include <torch/script.h> // One-stop header.

#include <iostream>
#include <memory>

int main(int argc, const char* argv[]) {
  if (argc != 2) {
    std::cerr << "usage: example-app <path-to-exported-script-module>\n";
    return -1;
  }
     // Deserialize the ScriptModule from a file using torch::jit::load().
     std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]);

  std::cout << "ok\n";
}


cc @suo @SsnL