Skip to content

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

matlab-deploy-ai-model

Verified

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 — or using the model in Simulink for simulation and code generation. Covers PyTorch ExportedProgram (.pt2) via loadPyTorchExportedProgram and LiteRT (.tflite) via loadLiteRTModel (R2026a+). Keywords: PyTorch, torch, .pt2, ExportedProgram, loadPyTorchExportedProgram, invoke, codegen, MEX, CUDA, GPU, C, C++, deploy, AI model, deep learning model, LiteRT, TFLite, TensorFlow Lite, Simulink, slbuild, PyTorch ExportedProgram block, MATLAB Function block, dlosslib, loadLiteRTModel.

references/pytorch-export-patterns.md

View on GitHub
# Export Patterns by Model Source

Six patterns covering the model-loading entry points encountered in
practice. Pick the one matching your source. Each pattern has a stable ID
(shown in its heading) — cross-references elsewhere in the skill use these
IDs rather than positional numbers.

## FULL_MODEL_PTH — Full model .pth (simplest case)

```python
import torch

model = torch.load('model.pth', map_location='cpu', weights_only=False)
model = model.cpu()
model.eval()
inputs = (torch.randn(1, 3, 224, 224, device='cpu'),)
exported = torch.export.export(model, inputs, strict=False)
torch.export.save(exported, 'model.pt2')
```

## HF_AUTOMODEL — HuggingFace models (most reliable)

Use `from_pretrained` — avoids pickle issues with SDPA attention and ensures correct architecture instantiation.

```python
import torch
from transformers import AutoModel

model = AutoModel.from_pretrained("bert-base-uncased", attn_implementation="eager")
model = model.cpu()
model.eval()
inputs = (torch.randint(0, 30522, (1, 128)),)
exported = torch.export.export(model, inputs, strict=False)
torch.export.save(exported, 'bert.pt2')
```

Key: `attn_implementation="eager"` prevents SDPA class which can't be exported.

## TORCHVISION — torchvision models

```python
import torch
import torchvision.models as models

model = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.DEFAULT)
model = model.cpu()
model.eval()
inputs = (torch.randn(1, 3, 224, 224, device='cpu'),)
exported = torch.export.export(model, inputs, strict=False)
torch.export.save(exported, 'mobilenet_v2.pt2')
```

## TIMM — timm models

```python
import torch
import timm

model = timm.create_model('tiny_vit_5m_224', pretrained=True)
model = model.cpu()
model.eval()
inputs = (torch.randn(1, 3, 224, 224, device='cpu'),)
exported = torch.export.export(model, inputs, strict=False)
torch.export.save(exported, 'tinyvit.pt2')
```

## STATE_DICT — State_dict-only .pth (requires knowing architecture)

If `torch.load()` returns a dict (not a Module), instantiate the architecture first:

```python
import torch
from torchvision.models import resnet18

model = resnet18()
state_dict = torch.load('resnet18.pth', map_location='cpu', weights_only=True)
model.load_state_dict(state_dict)
model = model.cpu()
model.eval()
inputs = (torch.randn(1, 3, 224, 224, device='cpu'),)
exported = torch.export.export(model, inputs, strict=False)
torch.export.save(exported, 'resnet18.pt2')
```

## TENSOR_WRAPPER — Models with dict inputs or dataclass outputs (`TensorOutputWrapper`)

Some models (especially HuggingFace transformers) have two torch.export
constraints that must be handled before `torch.export.export`:

1. **HF dataclass outputs don't round-trip** through `torch.export.save` /
   `torch.export.load` even when `transformers` is imported in the loader
   (`CausalLMOutputWithPast`, `Seq2SeqLMOutput`, etc.).
2. **`loadPyTorchExportedProgram` and other consumers only feed positional args** to
   the loaded exported program. If `example_inputs` is a dict (most HF
   models — `{input_ids: ...}`), exporting it as `kwargs=...` yields a
   keyword-only signature and the consumer fails with
   *"Ran into a kwarg keyword mismatch: Got the following keywords [] but
   expected ['input_ids']"*.

Wrap once to fix both:

```python
import torch
import torch.nn as nn

class TensorOutputWrapper(nn.Module):
    def __init__(self, model: nn.Module, kwarg_names: list[str] | None = None):
        super().__init__()
        self.model = model
        self.kwarg_names = kwarg_names  # set when example_inputs is a dict

    def forward(self, *args, **kwargs):
        if self.kwarg_names is not None and not kwargs:
            kwargs = dict(zip(self.kwarg_names, args))
            args = ()
        out = self.model(*args, **kwargs)
        # Flatten HF dataclass / dict / namedtuple to plain tensor(s)
        if isinstance(out, torch.Tensor):
            return out
        if hasattr(out, "to_tuple"):
            flat = tuple(t for t in out.to_tuple() if isinstance(t, torch.Tensor))
            return flat[0] if len(flat) == 1 else flat
        if isinstance(out, dict):
            flat = tuple(v for v in out.values() if isinstance(v, torch.Tensor))
            return flat[0] if len(flat) == 1 else flat
        if isinstance(out, (tuple, list)):
            flat = tuple(t for t in out if isinstance(t, torch.Tensor))
            return flat[0] if len(flat) == 1 else flat
        return out

from transformers import BertModel
module = BertModel.from_pretrained("bert-base-uncased", attn_implementation="eager")
module = module.cpu()
example_inputs = {"input_ids": torch.randint(0, 30522, (1, 128), device='cpu'),
                  "attention_mask": torch.ones(1, 128, dtype=torch.long, device='cpu')}
module.eval()

if isinstance(example_inputs, dict):
    kwarg_names = list(example_inputs.keys())
    args_tuple  = tuple(example_inputs.values())
elif isinstance(example_inputs, (tuple, list)):
    kwarg_names, args_tuple = None, tuple(example_inputs)
else:
    kwarg_names, args_tuple = None, (example_inputs,)

module = TensorOutputWrapper(module, kwarg_names=kwarg_names).eval()
with torch.no_grad():
    ep = torch.export.export(module, args_tuple, kwargs={}, strict=False)
torch.export.save(ep, "bert_wrapped.pt2")
```

----

Copyright 2026 The MathWorks, Inc.

----
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 321kUpdated 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: 197kUpdated 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