microsoft / microsoft/onnxruntime
WIP [Documentation]: C# Workflow for consuming "Augmented" Onnx model with Custom Operators
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 21.9k
- Forks
- 4.2k
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 179
Description
3 ways to add a new operator
There is an issue to missing link
Use Case: PyTorch exports a custom_op_test.onnx with TWO custom operators
- CustomOpOne
- CustomOpTwo

To consume in CSharp exported PyTorch Onnx with custom operators, it is necessary to register these Custom Operators (written in C++) and provided in a Shared Library
CSharp API for loading Shared Library with Custom Operators
Create Custom_op_library.dll
- Custom_op_library.dll : source files
CSharp api for loading/registrating custom op shared library
RegisterCustomOps
#pragma once
#include "onnxruntime_c_api.h"
#ifdef __cplusplus
extern "C" {
#endif
ORT_EXPORT OrtStatus* ORT_API_CALL RegisterCustomOps(OrtSessionOptions* options, const OrtApiBase* api);
#ifdef __cplusplus
}
#endif
- With 2 Custom Operators
- CustomOpOne
- CustomOpTwo
#include "custom_op_library.h"
#define ORT_API_MANUAL_INIT
#include "onnxruntime_cxx_api.h"
#undef ORT_API_MANUAL_INIT
OrtStatus* ORT_API_CALL RegisterCustomOps(OrtSessionOptions* options, const OrtApiBase* api) {
OrtCustomOpDomain* domain = nullptr;
const OrtApi* ortApi = api->GetApi(ORT_API_VERSION);
if (auto status = ortApi->CreateCustomOpDomain(c_OpDomain, &domain)) {
return status;
}
AddOrtCustomOpDomainToContainer(domain, ortApi);
if (auto status = ortApi->CustomOpDomain_Add(domain, &c_CustomOpOne)) {
return status;
}
if (auto status = ortApi->CustomOpDomain_Add(domain, &c_CustomOpTwo)) {
return status;
}
return ortApi->AddCustomOpDomain(options, domain);
}
In CSharp Register Custom Operators within the Shared Library: Custom_op_library.dll
CSharp UnitTest: NetCoreApp/InferenceTest.netcore.c
private void TestRegisterCustomOpLibrary()
{
using (var option = new SessionOptions())
{
string libName = "custom_op_library.dll";
string modelPath = "custom_op_test.onnx";
@natke
WIP for CSharp
[Documentation] How to register custom operator in onnxruntime
Other references:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with docs/AddingCustomOp.md, the custom_op_library sources, and csharp/test/Microsoft.ML.OnnxRuntime.Tests.NetCoreApp/InferenceTest.netcore.cs. Trace how custom_op_test.onnx, custom_op_library.dll, and RegisterCustomOps are used in the C# test. Done means the C# workflow for building, loading, and registering the two custom operators is complete and documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, csharp
- Domain
- documentation, machine-learning, testing
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 28/100