1 /** 2 * Copyright 2019 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_TRANSFORM_GRAPH_IR_OP_DECLARE_MACRO_H_ 18 #define MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_DECLARE_MACRO_H_ 19 20 #include <string> 21 #include <unordered_map> 22 #include <memory> 23 #include "transform/graph_ir/op_adapter.h" 24 #include "transform/graph_ir/op_adapter_map.h" 25 #include "mindspore/core/base/core_ops.h" 26 27 namespace mindspore::transform { 28 #define DECLARE_OP_ADAPTER(T) \ 29 using T = ge::op::T; \ 30 template <> \ 31 const std::unordered_map<int, InputDesc> OpAdapter<T>::input_map_; \ 32 template <> \ 33 const std::unordered_map<std::string, AttrDesc> OpAdapter<T>::attr_map_; 34 35 #define DECLARE_OP_USE_OUTPUT(T) \ 36 template <> \ 37 const std::unordered_map<int, OutputDesc> OpAdapter<T>::output_map_; 38 39 #define DECLARE_OP_USE_ENUM(T) \ 40 template <> \ 41 const std::unordered_map<std::string, int> OpAdapter<T>::enum_map_{}; 42 43 #define DECLARE_OP_USE_INPUT_ATTR(T) \ 44 template <> \ 45 const std::unordered_map<unsigned int, AttrDesc> OpAdapter<T>::input_attr_map_; 46 47 #define DECLARE_OP_USE_DYN_INPUT(T) \ 48 template <> \ 49 const std::unordered_map<int, DynInputDesc> OpAdapter<T>::dyn_input_map_; 50 51 #define DECLARE_OP_USE_DYN_SUBGRAPH(T) \ 52 template <> \ 53 const std::unordered_map<int, DynSubGraphDesc> OpAdapter<T>::dyn_subgraph_map_; 54 55 #define DECLARE_OP_USE_DYN_OUTPUT(T) \ 56 template <> \ 57 const std::unordered_map<int, DynOutputDesc> OpAdapter<T>::dyn_output_map_; 58 59 #define INPUT_MAP(T) \ 60 template <> \ 61 const std::unordered_map<int, InputDesc> OpAdapter<T>::input_map_ 62 #define EMPTY_INPUT_MAP std::unordered_map<int, InputDesc>() 63 #define INPUT_DESC(name) \ 64 { \ 65 #name, \ 66 [](const OperatorPtr op, const OperatorPtr input) { \ 67 auto p = std::static_pointer_cast<OpType>(op); \ 68 (void)p->set_input_##name(*input); \ 69 }, \ 70 [](const OperatorPtr op, const OutHandler& handle) { \ 71 auto p = std::static_pointer_cast<OpType>(op); \ 72 (void)p->set_input_##name(*(handle.op), handle.out); \ 73 }, \ 74 [](const OperatorPtr op, const GeTensorDesc desc) { \ 75 auto p = std::static_pointer_cast<OpType>(op); \ 76 (void)p->update_input_desc_##name(desc); \ 77 } \ 78 } 79 80 #define DYN_INPUT_MAP(T) \ 81 template <> \ 82 const std::unordered_map<int, DynInputDesc> OpAdapter<T>::dyn_input_map_ 83 #define DYN_INPUT_DESC(name) \ 84 { \ 85 #name, \ 86 [](const OperatorPtr op, unsigned int num) { \ 87 auto p = std::static_pointer_cast<OpType>(op); \ 88 (void)p->create_dynamic_input_##name(num); \ 89 }, \ 90 [](const OperatorPtr op, unsigned int index, const OperatorPtr input) { \ 91 auto p = std::static_pointer_cast<OpType>(op); \ 92 (void)p->set_dynamic_input_##name(index, *input); \ 93 }, \ 94 [](const OperatorPtr op, unsigned int index, const OutHandler& handle) { \ 95 auto p = std::static_pointer_cast<OpType>(op); \ 96 (void)p->set_dynamic_input_##name(index, *(handle.op), handle.out); \ 97 } \ 98 } 99 100 #define DYN_SUBGRAPH_MAP(T) \ 101 template <> \ 102 const std::unordered_map<int, DynSubGraphDesc> OpAdapter<T>::dyn_subgraph_map_ 103 #define DYN_SUBGRAPH_DESC(name) \ 104 { \ 105 #name, \ 106 [](const OperatorPtr op, unsigned int num) { \ 107 auto p = std::static_pointer_cast<OpType>(op); \ 108 (void)p->create_dynamic_subgraph_##name(num); \ 109 }, \ 110 [](const OperatorPtr op, unsigned int index, const DfGraphPtr graph) { \ 111 auto p = std::static_pointer_cast<OpType>(op); \ 112 (void)p->set_dynamic_subgraph_builder_##name(index, [graph](){return *graph;}); \ 113 } \ 114 } 115 116 #define ATTR_MAP(T) \ 117 template <> \ 118 const std::unordered_map<std::string, AttrDesc> OpAdapter<T>::attr_map_ 119 #define EMPTY_ATTR_MAP std::unordered_map<std::string, AttrDesc>() 120 #define ATTR_DESC(name, ...) \ 121 { \ 122 #name, \ 123 [](const OperatorPtr op, const ValuePtr& value) { \ 124 auto p = std::static_pointer_cast<OpType>(op); \ 125 (void)p->set_attr_##name(ConvertAny(value, __VA_ARGS__)); \ 126 } \ 127 } 128 129 #define INPUT_ATTR_MAP(T) \ 130 template <> \ 131 const std::unordered_map<unsigned int, AttrDesc> OpAdapter<T>::input_attr_map_ 132 133 #define OUTPUT_MAP(T) \ 134 template <> \ 135 const std::unordered_map<int, OutputDesc> OpAdapter<T>::output_map_ 136 #define OUTPUT_DESC(name) \ 137 { \ 138 #name, \ 139 [](const OperatorPtr op, const GeTensorDesc desc) { \ 140 auto p = std::static_pointer_cast<OpType>(op); \ 141 (void)p->update_output_desc_##name(desc); \ 142 } \ 143 } 144 145 #define DYN_OUTPUT_MAP(T) \ 146 template <> \ 147 const std::unordered_map<int, DynOutputDesc> OpAdapter<T>::dyn_output_map_ 148 149 #define DYN_OUTPUT_DESC(name) \ 150 { \ 151 #name, \ 152 [](const OperatorPtr op, unsigned int num) { \ 153 auto p = std::static_pointer_cast<OpType>(op); \ 154 (void)p->create_dynamic_output_##name(num); \ 155 } \ 156 } 157 158 #define ADPT_DESC_ONE(T) std::make_shared<OpAdapterDesc>(std::make_shared<OpAdapter<T>>()) 159 #define ADPT_DESC_TWO(T, I) \ 160 std::make_shared<OpAdapterDesc>(std::make_shared<OpAdapter<T>>(), std::make_shared<OpAdapter<I>>()) 161 #define GET_MACRO(_1, _2, DESC, ...) DESC 162 #define ADPT_DESC(...) GET_MACRO(__VA_ARGS__, ADPT_DESC_TWO, ADPT_DESC_ONE, ...)(__VA_ARGS__) 163 #define REG_ADPT_DESC(name, name_str, adpt_desc) \ 164 static struct RegAdptDesc##name { \ 165 public: \ 166 RegAdptDesc##name() { OpAdapterMap::get()[name_str] = adpt_desc; } \ 167 \ 168 private: \ 169 int ph_{0}; \ 170 } g_reg_adpt_desc_##name; 171 } // namespace mindspore::transform 172 #endif // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_DECLARE_MACRO_H_ 173