1 /** 2 * Copyright 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_QUANTIZATION_PARAM_H_ 18 #define MINDSPORE_CORE_IR_QUANTIZATION_PARAM_H_ 19 20 #include <string> 21 #include <memory> 22 #include "ir/named.h" 23 #include "ir/primal_attr.h" 24 25 namespace mindspore { 26 /// \brief QuantizationParam defines tensor quantization param of MindSpore. 27 class MS_CORE_API QuantizationParam : public Value { 28 public: QuantizationParam(const std::string & quant_algo_name)29 explicit QuantizationParam(const std::string &quant_algo_name) : quant_algo_name_(quant_algo_name) {} 30 ~QuantizationParam() = default; 31 32 /// \brief Add attribute to QuantizationParam attribute map. 33 /// 34 /// \param[in] name The name of attribute. 35 /// \param[in] attr The value of attribute. 36 /// \return The QuantizationParam to which attribute has been added. AddAttr(const std::string & name,const ValuePtr & attr)37 QuantizationParam &AddAttr(const std::string &name, const ValuePtr &attr) { 38 attrs_[name] = attr; 39 return *this; 40 } 41 42 /// \brief Delete the attribute. 43 /// 44 /// \param[in] name The name of attribute to be delete. 45 /// \return The QuantizationParam to which attribute has been added. DelAttr(const std::string & name)46 QuantizationParam &DelAttr(const std::string &name) { 47 (void)attrs_.erase(name); 48 return *this; 49 } 50 51 /// \brief Set attribute to the quant param attribute map. SetAttr(const std::string & attrName,const ValuePtr & attr)52 void SetAttr(const std::string &attrName, const ValuePtr &attr) { attrs_[attrName] = attr; } 53 /// \brief Get QuantizationParam's attribute. 54 /// 55 /// \param[in] attrName QuantizationParam attribute name. 56 /// \return The value of attribute in QuantizationParam attribute map, if the map is not GetAttr(const std::string & attrName)57 ValuePtr GetAttr(const std::string &attrName) const { 58 auto iter = attrs_.find(attrName); 59 return iter == attrs_.cend() ? nullptr : iter->second; 60 } 61 62 /// \brief Use add attribute by using a map,all elements of the map will be added in the QuantizationParam's attribute 63 /// map. 64 /// 65 /// \param[in] attrs The attribute map needs to be added in the QuantizationParam attribute. 66 /// \return The QuantizationParam to which attribute has been added. set_attrs(const mindspore::HashMap<std::string,ValuePtr> & attrs)67 QuantizationParam &set_attrs(const mindspore::HashMap<std::string, ValuePtr> &attrs) { 68 for (auto &attr : attrs) { 69 attrs_[attr.first] = attr.second; 70 } 71 return *this; 72 } 73 74 /// \brief Get QuantizationParam's all attributes. 75 /// 76 /// \return The QuantizationParam's all attribute. attrs()77 const mindspore::HashMap<std::string, ValuePtr> &attrs() const { return attrs_; } 78 79 /// \brief Get QuantizationParam's algorithm name. 80 /// 81 /// \return The QuantizationParam's algorithm name. quant_algo_name()82 std::string quant_algo_name() const { return quant_algo_name_; } 83 84 /// \brief Set quantization algorithm name. 85 /// 86 /// \param[in] quant_algo_name The QuantizationParam's algorithm name. 87 /// \return The QuantizationParam to which algorithm name has been added. set_quant_algo_name(const std::string & quant_algo_name)88 QuantizationParam &set_quant_algo_name(const std::string &quant_algo_name) { 89 this->quant_algo_name_ = quant_algo_name; 90 return *this; 91 } 92 MS_DECLARE_PARENT(QuantizationParam, Value); 93 94 bool operator==(const Value &other) const override; 95 /// \brief To compare whether two Primitive objects are equal. 96 /// 97 /// \param[in] other The other QuantizationParam be compared with. 98 /// \return return true if the name and attributes of primitives are the same,otherwise return false. 99 bool operator==(const QuantizationParam &other) const; 100 101 private: 102 std::string quant_algo_name_; 103 mindspore::HashMap<std::string, ValuePtr> attrs_; 104 }; 105 using QuantizationParamPtr = std::shared_ptr<QuantizationParam>; 106 } // namespace mindspore 107 #endif // MINDSPORE_CORE_IR_QUANT_PARAM_H 108