class ov::Model

Overview

A user-defined model. More…

#include <model.hpp>

class Model: public std::enable_shared_from_this< Model >
{
public:
    // typedefs

    typedef std::function<std::vector<std::shared_ptr<ov::Node>>(const std::vector<std::shared_ptr<ov::Node>>&root_nodes)> topological_sort_t;

    // fields

    static const ov::DiscreteTypeInfo type_info;

    // construction

    Model(
        const ov::NodeVector& results,
        const ov::ParameterVector& parameters,
        const std::string& name = ""
        );

    Model(
        const ov::OutputVector& results,
        const ov::ParameterVector& parameters,
        const std::string& name = ""
        );

    Model(
        const std::shared_ptr<ov::Node>& result,
        const ov::ParameterVector& parameters,
        const std::string& name = ""
        );

    Model(
        const ov::ResultVector& results,
        const ov::ParameterVector& parameters,
        const std::string& name = ""
        );

    Model(
        const ov::ResultVector& results,
        const ov::SinkVector& sinks,
        const ov::ParameterVector& parameters,
        const std::string& name = ""
        );

    Model(
        const ov::OutputVector& results,
        const ov::SinkVector& sinks,
        const ov::ParameterVector& parameters,
        const std::string& name = ""
        );

    Model(
        const ov::ResultVector& results,
        const ov::SinkVector& sinks,
        const ov::ParameterVector& parameters,
        const ov::op::util::VariableVector& variables,
        const std::string& name = ""
        );

    Model(
        const ov::OutputVector& results,
        const ov::SinkVector& sinks,
        const ov::ParameterVector& parameters,
        const ov::op::util::VariableVector& variables,
        const std::string& name = ""
        );

    Model(
        const ov::ResultVector& results,
        const ov::ParameterVector& parameters,
        const ov::op::util::VariableVector& variables,
        const std::string& name = ""
        );

    Model(
        const ov::OutputVector& results,
        const ov::ParameterVector& parameters,
        const ov::op::util::VariableVector& variables,
        const std::string& name = ""
        );

    Model(const ov::OutputVector& results, const std::string& name = "");

    Model(
        const ov::OutputVector& results,
        const ov::SinkVector& sinks,
        const std::string& name = ""
        );

    Model(const Model&);
    Model(Model&&);

    // methods

    static _OPENVINO_HIDDEN_METHODconst ::ov::DiscreteTypeInfo& get_type_info_static();
    const ::ov::DiscreteTypeInfo& get_type_info() const;
    size_t get_output_size() const;
    std::shared_ptr<ov::Node> get_output_op(size_t i) const;
    std::shared_ptr<ov::Model> clone() const;
    std::vector<ov::Output<ov::Node>> outputs();
    ov::Output<ov::Node> output();
    ov::Output<ov::Node> output(size_t i);
    ov::Output<ov::Node> output(const std::string& tensor_name);
    std::vector<ov::Output<const ov::Node>> outputs() const;
    ov::Output<const ov::Node> output() const;
    ov::Output<const ov::Node> output(size_t i) const;
    ov::Output<const ov::Node> output(const std::string& tensor_name) const;
    std::vector<ov::Output<ov::Node>> inputs();
    ov::Output<ov::Node> input();
    ov::Output<ov::Node> input(size_t i);
    ov::Output<ov::Node> input(const std::string& tensor_name);
    std::vector<ov::Output<const ov::Node>> inputs() const;
    ov::Output<const ov::Node> input() const;
    ov::Output<const ov::Node> input(size_t i) const;
    ov::Output<const ov::Node> input(const std::string& tensor_name) const;
    ov::Output<ov::Node> add_output(const std::string& tensor_name);
    ov::Output<ov::Node> add_output(const std::string& op_name, size_t output_idx);
    ov::Output<ov::Node> add_output(const ov::Output<ov::Node>& port);
    void reshape(const ov::PartialShape& partial_shape);
    void reshape(const std::map<size_t, ov::PartialShape>& partial_shapes);
    void reshape(const std::map<std::string, ov::PartialShape>& partial_shapes);
    void reshape(const std::map<ov::Output<ov::Node>, ov::PartialShape>& partial_shapes);
    const ov::element::Type& get_output_element_type(size_t i) const;
    const Shape& get_output_shape(size_t i) const;
    const PartialShape& get_output_partial_shape(size_t i) const;
    std::shared_ptr<ov::Node> get_result() const;
    const std::string& get_name() const;
    void set_friendly_name(const std::string& name);
    const std::string& get_friendly_name() const;
    std::vector<std::shared_ptr<ov::Node>> get_ops() const;
    std::vector<std::shared_ptr<ov::Node>> get_ordered_ops() const;
    void map_unordered_ops(std::function<void(ov::Node \*)> f) const;
    void replace_node(std::shared_ptr<ov::Node> old, std::shared_ptr<ov::Node> repl);
    void validate_nodes_and_infer_types() const;
    size_t get_graph_size() const;
    bool is_dynamic() const;

    void replace_parameter(
        size_t parameter_index,
        const std::shared_ptr<ov::op::v0::Parameter>& parameter
        );

    void set_topological_sort(topological_sort_t);
    virtual bool visit_attributes(ov::AttributeVisitor& visitor);
    const ov::ParameterVector& get_parameters() const;
    const ov::ResultVector& get_results() const;
    int64_t get_parameter_index(const std::shared_ptr<ov::op::v0::Parameter>& parameter) const;
    int64_t get_result_index(const ov::Output<ov::Node>& value) const;
    int64_t get_result_index(const ov::Output<const ov::Node>& value) const;

    bool evaluate(
        const ov::HostTensorVector& output_tensors,
        const ov::HostTensorVector& input_tensors,
        ov::EvaluationContext& evaluation_context
        ) const;

    bool evaluate(
        const ov::HostTensorVector& output_tensors,
        const ov::HostTensorVector& input_tensors
        ) const;

    bool evaluate(
        ov::TensorVector& output_tensors,
        const ov::TensorVector& input_tensors,
        ov::EvaluationContext& evaluation_context
        ) const;

    bool evaluate(
        ov::TensorVector& output_tensors,
        const ov::TensorVector& input_tensors
        ) const;

    const ov::SinkVector& get_sinks() const;
    void add_sinks(const ov::SinkVector& sinks);
    void remove_sink(const std::shared_ptr<ov::op::Sink>& sink);
    void add_results(const ov::ResultVector& results);
    void remove_result(const std::shared_ptr<ov::op::v0::Result>& result);
    void add_parameters(const ov::ParameterVector& params);
    void remove_parameter(const std::shared_ptr<ov::op::v0::Parameter>& param);
    void add_variables(const ov::op::util::VariableVector& variables);
    void remove_variable(const ov::op::util::Variable::Ptr& variable);
    const ov::op::util::VariableVector& get_variables() const;
    ov::op::util::Variable::Ptr get_variable_by_id(const std::string& variable_id) const;
    RTMap& get_rt_info();
    const RTMap& get_rt_info() const;

    template <
        class T,
        class... Args,
        typename std::enable_if<!std::is_same<T, ov::Any>::value, bool>::type = true
        >
    const T& get_rt_info(Args... args) const;

    template <
        class T,
        typename std::enable_if<!std::is_same<T, ov::Any>::value, bool>::type = true
        >
    const T& get_rt_info(const std::vector<std::string>& args) const;

    template <class... Args>
    bool has_rt_info(Args... args) const;

    bool has_rt_info(const std::vector<std::string>& args) const;

    template <class T, class... Args>
    void set_rt_info(
        const T& argument,
        Args... args
        );

    template <class T>
    void set_rt_info(
        const T& argument,
        const std::vector<std::string>& args
        );

    Model& operator = (const Model&);
    Model& operator = (Model&&);
};

Detailed Documentation

A user-defined model.

Construction

Model(const ov::OutputVector& results, const std::string& name = "")

Constructs a Model. Lists of parameters and variables will be generated automatically based on traversing the graph from the results.

Model(
    const ov::OutputVector& results,
    const ov::SinkVector& sinks,
    const std::string& name = ""
    )

Constructs a Model. Lists of parameters and variables will be generated automatically based on traversing the graph from the results and the sinks.

Methods

size_t get_output_size() const

Return the number of outputs for this Model.

std::shared_ptr<ov::Node> get_output_op(size_t i) const

Return the op that generates output i.

std::shared_ptr<ov::Model> clone() const

Clones the original model.

std::vector<ov::Output<ov::Node>> outputs()

Model outputs.

std::vector<ov::Output<ov::Node>> inputs()

Model inputs.

const ov::element::Type& get_output_element_type(size_t i) const

Return the element type of output i.

const Shape& get_output_shape(size_t i) const

Return the shape of element i.

const PartialShape& get_output_partial_shape(size_t i) const

Return the partial shape of element i.

std::shared_ptr<ov::Node> get_result() const

Check that there is a single result and return it.

const std::string& get_name() const

Get the unique name of the model.

Returns:

A const reference to the model’s unique name.

void set_friendly_name(const std::string& name)

Sets a friendly name for a model. This does not overwrite the unique name of the model and is retrieved via get_friendly_name(). Used mainly for debugging.

Parameters:

name

is the friendly name to set

const std::string& get_friendly_name() const

Gets the friendly name for a model. If no friendly name has been set via set_friendly_name then the model’s unique name is returned.

Returns:

A const reference to the model’s friendly name.

size_t get_graph_size() const

Returns the sum of the size of all nodes in the graph plus the size of all constant data. This has little value beyond comparing the relative size of graphs and should not be considered the actual memory consumption of a graph.

bool is_dynamic() const

Returns true if any of the op’s defined in the model contains partial shape.

void replace_parameter(
    size_t parameter_index,
    const std::shared_ptr<ov::op::v0::Parameter>& parameter
    )

Replace the parameter_index th parameter of the model with parameter.

All users of the parameter_index th parameter are redirected to parameter, and the parameter_index th entry in the model parameter list is replaced with parameter.

Parameters:

parameter_index

The index of the parameter to replace.

parameter

The parameter to substitute for the parameter_index th parameter.

const ov::ParameterVector& get_parameters() const

Return the model parameters.

const ov::ResultVector& get_results() const

Return a list of model’s outputs.

int64_t get_parameter_index(const std::shared_ptr<ov::op::v0::Parameter>& parameter) const

Index for parameter, or -1.

int64_t get_result_index(const ov::Output<ov::Node>& value) const

Return the index of this model’s Result represented by the “value” Output object. This method returns -1 if an the passed output is not related to the Results of a model.

Parameters:

value

Output containing Node

int64_t get_result_index(const ov::Output<const ov::Node>& value) const

Return the index of this model’s Result represented by the “value” Output object. This method returns -1 if an the passed output is not related to the Results of a model.

Parameters:

value

Output containing Node

bool evaluate(
    const ov::HostTensorVector& output_tensors,
    const ov::HostTensorVector& input_tensors,
    ov::EvaluationContext& evaluation_context
    ) const

Evaluate the model on inputs, putting results in outputs.

Deprecated Use evaluate with ov::Tensor instead

Parameters:

output_tensors

Tensors for the outputs to compute. One for each result

input_tensors

Tensors for the inputs. One for each inputs.

evaluation_context

Storage of additional settings and attributes that can be used when evaluating the model. This additional information can be shared across nodes.

bool evaluate(
    const ov::HostTensorVector& output_tensors,
    const ov::HostTensorVector& input_tensors
    ) const

Evaluate the model on inputs, putting results in outputs.

Deprecated Use evaluate with ov::Tensor instead

Parameters:

output_tensors

Tensors for the outputs to compute. One for each result

input_tensors

Tensors for the inputs. One for each inputs.

bool evaluate(
    ov::TensorVector& output_tensors,
    const ov::TensorVector& input_tensors,
    ov::EvaluationContext& evaluation_context
    ) const

Evaluate the model on inputs, putting results in outputs.

Parameters:

output_tensors

Tensors for the outputs to compute. One for each result

input_tensors

Tensors for the inputs. One for each inputs.

evaluation_context

Storage of additional settings and attributes that can be used when evaluating the model. This additional information can be shared across nodes.

bool evaluate(
    ov::TensorVector& output_tensors,
    const ov::TensorVector& input_tensors
    ) const

Evaluate the model on inputs, putting results in outputs.

Parameters:

output_tensors

Tensors for the outputs to compute. One for each result

input_tensors

Tensors for the inputs. One for each inputs.

const ov::SinkVector& get_sinks() const

Return a list of model’s sinks.

void add_sinks(const ov::SinkVector& sinks)

Add new sink nodes to the list. Method doesn’t validate graph, it should be done manually after all changes.

Parameters:

sinks

new sink nodes

void remove_sink(const std::shared_ptr<ov::op::Sink>& sink)

Delete sink node from the list of sinks. Method doesn’t delete node from graph.

Parameters:

sink

Sink to delete

void add_results(const ov::ResultVector& results)

Add new Result nodes to the list. Method doesn’t validate graph, it should be done manually after all changes.

Parameters:

results

new Result nodes

void remove_result(const std::shared_ptr<ov::op::v0::Result>& result)

Delete Result node from the list of results. Method will not delete node from graph.

Parameters:

result

Result node to delete

void add_parameters(const ov::ParameterVector& params)

Add new Parameter nodes to the list.

Method doesn’t change or validate graph, it should be done manually. For example, if you want to replace ReadValue node by Parameter, you should do the following steps:

  • replace node ReadValue by Parameter in graph

  • call add_parameter() to add new input to the list

  • call graph validation to check correctness of changes

Parameters:

params

new Parameter nodes

void remove_parameter(const std::shared_ptr<ov::op::v0::Parameter>& param)

Delete Parameter node from the list of parameters. Method will not delete node from graph. You need to replace Parameter with other operation manually. Attention: Indexing of parameters can be changed.

Possible use of method is to replace input by variable. For it the following steps should be done:

  • Parameter node should be replaced by ReadValue

  • call remove_parameter(param) to remove input from the list

  • check if any parameter indexes are saved/used somewhere, update it for all inputs because indexes can be changed

  • call graph validation to check all changes

Parameters:

param

Parameter node to delete

void add_variables(const ov::op::util::VariableVector& variables)

Add new variables to the list. Method doesn’t validate graph, it should be done manually after all changes.

Parameters:

variables

new variables to add

void remove_variable(const ov::op::util::Variable::Ptr& variable)

Delete variable from the list of variables. Method doesn’t delete nodes that used this variable from the graph.

Parameters:

variable

Variable to delete

const ov::op::util::VariableVector& get_variables() const

Return a list of model’s variables.

ov::op::util::Variable::Ptr get_variable_by_id(const std::string& variable_id) const

Return a variable by specified variable_id.

RTMap& get_rt_info()

Returns a runtime info.

Returns:

reference to ov::AnyMap with runtime info

const RTMap& get_rt_info() const

Returns a constant runtime info.

Returns:

reference to const ov::AnyMap with runtime info

template <
    class T,
    class... Args,
    typename std::enable_if<!std::is_same<T, ov::Any>::value, bool>::type = true
    >
const T& get_rt_info(Args... args) const

Returns a runtime attribute for the path, throws an ov::Exception if path doesn’t exist.

Parameters:

T

the type of returned value

Args

types of variadic arguments

args

path to the runtime attribute

Returns:

constant reference to value from runtime info

template <
    class T,
    typename std::enable_if<!std::is_same<T, ov::Any>::value, bool>::type = true
    >
const T& get_rt_info(const std::vector<std::string>& args) const

Returns a runtime attribute for the path, throws an ov::Exception if path doesn’t exist.

Parameters:

T

the type of returned value

args

vector with path to the runtime attribute

Returns:

constant reference to value from runtime info

template <class... Args>
bool has_rt_info(Args... args) const

Checks if given path exists in runtime info.

Parameters:

Args

types of variadic arguments

args

path to the runtime attribute

Returns:

true if path exists, otherwise false

bool has_rt_info(const std::vector<std::string>& args) const

Checks if given path exists in runtime info.

Parameters:

args

vector with path to the runtime attribute

Returns:

true if path exists, otherwise false

template <class T, class... Args>
void set_rt_info(
    const T& argument,
    Args... args
    )

Add value inside the runtime info.

Parameters:

T

type of new value

Args

types of variadic arguments

argument

value for the runtime info

args

path to the runtime attribute

template <class T>
void set_rt_info(
    const T& argument,
    const std::vector<std::string>& args
    )

Add value inside the runtime info.

Parameters:

T

type of new value

argument

value for the runtime info

args

vector with path to the runtime attribute