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 #include "ops/reduce.h"
17 #include <string>
18 #include <algorithm>
19 #include <memory>
20 #include <set>
21 #include <vector>
22 #include "ops/op_utils.h"
23 #include "utils/check_convert_utils.h"
24 #include "abstract/primitive_infer_map.h"
25
26 namespace mindspore {
27 namespace ops {
28 namespace {
reduce_one_axis(const int64_t one_axis,const int64_t dim,std::set<int64_t> axis_reduce)29 void reduce_one_axis(const int64_t one_axis, const int64_t dim, std::set<int64_t> axis_reduce) {
30 CheckAndConvertUtils::CheckInRange("axis", one_axis, kIncludeLeft, {-dim, dim}, "Reduce");
31 if (one_axis < 0) {
32 axis_reduce.insert(one_axis);
33 }
34 }
35
infer_shape_reduce(std::vector<int64_t> input_x_shape,const ValuePtr axis_value,const bool keep_dims)36 std::vector<int64_t> infer_shape_reduce(std::vector<int64_t> input_x_shape, const ValuePtr axis_value,
37 const bool keep_dims) {
38 int64_t dim = SizeToLong(input_x_shape.size());
39 std::set<int64_t> axis_reduce;
40 if (axis_value == nullptr) {
41 std::vector<int64_t> vec;
42 if (keep_dims) {
43 return std::vector<int64_t>(dim, 1);
44 }
45 return vec;
46 }
47 auto axis_value_elem = GetValue<std::vector<int64_t>>(axis_value);
48 if (axis_value_elem.size() == 1) {
49 reduce_one_axis(axis_value_elem[0], dim, axis_reduce);
50 } else {
51 size_t size = axis_value_elem.size();
52 for (size_t i = 0; i < size; i++) {
53 reduce_one_axis(axis_value_elem[i], dim, axis_reduce);
54 }
55 }
56 std::vector<int64_t> out_shape;
57 for (int64_t i = 0; i < dim; i++) {
58 if (axis_reduce.find(i) != axis_reduce.end()) {
59 if (keep_dims) {
60 (void)out_shape.emplace_back(1);
61 }
62 } else {
63 (void)out_shape.emplace_back(input_x_shape[LongToSize(i)]);
64 }
65 }
66 return out_shape;
67 }
68
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)69 abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
70 auto axis_value = input_args[1]->BuildValue();
71
72 MS_EXCEPTION_IF_NULL(primitive);
73 auto input_x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
74
75 auto keep_dims = GetValue<bool>(primitive->GetAttr(kKeepDims));
76 auto out_shape = infer_shape_reduce(input_x_shape, axis_value, keep_dims);
77
78 return std::make_shared<abstract::Shape>(out_shape);
79 }
80
InferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)81 TypePtr InferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
82 for (const auto &item : input_args) {
83 MS_EXCEPTION_IF_NULL(item);
84 }
85 return CheckAndConvertUtils::CheckTensorTypeValid("input_x", input_args[0]->BuildType(), common_valid_types,
86 prim->name());
87 }
88 } // namespace
89
set_keep_dims(const bool keep_dims)90 void Reduce::set_keep_dims(const bool keep_dims) { (void)this->AddAttr(kKeepDims, MakeValue(keep_dims)); }
91
get_keep_dims() const92 bool Reduce::get_keep_dims() const { return GetValue<bool>(GetAttr(kKeepDims)); }
93
Init(const bool keep_dims)94 void Reduce::Init(const bool keep_dims) { this->set_keep_dims(keep_dims); }
95
ReduceInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)96 AbstractBasePtr ReduceInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
97 const std::vector<AbstractBasePtr> &input_args) {
98 return std::make_shared<abstract::AbstractTensor>(InferType(primitive, input_args),
99 InferShape(primitive, input_args)->shape());
100 }
101 REGISTER_PRIMITIVE_C(kNameReduce, Reduce);
102 } // namespace ops
103 } // namespace mindspore
104