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 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_INTEGER_OPS_TRANSPOSE_CONV_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_INTEGER_OPS_TRANSPOSE_CONV_H_
17
18 #include "tensorflow/lite/kernels/internal/optimized/optimized_ops.h"
19
20 namespace tflite {
21 namespace optimized_integer_ops {
22
23 // TransposeConvV2 expect the weights in HWOI order.
TransposeConvV2(const ConvParams & params,const int32 * output_multiplier,const int32 * output_shift,const RuntimeShape & input_shape,const int8_t * input_data,const RuntimeShape & hwoi_ordered_filter_shape,const int8_t * hwoi_ordered_filter_data,const RuntimeShape & bias_shape,const int32 * bias_data,const RuntimeShape & output_shape,int8_t * output_data,const RuntimeShape & col2im_shape,int32_t * col2im_data,int32_t * scratch_data,CpuBackendContext * cpu_backend_context)24 inline void TransposeConvV2(
25 const ConvParams& params, const int32* output_multiplier,
26 const int32* output_shift, const RuntimeShape& input_shape,
27 const int8_t* input_data, const RuntimeShape& hwoi_ordered_filter_shape,
28 const int8_t* hwoi_ordered_filter_data, const RuntimeShape& bias_shape,
29 const int32* bias_data, const RuntimeShape& output_shape,
30 int8_t* output_data, const RuntimeShape& col2im_shape, int32_t* col2im_data,
31 int32_t* scratch_data, CpuBackendContext* cpu_backend_context) {
32 ruy::profiler::ScopeLabel label("TransposeConvV2/int8");
33 TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
34 TFLITE_DCHECK_EQ(hwoi_ordered_filter_shape.DimensionsCount(), 4);
35 TFLITE_DCHECK(col2im_data);
36 TFLITE_DCHECK(hwoi_ordered_filter_data);
37
38 const int batch_size = MatchingDim(input_shape, 0, output_shape, 0);
39 const int input_image_size = input_shape.Dims(1) * input_shape.Dims(2);
40 const int output_height = output_shape.Dims(1);
41 const int output_width = output_shape.Dims(2);
42 const int output_image_size = output_height * output_width;
43 const int input_depth =
44 MatchingDim(input_shape, 3, hwoi_ordered_filter_shape, 3);
45 const int output_depth =
46 MatchingDim(output_shape, 3, hwoi_ordered_filter_shape, 2);
47 const int input_offset = input_image_size * input_depth;
48 const int output_offset = output_image_size * output_depth;
49
50 const int filter_height = hwoi_ordered_filter_shape.Dims(0);
51 const int filter_width = hwoi_ordered_filter_shape.Dims(1);
52 const int padding_top = params.padding_values.height;
53 const int padding_bottom =
54 params.padding_values.height + params.padding_values.height_offset;
55 const int padding_left = params.padding_values.width;
56 const int padding_right =
57 params.padding_values.width + params.padding_values.width_offset;
58 const int stride_height = params.stride_height;
59 const int stride_width = params.stride_width;
60
61 const int hwoi_ordered_filter_total_size =
62 filter_height * filter_width * output_depth;
63
64 cpu_backend_gemm::MatrixParams<int8_t> lhs_params;
65 lhs_params.order = cpu_backend_gemm::Order::kRowMajor;
66 lhs_params.rows = hwoi_ordered_filter_total_size;
67 lhs_params.cols = input_depth;
68 // Since our weight is symmetric quantized, the zp will always be 0.
69 lhs_params.zero_point = 0;
70
71 int32_t* scratch_data_p = scratch_data;
72 std::fill_n(scratch_data, output_offset * batch_size, static_cast<int32>(0));
73 for (int i = 0; i < batch_size; ++i) {
74 cpu_backend_gemm::MatrixParams<int8_t> rhs_params;
75 rhs_params.order = cpu_backend_gemm::Order::kColMajor;
76 rhs_params.rows = input_depth;
77 rhs_params.cols = input_image_size;
78 rhs_params.zero_point = -params.input_offset;
79
80 cpu_backend_gemm::MatrixParams<int32_t> dst_params;
81 dst_params.order = cpu_backend_gemm::Order::kColMajor;
82 dst_params.rows = hwoi_ordered_filter_total_size;
83 dst_params.cols = input_image_size;
84
85 cpu_backend_gemm::GemmParams<int32_t, int32_t> gemm_params;
86 cpu_backend_gemm::Gemm(lhs_params, hwoi_ordered_filter_data, rhs_params,
87 input_data + input_offset * i, dst_params,
88 col2im_data, gemm_params, cpu_backend_context);
89
90 optimized_ops::Col2im(
91 col2im_data, output_depth, output_height, output_width, filter_height,
92 filter_width, padding_top, padding_left, padding_bottom, padding_right,
93 stride_height, stride_width, scratch_data_p);
94
95 scratch_data_p += output_offset;
96 }
97 scratch_data_p = scratch_data;
98 optimized_ops::BiasAdd(scratch_data_p, bias_data, batch_size, output_height,
99 output_width, output_depth);
100
101 const int32_t output_min = std::numeric_limits<int8_t>::min();
102 const int32_t output_max = std::numeric_limits<int8_t>::max();
103
104 optimized_ops::Quantize(output_multiplier, output_shift, output_depth,
105 output_shape.FlatSize(), params.output_offset,
106 output_min, output_max, scratch_data, output_data);
107 }
108
109 } // namespace optimized_integer_ops
110 } // namespace tflite
111
112 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_INTEGER_OPS_TRANSPOSE_CONV_H_
113