Rate this Page

TorchScript#

Created On: Sep 07, 2018 | Last Updated On: Jul 16, 2025

Warning

TorchScript is deprecated, please usetorch.export instead.

Creating TorchScript Code#

script

Script the function.

trace

Trace a function and return an executable orScriptFunction that will be optimized using just-in-time compilation.

script_if_tracing

Compilesfn when it is first called during tracing.

trace_module

Trace a module and return an executableScriptModule that will be optimized using just-in-time compilation.

fork

Create an asynchronous task executingfunc and a reference to the value of the result of this execution.

wait

Force completion of atorch.jit.Future[T] asynchronous task, returning the result of the task.

ScriptModule

Wrapper for C++ torch::jit::Module with methods, attributes, and parameters.

ScriptFunction

Functionally equivalent to aScriptModule, but represents a single function and does not have any attributes or Parameters.

freeze

Freeze ScriptModule, inline submodules, and attributes as constants.

optimize_for_inference

Perform a set of optimization passes to optimize a model for the purposes of inference.

enable_onednn_fusion

Enable or disables onednn JIT fusion based on the parameterenabled.

onednn_fusion_enabled

Return whether onednn JIT fusion is enabled.

set_fusion_strategy

Set the type and number of specializations that can occur during fusion.

strict_fusion

Give errors if not all nodes have been fused in inference, or symbolically differentiated in training.

save

Save an offline version of this module for use in a separate process.

load

Load aScriptModule orScriptFunction previously saved withtorch.jit.save.

ignore

This decorator indicates to the compiler that a function or method should be ignored and left as a Python function.

unused

This decorator indicates to the compiler that a function or method should be ignored and replaced with the raising of an exception.

interface

Decorate to annotate classes or modules of different types.

isinstance

Provide container type refinement in TorchScript.

Attribute

This method is a pass-through function that returnsvalue, mostly used to indicate to the TorchScript compiler that the left-hand side expression is a class instance attribute with type oftype.

annotate

Use to give type ofthe_value in TorchScript compiler.