matlab-deploy-ai-model

Generate C/C++ or CUDA code from PyTorch and LiteRT AI models using MATLAB Coder.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-deploy-ai-model
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-deploy-ai-model
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/code-generation/matlab-deploy-ai-model
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-deploy-ai-model

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Deploying a trained PyTorch or LiteRT model as standalone C/C++ or CUDA code requires navigating multiple MATLAB APIs, data layouts, and verification steps, and choosing the wrong import path (e.g., dlnetwork converters) breaks the code generation workflow.

Core Features & Use Cases

  • Model Loading and Code Generation: Load .pt2 files with loadPyTorchExportedProgram or .tflite files with loadLiteRTModel, write a codegen entry-point, and generate MEX, static/dynamic libraries, or executables with MATLAB Coder or GPU Coder.
  • Numeric Verification: Compare MATLAB inference output against the original PyTorch or LiteRT model using pyrun, then verify MEX output with matlab.unittest tolerances before generating production code.
  • Simulink Integration: Use the dedicated PyTorch ExportedProgram or LiteRT blocks from dlosslib, or a MATLAB Function block, with slbuild code generation support.
  • Use Case: You have a ResNet model exported as model.pt2 and need a CUDA-accelerated library for deployment. This Skill walks you through loading the model, writing the entry-point with coder.Constant, verifying MEX numerics, and generating the final CUDA library.

Quick Start

Ask your agent to generate a verified MEX and then a C++ static library from your PyTorch .pt2 or LiteRT .tflite model file using MATLAB Coder.

Frequently Asked Questions about matlab-deploy-ai-model

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I generate C++ code from a PyTorch model in MATLAB?

Export the model to .pt2 with torch.export, load it with loadPyTorchExportedProgram, write an entry-point function calling invoke, and run codegen with coder.config. Generate and verify a MEX first, then produce a static library, DLL, or executable.

What is the difference between loadPyTorchExportedProgram and importNetworkFromPyTorch?

loadPyTorchExportedProgram loads a .pt2 file for direct C/C++/CUDA code generation via invoke, preserving PyTorch's NCHW layout. importNetworkFromPyTorch returns an editable dlnetwork for Deep Learning Toolbox workflows like quantization, pruning, and transfer learning.

Can I generate CUDA code from a LiteRT .tflite model?

Yes. Load the .tflite file with loadLiteRTModel, write an entry-point calling invoke, and use coder.gpuConfig with codegen to produce CUDA MEX, libraries, or executables. GPU Coder is required for CUDA targets.

Does loadLiteRTModel support stateful LSTM models?

No, stateful operations are not supported in R2026a. Rewrite the model in Python as stateless with explicit hidden and cell state inputs/outputs, then emulate stateful behavior in MATLAB by feeding states back in a loop.

Why does my PyTorch model inference fail with a shape mismatch in MATLAB?

PyTorchExportedProgram preserves PyTorch's NCHW layout, while MATLAB images from imread are HWC. Check inputSpecifications for the expected shape and permute image data to NCHW, for example [1, 3, 224, 224], before calling invoke.

How do I use a PyTorch or LiteRT model in Simulink?

Add the dedicated PyTorch ExportedProgram or LiteRT block from the dlosslib library and set its ModelFilePath parameter; the block auto-detects input and output shapes. Alternatively, use a MATLAB Function block with loadPyTorchExportedProgram or loadLiteRTModel and invoke.