microsoft / microsoft/onnxruntime

OrtApi Globals in C#

Open
#13,743 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

contributions welcome
Dominant language
C++
Stars
21.9k
Forks
4.2k
Avg merge
4d 11h
Merged PRs (30d)
184

Description

### Discussed in https://github.com/microsoft/onnxruntime/discussions/13307

Originally posted by **thoron** October 13, 2022
Hi everyone,

I am missing some of the C/java bindings in C#. Specifically SpinLock control and global threading options.
They already exist in the `OrtApi` struct in the wrapper: https://github.com/microsoft/onnxruntime/blob/a83a9ed6b01374a5e1e07b46dbf2fb8e3c407483/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs#L183

Could it be considered adding them to the `OrtEnv` class?
Similar to the java implementation: https://github.com/microsoft/onnxruntime/blob/0757d5133428a9c0afb65bc0f74b3ced81d77cd4/java/src/main/java/ai/onnxruntime/OrtEnvironment.java#L430

I suggest exposing the threading options same as for java:
```java
OrtEnvironment.ThreadingOptions threadOpts = new OrtEnvironment.ThreadingOptions();
threadOpts.setGlobalInterOpNumThreads(2);
threadOpts.setGlobalIntraOpNumThreads(2);
threadOpts.setGlobalDenormalAsZero();
threadOpts.setGlobalSpinControl(true);
```
(from https://github.com/microsoft/onnxruntime/blob/main/java/src/test/java/ai/onnxruntime/EnvironmentThreadPoolTest.java)

Or expanding the `OrtEnv` with additional methods:
```csharp
public void SetGlobalSpinControl(bool allowSpin)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetGlobalSpinControl(Handle, allowSpin));
}
```

Using it as:
```csharp
OrtEnv.Instance().SetGlobalInterOpNumThreads(2);
OrtEnv.Instance().SetGlobalIntraOpNumThreads(2);
OrtEnv.Instance().SetGlobalSpinControl(true);
```

Best regards,
Thomas

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 csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs and the OrtApi declarations, then compare the corresponding Java OrtEnvironment and EnvironmentThreadPoolTest references. Done means exposing the requested global threading options, including inter-op and intra-op threads, denormal-as-zero, and spin control, through OrtEnv.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.