• 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 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONV_METAL_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONV_METAL_H_
18 
19 #include <vector>
20 
21 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h"
22 #include "tensorflow/lite/delegates/gpu/common/operations.h"
23 #include "tensorflow/lite/delegates/gpu/common/task/gpu_operation.h"
24 #include "tensorflow/lite/delegates/gpu/common/task/weights_layout.h"
25 
26 namespace tflite {
27 namespace gpu {
28 
29 class ConvolutionMetal : public GPUOperation {
30  public:
31   enum class WeightsUploadType {
32     PRIVATE_MEM_SIMD8_BROADCAST,
33     PRIVATE_MEM_SIMD16_BROADCAST,
34     PRIVATE_MEM_SIMD32_BROADCAST,
35     LOCAL_MEM_BY_THREADS,
36     GLOBAL_MEM,
37     CONSTANT_MEM,
38   };
39 
40   struct ConvParams {
41     int3 block_size;
42     int3 work_group_size;
43     int3 work_group_launch_order;
44     int src_depth_loop_size;
45     bool need_src_loop = true;
46     bool need_dst_loop = true;
47     bool linear_wh;
48     bool linear_whs;
49     WeightsUploadType weights_upload_type;
50     WeightsLayout weights_layout;
51     bool different_weights_for_height = false;
52     bool x_kernel_is_1;
53     bool y_kernel_is_1;
54   };
55 
56   ConvolutionMetal() = default;
GetPossibleKernelWorkGroups(TuningType tuning_type,const GpuInfo & gpu_info,const KernelInfo & kernel_info,std::vector<int3> * work_groups)57   void GetPossibleKernelWorkGroups(
58       TuningType tuning_type, const GpuInfo& gpu_info,
59       const KernelInfo& kernel_info,
60       std::vector<int3>* work_groups) const override {
61     work_groups->push_back(work_group_size_);
62   }
63   int3 GetGridSize() const override;
64   absl::Status BindArguments(ArgumentsBinder* args) override;
65 
66   // Move only
67   ConvolutionMetal(ConvolutionMetal&& kernel) = default;
68   ConvolutionMetal& operator=(ConvolutionMetal&& kernel) = default;
69   ConvolutionMetal(const ConvolutionMetal&) = delete;
70   ConvolutionMetal& operator=(const ConvolutionMetal&) = delete;
71 
GetWeightsDescription()72   WeightsDescription GetWeightsDescription() const {
73     WeightsDescription desc;
74     desc.layout = params_.weights_layout;
75     desc.output_group_size = params_.block_size.z;
76     return desc;
77   }
78 
79  private:
ConvolutionMetal(const OperationDef & definition)80   explicit ConvolutionMetal(const OperationDef& definition)
81       : GPUOperation(definition) {}
82   friend ConvolutionMetal CreateConvolutionMetal(
83       const OperationDef& definition, const BHWC& dst_shape,
84       const Convolution2DAttributes& attr, const GpuInfo& gpu_info);
85 
86   friend ConvolutionMetal CreateConvolutionMetalWino4x4To6x6(
87       const OperationDef& definition, const BHWC& dst_shape,
88       const Convolution2DAttributes& attr, const GpuInfo& gpu_info);
89 
90   int2 padding_;
91   int2 dilation_;
92   ConvParams params_;
93 };
94 
95 ConvolutionMetal CreateConvolutionMetal(const OperationDef& definition,
96                                         const BHWC& dst_shape,
97                                         const Convolution2DAttributes& attr,
98                                         const GpuInfo& gpu_info);
99 
100 ConvolutionMetal CreateConvolutionMetalWino4x4To6x6(
101     const OperationDef& definition, const BHWC& dst_shape,
102     const Convolution2DAttributes& attr, const GpuInfo& gpu_info);
103 
104 bool IsConvolutionMetalSupported(const OperationDef& definition);
105 
106 }  // namespace gpu
107 }  // namespace tflite
108 
109 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONV_METAL_H_
110