openvinotoolkit / openvino

OpenVINO™ is an open-source toolkit for optimizing and deploying AI inference
https://docs.openvino.ai
Apache License 2.0
6.49k stars 2.11k forks source link

[Good First Issue]: Add new method to the Python API #25116

Closed akuporos closed 5 days ago

akuporos commented 4 weeks ago

Context

Needs to add the new method to openvino.Model class in Python API.

What needs to be done?

There are methods get_result_index and get_parameter_index of the Model class in Python API. We want to have get_sink_index here. Unlike the above methods, it is not implemented in the C++ API, so need to add it from scratch (only in Python API right now). It should take as input argument openvino.runtime.Output/openvino.runtime.Node -> thus, two overloads of the method should be implemented. The implementation of the remove_sink method may be useful here to check the type of the input Sync Node.

Also please cover this method by adding test to this file.

Example Pull Requests

No response

Resources

Contact points

@akuporos @p-wysocki @jiwaszki

Ticket

131043

kcin96 commented 3 weeks ago

.take

github-actions[bot] commented 3 weeks ago

Thank you for looking into this issue! Please let us know if you have any questions or require any help.

kcin96 commented 3 weeks ago

Is there a way to reduce the amount of build during the cmake step? i.e. limit Cmake build to only necessary files for this issue?

p-wysocki commented 3 weeks ago

Hello @kcin96, yes there is - you can take a look at available CMake flags at https://github.com/openvinotoolkit/openvino/blob/master/docs/dev/cmake_options_for_custom_compilation.md.

akuporos commented 3 weeks ago

Hi @kcin96,

You may turn off almost all Frontends (keep ENABLE_OV_IR_FRONTEND) and almost all plugins (keep ENABLE_INTEL_CPU and probably ENABLE_AUTO for running tests).

kcin96 commented 3 weeks ago

Thanks @akuporos & @p-wysocki! I've gotten to this point with the implementation of openvino.runtime.Node

    model.def("get_sink_index",
              [](ov::Model& self, const py::object& node) -> int64_t {
                if (py::isinstance<ov::op::v6::Assign>(node)) {
                    auto sink = std::dynamic_pointer_cast<ov::op::Sink>(node.cast<std::shared_ptr<ov::op::v6::Assign>>());
                    int64_t pos = 0;
                    for (const auto& s: self.get_sinks()) {
                        if (s == sink) {
                            return pos;
                        }
                        pos++;
                    };
                } else if (py::isinstance<ov::Node>(node)) {
                    auto sink = std::dynamic_pointer_cast<ov::op::Sink>(node.cast<std::shared_ptr<ov::Node>>());
                    int64_t pos = 0;
                    for (const auto& s: self.get_sinks()) {
                        if (s == sink) {
                            return pos;
                        }
                        pos++;
                    };
                } else {
                    throw py::type_error("Incorrect argument type. Sink node is expected as argument.");
                }
                return -1;
              },
              py::arg("sink"),
              R"(
                    Return index of sink.

                    Return -1 if `sink` not matched.

                    :param sink: Sink node.
                    :type sink: openvino.runtime.Node
                    :return: Index for sink node.
                    :rtype: int
                 )");

However, for openvino.runtime.Output I am not sure how sink nodes relate to Output? Also what's the function of sink nodes?

akuporos commented 2 weeks ago

Hi @kcin96,

Each Node has Output (or several Outputs). You can get sink Node from Output using get_node_shared_ptr() method.