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/gpu_info.h"
18 #include "tensorflow/lite/delegates/gpu/common/tasks/depthwise_conv.h"
19 #include "tensorflow/lite/delegates/gpu/common/tasks/depthwise_conv_3x3.h"
20 #include "tensorflow/lite/delegates/gpu/common/tasks/depthwise_conv_3x3_stride_h2.h"
21
22 namespace tflite {
23 namespace gpu {
24 namespace {
25
SelectDWConvolutionAdreno(const DepthwiseConvolution2DAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)26 std::unique_ptr<GPUOperation> SelectDWConvolutionAdreno(
27 const DepthwiseConvolution2DAttributes& attr, const GpuInfo& gpu_info,
28 const OperationDef& op_def) {
29 if (IsDepthwiseConv3x3Supported(attr)) {
30 return absl::make_unique<DepthwiseConv3x3>(
31 CreateDepthwiseConv3x3(gpu_info, op_def, attr));
32 } else {
33 return absl::make_unique<GPUOperation>(
34 CreateDepthwiseConvolution2D(gpu_info, op_def, attr));
35 }
36 }
37
SelectDWConvolutionPowerVR(const DepthwiseConvolution2DAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)38 std::unique_ptr<GPUOperation> SelectDWConvolutionPowerVR(
39 const DepthwiseConvolution2DAttributes& attr, const GpuInfo& gpu_info,
40 const OperationDef& op_def) {
41 if (IsDepthwiseConv3x3Supported(attr)) {
42 return absl::make_unique<DepthwiseConv3x3>(
43 CreateDepthwiseConv3x3(gpu_info, op_def, attr));
44 } else {
45 return absl::make_unique<GPUOperation>(
46 CreateDepthwiseConvolution2D(gpu_info, op_def, attr));
47 }
48 }
49
SelectDWConvolutionMali(const DepthwiseConvolution2DAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)50 std::unique_ptr<GPUOperation> SelectDWConvolutionMali(
51 const DepthwiseConvolution2DAttributes& attr, const GpuInfo& gpu_info,
52 const OperationDef& op_def) {
53 const auto storage_type = op_def.src_tensors[0].storage_type;
54 bool buffer_type = storage_type == TensorStorageType::BUFFER ||
55 storage_type == TensorStorageType::IMAGE_BUFFER;
56 const MaliInfo mali_info = gpu_info.mali_info;
57 if (IsDepthwiseConv3x3Supported(attr) && !mali_info.IsMidgard() &&
58 !buffer_type && op_def.precision != CalculationsPrecision::F32) {
59 return absl::make_unique<DepthwiseConv3x3>(
60 CreateDepthwiseConv3x3(gpu_info, op_def, attr));
61 } else {
62 return absl::make_unique<GPUOperation>(
63 CreateDepthwiseConvolution2D(gpu_info, op_def, attr));
64 }
65 }
66
SelectDWConvolutionApple(const DepthwiseConvolution2DAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)67 std::unique_ptr<GPUOperation> SelectDWConvolutionApple(
68 const DepthwiseConvolution2DAttributes& attr, const GpuInfo& gpu_info,
69 const OperationDef& op_def) {
70 if (IsDepthwiseConv3x3Supported(attr)) {
71 return absl::make_unique<DepthwiseConv3x3>(
72 CreateDepthwiseConv3x3(gpu_info, op_def, attr));
73 } else if (IsDepthWiseConv3x3StrideH2Supported(attr)) {
74 return absl::make_unique<DepthWiseConv3x3StrideH2>(
75 CreateDepthWiseConv3x3StrideH2(op_def, attr, gpu_info));
76 } else {
77 return absl::make_unique<GPUOperation>(
78 CreateDepthwiseConvolution2D(gpu_info, op_def, attr));
79 }
80 }
81 } // namespace
82
SelectDWConvolution(const DepthwiseConvolution2DAttributes & attr,const GpuInfo & gpu_info,const OperationDef & op_def)83 std::unique_ptr<GPUOperation> SelectDWConvolution(
84 const DepthwiseConvolution2DAttributes& attr, const GpuInfo& gpu_info,
85 const OperationDef& op_def) {
86 if (gpu_info.IsAdreno()) {
87 return SelectDWConvolutionAdreno(attr, gpu_info, op_def);
88 } else if (gpu_info.IsPowerVR()) {
89 return SelectDWConvolutionPowerVR(attr, gpu_info, op_def);
90 } else if (gpu_info.IsMali()) {
91 return SelectDWConvolutionMali(attr, gpu_info, op_def);
92 } else if (gpu_info.IsApple()) {
93 return SelectDWConvolutionApple(attr, gpu_info, op_def);
94 } else {
95 return SelectDWConvolutionAdreno(attr, gpu_info, op_def);
96 }
97 }
98
99 } // namespace gpu
100 } // namespace tflite
101