Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Generate 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. This skill currently documents the PyTorch ExportedProgram (.pt2) workflow via loadPyTorchExportedProgram; LiteRT is already supported by the product (loa
.claude/skills/matlab-matlab-deploy-ai-model/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-03 | ✗→✓ | ▲ Improved | 20% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 38% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 44% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 65% | 0% |
| case-09 | ✗→✓ | ▲ Improved | 247% | 0% |
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.
importNetworkFromPyTorch which returns a dlnetwork for PyTorch models. 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).| 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.
The code generation workflow follows the same steps for any framework:
Load the model and check its input/output specifications to determine expected shapes and types.
Create a codegen-compatible entry-point function that:
The model file path must be wrapped with coder.Constant so it's known at compile time.
Compare MATLAB inference output against the source framework to confirm correct loading. Use the same input data in both environments and compare with tolerance.
Always generate MEX before lib/exe to verify on the host machine:
CPU MEX:
matlabcfg = coder.config("mex"); codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint
CUDA MEX (GPU acceleration):
matlabcfg = 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 references/codegen-performance-options.md. For the DNN- inference-specific MEX AVX2 ceiling, see references/dnn-codegen-options.md.
Compare MEX output against MATLAB reference using matlab.unittest with tolerance:
matlabrefOut = 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))));
Once MEX is verified, generate production code:
matlabcfgLib = 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:
multithreaded loops, MATLAB Coder ↔ Simulink Coder naming duality): see references/codegen-performance-options.md.
DLTargetLibrary / DeepLearningConfig todisable third-party DL libraries, LargeConstantGeneration to serialize weights to data files): see references/dnn-codegen-options.md.
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.
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
| 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 |
coder.Constant for the model file path argumentimportNetworkFromPyTorch for code generation workflows — it returns dlnetwork for the DLT pathreferences/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 PyTorchExportedProgram block (Path A) vs MATLAB Function block (Path B), block mask parameters, code generation config, and key differences from command-line codegen.
references/codegen-performance-options.md — GENERIC codegen tuning(not AI-specific). SIMD instruction sets (InstructionSetExtensions for lib/exe/slbuild, SIMDAcceleration for MEX), reduction-loop vectorization (OptimizeReductions), OpenMP multi-threading (EnableOpenMP MATLAB Coder / MultiThreadedLoops Simulink Coder), and the MATLAB Coder ↔ Simulink Coder property naming table.
references/dnn-codegen-options.md — DNN-INFERENCE-SPECIFIC codegenoptions: 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").
matlab-deploy-embedded-code — Embedded Coder configuration, PIL/SIL verification, hardware targetsmatlab-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.Copyright 2026 The MathWorks, Inc.
Other measured skills in the registry, with their headline benchmark lift.