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