• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2024 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_INCLUDE_COMMON_UTILS_SIMPLE_INFER_H_
18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_SIMPLE_INFER_H_
19 
20 #include <vector>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <optional>
26 #include "utils/simple_info.h"
27 #include "base/base.h"
28 #include "ir/anf.h"
29 #include "ops/op_def.h"
30 #include "ops/ops_func_impl/op_func_impl.h"
31 
32 namespace mindspore::ops {
33 class MIND_API SimpleInfer {
34  public:
35   DISABLE_COPY_AND_ASSIGN(SimpleInfer);
36   static SimpleInfer &Instance() noexcept;
37   ops::OpFuncImplPtr GetFunc(const string &op_name);
38   void Register(const std::string &op_name, ops::OpFuncImplPtr &&func);
39 
40   void DoSimpleInfer(const PrimitivePtr &primitive, const ValueSimpleInfoPtr &value_simple_info,
41                      const ops::OpFuncImplPtr &simple_infer_func, const ValuePtrList &input_values);
42 
43  private:
44   SimpleInfer() = default;
45   ~SimpleInfer() = default;
46 
47   std::map<std::string, ops::OpFuncImplPtr> simple_infer_fun_;
48 };
49 
50 template <typename T>
ConvertValuePtr(const std::optional<T> & t)51 ValuePtr ConvertValuePtr(const std::optional<T> &t) {
52   if (!t.has_value()) {
53     return mindspore::kNone;
54   }
55   return t.value();
56 }
57 
58 template <typename T>
ConvertValuePtr(const T & t)59 ValuePtr ConvertValuePtr(const T &t) {
60   return t;
61 }
62 
63 // Api for vector input values
64 ValueSimpleInfoPtr InferBySimple(const PrimitivePtr &primitive, const ValuePtrList &input_values);
65 
66 template <typename... T>
InferBySimple(const PrimitivePtr & primitive,const T &...t)67 ValueSimpleInfoPtr InferBySimple(const PrimitivePtr &primitive, const T &... t) {
68   const auto &simple_infer_func = SimpleInfer::Instance().GetFunc(primitive->name());
69   if (simple_infer_func == nullptr) {
70     return nullptr;
71   }
72   auto value_simple_info = std::make_shared<ValueSimpleInfo>();
73   ValuePtrList input_values;
74   input_values.reserve(sizeof...(t));
75   (input_values.emplace_back(ConvertValuePtr(t)), ...);
76   SimpleInfer::Instance().DoSimpleInfer(primitive, value_simple_info, simple_infer_func, input_values);
77   return value_simple_info;
78 }
79 
80 class SimpleInferRegHelper {
81  public:
SimpleInferRegHelper(const std::string & op_name,ops::OpFuncImplPtr op_func)82   SimpleInferRegHelper(const std::string &op_name, ops::OpFuncImplPtr op_func) {
83     SimpleInfer::Instance().Register(op_name, std::move(op_func));
84   }
85   ~SimpleInferRegHelper() = default;
86 };
87 
88 #define REGISTER_SIMPLE_INFER(op_name, OpFuncImpl) \
89   static const auto op_simple_infer_##op_name =    \
90     mindspore::ops::SimpleInferRegHelper(op_name, std::make_shared<OpFuncImpl>());
91 }  // namespace mindspore::ops
92 #endif
93