Onnxify 0.1.0
dotnet add package Onnxify --version 0.1.0
NuGet\Install-Package Onnxify -Version 0.1.0
<PackageReference Include="Onnxify" Version="0.1.0" />
<PackageVersion Include="Onnxify" Version="0.1.0" />
<PackageReference Include="Onnxify" />
paket add Onnxify --version 0.1.0
#r "nuget: Onnxify, 0.1.0"
#:package Onnxify@0.1.0
#addin nuget:?package=Onnxify&version=0.1.0
#tool nuget:?package=Onnxify&version=0.1.0
Onnxify
Onnxify is for programmatic ONNX model work in .NET: open an existing .onnx, inspect the graph, modify it, save it back, or build a model from scratch in C#.
Install
dotnet add package Onnxify
Why This Package Exists
ONNX is often used as a model interchange format, but in .NET there is usually an awkward gap between two extremes:
- an inference runtime can execute a model, but usually does very little to help you rewrite it;
- the protobuf layer lets you do almost anything, but it is too low-level for day-to-day engineering work.
Onnxify is meant to fill exactly that space. The package exists for cases where an ONNX model is not just an artifact you "run and forget", but something you need to read, understand, version, patch, and generate programmatically.
In practice, it helps with scenarios like these:
- open an existing ONNX model and quickly understand its inputs, outputs, initializers, and nodes;
- patch a graph in C# without dropping down into raw protobuf manipulation;
- generate ONNX as the output of your own tool, exporter, converter, or build pipeline;
- keep graph structure, value types, shapes, and opset versions in normal .NET code instead of manual protobuf plumbing.
In short, Onnxify is for teams that need control, transparency, and editability around ONNX in .NET.
What The Package Provides
OnnxModel.FromFile(...)andSave(...)for reading and writing.onnxfiles.OnnxModel.Create(...)for creating a new model from scratch.OnnxGraphfor working with inputs, outputs, intermediate values, initializers, and nodes.- Typed value and tensor descriptions through
OnnxValue,OnnxTensor<T>, andOnnxTensorType. - Explicit operator construction through
AddNode(...)and typed operator wrappers when they are available. ValidateCompatibility(...)for structural compatibility checks.
Quick Start
- If you want the first copy-paste example to run as-is, start with
Build A Small ONNX Model Manually. - If you already have a
.onnxfile and want to inspect it, useOpen A Model And Inspect The Graph.
Example: Open A Model And Inspect The Graph
This snippet assumes you already have an ONNX file on disk. Replace "model.onnx" with the path to a real model in your project or local workspace.
using System.Linq;
using Onnxify;
var model = OnnxModel.FromFile("model.onnx");
Console.WriteLine($"Producer: {model.ProducerName}");
Console.WriteLine($"IR version: {model.IrVersion}");
Console.WriteLine($"Opsets: {string.Join(", ", model.OpsetImport.Select(x => $"{x.Domain}:{x.Version}"))}");
Console.WriteLine($"Inputs: {model.Graph.Inputs.Count}");
Console.WriteLine($"Outputs: {model.Graph.Outputs.Count}");
Console.WriteLine($"Nodes: {model.Graph.Nodes.Count}");
foreach (var input in model.Graph.Inputs)
{
Console.WriteLine($"Input: {input.Name} -> {input}");
}
var weights = model.Graph.Initializers
.OfType<OnnxTensor<float>>()
.FirstOrDefault(x => x.Name == "weights");
if (weights is not null)
{
Console.WriteLine($"Weights shape: [{string.Join(", ", weights.Shape)}]");
Console.WriteLine($"Preview: {weights}");
}
This is useful when a model comes from somewhere else and the first step is understanding it before changing anything.
Example: Build A Small ONNX Model Manually
using Onnxify;
var model = OnnxModel.Create(new OnnxModelCreationOptions
{
ProducerName = "demo",
Opset = 18,
});
var graph = model.Graph;
graph.Name = "bias_add";
var input = graph.AddInput(
name: "input",
type: OnnxTensorType.Create<float>(["batch", 4])
);
var bias = graph.AddTensor(
name: "bias",
shape: [4],
value: [0.1f, 0.2f, 0.3f, 0.4f]
);
var hidden = graph.Add(
name: "add_bias",
options: new AddInputOptions
{
A = input,
B = bias,
}
);
var outputEdge = graph.AddEdge("output");
graph.Identity(
name: "publish_output",
options: new IdentityInputOutputOptions
{
Input = hidden,
Output = outputEdge,
}
);
graph.AddOutput(
name: "output",
type: OnnxTensorType.Create<float>(["batch", 4])
);
model.AddMetadataProps("author", "onnxify-demo");
model.Save("bias_add.onnx", overwrite: true);
This approach is useful when the ONNX model is generated by your own code instead of being exported from an external framework. It is also the best first smoke test if you want to verify that the package is installed and working in a new project.
Recommendations
- Use
OnnxModel.FromFile(...)when you are adapting an existing ONNX model and want to make targeted changes without working directly with protobuf APIs. - Use
OnnxModel.Create(...)when the ONNX graph is the end product of your own generator, exporter, or toolchain. - Set
Opsetexplicitly, and useSetOpsetImport(...)when needed, if you are working beyond the defaultai.onnxdomain or targeting a specific runtime. - Run
ValidateCompatibility(...)before publishing or handing models off to other systems, especially when graphs are generated or modified automatically. - If the source model uses external tensor data, configure
DataLocationandDataReader. The currentOnnxifyserialization flow writes tensors back as embedded data by default. - Use the base
Onnxifypackage when your main need is direct control over the ONNX graph itself. If the task is more specialized, look at the neighboring packages: Onnxify.TorchSharpfor exporting TorchSharp models into a controllable ONNX graph.Onnxify.ModelGeneratorfor generating typed wrappers from an existing.onnx.Microsoft.ML.OnnxRuntimeif you only need inference and not model editing.
Repository
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Google.Protobuf (>= 3.34.0)
-
net8.0
- Google.Protobuf (>= 3.34.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Onnxify:
| Package | Downloads |
|---|---|
|
Onnxify.TorchSharp
TorchSharp-to-ONNX export layer for translating TorchSharp modules and model structure into explicit Onnxify graph operations. |
|
|
Onnxify.ProjectGenerator
Code generation library that turns an existing ONNX model into a C# project or program that reconstructs the model through the Onnxify API. |
GitHub repositories
This package is not used by any popular GitHub repositories.
## 0.0.0.1
- Initial release