• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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_CCSRC_IR_CELL_H_
18 #define MINDSPORE_CCSRC_IR_CELL_H_
19 
20 #include <memory>
21 #include <string>
22 #include <tuple>
23 
24 #include "utils/hash_map.h"
25 #include "abstract/abstract_value.h"
26 #include "utils/misc.h"
27 
28 namespace mindspore {
29 using abstract::AbstractBasePtr;
30 using abstract::AbstractBasePtrList;
31 enum MixedPrecisionType { kNotSet = 0, kFP16 = 1, kFP32 = 2, kBF16 = 3 };
32 
33 /// \brief The Cell class of MindSpore is the base class for building all networks and the basic unit of a network.
34 class MS_CORE_API Cell final : public Named {
35  public:
36   /// \brief Constructor.
37   ///
38   /// \param[in] name The name of Cell.
39   explicit Cell(const std::string &name);
40   MS_DECLARE_PARENT(Cell, Named);
41 
42   abstract::AbstractBasePtr ToAbstract() override;
43 
44   std::string ToString() const override;
45 
46   /// \brief Get the id of this Cell.
47   ///
48   /// \return The id of this Cell.
id()49   string id() const { return id_; }
50 
51   /// \brief Get information about all attributes.
52   ///
53   /// \return Details of all attributes.
54   std::string GetAttrString() const;
55 
56   /// \brief Obtain all attributes of Cell.
57   ///
58   /// \return All attributes of Cell.
attrs()59   const mindspore::HashMap<std::string, ValuePtr> &attrs() const { return attrs_; }
60 
61   /// \brief Set the attributes of Cell.
62   ///
63   /// \param[in] attributes Attributes.
set_attrs(const mindspore::HashMap<std::string,ValuePtr> & attrs_input)64   void set_attrs(const mindspore::HashMap<std::string, ValuePtr> &attrs_input) { attrs_ = attrs_input; }
65 
66   /// \brief Add a new attribute.
67   ///
68   /// \param[in] name The name of new attribute.
69   /// \param[in] attr The value of new attribute.
AddAttr(const std::string & name,const ValuePtr & attr)70   void AddAttr(const std::string &name, const ValuePtr &attr) { attrs_[name] = attr; }
71 
72   /// \brief Delete an attribute.
73   ///
74   /// \param[in] name The name of the attribute to be deleted.
75   void DelAttr(const std::string &name);
76 
77   /// \brief Obtain the attribute corresponding to the name.
78   ///
79   /// \param[in] attr_name The name of the attribute.
80   /// \return The value of the attribute corresponding to the name.
GetAttr(const std::string & attr_name)81   ValuePtr GetAttr(const std::string &attr_name) const {
82     auto iter = attrs_.find(attr_name);
83     return iter == attrs_.cend() ? nullptr : iter->second;
84   }
85 
86   /// \brief Determine whether the Cell has the attribute corresponding to the name.
87   ///
88   /// \param[in] attr_name The name of the attribute.
89   /// \return True if the Cell has this attribute, otherwise False.
HasAttr(const std::string & attr_name)90   bool HasAttr(const std::string &attr_name) const {
91     auto iter = attrs_.find(attr_name);
92     return !(iter == attrs_.cend());
93   }
94 
95   /// \brief Get mixed precision type.
96   ///
97   /// \return The mixed precision type.
GetMixedPrecisionType()98   MixedPrecisionType GetMixedPrecisionType() const { return mixed_type_; }
99 
100   /// \brief Set mixed precision type.
101   ///
102   /// \param[in] mixed_type The type of mixed precision, float16 or float32.
SetMixedPrecisionType(enum MixedPrecisionType mixed_type)103   void SetMixedPrecisionType(enum MixedPrecisionType mixed_type) { mixed_type_ = mixed_type; }
104 
105   bool operator==(const Value &other) const override;
106 
107   /// \brief Determine whether Cell is the same as other.
108   ///
109   /// \param[in] other Another Cell.
110   /// \return True if the same, otherwise False.
111   bool operator==(const Cell &other) const;
112 
113   /// \brief Destructor.
114   ~Cell() override = default;
115 
116  private:
117   string id_;
118   mindspore::HashMap<std::string, ValuePtr> attrs_;
119   enum MixedPrecisionType mixed_type_ { kNotSet };
120 };
121 
122 using CellPtr = std::shared_ptr<Cell>;
123 }  // namespace mindspore
124 #endif  // MINDSPORE_CCSRC_IR_CELL_H_
125