torch.jit.load(f, map_location=None, _extra_files=None, _restore_shapes=False)[source]

Load a ScriptModule or ScriptFunction previously saved with

All previously saved modules, no matter their device, are first loaded onto CPU, and then are moved to the devices they were saved from. If this fails (e.g. because the run time system doesn’t have certain devices), an exception is raised.

  • f – a file-like object (has to implement read, readline, tell, and seek), or a string containing a file name

  • map_location (string or torch.device) – A simplified version of map_location in used to dynamically remap storages to an alternative set of devices.

  • _extra_files (dictionary of filename to content) – The extra filenames given in the map would be loaded and their content would be stored in the provided map.

  • _restore_shapes (bool) – Whether or not to retrace the module on load using stored inputs


A ScriptModule object.

Example: .. testcode:

import torch
import io


# Load ScriptModule from io.BytesIO object
with open('', 'rb') as f:
    buffer = io.BytesIO(

# Load all tensors to the original device

# Load all tensors onto CPU, using a device
torch.jit.load(buffer, map_location=torch.device('cpu'))

# Load all tensors onto CPU, using a string
torch.jit.load(buffer, map_location='cpu')

# Load with extra files.
extra_files = {'foo.txt': ''}  # values will be replaced with data
torch.jit.load('', _extra_files=extra_files)


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources