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