xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/tasks/convolution_transposed_thin.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_THIN_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONVOLUTION_TRANSPOSED_THIN_H_
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
25 #include "tensorflow/lite/delegates/gpu/common/operations.h"
26 #include "tensorflow/lite/delegates/gpu/common/shape.h"
27 #include "tensorflow/lite/delegates/gpu/common/status.h"
28 #include "tensorflow/lite/delegates/gpu/common/task/buffer_desc.h"
29 #include "tensorflow/lite/delegates/gpu/common/task/gpu_operation.h"
30 #include "tensorflow/lite/delegates/gpu/common/task/tensor_desc.h"
31 #include "tensorflow/lite/delegates/gpu/common/task/texture2d_desc.h"
32 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
33 #include "tensorflow/lite/delegates/gpu/common/types.h"
34 
35 namespace tflite {
36 namespace gpu {
37 
38 class ConvolutionTransposedThin : public GPUOperation {
39  public:
40   ConvolutionTransposedThin() = default;
41   int3 GetGridSize() const override;
42 
43   // Move only
44   ConvolutionTransposedThin(ConvolutionTransposedThin&& operation);
45   ConvolutionTransposedThin& operator=(ConvolutionTransposedThin&& operation);
46   ConvolutionTransposedThin(const ConvolutionTransposedThin&) = delete;
47   ConvolutionTransposedThin& operator=(const ConvolutionTransposedThin&) =
48       delete;
49 
50  private:
51   friend ConvolutionTransposedThin CreateConvolutionTransposedThin(
52       const GpuInfo& gpu_info, const OperationDef& definition,
53       const ConvolutionTransposedAttributes& attr);
54   ConvolutionTransposedThin(const OperationDef& definition,
55                             const ConvolutionTransposedAttributes& attr,
56                             const GpuInfo& gpu_info);
57   template <DataType T>
58   void UploadData(const tflite::gpu::Tensor<OHWI, T>& weights,
59                   const tflite::gpu::Tensor<Linear, T>& biases);
60 
61   template <DataType S, typename T>
62   void RearrangeWeightsData(const tflite::gpu::Tensor<OHWI, S>& weights,
63                             absl::Span<T> dst);
64   std::string GenerateConvolutionTransposedCode(const OperationDef& op_def,
65                                                 int src_depth, int dst_channels,
66                                                 const int2& kernel_size);
67 };
68 
69 template <DataType T>
UploadData(const tflite::gpu::Tensor<OHWI,T> & weights,const tflite::gpu::Tensor<Linear,T> & biases)70 void ConvolutionTransposedThin::UploadData(
71     const tflite::gpu::Tensor<OHWI, T>& weights,
72     const tflite::gpu::Tensor<Linear, T>& biases) {
73   const int src_depth = DivideRoundUp(weights.shape.i, 4);
74   const int flt4_count =
75       weights.shape.w * weights.shape.h * src_depth * weights.shape.o;
76 
77   const bool f32_weights = definition_.precision == CalculationsPrecision::F32;
78   const int flt4_size = f32_weights ? sizeof(float4) : sizeof(half4);
79 
80   BufferDescriptor desc;
81   desc.element_type = f32_weights ? DataType::FLOAT32 : DataType::FLOAT16;
82   desc.element_size = 4;
83   desc.memory_type = MemoryType::CONSTANT;
84   desc.size = flt4_size * (flt4_count + 1);
85   desc.data.resize(desc.size);
86 
87   if (f32_weights) {
88     float4* gpu_data = reinterpret_cast<float4*>(desc.data.data());
89     RearrangeWeightsData(weights, absl::MakeSpan(gpu_data, flt4_count));
90     float4 bias_value(0.0f);
91     for (int i = 0; i < weights.shape.o; ++i) {
92       bias_value[i] = biases.data[i];
93     }
94     gpu_data[flt4_count] = bias_value;
95   } else {
96     half4* gpu_data = reinterpret_cast<half4*>(desc.data.data());
97     RearrangeWeightsData(weights, absl::MakeSpan(gpu_data, flt4_count));
98     half4 bias_value(0.0f);
99     for (int i = 0; i < weights.shape.o; ++i) {
100       bias_value[i] = biases.data[i];
101     }
102     gpu_data[flt4_count] = bias_value;
103   }
104 
105   args_.AddObject("weights",
106                   std::make_unique<BufferDescriptor>(std::move(desc)));
107 }
108 
109 template <DataType S, typename T>
RearrangeWeightsData(const tflite::gpu::Tensor<OHWI,S> & weights,absl::Span<T> dst)110 void ConvolutionTransposedThin::RearrangeWeightsData(
111     const tflite::gpu::Tensor<OHWI, S>& weights, absl::Span<T> dst) {
112   const int src_depth = DivideRoundUp(weights.shape.i, 4);
113   const int kernel_x = weights.shape.w;
114   const int kernel_y = weights.shape.h;
115 
116   int counter = 0;
117   for (int s = 0; s < src_depth; ++s) {
118     for (int y = 0; y < kernel_y; ++y) {
119       for (int x = 0; x < kernel_x; ++x) {
120         std::vector<T> filters(weights.shape.o);
121         for (int j = 0; j < weights.shape.o; ++j) {
122           for (int i = 0; i < 4; ++i) {
123             const int s_ch = s * 4 + i;
124             const int d_ch = j;
125             if (s_ch < weights.shape.i && d_ch < weights.shape.o) {
126               const int f_index = weights.shape.LinearIndex({d_ch, y, x, s_ch});
127               filters[j][i] = weights.data[f_index];
128             } else {
129               filters[j][i] = 0.0f;
130             }
131           }
132         }
133         for (int j = 0; j < weights.shape.o; ++j) {
134           dst[counter++] = filters[j];
135         }
136       }
137     }
138   }
139 }
140 
141 bool IsConvolutionTransposedThinSupported(
142     const ConvolutionTransposedAttributes& attr);
143 
144 ConvolutionTransposedThin CreateConvolutionTransposedThin(
145     const GpuInfo& gpu_info, const OperationDef& definition,
146     const ConvolutionTransposedAttributes& attr);
147 
148 }  // namespace gpu
149 }  // namespace tflite
150 
151 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASKS_CONVOLUTION_TRANSPOSED_THIN_H_
152