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

[Docs][PyOV] Documentation for Custom Python Operation #25615

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
205044d
[Docs][PyOV] Documentation for Custom Python Operation
akuporos Jul 17, 2024
13c0a10
make the snippet work
akuporos Jul 18, 2024
5b7f90e
update dpc
akuporos Jul 18, 2024
e0f40f9
try to fix doc build
akuporos Jul 18, 2024
8a417a6
Merge branch 'master' into akup/custom-op-docs
akuporos Jul 22, 2024
95ba862
change a little bit
akuporos Jul 22, 2024
091af42
Update docs/articles_en/documentation/openvino-extensibility/custom-o…
akuporos Jul 22, 2024
3cb000a
more changes
akuporos Jul 22, 2024
9939776
Update docs/articles_en/documentation/openvino-extensibility.rst
akuporos Jul 22, 2024
f0c3733
Merge branch 'master' into akup/custom-op-docs
mlukasze Jul 23, 2024
4e20847
Merge branch 'master' into akup/custom-op-docs
akuporos Jul 30, 2024
a237a03
Update docs/articles_en/documentation/openvino-extensibility.rst
akuporos Jul 30, 2024
753562d
Update docs/articles_en/documentation/openvino-extensibility.rst
akuporos Jul 30, 2024
9ba1424
Update docs/articles_en/documentation/openvino-extensibility.rst
akuporos Jul 30, 2024
d25ac84
Update docs/articles_en/documentation/openvino-extensibility/custom-o…
akuporos Jul 30, 2024
1c4732f
apply comment
akuporos Jul 31, 2024
110fd42
Merge branch 'master' into akup/custom-op-docs
akuporos Sep 6, 2024
c4d1de8
Update docs/articles_en/documentation/openvino-extensibility/custom-o…
kblaszczak-intel Sep 12, 2024
815bba4
Merge branch 'master' into akup/custom-op-docs
akuporos Sep 13, 2024
32d9076
Merge branch 'master' into akup/custom-op-docs
akuporos Sep 28, 2024
0d0543f
Merge branch 'master' into akup/custom-op-docs
akuporos Oct 14, 2024
b505644
Merge branch 'master' into akup/custom-op-docs
kblaszczak-intel Oct 28, 2024
77040b4
Merge branch 'master' into akup/custom-op-docs
akuporos Jan 16, 2025
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
57 changes: 57 additions & 0 deletions docs/articles_en/assets/snippets/ov_custom_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

# ! [op:common_include]
import numpy as np
from openvino import Op
from openvino.runtime import DiscreteTypeInfo
# ! [op:common_include]



# ! [op:header]
class Identity(Op):
class_type_info = DiscreteTypeInfo("Identity", "extension")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required to name it class_type_info, or is it just an example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I omit "extension" part?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required to name it class_type_info, or is it just an example?

Just an example

Can I omit "extension" part?

No, right now DiscreteTypeInfo doesn't have a default value for the second version_id argument


def get_type_info(self):
return Identity.class_type_info
# ! [op:header]

# ! [op:ctor]
def __init__(self, inputs, attrs=None):
super().__init__(self)
self.set_arguments(inputs)
self.constructor_validate_and_infer_types()
if attrs is not None:
self._attrs = attrs
mlukasze marked this conversation as resolved.
Show resolved Hide resolved
# ! [op:ctor]

# ! [op:validate]
def validate_and_infer_types(self):
self.set_output_type(0, self.get_input_element_type(0), self.get_input_partial_shape(0))
# ! [op:validate]

# ! [op:copy]
def clone_with_new_inputs(self, new_inputs):
return Identity(new_inputs)
# ! [op:copy]

# ! [op:evaluate]
def evaluate(self, outputs, inputs):
if np.array_equal(outputs[0].data, inputs[0].data): # Nothing to do
return True
mlukasze marked this conversation as resolved.
Show resolved Hide resolved
outputs[0].shape = inputs[0].shape
mlukasze marked this conversation as resolved.
Show resolved Hide resolved
inputs[0].copy_to(outputs[0])
return True

def has_evaluate(self):
return True
Comment on lines +44 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid doing that by just defining evaluate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand this method is obligatory and always goes together with evaluate

# ! [op:evaluate]

# ! [op:visit_attributes]
def visit_attributes(self, visitor):
if hasattr(self, "_attrs"):
visitor.on_attributes(self._attrs)
return True
# ! [op:visit_attributes]
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ The ``Identity`` is a custom operation class defined in :doc:`Custom Operation G
:language: cpp
:fragment: [add_frontend_extension]

When Python API is used, there is no way to implement a custom OpenVINO operation. Even if custom OpenVINO operation is implemented in C++ and loaded into the runtime by a shared library, there is still no way to add a frontend mapping extension that refers to this custom operation. In this case, use C++ shared library approach to implement both operations semantics and framework mapping.
The Python API now supports the implementation of custom OpenVINO operations, allowing for direct integration within Python code. Even if custom OpenVINO operation is implemented in C++ and loaded into the runtime by a shared library, there is still no way to add a frontend mapping extension that refers to this custom operation. In this case, use C++ shared library approach to implement both operations semantics and framework mapping.
akuporos marked this conversation as resolved.
Show resolved Hide resolved

Python can still be used to map and decompose operations when only operations from the standard OpenVINO operation set are used.

akuporos marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,54 @@ Custom OpenVINO Operations
custom operations to support models with operations
not supported by OpenVINO.

OpenVINO™ Extension API allows you to register custom operations to support models with operations which OpenVINO™ does not support out-of-the-box. This capability requires writing code in C++, so if you are using Python to develop your application you need to build a separate shared library implemented in C++ first and load it in Python using ``add_extension`` API. Please refer to :ref:`Create library with extensions <create_a_library_with_extensions>` for more details on library creation and usage. The remaining part of this document describes how to implement an operation class.
OpenVINO™ Extension API allows you to register custom operations to support models with operations which OpenVINO™ does not support out-of-the-box. While this process historically involved writing C++ code and constructing a separate shared library to be subsequently loaded into Python using the ``add_extension`` API, it is now also possible to implement custom operations directly within the Python API.
akuporos marked this conversation as resolved.
Show resolved Hide resolved

For those who still wish to use C++, the capability to create a shared library with custom operation implementations is maintained. Please refer to :ref:`Create library with extensions <create_a_library_with_extensions>` for more details on library creation and usage. The remaining part of this document describes how to implement an operation class using both the C++ API and Python API.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ extensions and libraries with such extensions are the first class citizen in OV, there shouldn't be any "still" or "is maintained", I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For those who still wish to use C++, the capability to create a shared library with custom operation implementations is maintained. Please refer to :ref:`Create library with extensions <create_a_library_with_extensions>` for more details on library creation and usage. The remaining part of this document describes how to implement an operation class using both the C++ API and Python API.
For those who wish to use C++, the capability to create a shared library with custom operation implementations is available. Please refer to :ref:`Create library with extensions <create_a_library_with_extensions>` for more details on library creation and usage. The remaining part of this document describes how to implement an operation class using both the C++ API and Python API.


Operation Class
###############

To add your custom operation, create a new class that extends ``ov::Op``, which is in turn derived from ``ov::Node``, the base class for all graph operations in OpenVINO™. To add ``ov::Op``, include the next file:

.. doxygensnippet:: src/core/template_extension/identity.hpp
:language: cpp
:fragment: [op:common_include]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: src/core/template_extension/identity.hpp
:language: cpp
:fragment: [op:common_include]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_custom_op.py
:language: python
:fragment: [op:common_include]

Follow the steps below to add a custom operation:

1. Add the ``OPENVINO_OP`` macro which defines a ``NodeTypeInfo`` object that identifies the type of the operation to the graph users and helps with dynamic type resolution. The type info of an operation currently consists of a string operation identifier and a string for operation version.
1. Define the operation type information:

a. For the C++ API, add the ``OPENVINO_OP`` macro which defines a ``NodeTypeInfo`` object that identifies the type of the operation to the graph users and helps with dynamic type resolution. The type info of an operation currently consists of a string operation identifier and a string for operation version.

b. For the Python API, define the ``class_type_info`` attribute in your custom operation class. This attribute should be an instance of ``DiscreteTypeInfo``, encapsulating both the operation name, which serves as a unique string identifier, and the operation version or namespace, represented as a second string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should it be a class field and also a function or only one is enough? See another comment in the snippet.


.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: src/core/template_extension/identity.hpp
:language: cpp
:fragment: [op:header]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_custom_op.py
:language: python
:fragment: [op:header]

2. Implement default constructor and constructors that optionally take the operation inputs and attributes as parameters.

Expand All @@ -45,43 +79,103 @@ OpenVINO™ operation contains two constructors:
* Default constructor, which enables you to create an operation without attributes
* Constructor that creates and validates an operation with specified inputs and attributes

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:ctor]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:ctor]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_custom_op.py
:language: python
:fragment: [op:ctor]

``validate_and_infer_types()``
++++++++++++++++++++++++++++++

``ov::Node::validate_and_infer_types`` method validates operation attributes and calculates output shapes using attributes of the operation.

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:validate]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:validate]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_custom_op.py
:language: python
:fragment: [op:validate]

``clone_with_new_inputs()``
+++++++++++++++++++++++++++

``ov::Node::clone_with_new_inputs`` method creates a copy of the operation with new inputs.

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:copy]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:copy]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_custom_op.py
:language: python
:fragment: [op:copy]

``visit_attributes()``
++++++++++++++++++++++

``ov::Node::visit_attributes`` method enables you to visit all operation attributes.

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:visit_attributes]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:visit_attributes]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_custom_op.py
:language: python
:fragment: [op:visit_attributes]

``evaluate() and has_evaluate()``
+++++++++++++++++++++++++++++++++

``ov::Node::evaluate`` method enables you to apply constant folding to an operation.

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:evaluate]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: src/core/template_extension/identity.cpp
:language: cpp
:fragment: [op:evaluate]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_custom_op.py
:language: python
:fragment: [op:evaluate]

Loading