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