microsoft / microsoft/onnxruntime

WIP [Documentation]: C# Workflow for consuming "Augmented" Onnx model with Custom Operators

Open
#11,657 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
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

custom_op_test.onnx

  • CustomOpOne
  • CustomOpTwo

image

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

CSharp api for loading/registrating custom op shared library

RegisterCustomOps

custom_op_library.h

#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

custom_op_library.cc

  • 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";

The c++ unit tests discussion

@natke

WIP for CSharp
[Documentation] How to register custom operator in onnxruntime

Other references:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.