主要内容

PyTorch ExportedProgram

Load PyTorch ExportedProgram model, compute output, and generate deployable code

Since R2026a

  • PyTorch Exported Program block

Libraries:
MATLAB Coder Support Package for PyTorch & LiteRT Models

Description

Add-On Required: This feature requires the MATLAB Coder Support Package for PyTorch and LiteRT Models add-on.

The PyTorch ExportedProgram block loads a PyTorch ExportedProgram model. The block uses the PyTorch ExportedProgram model to run inference on the input data to compute the outputs. The block can generate deployable code.

To load a PyTorch ExportedProgram model, use the PyTorch ExportedProgram model file parameter. By default, the block has one input and output port, in1 and out1. When you load the PyTorch ExportedProgram file, the block updates the number of input and output ports specified in the file.

Note

You must export a PyTorch® model to a torch.export.ExportedProgram in PyTorch 2.8.0, and save it as a PT2 PyTorch ExportedProgram file. For more information, see Prepare PyTorch Models for MATLAB and Simulink Code Generation.

Examples

Ports

Input

expand all

Input data, specified as a numeric array. You can rearrange the dimensions of the input data that the block passes to the PyTorch ExportedProgram model by specifying a permutation vector on the Inputs tab of the Block Parameters dialog box.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean

Output

expand all

Computed outputs, returned as a numeric array. You can rearrange the dimensions of the output data returned by the PyTorch ExportedProgram model by specifying a permutation vector on the Outputs tab of the Block Parameters dialog box.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean

Parameters

expand all

To edit block parameters interactively, use the Property Inspector. From the Simulink® Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.

Model

Specify the name or path of a PyTorch ExportedProgram model file. Alternatively, you can select a file by clicking the Browse button.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

To get the block parameter value programmatically, use the get_param (Simulink) function.

Block Parameter: ModelFilePath
Type: string scalar | character vector

Specify the discrete interval between sample time hits or specify another type of sample time, such as continuous (0) or inherited (–1). For more options, see Types of Sample Time (Simulink).

By default, the PyTorch ExportedProgram block inherits sample time based on the context of the block within the model.

Programmatic Use

Block Parameter: SampleTime
Type: string scalar or character vector
Values: scalar
Default: "–1"

Inputs

Properties to apply to the input data. Each row of the table corresponds to an individual input port of the PyTorch ExportedProgram block.

Double-click a table cell entry to edit its value. If you specify multiple input ports, their order must correspond to the input order in the PyTorch ExportedProgram model.

The table has these columns:

  • Port — Block input port label. This column is read-only.

  • Inferred type — Data type the PyTorch ExportedProgram model accepts, returned as a character vector. This column is read-only.

  • Inferred size — Size of the input data the PyTorch ExportedProgram model accepts. The dimension is a vector of nonnegative integer or Inf. Inf indicates a dimension is variable-sized. This column is read-only.

  • Permutation to apply — New dimension arrangement for the input data, specified as a numeric vector with unique positive integer elements that represent the dimensions of the input data (see permute). For example, if the input data is a 2D matrix, you can specify [2 1] to switch the row and column dimensions. The PyTorch ExportedProgram block passes the rearranged array to the PyTorch ExportedProgram model.

Programmatic Use

Block Parameter: InputsTable
Type: cell array

Outputs

Properties to apply to the output data. Each row of the table corresponds to an individual output port of the PyTorch ExportedProgram block.

Double-click a table cell entry to edit its value.

The table has these columns:

  • Port — Block output port label. This column is read-only.

  • Inferred type — Data type returned by the PyTorch ExportedProgram model. This column is read-only.

  • Inferred size — Size of the output data returned by the PyTorch ExportedProgram model. The dimension is a vector of nonnegative integer or Inf. Inf indicates a dimension is variable-sized. This column is read-only.

  • Maximum size — Maximum size of the block output along each dimension, specified as a vector of positive integers. The default value is same as the Inferred size. If the output is variable-sized, the generated code uses dynamic memory allocation, which may slow execution. Specify a maximum size for each variable-sized dimension to avoid dynamic allocation. If the Inferred size has a variable-sized dimension, you cannot set Maximum size to a scalar or vector with only singleton dimensions.

  • Permutation to apply — New dimension ordering for the array that the PyTorch ExportedProgram block passes to the output port, specified as a numeric vector with unique positive integer elements (see permute).

Programmatic Use

Block Parameter: OutputsTable
Type: cell array

Alternative Functionality

You can use a MATLAB Function block, along with the invoke object function of a PyTorchExportedProgram object, to compute output and generate code.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2026a