1 /* Copyright 2022 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_SIMD_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONV_METAL_SIMD_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 ConvolutionMetalSimd : public GPUOperation { 30 public: 31 ConvolutionMetalSimd() = default; GetPossibleKernelWorkGroups(TuningType tuning_type,const GpuInfo & gpu_info,const KernelInfo & kernel_info,std::vector<int3> * work_groups)32 void GetPossibleKernelWorkGroups( 33 TuningType tuning_type, const GpuInfo& gpu_info, 34 const KernelInfo& kernel_info, 35 std::vector<int3>* work_groups) const override { 36 work_groups->push_back(work_group_size_); 37 } 38 int3 GetGridSize() const override; 39 40 // Move only 41 ConvolutionMetalSimd(ConvolutionMetalSimd&& kernel) = default; 42 ConvolutionMetalSimd& operator=(ConvolutionMetalSimd&& kernel) = default; 43 ConvolutionMetalSimd(const ConvolutionMetalSimd&) = delete; 44 ConvolutionMetalSimd& operator=(const ConvolutionMetalSimd&) = delete; 45 GetWeightsDescription()46 WeightsDescription GetWeightsDescription() const { 47 WeightsDescription desc; 48 desc.type = DeduceDataTypeFromPrecision(definition_.precision); 49 desc.layout = WeightsLayout::kOSpatialIOGroupO4I4; 50 desc.output_group_size = 4; 51 return desc; 52 } 53 54 struct ConvParams { 55 int3 work_group_size; 56 int3 work_group_launch_order; 57 bool linear_spatial; // spatial dimensions are Width/Height/Depth 58 int slices_per_thread; 59 bool x_kernel_is_1 = true; 60 bool y_kernel_is_1 = true; 61 bool z_kernel_is_1 = true; 62 63 // must be 32 * k GetSpatialThreadsCountConvParams64 int GetSpatialThreadsCount() const { 65 if (linear_spatial) { 66 return work_group_size.x; 67 } else { 68 return work_group_size.x * work_group_size.y; 69 } 70 } 71 GetX4SlicesCountConvParams72 int GetX4SlicesCount() const { 73 if (linear_spatial) { 74 return work_group_size.y; 75 } else { 76 return work_group_size.z; 77 } 78 } 79 }; 80 81 ConvParams params_; 82 83 private: ConvolutionMetalSimd(const OperationDef & definition)84 explicit ConvolutionMetalSimd(const OperationDef& definition) 85 : GPUOperation(definition) {} 86 friend ConvolutionMetalSimd CreateConvolutionMetalSimd( 87 const OperationDef& definition, const BHWC& dst_shape, 88 const Convolution2DAttributes& attr, const GpuInfo& gpu_info); 89 }; 90 91 ConvolutionMetalSimd CreateConvolutionMetalSimd( 92 const OperationDef& definition, const BHWC& dst_shape, 93 const Convolution2DAttributes& attr, const GpuInfo& gpu_info); 94 95 bool IsConvolutionMetalSimdSupported(const GpuInfo& gpu_info, 96 const OperationDef& definition, 97 const Convolution2DAttributes& attr); 98 99 bool IsGoodTaskSizeForAppleConvSimd(const BHWC& dst_shape, 100 const GpuInfo& gpu_info); 101 102 } // namespace gpu 103 } // namespace tflite 104 105 #endif // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONV_METAL_SIMD_H_ 106