matlab/matlab-agentic-toolkit / skills-catalog/code-generation/matlab-deploy-ai-model
matlab-deploy-ai-model
VerifiedGenerate 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.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 10.8 KB |
| manifest.yaml | config | 617 B |
| references/codegen-workflow.md | markdown | 4.0 KB |
| references/dnn-codegen-options.md | markdown | 6.9 KB |
| references/export-pytorch-models.md | markdown | 4.6 KB |
| references/litert-numeric-verification.md | markdown | 3.8 KB |
| references/litert-workflow.md | markdown | 14.7 KB |
| references/pytorch-api-reference.md | markdown | 2.6 KB |
| references/pytorch-data-layout.md | markdown | 4.1 KB |
| references/pytorch-export-gotchas.md | markdown | 4.7 KB |
| references/pytorch-export-patterns.md | markdown | 5.2 KB |
| references/pytorch-numeric-verification.md | markdown | 2.6 KB |
| references/pytorch-supported-models.md | markdown | 3.4 KB |
| references/pytorch-workflow.md | markdown | 5.7 KB |
| references/simulink-workflow.md | markdown | 12.7 KB |
| references/tensorflow-to-litert-conversion.md | markdown | 5.8 KB |
references/pytorch-supported-models.md
View on GitHub# Supported Models and Limitations
## Supported Model Types
`loadPyTorchExportedProgram` supports models exported with `torch.export.export()`
to the `.pt2` format. This includes:
- Convolutional networks (ResNet, VGG, MobileNet, EfficientNet)
- Recurrent networks (LSTM, GRU)
- Transformer models (encoder/decoder architectures)
- Custom models using supported operators
- Sequence-to-sequence models
- Classification, detection, segmentation models
## PyTorch Version Requirement
The required PyTorch version depends on the MATLAB release:
| MATLAB Release | PyTorch Version | Notes |
|---------------|----------------|-------|
| R2026a | 2.7.0 – 2.8.0 (stable releases only) | Any stable release in this range |
The export uses `torch.export.export()` which produces the ExportedProgram
format (.pt2).
## Execution Modes
| Mode | Description | Trade-off |
|------|-------------|-----------|
| `"JIT"` (default) | Just-in-time compilation | Faster load time, but no OpenMP parallelization — lower inference performance |
| `"MEX"` | MEX-based execution | Slower initial compilation, but leverages OpenMP for better inference performance, requires C compiler |
```matlab
% Faster to load, slower inference (no parallelization)
model = loadPyTorchExportedProgram("model.pt2", ExecutionMode="JIT");
% Slower initial compile, faster inference (OpenMP parallelization)
model = loadPyTorchExportedProgram("model.pt2", ExecutionMode="MEX");
```
Choose `"MEX"` when inference performance matters. Choose `"JIT"` for quick
prototyping where load time is more important than execution speed.
## Known Limitations
| Limitation | Description |
|-----------|-------------|
| No MAT-file saved models | Cannot save `PyTorchExportedProgram` to a MAT-file and reload |
| No quantized models | INT8/quantized PyTorch models are not supported |
| No complex dtypes | Complex-valued tensors are not supported |
| No CUDA-exported tensors | Models must be exported on CPU (`model.cpu()` before export) |
| No custom output classes | Model output must be standard tensors, not custom Python objects |
| Single-device only | Multi-GPU or distributed models must be consolidated to single device |
## Unsupported Operations
When code generation encounters an unsupported operator:
1. **Named operator in error message** (e.g., "operator X is not supported"):
- The only workaround is to restructure the PyTorch model to avoid that operator
- This may not always be feasible depending on the model architecture
- Check if an equivalent supported operator exists
2. **Unnamed/unclear error** (e.g., internal assertion, cryptic error):
- This is likely a bug in the support package
- Report to MathWorks Technical Support
- Provide the .pt2 file and the full error message
## Code Generation Support
| Target | Supported |
|--------|-----------|
| MEX (desktop) | Yes |
| Static library (lib) | Yes |
| Dynamic library (dll) | Yes |
| Executable (exe) | Yes |
| Embedded Coder (ERT) | Yes |
| GPU Coder (CUDA) | Yes |
## Required Add-On
**MATLAB Coder Support Package for PyTorch and LiteRT Models**
Install via Add-On Explorer or:
```matlab
matlab.addons.install("MATLAB Coder Support Package for PyTorch and LiteRT Models")
```
Verify installation:
```matlab
ver('coder') % MATLAB Coder must be installed
% Support package presence confirmed if loadPyTorchExportedProgram resolves
which loadPyTorchExportedProgram
```
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| 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: 197k | Updated | Claude CodeCodex | Verified | Install |
| 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: 119k | Updated | Claude CodeCodex | Verified | Install |
| 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: 118k | Updated | Claude CodeCodex | Verified | Install |