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 <memory>
17 #include <utility>
18 
19 #include "absl/memory/memory.h"
20 #include "tensorflow/lite/delegates/gpu/common/task/tensor_desc.h"
21 #include "tensorflow/lite/delegates/gpu/common/tasks/convolution_transposed.h"
22 #include "tensorflow/lite/delegates/gpu/common/tasks/convolution_transposed_3x3.h"
23 #include "tensorflow/lite/delegates/gpu/common/tasks/convolution_transposed_3x3_thin.h"
24 #include "tensorflow/lite/delegates/gpu/common/tasks/convolution_transposed_4x4.h"
25 #include "tensorflow/lite/delegates/gpu/common/tasks/convolution_transposed_thin.h"
26 
27 namespace tflite {
28 namespace gpu {
29 namespace {
30 
SelectConvolutionTransposedAdreno(const ConvolutionTransposedAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)31 std::unique_ptr<GPUOperation> SelectConvolutionTransposedAdreno(
32     const ConvolutionTransposedAttributes& attr, const GpuInfo& gpu_info,
33     const OperationDef& op_def) {
34   if (IsConvolutionTransposedThinSupported(attr)) {
35     ConvolutionTransposedThin conv =
36         CreateConvolutionTransposedThin(gpu_info, op_def, attr);
37     return std::make_unique<ConvolutionTransposedThin>(std::move(conv));
38   } else if (IsConvolutionTransposed3x3ThinSupported(attr)) {
39     ConvolutionTransposed3x3Thin conv =
40         CreateConvolutionTransposed3x3Thin(gpu_info, op_def, attr);
41     return std::make_unique<ConvolutionTransposed3x3Thin>(std::move(conv));
42   } else {
43     ConvolutionTransposed conv =
44         CreateConvolutionTransposed(gpu_info, op_def, attr);
45     return std::make_unique<ConvolutionTransposed>(std::move(conv));
46   }
47 }
48 
SelectConvolutionTransposedPowerVR(const ConvolutionTransposedAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)49 std::unique_ptr<GPUOperation> SelectConvolutionTransposedPowerVR(
50     const ConvolutionTransposedAttributes& attr, const GpuInfo& gpu_info,
51     const OperationDef& op_def) {
52   if (IsConvolutionTransposedThinSupported(attr)) {
53     ConvolutionTransposedThin conv =
54         CreateConvolutionTransposedThin(gpu_info, op_def, attr);
55     return std::make_unique<ConvolutionTransposedThin>(std::move(conv));
56   } else if (IsConvolutionTransposed3x3ThinSupported(attr)) {
57     ConvolutionTransposed3x3Thin conv =
58         CreateConvolutionTransposed3x3Thin(gpu_info, op_def, attr);
59     return std::make_unique<ConvolutionTransposed3x3Thin>(std::move(conv));
60   } else if (IsConvolutionTransposed3x3Supported(op_def, attr)) {
61     ConvolutionTransposed3x3 conv =
62         CreateConvolutionTransposed3x3(gpu_info, op_def, attr);
63     return std::make_unique<ConvolutionTransposed3x3>(std::move(conv));
64   } else if (IsConvolutionTransposed4x4Supported(op_def, attr)) {
65     ConvolutionTransposed4x4 conv =
66         CreateConvolutionTransposed4x4(gpu_info, op_def, attr);
67     return std::make_unique<ConvolutionTransposed4x4>(std::move(conv));
68   } else {
69     ConvolutionTransposed conv =
70         CreateConvolutionTransposed(gpu_info, op_def, attr);
71     return std::make_unique<ConvolutionTransposed>(std::move(conv));
72   }
73 }
74 
SelectConvolutionTransposedMali(const ConvolutionTransposedAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)75 std::unique_ptr<GPUOperation> SelectConvolutionTransposedMali(
76     const ConvolutionTransposedAttributes& attr, const GpuInfo& gpu_info,
77     const OperationDef& op_def) {
78   ConvolutionTransposed conv =
79       CreateConvolutionTransposed(gpu_info, op_def, attr);
80   return std::make_unique<ConvolutionTransposed>(std::move(conv));
81 }
82 }  // namespace
83 
SelectConvolutionTransposed(const ConvolutionTransposedAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)84 std::unique_ptr<GPUOperation> SelectConvolutionTransposed(
85     const ConvolutionTransposedAttributes& attr, const GpuInfo& gpu_info,
86     const OperationDef& op_def) {
87   if (gpu_info.IsAMD()) {
88     ConvolutionTransposed conv =
89         CreateConvolutionTransposed(gpu_info, op_def, attr);
90     return std::make_unique<ConvolutionTransposed>(std::move(conv));
91   } else if (gpu_info.IsAdreno()) {
92     return SelectConvolutionTransposedAdreno(attr, gpu_info, op_def);
93   } else if (gpu_info.IsPowerVR() || gpu_info.IsAMD() || gpu_info.IsNvidia() ||
94              gpu_info.IsIntel() || gpu_info.IsApple()) {
95     return SelectConvolutionTransposedPowerVR(attr, gpu_info, op_def);
96   } else if (gpu_info.IsMali()) {
97     return SelectConvolutionTransposedMali(attr, gpu_info, op_def);
98   } else {
99     return SelectConvolutionTransposedAdreno(attr, gpu_info, op_def);
100   }
101 }
102 
SelectConvolutionTransposedWithDynamicWeights(const ConvolutionTransposedAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def,WeightsDescription * weights_desc)103 std::unique_ptr<GPUOperation> SelectConvolutionTransposedWithDynamicWeights(
104     const ConvolutionTransposedAttributes& attr, const GpuInfo& gpu_info,
105     const OperationDef& op_def, WeightsDescription* weights_desc) {
106   if (gpu_info.IsAMD()) {
107     ConvolutionTransposed conv =
108         CreateConvolutionTransposedDynamicWeights(gpu_info, op_def, attr);
109     *weights_desc = conv.GetWeightsDescription();
110     return std::make_unique<ConvolutionTransposed>(std::move(conv));
111   } else if (gpu_info.IsAdreno()) {
112     if (IsConvolutionTransposed3x3ThinSupported(attr)) {
113       ConvolutionTransposed3x3Thin conv =
114           CreateConvolutionTransposed3x3ThinDynamicWeights(gpu_info, op_def,
115                                                            attr);
116       *weights_desc = conv.GetWeightsDescription();
117       return std::make_unique<ConvolutionTransposed3x3Thin>(std::move(conv));
118     } else {
119       ConvolutionTransposed conv =
120           CreateConvolutionTransposedDynamicWeights(gpu_info, op_def, attr);
121       *weights_desc = conv.GetWeightsDescription();
122       return std::make_unique<ConvolutionTransposed>(std::move(conv));
123     }
124   } else if (gpu_info.IsPowerVR() || gpu_info.IsAMD() || gpu_info.IsNvidia() ||
125              gpu_info.IsIntel()) {
126     if (IsConvolutionTransposed4x4Supported(op_def, attr)) {
127       ConvolutionTransposed4x4 conv =
128           CreateConvolutionTransposed4x4DynamicWeights(gpu_info, op_def, attr);
129       *weights_desc = conv.GetWeightsDescription();
130       return std::make_unique<ConvolutionTransposed4x4>(std::move(conv));
131     } else if (IsConvolutionTransposed3x3ThinSupported(attr)) {
132       ConvolutionTransposed3x3Thin conv =
133           CreateConvolutionTransposed3x3ThinDynamicWeights(gpu_info, op_def,
134                                                            attr);
135       *weights_desc = conv.GetWeightsDescription();
136       return std::make_unique<ConvolutionTransposed3x3Thin>(std::move(conv));
137     } else if (IsConvolutionTransposed3x3Supported(op_def, attr)) {
138       ConvolutionTransposed3x3 conv =
139           CreateConvolutionTransposed3x3DynamicWeights(gpu_info, op_def, attr);
140       *weights_desc = conv.GetWeightsDescription();
141       return std::make_unique<ConvolutionTransposed3x3>(std::move(conv));
142     } else {
143       ConvolutionTransposed conv =
144           CreateConvolutionTransposedDynamicWeights(gpu_info, op_def, attr);
145       *weights_desc = conv.GetWeightsDescription();
146       return std::make_unique<ConvolutionTransposed>(std::move(conv));
147     }
148   } else {
149     ConvolutionTransposed conv =
150         CreateConvolutionTransposedDynamicWeights(gpu_info, op_def, attr);
151     *weights_desc = conv.GetWeightsDescription();
152     return std::make_unique<ConvolutionTransposed>(std::move(conv));
153   }
154 }
155 
156 }  // namespace gpu
157 }  // namespace tflite
158