• 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 <set>
18 #include <string>
19 #include <vector>
20 #include <map>
21 #include <memory>
22 #include "ops/addn.h"
23 #include "ops/op_utils.h"
24 
25 namespace mindspore {
26 namespace ops {
27 namespace {
AddNInferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)28 abstract::ShapePtr AddNInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
29   MS_EXCEPTION_IF_NULL(primitive);
30   if (!input_args[0]->isa<abstract::AbstractTuple>() && !input_args[0]->isa<abstract::AbstractList>()) {
31     MS_EXCEPTION(TypeError) << "The input of AddN must be list or tuple of tensors.";
32   }
33   auto elements = input_args[0]->isa<abstract::AbstractTuple>()
34                     ? input_args[0]->cast<abstract::AbstractTuplePtr>()->elements()
35                     : input_args[0]->cast<abstract::AbstractListPtr>()->elements();
36   (void)CheckAndConvertUtils::CheckInteger("concat element num", SizeToLong(elements.size()), kGreaterEqual, 1,
37                                            primitive->name());
38   (void)primitive->AddAttr("n", MakeValue(SizeToLong(elements.size())));
39   auto shape_0 = elements[0]->BuildShape();
40   auto element0_shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(shape_0);
41   for (size_t i = 0; i < elements.size(); ++i) {
42     auto shape = elements[i]->BuildShape();
43     if (shape->isa<abstract::Shape>() && shape_0->isa<abstract::Shape>()) {
44       const auto &shape_vec = shape->cast<abstract::ShapePtr>()->shape();
45       const auto &shape_0_vec = shape_0->cast<abstract::ShapePtr>()->shape();
46       if ((shape_vec == ShapeVector({1}) && shape_0_vec == ShapeVector()) ||
47           (shape_vec == ShapeVector() && shape_0_vec == ShapeVector({1}))) {
48         MS_LOG(DEBUG) << "The primitive[" << primitive->name() << "]'s input[" << i << "] shape: " << shape->ToString()
49                       << " are consistent with the shape of input[0]" << shape_0->ToString();
50         continue;
51       }
52     }
53     if (*shape != *shape_0) {
54       MS_EXCEPTION(ValueError) << "The primitive[" << primitive->name() << "]'s input shape must be same, "
55                                << "but got the shape of input[" << i << "]: " << shape->ToString()
56                                << ", shape of input[0]:" << shape_0->ToString();
57     }
58   }
59   auto in_shape = element0_shape_map[kShape];
60   auto min_shape = element0_shape_map[kMinShape];
61   auto max_shape = element0_shape_map[kMaxShape];
62   return std::make_shared<abstract::Shape>(in_shape, min_shape, max_shape);
63 }
64 
AddNInferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)65 TypePtr AddNInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
66   MS_EXCEPTION_IF_NULL(prim);
67   auto prim_name = prim->name();
68   if (!input_args[0]->isa<abstract::AbstractTuple>() && !input_args[0]->isa<abstract::AbstractList>()) {
69     MS_EXCEPTION(TypeError) << "The input of AddN must be list or tuple of tensors.";
70   }
71   auto elements = input_args[0]->isa<abstract::AbstractTuple>()
72                     ? input_args[0]->cast<abstract::AbstractTuplePtr>()->elements()
73                     : input_args[0]->cast<abstract::AbstractListPtr>()->elements();
74   (void)CheckAndConvertUtils::CheckInteger("concat element num", SizeToLong(elements.size()), kGreaterEqual, 1,
75                                            prim->name());
76   std::map<std::string, TypePtr> types;
77   (void)types.emplace("element_0", elements[0]->BuildType());
78   for (size_t i = 0; i < elements.size(); ++i) {
79     if (elements[i]->BuildType()->type_id() == kObjectTypeUndeterminedType) {
80       return elements[0]->BuildType();
81     }
82     std::string element_i = "element_" + std::to_string(i);
83     (void)types.emplace(element_i, elements[i]->BuildType());
84   }
85   std::set<TypePtr> valid_types = common_valid_types;
86   valid_types.insert(kBool);
87   (void)CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim_name);
88   return elements[0]->BuildType();
89 }
90 }  // namespace
AddNInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)91 AbstractBasePtr AddNInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
92                           const std::vector<AbstractBasePtr> &input_args) {
93   MS_EXCEPTION_IF_NULL(primitive);
94   auto prim_name = primitive->name();
95   (void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kGreaterEqual, 1, prim_name);
96   for (const auto &item : input_args) {
97     MS_EXCEPTION_IF_NULL(item);
98   }
99   return abstract::MakeAbstract(AddNInferShape(primitive, input_args), AddNInferType(primitive, input_args));
100 }
101 REGISTER_PRIMITIVE_EVAL_IMPL(AddN, prim::kPrimAddN, AddNInfer, nullptr, true);
102 }  // namespace ops
103 }  // namespace mindspore
104