主要内容

invoke

Run inference on a PyTorch ExportedProgram or LiteRT model input to compute output

Since R2026a

    Description

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

    Y = invoke(model,X) computes the output of the PyTorch® ExportedProgram or LiteRT model, model, for the input data X. This syntax takes a single input and outputs a single output.

    example

    Y = invoke(model,X1,...,XM) computes the output of the PyTorch ExportedProgram or LiteRT model for the input data X1,X2,...,XM. This syntax takes multiple inputs and outputs a single output.

    [Y1,...,YN] = invoke(___) returns the outputs Y1, …, YN for models that have N outputs.

    example

    [___] = invoke(___,FcnName=FcnName) computes the outputs for the function, FcnName, in the model.

    Note

    If the model contains multiple functions, you must specify the name of the function for which to calculate the outputs.

    Note

    Instead of using the invoke method, you can call object with an argument as if it were a function. For example, Y = model(X) and Y = invoke(model, X) perform equivalent operations.

    example

    Examples

    collapse all

    This example shows how to compute the output of a PyTorch® ExportedProgram or LiteRT model when the model takes one input.

    Suppose that your current working directory contains a LiteRT model named siso.tflite. Load the model.

    myModel = loadLiteRTModel("siso.tflite")
    Loading the model. This may take a few minutes.
    
    myModel = 
      LiteRTModel contained in siso.tflite: 
    
                              Input Specifications                       
        _________________________________________________________________
    
        Input     Name                    Size                     Type  
        _____    _______    _________________________________    ________
                                                                         
          1      "image"    "<variable-size> x 128 x 128 x 3"    "single"
    
    
                          Output Specifications                  
        _________________________________________________________
    
        Output       Name               Size               Type  
        ______    __________    _____________________    ________
                                                                 
          1       "output_0"    "<variable-size> 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
    
    

    The model takes a single-precision input of size Inf-by-128-by-128-by-3, and returns a single-precision output of size Inf-by-1. The first dimension of the input and output is unbounded variable size.

    Create a 2-by-128-by-128-by-3 input array of the single data type. Compute the output by passing the model object and input to the invoke function.

    in = ones(2, 128, 128, 3, "single");
    out = invoke(myModel, in);

    This example shows how to compute multiple outputs when the model takes multiple inputs.

    Suppose that your current working directory contains a LiteRT model named mimo.tflite. Load the model by using the loadLiteRTModel function.

    myModel = loadLiteRTModel("mimo.tflite")
    Loading the model. This may take a few minutes.
    
    myModel = 
      LiteRTModel contained in mimo.tflite: 
    
                           Input Specifications                    
        ___________________________________________________________
    
        Input        Name                 Size               Type  
        _____    ____________    ______________________    ________
                                                                   
          1      "features_a"    "<variable-size> x 16"    "single"
          2      "features_b"    "<variable-size> x 8"     "single"
    
    
                          Output Specifications                  
        _________________________________________________________
    
        Output       Name               Size               Type  
        ______    __________    _____________________    ________
                                                                 
          1       "output_1"    "<variable-size> x 2"    "single"
          2       "output_0"    "<variable-size> x 4"    "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
    
    

    The model takes a single-precision input of size Inf-by-16 and Inf-by-8, and two single-precision outputs of sizes Inf-by-2 and Inf-by-4. The first dimension of both inputs is unbounded variable size. The size of the first dimension in the outputs is determined by the size of the first dimension in the corresponding inputs.

    Compute the outputs by creating two input single-precision arrays and passing them to the invoke function.

    in1 = ones(3,16,"single");
    in2 = ones(3,8,"single");
    [out1, out2] = invoke(myModel,in1,in2);

    This example shows how to compute the output for a specific function in a model.

    Suppose that your current working directory contains a LiteRT model named multi_sig.tflite. Load the model.

    myModel = loadLiteRTModel("multi_sig.tflite")
    Loading the model. This may take a few minutes.
    
    myModel = 
      LiteRTModel contained in multi_sig.tflite: 
    
    Contains 2 functions: adder, multiplier 
    
        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
    
    

    myModel object contains two functions. Display the input and output specifications for the adder function.

    summary(myModel,FcnName="adder")
                Input Specifications        
        ____________________________________
    
        Input    Name     Size        Type  
        _____    ____    _______    ________
                                            
          1      "x"     "1 x 1"    "single"
    
    
                  Output Specifications          
        _________________________________________
    
        Output      Name       Size        Type  
        ______    ________    _______    ________
                                                 
          1       "output"    "1 x 1"    "single"
    

    The adder function takes a single-precision scalar input, and returns a single-precision scalar output.

    Create a single-precision scalar input in. Compute the output for the adder function of the myModel with input in.

    in = single(1);
    out = invoke(myModel,in,FcnName = "adder")
    out = single
    
    3
    

    Input Arguments

    collapse all

    Input model, specified as either:

    Input data, specified as an array of numeric or logical values.

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

    Name of function for which to calculate the outputs, specified as a character vector or string scalar.

    Example: "predict"

    Example: "plus_miso_3x4x5"

    Output Arguments

    collapse all

    Output data, returned as an array of numeric or logical values.

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

    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