Main Content

loadPyTorchExportedProgram

R2026b

Load PyTorch ExportedProgram model file

Since R2026a

    Description

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

    model = loadPyTorchExportedProgram(modelFileName) loads a pretrained PyTorch® ExportedProgram model file.

    Note

    This function takes a PyTorch ExportedProgram model file as input.

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

    example

    model = loadPyTorchExportedProgram(modelFileName,ExecutionMode=ExecutionMode) loads a pretrained PyTorch ExportedProgram model file using the specified execution mode.

    Examples

    collapse all

    Suppose that you have a PyTorch ExportedProgram model named siso.pt2 that takes one input and returns one output. Create a PyTorchExportedProgram object.

    myModel = loadPyTorchExportedProgram("siso.pt2")
    Loading the model. This may take a few minutes.
    
    myModel = 
      PyTorchExportedProgram contained in siso.pt2: 
    
                      Input Specifications               
        _________________________________________________
    
        Input    Name            Size              Type  
        _____    _____    ___________________    ________
                                                         
          1      "in1"    "1 x 3 x 128 x 128"    "single"
    
    
                     Output Specifications             
        _______________________________________________
    
        Output     Name          Size            Type  
        ______    ______    _______________    ________
                                                       
          1       "out1"    "1 x 8 x 2 x 1"    "single"
    
    
        properties: 
            ModelPath                         -      Path to the model file
            FcnNames                          -      Names of the functions in the model
        methods: 
            invoke                            -      Performs forward inference by invoking a function in the model
            inputSpecifications               -      Returns the input specifications of the model or of a specific function in the model
            outputSpecifications              -      Returns the output specifications of the model or of a specific function in the model
            summary                           -      Displays the input and output specifications of the model or of a specific function in the model
    
    

    Input Arguments

    collapse all

    Name of the ExportedProgram model file, specified as a character vector or a scalar string.

    Example: "myModelName"

    Example: "myfolder/myModelName"

    Execution mode to use for MATLAB simulation behavior, specified as "JIT" or "MEX".

    JIT code is incompatible with the OpenMP application interface. JIT execution mode does not support multi-threading. If you want to parallelize for-loops, use MEX execution mode. You must install a supported C compiler to use the MEX execution mode. For more information about setting up the C/C++ compiler, see Installing and Configuring Prerequisite Products.

    Output Arguments

    collapse all

    PyTorch ExportedProgram model, returned as a PyTorchExportedProgram object.

    Limitations

    The code generator might apply optimizations that assume the absence of NaN values, which can affect NaN propagation behavior. If model inputs contain NaN values, generated code might produce different results from the reference framework.

    Extended Capabilities

    expand all

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

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

    Version History

    Introduced in R2026a