Docs/AI Model Integration/ONNX Upload & Component Generation

ONNX Upload & Component Generation

CoModel wraps AI models in ONNX format as black-box MTK components so they can co-simulate with physics models and FMU components on the same canvas. This article walks through the complete workflow from uploading an ONNX file to component generation.

Prerequisites

Before uploading, make sure your model meets these conditions:

  • The model is exported as .onnx (from PyTorch, TensorFlow, Keras, skl2onnx, and similar frameworks).
  • Input/output tensors are scalars or 1-D vectors, with data type float32 or float64.
  • You know what the model's inputs and outputs mean, so you can wire it correctly later.

Tip: CoModel treats an ONNX model as a black box: it reads only the input/output interfaces and does not parse the internal computation graph or weights. Train and export the model outside the platform.

Uploading an ONNX Model

  1. Sign in to CoModel and click AI Components in the left navigation.
  2. Click the Upload AI Model button at the top right of the page.
  3. In the dialog that opens:
    • select a local .onnx file;
    • enter a Display Name (unique under your account, e.g. Nonlinear Friction Surrogate);
    • enter an MTK Component Name (generated automatically from the display name when left empty; must be a valid Julia identifier);
    • choose a Category (optional; defaults to Uncategorized);
    • tick Enable lag filter if needed (see below).
  4. Click Upload to start asynchronous parsing.

ONNX upload dialog

When parsing completes, the system generates the corresponding MTK @component wrapper file and registers the component in your component library. The list shows the parsing status, file size, number of input/output ports, and more.

AI Components management page entry

ONNX File Constraints

The system validates the ONNX file at upload time. The table below lists the IO types and limits supported in the current version:

Tensor case Handling Notes
Scalar (total element count 1) Mapped to 1 RealInput / RealOutput e.g. shapes [], [1], [1,1]
1-D vector [N] (N > 1) Flattened into N scalar ports Port names like x_0, x_1, x_2
Higher-rank tensor (rank > 1 and not a degenerate batch=1) Rejected at parse time Not supported in v1, e.g. [2,3,4]
float32 / float64 Accepted Simulated internally as Float64
int, bool, string, etc. Rejected at parse time IO tensors must be floating point

Other platform constraints:

Item Limit Notes
File format .onnx ONNX format only
File size ≤ 100 MB Adjustable by an administrator
Per-user storage quota ≤ 500 MB Uploads fail once the quota is exceeded
Inference device CPU v1 supports the CPU execution provider only

How Component Generation Works

After successful parsing, CoModel generates a Julia wrapper file automatically. Its core logic:

  1. Loads the ONNX model with ONNXRunTime.load_inference;
  2. Packs the input scalars into a tensor and calls ONNX Runtime for inference;
  3. Unpacks the output tensor into scalars;
  4. Registers the inference function with @register_symbolic as an opaque Symbolics leaf node;
  5. Generates a standard @component that exposes RealInput / RealOutput causal signal ports.

Generated code for a typical SISO (single-input, single-output) static component:

using ONNXRunTime
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary.Blocks: RealInput, RealOutput
using Symbolics: @register_symbolic

const _ONNX_SESSION_42 = ONNXRunTime.load_inference(
    __COMODEL_AI_PATHS[42]; execution_provider = :cpu)
const _ONNX_INPUT_42  = first(_ONNX_SESSION_42.input_names)
const _ONNX_OUTPUT_42 = first(_ONNX_SESSION_42.output_names)

function _onnx_predict_42(x::Real)
    xin = reshape(Float32[Float64(x)], 1, 1)
    y   = _ONNX_SESSION_42(Dict(_ONNX_INPUT_42 => xin))
    return Float64(first(y[_ONNX_OUTPUT_42]))
end
@register_symbolic _onnx_predict_42(x::Real)

@component function LinearRegressor(; name)
    systems = @named begin
        input  = RealInput()
        output = RealOutput()
    end
    eqs = [output.u ~ _onnx_predict_42(input.u)]
    return System(eqs, t, [], []; name, systems)
end

Note: This code is generated automatically by the system and stored on the server; you never write it by hand. You can preview the generated code on the AI component details page.

Output Lag Filter

At upload time you can enable the output lag filter. When enabled, the component introduces a first-order low-pass state z and a time-constant parameter τ for each output:

τ·dz/dt + z = ŷ
output.u    = z

where ŷ is the raw ONNX prediction and output.u is the filtered output.

Mode Internal states Tunable parameter Use case
Static mode (default) None None Pure feedforward mapping, no phase lag
Lag filter mode One z per output τ Smoothing AI output noise, breaking algebraic loops

The default τ is 1e-2 s. If you want an output with no lag at all, use static mode; for light smoothing or to break an algebraic loop, use lag filter mode and keep the default value.

Managing AI Components

On the AI Components page you can perform the following operations on an uploaded model:

Operation Description
View details Shows the full metadata, the IO tensor list, the port mapping, and a preview of the generated code
Change category Reassigns the category the component belongs to in the editor component tree
Reparse Runs the parsing workflow again on a file that failed to parse
Delete Removes the ONNX file, the generated wrapper file, and the component library record; blocked if models still reference the component

Before deleting an AI component, the system checks whether any model or composite component under your account references it. If references exist, it returns the list of referencing items; remove the component from those models or delete the models first, then delete the component.

AI component details page

Naming Rules

To avoid conflicts, AI component naming follows these uniqueness constraints:

Name Unique within Constraint
Display name Your account Cannot be empty
MTK component name Your account (across all component types) Must be a valid Julia identifier, e.g. LinearRegressor

When the MTK component name is left empty, the system sanitizes the display name to generate one. If the sanitized name is invalid or conflicts with another component, you are prompted to change it at upload time.

Next Steps