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_CONVOLUTION_TRANSPOSED_4X4_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONVOLUTION_TRANSPOSED_4X4_H_ 18 19 #include <string> 20 #include <vector> 21 22 #include "tensorflow/lite/delegates/gpu/common/data_type.h" 23 #include "tensorflow/lite/delegates/gpu/common/operations.h" 24 #include "tensorflow/lite/delegates/gpu/common/shape.h" 25 #include "tensorflow/lite/delegates/gpu/common/status.h" 26 #include "tensorflow/lite/delegates/gpu/common/task/buffer_desc.h" 27 #include "tensorflow/lite/delegates/gpu/common/task/gpu_operation.h" 28 #include "tensorflow/lite/delegates/gpu/common/task/tensor_desc.h" 29 #include "tensorflow/lite/delegates/gpu/common/task/weights_conversion.h" 30 #include "tensorflow/lite/delegates/gpu/common/task/weights_layout.h" 31 #include "tensorflow/lite/delegates/gpu/common/tensor.h" 32 #include "tensorflow/lite/delegates/gpu/common/types.h" 33 34 namespace tflite { 35 namespace gpu { 36 37 class ConvolutionTransposed4x4 : public GPUOperation { 38 public: 39 ConvolutionTransposed4x4() = default; GetPossibleKernelWorkGroups(TuningType tuning_type,const GpuInfo & gpu_info,const KernelInfo & kernel_info,std::vector<int3> * work_groups)40 void GetPossibleKernelWorkGroups( 41 TuningType tuning_type, const GpuInfo& gpu_info, 42 const KernelInfo& kernel_info, 43 std::vector<int3>* work_groups) const override { 44 work_groups->push_back(work_group_size_); 45 } 46 absl::Status BindArguments(ArgumentsBinder* args) override; 47 int3 GetGridSize() const override; 48 49 // Move only 50 ConvolutionTransposed4x4(ConvolutionTransposed4x4&& operation) = default; 51 ConvolutionTransposed4x4& operator=(ConvolutionTransposed4x4&& operation) = 52 default; 53 ConvolutionTransposed4x4(const ConvolutionTransposed4x4&) = delete; 54 ConvolutionTransposed4x4& operator=(const ConvolutionTransposed4x4&) = delete; 55 GetWeightsDescription()56 WeightsDescription GetWeightsDescription() const { 57 WeightsDescription desc; 58 desc.type = DeduceDataTypeFromPrecision(definition_.precision); 59 desc.layout = weights_layout_; 60 desc.spatial_remap = GetSpatialWeightsRemap(); 61 return desc; 62 } 63 64 enum class WeightsUploadType { 65 LOCAL_MEM_ASYNC, 66 LOCAL_MEM_BY_THREADS, 67 GLOBAL_MEM, 68 CONSTANT_MEM, 69 }; 70 71 private: 72 ConvolutionTransposed4x4(const OperationDef& definition, 73 const GpuInfo& gpu_info); 74 75 friend ConvolutionTransposed4x4 CreateConvolutionTransposed4x4( 76 const GpuInfo& gpu_info, const OperationDef& definition, 77 const ConvolutionTransposedAttributes& attr); 78 friend ConvolutionTransposed4x4 CreateConvolutionTransposed4x4DynamicWeights( 79 const GpuInfo& gpu_info, const OperationDef& definition, 80 const ConvolutionTransposedAttributes& attr); 81 82 void UploadWeights( 83 const tflite::gpu::Tensor<OHWI, DataType::FLOAT32>& weights, 84 WeightsUploadType weights_upload_type); 85 86 std::vector<int> GetSpatialWeightsRemap() const; 87 88 std::string GenerateConvolutionTransposedCode( 89 const GpuInfo& gpu_info, const OperationDef& op_def, 90 WeightsUploadType weights_upload_type); 91 92 WeightsLayout weights_layout_; 93 }; 94 95 bool IsConvolutionTransposed4x4Supported( 96 const OperationDef& definition, 97 const ConvolutionTransposedAttributes& attr); 98 99 ConvolutionTransposed4x4 CreateConvolutionTransposed4x4( 100 const GpuInfo& gpu_info, const OperationDef& definition, 101 const ConvolutionTransposedAttributes& attr); 102 103 ConvolutionTransposed4x4 CreateConvolutionTransposed4x4DynamicWeights( 104 const GpuInfo& gpu_info, const OperationDef& definition, 105 const ConvolutionTransposedAttributes& attr); 106 107 } // namespace gpu 108 } // namespace tflite 109 110 #endif // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONVOLUTION_TRANSPOSED_4X4_H_ 111