• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
18 #include <algorithm>
19 #include <memory>
20 #include <set>
21 #include <vector>
22 #include <numeric>
23 #include <functional>
24 #include "ops/reshape.h"
25 #include "utils/check_convert_utils.h"
26 #include "abstract/primitive_infer_map.h"
27 
28 namespace mindspore {
29 namespace ops {
ReshapeInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)30 AbstractBasePtr ReshapeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
31                              const std::vector<AbstractBasePtr> &input_args) {
32   MS_EXCEPTION_IF_NULL(primitive);
33   auto prim_name = primitive->name();
34   const int64_t input_num = 2;
35   (void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
36   for (const auto &item : input_args) {
37     MS_EXCEPTION_IF_NULL(item);
38   }
39   auto x = input_args[0]->cast<abstract::AbstractTensorPtr>();
40   MS_EXCEPTION_IF_NULL(x);
41   auto shape = input_args[1]->cast<abstract::AbstractTuplePtr>();
42   MS_EXCEPTION_IF_NULL(shape);
43   auto shape_v = GetValue<std::vector<int64_t>>(shape->BuildValue());
44   int64_t neg_index = -1;
45   int64_t dim_prod = 1;
46   for (size_t i = 0; i < shape_v.size(); ++i) {
47     if (shape_v[i] == -1) {
48       if (neg_index != -1) {
49         MS_LOG(EXCEPTION) << "The Reshape's shape input can only has one -1 at most.";
50       }
51       neg_index = SizeToLong(i);
52     } else {
53       dim_prod *= shape_v[i];
54     }
55   }
56   MS_EXCEPTION_IF_NULL(x->shape());
57   auto x_shape = x->shape()->shape();
58   int64_t arr_prod =
59     std::accumulate(x_shape.begin(), x_shape.end(), static_cast<int64_t>(1), std::multiplies<int64_t>());
60   if (arr_prod <= 0) {
61     ShapeVector x_max_shape = x->shape()->max_shape();
62     ShapeVector x_min_shape = x->shape()->min_shape();
63     if (x_max_shape.empty()) {
64       x_max_shape = x_shape;
65     }
66     if (x_min_shape.empty()) {
67       x_min_shape = x_shape;
68     }
69     int64_t max_arr_prod =
70       std::accumulate(x_max_shape.begin(), x_max_shape.end(), static_cast<int64_t>(1), std::multiplies<int64_t>());
71     int64_t min_arr_prod =
72       std::accumulate(x_min_shape.begin(), x_min_shape.end(), static_cast<int64_t>(1), std::multiplies<int64_t>());
73     ShapeVector max_shape = shape_v;
74     ShapeVector min_shape = shape_v;
75     if (neg_index != -1) {
76       max_shape[LongToSize(neg_index)] = max_arr_prod / dim_prod;
77       min_shape[LongToSize(neg_index)] = min_arr_prod / dim_prod;
78     } else {
79       MS_LOG(EXCEPTION) << "For dynamic shape, Reshape's shape input must have neg index";
80     }
81     return std::make_shared<abstract::AbstractTensor>(x->element(),
82                                                       std::make_shared<abstract::Shape>(shape_v, min_shape, max_shape));
83   } else {
84     if (dim_prod <= 0 || arr_prod % dim_prod != 0) {
85       MS_LOG(EXCEPTION) << "The product of input_x's shape should > 0, and can be divided by product of input_shape, "
86                            "but product of input_x's shape is "
87                         << arr_prod << ", product of input_shape is" << dim_prod;
88     }
89     if (neg_index != -1) {
90       shape_v[LongToSize(neg_index)] = arr_prod / dim_prod;
91       dim_prod *= shape_v[LongToSize(neg_index)];
92     }
93     if (arr_prod != dim_prod) {
94       MS_LOG(EXCEPTION) << "The product of input_x's shape should be equal to product of input_shape, "
95                            "but product of input_x's shape is "
96                         << arr_prod << ", product of input_shape is" << dim_prod;
97     }
98     return std::make_shared<abstract::AbstractTensor>(x->element(), std::make_shared<abstract::Shape>(shape_v));
99   }
100 }
101 REGISTER_PRIMITIVE_C(kNameReshape, Reshape);
102 }  // namespace ops
103 }  // namespace mindspore
104