Repository: pytorch
Issue: torch::jit::pickle_load is unable to read some files saved by torch.save
Labels: oncall: jit

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

I have two files `data1.pt` and `data2.pt`, generated by the following code
```python3
#!/usr/bin/env python3

import torch

data1 = {"arcs": torch.arange(4, dtype=torch.int32)}
data2 = {"arcs": torch.arange(4, dtype=torch.int32), "aux_labels": torch.arange(3)}
torch.save(data1, "data1.pt")
torch.save(data2, "data2.pt")
```

The following C++ code is used to read the above two files:
```cpp
// main.cc
#include <cassert>
#include <iostream>

#include "torch/script.h"
int main(int argc, char *argv[]) {
  assert(argc == 2);
  std::cout << "Loading " << argv[1] << "\n";

  FILE *fp = fopen(argv[1], "rb");
  fseek(fp, 0, SEEK_END);
  size_t n = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  std::vector<char> data(n);
  fread(data.data(), n, 1, fp);
  fclose(fp);

  auto a = torch::jit::pickle_load(data);
  auto dict = a.toGenericDict();
  std::cout << dict.at("arcs").toTensor();
  if (dict.contains("aux_labels")) {
    std::cout << dict.at("aux_labels").toTensor();
  }
  return 0;
}
```

The above C++ code works fine to read `data2.pt`. Its output is
```
$ ./bin/main data2.pt
Loading data2.pt
 0
 1
 2
 3
[ CPUIntType{4} ] 0
 1
 2
[ CPULongType{3} ]
```

But it crashes while reading `data1.pt`, see the log below
```
$ ./bin/main data1.pt
Loading data1.pt
terminate called after throwing an instance of 'c10::Error'
  what():  Unknown opcode for unpickling at 0x73: 115
Exception raised from readInstruction at /pytorch/torch/csrc/jit/serialization/unpickler.cpp:441 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x42 (0x7f4b83f698b2 in /ceph-fj/fangjun/py38/lib/python3.8/site-packages/torch/lib/libc10.so)
frame #1: torch::jit::Unpickler::readInstruction() + 0x42c (0x7f4b765b758c in /ceph-fj/fangjun/py38/lib/python3.8/site-packages/torch/lib/libtorch_cpu.so)
frame #2: torch::jit::Unpickler::run() + 0xa0 (0x7f4b765b95c0 in /ceph-fj/fangjun/py38/lib/python3.8/site-packages/torch/lib/libtorch_cpu.so)
frame #3: torch::jit::Unpickler::parse_ivalue() + 0x20 (0x7f4b765b9a60 in /ceph-fj/fangjun/py38/lib/python3.8/site-packages/torch/lib/libtorch_cpu.so)
frame #4: torch::jit::readArchiveAndTensors(std::string const&, c10::optional<std::function<c10::StrongTypePtr (c10::QualifiedName const&)> >, c10::optional<std::function<c10::intrusive_ptr<c10::ivalue::Object, c10::detail::intrusive_target_default_null_type<c10::ivalue::Object> > (c10::StrongTypePtr, c10::IValue)> >, c10::optional<c10::Device>, caffe2::serialize::PyTorchStreamReader&) + 0x597 (0x7f4b76561d27 in /ceph-fj/fangjun/py38/lib/python3.8/site-packages/torch/lib/libtorch_cpu.so)
frame #5: torch::jit::pickle_load(std::vector<char, std::allocator<char> > const&) + 0x106 (0x7f4b76583836 in /ceph-fj/fangjun/py38/lib/python3.8/site-packages/torch/lib/libtorch_cpu.so)
frame #6: <unknown function> + 0x3dfa (0x5645f8f67dfa in ./bin/main)
frame #7: __libc_start_main + 0xe7 (0x7f4b397c2bf7 in /lib/x86_64-linux-gnu/libc.so.6)
frame #8: <unknown function> + 0x3aea (0x5645f8f67aea in ./bin/main)

Aborted
```

## To Reproduce

Steps to reproduce the behavior:

1. Use the above Python snippet to generate `data1.pt` and `data2.pt`
2. Compile the above C++ code to get a binary named `./main`
3. Run `./main data1.pt`
4. Run `./main data2.pt`

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->

It should work for both `./main data1.pt` and `./main data2.pt`.



## Environment

```
wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
```

```
$ python3 collect_env.py
Collecting environment information...
PyTorch version: 1.7.1+cu101
Is debug build: False
CUDA used to build PyTorch: 10.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.10.2
Libc version: glibc-2.27

Python version: 3.8.6 (default, Dec  2 2020, 15:56:31)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-4.9.2-3.0.0.std7b.el7.5.x86_64-x86_64-with-glibc2.2.5
Is CUDA available: True
CUDA runtime version: 10.1.243
GPU models and configuration: GPU 0: Tesla V100-PCIE-32GB
Nvidia driver version: 440.33.01
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.2
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.20.1
[pip3] torch==1.7.1+cu101
[pip3] torchaudio==0.7.2
[conda] Could not collect
```