Onnxify.ModelGenerator 0.1.1

dotnet add package Onnxify.ModelGenerator --version 0.1.1
                    
NuGet\Install-Package Onnxify.ModelGenerator -Version 0.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Onnxify.ModelGenerator" Version="0.1.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Onnxify.ModelGenerator" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Onnxify.ModelGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Onnxify.ModelGenerator --version 0.1.1
                    
#r "nuget: Onnxify.ModelGenerator, 0.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Onnxify.ModelGenerator@0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Onnxify.ModelGenerator&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Onnxify.ModelGenerator&version=0.1.1
                    
Install as a Cake Tool

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 NamedOnnxValue collections 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 optional RunOptions

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
<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" />
  </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.

With the configuration above, the generator emits types like:

  • MyApp.Models.SampleClassifierModel
  • MyApp.Models.SampleClassifierModelInputs
  • MyApp.Models.SampleClassifierModelOutputs

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:

  • SessionOptions is disposable
  • the generated model wrapper is disposable because it owns InferenceSession
  • RunOptions is 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:

  • SessionOptions
  • RunOptions
  • 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>) and Run(..., 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, and OutputNames metadata for runtime inspection.
  • Models that use external tensor data still require their sibling external-data files at deployment time.

Repository

There are no supported framework assets in this package.

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.1.1 37 5/20/2026
0.1.0 86 5/18/2026
0.0.0.15 88 5/18/2026
0.0.0.14 88 5/17/2026
0.0.0.13 84 5/14/2026

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