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-workflow.md
View on GitHub# Generate C/C++/CUDA Code from a PyTorch Model
Complete workflow for generating C/C++ or CUDA code from a PyTorch ExportedProgram
(.pt2) using the MATLAB Coder Support Package for PyTorch and LiteRT Models (R2026a+).
## Critical: API Routing
Two completely different pipelines exist. Choose based on the goal:
| | Deep Learning Toolbox path (editable dlnetwork) | MATLAB Coder Support Package for PyTorch and LiteRT Models (code generation) |
|---|---|---|
| Load | `importNetworkFromPyTorch` | `loadPyTorchExportedProgram` |
| Object | `dlnetwork` | `PyTorchExportedProgram` |
| Inference | `predict(net, dlarray(...))` | `invoke(model, input)` |
| Codegen | `coder.loadDeepLearningNetwork` + `DeepLearningConfig` | Direct `codegen` with entry-point |
| Data layout (2-D images) | HWCN (MATLAB convention) | NCHW (PyTorch convention preserved) |
| Since | R2022b | R2026a |
| Use for | Quantization, compression, transfer learning (DLT tools) | C/C++/CUDA code generation |
**Rule:** If the goal is to keep the model's source of truth in PyTorch and
generate code from it, use the MATLAB Coder Support Package for PyTorch and
LiteRT Models path (`loadPyTorchExportedProgram` + `invoke`). Do not use
`importNetworkFromPyTorch`, `coder.loadDeepLearningNetwork`, or `dlarray` on
this path — that path converts the PyTorch model to a `dlnetwork` first.
**If the user needs quantization (`dlquantizer`), projection, pruning, or
`exportNetworkToSimulink`** and is OK with translating the model to a
`dlnetwork` using the Deep Learning Toolbox, then route to the
`matlab-deploy-embedded-ai` skill (Pattern 1), which covers
`dlnetwork`-based workflows.
## Export PyTorch Model (if user doesn't have .pt2 yet)
If the user has a PyTorch model but hasn't exported it to `.pt2` format, use
the `export-pytorch-models` reference for the full `torch.export` workflow
(covers HuggingFace, torchvision, timm, torchbench sources and known serialization issues).
## Load and Inspect
```matlab
model = loadPyTorchExportedProgram("model.pt2");
summary(model)
inSpecs = model.inputSpecifications;
outSpecs = model.outputSpecifications;
```
Use `inputSpecifications` to determine the expected input shape and type.
The model preserves PyTorch's NCHW layout — see `pytorch-data-layout.md`.
## Entry-Point Pattern
The standard entry-point for PyTorch model codegen:
```matlab
function out = mInvoke(networkPath, input) %#codegen
net = loadPyTorchExportedProgram(networkPath);
out = invoke(net, input);
end
```
## Code Generation (MEX, Library, Executable)
For the full codegen workflow (MEX generation, verification, library/executable
targets, `coder.Constant` usage, and `coder.DeepLearningConfig` notes), see
`codegen-workflow.md`.
Quick reference — CPU MEX:
```matlab
cfg = coder.config("mex");
input = ones(1, 3, 224, 224, "single"); % NCHW — match inputSpecifications
codegen -config cfg -args {coder.Constant("model.pt2"), input} mInvoke
```
## Common Mistakes
| Mistake | Why It's Wrong | Correct Approach |
|---------|---------------|-----------------|
| `importNetworkFromPyTorch("model.pt2")` on this skill's codegen path | Returns a `dlnetwork` (DLT path) — cannot use `invoke`, and codegen from a `dlnetwork` follows a different pipeline than the one this skill documents | Use `loadPyTorchExportedProgram("model.pt2")` for `.pt2` codegen. If a `dlnetwork` is required for quantization/projection/pruning/`exportNetworkToSimulink`, route to `matlab-deploy-embedded-ai` |
| `coder.loadDeepLearningNetwork(...)` | DLT codegen API, not compatible with PyTorchExportedProgram | Use `loadPyTorchExportedProgram` in the entry-point directly |
| `predict(net, dlarray(x, 'BCSS'))` | DLT inference API with dlarray wrapping | `invoke(model, x)` — no dlarray needed |
| `coder.DeepLearningConfig('mkldnn')` | Ignored by the MATLAB Coder Support Package for PyTorch and LiteRT Models path | Optional — has no effect on PyTorchExportedProgram codegen |
| Input shape `[224, 224, 3]` (HWC) from `imread` | PyTorchExportedProgram preserves NCHW | Permute to `[1, 3, 224, 224]` (check `inputSpecifications`) |
| Saving to `.mat` before codegen | Unnecessary intermediate step | Pass .pt2 path via `coder.Constant` directly |
| Omitting `coder.Constant` for model path | Codegen needs model path at compile time | Always wrap model path: `coder.Constant("model.pt2")` |
| Using `classify()` on the model | `classify` is for `dlnetwork`/`SeriesNetwork`, not `PyTorchExportedProgram` | Use `invoke(model, input)` |
## Conventions
- Always use `loadPyTorchExportedProgram` for `.pt2` code generation workflows
- PyTorch models use NCHW layout — do not feed HWC data without permuting
- Do NOT use `importNetworkFromPyTorch`, `coder.loadDeepLearningNetwork`, `dlarray`, `predict`, or `classify` on this skill's `.pt2` codegen path. If the user needs a `dlnetwork` for quantization/projection/pruning or `exportNetworkToSimulink` prior to codegen, route to the `matlab-deploy-embedded-ai` skill
- For shared conventions (MEX-first, `coder.Constant`, verify before lib), see `codegen-workflow.md`
## Detailed References
- `codegen-workflow.md` — Shared codegen steps: MEX generation, verification,
library/executable targets, `coder.Constant`, `coder.DeepLearningConfig`.
- `pytorch-api-reference.md` — Full API signatures and properties for
`loadPyTorchExportedProgram` and `PyTorchExportedProgram`.
- `pytorch-numeric-verification.md` — MATLAB Engine for Python pattern for
comparing PyTorch vs MATLAB outputs.
- `pytorch-data-layout.md` — NCHW vs HWC layout explanation and permutation
patterns for PyTorch models.
- `pytorch-supported-models.md` — Model constraints, PyTorch version requirements,
execution modes, and troubleshooting.
----
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 |