matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-deploy-embedded-ai
matlab-deploy-embedded-ai
VerifiedDeploy 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/pattern1/custom-layers-codegen.md
View on GitHub# Codegen-Compatible Custom Layers
When the native dlnetwork needs a layer that MATLAB does not provide as a built-in, write a
custom layer that satisfies MATLAB Coder requirements.
## Requirements for Codegen Compatibility
```matlab
classdef MyLayer < nnet.layer.Layer
%#codegen % Required pragma
methods
function Z = predict(layer, X)
% Only use MATLAB Coder-supported operations
Z = someCoderSafeOp(X);
end
end
end
```
**Must have:**
1. Inherit from `nnet.layer.Layer` (NOT `AutogeneratedFromPyTorch`)
2. `%#codegen` pragma on the classdef
3. `predict()` uses only Coder-safe operations
**Optional:** Inherit from `nnet.layer.Acceleratable` (R2022a+) to enable
`dlaccelerate`-based training/inference acceleration. This is a good practice when
the layer's `predict` method has no control flow or dynamic sizing (the same
constraints that make it codegen-compatible), but it is NOT required for code
generation. `Acceleratable` is for training speed via trace-based compilation, not
for C code generation.
**Restrictions for codegen-compatible predict():**
- No `try/catch`
- No variable-size cell arrays of non-uniform type (fixed-size heterogeneous cell arrays are supported)
- Handle objects cannot be entry-point function I/O arguments (supported within the layer internally)
`feval`, `str2func`, `dims`, and `struct` are all supported for code generation.
Consult MATLAB Coder documentation for the full list of supported language features.
## Verify Before Using
```matlab
% Check a single layer for codegen compatibility
checkLayer(myLayer, expectedInputSize, 'CheckCodegenCompatibility', true)
% Check the full network
analyzeNetworkForCodegen(net, TargetLibrary='none');
```
---
## ChannelShuffleLayer
Replaces autogenerated `view_view` layers. Used in ShuffleNet, MobileNet, and any model
with grouped convolutions that interleave channels.
**Operation:** Reshape channels into [groups, channels_per_group], transpose, reshape back.
```matlab
classdef ChannelShuffleLayer < nnet.layer.Layer
%#codegen
properties
NumGroups (1,1) double = 2
end
methods
function layer = ChannelShuffleLayer(numGroups, name)
arguments
numGroups (1,1) double = 2
name string = ""
end
layer.Name = name;
layer.NumGroups = numGroups;
end
function Z = predict(layer, X)
G = layer.NumGroups;
sz = size(X);
H = sz(1); W = sz(2); C = sz(3);
if numel(sz) >= 4, N = sz(4); else, N = 1; end
CperG = C / G;
Xr = reshape(X, [H, W, CperG, G, N]);
Xp = permute(Xr, [1, 2, 4, 3, 5]); % swap group and channel dims
Z = reshape(Xp, [H, W, C, N]);
end
end
end
```
**Usage:**
```matlab
net = replaceLayer(net, 'view_view_1', ChannelShuffleLayer(2, 'view_view_1'));
```
---
## ChannelSplitLayer
Replaces autogenerated `chunk` layers. Splits the channel dimension into two equal halves.
```matlab
classdef ChannelSplitLayer < nnet.layer.Layer
%#codegen
methods
function layer = ChannelSplitLayer(name)
arguments
name string = ""
end
layer.Name = name;
layer.NumOutputs = 2;
end
function [Z1, Z2] = predict(~, X)
C = size(X, 3);
half = C / 2;
Z1 = X(:, :, 1:half, :);
Z2 = X(:, :, half+1:C, :);
end
end
end
```
**Usage:**
```matlab
net = replaceLayer(net, 'chunk_1', ChannelSplitLayer('chunk_1'));
```
---
## PatchFlattenLayer (Vision Transformers)
Reshapes Conv2D patch embedding output from spatial format (SSCB) to sequence format (CTB).
```matlab
classdef PatchFlattenLayer < nnet.layer.Layer
%#codegen
methods
function layer = PatchFlattenLayer(name)
layer.Name = name;
end
function Z = predict(~, X)
sz = size(X); % [H, W, C, B] in SSCB format
% Permute to [C, W, H, B] for row-major patch ordering
X = permute(X, [3, 2, 1, 4]);
% Flatten spatial dims: [C, H*W, B]
Z = reshape(X, sz(3), [], sz(4));
Z = dlarray(Z, 'CTB');
end
end
end
```
---
## AddPositionEmbeddingLayer (Vision Transformers)
Adds a learnable positional embedding to the token sequence.
```matlab
classdef AddPositionEmbeddingLayer < nnet.layer.Layer
%#codegen
properties (Learnable)
Weights % [embedDim, numTokens]
end
methods
function layer = AddPositionEmbeddingLayer(posEmbed, name)
layer.Name = name;
layer.Weights = posEmbed;
end
function Z = predict(layer, X)
% X: [C, B, T] or [C, T, B] depending on format
posEmb = reshape(layer.Weights, size(layer.Weights,1), 1, []); % [C, 1, T]
Z = X + posEmb; % Broadcast over batch dimension
end
end
end
```
---
## Codegen-Safe Activation Expressions
**Prefer built-in layers when available.** MATLAB provides `geluLayer` (R2022a+)
and `swishLayer` (R2022a+, equivalent to SiLU). These are codegen-compatible and
do not require custom layer implementations.
For activations that have no built-in layer:
| Activation | Built-in? | Coder-Safe Expression (if custom needed) |
|---|---|---|
| SiLU / Swish | **Yes** — use `swishLayer` | — |
| GELU | **Yes** — use `geluLayer` | — |
| RMSNorm | No | `X ./ sqrt(mean(X.^2, 1) + 1e-6)` |
| Mish | No | `X .* tanh(log(1 + exp(X)))` |
| HardSwish | No | `X .* min(max(X + 3, 0), 6) / 6` |
Example custom activation layer (for activations without a built-in):
```matlab
classdef MishLayer < nnet.layer.Layer
%#codegen
methods
function layer = MishLayer(name)
layer.Name = name;
end
function Z = predict(~, X)
Z = X .* tanh(log(1 + exp(X)));
end
end
end
```
---
## Replacing Mean with GAP + Flatten (Graph Surgery)
The autogenerated `mean` layer computes global average pooling via `torch.mean` over spatial
dimensions. Replace with built-in layers, which requires network graph manipulation:
```matlab
meanLayerName = 'mean_1';
% Find downstream connection
conns = net.Connections;
destIdx = find(strcmp(conns.Source, [meanLayerName '/out1']) | ...
strcmp(conns.Source, meanLayerName));
fcDest = conns.Destination{destIdx(1)};
% Replace mean with GAP
gapLayer = globalAveragePooling2dLayer('Name', meanLayerName);
net = replaceLayer(net, meanLayerName, gapLayer);
% Insert flatten between GAP and FC
flatLayer = flattenLayer('Name', [meanLayerName '_flatten']);
net = addLayers(net, flatLayer);
net = disconnectLayers(net, meanLayerName, fcDest);
net = connectLayers(net, meanLayerName, [meanLayerName '_flatten']);
net = connectLayers(net, [meanLayerName '_flatten'], fcDest);
% CRITICAL: Reset FC OperationDimension
% The imported FC has OperationDimension set for PyTorch dimension ordering.
% After GAP+flatten, the tensor is in MATLAB's native ordering.
fcIdx = strcmp({net.Layers.Name}, 'fc');
fcOld = net.Layers(fcIdx);
fcNew = fullyConnectedLayer(fcOld.OutputSize, 'Name', fcOld.Name, ...
'Weights', fcOld.Weights, 'Bias', fcOld.Bias);
net = replaceLayer(net, fcOld.Name, fcNew);
```
---
## Generalizing to Unknown Custom Layers
When you encounter an unfamiliar autogenerated layer:
1. **Find the generated file:** Look in `+ModelName/LayerName.m` in the working directory
2. **Read the `predict()` method:** Strip away `tracedPyTorchFunction` dispatch and
format-handling scaffolding -- find the actual math underneath
3. **Write a replacement:** Implement only the known-layout math with static operations
4. **Test:** `checkLayer(myLayer, inputSize, 'CheckCodegenCompatibility', true)`
5. **Replace:** `net = replaceLayer(net, oldName, newLayer)`
6. **Verify:** Compare `predict()` outputs on 5+ random inputs (max error < 1e-5)
Copyright 2026 The MathWorks, Inc.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| 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: 196k | Updated | Claude CodeCodex | Verified | Install |
| 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: 118k | Updated | Claude CodeCodex | Verified | Install |
| 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: 118k | Updated | Claude CodeCodex | Verified | Install |