Onnxify.ModelGenerator
0.3.2
See the version list below for details.
dotnet add package Onnxify.ModelGenerator --version 0.3.2
NuGet\Install-Package Onnxify.ModelGenerator -Version 0.3.2
<PackageReference Include="Onnxify.ModelGenerator" Version="0.3.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Onnxify.ModelGenerator" Version="0.3.2" />
<PackageReference Include="Onnxify.ModelGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Onnxify.ModelGenerator --version 0.3.2
#r "nuget: Onnxify.ModelGenerator, 0.3.2"
#:package Onnxify.ModelGenerator@0.3.2
#addin nuget:?package=Onnxify.ModelGenerator&version=0.3.2
#tool nuget:?package=Onnxify.ModelGenerator&version=0.3.2
Onnxify.ModelGenerator
Onnxify.ModelGenerator is a Roslyn source generator that turns .onnx files in your project into typed Microsoft.ML.OnnxRuntime wrapper classes.
Why This Package Exists
Using ONNX Runtime directly is powerful, but repetitive:
- you have to load the model manually with
InferenceSession - you have to keep string-based input and output names in sync with the real ONNX signature
- you have to map tensors into
NamedOnnxValuecollections by hand - you have to remember which outputs should be disposed and when
Onnxify.ModelGenerator removes that plumbing. You add an ONNX model to your project, and the generator emits a small typed API around it:
- a model wrapper such as
SampleClassifierModel - an input contract such as
SampleClassifierModelInputs - an output contract such as
SampleClassifierModelOutputs - typed
Run(...)overloads with optionalRunOptions
Use the main Onnxify package instead when your goal is to inspect, build, or edit ONNX graphs themselves.
Install
dotnet add package Onnxify
dotnet add package Onnxify.ModelGenerator
dotnet add package Microsoft.ML.OnnxRuntime
Recommended csproj Setup
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Onnxify" Version="0.1.0" />
<PackageReference Include="Onnxify.ModelGenerator" Version="0.1.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.24.4" />
</ItemGroup>
<ItemGroup>
<OnnxModel Include="Models\sample-classifier.onnx"
OnnxifyModelNamespace="MyApp.Models"
OnnxifyModelClassName="SampleClassifier"
OnnxifyModelImportType="OnnxRuntimeInference" />
</ItemGroup>
</Project>
The packaged .targets file forwards OnnxModel items to Roslyn as additional files and keeps the model copied to the output directory by default.
Import Type
OnnxifyModelImportType controls which generated source shape is emitted for each OnnxModel item. The metadata value is case-insensitive and accepts one or more comma-separated values.
There are two import types:
OnnxRuntimeInferencegenerates a typed ONNX Runtime inference wrapper.TorchModulegenerates a graph-shaped TorchSharp module that reconstructs supported ONNX graphs as idiomatictorch.nn.Modulecode.
If OnnxifyModelImportType is omitted or empty, the generator uses OnnxRuntimeInference.
OnnxRuntimeInference
Use OnnxRuntimeInference when you want the default typed Microsoft.ML.OnnxRuntime inference wrapper:
<OnnxModel Include="Models\sample-classifier.onnx"
OnnxifyModelImportType="OnnxRuntimeInference" />
This mode is the right choice for production inference paths that should stay close to ONNX Runtime. It generates:
- a disposable model wrapper such as
SampleClassifierModel - input and output contract types
- typed
Run(...)overloads - model signature metadata through
Inputs,Outputs, andOutputNames - constructors for loading the model from the default project-relative path, a custom file path, or raw model bytes
OnnxRuntimeInference requires Microsoft.ML.OnnxRuntime in the consuming project. It does not try to reinterpret the graph as C# model code; it keeps ONNX Runtime as the execution engine and removes the repetitive session/input/output plumbing around it.
TorchModule
Use TorchModule when you want a graph-shaped TorchSharp module reconstructed from supported ONNX operators:
<OnnxModel Include="Models\sample-classifier.onnx"
OnnxifyModelImportType="TorchModule" />
This is the most distinctive import mode in Onnxify.ModelGenerator: it turns a compatible ONNX graph into a real TorchSharp module. Instead of treating the ONNX file only as an opaque runtime artifact, the generator rebuilds the supported graph structure as torch.nn.Module<Tensor, Tensor> code that can live naturally inside a TorchSharp application.
That is a big deal for .NET ML workflows. TorchModule gives you a bridge back from an exported ONNX artifact to an editable, composable TorchSharp model surface: you can inspect the generated module, compose it with handwritten TorchSharp code, move weights across compatible graph variants, and keep working in the TorchSharp ecosystem without manually rebuilding the model layer by layer.
TorchModule generates a <ModelName>TorchModule type with:
- private TorchSharp module fields for recognized module-shaped operators such as convolutions, linear layers, recurrent layers, normalization layers, pooling, and activations
- inline TorchSharp tensor expressions for supported primitive ONNX operators
- registered TorchSharp parameters for trainable float32 initializers
- registered buffers for non-parameter constants
- a TorchSharp
forward(...)method that follows the ONNX graph order LoadWeightsFromOnnx(string modelPath)andLoadWeightsFromOnnx(OnnxModel model)helpers for copying initializer values into the generated module
The weight loader is designed for practical model surgery. It canonicalizes the source graph before loading weights and can fall back from initializer names to deterministic canonical initializer indexes. That means structurally identical models can still transfer weights even when node or value names differ.
TorchModule requires TorchSharp in the consuming project. It currently supports ONNX graphs that can be reconstructed from the generator's supported operator set, with exactly one non-initializer runtime input and one graph output for the main module shape. Unsupported graphs report an OMG006 diagnostic during generation, so failures are visible at build time instead of surfacing later during inference.
This mode is especially useful when you want to:
- start from an ONNX model but continue working in TorchSharp
- inspect or adapt the model as C# module code
- load compatible ONNX weights into a TorchSharp module
- compose generated modules with handwritten TorchSharp layers or training pipelines
- keep a path from exported ONNX artifacts back into a trainable or editable .NET model representation
Generating Both APIs
Use both values when the same model should generate both APIs:
<OnnxModel Include="Models\sample-classifier.onnx"
OnnxifyModelImportType="OnnxRuntimeInference,TorchModule" />
This is a strong default while evaluating a model integration: use the ONNX Runtime wrapper for deployment-style inference and the TorchSharp module for experiments, adaptation, debugging, or weight transfer.
With the configuration above, the generator emits types like:
MyApp.Models.SampleClassifierModelMyApp.Models.SampleClassifierModelInputsMyApp.Models.SampleClassifierModelOutputs
When TorchModule is enabled, the generator also emits SampleClassifierModelTorchModule.
Example: Run Inference With SessionOptions And RunOptions
This is the most copy-paste-friendly shape for a consumer project:
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using MyApp.Models;
using var sessionOptions = new SessionOptions();
using var model = new SampleClassifierModel(sessionOptions);
var image = new DenseTensor<float>(
new float[1 * 3 * 16 * 16],
new[] { 1, 3, 16, 16 }
);
using var runOptions = new RunOptions();
runOptions.LogId = "image-classification";
using var outputs = model.Run(
input: image,
runOptions: runOptions
);
Tensor<float> prediction = outputs.Output;
Console.WriteLine($"Output tensor length: {prediction.Length}");
This example shows the most important runtime lifetimes:
SessionOptionsis disposable- the generated model wrapper is disposable because it owns
InferenceSession RunOptionsis disposable- the generated outputs wrapper is disposable because it owns ONNX Runtime output values
Example: Use The Generated Input Contract
If you prefer an object that mirrors the model signature, use the generated input class:
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using MyApp.Models;
using var model = new SampleClassifierModel();
using var runOptions = new RunOptions();
runOptions.LogId = "typed-inputs";
var inputs = new SampleClassifierModelInputs
{
Input = new DenseTensor<float>(
new float[1 * 3 * 16 * 16],
new[] { 1, 3, 16, 16 }
),
};
using var outputs = model.Run(
inputs: inputs,
runOptions: runOptions
);
Tensor<float> prediction = outputs.Output;
Console.WriteLine($"Output tensor length: {prediction.Length}");
This style becomes especially useful when the ONNX model has multiple inputs or optional inputs.
Disposal Guidance
Treat these objects as scoped resources:
SessionOptionsRunOptions- the generated model wrapper, for example
SampleClassifierModel - the generated outputs wrapper, for example
SampleClassifierModelOutputs
Prefer using var for all of them.
Also note that outputs.Output and outputs.Raw are tied to the lifetime of the output wrapper. Read or copy the data you need before leaving the using scope.
Generated API Notes
- The default constructor loads the model from
DefaultModelPath, resolved relative to the application output folder. - You can also construct the wrapper from a custom file path or raw model bytes.
- The generated wrapper exposes both
Run(<generated inputs>)andRun(..., RunOptions? runOptions)overloads. - Optional ONNX inputs become nullable properties on the generated input type and nullable parameters on the generated
Run(...)overloads. - The generated wrapper also exposes
Inputs,Outputs, andOutputNamesmetadata for runtime inspection. - Models that use external tensor data still require their sibling external-data files at deployment time.
Repository
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.3.7 | 0 | 6/27/2026 |
| 0.3.6 | 48 | 6/26/2026 |
| 0.3.5 | 94 | 6/23/2026 |
| 0.3.4 | 110 | 6/19/2026 |
| 0.3.3 | 104 | 6/15/2026 |
| 0.3.2 | 98 | 6/14/2026 |
| 0.3.1 | 93 | 6/12/2026 |
| 0.3.0 | 99 | 6/11/2026 |
| 0.2.0 | 104 | 6/8/2026 |
| 0.1.4 | 99 | 6/2/2026 |
| 0.1.3 | 107 | 6/1/2026 |
| 0.1.2 | 112 | 5/28/2026 |
| 0.1.1 | 104 | 5/20/2026 |
| 0.1.0 | 99 | 5/18/2026 |
| 0.0.0.15 | 101 | 5/18/2026 |
| 0.0.0.14 | 101 | 5/17/2026 |
| 0.0.0.13 | 99 | 5/14/2026 |
## 0.3.2
- Aligned the package version with the 0.3.2 Onnxify package family release.
- Picked up the core graph-loading fix for ONNX models whose `value_info` repeats graph input or output names when generated TorchModule loader code reads weights through `OnnxModel.FromFile(...)`.
- Kept the source generator strictly on `netstandard2.0` while referencing the core `Onnxify` package for shared graph loading and `OnnxGraph.SortTopologically()`.
- Removed the duplicated raw-protobuf topological sort from TorchModule analysis; TorchModule generation now walks the shared `OnnxGraph` wrapper in `PreserveUntyped` mode to retain raw ONNX node inputs and attributes.
- Fixed TorchModule deep import for ONNX `Conv` nodes with asymmetric `pads` by emitting an explicit `torch.nn.functional.pad(...)` before `conv2d(...)` instead of dropping the ending spatial padding values.
- Added deep roundtrip tests that import ONNX graphs as TorchSharp modules, export them back to ONNX, and compare runtime outputs for classifier-head, convolution, and MobileNet-style residual patterns.
## 0.3.1
- Added TorchModule initializer import support for ONNX `float16` and `bfloat16` tensors, using TorchSharp `BFloat16` support from TorchSharp `0.107.0` for generated bfloat16 loader paths.
- Expanded TorchModule generation to support ONNX graphs with multiple non-initializer inputs and multiple graph outputs.
- Added TorchModule reconstruction support for `ScatterND`, `ConvTranspose`, `SimplifiedLayerNormalization`, `GRU` initial hidden state inputs, and asymmetric convolution fallback through functional `conv2d`.
## 0.3.0
- Expanded TorchModule import support for ONNX graphs with broader runtime tensor data types that map to TorchSharp `ScalarType`, including non-float inputs and outputs.
- Added TorchModule import support for `GRU` and multi-output operators such as `Split` and `TopK`.
- Added TorchModule inline support for additional ONNX operators including `ArgMax`, `ArgMin`, `Celu`, `CumSum`, `DepthToSpace`, `Dropout`, `Expand`, `GatherElements`, `Gelu`, `GroupNormalization`, `HardSwish`, `InstanceNormalization`, `LayerNormalization`, `LogSoftmax`, `Mish`, `PRelu`, `Pad`, `ReduceMax`, `ReduceMin`, `ReduceProd`, `Resize`, `Selu`, `Slice`, `Softplus`, `SpaceToDepth`, `Tile`, and `Trilu`.
- TorchModule `LoadWeightsFromOnnx(...)` now canonicalizes the source graph and can fall back from initializer names to deterministic canonical initializer indexes when loading weights from structurally identical models with different value names.
- Removed MVP wording from TorchModule backend diagnostics.
## 0.2.0
- Aligned the package version with the 0.2.0 Onnxify package family release.
- Added `OnnxifyModelImportType`, with `OnnxRuntimeInference` as the default and an opt-in `TorchModule` mode that emits a graph-shaped TorchSharp module for supported single-input/single-output ONNX graphs.
- Added TorchModule import support for ONNX `Acos`, `Acosh`, `Asin`, `Asinh`, `Atan`, `Atanh`, `Round`, `Sign`, `GreaterOrEqual`, and `LessOrEqual` operators.
- Added TorchModule import support for runtime input and output tensor data types that map to TorchSharp `ScalarType`, plus ONNX `LSTM`, `Not`, `Max`, and `Min`, including LSTM gate-order conversion when loading ONNX weights into TorchSharp.
## 0.1.2
- Aligned the package version with the 0.1.2 Onnxify package family release.
## 0.1.1
- Aligned the package version with the 0.1.1 Onnxify package family release.
## 0.1.0
- Fixed `OnnxModel` metadata overrides flowing from MSBuild into the source generator, so `OnnxifyModelNamespace` and `OnnxifyModelClassName` now work in real consumer projects.
- Expanded the package documentation with working runtime snippets that show `SessionOptions`, `RunOptions`, and correct disposal of generated model and output wrappers.
## 0.0.0.14
- Added `Microsoft.ML.OnnxRuntime.Float16` support for generated wrappers over ONNX `float16` tensor inputs and outputs.
- Added `Microsoft.ML.OnnxRuntime.BFloat16` support for generated wrappers over ONNX `bfloat16` tensor inputs and outputs.
## 0.0.0.8
- Initial release