• 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 "ops/fusion/add_fusion.h"
18 #include <string>
19 #include <algorithm>
20 #include <memory>
21 #include <vector>
22 #include <map>
23 
24 #include "ops/op_utils.h"
25 
26 namespace mindspore {
27 namespace ops {
set_activation_type(const ActivationType activation_type)28 void AddFusion::set_activation_type(const ActivationType activation_type) {
29   int64_t swi = activation_type;
30   (void)this->AddAttr(kActivationType, MakeValue(swi));
31 }
get_activation_type() const32 ActivationType AddFusion::get_activation_type() const {
33   auto value_ptr = GetAttr(kActivationType);
34   return ActivationType(GetValue<int64_t>(value_ptr));
35 }
Init(const ActivationType activation_type)36 void AddFusion::Init(const ActivationType activation_type) { this->set_activation_type(activation_type); }
37 
38 namespace {
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)39 abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
40   MS_EXCEPTION_IF_NULL(primitive);
41   auto op_name = primitive->name();
42   return BroadCastInferShape(op_name, input_args);
43 }
44 
InferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)45 TypePtr InferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
46   if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr &a) { return a == nullptr; })) {
47     MS_LOG(EXCEPTION) << "nullptr";
48   }
49   std::map<std::string, TypePtr> types;
50   (void)types.emplace("x", input_args[kInputIndex0]->BuildType());
51   (void)types.emplace("y", input_args[kInputIndex1]->BuildType());
52   return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name());
53 }
54 }  // namespace
55 
AddFusionInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)56 AbstractBasePtr AddFusionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
57                                const std::vector<AbstractBasePtr> &input_args) {
58   return std::make_shared<abstract::AbstractTensor>(InferType(primitive, input_args),
59                                                     InferShape(primitive, input_args));
60 }
61 REGISTER_PRIMITIVE_C(kNameAddFusion, AddFusion);
62 }  // namespace ops
63 }  // namespace mindspore
64