• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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 "ops/tile.h"
19 #include "ops/op_utils.h"
20 #include "utils/check_convert_utils.h"
21 #include "abstract/primitive_infer_map.h"
22 
23 namespace mindspore {
24 namespace ops {
25 namespace {
GetInferShape(const std::vector<int64_t> & input_shape,const std::vector<int64_t> & multiples_v)26 std::vector<int64_t> GetInferShape(const std::vector<int64_t> &input_shape, const std::vector<int64_t> &multiples_v) {
27   int64_t len_sub = SizeToLong(multiples_v.size() - input_shape.size());
28   std::vector<int64_t> infer_shape = input_shape;
29   std::vector<int64_t> multiples_w;
30   if (len_sub == 0) {
31     multiples_w = multiples_v;
32   }
33   if (len_sub > 0) {
34     for (int64_t i = 0; i < len_sub; i++) {
35       (void)infer_shape.insert(infer_shape.begin(), 1);
36     }
37     multiples_w = multiples_v;
38   }
39   if (len_sub < 0) {
40     MS_EXCEPTION(ValueError) << "the length of multiples can not be smaller than the"
41                                 "length of dimension in input_x";
42   }
43   for (size_t i = 0; i < multiples_w.size(); i++) {
44     if (infer_shape[i] == abstract::Shape::SHP_ANY) {
45       continue;
46     }
47     infer_shape[i] *= multiples_w[i];
48   }
49   return infer_shape;
50 }
51 
TileInferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)52 abstract::ShapePtr TileInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
53   MS_EXCEPTION_IF_NULL(primitive);
54   auto prim_name = primitive->name();
55   const int INDEX = 2;
56   (void)CheckAndConvertUtils::CheckInteger("input numbers", SizeToLong(input_args.size()), kEqual, INDEX, prim_name);
57   for (const auto &item : input_args) {
58     MS_EXCEPTION_IF_NULL(item);
59   }
60   auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape());
61   auto input_shape = shape_map[kShape];
62   auto min_shape = shape_map[kMinShape];
63   auto max_shape = shape_map[kMaxShape];
64   auto get_cast_temp = input_args[1]->cast<abstract::AbstractTuplePtr>();
65   MS_EXCEPTION_IF_NULL(get_cast_temp);
66   auto multiples_v = GetValue<std::vector<int64_t>>(get_cast_temp->BuildValue());
67   auto infer_shape = GetInferShape(input_shape, multiples_v);
68   if (max_shape.empty() && min_shape.empty()) {
69     return std::make_shared<abstract::Shape>(infer_shape);
70   }
71   auto infer_shape_min = GetInferShape(min_shape, multiples_v);
72   auto infer_shape_max = GetInferShape(max_shape, multiples_v);
73   return std::make_shared<abstract::Shape>(infer_shape, infer_shape_min, infer_shape_max);
74 }
75 
TileInferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)76 TypePtr TileInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
77   MS_EXCEPTION_IF_NULL(prim);
78   auto prim_name = prim->name();
79   const int64_t input_num = 2;
80   (void)CheckAndConvertUtils::CheckInteger("infer", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
81   for (const auto &item : input_args) {
82     MS_EXCEPTION_IF_NULL(item);
83   }
84   MS_EXCEPTION_IF_NULL(input_args[0]);
85   auto x_type_map = input_args[0]->BuildType();
86   MS_EXCEPTION_IF_NULL(x_type_map);
87   auto x_dtype = x_type_map->cast<TensorTypePtr>();
88   MS_EXCEPTION_IF_NULL(x_dtype);
89   std::set<TypePtr> template_types = {kTensorType};
90   return CheckAndConvertUtils::CheckTensorTypeValid("x_dtype", x_dtype, template_types, prim_name);
91 }
92 }  // namespace
93 
TileInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)94 AbstractBasePtr TileInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
95                           const std::vector<AbstractBasePtr> &input_args) {
96   return abstract::MakeAbstract(TileInferShape(primitive, input_args), TileInferType(primitive, input_args));
97 }
98 REGISTER_PRIMITIVE_C(kNameTile, Tile);
99 }  // namespace ops
100 }  // namespace mindspore
101