$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-deploy-ai-modelGenerate 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. Cu
| 1 | # Generate C/C++/CUDA Code from an AI Model |
| 2 | |
| 3 | Generate deployable C/C++ or CUDA code from an AI model using MATLAB Coder or |
| 4 | GPU Coder. The workflow follows a common pattern regardless of model framework: |
| 5 | load, inspect, write entry-point, generate MEX, verify, then generate production code. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - User wants to generate C/C++/CUDA code from an AI model (PyTorch, LiteRT) |
| 10 | - User has a model file (.pt2, .tflite) and wants to load it into MATLAB |
| 11 | - User wants MEX acceleration for an AI model |
| 12 | - User wants to generate CUDA code or GPU-accelerated MEX from an AI model |
| 13 | - User wants to deploy an AI model to hardware |
| 14 | - User wants to verify AI model numerics between the source framework and MATLAB |
| 15 | |
| 16 | ## When NOT to Use |
| 17 | |
| 18 | - **General MATLAB Coder usage** (codegen syntax, config tuning, writing codegen-ready code) |
| 19 | - **Editable dlnetwork for Deep Learning Toolbox workflows** (quantization, compression, transfer learning) — use `importNetworkFromPyTorch` which returns a `dlnetwork` for PyTorch models |
| 20 | - **Training or fine-tuning** — this skill is for inference code generation only |
| 21 | |
| 22 | ## Supported Frameworks |
| 23 | |
| 24 | | Framework | Model format | Load function | Status | |
| 25 | |-----------|-------------|---------------|--------| |
| 26 | | PyTorch | `.pt2` | `loadPyTorchExportedProgram` | Supported (R2026a+) | |
| 27 | | LiteRT / TFLite | `.tflite` | `loadLiteRTModel` | Supported (R2026a+) | |
| 28 | |
| 29 | For PyTorch-specific details (API routing, entry-point pattern, export workflow, |
| 30 | data layout, common mistakes): see `references/pytorch-workflow.md`. |
| 31 | |
| 32 | ## Generic Workflow |
| 33 | |
| 34 | The code generation workflow follows the same steps for any framework: |
| 35 | |
| 36 | ### 1. Load and Inspect |
| 37 | |
| 38 | Load the model and check its input/output specifications to determine expected |
| 39 | shapes and types. |
| 40 | |
| 41 | ### 2. Write Entry-Point Function |
| 42 | |
| 43 | Create a codegen-compatible entry-point function that: |
| 44 | - Loads the model from a file path |
| 45 | - Runs inference on an input |
| 46 | - Returns the output |
| 47 | |
| 48 | The model file path must be wrapped with `coder.Constant` so it's known at |
| 49 | compile time. |
| 50 | |
| 51 | ### 3. Verify Numerics |
| 52 | |
| 53 | Compare MATLAB inference output against the source framework to confirm correct |
| 54 | loading. Use the same input data in both environments and compare with tolerance. |
| 55 | |
| 56 | ### 4. Generate MEX (First!) |
| 57 | |
| 58 | Always generate MEX before lib/exe to verify on the host machine: |
| 59 | |
| 60 | **CPU MEX:** |
| 61 | ```matlab |
| 62 | cfg = coder.config("mex"); |
| 63 | codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint |
| 64 | ``` |
| 65 | |
| 66 | **CUDA MEX (GPU acceleration):** |
| 67 | ```matlab |
| 68 | cfg = coder.gpuConfig("mex"); |
| 69 | codegen -config cfg -args {coder.Constant("model_file"), input} entryPoint |
| 70 | ``` |
| 71 | |
| 72 | ### 5. Verify MEX Output |
| 73 | |
| 74 | Compare MEX output against MATLAB reference using `matlab.unittest` with |
| 75 | tolerance: |
| 76 | |
| 77 | ```matlab |
| 78 | refOut = entryPoint("model_file", input); |
| 79 | mexOut = entryPoint_mex("model_file", input); |
| 80 | testCase = matlab.unittest.TestCase.forInteractiveUse; |
| 81 | testCase.verifyThat(mexOut, matlab.unittest.constraints.IsEqualTo(refOut, ... |
| 82 | 'Within', matlab.unittest.constraints.AbsoluteTolerance(single(1e-5)))); |
| 83 | ``` |
| 84 | |
| 85 | ### 6. Generate Library/Executable |
| 86 | |
| 87 | Once MEX is verified, generate production code: |
| 88 | |
| 89 | ```matlab |
| 90 | cfgLib = coder.config("lib"); |
| 91 | codegen -config cfgLib -args {coder.Constant("model_file"), input} entryPoint |
| 92 | ``` |
| 93 | |
| 94 | For DLL: `coder.config("dll")`. For executable: `coder.config("exe")`. |
| 95 | |
| 96 | **CUDA variants:** Replace `coder.config` with `coder.gpuConfig`. |
| 97 | |
| 98 | ### 7. Deploy to Hardware (Optional — requires Embedded Coder) |
| 99 | |
| 100 | For embedded deployment, use the same entry-point function with an Embedded Coder |
| 101 | configuration. See the `matlab-deploy-embedded-code` skill for ERT config, |
| 102 | hardware settings, PIL/SIL verification, and target-specific options. |
| 103 | Ask the user to install the skill if it is not installed |
| 104 | |
| 105 | ## Key Functions |
| 106 | |
| 107 | | Function | Purpose | Package | Since | |
| 108 | |----------|---------|---------|-------| |
| 109 | | `coder.Constant` | Make argument a compile-time constant | MATLAB Coder | R2011a | |
| 110 | | `coder.gpuConfig` | Create GPU (CUDA) code generation config | GPU Coder | R2017b | |
| 111 | | `codegen` | Generate code | MATLAB Coder | R2011a | |
| 112 | | `loadPyTorchExportedProgram` | Load .pt2 into MATLAB | MATLAB Coder Support Package for PyTorch and LiteRT Models | R2026a | |
| 113 | | `loadLiteRTModel` | Load .tflite into MATLAB | MATLAB Coder Support Package for PyTorch and LiteRT Models | R2026a | |
| 114 | |
| 115 | ## Conventions |