diff --git a/onnxruntime/core/providers/dml/dml_provider_factory.cc b/onnxruntime/core/providers/dml/dml_provider_factory.cc index 079c444197..a16c154fbf 100644 --- a/onnxruntime/core/providers/dml/dml_provider_factory.cc +++ b/onnxruntime/core/providers/dml/dml_provider_factory.cc @@ -28,14 +28,17 @@ struct DMLProviderFactory : IExecutionProviderFactory { std::unique_ptr CreateProvider() override; void SetDefaultRoundingMode(AllocatorRoundingMode rounding_mode); + void SetMetacommandsEnabled(bool metacommands_enabled); + private: ComPtr dml_device_{}; ComPtr cmd_queue_{}; AllocatorRoundingMode rounding_mode_ = AllocatorRoundingMode::Enabled; + bool metacommands_enabled_ = true; }; std::unique_ptr 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 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 CreateExecutionProviderFactory_DML(ID } void DmlConfigureProviderFactoryDefaultRoundingMode(IExecutionProviderFactory* factory, AllocatorRoundingMode rounding_mode) { - auto dml_prvider_factory = static_cast(factory); - dml_prvider_factory->SetDefaultRoundingMode(rounding_mode); + auto dml_provider_factory = static_cast(factory); + dml_provider_factory->SetDefaultRoundingMode(rounding_mode); } +void DmlConfigureProviderFactoryMetacommandsEnabled(IExecutionProviderFactory* factory, bool metacommandsEnabled) { + auto dml_provider_factory = static_cast(factory); + dml_provider_factory->SetMetacommandsEnabled(metacommandsEnabled); +} + + bool IsSoftwareAdapter(IDXGIAdapter1* adapter) { DXGI_ADAPTER_DESC1 desc; adapter->GetDesc1(&desc); diff --git a/winml/adapter/winml_adapter_apis.h b/winml/adapter/winml_adapter_apis.h index a4477c264a..4beecdfb97 100644 --- a/winml/adapter/winml_adapter_apis.h +++ b/winml/adapter/winml_adapter_apis.h @@ -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); diff --git a/winml/adapter/winml_adapter_c_api.h b/winml/adapter/winml_adapter_c_api.h index 76dec8f49a..7536d0d769 100644 --- a/winml/adapter/winml_adapter_c_api.h +++ b/winml/adapter/winml_adapter_c_api.h @@ -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 diff --git a/winml/adapter/winml_adapter_dml.cpp b/winml/adapter/winml_adapter_dml.cpp index 235ebbf4d0..b289ae10f3 100644 --- a/winml/adapter/winml_adapter_dml.cpp +++ b/winml/adapter/winml_adapter_dml.cpp @@ -49,12 +49,13 @@ Microsoft::WRL::ComPtr 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 diff --git a/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.cpp b/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.cpp index c393dfd776..7e55919ca8 100644 --- a/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.cpp +++ b/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.cpp @@ -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 diff --git a/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.h b/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.h index 8efd98e5b6..a0742e4de2 100644 --- a/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.h +++ b/winml/lib/Api.Ort/OnnxruntimeDmlSessionBuilder.h @@ -13,7 +13,7 @@ class OnnxruntimeDmlSessionBuilder : public Microsoft::WRL::RuntimeClass< Microsoft::WRL::RuntimeClassFlags, 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 engine_factory_; winrt::com_ptr device_; winrt::com_ptr queue_; + bool metacommands_enabled_ = true; }; } // namespace Windows::AI::MachineLearning \ No newline at end of file diff --git a/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.cpp b/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.cpp index 9514de9478..4813b9659d 100644 --- a/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.cpp +++ b/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.cpp @@ -28,7 +28,7 @@ STDMETHODIMP OnnxruntimeEngineBuilder::CreateEngine(Windows::AI::MachineLearning RETURN_IF_FAILED(Microsoft::WRL::MakeAndInitialize(&onnxruntime_session_builder, engine_factory_.Get())); } else { #ifdef USE_DML - RETURN_IF_FAILED(Microsoft::WRL::MakeAndInitialize(&onnxruntime_session_builder, engine_factory_.Get(), device_.Get(), queue_.Get())); + RETURN_IF_FAILED(Microsoft::WRL::MakeAndInitialize(&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(enabled); + return S_OK; +} + STDMETHODIMP OnnxruntimeEngineBuilder::GetID3D12CommandQueue(ID3D12CommandQueue** queue) { *queue = queue_.Get(); return S_OK; diff --git a/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.h b/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.h index 6ac5f47802..14597ed546 100644 --- a/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.h +++ b/winml/lib/Api.Ort/OnnxruntimeEngineBuilder.h @@ -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 engine_factory_; Microsoft::WRL::ComPtr device_ = nullptr; Microsoft::WRL::ComPtr queue_ = nullptr; + bool metacommands_enabled_ = true; std::optional batch_size_override_; }; diff --git a/winml/lib/Api/LearningModelSession.cpp b/winml/lib/Api/LearningModelSession.cpp index a8f1e8ee7b..8dfa252706 100644 --- a/winml/lib/Api/LearningModelSession.cpp +++ b/winml/lib/Api/LearningModelSession.cpp @@ -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 diff --git a/winml/lib/Common/inc/iengine.h b/winml/lib/Common/inc/iengine.h index 8ec9d61d09..17ef639572 100644 --- a/winml/lib/Common/inc/iengine.h +++ b/winml/lib/Common/inc/iengine.h @@ -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;