• 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/full_connection.h"
18 #include <string>
19 #include "ops/op_utils.h"
20 
21 namespace mindspore {
22 namespace ops {
set_has_bias(const bool has_bias)23 void FullConnection::set_has_bias(const bool has_bias) { (void)this->AddAttr(kHasBias, MakeValue(has_bias)); }
24 
get_has_bias() const25 bool FullConnection::get_has_bias() const {
26   auto value_ptr = GetAttr(kHasBias);
27   MS_EXCEPTION_IF_NULL(value_ptr);
28   return GetValue<bool>(value_ptr);
29 }
30 
set_axis(const int64_t axis)31 void FullConnection::set_axis(const int64_t axis) { (void)this->AddAttr(kAxis, MakeValue(axis)); }
get_axis() const32 int64_t FullConnection::get_axis() const {
33   auto value_ptr = GetAttr(kAxis);
34   MS_EXCEPTION_IF_NULL(value_ptr);
35   return GetValue<int64_t>(value_ptr);
36 }
37 
set_use_axis(const bool use_axis)38 void FullConnection::set_use_axis(const bool use_axis) { (void)this->AddAttr(kUseAxis, MakeValue(use_axis)); }
get_use_axis() const39 bool FullConnection::get_use_axis() const {
40   auto value_ptr = GetAttr(kUseAxis);
41   MS_EXCEPTION_IF_NULL(value_ptr);
42   return GetValue<bool>(value_ptr);
43 }
44 
set_activation_type(const ActivationType & activation_type)45 void FullConnection::set_activation_type(const ActivationType &activation_type) {
46   int64_t swi = activation_type;
47   (void)this->AddAttr(kActivationType, MakeValue(swi));
48 }
get_activation_type() const49 ActivationType FullConnection::get_activation_type() const {
50   auto value_ptr = GetAttr(kActivationType);
51   MS_EXCEPTION_IF_NULL(value_ptr);
52   return ActivationType(GetValue<int64_t>(value_ptr));
53 }
Init(const bool has_bias,const int64_t axis,const bool use_axis,const ActivationType & activation_type)54 void FullConnection::Init(const bool has_bias, const int64_t axis, const bool use_axis,
55                           const ActivationType &activation_type) {
56   this->set_has_bias(has_bias);
57   this->set_axis(axis);
58   this->set_use_axis(use_axis);
59   this->set_activation_type(activation_type);
60 }
FullConnectionInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)61 AbstractBasePtr FullConnectionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
62                                     const std::vector<AbstractBasePtr> &input_args) {
63   MS_EXCEPTION_IF_NULL(primitive);
64   auto prim_name = primitive->name();
65   MS_EXCEPTION_IF_NULL(input_args[kInputIndex0]);
66   MS_EXCEPTION_IF_NULL(input_args[kInputIndex1]);
67   auto input0 = input_args[0];
68   auto input1 = input_args[1];
69   auto input0_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input0->BuildShape())[kShape];
70   auto input1_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input1->BuildShape())[kShape];
71   auto prim_axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
72   auto has_bias = GetValue<bool>(primitive->GetAttr(kHasBias));
73   const int64_t input_num_bias = 3;
74   const int64_t input_num = 2;
75   if (has_bias) {
76     (void)CheckAndConvertUtils::CheckInteger("input_args.size()", SizeToLong(input_args.size()), kEqual, input_num_bias,
77                                              prim_name);
78   } else {
79     (void)CheckAndConvertUtils::CheckInteger("input_args.size()", SizeToLong(input_args.size()), kEqual, input_num,
80                                              prim_name);
81   }
82   auto use_axis = GetValue<bool>(primitive->GetAttr(kUseAxis));
83   if (use_axis && (prim_axis < 1 || prim_axis > (int64_t)input0_shape.size())) {
84     MS_EXCEPTION(ValueError) << "Full Connection axis is invalid";
85   }
86   int64_t new_k = 1;
87   if (use_axis) {
88     for (size_t t = LongToSize(prim_axis); t < input0_shape.size(); t++) {
89       new_k *= input0_shape[t];
90     }
91     if (new_k != input1_shape[1]) {
92       MS_EXCEPTION(ValueError) << "Input1 size is invalid";
93     }
94   } else {
95     new_k = input1_shape[1];
96   }
97   if (has_bias) {
98     auto input2_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
99     if (input2_shape[0] != input1_shape[0]) {
100       MS_EXCEPTION(ValueError) << "Bias size is invalid";
101     }
102   }
103   std::vector<int64_t> out_shape = {(int64_t)input0_shape.size()};
104   if (use_axis) {
105     out_shape.resize(LongToSize(prim_axis) + 1);
106     out_shape[LongToSize(prim_axis)] = input1_shape[0];
107   } else {
108     int64_t total = 1;
109     for (size_t i = 0; i < input0_shape.size(); i++) {
110       total *= input0_shape[i];
111     }
112     out_shape.resize(2);
113     auto batch_size = total / new_k;
114     out_shape[0] = batch_size;
115     out_shape[1] = input1_shape[0];
116   }
117   auto input0_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
118   return std::make_shared<abstract::AbstractTensor>(input0_type, out_shape);
119 }
120 REGISTER_PRIMITIVE_C(kNameFullConnection, FullConnection);
121 }  // namespace ops
122 }  // namespace mindspore
123