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_REFERENCE_INTEGER_OPS_FULLY_CONNECTED_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_FULLY_CONNECTED_H_
17
18 #include "tensorflow/lite/kernels/internal/common.h"
19
20 namespace tflite {
21 namespace reference_integer_ops {
22
FullyConnected(const FullyConnectedParams & params,const RuntimeShape & input_shape,const int8_t * input_data,const RuntimeShape & filter_shape,const int8_t * filter_data,const RuntimeShape & bias_shape,const int32_t * bias_data,const RuntimeShape & output_shape,int8_t * output_data)23 inline void FullyConnected(
24 const FullyConnectedParams& params, const RuntimeShape& input_shape,
25 const int8_t* input_data, const RuntimeShape& filter_shape,
26 const int8_t* filter_data, const RuntimeShape& bias_shape,
27 const int32_t* bias_data, const RuntimeShape& output_shape,
28 int8_t* output_data) {
29 const int32_t input_offset = params.input_offset;
30 const int32_t filter_offset = params.weights_offset;
31 const int32_t output_offset = params.output_offset;
32 const int32_t output_multiplier = params.output_multiplier;
33 const int output_shift = params.output_shift;
34 const int32_t output_activation_min = params.quantized_activation_min;
35 const int32_t output_activation_max = params.quantized_activation_max;
36 TFLITE_DCHECK_GE(filter_shape.DimensionsCount(), 2);
37 TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 2);
38
39 TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
40 const int filter_dim_count = filter_shape.DimensionsCount();
41 const int batches = output_shape.Dims(0);
42 const int output_depth = output_shape.Dims(1);
43 TFLITE_DCHECK_LE(output_depth, filter_shape.Dims(filter_dim_count - 2));
44 const int accum_depth = filter_shape.Dims(filter_dim_count - 1);
45 for (int b = 0; b < batches; ++b) {
46 for (int out_c = 0; out_c < output_depth; ++out_c) {
47 int32_t acc = 0;
48 for (int d = 0; d < accum_depth; ++d) {
49 int32_t input_val = input_data[b * accum_depth + d];
50 int32_t filter_val = filter_data[out_c * accum_depth + d];
51 acc += (filter_val + filter_offset) * (input_val + input_offset);
52 }
53 if (bias_data) {
54 acc += bias_data[out_c];
55 }
56 acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
57 acc += output_offset;
58 acc = std::max(acc, output_activation_min);
59 acc = std::min(acc, output_activation_max);
60 output_data[out_c + output_depth * b] = static_cast<int8_t>(acc);
61 }
62 }
63 }
64
FullyConnected(const FullyConnectedParams & params,const RuntimeShape & input_shape,const int16_t * input_data,const RuntimeShape & filter_shape,const int8_t * filter_data,const RuntimeShape & bias_shape,const int64_t * bias_data,const RuntimeShape & output_shape,int16_t * output_data)65 inline void FullyConnected(
66 const FullyConnectedParams& params, const RuntimeShape& input_shape,
67 const int16_t* input_data, const RuntimeShape& filter_shape,
68 const int8_t* filter_data, const RuntimeShape& bias_shape,
69 const int64_t* bias_data, const RuntimeShape& output_shape,
70 int16_t* output_data) {
71 const int32_t filter_offset = params.weights_offset;
72 const int32_t output_multiplier = params.output_multiplier;
73 const int output_shift = params.output_shift;
74 const int32_t output_activation_min = params.quantized_activation_min;
75 const int32_t output_activation_max = params.quantized_activation_max;
76 TFLITE_DCHECK_GE(filter_shape.DimensionsCount(), 2);
77 TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 2);
78
79 TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
80 const int filter_dim_count = filter_shape.DimensionsCount();
81 const int batches = output_shape.Dims(0);
82 const int output_depth = output_shape.Dims(1);
83 TFLITE_DCHECK_LE(output_depth, filter_shape.Dims(filter_dim_count - 2));
84 const int accum_depth = filter_shape.Dims(filter_dim_count - 1);
85 for (int b = 0; b < batches; ++b) {
86 for (int out_c = 0; out_c < output_depth; ++out_c) {
87 int64_t acc = 0;
88 for (int d = 0; d < accum_depth; ++d) {
89 int32_t input_val = input_data[b * accum_depth + d];
90 int32_t filter_val = filter_data[out_c * accum_depth + d];
91 acc += (filter_val + filter_offset) * input_val;
92 }
93 if (bias_data) {
94 acc += bias_data[out_c];
95 }
96 int32_t acc_scaled =
97 MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
98 acc_scaled = std::max(acc_scaled, output_activation_min);
99 acc_scaled = std::min(acc_scaled, output_activation_max);
100 output_data[out_c + output_depth * b] = static_cast<int16_t>(acc_scaled);
101 }
102 }
103 }
104
105 } // namespace reference_integer_ops
106 } // namespace tflite
107
108 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_FULLY_CONNECTED_H_
109