1 /** 2 * Copyright 2022-2023 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_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_DYNAMIC_SHAPE_HELPER_H 18 #define MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_DYNAMIC_SHAPE_HELPER_H 19 20 #include <vector> 21 #include <memory> 22 #include <string> 23 24 #include "ir/anf.h" 25 #include "ir/functor.h" 26 #include "utils/ms_utils.h" 27 #include "include/backend/optimizer/optimizer.h" 28 #include "include/backend/optimizer/helper.h" 29 #include "runtime/pynative/op_compiler.h" 30 #include "kernel/framework_utils.h" 31 32 namespace mindspore::opt::dynamic_shape { 33 BaseShapePtr InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args); 34 35 void UpdateKernelTensorShape(const BaseShapePtr &base_shape, 36 const std::vector<kernel::KernelTensor *> &output_kernel_tensors); 37 38 abstract::AbstractBasePtr InferShapeAndType(const PrimitivePtr &primitive, 39 const std::vector<AbstractBasePtr> &input_args); 40 41 void UpdateKernelTensorType(const TypePtr &type, const std::vector<kernel::KernelTensor *> &output_kernel_tensors); 42 43 bool IsRealCNode(const BaseRef &n); 44 void InferOp(const CNodePtr &node, void *args = nullptr); 45 AnfNodePtr GenInferNode(const AnfNodePtr &node); 46 AnfNodePtr GenInitNode(const AnfNodePtr &node); 47 48 struct RelatedCustomActorNode { 49 AnfNodePtr infer_node; 50 AnfNodePtr init_node; 51 }; 52 53 class CustomActorNodeManager { 54 public: 55 static CustomActorNodeManager &Instance(); Reset()56 void Reset() { custom_nodes_map_.clear(); } Register(const AnfNodePtr & node,const RelatedCustomActorNode & custom_nodes)57 void Register(const AnfNodePtr &node, const RelatedCustomActorNode &custom_nodes) { 58 (void)custom_nodes_map_.emplace(node, custom_nodes); 59 } IsRegistered(const AnfNodePtr & node)60 bool IsRegistered(const AnfNodePtr &node) const { return custom_nodes_map_.find(node) != custom_nodes_map_.end(); } GetCustomActorNodes(const AnfNodePtr & node)61 const RelatedCustomActorNode &GetCustomActorNodes(const AnfNodePtr &node) const { 62 if (auto iter = custom_nodes_map_.find(node); iter != custom_nodes_map_.end()) { 63 return iter->second; 64 } 65 66 MS_LOG(EXCEPTION) << "Not registered node!"; 67 } 68 69 private: 70 CustomActorNodeManager() = default; 71 ~CustomActorNodeManager() = default; 72 DISABLE_COPY_AND_ASSIGN(CustomActorNodeManager) 73 OrderedMap<AnfNodePtr, RelatedCustomActorNode> custom_nodes_map_; 74 }; 75 76 /// \brief The class to implement an InferShape function, which is decoupled from the mindspore/core. 77 class InferShapeFunctor : public Functor { 78 public: 79 /// \brief Constructor of InferShapeFunctor. InferShapeFunctor(const std::string & name)80 explicit InferShapeFunctor(const std::string &name) : Functor(name) {} 81 82 /// \brief Destructor of InferShapeFunctor. 83 ~InferShapeFunctor() override = default; 84 MS_DECLARE_PARENT(InferShapeFunctor, Functor) 85 86 /// \brief Infer output shape. 87 /// \param[in] args AbstractBasePtrList of the inputs. 88 /// \return Result BaseShapePtr. 89 virtual BaseShapePtr InferShape(const AbstractBasePtrList &args) = 0; 90 91 /// \brief Pack functor name to a Value 92 /// \return The name of this infershape functor. ToValue()93 ValuePtr ToValue() const override { return MakeValue(name_); }; 94 95 /// \brief Rename the functor. FromValue(const ValuePtr & value)96 void FromValue(const ValuePtr &value) override { name_ = GetValue<std::string>(value); }; 97 }; 98 using InferShapeFunctorPtr = std::shared_ptr<InferShapeFunctor>; 99 constexpr auto kAttrInferShapeFunctor = "infer_shape_functor"; 100 } // namespace mindspore::opt::dynamic_shape 101 #endif // MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_DYNAMIC_SHAPE_HELPER_H 102