matlab-deploy-ai-model
>
git clone --depth 1 https://github.com/matlab/matlab-agentic-toolkit /tmp/matlab-deploy-ai-model && cp -r /tmp/matlab-deploy-ai-model/skills-catalog/code-generation/matlab-deploy-ai-model ~/.claude/skills/matlab-deploy-ai-modelSKILL.md
# 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:**
```matlab
cfg = coder.config("mex");
codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint
```
**CUDA MEX (GPU acceleration):**
```matlab
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:
```matlab
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:
```matlab
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>
Import recorded driving sensor data (GPS, camera, lidar, actor tracks, lanes) into scenariobuilder.* objects (GPSData, CameraData, LidarData, ActorTrackData, Trajectory, laneData) and run preprocessing — synchronize, offset correction, crop, normalizeTimestamps, convertTimestamps. Also: compute actor tracks from lidar when no annotations exist, attach camera/lidar mounting + intrinsics, export to MAT/workspace/timetable/script. Use for raw driving dataset files (KITTI, nuScenes, Waymo, Pandaset, ROS/ROS2 bags, .mat, .csv, .mp4) or driving/vehicle/sensor logs that need wrapping. drivingLogAnalyzer (DLA) is OPT-IN ONLY — invoke only on explicit user request ('DLA', 'open in DLA', 'inspect/explore/analyze the recording') or reported sensor problem (sync drift, timestamp mismatch, overlay misalignment). NEVER auto-launch DLA after wrapping (Rule 0). For 'build scenario / export to RoadRunner / drivingScenario / OpenSCENARIO / Unreal / simulate', hand off to matlab-scenario-builder.
Generate driving scenes, scenarios, road surfaces, and 3D content from already-wrapped scenariobuilder.* sensor data (GPS, camera, lidar, actor tracks) using Scenario Builder for Automated Driving Toolbox. Use to BUILD, EXPORT, or AUGMENT a virtual scenario/scene/map: ego or actor trajectories, trajectory smoothing, OpenCRG road-surface extraction, 3D asset generation, static-object placement, point-cloud georeferencing + elevation, lane-based ego localization, sensor-fusion tracking, scenario-event extraction (cut-ins, hard brakes, near-misses, ADAS disengagements), or export to RoadRunner, drivingScenario, OpenDRIVE, OpenCRG, OpenSCENARIO, or Unreal Engine. Also: log-to-scenario, scenario harvesting, accident/near-miss reconstruction, SOTIF (ISO 21448) and ISO 26262 scenario coverage, USGS-aerial-lidar scene augmentation, traffic-sign placement from camera+lidar logs. NOT for raw-data import or multi-sensor sync/crop/offset/timestamp normalization — route those to matlab-driving-data-importer.
>
>
>
>
Build, modify, and diagram SimBiology models — API reference, helper functions, and layout patterns. Use when constructing or editing models programmatically or visually.