• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CCSRC_COMMON_EXPANDER_CORE_INFER_H_
18 #define MINDSPORE_CCSRC_COMMON_EXPANDER_CORE_INFER_H_
19 #include <memory>
20 #include "include/common/expander/core/node.h"
21 #include "abstract/ops/primitive_infer_map.h"
22 
23 namespace mindspore {
24 namespace expander {
25 /// \brief ExpanderInfer is the adapter for inferring functions that is called in emitter.
26 class COMMON_EXPORT ExpanderInfer {
27  public:
28   ExpanderInfer() = default;
29   virtual ~ExpanderInfer() = default;
30 
31   /// \brief Infer shape and dtype for node
32   virtual void Infer(const NodePtr &node) = 0;
33 
34   virtual AbstractBasePtr GetAbstract(const NodePtr &node) = 0;
35   virtual BaseShapePtr GetShape(const NodePtr &node) = 0;
36   virtual TypePtr GetDtype(const NodePtr &node) = 0;
37 };
38 using ExpanderInferPtr = std::shared_ptr<ExpanderInfer>;
39 
40 /// \brief CppInfer calls the InferShapeAndType interface of frontend or backend map.
41 class COMMON_EXPORT CppInfer : public ExpanderInfer {
42  public:
Infer(const NodePtr & node)43   void Infer(const NodePtr &node) override { return InferAnfnode(node->get()); }
GetAbstract(const NodePtr & node)44   AbstractBasePtr GetAbstract(const NodePtr &node) override { return node->get()->abstract(); }
45   BaseShapePtr GetShape(const NodePtr &node) override;
46   TypePtr GetDtype(const NodePtr &node) override;
47 
48  protected:
49   void InferAnfnode(const AnfNodePtr &anfnode) const;
infer_impl_cache()50   static abstract::PrimitiveEvalImplMap &infer_impl_cache() {
51     static abstract::PrimitiveEvalImplMap cache{};
52     return cache;
53   }
54 };
55 }  // namespace expander
56 }  // namespace mindspore
57 #endif  // MINDSPORE_CCSRC_COMMON_EXPANDER_CORE_INFER_H_
58