diff --git a/src/core/shape_inference/include/identity_shape_inference.hpp b/src/core/shape_inference/include/identity_shape_inference.hpp new file mode 100644 index 00000000000000..9b2657464aeda1 --- /dev/null +++ b/src/core/shape_inference/include/identity_shape_inference.hpp @@ -0,0 +1,22 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "openvino/op/identity.hpp" + +namespace ov { +namespace op { +namespace v16 { +template > +std::vector shape_infer(const Identity* op, const std::vector& input_shapes) { + NODE_VALIDATION_CHECK(op, input_shapes.size() == 1); + + const auto& input_shape = input_shapes[0]; + + return {input_shape}; +} +} // namespace v16 +} // namespace op +} // namespace ov diff --git a/src/plugins/intel_cpu/src/nodes/identity.cpp b/src/plugins/intel_cpu/src/nodes/identity.cpp index 999786316e71aa..bb44cc61f4e60a 100644 --- a/src/plugins/intel_cpu/src/nodes/identity.cpp +++ b/src/plugins/intel_cpu/src/nodes/identity.cpp @@ -30,11 +30,11 @@ Identity::Identity(const std::shared_ptr& op, const GraphContext::CPtr std::string errorMessage; if (!isSupportedOperation(op, errorMessage)) { THROW_CPU_NODE_ERR(errorMessage); - }\ + } - auto op = as_type_ptr(op); + auto identity_op = as_type_ptr(op); - if (is_type(op->get_input_node_ptr(0))) { + if (is_type(identity_op->get_input_node_ptr(0))) { m_const_input = true; constant = ConstantType::Const; // Node always produces the same output } else { @@ -57,7 +57,7 @@ void Identity::initSupportedPrimitiveDescriptors() { auto out_prc = getOriginalOutputPrecisionAtPort(0); if (shape_prc != out_prc) { - THROW_CPU_NODE_ERR("has to have the same dtype for input and output nodes.") + THROW_CPU_NODE_ERR("has to have the same dtype for input and output nodes."); } m_out_prc = out_prc; diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/identity.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/identity.cpp index e69128f91846b9..511befe18f9336 100644 --- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/identity.cpp +++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/identity.cpp @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // -#include "single_layer_tests/classes/identity.hpp" +#include "identity.hpp" #include "common_test_utils/node_builders/constant.hpp" using namespace CPUTestUtils; @@ -43,18 +43,18 @@ void IdentityLayerTestCPU::SetUp() { std::tie(inFmts, outFmts, priority, selectedType) = cpu_params; - updateSelectedType("ref_any", m_output_precision, configuration); + updateSelectedType("ref_any", output_precision, configuration); std::vector in_shapes; if (!const_input) { - in_shapes.push_back({{}, {{m_output_shape}}}); + in_shapes.push_back({{}, {{output_shape}}}); } else { - in_shapes.push_back({{m_output_shape}, {{m_output_shape}}}); + in_shapes.push_back({{output_shape}, {{output_shape}}}); } init_input_shapes(in_shapes); - const auto data = std::make_shared(m_output_precision, m_output_shape); + const auto data = std::make_shared(output_precision, output_shape); data->set_friendly_name("data"); const auto op = std::make_shared(data); @@ -65,7 +65,9 @@ void IdentityLayerTestCPU::SetUp() { void IdentityLayerTestCPU::generate_inputs(const std::vector& targetInputStaticShapes) { inputs.clear(); - auto tensor = ov::test::utils::create_and_fill_tensor(function->inputs()[0].get_element_type(), targetInputStaticShapes[0], InputGenerateData()); + const auto& func_inputs = function->inputs(); + const auto& func_input = func_inputs[0]; + auto tensor = ov::test::utils::create_and_fill_tensor(func_input.get_element_type(), targetInputStaticShapes[0], utils::InputGenerateData()); inputs.insert({func_input.get_node_shared_ptr(), tensor}); } diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/identity.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/identity.cpp index 5da448d827be17..4367792c7ed2fe 100644 --- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/identity.cpp +++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/identity.cpp @@ -17,7 +17,7 @@ static const std::vector shapes = { static const std::vector prc = { ElementType::f32, - ElementType::f16. + ElementType::f16, ElementType::bf16, ElementType::i32, ElementType::u16, diff --git a/src/plugins/intel_cpu/tests/unit/shape_inference_test/identity_shape_inference_test.cpp b/src/plugins/intel_cpu/tests/unit/shape_inference_test/identity_shape_inference_test.cpp new file mode 100644 index 00000000000000..e9f9f8f277b533 --- /dev/null +++ b/src/plugins/intel_cpu/tests/unit/shape_inference_test/identity_shape_inference_test.cpp @@ -0,0 +1,71 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "identity_shape_inference.hpp" +#include "utils.hpp" + +using namespace ov; +using namespace ov::intel_cpu; + +class Identityv14StaticShapeInferenceTest : public OpStaticShapeInferenceTest { +protected: +}; + +TEST_F(Identityv14StaticShapeInferenceTest, Identity_default_ctor) { + op = make_op(); + op->set_adjoint(false); + + input_shapes = ShapeVector{{2, 2}}; + auto output_shapes = shape_inference(op.get(), input_shapes); + + EXPECT_EQ(output_shapes.size(), 1); + EXPECT_EQ(output_shapes[0], StaticShape({2, 2})); +} + +TEST_F(Identityv14StaticShapeInferenceTest, Identity_4_4_small_matrix) { + auto data = std::make_shared(element::f32, PartialShape::dynamic(2)); + auto Identity = std::make_shared(data); + + input_shapes = ShapeVector{{4, 4}}; + auto output_shapes = shape_inference(Identity.get(), input_shapes); + ASSERT_EQ(output_shapes[0], StaticShape({4, 4})); +} + +TEST_F(Identityv14StaticShapeInferenceTest, Identity_10_10_big_matrix) { + auto data = std::make_shared(element::f32, PartialShape::dynamic(2)); + auto Identity = std::make_shared(data); + + input_shapes = ShapeVector{{10, 10}}; + auto output_shapes = shape_inference(Identity.get(), input_shapes); + ASSERT_EQ(output_shapes[0], StaticShape({10, 10})); +} + +TEST_F(Identityv14StaticShapeInferenceTest, Identity_10_1_1_keep_batch_when_single_cell_matrix) { + auto data = std::make_shared(element::f32, PartialShape::dynamic(3)); + auto Identity = std::make_shared(data); + + input_shapes = ShapeVector{{10, 1, 1}}; + auto output_shapes = shape_inference(Identity.get(), input_shapes); + ASSERT_EQ(output_shapes[0], StaticShape({10, 1, 1})); +} + +TEST_F(Identityv14StaticShapeInferenceTest, Identity_10_9_9_keep_batch_big_matrix) { + auto data = std::make_shared(element::f32, PartialShape::dynamic(3)); + auto Identity = std::make_shared(data); + + input_shapes = ShapeVector{{10, 9, 9}}; + auto output_shapes = shape_inference(Identity.get(), input_shapes); + ASSERT_EQ(output_shapes[0], StaticShape({10, 9, 9})); +} + +TEST_F(Identityv14StaticShapeInferenceTest, Identity_10_5_3_2_2_complex_multi_dim_matrix) { + auto data = std::make_shared(element::f32, PartialShape::dynamic(3)); + auto Identity = std::make_shared(data); + +input_shapes = ShapeVector{{10, 5, 3, 2, 2}}; + auto output_shapes = shape_inference(Identity.get(), input_shapes); + ASSERT_EQ(output_shapes[0], StaticShape({10, 5, 3, 2, 2})); +}