From 256735135f788b8c938af527a80b4dfe2a36d37b Mon Sep 17 00:00:00 2001 From: Sumit Agarwal Date: Thu, 23 Jun 2022 16:07:01 -0700 Subject: [PATCH 1/5] Pad fallback to CPU --- .../src/Operators/DmlOperatorPadding.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp index b0abb3baef0a2..db3edba6172b6 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp @@ -95,6 +95,27 @@ class DmlOperatorPadding : public DmlOperator, public PaddingHelper } }; +void CALLBACK QueryPad(IMLOperatorSupportQueryContextPrivate* context, /*out*/ bool* isSupported) +{ + // At the time of writing this, DML_PADDING1_OPERATOR_DESC doesn't support negative element for + // padding count i.e. StartPadding and EndPadding can't contain negative elements. + // For opset < 11, + // if attribute 'pads' contains negative element, fall back to CPU + // opset >= 11 + // DML EP continues to produce wrong result. [TODO: After DML1.9 release, introduce new API for pad to + // handle negative values for StartPadding and EndPadding] + *isSupported = true; + + MLOperatorAttributes attributes(context); + + std::vector padding = attributes.GetOptionalAttributeVectorInt32(AttrName::Pads); + auto isNegativeElementPresent = std::find_if(padding.begin(), padding.end(), [](int32_t padCount) {return padCount < 0; }); + if (isNegativeElementPresent != padding.end()) + { + *isSupported = false; + } +} + DML_OP_DEFINE_CREATION_FUNCTION(Pad7, VersionedKernel); DML_OP_DEFINE_CREATION_FUNCTION(Pad11, VersionedKernel); DML_OP_DEFINE_CREATION_FUNCTION(Pad13, VersionedKernel); From e377d019c2c36dee9438b4365864d95f3dc83038 Mon Sep 17 00:00:00 2001 From: Sumit Agarwal Date: Thu, 23 Jun 2022 18:05:57 -0700 Subject: [PATCH 2/5] Added queryPad in operatorRegistration.cpp --- .../src/Operators/OperatorRegistration.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp index 3ad7c4606be5c..7eb23c1e7ae5b 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp @@ -264,6 +264,7 @@ DML_OP_EXTERN_QUERY_FUNCTION(Resize); DML_OP_EXTERN_QUERY_FUNCTION(EinSum); DML_OP_EXTERN_QUERY_FUNCTION(RecurrentNeuralNetwork); DML_OP_EXTERN_QUERY_FUNCTION(BatchNormalization); +DML_OP_EXTERN_QUERY_FUNCTION(Pad); constexpr static std::array typeNameListDefault = {"T"}; constexpr static std::array typeNameListTwo = { "T1", "T2" }; @@ -428,7 +429,7 @@ constexpr static OperatorRegistrationInformation operatorRegistrationInformation {REG_INFO_VER( 10, Slice, typeNameListSlice10, supportedTypeListSlice10, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2, 3, 4), std::nullopt, QuerySlice)}, // Adds negative axes. {REG_INFO_VER( 11, Slice, typeNameListSlice10, supportedTypeListSlice10, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2, 3, 4), std::nullopt, QuerySlice)}, {REG_INFO_VER( 13, Slice, typeNameListSlice10, supportedTypeListSlice10, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2, 3, 4), std::nullopt, QuerySlice)}, - {REG_INFO_VER( 7, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported)}, + {REG_INFO_VER( 7, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported, requiredConstantCpuInputs(), std::nullopt, QueryPad)}, {REG_INFO_VER( 11, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2) /*pads, value*/)}, // https://microsoft.visualstudio.com/OS/_workitems/edit/26007728 {REG_INFO_VER( 13, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2) /*pads, value*/)}, // https://microsoft.visualstudio.com/OS/_workitems/edit/26007728 {REG_INFO( 7, SpaceToDepth, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported)}, From b7346a921a6b2b9b4513e4c1f2af21339a16e1ec Mon Sep 17 00:00:00 2001 From: Sumit Agarwal Date: Fri, 24 Jun 2022 11:50:50 -0700 Subject: [PATCH 3/5] Acknowledged PR comments --- .../DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp index db3edba6172b6..6d26eec682353 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp @@ -97,8 +97,8 @@ class DmlOperatorPadding : public DmlOperator, public PaddingHelper void CALLBACK QueryPad(IMLOperatorSupportQueryContextPrivate* context, /*out*/ bool* isSupported) { - // At the time of writing this, DML_PADDING1_OPERATOR_DESC doesn't support negative element for - // padding count i.e. StartPadding and EndPadding can't contain negative elements. + // DML_PADDING1_OPERATOR_DESC doesn't support negative padding counts i.e. StartPadding and EndPadding + // can't contain negative elements. // For opset < 11, // if attribute 'pads' contains negative element, fall back to CPU // opset >= 11 From 0aea88dcb69c8206706179ab64d3d0ef7cf9e515 Mon Sep 17 00:00:00 2001 From: Sumit Agarwal Date: Fri, 24 Jun 2022 13:54:50 -0700 Subject: [PATCH 4/5] Used any_of --- .../src/Operators/DmlOperatorPadding.cpp | 14 +++++--------- .../src/Operators/OperatorRegistration.cpp | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp index 6d26eec682353..d166a67f53dcb 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp @@ -97,23 +97,19 @@ class DmlOperatorPadding : public DmlOperator, public PaddingHelper void CALLBACK QueryPad(IMLOperatorSupportQueryContextPrivate* context, /*out*/ bool* isSupported) { - // DML_PADDING1_OPERATOR_DESC doesn't support negative padding counts i.e. StartPadding and EndPadding - // can't contain negative elements. - // For opset < 11, + // DML_PADDING1_OPERATOR_DESC doesn't support negative padding counts i.e. StartPadding and EndPadding + // can't contain negative elements. + // For opset < 11, // if attribute 'pads' contains negative element, fall back to CPU // opset >= 11 - // DML EP continues to produce wrong result. [TODO: After DML1.9 release, introduce new API for pad to + // DML EP continues to produce wrong result. [TODO: After DML1.9 release, introduce new API for pad to // handle negative values for StartPadding and EndPadding] *isSupported = true; MLOperatorAttributes attributes(context); std::vector padding = attributes.GetOptionalAttributeVectorInt32(AttrName::Pads); - auto isNegativeElementPresent = std::find_if(padding.begin(), padding.end(), [](int32_t padCount) {return padCount < 0; }); - if (isNegativeElementPresent != padding.end()) - { - *isSupported = false; - } + *isSupported = std::any_of(padding.begin(), padding.end(), [](int32_t padCount) {return padCount < 0; }); } DML_OP_DEFINE_CREATION_FUNCTION(Pad7, VersionedKernel); diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp index 7eb23c1e7ae5b..930afee02ea89 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/OperatorRegistration.cpp @@ -429,7 +429,7 @@ constexpr static OperatorRegistrationInformation operatorRegistrationInformation {REG_INFO_VER( 10, Slice, typeNameListSlice10, supportedTypeListSlice10, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2, 3, 4), std::nullopt, QuerySlice)}, // Adds negative axes. {REG_INFO_VER( 11, Slice, typeNameListSlice10, supportedTypeListSlice10, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2, 3, 4), std::nullopt, QuerySlice)}, {REG_INFO_VER( 13, Slice, typeNameListSlice10, supportedTypeListSlice10, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2, 3, 4), std::nullopt, QuerySlice)}, - {REG_INFO_VER( 7, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported, requiredConstantCpuInputs(), std::nullopt, QueryPad)}, + {REG_INFO_VER( 7, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported, requiredConstantCpuInputs(), std::nullopt, QueryPad)}, {REG_INFO_VER( 11, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2) /*pads, value*/)}, // https://microsoft.visualstudio.com/OS/_workitems/edit/26007728 {REG_INFO_VER( 13, Pad, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported, requiredConstantCpuInputs(1, 2) /*pads, value*/)}, // https://microsoft.visualstudio.com/OS/_workitems/edit/26007728 {REG_INFO( 7, SpaceToDepth, typeNameListDefault, supportedTypeListAllScalars, DmlGraphSupport::Supported)}, From 939863d3e9de8814caedb478c7f8ee10ce168eba Mon Sep 17 00:00:00 2001 From: Sumit Agarwal Date: Mon, 27 Jun 2022 21:32:33 -0700 Subject: [PATCH 5/5] used none_of instead of any_of --- .../DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp index d166a67f53dcb..84046f74eacf3 100644 --- a/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp +++ b/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorPadding.cpp @@ -109,7 +109,7 @@ void CALLBACK QueryPad(IMLOperatorSupportQueryContextPrivate* context, /*out*/ b MLOperatorAttributes attributes(context); std::vector padding = attributes.GetOptionalAttributeVectorInt32(AttrName::Pads); - *isSupported = std::any_of(padding.begin(), padding.end(), [](int32_t padCount) {return padCount < 0; }); + *isSupported = std::none_of(padding.begin(), padding.end(), [](int32_t padCount) {return padCount < 0; }); } DML_OP_DEFINE_CREATION_FUNCTION(Pad7, VersionedKernel);