• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019-2022 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 
17 #ifndef MINDSPORE_CORE_IR_NAMED_H_
18 #define MINDSPORE_CORE_IR_NAMED_H_
19 
20 #include <string>
21 #include <memory>
22 #include <functional>
23 #include <utility>
24 #include <vector>
25 
26 #include "ir/anf.h"
27 
28 namespace mindspore {
29 /// \brief Named defines an abstract class that records the name and hash_id.
30 class MS_CORE_API Named : public Value {
31  public:
32   /// \brief The constructor for Named.
33   ///
34   /// \param[in] name The name of object.
Named(const std::string & name)35   explicit Named(const std::string &name) : name_(name), hash_id_(std::hash<std::string>{}(name)) {}
36   /// \brief The constructor for Named, create a Named for another Named.
37   ///
38   /// \param[in]  other The input tensor.
Named(const Named & other)39   Named(const Named &other) : Value(other) {
40     this->name_ = other.name_;
41     hash_id_ = std::hash<std::string>{}(other.name_);
42   }
43   /// \brief The destructor of None.
44   ~Named() override = default;
45   MS_DECLARE_PARENT(Named, Value);
46   /// \brief Getting name of object.
47   ///
48   /// \return The name of object.
name()49   const std::string &name() const { return name_; }
50   /// \brief Setting name of object.
51   ///
52   /// \param[in] name The name set for the object.
53   /// \no return.
set_name(const std::string & name)54   void set_name(const std::string &name) {
55     name_ = name;
56     hash_id_ = std::hash<std::string>{}(name_);
57   }
58   /// \brief Check whether two Named objects are the same.
59   ///
60   /// \param[in] other The other Named to be compared with.
61   /// \return Return true if the same,otherwise return false.
62   virtual bool operator==(const Named &other) const { return name_ == other.name(); }
63   bool operator==(const Value &other) const override;
64   /// \brief Overloads operator '=' for Named.
65   ///
66   /// \param[in] other An existing Named object.
67   /// \return A Named object set with the same type, name and hash_id as other.
68   Named &operator=(const Named &other) {
69     if (&other != this) {
70       this->type_ = other.type_;
71       this->name_ = other.name_;
72       hash_id_ = std::hash<std::string>{}(name_);
73     }
74     return *this;
75   }
76   /// \brief Get hash id for named.
77   ///
78   /// \return The restored hash id of Named.
Hash()79   std::size_t Hash() const { return hash_id_; }
hash()80   std::size_t hash() const override { return hash_id_; }
81   /// \brief Overloads operator << for Named.
82   ///
83   /// \param os The output stream.
84   /// \param nmd Named to be displayed.
85   /// \return Output stream that contains the name of Named object.
86   friend std::ostream &operator<<(std::ostream &os, const Named &nmd) {
87     os << nmd.name();
88     return os;
89   }
90   /// \brief Get name for Named.
91   ///
92   /// \return The restored name of Named.
ToString()93   std::string ToString() const override { return name(); }
94 
95  private:
96   std::string name_;
97   std::size_t hash_id_;
98 };
99 using NamedPtr = std::shared_ptr<Named>;
100 /// \brief Implementation of hash operation.
101 struct MS_CORE_API NamedHasher {
102   /// \brief Implementation of hash operation.
103   ///
104   /// \param name The Name object need to be hashed.
105   /// \return The hash result.
operatorNamedHasher106   std::size_t operator()(NamedPtr const &name) const {
107     std::size_t hash = name->Hash();
108     return hash;
109   }
110 };
111 /// \brief Equal operator for Name.
112 struct MS_CORE_API NamedEqual {
113   /// \brief Implementation of Equal operation.
114   ///
115   /// \param t1 The left Named to compare.
116   /// \param t2 The right Named to compare.
117   /// \return The comparison result, Return true if t1 and t2 is the same,else return false.
operatorNamedEqual118   bool operator()(NamedPtr const &t1, NamedPtr const &t2) const {
119     MS_EXCEPTION_IF_NULL(t1);
120     MS_EXCEPTION_IF_NULL(t2);
121     return *t1 == *t2;
122   }
123 };
124 /// \brief None defines interface for none data.
125 class MS_CORE_API None final : public Named {
126  public:
127   /// \brief The default constructor for None.
None()128   None() : Named("None") {}
129   /// \brief The destructor of None.
130   ~None() override = default;
131   MS_DECLARE_PARENT(None, Named);
132   abstract::AbstractBasePtr ToAbstract() override;
133 };
134 GVAR_DEF(NamedPtr, kNone, std::make_shared<None>());
135 
136 /// \brief Null defines interface for null data.
137 class MS_CORE_API Null final : public Named {
138  public:
139   /// \brief The default constructor for Null.
Null()140   Null() : Named("Null") {}
141   /// \brief The destructor of Null.
142   ~Null() override = default;
143   MS_DECLARE_PARENT(Null, Named);
144   abstract::AbstractBasePtr ToAbstract() override;
145 };
146 GVAR_DEF(NamedPtr, kNull, std::make_shared<Null>());
147 
148 /// \brief Ellipsis defines interface for ... data.
149 class MS_CORE_API Ellipsis final : public Named {
150  public:
151   /// \brief The default constructor for Ellipsis.
Ellipsis()152   Ellipsis() : Named("Ellipsis") {}
153   /// \brief The destructor of Ellipsis.
154   ~Ellipsis() override = default;
155   MS_DECLARE_PARENT(Ellipsis, Named);
156   abstract::AbstractBasePtr ToAbstract() override;
157 };
158 GVAR_DEF(NamedPtr, kEllipsis, std::make_shared<Ellipsis>());
159 
160 class MS_CORE_API MindIRClassType final : public Named {
161  public:
MindIRClassType(const std::string & class_type)162   explicit MindIRClassType(const std::string &class_type) : Named(class_type) {}
163   ~MindIRClassType() override = default;
164   MS_DECLARE_PARENT(MindIRClassType, Named);
165   abstract::AbstractBasePtr ToAbstract() override;
ToString()166   std::string ToString() const override { return type_name() + ":" + name(); }
167 };
168 using MindIRClassTypePtr = std::shared_ptr<MindIRClassType>;
169 
170 class MS_CORE_API MindIRMetaFuncGraph final : public Named {
171  public:
MindIRMetaFuncGraph(const std::string & name)172   explicit MindIRMetaFuncGraph(const std::string &name) : Named(name) {}
173   ~MindIRMetaFuncGraph() override = default;
174   MS_DECLARE_PARENT(MindIRMetaFuncGraph, Named);
175   abstract::AbstractBasePtr ToAbstract() override;
ToString()176   std::string ToString() const override { return type_name() + ":" + name(); }
177 };
178 using MindIRMetaFuncGraphPtr = std::shared_ptr<MindIRMetaFuncGraph>;
179 
180 class MS_CORE_API MindIRNameSpace final : public Named {
181  public:
MindIRNameSpace(const std::string & name_space)182   explicit MindIRNameSpace(const std::string &name_space) : Named(name_space), name_space_(name_space) {}
183   ~MindIRNameSpace() override = default;
184   MS_DECLARE_PARENT(MindIRNameSpace, Named);
name_space()185   const std::string &name_space() const { return name_space_; }
186   abstract::AbstractBasePtr ToAbstract() override;
ToString()187   std::string ToString() const override { return type_name() + ":" + name(); }
188 
189  private:
190   std::string name_space_;
191 };
192 using MindIRNameSpacePtr = std::shared_ptr<MindIRNameSpace>;
193 
194 class MS_CORE_API MindIRSymbol final : public Named {
195  public:
MindIRSymbol(const std::string & symbol)196   explicit MindIRSymbol(const std::string &symbol) : Named(symbol), symbol_(symbol) {}
197   ~MindIRSymbol() override = default;
198   MS_DECLARE_PARENT(MindIRSymbol, Named);
symbol()199   const std::string &symbol() const { return symbol_; }
200   abstract::AbstractBasePtr ToAbstract() override;
ToString()201   std::string ToString() const override { return type_name() + ":" + name(); }
202 
203  private:
204   std::string symbol_;
205 };
206 using MindIRSymbolPtr = std::shared_ptr<MindIRSymbol>;
207 }  // namespace mindspore
208 #endif  // MINDSPORE_CORE_IR_NAMED_H_
209