• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 Huawei Technologies Co., Ltd
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 #ifndef MINDSPORE_PI_JIT_VALUE_H_
17 #define MINDSPORE_PI_JIT_VALUE_H_
18 
19 #include <memory>
20 #include <string>
21 #include "pipeline/jit/pi/graph_compiler/pi_ir/node.h"
22 #include "pipeline/jit/pi/graph_compiler/pi_ir/scope.h"
23 #include "pybind11/stl.h"
24 
25 namespace mindspore {
26 namespace pijit {
27 namespace ir {
28 namespace py = pybind11;
29 
30 /// \brief Value is is the class which represent a python object
31 class Value : public Node {
32  public:
33   /**
34    * \brief The constructor of value node.
35    *
36    * \param[in] value the python object.
37    *
38    * \return The instance of value node.
39    */
Value(const py::object & value)40   explicit Value(const py::object &value) : Value(value, "", kScopeLocal) {}
41 
42   /**
43    * \brief The constructor of value node.
44    *
45    * \param[in] value the python object.
46    * \param[in] scope the located of python object.
47    *
48    * \return The instance of value node.
49    */
Value(const py::object & value,Scope scope)50   Value(const py::object &value, Scope scope) : Value(value, "", scope) {}
51 
52   /**
53    * \brief The constructor of value node.
54    *
55    * \param[in] value the python object.
56    * \param[in] name the name of python object.
57    *
58    * \return The instance of value node.
59    */
Value(const py::object & value,const std::string & name)60   Value(const py::object &value, const std::string &name) : Value(value, name, kScopeLocal) {}
61 
62   /**
63    * \brief The constructor of value node.
64    *
65    * \param[in] value the python object.
66    * \param[in] name the name of python object.
67    * \param[in] scope the located of python object.
68    *
69    * \return The instance of value node.
70    */
Value(const py::object & value,const std::string & name,Scope scope)71   Value(const py::object &value, const std::string &name, Scope scope) : value_(value), name_(name), scope_(scope) {}
72 
73   // \brief Destructor.
74   ~Value() override = default;
75   JIT_DECLARE_PARENT(Value, Node);
76 
77   bool operator==(const Value &other) { return value_.ptr() == other.value_.ptr(); }
78 
79   /**
80    * \brief Get python object.
81    *
82    * \return the python object.
83    */
GetValue()84   const py::object &GetValue() const { return value_; }
85 
86   /**
87    * \brief Set the python object.
88    *
89    * \param[in] value the python object.
90    */
SetValue(const py::object & value)91   void SetValue(const py::object &value) { value_ = value; }
92 
93   /**
94    * \brief Get the name of python object.
95    *
96    * \return the name of python object.
97    */
GetName()98   const std::string &GetName() const { return name_; }
99 
100   /**
101    * \brief Set the name of python object.
102    *
103    * \param[in] name the name of python object.
104    */
SetName(const std::string & name)105   void SetName(const std::string &name) { name_ = name; }
106 
107   /**
108    * \brief Get the scope of the value.
109    *
110    * \return the scope of the value.
111    */
GetScope()112   Scope GetScope() const { return scope_; }
113 
114   /**
115    * \brief Get the description of this value.
116    * \return The description.
117    */
ToString()118   std::string ToString() const override {
119     return "%" + std::to_string(GetNodeId()) + " = Value[" + GetType()->GetName() + "](Name : " + name_ +
120            " Value : " + py::str(value_).cast<std::string>() + ")";
121   }
122 
123  private:
124   /// \brief The python object.
125   py::object value_;
126   /// \brief The name of python object.
127   std::string name_;
128   /// Where the value is located
129   Scope scope_;
130 };
131 
132 using ValuePtr = std::shared_ptr<Value>;
133 }  // namespace ir
134 }  // namespace pijit
135 }  // namespace mindspore
136 
137 #endif  // MINDSPORE_PI_JIT_VALUE_H_
138