onnxruntime/winml/lib/Api.Ort/OnnxruntimeCpuSessionBuilder.cpp
Ryan Lai 5ce675c3b9
Expose Onnxruntime Intra Op thread controls through WinML Native API (#4638)
* Register ILearningModelSessionOptionsNate interface

* Threading options exposed

* Add interrogator for Session options

* Add test

* Polish test

* PR comments

* Set intra op threads

* Add adapter api to grab intra op threads

* Add adapter test for getting intraop num threads

* Make ILearningModelSessionNative and update winml api test

* Make it required when building engine to set the intraop num threads

* Make test  more pretty

* Change naming of idl function

* Revert "Change naming of idl function"

This reverts commit c06916aa5bf94e3bf233ed281e508b935fc8638d.

* PR comment on naming

* Skip the test because it's influenced if it's built with openmp

Co-authored-by: Ryan Lai <ryalai96@gamil.com>
2020-07-30 17:55:26 -07:00

81 lines
2.5 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#include "OnnxruntimeCpuSessionBuilder.h"
#include "OnnxruntimeEngine.h"
#include "OnnxruntimeErrors.h"
using namespace _winml;
HRESULT OnnxruntimeCpuSessionBuilder::RuntimeClassInitialize(OnnxruntimeEngineFactory* engine_factory) {
engine_factory_ = engine_factory;
return S_OK;
}
HRESULT
OnnxruntimeCpuSessionBuilder::CreateSessionOptions(
OrtSessionOptions** options) {
RETURN_HR_IF_NULL(E_POINTER, options);
auto ort_api = engine_factory_->UseOrtApi();
auto winml_adapter_api = engine_factory_->UseWinmlAdapterApi();
OrtSessionOptions* ort_options;
RETURN_HR_IF_NOT_OK_MSG(ort_api->CreateSessionOptions(&ort_options),
ort_api);
auto session_options = UniqueOrtSessionOptions(ort_options, ort_api->ReleaseSessionOptions);
// set the graph optimization level to all (used to be called level 3)
RETURN_HR_IF_NOT_OK_MSG(ort_api->SetSessionGraphOptimizationLevel(session_options.get(), GraphOptimizationLevel::ORT_ENABLE_ALL),
ort_api);
#ifndef _WIN64
auto use_arena = false;
#else
auto use_arena = true;
#endif
RETURN_HR_IF_NOT_OK_MSG(winml_adapter_api->OrtSessionOptionsAppendExecutionProvider_CPU(session_options.get(), use_arena),
ort_api);
// call release() so the underlying OrtSessionOptions object isn't freed
*options = session_options.release();
return S_OK;
}
HRESULT
OnnxruntimeCpuSessionBuilder::CreateSession(
OrtSessionOptions* options,
OrtSession** session) {
RETURN_HR_IF_NULL(E_POINTER, session);
auto ort_api = engine_factory_->UseOrtApi();
auto winml_adapter_api = engine_factory_->UseWinmlAdapterApi();
OrtEnv* ort_env;
RETURN_IF_FAILED(engine_factory_->GetOrtEnvironment(&ort_env));
OrtSession* ort_session_raw;
RETURN_HR_IF_NOT_OK_MSG(winml_adapter_api->CreateSessionWithoutModel(ort_env, options, &ort_session_raw),
engine_factory_->UseOrtApi());
auto ort_session = UniqueOrtSession(ort_session_raw, ort_api->ReleaseSession);
*session = ort_session.release();
return S_OK;
}
HRESULT
OnnxruntimeCpuSessionBuilder::Initialize(
OrtSession* session) {
RETURN_HR_IF_NULL(E_INVALIDARG, session);
auto winml_adapter_api = engine_factory_->UseWinmlAdapterApi();
RETURN_HR_IF_NOT_OK_MSG(winml_adapter_api->SessionInitialize(session),
engine_factory_->UseOrtApi());
return S_OK;
}