Skip to content

matlab/agent-skills-playground / demos/embedded-ai-deployment/skills/embedded-ai-deployment

embedded-ai-deployment

Verified

Deploy AI models to embedded hardware using MathWorks tools (MATLAB, Simulink, Embedded Coder). Covers two workflow patterns: (1) MathWorks-native or 3P-imported models rebuilt as dlnetwork for lean hardware (Cortex-M, DSP), (2) direct C/C++ code generation from PyTorch and LiteRT models for high-performance hardware (Cortex-A, x86, GPU). Trigger when: user wants to deploy AI to embedded targets; generate C/CUDA from neural networks; compress AI models for MCU/DSP; 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, importNetworkFromPyTorch, 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 & nnet.layer.Acceleratable
    %#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. Inherit from `nnet.layer.Acceleratable` (codegen compatibility mixin, R2022a+)
3. `%#codegen` pragma on the classdef
4. `predict()` uses only Coder-safe operations

**Must NOT use in predict():**
- Cell arrays
- Dynamic dispatch (`feval`, `str2func`)
- Object handles
- `try/catch`
- Format-string switches or runtime dimension queries (`dims(dlX)`)
- `struct` / `subsref` for dynamic indexing

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

If you need a custom activation not available as a built-in layer:

| Activation | Coder-Safe Expression |
|---|---|
| SiLU / Swish | `X .* (1 ./ (1 + exp(-X)))` |
| GELU | `X .* 0.5 .* (1 + tanh(sqrt(2/pi).*(X + 0.044715.*X.^3)))` |
| RMSNorm | `X ./ sqrt(mean(X.^2, 1) + 1e-6)` |
| Mish | `X .* tanh(log(1 + exp(X)))` |
| HardSwish | `X .* min(max(X + 3, 0), 6) / 6` |

Example custom activation layer:
```matlab
classdef SiLULayer < nnet.layer.Layer & nnet.layer.Acceleratable
    %#codegen
    methods
        function layer = SiLULayer(name)
            layer.Name = name;
        end
        function Z = predict(~, X)
            Z = X .* (1 ./ (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)
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 322kUpdated 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: 198kUpdated 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: 119kUpdated 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