xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/transformations/merge_densify.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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/merge_densify.h"
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "absl/types/any.h"
23 #include "tensorflow/lite/delegates/gpu/common/data_type.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 
34 class MergeDensify : public NodeTransformation {
35  public:
ApplyToNode(Node * node,GraphFloat32 * graph)36   TransformResult ApplyToNode(Node* node, GraphFloat32* graph) final {
37     // Only CONV_2D & DEPTHWISE_CONV_2D.
38     const std::string& node_type = node->operation.type;
39     if (node_type != ToString(OperationType::CONVOLUTION_2D) &&
40         node_type != ToString(OperationType::DEPTHWISE_CONVOLUTION)) {
41       return {TransformStatus::SKIPPED, ""};
42     }
43 
44     // Only with a runtime weights.
45     const std::vector<Value*> inputs = graph->FindInputs(node->id);
46     if (inputs.size() != 2) return {TransformStatus::SKIPPED, ""};
47 
48     const Node* dequantize_or_densify = graph->FindProducer(inputs[1]->id);
49     if (!dequantize_or_densify ||
50         (dequantize_or_densify->operation.type !=
51              ToString(OperationType::DENSIFY) &&
52          dequantize_or_densify->operation.type !=
53              ToString(OperationType::QUANTIZE_AND_DEQUANTIZE))) {
54       return {TransformStatus::SKIPPED, ""};
55     }
56     const Node* dequantize_node;
57     const Node* densify_node;
58     if (dequantize_or_densify->operation.type ==
59         ToString(OperationType::QUANTIZE_AND_DEQUANTIZE)) {
60       dequantize_node = dequantize_or_densify;
61       densify_node =
62           graph->FindProducer(graph->FindInputs(dequantize_node->id)[0]->id);
63       if (!densify_node ||
64           densify_node->operation.type != ToString(OperationType::DENSIFY)) {
65         return {TransformStatus::SKIPPED, ""};
66       }
67     } else {
68       dequantize_node = nullptr;
69       densify_node = dequantize_or_densify;
70     }
71 
72     // Create a copy of the const tensor with a cast from BHWC to OHWI.
73     const Tensor<BHWC, DataType::FLOAT32>& src =
74         absl::any_cast<DensifyAttributes>(&densify_node->operation.attributes)
75             ->tensor;
76     Tensor<OHWI, DataType::FLOAT32> dst;
77     dst.id = src.id;
78     dst.shape = OHWI(src.shape.b, src.shape.h, src.shape.w, src.shape.c);
79     dst.data = src.data;
80 
81     // Remove DEQUANTIZE.
82     if (dequantize_node) {
83       const auto status = RemovePrecedingNode(graph, dequantize_node, node);
84       if (!status.ok()) return {TransformStatus::INVALID, status.ToString()};
85     }
86 
87     // Remove DENSIFY.
88     const auto status = RemovePrecedingNode(graph, densify_node, node);
89     if (!status.ok()) return {TransformStatus::INVALID, status.ToString()};
90 
91     // Update CONV_2D / DEPTHWISE_CONV_2D weights.
92     if (node->operation.type == ToString(OperationType::CONVOLUTION_2D)) {
93       absl::any_cast<Convolution2DAttributes>(&node->operation.attributes)
94           ->weights = std::move(dst);
95     } else {
96       absl::any_cast<DepthwiseConvolution2DAttributes>(
97           &node->operation.attributes)
98           ->weights = std::move(dst);
99     }
100     return {TransformStatus::APPLIED, ""};
101   }
102 };
103 
104 }  // namespace
105 
NewMergeDensify()106 std::unique_ptr<NodeTransformation> NewMergeDensify() {
107   return absl::make_unique<MergeDensify>();
108 }
109 
110 }  // namespace gpu
111 }  // namespace tflite
112