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 "abstract/abstract_value.h"
26 #include "abstract/utils.h"
27 #include "utils/any.h"
28 #include "ir/primitive.h"
29
30 namespace mindspore {
31 namespace abstract {
32 // check if variable's type is an instance of any of accepts or of a subclass of it.
33 TypePtr CheckType(TypePtr type, const TypePtrList &accepts, const std::string &error_message_prefix);
34
35 TypePtr CheckTensorDType(const AbstractTensorPtr &tensor, const TypePtrList &accepts,
36 const std::string &error_message_prefix);
37
38 TypePtr CheckTensorsDTypeSame(const AbstractTensorPtrList &tensor_list, const TypePtrList &accepts,
39 const std::string &error_message_prefix);
40
41 TypePtr CheckScalarType(const AbstractScalarPtr &scalar, const TypePtrList &accepts,
42 const std::string &error_message_prefix);
43
44 ShapePtr CheckShapeSame(const std::string &op, const AbstractTensorPtr &tensor_base, const AbstractTensorPtr &tensor);
45
46 TypePtr CheckDtypeSame(const std::string &op, const AbstractTensorPtr &tensor_base, const AbstractTensorPtr &tensor);
47
48 int64_t CheckAxis(const std::string &op, const ValuePtr &axis, int64_t min, int64_t max);
49
50 void CheckArgsSize(const std::string &op, const AbstractBasePtrList &args_spec_list, size_t size_expect);
51
52 void CheckShapeAllPositive(const std::string &op, const ShapeVector &shape);
53
54 void CheckShapeAnyAndPositive(const std::string &op, const ShapeVector &shape);
55
56 int64_t CheckAttrPositiveInt64(const std::string &op, const ValuePtr &attr, const std::string &attr_name);
57
58 std::vector<int64_t> CheckAttrIntOrTuple(const std::string &op, const ValuePtr &attr, const size_t start_idx,
59 const size_t num_element);
60
61 std::string CheckAttrStringSet(const std::string &op, const ValuePtr &attr, const std::string &attr_name,
62 const std::set<std::string> &val_set);
63
64 void CheckRequiredArgsSize(const std::string &op, const AbstractBasePtrList &args_spec_list, size_t size_expect);
65
66 template <typename T>
67 struct ReportNameTraits {};
68
69 #define ABSTRACT_REPORT_NAME_TRAITS(abstract) \
70 template <> \
71 struct ReportNameTraits<Abstract##abstract> { \
72 static constexpr char name[] = #abstract; \
73 };
74 ABSTRACT_REPORT_NAME_TRAITS(Tensor)
ABSTRACT_REPORT_NAME_TRAITS(Tuple)75 ABSTRACT_REPORT_NAME_TRAITS(Tuple)
76 ABSTRACT_REPORT_NAME_TRAITS(Scalar)
77 ABSTRACT_REPORT_NAME_TRAITS(List)
78 ABSTRACT_REPORT_NAME_TRAITS(Dictionary)
79 ABSTRACT_REPORT_NAME_TRAITS(Slice)
80 ABSTRACT_REPORT_NAME_TRAITS(Function)
81 ABSTRACT_REPORT_NAME_TRAITS(Type)
82 ABSTRACT_REPORT_NAME_TRAITS(KeywordArg)
83 ABSTRACT_REPORT_NAME_TRAITS(Class)
84 ABSTRACT_REPORT_NAME_TRAITS(RowTensor)
85 ABSTRACT_REPORT_NAME_TRAITS(SparseTensor)
86 ABSTRACT_REPORT_NAME_TRAITS(Sequeue)
87
88 template <typename T>
89 std::shared_ptr<T> CheckArg(const std::string &op, const AbstractBasePtrList &args_spec_list, size_t index) {
90 if (index >= args_spec_list.size()) {
91 MS_EXCEPTION(ValueError) << op << " evaluator args list index out of bound, size " << args_spec_list.size()
92 << ", index " << index;
93 }
94 auto arg = dyn_cast<T>(args_spec_list[index]);
95 if (arg == nullptr) {
96 MS_EXCEPTION(TypeError) << "Operator " << op << " input[" << index << "] should be " << ReportNameTraits<T>::name
97 << ", but got " << args_spec_list[index]->BuildType()->ToString() << ".";
98 }
99 return arg;
100 }
101
102 // check if each element in args_spec is type T, and can be joined.
103 template <typename T>
CheckArgsSpec(const AbstractBasePtrList & args_list)104 void CheckArgsSpec(const AbstractBasePtrList &args_list) {
105 for (const auto &arg : args_list) {
106 MS_EXCEPTION_IF_NULL(arg);
107 if (!arg->isa<T>()) {
108 auto type = arg->BuildType();
109 MS_EXCEPTION_IF_NULL(type);
110 MS_EXCEPTION(TypeError) << "Expected type " << ReportNameTraits<T>::name << ", but got " << type->ToString()
111 << ".";
112 }
113 }
114 (void)AbstractJoin(args_list);
115 }
116 } // namespace abstract
117 } // namespace mindspore
118
119 #endif // MINDSPORE_CORE_ABSTRACT_PARAM_VALIDATOR_H_
120