title: last.ckpt | RuntimeError: [enforce fail at inline_container.cc:145] . PytorchStreamReader failed reading zip archive: failed finding central directory
body: <p>I am using <code>JupyterLab</code> on <code>AWS SageMaker</code>. Kernel: <code>conda_pytorch_latest_p36</code>.</p>
  <p>I have successfully performed training.</p>
  <p>Now, I attempt to set up the model for predictions, i.e. testing.</p>
  <p>I suspect <code>last.ckpt</code> file is corrupt; as it fails on line:</p>
  <pre class="lang-py prettyprint-override"><code>model = OntologyTaggerModel.load_from_checkpoint('last.ckpt.2cCC2f52', map_location=torch.device(device), from_checkpoint=True)
  </code></pre>
  <hr />
  <p><strong>Where does <code>last.ckpt</code> file come from - BERT download or my own model definition?</strong></p>
  <p><strong>How do I regenerate it?</strong></p>
  <p>Update: I was able to re-generate it: <code>last.ckpt.E342d53e</code>.</p>
  <p>Run model load with <code>last.ckpt.**E342d53e**</code>:</p>
  <pre class="lang-py prettyprint-override"><code>RuntimeError: [enforce fail at inline_container.cc:145] . PytorchStreamReader failed reading zip archive: failed finding central directory
  </code></pre>
  <p>Run model load with <code>last.ckpt</code> (without unique string in filename):</p>
  <pre class="lang-py prettyprint-override"><code>FileNotFoundError: [Errno 2] No such file or directory: '/home/ec2-user/SageMaker/last.ckpt'
  </code></pre>
  <p>I launched a new <code>AWS SageMaker</code> instance without luck.</p>
  <hr />
  <p>Suspect Code (2nd last line):</p>
  <pre class="lang-py prettyprint-override"><code>def get_device():
      device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
      return device
  
  def preprocess(input, preprocessor):
      result = [torch.tensor(preprocessor.tokenise(i)).unsqueeze(dim=0) for i in input]
      result = torch.cat(result)
      return result
  def predict_fn(input, model_artifacts):
      preprocessor, model, label_mapper = model_artifacts
      # Pre-process
      input_tensor = preprocess(input, preprocessor)
      # Copy input to gpu if available
      device = get_device()
      
      input_tensor = input_tensor.to(device=device)
      # Invoke
      model.eval()
      classes = []
      probs = []
      with torch.no_grad():
          output_tensors = model(input_tensor)[1]
          # Convert to probabilities
          softmax = torch.nn.Softmax()
          for class_index, output_tensor in enumerate(output_tensors):
               output_tensor = softmax(output_tensor)
               prob, predictions = torch.max(output_tensor, dim=1)   
               classes.append(label_mapper.reverse_map(predictions, class_index))
               probs.append(prob)
          classes = [c for c in zip(*classes)]   
          probs = [c for c in zip(*probs)]
  
      return classes, probs
  
  device = get_device()
  tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
  label_mapper = LabelMapper('classes.txt')
  model = OntologyTaggerModel.load_from_checkpoint('last.ckpt.2cCC2f52', map_location=torch.device(device), from_checkpoint=True) # CRASH !
  model = model.to(device)
  </code></pre>
  <p>Traceback:</p>
  <pre class="lang-py prettyprint-override"><code>---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-8-ba98e0974205&gt; in &lt;module&gt;
       36 tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
       37 label_mapper = LabelMapper('classes.txt')
  ---&gt; 38 model = OntologyTaggerModel.load_from_checkpoint('last.ckpt.2cCC2f52', map_location=torch.device(device), from_checkpoint=True)
       39 model = model.to(device)
  
  ~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/core/saving.py in load_from_checkpoint(cls, checkpoint_path, map_location, hparams_file, strict, **kwargs)
      131         &quot;&quot;&quot;
      132         if map_location is not None:
  --&gt; 133             checkpoint = pl_load(checkpoint_path, map_location=map_location)
      134         else:
      135             checkpoint = pl_load(checkpoint_path, map_location=lambda storage, loc: storage)
  
  ~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/utilities/cloud_io.py in load(path_or_url, map_location)
       44     fs = get_filesystem(path_or_url)
       45     with fs.open(path_or_url, &quot;rb&quot;) as f:
  ---&gt; 46         return torch.load(f, map_location=map_location)
       47 
       48 
  
  ~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
      585             # reset back to the original position.
      586             orig_position = opened_file.tell()
  --&gt; 587             with _open_zipfile_reader(opened_file) as opened_zipfile:
      588                 if _is_torchscript_zip(opened_zipfile):
      589                     warnings.warn(&quot;'torch.load' received a zip file that looks like a TorchScript archive&quot;
  
  ~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/serialization.py in __init__(self, name_or_buffer)
      240 class _open_zipfile_reader(_opener):
      241     def __init__(self, name_or_buffer) -&gt; None:
  --&gt; 242         super(_open_zipfile_reader, self).__init__(torch._C.PyTorchFileReader(name_or_buffer))
      243 
      244 
  
  RuntimeError: [enforce fail at inline_container.cc:145] . PytorchStreamReader failed reading zip archive: failed finding central directory
  </code></pre>
  <p>Please let me know if I should add anything else.</p>