1 /* Copyright 2020 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 #include "tensorflow/lite/c/builtin_op_data.h"
17 #include "tensorflow/lite/c/common.h"
18 #include "tensorflow/lite/kernels/internal/common.h"
19 #include "tensorflow/lite/kernels/internal/quantization_util.h"
20 #include "tensorflow/lite/kernels/internal/reference/fully_connected.h"
21 #include "tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h"
22 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
23 #include "tensorflow/lite/kernels/kernel_util.h"
24 #include "tensorflow/lite/micro/kernels/fully_connected.h"
25 #include "tensorflow/lite/micro/kernels/kernel_util.h"
26
27 namespace tflite {
28
29 const int kFullyConnectedInputTensor = 0;
30 const int kFullyConnectedWeightsTensor = 1;
31 const int kFullyConnectedBiasTensor = 2;
32 const int kFullyConnectedOutputTensor = 0;
33
FullyConnectedParamsQuantized(const OpDataFullyConnected & op_data)34 FullyConnectedParams FullyConnectedParamsQuantized(
35 const OpDataFullyConnected& op_data) {
36 FullyConnectedParams op_params;
37 op_params.input_offset = -op_data.input_zero_point;
38 op_params.weights_offset = -op_data.filter_zero_point;
39 op_params.output_offset = op_data.output_zero_point;
40 op_params.output_multiplier = op_data.output_multiplier;
41 op_params.output_shift = op_data.output_shift;
42 op_params.quantized_activation_min = op_data.output_activation_min;
43 op_params.quantized_activation_max = op_data.output_activation_max;
44 return op_params;
45 }
46
FullyConnectedParamsFloat(TfLiteFusedActivation activation)47 FullyConnectedParams FullyConnectedParamsFloat(
48 TfLiteFusedActivation activation) {
49 FullyConnectedParams op_params;
50 CalculateActivationRange(activation, &op_params.float_activation_min,
51 &op_params.float_activation_max);
52 return op_params;
53 }
54
CalculateOpDataFullyConnected(TfLiteContext * context,TfLiteFusedActivation activation,TfLiteType data_type,const TfLiteTensor * input,const TfLiteTensor * filter,const TfLiteTensor * bias,TfLiteTensor * output,OpDataFullyConnected * data)55 TfLiteStatus CalculateOpDataFullyConnected(
56 TfLiteContext* context, TfLiteFusedActivation activation,
57 TfLiteType data_type, const TfLiteTensor* input, const TfLiteTensor* filter,
58 const TfLiteTensor* bias, TfLiteTensor* output,
59 OpDataFullyConnected* data) {
60 if (data_type != kTfLiteFloat32) {
61 double real_multiplier = 0.0;
62 TF_LITE_ENSURE_STATUS(GetQuantizedConvolutionMultipler(
63 context, input, filter, bias, output, &real_multiplier));
64 QuantizeMultiplier(real_multiplier, &data->output_multiplier,
65 &data->output_shift);
66
67 data->input_zero_point = input->params.zero_point;
68 data->filter_zero_point = filter->params.zero_point;
69 data->output_zero_point = output->params.zero_point;
70
71 return CalculateActivationRangeQuantized(context, activation, output,
72 &data->output_activation_min,
73 &data->output_activation_max);
74 }
75 return kTfLiteOk;
76 }
77
78 } // namespace tflite
79