OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core][PyTorch] Fix FloorMod bounds evaluation for Swin3D (#39546)
Maxim Vafinf91b84942141
diff --git a/src/core/include/openvino/op/floor_mod.hpp b/src/core/include/openvino/op/floor_mod.hppindex 92f87afbce2..4e202467086 100644--- a/src/core/include/openvino/op/floor_mod.hpp+++ b/src/core/include/openvino/op/floor_mod.hpp@@ -35,6 +35,8 @@ public:std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;bool evaluate(TensorVector& outputs, const TensorVector& inputs) const override;+ bool evaluate_lower(TensorVector& outputs) const override;+ bool evaluate_upper(TensorVector& outputs) const override;bool has_evaluate() const override;};} // namespace v1diff --git a/src/core/src/op/floor_mod.cpp b/src/core/src/op/floor_mod.cppindex 4a7d3261da7..83b0be6198a 100644--- a/src/core/src/op/floor_mod.cpp+++ b/src/core/src/op/floor_mod.cpp@@ -4,6 +4,7 @@#include "openvino/op/floor_mod.hpp"+#include "bound_evaluate.hpp"#include "element_visitor.hpp"#include "itt.hpp"#include "openvino/reference/floor_mod.hpp"@@ -80,6 +81,17 @@ bool FloorMod::has_evaluate() const {return false;}}++bool FloorMod::evaluate_lower(TensorVector& outputs) const {+ // Remainders are not monotonic, and interval endpoints may include a zero divisor.+ return get_input_tensor(0).has_and_set_bound() && get_input_tensor(1).has_and_set_bound() &&+ default_lower_bound_evaluator(this, outputs);+}++bool FloorMod::evaluate_upper(TensorVector& outputs) const {+ return get_input_tensor(0).has_and_set_bound() && get_input_tensor(1).has_and_set_bound() &&+ default_upper_bound_evaluator(this, outputs);+}} // namespace v1} // namespace op} // namespace ovdiff --git a/src/core/tests/type_prop/floor_mod.cpp b/src/core/tests/type_prop/floor_mod.cppindex 73cc92abd10..f15ef68ba9d 100644--- a/src/core/tests/type_prop/floor_mod.cpp+++ b/src/core/tests/type_prop/floor_mod.cpp@@ -5,7 +5,53 @@#include "openvino/op/floor_mod.hpp"#include "arithmetic_ops.hpp"+#include "openvino/core/bound_evaluation_util.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/shape_of.hpp"using Type = ::testing::Types<ov::op::v1::FloorMod>;INSTANTIATE_TYPED_TEST_SUITE_P(type_prop_floormod, ArithmeticOperator, Type);++TEST(type_prop_floormod, constant_bounds) {+ const auto lhs = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {-7, 7, -7, 7});+ const auto rhs = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {3, -3, -3, 3});+ const auto mod = std::make_shared<ov::op::v1::FloorMod>(lhs, rhs);+ const auto& [lower, upper] = ov::util::evaluate_both_bounds(mod);++ ASSERT_TRUE(lower);+ ASSERT_TRUE(upper);+ const std::vector<int64_t> expected{2, -2, -1, 1};+ EXPECT_EQ(std::vector<int64_t>(lower.data<int64_t>(), lower.data<int64_t>() + 4), expected);+ EXPECT_EQ(std::vector<int64_t>(upper.data<int64_t>(), upper.data<int64_t>() + 4), expected);+}++TEST(type_prop_floormod, static_shape_bounds) {+ const auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{7, 10});+ const auto shape = std::make_shared<ov::op::v3::ShapeOf>(data);+ const auto divisor = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {3});+ const auto mod = std::make_shared<ov::op::v1::FloorMod>(shape, divisor);+ const auto bounds = ov::util::evaluate_both_bounds(mod);++ ASSERT_TRUE(bounds.first);+ ASSERT_TRUE(bounds.second);+ EXPECT_EQ(bounds.first.data<int64_t>()[0], 1);+ EXPECT_EQ(bounds.first.data<int64_t>()[1], 1);+ EXPECT_EQ(bounds.second.data<int64_t>()[0], 1);+ EXPECT_EQ(bounds.second.data<int64_t>()[1], 1);+}++TEST(type_prop_floormod, interval_bounds_are_not_evaluated_at_endpoints) {+ for (const auto& shape : {ov::PartialShape{-1}, ov::PartialShape{{0, 7}}, ov::PartialShape{{5, 7}}}) {+ const auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);+ const auto dims = std::make_shared<ov::op::v3::ShapeOf>(data);+ const auto three = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {3});+ for (const auto& inputs :+ {ov::OutputVector{dims, dims}, ov::OutputVector{dims, three}, ov::OutputVector{three, dims}}) {+ const auto mod = std::make_shared<ov::op::v1::FloorMod>(inputs[0], inputs[1]);+ const auto bounds = ov::util::evaluate_both_bounds(mod);+ EXPECT_FALSE(bounds.first);+ EXPECT_FALSE(bounds.second);+ }+ }+}diff --git a/tests/layer_tests/pytorch_tests/test_pad.py b/tests/layer_tests/pytorch_tests/test_pad.pyindex 1c0c72dc39b..a2993997fb1 100644--- a/tests/layer_tests/pytorch_tests/test_pad.py+++ b/tests/layer_tests/pytorch_tests/test_pad.py@@ -109,6 +109,27 @@ class TestPad(PytorchLayerTest):kwargs_to_prepare_input={"ndim": 2, "dtype": dtype}, trace_model=True)+class TestPadDynamicWindow(PytorchLayerTest):+ def _prepare_input(self):+ return (self.random.randn(1, 3, 7, 7),)++ def create_model(self):+ import torch++ class PadDynamicWindow(torch.nn.Module):+ def forward(self, x):+ window = x.shape[-1]+ pad = (window - x.shape[-1] % window) % window+ return torch.nn.functional.pad(x, (0, pad))++ return PadDynamicWindow(), "aten::pad"++ @pytest.mark.precommit+ @pytest.mark.nightly+ def test_pad_dynamic_window(self, ie_device, precision, ir_version):+ self._test(*self.create_model(), ie_device, precision, ir_version, trace_model=True)++class TestPadListPaddingings(PytorchLayerTest):def _prepare_input(self, ndim=4, pad_w=0, pad_h=0, dtype="float32"):import numpy as npdiff --git a/tests/model_hub_tests/pytorch/test_torchvision_models.py b/tests/model_hub_tests/pytorch/test_torchvision_models.pyindex 75dcfae8b96..cdbd24cfe29 100644--- a/tests/model_hub_tests/pytorch/test_torchvision_models.py+++ b/tests/model_hub_tests/pytorch/test_torchvision_models.py@@ -26,10 +26,11 @@ class TestTorchHubConvertModel(TestTorchConvertModel):def load_model(self, model_name, model_link):m = get_model(model_name, weights='DEFAULT')m.eval()- if model_name == "s3d" or any(m in model_name for m in ["swin3d", "r3d_18", "mc3_18", "r2plus1d_18"]):- self.example = (torch.randn([2, 3, 224, 224, 224]),)- self.inputs = (torch.randn([3, 3, 224, 224, 224]),)- elif "mvit" in model_name:+ if any(m in model_name for m in ["swin3d", "r3d_18", "mc3_18", "r2plus1d_18"]):+ # Short low-resolution clips keep video tests affordable.+ self.example = (torch.randn([2, 3, 8, 64, 64]),)+ self.inputs = (torch.randn([3, 3, 8, 64, 64]),)+ elif model_name == "s3d" or "mvit" in model_name:# 16 frames from videoself.example = (torch.randn(2, 3, 16, 224, 224),)self.inputs = (torch.randn(3, 3, 16, 224, 224),)@@ -93,6 +94,7 @@ class TestTorchHubConvertModel(TestTorchConvertModel):models.extend(["raft_small","swin_v2_s",+ "swin3d_t","quantized_mobilenet_v3_large",])return models