1 /**
2 * Copyright 2019 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_CORE_ABSTRACT_PARAM_VALIDATOR_H_
18 #define MINDSPORE_CORE_ABSTRACT_PARAM_VALIDATOR_H_
19
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 #include <cassert>
26 #include "abstract/abstract_value.h"
27 #include "abstract/utils.h"
28 #include "utils/any.h"
29 #include "ir/primitive.h"
30 #include "mindapi/base/macros.h"
31
32 namespace mindspore {
33 namespace abstract {
34 // check if variable's type is an instance of any of accepts or of a subclass of it.
35 TypePtr CheckType(TypePtr type, const TypePtrList &accepts, const std::string &error_message_prefix);
36
37 TypePtr CheckTensorDType(const AbstractBasePtr &tensor, const TypePtrList &accepts,
38 const std::string &error_message_prefix);
39
40 TypePtr CheckTensorsDTypeSame(const AbstractTensorPtrList &tensor_list, const TypePtrList &accepts,
41 const std::string &error_message_prefix);
42
43 TypePtr CheckScalarType(const AbstractScalarPtr &scalar, const TypePtrList &accepts,
44 const std::string &error_message_prefix);
45
46 void CheckShapeSame(const std::string &op, const AbstractTensorPtr &tensor_base, const AbstractTensorPtr &tensor);
47
48 void CheckShapeSame(const std::string &op, const AbstractBasePtr &tensor_base, const AbstractBasePtr &tensor);
49
CheckDtypeSame(const std::string & op,const TypePtr & type1,const TypePtr & type2)50 inline void CheckDtypeSame(const std::string &op, const TypePtr &type1, const TypePtr &type2) {
51 if (*type1 != *type2) {
52 MS_EXCEPTION(TypeError) << "For '" << op << "', the dtype of two args should be same, but the first arg dtype "
53 << type1->ToString() << " are not consistent with second arg dtype " << type2->ToString();
54 }
55 }
56
57 TypePtr CheckDtypeSame(const std::string &op, const AbstractTensorPtr &tensor_base, const AbstractTensorPtr &tensor);
58
59 TypePtr CheckDtypeSame(const std::string &op, const AbstractBasePtr &tensor_base, const AbstractBasePtr &tensor);
60
61 MS_CORE_API int64_t CheckAxis(const std::string &op, const std::string &args_name, const ValuePtr &axis, int64_t min,
62 int64_t max, const std::string &rank_name);
63
64 MS_CORE_API void CheckArgsSize(const std::string &op, const AbstractBasePtrList &args_abs_list, size_t size_expect);
65
66 void CheckShapeAllPositive(const std::string &op, const ShapeVector &shape);
67
68 void CheckShapeAnyAndPositive(const std::string &op, const ShapeVector &shape);
69
70 std::vector<int64_t> CheckAttrIntOrTuple(const std::string &op, const ValuePtr &attr, const size_t start_idx,
71 const size_t num_element);
72
73 std::string CheckAttrStringSet(const std::string &op, const ValuePtr &attr, const std::string &attr_name,
74 const std::set<std::string> &val_set);
75
76 void CheckRequiredArgsSize(const std::string &op, const AbstractBasePtrList &args_abs_list, size_t size_expect);
77
78 template <typename T>
79 struct ReportNameTraits {};
80
81 #define ABSTRACT_REPORT_NAME_TRAITS(abstract) \
82 template <> \
83 struct ReportNameTraits<Abstract##abstract> { \
84 static constexpr char name[] = #abstract; \
85 };
86 ABSTRACT_REPORT_NAME_TRAITS(Tensor)
ABSTRACT_REPORT_NAME_TRAITS(Tuple)87 ABSTRACT_REPORT_NAME_TRAITS(Tuple)
88 ABSTRACT_REPORT_NAME_TRAITS(Scalar)
89 ABSTRACT_REPORT_NAME_TRAITS(List)
90 ABSTRACT_REPORT_NAME_TRAITS(Dictionary)
91 ABSTRACT_REPORT_NAME_TRAITS(Slice)
92 ABSTRACT_REPORT_NAME_TRAITS(Function)
93 ABSTRACT_REPORT_NAME_TRAITS(Type)
94 ABSTRACT_REPORT_NAME_TRAITS(KeywordArg)
95 ABSTRACT_REPORT_NAME_TRAITS(RowTensor)
96 ABSTRACT_REPORT_NAME_TRAITS(COOTensor)
97 ABSTRACT_REPORT_NAME_TRAITS(CSRTensor)
98 ABSTRACT_REPORT_NAME_TRAITS(MapTensor)
99 ABSTRACT_REPORT_NAME_TRAITS(Sequence)
100
101 template <typename T>
102 std::shared_ptr<T> CheckArg(const std::string &op, const AbstractBasePtrList &args_abs_list, size_t index) {
103 if (index >= args_abs_list.size()) {
104 MS_EXCEPTION(ValueError) << op << " evaluator args list index out of bound, size " << args_abs_list.size()
105 << ", index " << index;
106 }
107 auto arg = dyn_cast<T>(args_abs_list[index]);
108 if (arg == nullptr) {
109 MS_EXCEPTION(TypeError) << "For \'" << op << "\', input[" << index << "] should be " << ReportNameTraits<T>::name
110 << ", but got " << args_abs_list[index]->BuildType()->ToString() << ".";
111 }
112 return arg;
113 }
114
115 // check if each element in args_abs is type T, and can be joined.
116 template <typename T>
CheckArgsSpec(const AbstractBasePtrList & args_list)117 void CheckArgsSpec(const AbstractBasePtrList &args_list) {
118 for (const auto &arg : args_list) {
119 MS_EXCEPTION_IF_NULL(arg);
120 if (!arg->isa<T>()) {
121 auto type = arg->BuildType();
122 MS_EXCEPTION_IF_NULL(type);
123 MS_EXCEPTION(TypeError) << "Expected type " << ReportNameTraits<T>::name << ", but got " << type->ToString()
124 << ".";
125 }
126 }
127 (void)AbstractJoin(args_list);
128 }
129 } // namespace abstract
130 } // namespace mindspore
131
132 #endif // MINDSPORE_CORE_ABSTRACT_PARAM_VALIDATOR_H_
133