Merged PR 4408735: Fix control over metacommands being enabled or disabled

Fix control over metacommands being enabled or disabled

Related work items: #25512218
This commit is contained in:
Jeff Bloomfield 2020-03-10 17:13:59 +00:00
parent 9ba05a4a1c
commit be0c0da9ef
10 changed files with 42 additions and 11 deletions

View file

@ -28,14 +28,17 @@ struct DMLProviderFactory : IExecutionProviderFactory {
std::unique_ptr<IExecutionProvider> CreateProvider() override;
void SetDefaultRoundingMode(AllocatorRoundingMode rounding_mode);
void SetMetacommandsEnabled(bool metacommands_enabled);
private:
ComPtr<IDMLDevice> dml_device_{};
ComPtr<ID3D12CommandQueue> cmd_queue_{};
AllocatorRoundingMode rounding_mode_ = AllocatorRoundingMode::Enabled;
bool metacommands_enabled_ = true;
};
std::unique_ptr<IExecutionProvider> DMLProviderFactory::CreateProvider() {
auto provider = Dml::CreateExecutionProvider(dml_device_.Get(), cmd_queue_.Get());
auto provider = Dml::CreateExecutionProvider(dml_device_.Get(), cmd_queue_.Get(), metacommands_enabled_);
Dml::SetDefaultRoundingMode(provider.get(), rounding_mode_);
return provider;
}
@ -44,6 +47,10 @@ void DMLProviderFactory::SetDefaultRoundingMode(AllocatorRoundingMode rounding_m
rounding_mode_ = rounding_mode;
}
void DMLProviderFactory::SetMetacommandsEnabled(bool metacommands_enabled) {
metacommands_enabled_ = metacommands_enabled;
}
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_DML(IDMLDevice* dml_device,
ID3D12CommandQueue* cmd_queue) {
// Validate that the D3D12 devices match between DML and the command queue. This specifically asks for IUnknown in
@ -66,10 +73,16 @@ std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_DML(ID
}
void DmlConfigureProviderFactoryDefaultRoundingMode(IExecutionProviderFactory* factory, AllocatorRoundingMode rounding_mode) {
auto dml_prvider_factory = static_cast<DMLProviderFactory*>(factory);
dml_prvider_factory->SetDefaultRoundingMode(rounding_mode);
auto dml_provider_factory = static_cast<DMLProviderFactory*>(factory);
dml_provider_factory->SetDefaultRoundingMode(rounding_mode);
}
void DmlConfigureProviderFactoryMetacommandsEnabled(IExecutionProviderFactory* factory, bool metacommandsEnabled) {
auto dml_provider_factory = static_cast<DMLProviderFactory*>(factory);
dml_provider_factory->SetMetacommandsEnabled(metacommandsEnabled);
}
bool IsSoftwareAdapter(IDXGIAdapter1* adapter) {
DXGI_ADAPTER_DESC1 desc;
adapter->GetDesc1(&desc);

View file

@ -39,7 +39,7 @@ ORT_API_STATUS(ModelGetMetadataCount, _In_ const OrtModel* model, _Out_ size_t*
ORT_API_STATUS(ModelGetMetadata, _In_ const OrtModel* model, _Out_ size_t count, _Out_ const char** const key, _Out_ size_t* key_len, _Out_ const char** const value, _Out_ size_t* value_len);
ORT_API_STATUS(ModelEnsureNoFloat16, _In_ const OrtModel* model);
ORT_API_STATUS(OrtSessionOptionsAppendExecutionProviderEx_DML, _In_ OrtSessionOptions* options, _In_ ID3D12Device* d3d_device, _In_ ID3D12CommandQueue* cmd_queue);
ORT_API_STATUS(OrtSessionOptionsAppendExecutionProviderEx_DML, _In_ OrtSessionOptions* options, _In_ ID3D12Device* d3d_device, _In_ ID3D12CommandQueue* cmd_queue, bool metacommands_enabled);
// OrtSession methods
ORT_API_STATUS(CreateSessionWithoutModel, _In_ OrtEnv* env, _In_ const OrtSessionOptions* options, _Outptr_ OrtSession** session);

View file

@ -213,7 +213,7 @@ struct WinmlAdapterApi {
* OrtSessionOptionsAppendExecutionProvider_DML
* This api is used to add the DML EP to OrtSessionOptions.
*/
OrtStatus*(ORT_API_CALL* OrtSessionOptionsAppendExecutionProvider_DML)(_In_ OrtSessionOptions* options, ID3D12Device* device, ID3D12CommandQueue* queue)NO_EXCEPTION;
OrtStatus*(ORT_API_CALL* OrtSessionOptionsAppendExecutionProvider_DML)(_In_ OrtSessionOptions* options, ID3D12Device* device, ID3D12CommandQueue* queue, bool metacommands_enabled)NO_EXCEPTION;
// OrtSession methods

View file

@ -49,12 +49,13 @@ Microsoft::WRL::ComPtr<IDMLDevice> CreateDmlDevice(ID3D12Device* d3d12Device) {
namespace onnxruntime {
void DmlConfigureProviderFactoryDefaultRoundingMode(onnxruntime::IExecutionProviderFactory* factory, AllocatorRoundingMode rounding_mode);
void DmlConfigureProviderFactoryMetacommandsEnabled(IExecutionProviderFactory* factory, bool metacommandsEnabled);
}
#endif // USE_DML
ORT_API_STATUS_IMPL(winmla::OrtSessionOptionsAppendExecutionProviderEx_DML, _In_ OrtSessionOptions* options,
ID3D12Device* d3d_device, ID3D12CommandQueue* queue) {
ID3D12Device* d3d_device, ID3D12CommandQueue* queue, bool metacommands_enabled) {
API_IMPL_BEGIN
#ifdef USE_DML
auto dml_device = CreateDmlDevice(d3d_device);
@ -68,6 +69,8 @@ ORT_API_STATUS_IMPL(winmla::OrtSessionOptionsAppendExecutionProviderEx_DML, _In_
// lifetime and can be large, so shouldn't be rounded.
// So we create the provider with rounding disabled, and expect the caller to enable it after.
onnxruntime::DmlConfigureProviderFactoryDefaultRoundingMode(factory, AllocatorRoundingMode::Disabled);
onnxruntime::DmlConfigureProviderFactoryMetacommandsEnabled(factory, metacommands_enabled);
#endif // USE_DML
return nullptr;
API_IMPL_END

View file

@ -12,10 +12,11 @@
using namespace Windows::AI::MachineLearning;
HRESULT OnnxruntimeDmlSessionBuilder::RuntimeClassInitialize(OnnxruntimeEngineFactory* engine_factory, ID3D12Device* device, ID3D12CommandQueue* queue) {
HRESULT OnnxruntimeDmlSessionBuilder::RuntimeClassInitialize(OnnxruntimeEngineFactory* engine_factory, ID3D12Device* device, ID3D12CommandQueue* queue, bool metacommands_enabled) {
engine_factory_ = engine_factory;
device_.copy_from(device);
queue_.copy_from(queue);
metacommands_enabled_ = metacommands_enabled;
return S_OK;
}
@ -42,7 +43,7 @@ OnnxruntimeDmlSessionBuilder::CreateSessionOptions(
ort_api);
// Request the dml ep
RETURN_HR_IF_NOT_OK_MSG(winml_adapter_api->OrtSessionOptionsAppendExecutionProvider_DML(session_options.get(), device_.get(), queue_.get()),
RETURN_HR_IF_NOT_OK_MSG(winml_adapter_api->OrtSessionOptionsAppendExecutionProvider_DML(session_options.get(), device_.get(), queue_.get(), metacommands_enabled_),
ort_api);
#ifndef _WIN64

View file

@ -13,7 +13,7 @@ class OnnxruntimeDmlSessionBuilder : public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
IOrtSessionBuilder> {
public:
HRESULT RuntimeClassInitialize(OnnxruntimeEngineFactory* engine_factory, ID3D12Device* device, ID3D12CommandQueue* queue);
HRESULT RuntimeClassInitialize(OnnxruntimeEngineFactory* engine_factory, ID3D12Device* device, ID3D12CommandQueue* queue, bool metacommands_enabled_);
HRESULT STDMETHODCALLTYPE CreateSessionOptions(
OrtSessionOptions** options) override;
@ -29,6 +29,7 @@ class OnnxruntimeDmlSessionBuilder : public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::ComPtr<OnnxruntimeEngineFactory> engine_factory_;
winrt::com_ptr<ID3D12Device> device_;
winrt::com_ptr<ID3D12CommandQueue> queue_;
bool metacommands_enabled_ = true;
};
} // namespace Windows::AI::MachineLearning

View file

@ -28,7 +28,7 @@ STDMETHODIMP OnnxruntimeEngineBuilder::CreateEngine(Windows::AI::MachineLearning
RETURN_IF_FAILED(Microsoft::WRL::MakeAndInitialize<OnnxruntimeCpuSessionBuilder>(&onnxruntime_session_builder, engine_factory_.Get()));
} else {
#ifdef USE_DML
RETURN_IF_FAILED(Microsoft::WRL::MakeAndInitialize<OnnxruntimeDmlSessionBuilder>(&onnxruntime_session_builder, engine_factory_.Get(), device_.Get(), queue_.Get()));
RETURN_IF_FAILED(Microsoft::WRL::MakeAndInitialize<OnnxruntimeDmlSessionBuilder>(&onnxruntime_session_builder, engine_factory_.Get(), device_.Get(), queue_.Get(), metacommands_enabled_));
#endif
}
@ -64,6 +64,11 @@ STDMETHODIMP OnnxruntimeEngineBuilder::SetD3D12Resources(ID3D12Device* device, I
return S_OK;
}
STDMETHODIMP OnnxruntimeEngineBuilder::SetMetacommandsEnabled(int enabled) {
metacommands_enabled_ = static_cast<bool>(enabled);
return S_OK;
}
STDMETHODIMP OnnxruntimeEngineBuilder::GetID3D12CommandQueue(ID3D12CommandQueue** queue) {
*queue = queue_.Get();
return S_OK;

View file

@ -14,6 +14,9 @@ class OnnxruntimeEngineBuilder : public Microsoft::WRL::RuntimeClass<
STDMETHOD(SetD3D12Resources)
(ID3D12Device* device, ID3D12CommandQueue* queue);
STDMETHOD(SetMetacommandsEnabled)
(int enabled);
STDMETHOD(GetD3D12Device)
(_Outptr_ ID3D12Device** device);
@ -30,6 +33,7 @@ class OnnxruntimeEngineBuilder : public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::ComPtr<OnnxruntimeEngineFactory> engine_factory_;
Microsoft::WRL::ComPtr<ID3D12Device> device_ = nullptr;
Microsoft::WRL::ComPtr<ID3D12CommandQueue> queue_ = nullptr;
bool metacommands_enabled_ = true;
std::optional<uint32_t> batch_size_override_;
};

View file

@ -107,6 +107,7 @@ void LearningModelSession::Initialize() {
if (device_impl->IsCpuDevice() == false) {
engine_builder->SetD3D12Resources(device_impl->GetD3DDevice(), device_impl->GetDeviceQueue());
engine_builder->SetMetacommandsEnabled(device_impl->MetacommandsEnabled());
}
// Make onnxruntime apply the batch size override, if any

View file

@ -142,11 +142,14 @@ IEngine : IUnknown {
(IInspectable * sequence, winml::TensorKind key_kind, winml::TensorKind value_kind, IValue * value) PURE;
};
MIDL_INTERFACE("0452ef15-b66b-47ca-9eff-aedac571764e")
MIDL_INTERFACE("8ac0b6b9-4561-492b-b63d-a07bdd8292c6")
IEngineBuilder : IUnknown {
STDMETHOD(SetD3D12Resources)
(ID3D12Device * device, ID3D12CommandQueue * queue) PURE;
STDMETHOD(SetMetacommandsEnabled)
(int enabled) PURE;
STDMETHOD(GetD3D12Device)
(ID3D12Device * *device) PURE;