Verify Numerical Consistency of a LiteRT or PyTorch ExportedProgram Model in Python
This page describes how to verify numerical consistency for a Python® model when you run it natively in Python environment and when you run it by calling a MATLAB® entry-point function. The mInvoke_torch entry-point
function loads a PyTorch ExportedProgram file by using the loadPyTorchExportedProgram function and performs prediction by calling the
invoke
function.
Prerequisite
To call MATLAB and execute MATLAB commands from within a Python environment as a computational engine, you must install the MATLAB Engine API for Python. For more information, see Install MATLAB Engine API for Python.
You also need to install the MATLAB Coder™ Support Package for PyTorch® and LiteRT Models. For more information, see Install MATLAB Coder Support Package for PyTorch and LiteRT Models.
Create the MATLAB Function
Create a mInvoke_torch function. The function loads a PyTorch
ExportedProgram model file by using the loadPyTorchExportedProgram
function. It then performs prediction by passing the
PyTorchExportedProgram object to the invoke
function.
function out = mInvoke_torch(network, input) persistent net; if isempty(net) net = loadPyTorchExportedProgram(network); end out = invoke(net,input); end
Execute the mInvoke_torch Function in Python
Start the MATLAB Engine in Python.
import matlab.engine
eng = matlab.engine.start_matlab()Use this code to add the MATLAB file on path and then specify the path to the PyTorch
ExportedProgram file. Replace pathtoMfiles with the path to
mInvoke_torch function, and pathtoModel with
the path to the PyTorch ExportedProgram file.
eng.addpath(r"/pathtoMfiles", nargout=0)
pt2_path = r"/pathtoModel/Model.pt2"Create test input x_torch in Python. Then cast the input data to
single data type.
import torch
import numpy as np
x_torch = torch.randn(1, 3, 224, 224, dtype=torch.float32)
x_np = x_torch.numpy()
x_mat_single = eng.single(x_np)
Call the MATLAB function by passing the input data
x_mat_single.
y_matlab = eng.mInvoke_torch(pt2_path, x_mat_single)Convert the MATLAB output to NumPy.
y_matlab_np = np.array(y_matlab).astype(np.float32)Execute the PyTorch ExportedProgram in Python
Load the PyTorch ExportedProgram file in Python. Create a callable
nn.Module by using .module().
from torch.export import load
exported_program = torch.export.load(pt2_path) # returns an ExportedProgram
model_ep = exported_program.module() # get a callable nn.Module
Invoke the nn.Module by passing the same input tensor previously
used to call the MATLAB function.
y_python = model_ep(x_torch).detach().numpy().astype(np.float32)Compare Numerical Outputs
MATLAB might return a row vector of size 1-by-N while the Python model returns a 1‑D vector. For element‑wise comparison, reshape the MATLAB output to remove the singleton dimension so it matches the shape of the Python output.
if y_python.shape != y_matlab_np.shape:
y_matlab_np = y_matlab_np.reshape(y_python.shape)
Calculate the maximum absolute and relative error between MATLAB and Python outputs.
max_abs_err = float(np.max(np.abs(y_python - y_matlab_np)))
max_rel_err = float(np.max(np.abs(y_python - y_matlab_np) / (np.abs(y_python) + 1e-12)))
print(
"Comparison summary:\n"
f" Max absolute error : {max_abs_err:.3e}\n"
f" Max relative error : {max_rel_err:.3e}"
)
See Also
PyTorchExportedProgram | LiteRTModel | loadPyTorchExportedProgram | loadLiteRTModel | summary | inputSpecifications | outputSpecifications | invoke