Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-deploy-embedded-ai

matlab-deploy-embedded-ai

Verified

Deploy AI models to embedded hardware using MathWorks tools (MATLAB, Simulink, Embedded Coder). Covers two workflow patterns: (1) MathWorks-native or imported models rebuilt as dlnetwork for lean hardware, (2) direct C/C++ code generation from PyTorch and LiteRT models. Both patterns support all targets (Cortex-M/A/R, x86, GPU). Trigger when: user wants to deploy AI to embedded targets; generate C/CUDA from neural networks; compress AI models for MCU; integrate AI in Simulink for system-level simulation; import PyTorch/ONNX/TensorFlow models for embedded deployment; optimize AI for resource-constrained hardware; or use loadPyTorchExportedProgram, loadLiteRTModel, importNetworkFromPyTorch, importNetworkFromONNX, importNetworkFromTensorFlow, importNetworkFromKeras, dlquantizer, exportNetworkToSimulink, or Embedded Coder with AI models.

references/pattern2/coder-configuration.md

View on GitHub
# MATLAB Coder Configuration for PyTorch Support Package

## Minimal Working Configuration

The absolute minimum to generate C from a PyTorch model via the support package:

```matlab
cfg = coder.config('lib');
cfg.TargetLang = 'C';
cfg.DeepLearningConfig = coder.DeepLearningConfig('none');

inputType = coder.typeof(single(zeros(inputShape)), inputShape, false(size(inputShape)));
codegen -config cfg predict_fn -args {inputType}
```

## Full Production Configuration

Expert-reviewed settings validated across 8+ models with measured 3x+ speedup.

```matlab
%% 1. Embedded Coder config (not plain coder.config('lib'))
cfg = coder.config('lib', 'ecoder', true);
cfg.TargetLang = 'C';
cfg.GenCodeOnly = true;   % true unless cross-compiler is installed

%% 2. Deep learning config -- 'none' for portable C without runtime deps
%%    CRITICAL: Without this, codegen may try to use CUDNN/TensorRT/oneDNN
dlcfg = coder.DeepLearningConfig('none');
cfg.DeepLearningConfig = dlcfg;

%% 3. Hardware target (choose one based on your deployment)
%    See "Target-Specific Configurations" section below

%% 4. SIMD -- generates vectorized code for ARM
%%    CRITICAL: Property of cfg, NOT dlcfg! Common source of confusion.
cfg.InstructionSetExtensions = 'Neon v7';   % For ARM Cortex-A

%% 5. OpenMP -- multi-core acceleration
cfg.EnableOpenMP = true;    % For multi-core (Cortex-A) -- measured 3x speedup
% cfg.EnableOpenMP = false; % For single-core (Cortex-M)

%% 6. Weight storage -- separate binary files
cfg.LargeConstantThreshold = 0;  % Weights in .bin files, not inline in .c
% Result: 8K lines of logic vs 63K lines with embedded constants

%% 7. Memory and optimization settings
% cfg.SupportNonFinite = false;         % Only set if model guaranteed no NaN/Inf
cfg.PreserveVariableNames = 'None';     % Smaller generated code
cfg.InlineBetweenUserFunctions = 'Always';
cfg.InlineBetweenMathWorksFunctions = 'Always';
cfg.EnableMemcpy = true;
cfg.MemcpyThreshold = 64;
cfg.BuildConfiguration = 'Faster Runs';
cfg.PurelyIntegerCode = false;          % Need float for DNN

%% 8. Stack constraints for MCU
cfg.StackUsageMax = 4096;  % Ask user; default 4096 for Cortex-M if unknown
```

## DeepLearningConfig Target Libraries

| Value | Use Case | Dependencies |
|-------|----------|-------------|
| `'none'` | **Portable C, bare-metal MCU, any CPU** | None -- fully self-contained |
| `'mkldnn'` | Intel CPUs with AVX2 | Intel oneDNN library |
| `'arm-compute'` | ARM Cortex-A with NEON | ARM Compute Library |
| `'cudnn'` | NVIDIA GPU (GPU Coder) | cuDNN |
| `'tensorrt'` | NVIDIA GPU optimized (GPU Coder) | TensorRT |

**For embedded deployment:** Always use `'none'` combined with `InstructionSetExtensions` for SIMD.

## Target-Specific Configurations

### Bare-Metal MCU (STM32, Cortex-M)

```matlab
cfg = coder.config('lib', 'ecoder', true);
cfg.TargetLang = 'C';
cfg.HardwareImplementation.ProdHWDeviceType = 'ARM Compatible->ARM Cortex-M';
cfg.HardwareImplementation.ProdBitPerFloat = 32;
cfg.HardwareImplementation.ProdBitPerDouble = 64;
cfg.EnableOpenMP = false;       % Single-core -- OpenMP adds overhead
cfg.StackUsageMax = 4096;       % Size from target's SRAM budget; check codegen report
% cfg.SupportNonFinite = false; % Only set if model guaranteed no NaN/Inf
cfg.DeepLearningConfig = coder.DeepLearningConfig('none');
```

### Linux SBC (Raspberry Pi, Cortex-A)

```matlab
cfg = coder.config('lib', 'ecoder', true);
cfg.TargetLang = 'C';
try
    cfg.Hardware = coder.hardware('Raspberry Pi');
catch
    fprintf('Raspberry Pi support package not installed -- using generic ARM.\n');
    cfg.HardwareImplementation.ProdHWDeviceType = 'ARM Compatible->ARM Cortex-A';
end
cfg.InstructionSetExtensions = 'Neon v7';  % 128-bit SIMD, 4x float32
cfg.EnableOpenMP = true;                    % Quad-core -- measured 3x speedup
cfg.LargeConstantThreshold = 0;            % Weights in .bin files
cfg.DeepLearningConfig = coder.DeepLearningConfig('none');
```

### Desktop Validation (x86-64)

```matlab
cfg = coder.config('lib');
cfg.TargetLang = 'C';
cfg.EnableOpenMP = true;
cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn');  % Intel optimized
```

## InstructionSetExtensions by Target

| Target | Value | Notes |
|--------|-------|-------|
| ARM Cortex-A (RPi) | `'Neon v7'` | 128-bit SIMD, 4x float32 |
| Intel x86-64 | `'SSE'`, `'SSE4.1'`, `'AVX'`, `'AVX2'`, `'FMA'`, `'AVX512F'` | Match target CPU |
| ARM Cortex-M | Use `CodeReplacementLibrary = 'ARM Cortex-M'` instead | Different mechanism |

## MEX Configuration (Host Validation Before Deploying)

Generate a MEX function first to validate on the host machine before cross-compiling:

```matlab
mexCfg = coder.config('mex');
mexCfg.TargetLang = 'C';
mexCfg.SIMDAcceleration = 'full';   % Best MEX performance
mexCfg.DeepLearningConfig = coder.DeepLearningConfig('none');

codegen -config mexCfg predict_fn -args {inputType}
% Then call: predict_fn_mex(testInput) to validate
```

## Common Configuration Mistakes

### Mistake 1: InstructionSetExtensions on wrong object
```matlab
% WRONG -- dlcfg does not have this property
dlcfg.InstructionSetExtensions = 'Neon v7';   % ERROR: Unrecognized property

% CORRECT -- property of cfg (EmbeddedCodeConfig)
cfg.InstructionSetExtensions = 'Neon v7';
```

### Mistake 2: Invalid hardware names
```matlab
% WRONG -- not valid Embedded Coder hardware names
cfg.Hardware = coder.hardware('ARM Cortex-A embedded platform');
cfg.Hardware = coder.hardware('ARM Cortex-A');
cfg.Hardware = coder.hardware('STM32F746G-Discovery');  % only if support pkg installed

% CORRECT -- valid names (require support packages)
cfg.Hardware = coder.hardware('Raspberry Pi');
cfg.Hardware = coder.hardware('BeagleBone Black');
cfg.Hardware = coder.hardware('NVIDIA Jetson');
```

### Mistake 3: Forgetting DeepLearningConfig
```matlab
% WRONG -- default may try to use unavailable libraries
cfg = coder.config('lib');
codegen ...  % Fails with missing library errors

% CORRECT
cfg.DeepLearningConfig = coder.DeepLearningConfig('none');
```

### Mistake 4: Not separating weights from code
```matlab
% DEFAULT -- weights embedded inline in .c
% Result: 63,000 lines of float constants, slow compile

% CORRECT
cfg.LargeConstantThreshold = 0;
% Result: 8,000 lines of logic + separate .bin files
```

### Mistake 5: OpenMP on single-core MCU
```matlab
% WRONG for Cortex-M (single-core) -- adds unnecessary overhead
cfg.EnableOpenMP = true;

% CORRECT for Cortex-M
cfg.EnableOpenMP = false;
```

## Codegen Command Variants

```matlab
% Generate static library (most common for embedded)
codegen -config cfg predict_fn -args {inputType}

% Generate MEX for host testing
codegen -config mexCfg predict_fn -args {inputType}

% Generate code only (no compile -- for cross-compilation)
cfg.GenCodeOnly = true;
codegen -config cfg predict_fn -args {inputType}

% Specify output directory
codegen -config cfg predict_fn -args {inputType} -d codegen_output
```


Copyright 2026 The MathWorks, Inc.
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 319kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall