Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/code-generation/matlab-deploy-ai-model

matlab-deploy-ai-model

Verified

Generate C/C++ or CUDA code from an AI model (PyTorch, LiteRT) using MATLAB Coder or GPU Coder. Use when the user wants to integrate an AI model into an application with code generation as the end goal — generating MEX, CUDA MEX, static library, dynamic library, or executable — or using the model in Simulink for simulation and code generation. Covers PyTorch ExportedProgram (.pt2) via loadPyTorchExportedProgram and LiteRT (.tflite) via loadLiteRTModel (R2026a+). Keywords: PyTorch, torch, .pt2, ExportedProgram, loadPyTorchExportedProgram, invoke, codegen, MEX, CUDA, GPU, C, C++, deploy, AI model, deep learning model, LiteRT, TFLite, TensorFlow Lite, Simulink, slbuild, PyTorch ExportedProgram block, MATLAB Function block, dlosslib, loadLiteRTModel.

references/litert-numeric-verification.md

View on GitHub
# Numeric Verification: MATLAB vs Python LiteRT

Compare inference output between the original LiteRT model (in Python via
`ai_edge_litert.Interpreter`) and the MATLAB-loaded model to confirm correct
loading and numerical equivalence.

## Approach: pyrun from MATLAB

Use MATLAB's `pyrun` to call Python code inline from a MATLAB script. This
keeps everything in a single MATLAB script — no separate Python process, no
`matlab.engine` package needed.

**Python environment setup:** See `evals/README.md` for venv location and
authoring styles. Point MATLAB at the venv with `ai-edge-litert`:

```matlab
venvRoot = getenv("LITERT_VENV_ROOT_DIR");
pyenv(ExecutionMode="OutOfProcess", ...
    Version=fullfile(venvRoot, "bin", "python3"));
```

## Complete Verification Script

```matlab
%% Configure Python environment
venvRoot = getenv("LITERT_VENV_ROOT_DIR");
pyenv(ExecutionMode="OutOfProcess", ...
    Version=fullfile(venvRoot, "bin", "python3"));

%% MATLAB inference
modelPath = "/path/to/model.tflite";
model = loadLiteRTModel(modelPath);
specs = inputSpecifications(model);
input = randn(specs.Size{1}, specs.Type{1});

matlabOut = invoke(model, input);

%% Python LiteRT inference via pyrun
pyOut = pyrun( ...
    ["from ai_edge_litert.interpreter import Interpreter;" ...
     "import numpy as np;" ...
     "interp = Interpreter(model_path=mp);" ...
     "interp.allocate_tensors();" ...
     "inp_det = interp.get_input_details();" ...
     "out_det = interp.get_output_details();" ...
     "interp.set_tensor(inp_det[0]['index'], np.array(x, dtype='float32'));" ...
     "interp.invoke();" ...
     "result = interp.get_tensor(out_det[0]['index'])"], ...
    "result", mp=modelPath, x=input);
pyOut = single(pyOut);

%% Compare with tolerance
testCase = matlab.unittest.TestCase.forInteractiveUse;
testCase.verifyThat(matlabOut, matlab.unittest.constraints.IsEqualTo(pyOut, ...
    'Within', matlab.unittest.constraints.AbsoluteTolerance(single(1e-5))));
fprintf('VERIFICATION PASSED: MATLAB and Python outputs match within 1e-5.\n');
```

## Key Details

**Python package:** Use `ai-edge-litert` (current Google AI Edge package).
Import as `from ai_edge_litert.interpreter import Interpreter`. The legacy
`tflite_runtime` package is deprecated.

**Windows note:** `ai_edge_litert` requires the Visual C++ Redistributable to
be installed on Windows. Alternatively, use `tf.lite.Interpreter` from the
`tensorflow` package, which has the same API:

```python
import tensorflow as tf
interp = tf.lite.Interpreter(model_path=mp)
```

**ExecutionMode OutOfProcess:** Required to avoid library conflicts between
MATLAB and the Python runtime. Set this before any `pyrun` call.

**Data passing:** `pyrun` converts MATLAB arrays to numpy arrays automatically.
The returned numpy array converts back with `single(pyOut)`.

**Tolerance values:**
- Absolute tolerance: `1e-5` (standard for single-precision inference)
- Differences arise from floating-point operation ordering, not from incorrect loading

**Shape matching:** LiteRT models use NHWC layout in both Python and MATLAB,
so shapes align without permutation.

**Variable-size batch dimension:** If `inputSpecifications` shows `Inf` for the
batch dimension, replace it with a concrete size (e.g., 1) before creating the
test input:

```matlab
inputSize = specs.Size{1};
inputSize(isinf(inputSize)) = 1;
input = randn(inputSize, specs.Type{1});
```

## When to Use

- After loading a model with `loadLiteRTModel` for the first time
- Before proceeding to MEX generation (confirm the model loaded correctly)
- When debugging unexpected inference results
- When comparing MATLAB output against a known Python reference

## Prerequisites

- Python venv with `ai-edge-litert` installed (path in `LITERT_VENV_ROOT_DIR`)
- The `.tflite` file accessible from MATLAB

----

Copyright 2026 The MathWorks, Inc.

----
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 322kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall