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/make_fully_connected.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/model.h"
25 #include "tensorflow/lite/delegates/gpu/common/model_transformer.h"
26 #include "tensorflow/lite/delegates/gpu/common/operations.h"
27 #include "tensorflow/lite/delegates/gpu/common/shape.h"
28 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
29 
30 namespace tflite {
31 namespace gpu {
32 namespace {
33 
IsConvEquivalentToFullyConnected(const Convolution2DAttributes & attr)34 bool IsConvEquivalentToFullyConnected(const Convolution2DAttributes& attr) {
35   return attr.weights.shape.w == 1 &&           //
36          attr.weights.shape.h == 1 &&           //
37          attr.strides == HW(1, 1) &&            //
38          attr.dilations == HW(1, 1) &&          //
39          attr.padding.prepended == HW(0, 0) &&  //
40          attr.padding.appended == HW(0, 0);
41 }
42 
43 class MakeFullyConnectedFromConvolution : public NodeTransformation {
44  public:
ApplyToNode(Node * node,GraphFloat32 * graph)45   TransformResult ApplyToNode(Node* node, GraphFloat32* graph) final {
46     if (node->operation.type != ToString(OperationType::CONVOLUTION_2D)) {
47       return {TransformStatus::SKIPPED, ""};
48     }
49     auto inputs = graph->FindInputs(node->id);
50     if (inputs.size() != 1) {
51       return {TransformStatus::SKIPPED, ""};
52     }
53 
54     const auto& input_shape = inputs[0]->tensor.shape;
55     if (input_shape.w != 1 || input_shape.h != 1) {
56       return {TransformStatus::SKIPPED, ""};
57     }
58 
59     const auto& conv_attr = absl::any_cast<const Convolution2DAttributes&>(
60         node->operation.attributes);
61     if (!IsConvEquivalentToFullyConnected(conv_attr)) {
62       return {TransformStatus::SKIPPED, ""};
63     }
64 
65     FullyConnectedAttributes fc_attr;
66     fc_attr.weights = conv_attr.weights;
67     fc_attr.bias = conv_attr.bias;
68 
69     node->operation.attributes = fc_attr;
70     node->operation.type = ToString(OperationType::FULLY_CONNECTED);
71     return {TransformStatus::APPLIED,
72             "Replaced convolution with fully connected."};
73   }
74 };
75 
76 }  // namespace
77 
NewMakeFullyConnectedFromConvolution()78 std::unique_ptr<NodeTransformation> NewMakeFullyConnectedFromConvolution() {
79   return absl::make_unique<MakeFullyConnectedFromConvolution>();
80 }
81 
82 }  // namespace gpu
83 }  // namespace tflite
84