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/dnn-codegen-options.md
View on GitHub# DNN-Specific Codegen Options
Options that only apply to deep-learning inference codegen. For generic
codegen tuning (SIMD instruction sets, OpenMP multi-threading, MEX SIMD,
`OptimizeReductions`, the MATLAB Coder ↔ Simulink Coder naming duality),
see the `matlab-generate-code` skill.
**When to read this file:** you're generating code from a `.pt2` or
`.tflite` model and want to (a) route around third-party DL libraries,
(b) serialize weights to data files, or (c) understand DNN-inference
specifics of `SIMDAcceleration` on MEX.
## 1. Deep learning target library — different name per API
To use the plain-C codegen path (no third-party DL library backends like
cuDNN, MKL-DNN), disable the DL library backend:
```matlab
% MATLAB Coder
cfg.DeepLearningConfig = coder.DeepLearningConfig("none");
% Simulink Coder — set BOTH. SimDLTargetLibrary controls simulation
% (normal-mode / sim()); DLTargetLibrary controls slbuild-time code
% generation. They are independent parameters — setting one does NOT set
% the other, so setting only DLTargetLibrary leaves simulation still
% routing inference through the DL library.
set_param(modelName, "SimDLTargetLibrary", "none"); % simulation
set_param(modelName, "DLTargetLibrary", "none"); % code generation
```
Setting a target library routes inference through the library at runtime
and bypasses `InstructionSetExtensions`, OpenMP, and
`LargeConstantGeneration` — those parameters only affect generated source
code, not library calls. For plain-C DNN inference on host CPU or embedded
targets, `"none"` is the correct choice; the perf knobs from
the `matlab-generate-code` skill (SIMD, OpenMP, `OptimizeReductions`) then
take effect on the generated inference code.
## 2. Large DNN constants (weights) — `LargeConstantGeneration` (R2024a+)
Model weight constants can either be embedded directly in generated source
(`"KeepInSourceFiles"`) or written to binary data files that the generated
code loads at run time (`"WriteOnlyDNNConstantsToDataFiles"`, for constants
above `LargeConstantThreshold`, default 131072 bytes). Embedding blows up
compile time and binary size for larger models but has no filesystem
dependency; the data-files path is smaller and faster to compile but
requires the deployment target to have a filesystem.
Since R2024a, defaults vary by codegen path — only `ert.tlc` defaults to
the inline path:
| Path | Default |
|---|---|
| MATLAB Coder `coder.config("lib")` (ecoder or not) | `"WriteOnlyDNNConstantsToDataFiles"` |
| Simulink `grt.tlc` | `"WriteOnlyDNNConstantsToDataFiles"` |
| Simulink `ert.tlc` | `"KeepInSourceFiles"` — must flip to `"WriteOnlyDNNConstantsToDataFiles"` for the data-files path |
Same property name on both APIs. **Requires MATLAB R2024a or later** — the property does not exist in earlier releases.
```matlab
% MATLAB Coder (requires R2024a+)
cfg.LargeConstantGeneration = "WriteOnlyDNNConstantsToDataFiles";
% cfg.LargeConstantThreshold = 131072; % optional (bytes)
% Simulink Coder (requires R2024a+)
set_param(modelName, "MATLABDynamicMemAlloc", "on");
set_param(modelName, "LargeConstantGeneration", "WriteOnlyDNNConstantsToDataFiles");
```
The generated loader needs dynamic memory allocation at runtime. Defaults
vary by codegen path:
| Path | Property | Default |
|---|---|---|
| MATLAB Coder `coder.config("lib")` (ecoder or not) | `EnableDynamicMemoryAllocation` (boolean) | `true` |
| Simulink `grt.tlc` | `MATLABDynamicMemAlloc` (`"on"`/`"off"`) | `"on"` |
| Simulink `ert.tlc` | `MATLABDynamicMemAlloc` (`"on"`/`"off"`) | `"off"` — must flip to `"on"` |
`ert.tlc` (Embedded Coder) is the one where the flip is genuinely required —
a fresh model that switches to `ert.tlc` gets `MATLABDynamicMemAlloc = "off"`.
On the MATLAB Coder side use `cfg.EnableDynamicMemoryAllocation`, NOT
`cfg.MATLABDynamicMemAlloc` (that's the Simulink `set_param` name and throws
an unrecognized-property error on `coder.EmbeddedCodeConfig`).
Notes:
- Applies only to code that does NOT depend on third-party deep learning
libraries (the `loadPyTorchExportedProgram` / `loadLiteRTModel` codegen path
qualifies).
- Non-DNN constants are always inlined in source, regardless of size.
- The binary data files land in the code generation folder. If you relocate
them at deploy time, set the `CODER_DATA_PATH` environment variable to the
new location before running the generated code.
- The deployment target must have a filesystem so the generated code can read
the data files at runtime — not suitable for bare-metal MCU targets. For
bare-metal, use `"KeepInSourceFiles"` (already the default on `ert.tlc`;
must be set explicitly on MATLAB Coder and `grt.tlc`) (see
`matlab-deploy-embedded-code`).
## 3. MEX SIMD ceiling (DNN-inference context)
Generic `SIMDAcceleration` values are documented in the `matlab-generate-code` skill.
The DNN-inference-specific note:
**MEX SIMD is capped at AVX2** via `"Full"` — there is no AVX512 option for
MEX, even on AVX512-capable hosts. For AI model MEX inference where the user
wants maximum SIMD throughput, `"Full"` is the ceiling. If a user needs AVX512
for their DNN inference, ask whether generating a library
(`coder.config("lib")`) instead of a MEX is acceptable — MEX and lib serve
different purposes (host-side iteration vs. deployable artifact), so don't
switch on their behalf.
## DNN-Inference Quick Recipe
Plain-C DNN inference with maximum vectorization + multi-threading (Intel/AMD
host, Embedded Coder available):
**Simulink:**
```matlab
set_param(modelName, "SystemTargetFile", "ert.tlc");
set_param(modelName, "ProdHWDeviceType", "Intel->x86-64 (Windows64)");
% set_param(modelName, "ProdHWDeviceType", "Intel->x86-64 (Linux 64)"); % Linux host
set_param(modelName, "InstructionSetExtensions", "AVX512F"); % see generic ref for SIMD ladder
set_param(modelName, "OptimizeReductions", "on");
set_param(modelName, "MultiThreadedLoops", "on");
set_param(modelName, "DLTargetLibrary", "none"); % plain-C path
% For large models, serialize weights to data files:
set_param(modelName, "MATLABDynamicMemAlloc", "on");
set_param(modelName, "LargeConstantGeneration", "WriteOnlyDNNConstantsToDataFiles");
```
**MATLAB Coder:**
```matlab
cfg = coder.config("lib", "ecoder", true);
cfg.HardwareImplementation.ProdHWDeviceType = "Intel->x86-64 (Windows64)";
% cfg.HardwareImplementation.ProdHWDeviceType = "Intel->x86-64 (Linux 64)"; % Linux host
cfg.InstructionSetExtensions = "AVX512F";
cfg.OptimizeReductions = "on";
cfg.EnableOpenMP = true;
cfg.DeepLearningConfig = coder.DeepLearningConfig("none"); % plain-C path
% EnableDynamicMemoryAllocation is true by default on ecoder configs.
cfg.LargeConstantGeneration = "WriteOnlyDNNConstantsToDataFiles";
```
For target-hardware-specific config (device selection,
`EnableDynamicMemoryAllocation`, `StackUsageMax`, `CodeReplacementLibrary`
catalog, PIL/SIL, hardware boards, target-connection), see the
`matlab-deploy-embedded-code` skill.
----
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: 321k | 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 |