主要内容

loadPyTorchExportedProgram

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 2.8.0, and save it as a 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

    You must first export a PyTorch model to a torch.export.ExportedProgram in PyTorch 2.8.0, and save it as a PyTorch ExportedProgram file. loadPyTorchExportedProgram does not support loading:

    • Models saved in MAT-files.

    • Quantized models.

    • Models with complex data types.

    • Models exported using CUDA tensors. You must move both the model parameters and tensor inputs to CPU before exporting the model.

    • Models that return custom output classes. If the model has a custom output class, wrap it in a custom nn.Module so that it returns only native tensors or standard Python containers. This removes any external class dependencies from the exported graph.

    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