assign.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include "ngraph/op/sink.hpp"
20 #include "ngraph/op/util/variable.hpp"
21 
22 namespace ngraph
23 {
24  namespace op
25  {
26  class NGRAPH_API AssignBase : public Sink
27  {
28  public:
29  NGRAPH_RTTI_DECLARATION;
30  AssignBase() = default;
31  /// \brief Constructs an AssignBase operation.
32  explicit AssignBase(const OutputVector& arguments)
33  : Sink(arguments)
34  {
35  }
36 
37  /// \brief Returns variable connected to this node.
38  virtual std::shared_ptr<ngraph::Variable> get_variable() const { return m_variable; }
39  /// \brief Sets a new variable to be connected to this node.
40  ///
41  /// \param variable New variable to be connected to this node.
42  virtual void set_variable(const std::shared_ptr<ngraph::Variable>& variable)
43  {
44  m_variable = variable;
45  }
46 
47  /// \brief Sets the identifier of corresponding variable
48  ///
49  /// \param variable_id New identifier of the variable.
50  virtual void set_variable_id(const std::string& variable_id){};
51 
52  /// \brief Returns the identifier of corresponding variable.
53  virtual std::string get_variable_id() const = 0;
54 
55  protected:
56  std::shared_ptr<ngraph::Variable> m_variable;
57  };
58 
59  namespace v3
60  {
61  /// \brief Assign operation sets an input value to the variable with `variable_id`
62  class NGRAPH_API Assign : public AssignBase
63  {
64  public:
65  NGRAPH_RTTI_DECLARATION;
66  Assign() = default;
67 
68  /// \brief Constructs an Assign operation.
69  ///
70  /// \param new_value Node that produces the input tensor.
71  /// \param variable_id identifier of the variable to be updated.
72  Assign(const Output<Node>& new_value, const std::string& variable_id);
73 
74  void validate_and_infer_types() override;
75  std::string get_variable_id() const override { return m_variable_id; }
76  void set_variable_id(const std::string& variable_id) override
77  {
78  m_variable_id = variable_id;
79  }
80 
81  std::shared_ptr<Node>
82  clone_with_new_inputs(const OutputVector& new_args) const override;
83 
84  bool visit_attributes(AttributeVisitor& visitor) override;
85 
86  private:
87  std::string m_variable_id;
88  };
89  }
90  namespace v6
91  {
92  /// \brief Assign operation sets an input value to the variable with `variable_id`
93  class NGRAPH_API Assign : public AssignBase
94  {
95  public:
96  NGRAPH_RTTI_DECLARATION;
97  Assign() = default;
98 
99  /// \brief Constructs an Assign operation.
100  ///
101  /// \param new_value Node that produces the input tensor.
102  /// \param variable Class for storing and synchronizing element types, shapes and
103  /// identifiers
104  /// between pairs of Assign/ReadValue nodes.
105  Assign(const Output<Node>& new_value, const std::shared_ptr<Variable>& variable);
106 
107  void validate_and_infer_types() override;
108 
109  std::shared_ptr<Node>
110  clone_with_new_inputs(const OutputVector& new_args) const override;
111 
112  bool visit_attributes(AttributeVisitor& visitor) override;
113 
114  std::string get_variable_id() const override
115  {
116  NGRAPH_CHECK(m_variable,
117  "Variable is not initialized. Variable_id is unavailable");
118  return m_variable->get_info().variable_id;
119  }
120  };
121  }
122  }
123 }
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Definition: assign.hpp:27
AssignBase(const OutputVector &arguments)
Constructs an AssignBase operation.
Definition: assign.hpp:32
virtual std::shared_ptr< ngraph::Variable > get_variable() const
Returns variable connected to this node.
Definition: assign.hpp:38
virtual std::string get_variable_id() const =0
Returns the identifier of corresponding variable.
virtual void set_variable_id(const std::string &variable_id)
Sets the identifier of corresponding variable.
Definition: assign.hpp:50
virtual void set_variable(const std::shared_ptr< ngraph::Variable > &variable)
Sets a new variable to be connected to this node.
Definition: assign.hpp:42
Root of nodes that can be sink nodes.
Definition: sink.hpp:29
Assign operation sets an input value to the variable with variable_id
Definition: assign.hpp:63
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
void set_variable_id(const std::string &variable_id) override
Sets the identifier of corresponding variable.
Definition: assign.hpp:76
std::string get_variable_id() const override
Returns the identifier of corresponding variable.
Definition: assign.hpp:75
Assign(const Output< Node > &new_value, const std::string &variable_id)
Constructs an Assign operation.
Assign operation sets an input value to the variable with variable_id
Definition: assign.hpp:94
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
Assign(const Output< Node > &new_value, const std::shared_ptr< Variable > &variable)
Constructs an Assign operation.
std::string get_variable_id() const override
Returns the identifier of corresponding variable.
Definition: assign.hpp:114
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28