Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ref][Core][Opset15] Identity operation #26716

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/core/include/openvino/op/identity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "openvino/op/op.hpp"

namespace ov {
namespace op {
namespace v15 {
/// \brief Identity operation is used as a placeholder op.
///
/// \ingroup ov_ops_cpp_api
class OPENVINO_API Identity : public Op {
public:
OPENVINO_OP("Identity", "opset15");
Identity() = default;
/**
* @brief Identity operation is used as a placeholder. It copies the tensor data to the output.
*/
Identity(const Output<Node>& data);

bool visit_attributes(AttributeVisitor& visitor) override;
void validate_and_infer_types() override;
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
};
} // namespace v15
} // namespace op
} // namespace ov
1 change: 1 addition & 0 deletions src/core/include/openvino/op/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
#include "openvino/op/hswish.hpp"
#include "openvino/op/i420_to_bgr.hpp"
#include "openvino/op/i420_to_rgb.hpp"
#include "openvino/op/identity.hpp"
#include "openvino/op/idft.hpp"
#include "openvino/op/if.hpp"
#include "openvino/op/interpolate.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/core/include/openvino/opsets/opset15_tbl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ _OPENVINO_OP_REG(BitwiseLeftShift, ov::op::v15)
_OPENVINO_OP_REG(BitwiseRightShift, ov::op::v15)
_OPENVINO_OP_REG(SliceScatter, ov::op::v15)
_OPENVINO_OP_REG(SearchSorted, ov::op::v15)
_OPENVINO_OP_REG(Identity, ov::op::v15)
21 changes: 21 additions & 0 deletions src/core/reference/include/openvino/reference/identity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <cstring>

namespace ov {
namespace reference {
/**
* @brief Identity operation computes the identity of the input tensor.
*
* @param input Input matrix (matrices) pointer.
* @param output Output matrix (matrices) pointer.
**/
static inline void identity(const char* input, char* output, const size_t size_in_bytes) {
std::memcpy(output, input, size_in_bytes);
}
} // namespace reference
} // namespace ov
44 changes: 44 additions & 0 deletions src/core/src/op/identity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/identity.hpp"

#include <cstring>

#include "itt.hpp"
#include "openvino/core/attribute_visitor.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/op/util/op_types.hpp"
#include "openvino/reference/identity.hpp"

namespace ov {
namespace op {
namespace v15 {

Identity::Identity(const Output<Node>& data) : Op({data}) {
constructor_validate_and_infer_types();
}

bool Identity::Identity::visit_attributes(AttributeVisitor& visitor) {
OV_OP_SCOPE(v15_Identity_visit_attributes);
return true;
}

void Identity::Identity::validate_and_infer_types() {
OV_OP_SCOPE(v15_Identity_validate_and_infer_types);

const auto input_shapes = ov::util::get_node_input_partial_shapes(*this);

set_output_type(0, get_input_element_type(0), input_shapes[0]);
}

std::shared_ptr<Node> Identity::Identity::clone_with_new_inputs(const OutputVector& new_args) const {
OV_OP_SCOPE(v15_Identity_clone_with_new_inputs);
check_new_args_count(this, new_args);

return std::make_shared<Identity>(new_args.at(0));
}
} // namespace v15
} // namespace op
} // namespace ov
2 changes: 1 addition & 1 deletion src/core/tests/opset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ INSTANTIATE_TEST_SUITE_P(opset,
OpsetTestParams{ov::get_opset12, 178},
OpsetTestParams{ov::get_opset13, 186},
OpsetTestParams{ov::get_opset14, 188},
OpsetTestParams{ov::get_opset15, 15}),
OpsetTestParams{ov::get_opset15, 16}),
OpsetTestNameGenerator{});

class MyOpOld : public ov::op::Op {
Expand Down
32 changes: 32 additions & 0 deletions src/core/tests/type_prop/identity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/identity.hpp"

#include <gtest/gtest.h>

#include "common_test_utils/test_assertions.hpp"
#include "common_test_utils/type_prop.hpp"
#include "openvino/op/constant.hpp"

using namespace testing;

namespace ov {
namespace test {

class TypePropIdentityV15Test : public TypePropOpTest<op::v15::Identity> {};

TEST_F(TypePropIdentityV15Test, default_ctor) {
PiotrKrzem marked this conversation as resolved.
Show resolved Hide resolved
PiotrKrzem marked this conversation as resolved.
Show resolved Hide resolved
const auto data = op::v0::Constant::create(element::f64, Shape{2, 2}, {1.0f, 1.0f, 1.0f, 1.0f});
const auto op = make_op();
op->set_arguments(OutputVector{data});
op->validate_and_infer_types();

EXPECT_EQ(op->get_input_size(), 1);
EXPECT_EQ(op->get_output_size(), 1);
EXPECT_EQ(op->get_output_element_type(0), element::f64);
EXPECT_EQ(op->get_output_partial_shape(0), PartialShape({2, 2}));
}
} // namespace test
} // namespace ov
28 changes: 28 additions & 0 deletions src/core/tests/visitors/op/identity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/identity.hpp"

#include <gtest/gtest.h>

#include "openvino/op/unique.hpp"
#include "visitors/visitors.hpp"

namespace ov {
namespace test {

TEST(attributes, Identity) {
NodeBuilder::opset().insert<ov::op::v15::Identity>();
const auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{2, 2});

const auto op = std::make_shared<ov::op::v15::Identity>(data);
NodeBuilder builder(op, {data});
auto g_identity = ov::as_type_ptr<ov::op::v15::Identity>(builder.create());

constexpr auto expected_attr_count = 0;
EXPECT_EQ(builder.get_value_map_size(), expected_attr_count);
}

} // namespace test
} // namespace ov
25 changes: 25 additions & 0 deletions src/plugins/template/backend/ops/identity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/reference/identity.hpp"

#include "evaluate_node.hpp"
#include "openvino/core/type/element_iterator.hpp"

template <>
bool evaluate_node<ov::op::v15::Identity>(std::shared_ptr<ov::Node> node,
ov::TensorVector& outputs,
const ov::TensorVector& inputs) {
const auto input_shape = inputs[0].get_shape();
const auto total_elements =
std::accumulate(input_shape.begin(), input_shape.end(), static_cast<size_t>(1), std::multiplies<size_t>());
const auto total_size_in_bytes = get_memory_size(inputs[0].get_element_type(), total_elements);

outputs[0].set_shape(input_shape);

ov::reference::identity(static_cast<const char*>(inputs[0].data()),
static_cast<char*>(outputs[0].data()),
total_size_in_bytes);
return true;
}
4 changes: 4 additions & 0 deletions src/plugins/template/backend/ops/ops_evaluates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ extern template bool evaluate_node<ov::op::v15::EmbeddingBagPacked>(std::shared_
ov::TensorVector& outputs,
const ov::TensorVector& inputs);

extern template bool evaluate_node<ov::op::v15::Identity>(std::shared_ptr<ov::Node> node,
ov::TensorVector& outputs,
const ov::TensorVector& inputs);

extern template bool evaluate_node<ov::op::v15::SliceScatter>(std::shared_ptr<ov::Node> node,
ov::TensorVector& outputs,
const ov::TensorVector& inputs);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/template/backend/opset_int_tbl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,18 @@ _OPENVINO_OP_REG(AvgPool, ov::op::v14)
_OPENVINO_OP_REG(MaxPool, ov::op::v14)

_OPENVINO_OP_REG(ROIAlignRotated, ov::op::v15)

_OPENVINO_OP_REG(EmbeddingBagOffsets, op::v15)
_OPENVINO_OP_REG(EmbeddingBagPacked, op::v15)
_OPENVINO_OP_REG(Col2Im, ov::op::v15)

_OPENVINO_OP_REG(STFT, ov::op::v15)
_OPENVINO_OP_REG(StringTensorUnpack, ov::op::v15)
_OPENVINO_OP_REG(StringTensorPack, ov::op::v15)
_OPENVINO_OP_REG(BitwiseLeftShift, ov::op::v15)
_OPENVINO_OP_REG(BitwiseRightShift, ov::op::v15)
_OPENVINO_OP_REG(SliceScatter, ov::op::v15)
_OPENVINO_OP_REG(SearchSorted, ov::op::v15)
_OPENVINO_OP_REG(Identity, ov::op::v15)

_OPENVINO_OP_REG(AUGRUCell, ov::op::internal)
_OPENVINO_OP_REG(AUGRUSequence, ov::op::internal)
Expand Down
92 changes: 92 additions & 0 deletions src/plugins/template/tests/functional/op_reference/identity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/identity.hpp"

#include "base_reference_test.hpp"
#include "gtest/gtest.h"
#include "openvino/op/parameter.hpp"

namespace {
using IdentityParams = std::tuple<reference_tests::Tensor, std::string>;

class ReferenceIdentity : public testing::TestWithParam<IdentityParams>, public reference_tests::CommonReferenceTest {
public:
void SetUp() override {
const auto& params = GetParam();
const auto& matrices = std::get<0>(params);
function = CreateFunction(params);
inputData = {matrices.data};
refOutData = {matrices.data};
}

static std::string getTestCaseName(const testing::TestParamInfo<IdentityParams>& obj) {
std::ostringstream name;
const auto& matrices = std::get<0>(obj.param);
name << std::get<1>(obj.param);
name << "_input_type_";
name << matrices.type;
name << "_shape_";
name << matrices.shape;
return name.str();
}

private:
static std::shared_ptr<ov::Model> CreateFunction(const IdentityParams& params) {
const auto& matrices = std::get<0>(params);
const auto in_matrices = std::make_shared<ov::op::v0::Parameter>(matrices.type, matrices.shape);
const auto identity = std::make_shared<ov::op::v15::Identity>(in_matrices);
return std::make_shared<ov::Model>(identity->outputs(), ov::ParameterVector{in_matrices});
}
};

template <ov::element::Type_t ET>
std::vector<IdentityParams> generateIdentityParams() {
using VT = typename ov::element_type_traits<ET>::value_type;

const ov::Shape matrices_2_2_shape{2, 2};
const ov::Shape matrices_2_3_3_shape{2, 3, 3};

reference_tests::Tensor matrices_2_2(matrices_2_2_shape, ET, std::vector<VT>{1, 2, 4, 5});

reference_tests::Tensor matrices_2_3_3(matrices_2_3_3_shape,
ET,
std::vector<VT>{1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 4, 5, 3, 1, 2, 6});

std::vector<IdentityParams> params;
params.emplace_back(matrices_2_2, "single");
params.emplace_back(matrices_2_3_3, "many");

return params;
}

std::vector<IdentityParams> generateIdentityParams() {
std::vector<std::vector<IdentityParams>> combo_params{generateIdentityParams<ov::element::boolean>(),
generateIdentityParams<ov::element::bf16>(),
generateIdentityParams<ov::element::f16>(),
generateIdentityParams<ov::element::f64>(),
generateIdentityParams<ov::element::f32>(),
generateIdentityParams<ov::element::i8>(),
generateIdentityParams<ov::element::i16>(),
generateIdentityParams<ov::element::i32>(),
generateIdentityParams<ov::element::i64>(),
generateIdentityParams<ov::element::u8>(),
generateIdentityParams<ov::element::u16>(),
generateIdentityParams<ov::element::u32>(),
generateIdentityParams<ov::element::u64>()};
std::vector<IdentityParams> test_params;
for (auto& params : combo_params)
std::move(params.begin(), params.end(), std::back_inserter(test_params));
return test_params;
}
} // namespace

TEST_P(ReferenceIdentity, CompareWithRefs) {
Exec();
}

INSTANTIATE_TEST_SUITE_P(smoke,
ReferenceIdentity,
::testing::ValuesIn(generateIdentityParams()),
ReferenceIdentity::getTestCaseName);
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,13 @@ std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v0::Interpolat
return std::make_shared<ov::Model>(results, params, "Interpolat-1");
}

std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v15::Identity>& node) {
ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{4, 4, 4})};
const auto identity = std::make_shared<ov::op::v15::Identity>(params[0]);
ov::ResultVector results{std::make_shared<ov::op::v0::Result>(identity)};
return std::make_shared<ov::Model>(results, params, "Identity");
}

std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v4::Interpolate> &node) {
using InterpolateAttrs = op::v4::Interpolate::InterpolateAttrs;
using InterpolateMode = op::v4::Interpolate::InterpolateMode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "shared_test_classes/single_op/identity.hpp"

namespace ov {
namespace test {
TEST_P(IdentityLayerTest, Inference) {
run();
};
} // namespace test
} // namespace ov
Loading