From e570e662fa0fb46d2015c6f892a6b4ab609879eb Mon Sep 17 00:00:00 2001 From: Ashwini Khade Date: Tue, 20 Aug 2019 20:19:44 -0700 Subject: [PATCH 1/4] update clip for opset 11 --- .../providers/cpu/cpu_execution_provider.cc | 6 ++++ onnxruntime/core/providers/cpu/math/clip.cc | 6 ++++ onnxruntime/core/providers/cpu/math/clip.h | 36 +++++++++++++++++-- onnxruntime/test/onnx/main.cc | 8 +---- .../test/providers/cpu/math/clip_test.cc | 36 +++++++++++++++++-- .../test/python/onnx_backend_test_series.py | 1 - 6 files changed, 81 insertions(+), 12 deletions(-) diff --git a/onnxruntime/core/providers/cpu/cpu_execution_provider.cc b/onnxruntime/core/providers/cpu/cpu_execution_provider.cc index 6508bfdf86734..6e3d2960d3930 100644 --- a/onnxruntime/core/providers/cpu/cpu_execution_provider.cc +++ b/onnxruntime/core/providers/cpu/cpu_execution_provider.cc @@ -298,6 +298,9 @@ class ONNX_OPERATOR_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, class ONNX_OPERATOR_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 10, double, RoiAlign); class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 10, ReverseSequence); +// opset 11 +class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 11, Clip); + void RegisterOnnxOperatorKernels(KernelRegistry& kernel_registry) { static const BuildKernelCreateInfoFn function_table[] = { BuildKernelCreateInfo, @@ -579,6 +582,9 @@ void RegisterOnnxOperatorKernels(KernelRegistry& kernel_registry) { BuildKernelCreateInfo, BuildKernelCreateInfo, BuildKernelCreateInfo, + + //opset 11 + BuildKernelCreateInfo, }; for (auto& function_table_entry : function_table) { diff --git a/onnxruntime/core/providers/cpu/math/clip.cc b/onnxruntime/core/providers/cpu/math/clip.cc index 160d587df7238..f1e54aafde64d 100644 --- a/onnxruntime/core/providers/cpu/math/clip.cc +++ b/onnxruntime/core/providers/cpu/math/clip.cc @@ -9,6 +9,12 @@ ONNX_CPU_OPERATOR_KERNEL( Clip, 6, KernelDefBuilder().MayInplace(0, 0).TypeConstraint("T", DataTypeImpl::GetTensorType()), + Clip_6); + +ONNX_CPU_OPERATOR_KERNEL( + Clip, + 11, + KernelDefBuilder().MayInplace(0, 0).TypeConstraint("T", DataTypeImpl::GetTensorType()), Clip); } // namespace onnxruntime diff --git a/onnxruntime/core/providers/cpu/math/clip.h b/onnxruntime/core/providers/cpu/math/clip.h index 653967547bb02..b4ef64398dddf 100644 --- a/onnxruntime/core/providers/cpu/math/clip.h +++ b/onnxruntime/core/providers/cpu/math/clip.h @@ -10,9 +10,9 @@ namespace onnxruntime { template -class Clip final : public OpKernel { +class Clip_6 final : public OpKernel { public: - Clip(const OpKernelInfo& info) : OpKernel(info) { + Clip_6(const OpKernelInfo& info) : OpKernel(info) { ORT_ENFORCE(info.GetAttr("max", &max_).IsOK()); ORT_ENFORCE(info.GetAttr("min", &min_).IsOK()); } @@ -32,4 +32,36 @@ class Clip final : public OpKernel { T min_; }; +template +class Clip final : public OpKernel { + public: + Clip(const OpKernelInfo& info) : OpKernel(info) { + } + + Status Compute(OpKernelContext* ctx) const override { + const auto* X = ctx->Input(0); + const auto* min = ctx->Input(1); + const auto* max = ctx->Input(2); + Tensor* Y = ctx->Output(0, X->Shape()); + + auto min_val = -std::numeric_limits::infinity(); + auto max_val = std::numeric_limits::infinity(); + if (min) { + ORT_ENFORCE(min->Shape().NumDimensions() == 0, "min should be a scalar."); + min_val = *(min->template Data()); + } + if (max) { + ORT_ENFORCE(max->Shape().NumDimensions() == 0, "max should be a scalar."); + max_val = *(max->template Data()); + } + + EigenVectorMap(Y->template MutableData(), Y->Shape().Size()) = + ConstEigenVectorMap(X->template Data(), X->Shape().Size()) + .cwiseMax(min_val) + .cwiseMin(max_val); + + return Status::OK(); + } +}; + } // namespace onnxruntime diff --git a/onnxruntime/test/onnx/main.cc b/onnxruntime/test/onnx/main.cc index 110aacfbc7e5e..525406f3c067c 100644 --- a/onnxruntime/test/onnx/main.cc +++ b/onnxruntime/test/onnx/main.cc @@ -404,13 +404,7 @@ int real_main(int argc, char* argv[], Ort::Env& env) { {"cumsum_1d_reverse_exclusive", "not implemented yet"}, {"cumsum_1d_reverse", "not implemented yet"}, {"cumsum_1d_exclusive", "not implemented yet"}, - {"cumsum_1d", "not implemented yet"}, - {"clip_splitbounds", "not implemented yet for opset 11"}, - {"clip_outbounds", "not implemented yet for opset 11"}, - {"clip_example", "not implemented yet for opset 11"}, - {"clip_default_min", "not implemented yet for opset 11"}, - {"clip_default_max", "not implemented yet for opset 11"}, - {"clip", "not implemented yet for opset 11"}, + {"cumsum_1d", "not implemented yet"}, }; #ifdef USE_NGRAPH diff --git a/onnxruntime/test/providers/cpu/math/clip_test.cc b/onnxruntime/test/providers/cpu/math/clip_test.cc index 7a80f6a9835be..2b6ce931a1556 100644 --- a/onnxruntime/test/providers/cpu/math/clip_test.cc +++ b/onnxruntime/test/providers/cpu/math/clip_test.cc @@ -7,8 +7,8 @@ namespace onnxruntime { namespace test { -TEST(MathOpTest, Clip) { - OpTester test("Clip"); +TEST(MathOpTest, Clip_6) { + OpTester test("Clip", 6); test.AddAttribute("min", -10.0f); test.AddAttribute("max", 10.0f); @@ -25,5 +25,37 @@ TEST(MathOpTest, Clip) { test.Run(); } +TEST(MathOpTest, Clip_Default) { + OpTester test("Clip", 11); + + std::vector dims{3, 3}; + test.AddInput("X", dims, + {11.0f, 4.4f, 432.3f, + -1.3f, 3.5f, 64.0f, + -5.4f, 9.3f, 82.4f}); + test.AddOutput("Y", dims, + {11.0f, 4.4f, 432.3f, + -1.3f, 3.5f, 64.0f, + -5.4f, 9.3f, 82.4f}); + test.Run(); +} + +TEST(MathOpTest, Clip) { + OpTester test("Clip", 11); + + std::vector dims{3, 3}; + test.AddInput("X", dims, + {-1.0f, 0.0f, 1.0f, + -6.0f, 0.0f, 6.0f, + -5.4f, 2.0f, 6.0f}); + test.AddInput("min", {}, {-5}); + test.AddInput("max", {}, {5}); + test.AddOutput("Y", dims, + {-1.0f, 0.0f, 1.0f, + -5.0f, 0.0f, 5.0f, + -5.0f, 2.0f, 5.0f}); + test.Run(); +} + } // namespace test } // namespace onnxruntime diff --git a/onnxruntime/test/python/onnx_backend_test_series.py b/onnxruntime/test/python/onnx_backend_test_series.py index 47d1855db08ad..0202df1927e38 100644 --- a/onnxruntime/test/python/onnx_backend_test_series.py +++ b/onnxruntime/test/python/onnx_backend_test_series.py @@ -116,7 +116,6 @@ def create_backend_test(testname=None): '^test_dynamicquantizelinear_max_adjusted_expanded*', '^test_dynamicquantizelinear_min_adjusted*', '^test_dynamicquantizelinear_min_adjusted_expanded*', - '^test_clip*', '^test_depthtospace*', '^test_gather_elements*', '^test_scatter_elements*', From 02ca14ebfced70ca3b2dd95f6cc820867b591fa7 Mon Sep 17 00:00:00 2001 From: Ashwini Khade Date: Tue, 20 Aug 2019 22:08:52 -0700 Subject: [PATCH 2/4] exclude ngraph provider for clip unit tests --- onnxruntime/test/providers/cpu/math/clip_test.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/onnxruntime/test/providers/cpu/math/clip_test.cc b/onnxruntime/test/providers/cpu/math/clip_test.cc index 2b6ce931a1556..f68f89d7ef4b1 100644 --- a/onnxruntime/test/providers/cpu/math/clip_test.cc +++ b/onnxruntime/test/providers/cpu/math/clip_test.cc @@ -37,7 +37,9 @@ TEST(MathOpTest, Clip_Default) { {11.0f, 4.4f, 432.3f, -1.3f, 3.5f, 64.0f, -5.4f, 9.3f, 82.4f}); - test.Run(); + + // nGraph does not support Clip opset 11 yet. + test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kNGraphExecutionProvider}); } TEST(MathOpTest, Clip) { @@ -54,7 +56,9 @@ TEST(MathOpTest, Clip) { {-1.0f, 0.0f, 1.0f, -5.0f, 0.0f, 5.0f, -5.0f, 2.0f, 5.0f}); - test.Run(); + + // nGraph does not support Clip opset 11 yet. + test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kNGraphExecutionProvider}); } } // namespace test From 7b54c9a6bda04ae3e4344933865c5cb4a0183d09 Mon Sep 17 00:00:00 2001 From: Ashwini Khade Date: Tue, 20 Aug 2019 22:18:29 -0700 Subject: [PATCH 3/4] exclude ngraph for all clip opset 11 tests --- onnxruntime/test/onnx/main.cc | 6 ++++++ onnxruntime/test/python/onnx_backend_test_series.py | 2 ++ 2 files changed, 8 insertions(+) diff --git a/onnxruntime/test/onnx/main.cc b/onnxruntime/test/onnx/main.cc index 525406f3c067c..db35cef4e912c 100644 --- a/onnxruntime/test/onnx/main.cc +++ b/onnxruntime/test/onnx/main.cc @@ -411,6 +411,12 @@ int real_main(int argc, char* argv[], Ort::Env& env) { broken_tests.insert({"dequantizelinear", "ambiguity in scalar dimensions [] vs [1]", {"onnx150"}}); broken_tests.insert({"qlinearconv", "ambiguity in scalar dimensions [] vs [1]"}); broken_tests.insert({"quantizelinear", "ambiguity in scalar dimensions [] vs [1]", {"onnx150"}}); + broken_tests.insert({"clip_splitbounds", "not implemented yet for opset 11"}); + broken_tests.insert({"clip_outbounds", "not implemented yet for opset 11"}); + broken_tests.insert({"clip_example", "not implemented yet for opset 11"}); + broken_tests.insert({"clip_default_min", "not implemented yet for opset 11"}); + broken_tests.insert({"clip_default_max", "not implemented yet for opset 11"}); + broken_tests.insert({"clip", "not implemented yet for opset 11"}); #endif #ifdef USE_MKLDNN diff --git a/onnxruntime/test/python/onnx_backend_test_series.py b/onnxruntime/test/python/onnx_backend_test_series.py index 0202df1927e38..a2f6011cced1b 100644 --- a/onnxruntime/test/python/onnx_backend_test_series.py +++ b/onnxruntime/test/python/onnx_backend_test_series.py @@ -126,6 +126,8 @@ def create_backend_test(testname=None): # Example of how to disable tests for a specific provider. # if c2.supports_device('NGRAPH'): # current_failing_tests = current_failing_tests + ('|^test_operator_repeat_dim_overflow_cpu.*',) + if c2.supports_device('NGRAPH'): + current_failing_tests = current_failing_tests + ('|^test_clip*',) filters = current_failing_tests + \ tests_with_pre_opset7_dependencies_filters() + \ From e96e6cdd0d3fd53ceed0d90ff407b11d8c3848ad Mon Sep 17 00:00:00 2001 From: Ashwini Khade Date: Wed, 21 Aug 2019 11:56:31 -0700 Subject: [PATCH 4/4] fix op version --- onnxruntime/core/providers/cpu/cpu_execution_provider.cc | 4 ++-- onnxruntime/core/providers/cpu/math/clip.cc | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/providers/cpu/cpu_execution_provider.cc b/onnxruntime/core/providers/cpu/cpu_execution_provider.cc index 6e3d2960d3930..6bc23ef14a967 100644 --- a/onnxruntime/core/providers/cpu/cpu_execution_provider.cc +++ b/onnxruntime/core/providers/cpu/cpu_execution_provider.cc @@ -18,7 +18,7 @@ namespace onnxruntime { // Forward declarations of op kernels -class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 6, Clip); +class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 6, 10, Clip); class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 6, Elu); class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 6, HardSigmoid); class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 6, LeakyRelu); @@ -303,7 +303,7 @@ class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 11, Cl void RegisterOnnxOperatorKernels(KernelRegistry& kernel_registry) { static const BuildKernelCreateInfoFn function_table[] = { - BuildKernelCreateInfo, + BuildKernelCreateInfo, BuildKernelCreateInfo, BuildKernelCreateInfo, BuildKernelCreateInfo, diff --git a/onnxruntime/core/providers/cpu/math/clip.cc b/onnxruntime/core/providers/cpu/math/clip.cc index f1e54aafde64d..dc99582ddca04 100644 --- a/onnxruntime/core/providers/cpu/math/clip.cc +++ b/onnxruntime/core/providers/cpu/math/clip.cc @@ -5,9 +5,10 @@ namespace onnxruntime { -ONNX_CPU_OPERATOR_KERNEL( +ONNX_CPU_OPERATOR_VERSIONED_KERNEL( Clip, 6, + 10, KernelDefBuilder().MayInplace(0, 0).TypeConstraint("T", DataTypeImpl::GetTensorType()), Clip_6);