Generate C/C++/CUDA Code from an AI Model
Generate deployable C/C++ or CUDA code from an AI model using MATLAB Coder or
GPU Coder. The workflow follows a common pattern regardless of model framework:
load, inspect, write entry-point, generate MEX, verify, then generate production code.
When to Use
- User wants to generate C/C++/CUDA code from an AI model (PyTorch, LiteRT)
- User has a model file (.pt2, .tflite) and wants to load it into MATLAB
- User wants MEX acceleration for an AI model
- User wants to generate CUDA code or GPU-accelerated MEX from an AI model
- User wants to deploy an AI model to hardware
- User wants to use a PyTorch or LiteRT model in Simulink (simulation or code generation)
- User wants to verify AI model numerics between the source framework and MATLAB
When NOT to Use
- General MATLAB Coder usage (codegen syntax, config tuning, writing codegen-ready code)
- Editable dlnetwork for Deep Learning Toolbox workflows (quantization, compression, transfer learning) — use
importNetworkFromPyTorch (PyTorch), importNetworkFromTensorFlow (SavedModel), or importNetworkFromKeras (.keras/.h5) which return a dlnetwork. For deployment of an editable dlnetwork with model compression (INT8 quantization via dlquantizer, pruning, projection) or exportNetworkToSimulink workflows — use matlab-deploy-embedded-ai (Pattern 1).
- Training or fine-tuning — this skill is for inference code generation only
Supported Frameworks
| Framework |
Model format |
Load function |
Status |
| PyTorch |
.pt2 |
loadPyTorchExportedProgram |
Supported (R2026a+) |
| LiteRT / TFLite |
.tflite |
loadLiteRTModel |
Supported (R2026a+) |
For PyTorch-specific details (API routing, entry-point pattern, export workflow,
data layout, common mistakes): see references/pytorch-workflow.md.
For LiteRT-specific details (API routing, entry-point pattern, variable-size inputs,
Simulink integration, conventions): see references/litert-workflow.md.
For converting TensorFlow/Keras/.h5 to .tflite: see
references/tensorflow-to-litert-conversion.md.
Generic Workflow
The code generation workflow follows the same steps for any framework:
1. Load and Inspect
Load the model and check its input/output specifications to determine expected
shapes and types.
2. Write Entry-Point Function
Create a codegen-compatible entry-point function that:
- Loads the model from a file path
- Runs inference on an input
- Returns the output
The model file path must be wrapped with coder.Constant so it's known at
compile time.
3. Verify Numerics
Compare MATLAB inference output against the source framework to confirm correct
loading. Use the same input data in both environments and compare with tolerance.
4. Generate MEX (First!)
Always generate MEX before lib/exe to verify on the host machine:
CPU MEX:
cfg = coder.config("mex");
codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint
CUDA MEX (GPU acceleration):
cfg = coder.gpuConfig("mex");
codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint
For CPU MEX SIMD acceleration (SIMDAcceleration = 'Full' for AVX2 on
Intel/AMD), see the matlab-generate-code skill. For the DNN-
inference-specific MEX AVX2 ceiling, see references/dnn-codegen-options.md.
5. Verify MEX Output
Compare MEX output against MATLAB reference using matlab.unittest with
tolerance:
refOut = entryPoint("model_file", input);
mexOut = entryPoint_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))));
6. Generate Library/Executable
Once MEX is verified, generate production code:
cfgLib = coder.config("lib");
cfgLib.TargetLang = "C++"; % set to "C++" for C++ output; default is "C"
codegen -config cfgLib -args {coder.Constant("model_file"), input} entryPoint
For DLL: coder.config("dll"). For executable: coder.config("exe").
CUDA variants: Replace coder.config with coder.gpuConfig.
Performance tuning:
- Generic knobs (SIMD instruction sets, reduction-loop vectorization,
multithreaded loops): see the
matlab-generate-code skill.
- MATLAB Coder ↔ Simulink Coder property naming duality and
slbuild
set_param patterns: see the matlab-deploy-embedded-code skill.
- DNN-inference-specific knobs (
DLTargetLibrary / DeepLearningConfig to
disable third-party DL libraries, LargeConstantGeneration to serialize
weights to data files): see references/dnn-codegen-options.md.
7. Use in Simulink
For Simulink integration, use the dedicated PyTorch ExportedProgram block from
dlosslib — set ModelFilePath to the .pt2 file and it auto-detects
input/output shapes. No entry-point function or coder.Constant needed.
Pre/post-processing can be done with Simulink blocks around the dedicated block.
If you need everything in a single block, use a MATLAB Function block with
loadPyTorchExportedProgram + invoke (same pattern as the entry-point, but
the model path is a string literal — no coder.Constant).
Both paths support slbuild code generation (requires fixed-step solver + ERT
or GRT target). See references/simulink-workflow.md for full details.
8. Deploy to Hardware (Optional — requires Embedded Coder)
For embedded deployment, use the same entry-point function with an Embedded Coder
configuration. See the matlab-deploy-embedded-code skill for ERT config,
hardware settings, PIL/SIL verification, and target-specific options.
Ask the user to install the skill if it is not installed
Key Functions
| Function |
Purpose |
Package |
Since |
coder.Constant |
Make argument a compile-time constant |
MATLAB Coder |
R2011a |
coder.gpuConfig |
Create GPU (CUDA) code generation config |
GPU Coder |
R2017b |
codegen |
Generate code |
MATLAB Coder |
R2011a |
loadPyTorchExportedProgram |
Load .pt2 into MATLAB |
MATLAB Coder Support Package for PyTorch and LiteRT Models |
R2026a |
loadLiteRTModel |
Load .tflite into MATLAB |
MATLAB Coder Support Package for PyTorch and LiteRT Models |
R2026a |
Conventions
- Always check the model's input specifications for correct input shape and type
- Always generate MEX first, verify, then proceed to lib/exe
- Always use
coder.Constant for the model file path argument
- Input data is typically single-precision (check model input specs to confirm)
- Do NOT use
importNetworkFromPyTorch, importNetworkFromTensorFlow, or importNetworkFromKeras in this skill — they return a dlnetwork on a different path. If the user needs a dlnetwork for quantization, projection, pruning, or exportNetworkToSimulink before code generation, route to matlab-deploy-embedded-ai instead
References
references/pytorch-workflow.md — Full PyTorch-specific workflow: API routing,
entry-point pattern, export guidance, common mistakes, and conventions. Consult
for any PyTorch/.pt2 model code generation task. Links to deeper PyTorch
references (API signatures, data layout, numeric verification, supported models).
references/export-pytorch-models.md — Exporting an eager-mode PyTorch model to
.pt2 with torch.export (upstream of loading). Consult when the user has a
PyTorch model but no .pt2 file yet, or hits torch.export SerializeError /
kwarg-mismatch errors. Links to pytorch-export-patterns.md (per-source
templates) and pytorch-export-gotchas.md (torch 2.11 serialization fixes).
references/simulink-workflow.md — Simulink integration: dedicated PyTorch
ExportedProgram block (Path A) vs MATLAB Function block (Path B), block mask
parameters, code generation config, and key differences from command-line codegen.
references/litert-workflow.md — Full LiteRT-specific workflow: API routing
(loadLiteRTModel → inputSpecifications → invoke), entry-point pattern,
variable-size input handling, Simulink integration, and conventions. Consult
for any LiteRT/.tflite model code generation task.
references/tensorflow-to-litert-conversion.md — Converting TensorFlow
SavedModel/Keras/.h5 to .tflite via the Python tf.lite.TFLiteConverter API.
Consult when the user has a TensorFlow model but no .tflite file yet.
references/litert-numeric-verification.md — Verifying MEX numerics against
MATLAB reference for LiteRT models (tolerance guidance, common mismatches).
references/codegen-workflow.md — Shared code generation steps for both
PyTorch and LiteRT: MEX generation, MEX verification, library/executable
targets, coder.Constant usage, and coder.DeepLearningConfig notes.
references/dnn-codegen-options.md — DNN-INFERENCE-SPECIFIC codegen
options: DLTargetLibrary / DeepLearningConfig('none') for the plain-C
DL path, LargeConstantGeneration for serializing large DNN weights to
data files, and MEX SIMD ceiling in a DNN-inference context. Read this
when the generic file's knobs need DNN-specific framing (e.g., "the MEX
SIMD cap matters because inference is the target").
See Also
matlab-generate-code — Generic MATLAB Coder tuning (SIMD instruction sets, OpenMP multi-threading, reduction-loop vectorization). Consult for performance options not specific to DNN inference.
matlab-deploy-embedded-ai — dlnetwork-based codegen with model compression (quantization, pruning, projection) and exportNetworkToSimulink workflows (Pattern 1). Use it when the source is an editable dlnetwork in MATLAB rather than a .pt2 / .tflite file.
matlab-optimize-gpu-codegen — CUDA-target codegen tuning (coder.gpuConfig, kernel fusion, memory-hierarchy options) when the deployment target is an NVIDIA GPU rather than CPU or embedded hardware.
Copyright 2026 The MathWorks, Inc.
1---2name: matlab-deploy-ai-model3description: >4 Generate C/C++ or CUDA code from an AI model (PyTorch, LiteRT) using5 MATLAB Coder or GPU Coder. Use when the user wants to integrate an AI model6 into an application with code generation as the end goal — generating MEX,7 CUDA MEX, static library, dynamic library, or executable — or using the model8 in Simulink for simulation and code generation. Covers PyTorch ExportedProgram9 (.pt2) via loadPyTorchExportedProgram and LiteRT (.tflite) via loadLiteRTModel10 (R2026a+).11 Keywords: PyTorch, torch, .pt2, ExportedProgram, loadPyTorchExportedProgram,12 invoke, codegen, MEX, CUDA, GPU, C, C++, deploy, AI model, deep learning model,13 LiteRT, TFLite, TensorFlow Lite, Simulink, slbuild, PyTorch ExportedProgram block,14 MATLAB Function block, dlosslib, loadLiteRTModel.15license: https://www.mathworks.com/content/dam/mathworks/license/pmrl/license.md16metadata:17 source: https://skillmd.com/skills/matlab/matlab-deploy-ai-model18 author: MathWorks19 version: "1.2"20---2122# Generate C/C++/CUDA Code from an AI Model2324Generate deployable C/C++ or CUDA code from an AI model using MATLAB Coder or25GPU Coder. The workflow follows a common pattern regardless of model framework:26load, inspect, write entry-point, generate MEX, verify, then generate production code.2728## When to Use2930- User wants to generate C/C++/CUDA code from an AI model (PyTorch, LiteRT)31- User has a model file (.pt2, .tflite) and wants to load it into MATLAB32- User wants MEX acceleration for an AI model33- User wants to generate CUDA code or GPU-accelerated MEX from an AI model34- User wants to deploy an AI model to hardware35- User wants to use a PyTorch or LiteRT model in Simulink (simulation or code generation)36- User wants to verify AI model numerics between the source framework and MATLAB3738## When NOT to Use3940- **General MATLAB Coder usage** (codegen syntax, config tuning, writing codegen-ready code)41- **Editable dlnetwork for Deep Learning Toolbox workflows** (quantization, compression, transfer learning) — use `importNetworkFromPyTorch` (PyTorch), `importNetworkFromTensorFlow` (SavedModel), or `importNetworkFromKeras` (`.keras`/`.h5`) which return a `dlnetwork`. For deployment of an editable `dlnetwork` with model compression (INT8 quantization via `dlquantizer`, pruning, projection) or `exportNetworkToSimulink` workflows — use `matlab-deploy-embedded-ai` (Pattern 1).42- **Training or fine-tuning** — this skill is for inference code generation only4344## Supported Frameworks4546| Framework | Model format | Load function | Status |47|-----------|-------------|---------------|--------|48| PyTorch | `.pt2` | `loadPyTorchExportedProgram` | Supported (R2026a+) |49| LiteRT / TFLite | `.tflite` | `loadLiteRTModel` | Supported (R2026a+) |5051For PyTorch-specific details (API routing, entry-point pattern, export workflow,52data layout, common mistakes): see `references/pytorch-workflow.md`.5354For LiteRT-specific details (API routing, entry-point pattern, variable-size inputs,55Simulink integration, conventions): see `references/litert-workflow.md`.56For converting TensorFlow/Keras/.h5 to `.tflite`: see57`references/tensorflow-to-litert-conversion.md`.5859## Generic Workflow6061The code generation workflow follows the same steps for any framework:6263### 1. Load and Inspect6465Load the model and check its input/output specifications to determine expected66shapes and types.6768### 2. Write Entry-Point Function6970Create a codegen-compatible entry-point function that:71- Loads the model from a file path72- Runs inference on an input73- Returns the output7475The model file path must be wrapped with `coder.Constant` so it's known at76compile time.7778### 3. Verify Numerics7980Compare MATLAB inference output against the source framework to confirm correct81loading. Use the same input data in both environments and compare with tolerance.8283### 4. Generate MEX (First!)8485Always generate MEX before lib/exe to verify on the host machine:8687**CPU MEX:**88```matlab89cfg = coder.config("mex");90codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint91```9293**CUDA MEX (GPU acceleration):**94```matlab95cfg = coder.gpuConfig("mex");96codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint97```9899For CPU MEX SIMD acceleration (`SIMDAcceleration = 'Full'` for AVX2 on100Intel/AMD), see the `matlab-generate-code` skill. For the DNN-101inference-specific MEX AVX2 ceiling, see `references/dnn-codegen-options.md`.102103### 5. Verify MEX Output104105Compare MEX output against MATLAB reference using `matlab.unittest` with106tolerance:107108```matlab109refOut = entryPoint("model_file", input);110mexOut = entryPoint_mex("model_file", input);111testCase = matlab.unittest.TestCase.forInteractiveUse;112testCase.verifyThat(mexOut, matlab.unittest.constraints.IsEqualTo(refOut, ...113 'Within', matlab.unittest.constraints.AbsoluteTolerance(single(1e-5))));114```115116### 6. Generate Library/Executable117118Once MEX is verified, generate production code:119120```matlab121cfgLib = coder.config("lib");122cfgLib.TargetLang = "C++"; % set to "C++" for C++ output; default is "C"123codegen -config cfgLib -args {coder.Constant("model_file"), input} entryPoint124```125126For DLL: `coder.config("dll")`. For executable: `coder.config("exe")`.127128**CUDA variants:** Replace `coder.config` with `coder.gpuConfig`.129130**Performance tuning:**131- Generic knobs (SIMD instruction sets, reduction-loop vectorization,132 multithreaded loops): see the `matlab-generate-code` skill.133- MATLAB Coder ↔ Simulink Coder property naming duality and `slbuild`134 `set_param` patterns: see the `matlab-deploy-embedded-code` skill.135- DNN-inference-specific knobs (`DLTargetLibrary` / `DeepLearningConfig` to136 disable third-party DL libraries, `LargeConstantGeneration` to serialize137 weights to data files): see `references/dnn-codegen-options.md`.138139### 7. Use in Simulink140141For Simulink integration, use the dedicated `PyTorch ExportedProgram` block from142`dlosslib` — set `ModelFilePath` to the `.pt2` file and it auto-detects143input/output shapes. No entry-point function or `coder.Constant` needed.144145Pre/post-processing can be done with Simulink blocks around the dedicated block.146If you need everything in a single block, use a MATLAB Function block with147`loadPyTorchExportedProgram` + `invoke` (same pattern as the entry-point, but148the model path is a string literal — no `coder.Constant`).149150Both paths support `slbuild` code generation (requires fixed-step solver + ERT151or GRT target). See `references/simulink-workflow.md` for full details.152153### 8. Deploy to Hardware (Optional — requires Embedded Coder)154155For embedded deployment, use the same entry-point function with an Embedded Coder156configuration. See the `matlab-deploy-embedded-code` skill for ERT config,157hardware settings, PIL/SIL verification, and target-specific options.158Ask the user to install the skill if it is not installed159160## Key Functions161162| Function | Purpose | Package | Since |163|----------|---------|---------|-------|164| `coder.Constant` | Make argument a compile-time constant | MATLAB Coder | R2011a |165| `coder.gpuConfig` | Create GPU (CUDA) code generation config | GPU Coder | R2017b |166| `codegen` | Generate code | MATLAB Coder | R2011a |167| `loadPyTorchExportedProgram` | Load .pt2 into MATLAB | MATLAB Coder Support Package for PyTorch and LiteRT Models | R2026a |168| `loadLiteRTModel` | Load .tflite into MATLAB | MATLAB Coder Support Package for PyTorch and LiteRT Models | R2026a |169170## Conventions171172- Always check the model's input specifications for correct input shape and type173- Always generate MEX first, verify, then proceed to lib/exe174- Always use `coder.Constant` for the model file path argument175- Input data is typically single-precision (check model input specs to confirm)176- Do NOT use `importNetworkFromPyTorch`, `importNetworkFromTensorFlow`, or `importNetworkFromKeras` in this skill — they return a `dlnetwork` on a different path. If the user needs a `dlnetwork` for quantization, projection, pruning, or `exportNetworkToSimulink` before code generation, route to `matlab-deploy-embedded-ai` instead177178## References179180- `references/pytorch-workflow.md` — Full PyTorch-specific workflow: API routing,181 entry-point pattern, export guidance, common mistakes, and conventions. Consult182 for any PyTorch/.pt2 model code generation task. Links to deeper PyTorch183 references (API signatures, data layout, numeric verification, supported models).184- `references/export-pytorch-models.md` — Exporting an eager-mode PyTorch model to185 `.pt2` with `torch.export` (upstream of loading). Consult when the user has a186 PyTorch model but no `.pt2` file yet, or hits `torch.export` `SerializeError` /187 kwarg-mismatch errors. Links to `pytorch-export-patterns.md` (per-source188 templates) and `pytorch-export-gotchas.md` (torch 2.11 serialization fixes).189- `references/simulink-workflow.md` — Simulink integration: dedicated PyTorch190 ExportedProgram block (Path A) vs MATLAB Function block (Path B), block mask191 parameters, code generation config, and key differences from command-line codegen.192- `references/litert-workflow.md` — Full LiteRT-specific workflow: API routing193 (`loadLiteRTModel` → `inputSpecifications` → `invoke`), entry-point pattern,194 variable-size input handling, Simulink integration, and conventions. Consult195 for any LiteRT/.tflite model code generation task.196- `references/tensorflow-to-litert-conversion.md` — Converting TensorFlow197 SavedModel/Keras/.h5 to `.tflite` via the Python `tf.lite.TFLiteConverter` API.198 Consult when the user has a TensorFlow model but no `.tflite` file yet.199- `references/litert-numeric-verification.md` — Verifying MEX numerics against200 MATLAB reference for LiteRT models (tolerance guidance, common mismatches).201- `references/codegen-workflow.md` — Shared code generation steps for both202 PyTorch and LiteRT: MEX generation, MEX verification, library/executable203 targets, `coder.Constant` usage, and `coder.DeepLearningConfig` notes.204- `references/dnn-codegen-options.md` — DNN-INFERENCE-SPECIFIC codegen205 options: `DLTargetLibrary` / `DeepLearningConfig('none')` for the plain-C206 DL path, `LargeConstantGeneration` for serializing large DNN weights to207 data files, and MEX SIMD ceiling in a DNN-inference context. Read this208 when the generic file's knobs need DNN-specific framing (e.g., "the MEX209 SIMD cap matters because inference is the target").210211## See Also212213- `matlab-generate-code` — Generic MATLAB Coder tuning (SIMD instruction sets, OpenMP multi-threading, reduction-loop vectorization). Consult for performance options not specific to DNN inference.214- `matlab-deploy-embedded-ai` — `dlnetwork`-based codegen with model compression (quantization, pruning, projection) and `exportNetworkToSimulink` workflows (Pattern 1). Use it when the source is an editable `dlnetwork` in MATLAB rather than a `.pt2` / `.tflite` file.215- `matlab-optimize-gpu-codegen` — CUDA-target codegen tuning (`coder.gpuConfig`, kernel fusion, memory-hierarchy options) when the deployment target is an NVIDIA GPU rather than CPU or embedded hardware.216217----218219Copyright 2026 The MathWorks, Inc.220221----