1 /** 2 * Copyright 2020-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 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_H_ 17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_H_ 18 #include <string> 19 20 #include "ir/anf.h" 21 #include "utils/hash_map.h" 22 #include "utils/hash_set.h" 23 #include "utils/ms_utils.h" 24 #include "include/backend/visible.h" 25 26 namespace mindspore { 27 namespace opt { 28 class ConstInputToAttrInfoRegister { 29 public: op_name_(op_name)30 explicit ConstInputToAttrInfoRegister(const std::string &op_name = "") : op_name_(op_name) {} 31 virtual ~ConstInputToAttrInfoRegister() = default; 32 SetConstInputToAttr(size_t input_index)33 ConstInputToAttrInfoRegister &SetConstInputToAttr(size_t input_index) { 34 (void)input_attr_set_.insert(input_index); 35 return *this; 36 } 37 SetConstInputToAttr(const mindspore::HashSet<size_t> & input_attr_set)38 ConstInputToAttrInfoRegister &SetConstInputToAttr(const mindspore::HashSet<size_t> &input_attr_set) { 39 input_attr_set_.insert(input_attr_set.cbegin(), input_attr_set.cend()); 40 return *this; 41 } 42 GetConstInputAttrInfo()43 const mindspore::HashSet<size_t> &GetConstInputAttrInfo() const { return input_attr_set_; } GetOpName()44 const std::string &GetOpName() const { return op_name_; } 45 46 private: 47 std::string op_name_; 48 mindspore::HashSet<size_t> input_attr_set_; 49 }; 50 51 class ConstInputToAttrInfoRegistry { 52 public: 53 static ConstInputToAttrInfoRegistry &Instance(); 54 void Register(const ConstInputToAttrInfoRegister ®); 55 void Register(const std::string &op_name, const mindspore::HashSet<size_t> &input_attr_set); 56 bool GetRegisterByOpName(const std::string &op_name, ConstInputToAttrInfoRegister *reg) const; 57 58 private: 59 ConstInputToAttrInfoRegistry(); 60 ~ConstInputToAttrInfoRegistry() = default; 61 DISABLE_COPY_AND_ASSIGN(ConstInputToAttrInfoRegistry) 62 mindspore::HashMap<std::string, ConstInputToAttrInfoRegister> op_input_to_attr_map_; 63 }; 64 65 BACKEND_EXPORT CNodePtr ConstInputToAttr(const CNodePtr &cnode, const mindspore::HashSet<size_t> &input_attrs); 66 } // namespace opt 67 } // namespace mindspore 68 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_H_ 69