agentsclimarketplace

Matlab deploy ai model

Skill matlab/matlab-agentic-toolkit/skills-catalog/code-generation/matlab-deploy-ai-model

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. Currently covers PyTorch ExportedProgram (.pt2) via loadPyTorchExportedProgram; LiteRT support planned. Keywords: PyTorch, torch, .pt2, ExportedProgram, loadPyTorchExportedProgram, invoke, codegen, MEX, CUDA, GPU, C, C++, deploy, AI model, deep learning model, LiteRT, TFLite, TensorFlow Lite.From its SKILL.md

Install
npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-deploy-ai-model

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

What its file declares

Copied from the file, not written here

The file declares its own license as MathWorks BSD-3-Clause. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.

SKILL.md

6.2 KB, ~1.4k tokens by cl100k_base, as published. Nobody here has run it

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 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 which returns a dlnetwork for PyTorch models
  • Training or fine-tuning — this skill is for inference code generation only

Supported Frameworks

FrameworkModel formatLoad functionStatus
PyTorch.pt2loadPyTorchExportedProgramSupported (R2026a+)
LiteRT / TFLite.tfliteloadLiteRTModelSupported (R2026a+)

For PyTorch-specific details (API routing, entry-point pattern, export workflow, data layout, common mistakes): see references/pytorch-workflow.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

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");
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.

7. 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

FunctionPurposePackageSince
coder.ConstantMake argument a compile-time constantMATLAB CoderR2011a
coder.gpuConfigCreate GPU (CUDA) code generation configGPU CoderR2017b
codegenGenerate codeMATLAB CoderR2011a
loadPyTorchExportedProgramLoad .pt2 into MATLABMATLAB Coder Support Package for PyTorch and LiteRT ModelsR2026a
loadLiteRTModelLoad .tflite into MATLABMATLAB Coder Support Package for PyTorch and LiteRT ModelsR2026a

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 for code generation workflows — it returns dlnetwork for the DLT path

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).

See Also

  • matlab-deploy-embedded-code — Embedded Coder configuration, PIL/SIL verification, hardware targets

Copyright 2026 The MathWorks, Inc.


What ships with it: 12 files

55.2 KB alongside SKILL.md

Keep looking

Skills are one crate of 326,871. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.