From 81cf43c1d8bc06e7fdf1e37a6552db76c1549ead Mon Sep 17 00:00:00 2001 From: Ishita Singh Date: Fri, 20 Nov 2026 08:22:26 +0530 Subject: [PATCH] dnn: handle min > max in Clip layer and enable ONNX conformance test (#30891) * dnn: handle min > max in Clip layer and enable ONNX conformance test Under the ONNX specification, the Clip operator is defined mathematically as y = min(max(x, min), max). When min > max, the operation clamps identically to max for all input values. Previously, ClipLayerImpl asserted minValue <= maxValue in its constructor and actualMin <= actualMax in forward, causing assertion failures on valid ONNX models with min > max. Additionally, the adjacency graph fusion interpreter checked a < min first, returning min instead of max. This commit: 1. Removes the min <= max assertions in ClipLayerImpl constructor and forward, allowing ONNX-conforming evaluation where min > max clamps to max. 2. Updates FusionEltwiseOp::CLAMP in AdjacencyGraph interpreter to use std::min(std::max(a, n.scalar), n.scalar2), matching the ONNX evaluation order. 3. Removes test_clip_min_greater_than_max from the layer parser denylist. 4. Adds unit tests covering static and dynamic min > max clipping. * dnn: filter test_clip_min_greater_than_max for OpenVINO backend * dnn(test): address review comments on Clip min > max tests and fusion coverage --- modules/dnn/src/adjacency_graph.hpp | 2 +- modules/dnn/src/layers/clip_layer.cpp | 3 - modules/dnn/test/test_fusion.cpp | 4 + modules/dnn/test/test_layers.cpp | 76 +++++++++++++++++++ ...conformance_layer_filter__openvino.inl.hpp | 2 + ..._conformance_layer_parser_denylist.inl.hpp | 1 - 6 files changed, 83 insertions(+), 5 deletions(-) diff --git a/modules/dnn/src/adjacency_graph.hpp b/modules/dnn/src/adjacency_graph.hpp index b737cfaedf..53de500c74 100644 --- a/modules/dnn/src/adjacency_graph.hpp +++ b/modules/dnn/src/adjacency_graph.hpp @@ -437,7 +437,7 @@ inline float evalElement(const AdjacencyGraph& g, float x, case FusionEltwiseOp::SQRT: v[i] = std::sqrt(a); break; case FusionEltwiseOp::RECIP: v[i] = 1.f / a; break; case FusionEltwiseOp::CLAMP: - v[i] = a < n.scalar ? n.scalar : (a > n.scalar2 ? n.scalar2 : a); + v[i] = std::min(std::max(a, n.scalar), n.scalar2); break; case FusionEltwiseOp::TENSOR: // Only a sink that opted in is offered one, and it reads the nodes itself. diff --git a/modules/dnn/src/layers/clip_layer.cpp b/modules/dnn/src/layers/clip_layer.cpp index 95cdd1481c..16ba459565 100644 --- a/modules/dnn/src/layers/clip_layer.cpp +++ b/modules/dnn/src/layers/clip_layer.cpp @@ -85,8 +85,6 @@ public: hasMax = params.has("max"); if (hasMin) minValue = params.get("min"); if (hasMax) maxValue = params.get("max"); - if (hasMin && hasMax) - CV_Assert(minValue <= maxValue); } bool unfoldMath(LayerMath& r, const ConstOperand& side) const @@ -172,7 +170,6 @@ public: double actualMin = dynMin ? getScalar(inputs[1]) : (hasMin ? minValue : typeMin(data.depth())); double actualMax = dynMax ? getScalar(inputs[2]) : (hasMax ? maxValue : typeMax(data.depth())); - CV_Assert(actualMin <= actualMax); // Fused single-pass clamp for contiguous CV_32F. Halves memory traffic // vs the cv::max + cv::min pair, and lets the allocator run it in-place. diff --git a/modules/dnn/test/test_fusion.cpp b/modules/dnn/test/test_fusion.cpp index 5c46a97d10..040aad4327 100644 --- a/modules/dnn/test/test_fusion.cpp +++ b/modules/dnn/test/test_fusion.cpp @@ -139,6 +139,10 @@ TEST(Fusion, MathMatchesClosedForm) r.clamp(LayerMath::INPUT_VALUE, 0.f, 6.f); EXPECT_FLOAT_EQ(std::min(std::max(x, 0.f), 6.f), eval1(r, x)) << "clip " << x; + r = LayerMath(); + r.clamp(LayerMath::INPUT_VALUE, 6.f, 0.f); + EXPECT_FLOAT_EQ(std::min(std::max(x, 6.f), 0.f), eval1(r, x)) << "clip min > max " << x; + r = LayerMath(); fusion::detail::sigmoid(r); EXPECT_NEAR(1.f / (1.f + std::exp(-x)), eval1(r, x), 1e-5) << "sigmoid " << x; diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp index 72b5896561..02533cb934 100644 --- a/modules/dnn/test/test_layers.cpp +++ b/modules/dnn/test/test_layers.cpp @@ -4910,4 +4910,80 @@ INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Hardmax, Values( make_tuple(CV_64F, -std::numeric_limits::infinity()) )); +static void testClipMinGreaterThanMax(int depth, bool dynamic) +{ + const int total = 64; + Mat input(1, total, depth); + Mat ref(1, total, depth); + + const double minVal = 5.0; + const double maxVal = 2.0; + + if (depth == CV_32F) + { + float* inPtr = input.ptr(); + float* refPtr = ref.ptr(); + for (int i = 0; i < total; ++i) + { + inPtr[i] = (float)i * 0.5f - 15.0f; + refPtr[i] = (float)maxVal; + } + } + else // CV_64F + { + double* inPtr = input.ptr(); + double* refPtr = ref.ptr(); + for (int i = 0; i < total; ++i) + { + inPtr[i] = (double)i * 0.5 - 15.0; + refPtr[i] = maxVal; + } + } + + LayerParams lp; + lp.type = "Clip"; + lp.name = "ClipLayer"; + + std::vector inputs = {input}; + if (!dynamic) + { + lp.set("min", (float)minVal); + lp.set("max", (float)maxVal); + } + else + { + Mat minMat(1, 1, depth, Scalar(minVal)); + Mat maxMat(1, 1, depth, Scalar(maxVal)); + inputs.push_back(minMat); + inputs.push_back(maxMat); + } + + Ptr layer = LayerFactory::createLayerInstance("Clip", lp); + std::vector outputs; + runLayer(layer, inputs, outputs); + + ASSERT_EQ(outputs.size(), 1u); + normAssert(ref, outputs[0], "Clip min > max output mismatch"); +} + +TEST(Layer_Test_Clip, MinGreaterThanMax_Float32) +{ + testClipMinGreaterThanMax(CV_32F, false); +} + +TEST(Layer_Test_Clip, MinGreaterThanMax_Float64) +{ + testClipMinGreaterThanMax(CV_64F, false); +} + +TEST(Layer_Test_Clip, DynamicMinGreaterThanMax_Float32) +{ + testClipMinGreaterThanMax(CV_32F, true); +} + +TEST(Layer_Test_Clip, DynamicMinGreaterThanMax_Float64) +{ + testClipMinGreaterThanMax(CV_64F, true); +} + }} // namespace diff --git a/modules/dnn/test/test_onnx_conformance_layer_filter__openvino.inl.hpp b/modules/dnn/test/test_onnx_conformance_layer_filter__openvino.inl.hpp index f530ac870d..eb61a614ab 100644 --- a/modules/dnn/test/test_onnx_conformance_layer_filter__openvino.inl.hpp +++ b/modules/dnn/test/test_onnx_conformance_layer_filter__openvino.inl.hpp @@ -720,6 +720,8 @@ CASE(test_clip_inbounds) SKIP; CASE(test_clip_inbounds_expanded) SKIP; +CASE(test_clip_min_greater_than_max) + SKIP; CASE(test_clip_min_greater_than_max_expanded) SKIP; CASE(test_clip_outbounds) diff --git a/modules/dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hpp b/modules/dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hpp index eeee0cecb9..ef4b1a2da0 100644 --- a/modules/dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hpp +++ b/modules/dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hpp @@ -30,7 +30,6 @@ "test_bernoulli_seed_expanded", // ---- same as above --- "test_castlike_FLOAT_to_STRING", "test_castlike_STRING_to_FLOAT", // Issues::Layer::Can't create layer "onnx_node_output_0!output" of type "CastLike" in function 'getLayerInstance' -"test_clip_min_greater_than_max", "test_col2im", "test_col2im_5d", "test_col2im_dilations",