1 /** 2 * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). 3 * 4 * Copyright 2019-2020 Huawei Technologies Co., Ltd 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 #ifndef MINDSPORE_CORE_ABSTRACT_PRIMITIVE_INFER_MAP_H_ 19 #define MINDSPORE_CORE_ABSTRACT_PRIMITIVE_INFER_MAP_H_ 20 #include <unordered_map> 21 #include <vector> 22 #include <memory> 23 #include "ir/primitive.h" 24 #include "ops/primitive_c.h" 25 #include "base/core_ops.h" 26 #include "abstract/abstract_value.h" 27 #include "ir/anf.h" 28 29 namespace mindspore { 30 namespace abstract { 31 using InferShapeImpl = AbstractBasePtr (*)(const abstract::AnalysisEnginePtr &, const PrimitivePtr &, 32 const AbstractBasePtrList &); 33 using InferValueImpl = ValuePtr (*)(const PrimitivePtr &, const AbstractBasePtrList &); 34 35 struct StandardPrimitiveImplReg { 36 InferShapeImpl infer_shape_impl_; // infer shape and type for ops 37 InferValueImpl infer_value_impl_; // infer value for ops 38 // in_white_list_ is true means this primitive can be executed by vm backend 39 // else will be optimized by frontend 40 bool in_white_list_; 41 }; 42 43 using PrimitiveEvalImplMap = 44 std::unordered_map<PrimitivePtr, StandardPrimitiveImplReg, PrimitiveHasher, PrimitiveEqual>; 45 46 PrimitiveEvalImplMap &GetPrimitiveToEvalImplMap(); 47 48 PrimitiveEvalImplMap &GetPrimitiveToBackendEvalImplMap(); 49 50 StandardPrimitiveImplReg GetPrimitiveInferImpl(const PrimitivePtr &primitive); 51 52 std::vector<int64_t> GetDependsFormMap(const CNodePtr &cnode); 53 54 void RegisterStandardPrimitiveImpl(const PrimitivePtr &primitive, const StandardPrimitiveImplReg &impl_reg); 55 56 class RegisterStandardPrimitiveEvalHelper { 57 public: 58 RegisterStandardPrimitiveEvalHelper(const PrimitivePtr &primitive, const InferShapeImpl &infer_impl, 59 const InferValueImpl &infer_value_impl, const bool is_wight_list = true) { 60 const StandardPrimitiveImplReg impl_reg{infer_impl, infer_value_impl, is_wight_list}; 61 RegisterStandardPrimitiveImpl(primitive, impl_reg); 62 } 63 ~RegisterStandardPrimitiveEvalHelper() = default; 64 }; 65 66 #define REGISTER_PRIMITIVE_EVAL_IMPL(name, primitive, infer_impl, infer_value_impl, is_wight_list) \ 67 static auto helper_##name = \ 68 abstract::RegisterStandardPrimitiveEvalHelper(primitive, infer_impl, infer_value_impl, is_wight_list); \ 69 std::shared_ptr<ops::PrimitiveC> GetDefaultPrimC##name() { \ 70 auto out = std::make_shared<name>(); \ 71 return out; \ 72 } \ 73 ops::OpPrimCRegisterHelper primc_gen_##name(#name, GetDefaultPrimC##name); 74 } // namespace abstract 75 } // namespace mindspore 76 #endif // MINDSPORE_CORE_ABSTRACT_PRIMITIVE_INFER_MAP_H_ 77