Skip to content

Commit

Permalink
[PyAPI] Add Identity-16 Operation (#26717)
Browse files Browse the repository at this point in the history
### Details:
 - Extends Python API with Identity operation 
- Identity op Core/Ref files should disappear after merging:
#26716

### Tickets:
 - 152728

---------

Co-authored-by: Mateusz Mikolajczyk <[email protected]>
  • Loading branch information
PiotrKrzem and mmikolajcz authored Nov 8, 2024
1 parent 6aaddd5 commit 3c5744e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

# New operations added in Opset16
from openvino.runtime.opset16.ops import identity

# Operators from previous opsets
# TODO (ticket: 156877): Add previous opset operators at the end of opset16 development
22 changes: 22 additions & 0 deletions src/bindings/python/src/openvino/runtime/opset16/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@

"""Factory functions for ops added to openvino opset16."""
from functools import partial
from typing import Optional

from openvino.runtime import Node
from openvino.runtime.utils.decorators import nameable_op
from openvino.runtime.opset_utils import _get_node_factory
from openvino.runtime.utils.types import NodeInput, as_nodes

_get_node_factory_opset16 = partial(_get_node_factory, "opset16")

# -------------------------------------------- ops ------------------------------------------------


@nameable_op
def identity(
data: NodeInput,
name: Optional[str] = None,
) -> Node:
"""Identity operation is used as a placeholder. It creates a copy of the input to forward to the output.
:param data: Tensor with data.
:return: The new node performing Identity operation.
"""
return _get_node_factory_opset16().create(
"Identity",
as_nodes(data, name=name),
{},
)
31 changes: 31 additions & 0 deletions src/bindings/python/tests/test_graph/test_identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import numpy as np
import pytest

from openvino.runtime.opset15 import parameter
from openvino.runtime.opset16 import identity
from openvino import PartialShape, Type


@pytest.mark.parametrize(
("input_shape", "expected_output_shape"),
[
([4, 4], PartialShape([4, 4])),
([10, 8, 8], PartialShape([10, 8, 8])),
([-1, -1, -1], PartialShape([-1, -1, -1])),
([10, -1, -1], PartialShape([10, -1, -1])),
],
)
@pytest.mark.parametrize("op_name", ["identity", "identityOpset16"])
def test_inverse_param_inputs(input_shape, expected_output_shape, op_name):
data = parameter(input_shape, dtype=np.float32)

op = identity(data, name=op_name)
assert op.get_output_size() == 1
assert op.get_type_name() == "Identity"
assert op.get_friendly_name() == op_name
assert op.get_output_element_type(0) == Type.f32
assert op.get_output_partial_shape(0) == expected_output_shape

0 comments on commit 3c5744e

Please sign in to comment.