1 /**
2 * Copyright 2020 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 #include <map>
18 #include <string>
19 #include <set>
20 #include <vector>
21 #include <memory>
22
23 #include "ops/assert.h"
24 #include "ops/op_utils.h"
25
26 namespace mindspore {
27 namespace ops {
Init(const int64_t summarize)28 void Assert::Init(const int64_t summarize) { set_summarize(summarize); }
29
set_summarize(const int64_t summarize)30 void Assert::set_summarize(const int64_t summarize) { (void)this->AddAttr(kSummarize, MakeValue(summarize)); }
31
get_summarize() const32 int64_t Assert::get_summarize() const {
33 auto value_ptr = GetAttr(kSummarize);
34 return GetValue<int64_t>(value_ptr);
35 }
36
AssertInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)37 AbstractBasePtr AssertInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
38 const std::vector<AbstractBasePtr> &input_args) {
39 MS_EXCEPTION_IF_NULL(primitive);
40 auto op_name = primitive->name();
41 for (const auto &item : input_args) {
42 MS_EXCEPTION_IF_NULL(item);
43 }
44 TypePtr condition;
45 if (!(input_args[0]->BuildType()->type_id() == kObjectTypeTensorType)) {
46 auto condition_values = GetValue<std::vector<bool>>(input_args[0]->BuildValue());
47 (void)CheckAndConvertUtils::CheckInteger("condition's rank", SizeToLong(condition_values.size()), kLessEqual, 1,
48 op_name);
49 if (condition_values.size() == 1) {
50 if (!condition_values[0]) {
51 MS_EXCEPTION(ValueError) << "condition value must be `true` when only one value contained.";
52 }
53 }
54 condition = TypeIdToType(kNumberTypeBool);
55 } else {
56 auto condition_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
57 (void)CheckAndConvertUtils::CheckInteger("condition's rank", condition_shape[0], kLessEqual, 1, op_name);
58 if (condition_shape[0] == 1) {
59 auto condition_value = reinterpret_cast<bool *>(input_args[0]->BuildValue()->cast<tensor::TensorPtr>()->data_c());
60 MS_EXCEPTION_IF_NULL(condition_value);
61 if (!*condition_value) {
62 MS_EXCEPTION(ValueError) << "condition value must be `true` when only one value contained.";
63 }
64 }
65 condition = input_args[0]->BuildType();
66 }
67 std::vector<int64_t> output_shape = {1};
68 std::set<TypePtr> local_bool = {kBool};
69 std::map<std::string, TypePtr> args = {{"condition", condition}};
70 (void)CheckAndConvertUtils::CheckScalarOrTensorTypesSame(args, local_bool, op_name);
71 auto inputs_type = input_args[1]->BuildType()->cast<TuplePtr>()->elements();
72 for (auto dtype : inputs_type) {
73 std::set<TypePtr> template_types = {kTensorType};
74 (void)CheckAndConvertUtils::CheckSubClass("input", dtype, template_types, op_name);
75 }
76 return std::make_shared<abstract::AbstractTensor>(kInt32, output_shape);
77 }
78 REGISTER_PRIMITIVE_C(kNameAssert, Assert);
79 } // namespace ops
80 } // namespace mindspore
81