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/codegen-workflow.md
View on GitHub# Code Generation Workflow (Shared)
Common code generation steps for both PyTorch (.pt2) and LiteRT (.tflite)
models using the MATLAB Coder Support Package for PyTorch and LiteRT Models.
This reference covers the shared mechanics — MEX generation, verification,
library/executable generation, and conventions. For framework-specific details
(load functions, data layout, entry-point examples), see `pytorch-workflow.md`
or `litert-workflow.md`.
## Entry-Point Pattern
Both frameworks follow the same entry-point structure:
```matlab
function out = mInvoke(modelPath, input) %#codegen
model = <loadFunction>(modelPath);
out = invoke(model, input);
end
```
Where `<loadFunction>` is `loadPyTorchExportedProgram` (PyTorch) or
`loadLiteRTModel` (LiteRT).
**Why `coder.Constant` for the model path:** The model file path must be known
at compile time so the code generator can resolve the model at build time.
Pass it as `coder.Constant("model.pt2")` or `coder.Constant("model.tflite")`
in the `-args` list.
**Alternative:** Hardcode the path directly in the entry-point:
```matlab
function out = mInvoke(input) %#codegen
model = loadLiteRTModel("/path/to/model.tflite");
out = invoke(model, input);
end
```
## Generate MEX
Always generate and verify a MEX first before proceeding to library or
executable targets.
**CPU MEX:**
```matlab
cfg = coder.config("mex");
input = ones(<shape>, "single"); % match model's inputSpecifications
codegen -config cfg -args {coder.Constant("model_file"), input} mInvoke
```
**CUDA MEX (GPU acceleration):**
```matlab
cfg = coder.gpuConfig("mex");
input = ones(<shape>, "single"); % match model's inputSpecifications
codegen -config cfg -args {coder.Constant("model_file"), input} mInvoke
```
Key points:
- `coder.Constant` makes the model filename a compile-time constant
- Input shape and type must match `inputSpecifications` exactly
- PyTorch uses NCHW layout; LiteRT uses NHWC layout
## Verify MEX Output
Compare MEX output against MATLAB reference to confirm numeric equivalence:
```matlab
refOut = mInvoke("model_file", input);
mexOut = mInvoke_mex("model_file", input);
testCase = matlab.unittest.TestCase.forInteractiveUse;
testCase.verifyThat(mexOut, matlab.unittest.constraints.IsEqualTo(refOut, ...
'Within', matlab.unittest.constraints.AbsoluteTolerance(single(1e-5))));
```
## Generate Library/Executable
Once MEX is verified, generate production code:
**CPU targets:**
```matlab
cfgLib = coder.config("lib"); % static library
codegen -config cfgLib -args {coder.Constant("model_file"), input} mInvoke
```
Other CPU targets: `coder.config("dll")` (dynamic library),
`coder.config("exe")` (executable).
**CUDA targets:**
```matlab
cfgLib = coder.gpuConfig("lib"); % CUDA static library
codegen -config cfgLib -args {coder.Constant("model_file"), input} mInvoke
```
Other CUDA targets: `coder.gpuConfig("dll")`, `coder.gpuConfig("exe")`.
## coder.DeepLearningConfig
The MATLAB Coder Support Package for PyTorch and LiteRT Models path **ignores**
`coder.DeepLearningConfig`. It generates code directly from the model object
without requiring an external deep learning library (cuDNN, MKL-DNN, TensorRT).
Setting it won't cause errors but has no effect.
To explicitly disable DL library backends (ensuring plain-C codegen):
```matlab
cfg.DeepLearningConfig = coder.DeepLearningConfig("none");
```
See `dnn-codegen-options.md` for details on DL target library configuration.
## Conventions
- Always check `inputSpecifications` for correct input shape and type
- Always generate MEX first, verify numerics, then proceed to lib/exe
- Always use `coder.Constant` for the model file path argument
- Input data is typically single-precision (check `inputSpecifications`)
- Do NOT use `coder.loadDeepLearningNetwork` or `dlarray` on this path
- For generic performance tuning (SIMD, OpenMP), see the `matlab-generate-code` skill
- For DNN-inference-specific tuning (weight serialization, DLTargetLibrary),
see `dnn-codegen-options.md`
----
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: 319k | 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: 196k | 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: 118k | 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 |