• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/delegates/gpu/common/transformations/add_bias.h"
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "absl/memory/memory.h"
23 #include "absl/types/any.h"
24 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
25 #include "tensorflow/lite/delegates/gpu/common/model.h"
26 #include "tensorflow/lite/delegates/gpu/common/model_transformer.h"
27 #include "tensorflow/lite/delegates/gpu/common/operations.h"
28 #include "tensorflow/lite/delegates/gpu/common/shape.h"
29 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
30 
31 namespace tflite {
32 namespace gpu {
33 namespace {
34 
FillBias(int output_channels,tflite::gpu::Tensor<Linear,DataType::FLOAT32> * biases)35 TransformResult FillBias(
36     int output_channels,
37     tflite::gpu::Tensor<Linear, DataType::FLOAT32>* biases) {
38   if (biases->data.empty()) {
39     *biases =
40         MakeZeroTensor<Linear, DataType::FLOAT32>(Linear(output_channels));
41     return {TransformStatus::APPLIED, "Added bias"};
42   }
43   if (biases->shape.v != output_channels) {
44     float last_value = biases->data.back();
45     biases->shape.v = output_channels;
46     biases->data.resize(output_channels, last_value);
47     return {TransformStatus::APPLIED, "Bias extended"};
48   }
49   return {TransformStatus::SKIPPED, ""};
50 }
51 
52 class AddBias : public NodeTransformation {
53  public:
ApplyToNode(Node * node,GraphFloat32 * graph)54   TransformResult ApplyToNode(Node* node, GraphFloat32* graph) final {
55     if (node->operation.type == ToString(OperationType::CONVOLUTION_2D)) {
56       if (graph->FindInputs(node->id).size() != 1) {
57         return {TransformStatus::DECLINED,
58                 "This transformation is only applicable to conv with one "
59                 "runtime input."};
60       }
61       auto& attr =
62           absl::any_cast<Convolution2DAttributes&>(node->operation.attributes);
63       return FillBias(attr.weights.shape.o, &attr.bias);
64     }
65     if (node->operation.type ==
66         ToString(OperationType::CONVOLUTION_TRANSPOSED)) {
67       auto& attr = absl::any_cast<ConvolutionTransposedAttributes&>(
68           node->operation.attributes);
69       return FillBias(attr.weights.shape.o, &attr.bias);
70     }
71     if (node->operation.type ==
72         ToString(OperationType::DEPTHWISE_CONVOLUTION)) {
73       if (graph->FindInputs(node->id).size() != 1) {
74         return {TransformStatus::DECLINED,
75                 "This transformation is only applicable to depth wise conv "
76                 "with one "
77                 "runtime input."};
78       }
79       auto& attr = absl::any_cast<DepthwiseConvolution2DAttributes&>(
80           node->operation.attributes);
81       return FillBias(attr.weights.shape.o * attr.weights.shape.i, &attr.bias);
82     }
83     if (node->operation.type == ToString(OperationType::FULLY_CONNECTED)) {
84       auto& attr =
85           absl::any_cast<FullyConnectedAttributes&>(node->operation.attributes);
86       return FillBias(attr.weights.shape.o, &attr.bias);
87     }
88     return {TransformStatus::SKIPPED, ""};
89   }
90 };
91 
92 }  // namespace
93 
NewAddBias()94 std::unique_ptr<NodeTransformation> NewAddBias() {
95   return absl::make_unique<AddBias>();
96 }
97 
98 }  // namespace gpu
99 }  // namespace tflite
100