• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 #ifndef MINDSPORE_CORE_OPS_OPS_FUNC_IMPL_OP_FUNC_IMPL_H
17 #define MINDSPORE_CORE_OPS_OPS_FUNC_IMPL_OP_FUNC_IMPL_H
18 
19 #include <vector>
20 #include <set>
21 #include <memory>
22 #include "ir/primitive.h"
23 #include "abstract/abstract_value.h"
24 
25 namespace mindspore {
26 // The operator input shape and value check status.
27 constexpr int32_t OP_CHECK_SUCCESS = 0;
28 constexpr int32_t OP_CHECK_RETRY = -1;
29 
30 namespace ops {
31 using abstract::AbstractBasePtr;
32 
33 /// \brief This class is a collection of functions related to operator, such as InferShape, InferType, Check, etc.
34 class OpFuncImpl {
35  public:
36   OpFuncImpl() = default;
37   virtual ~OpFuncImpl() = default;
38 
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)39   virtual BaseShapePtr InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) const {
40     MS_LOG(EXCEPTION) << "Not implement exception";
41   }
42 
43   /// \brief Infer the output shape for target operator.
44   ///
45   /// \param[in] primitive Operator's primitive.
46   /// \param[in] input_values Operator's input value pointer list.
47   ///
48   /// \return The inferred output shape array.
InferShape(const PrimitivePtr & primitive,const ValuePtrList & input_values)49   virtual ShapeArray InferShape(const PrimitivePtr &primitive, const ValuePtrList &input_values) const {
50     MS_LOG(EXCEPTION) << "Not implement exception";
51   }
52 
53   /// \brief Infer the output type for target operator.
54   ///
55   /// \param[in] primitive Operator's primitive.
56   /// \param[in] input_args Operator's input arguments pointer list.
57   ///
58   /// \return The inferred object type, such as TensorType, Tuple, List.
InferType(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)59   virtual TypePtr InferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) const {
60     MS_LOG(EXCEPTION) << "Not implement exception";
61   }
62 
63   /// \brief Infer the output type for target operator.
64   ///
65   /// \param[in] primitive Operator's primitive.
66   /// \param[in] input_values Operator's input value pointer list.
67   ///
68   /// \return The inferred object type list, such as TensorType, Tuple, List.
InferType(const PrimitivePtr & primitive,const ValuePtrList & input_values)69   virtual TypePtrList InferType(const PrimitivePtr &primitive, const ValuePtrList &input_values) const {
70     MS_LOG(EXCEPTION) << "Not implement exception";
71   }
72 
73   /// \brief The operator input shape and value check, the function only carries the check of the
74   /// value of InferShape unrelated parameters.
75   ///
76   /// \param[in] primitive Operator's primitive.
77   /// \param[in] input_args Operator's input arguments pointer list.
78   ///
79   /// \return OP_CHECK_SUCCESS if success, else OP_CHECK_RETRY.
CheckValidation(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)80   virtual int32_t CheckValidation(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) const {
81     return OP_CHECK_SUCCESS;
82   }
83 
84   /// \brief Get the indices of infer-depend value.
85   ///
86   /// \return Set with indices of infer-depend value.
GetValueDependArgIndices()87   virtual std::set<int64_t> GetValueDependArgIndices() const { return {}; }
88 };
89 
90 using OpFuncImplPtr = std::shared_ptr<OpFuncImpl>;
91 using OpFuncImplRawPtr = OpFuncImpl *;
92 }  // namespace ops
93 }  // namespace mindspore
94 #endif  // MINDSPORE_CORE_OPS_OPS_FUNC_IMPL_OP_FUNC_IMPL_H
95