fully_connected.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "tensorflow/lite/kernels/internal/reference/fully_connected.h"
  13. #include "tensorflow/lite/c/builtin_op_data.h"
  14. #include "tensorflow/lite/c/common.h"
  15. #include "tensorflow/lite/kernels/internal/common.h"
  16. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  17. #include "tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h"
  18. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  19. #include "tensorflow/lite/kernels/kernel_util.h"
  20. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  21. namespace tflite {
  22. namespace ops {
  23. namespace micro {
  24. namespace fully_connected {
  25. namespace {
  26. struct OpData {
  27. // The scaling factor from input to output (aka the 'real multiplier') can
  28. // be represented as a fixed point multiplier plus a left shift.
  29. int32_t output_multiplier;
  30. int output_shift;
  31. // The range of the fused activation layer. For example for kNone and
  32. // uint8_t these would be 0 and 255.
  33. int32_t output_activation_min;
  34. int32_t output_activation_max;
  35. // The index of the temporary tensor where the quantized inputs are cached.
  36. int input_quantized_index;
  37. // Cached zero point values of tensors.
  38. int32_t input_zero_point;
  39. int32_t filter_zero_point;
  40. int32_t output_zero_point;
  41. };
  42. constexpr int kInputTensor = 0;
  43. constexpr int kWeightsTensor = 1;
  44. constexpr int kBiasTensor = 2;
  45. constexpr int kOutputTensor = 0;
  46. TfLiteStatus CalculateOpData(TfLiteContext* context,
  47. TfLiteFusedActivation activation,
  48. TfLiteType data_type, const TfLiteTensor* input,
  49. const TfLiteTensor* filter,
  50. const TfLiteTensor* bias, TfLiteTensor* output,
  51. OpData* data) {
  52. TfLiteStatus status = kTfLiteOk;
  53. if (data_type != kTfLiteFloat32) {
  54. double real_multiplier = 0.0;
  55. TF_LITE_ENSURE_STATUS(GetQuantizedConvolutionMultipler(
  56. context, input, filter, bias, output, &real_multiplier));
  57. int exponent;
  58. QuantizeMultiplier(real_multiplier, &data->output_multiplier, &exponent);
  59. data->output_shift = -exponent;
  60. TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized(
  61. context, activation, output, &data->output_activation_min,
  62. &data->output_activation_max));
  63. data->input_zero_point = input->params.zero_point;
  64. data->filter_zero_point = filter->params.zero_point;
  65. data->output_zero_point = output->params.zero_point;
  66. }
  67. return status;
  68. }
  69. } // namespace
  70. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  71. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  72. return context->AllocatePersistentBuffer(context, sizeof(OpData));
  73. }
  74. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  75. TFLITE_DCHECK(node->user_data != nullptr);
  76. TFLITE_DCHECK(node->builtin_data != nullptr);
  77. OpData* data = static_cast<OpData*>(node->user_data);
  78. const auto params =
  79. static_cast<const TfLiteFullyConnectedParams*>(node->builtin_data);
  80. const TfLiteTensor* input = GetInput(context, node, kInputTensor);
  81. const TfLiteTensor* filter = GetInput(context, node, kWeightsTensor);
  82. const TfLiteTensor* bias = GetOptionalInputTensor(context, node, kBiasTensor);
  83. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  84. TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
  85. TF_LITE_ENSURE_MSG(context, input->type == filter->type,
  86. "Hybrid models are not supported on TFLite Micro.");
  87. return CalculateOpData(context, params->activation, input->type, input,
  88. filter, bias, output, data);
  89. }
  90. TfLiteStatus EvalQuantizedInt8(TfLiteContext* context, TfLiteNode* node,
  91. const OpData& data,
  92. const TfLiteEvalTensor* input,
  93. const TfLiteEvalTensor* filter,
  94. const TfLiteEvalTensor* bias,
  95. TfLiteEvalTensor* output) {
  96. tflite::FullyConnectedParams op_params;
  97. op_params.input_offset = -data.input_zero_point;
  98. op_params.weights_offset = -data.filter_zero_point;
  99. op_params.output_offset = data.output_zero_point;
  100. op_params.output_multiplier = data.output_multiplier;
  101. // TODO(b/138810107): Figure out whether output shift should be inverted
  102. op_params.output_shift = -data.output_shift;
  103. op_params.quantized_activation_min = data.output_activation_min;
  104. op_params.quantized_activation_max = data.output_activation_max;
  105. reference_integer_ops::FullyConnected(
  106. op_params, tflite::micro::GetTensorShape(input),
  107. tflite::micro::GetTensorData<int8_t>(input),
  108. tflite::micro::GetTensorShape(filter),
  109. tflite::micro::GetTensorData<int8_t>(filter),
  110. tflite::micro::GetTensorShape(bias),
  111. tflite::micro::GetTensorData<int32_t>(bias),
  112. tflite::micro::GetTensorShape(output),
  113. tflite::micro::GetTensorData<int8_t>(output));
  114. return kTfLiteOk;
  115. }
  116. TfLiteStatus EvalQuantized(TfLiteContext* context, TfLiteNode* node,
  117. const OpData& data, const TfLiteEvalTensor* input,
  118. const TfLiteEvalTensor* filter,
  119. const TfLiteEvalTensor* bias,
  120. TfLiteEvalTensor* output) {
  121. const int32_t input_offset = -data.input_zero_point;
  122. const int32_t filter_offset = -data.filter_zero_point;
  123. const int32_t output_offset = data.output_zero_point;
  124. tflite::FullyConnectedParams op_params;
  125. op_params.input_offset = input_offset;
  126. op_params.weights_offset = filter_offset;
  127. op_params.output_offset = output_offset;
  128. op_params.output_multiplier = data.output_multiplier;
  129. // Legacy ops used mixed left and right shifts. Now all are +ve-means-left.
  130. op_params.output_shift = -data.output_shift;
  131. op_params.quantized_activation_min = data.output_activation_min;
  132. op_params.quantized_activation_max = data.output_activation_max;
  133. #define TF_LITE_FULLY_CONNECTED(output_data_type) \
  134. reference_ops::FullyConnected( \
  135. op_params, tflite::micro::GetTensorShape(input), \
  136. tflite::micro::GetTensorData<uint8_t>(input), \
  137. tflite::micro::GetTensorShape(filter), \
  138. tflite::micro::GetTensorData<uint8_t>(filter), \
  139. tflite::micro::GetTensorShape(bias), \
  140. tflite::micro::GetTensorData<int32_t>(bias), \
  141. tflite::micro::GetTensorShape(output), \
  142. tflite::micro::GetTensorData<output_data_type>(output))
  143. switch (output->type) {
  144. case kTfLiteUInt8:
  145. TF_LITE_FULLY_CONNECTED(uint8_t);
  146. break;
  147. case kTfLiteInt16:
  148. TF_LITE_FULLY_CONNECTED(int16_t);
  149. break;
  150. default:
  151. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  152. TfLiteTypeGetName(output->type), output->type);
  153. return kTfLiteError;
  154. }
  155. return kTfLiteOk;
  156. }
  157. TfLiteStatus EvalFloat(TfLiteContext* context, TfLiteNode* node,
  158. TfLiteFusedActivation activation,
  159. const TfLiteEvalTensor* input,
  160. const TfLiteEvalTensor* filter,
  161. const TfLiteEvalTensor* bias, TfLiteEvalTensor* output) {
  162. float output_activation_min, output_activation_max;
  163. CalculateActivationRange(activation, &output_activation_min,
  164. &output_activation_max);
  165. tflite::FullyConnectedParams op_params;
  166. op_params.float_activation_min = output_activation_min;
  167. op_params.float_activation_max = output_activation_max;
  168. tflite::reference_ops::FullyConnected(
  169. op_params, tflite::micro::GetTensorShape(input),
  170. tflite::micro::GetTensorData<float>(input),
  171. tflite::micro::GetTensorShape(filter),
  172. tflite::micro::GetTensorData<float>(filter),
  173. tflite::micro::GetTensorShape(bias),
  174. tflite::micro::GetTensorData<float>(bias),
  175. tflite::micro::GetTensorShape(output),
  176. tflite::micro::GetTensorData<float>(output));
  177. return kTfLiteOk;
  178. }
  179. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  180. TFLITE_DCHECK(node->builtin_data != nullptr);
  181. const auto* params =
  182. static_cast<const TfLiteFullyConnectedParams*>(node->builtin_data);
  183. const TfLiteEvalTensor* input =
  184. tflite::micro::GetEvalInput(context, node, kInputTensor);
  185. const TfLiteEvalTensor* filter =
  186. tflite::micro::GetEvalInput(context, node, kWeightsTensor);
  187. const TfLiteEvalTensor* bias =
  188. tflite::micro::GetEvalInput(context, node, kBiasTensor);
  189. TfLiteEvalTensor* output =
  190. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  191. TFLITE_DCHECK(node->user_data != nullptr);
  192. const OpData& data = *(static_cast<const OpData*>(node->user_data));
  193. // Checks in Prepare ensure input, output and filter types are all the same.
  194. switch (input->type) {
  195. case kTfLiteFloat32:
  196. return EvalFloat(context, node, params->activation, input, filter, bias,
  197. output);
  198. case kTfLiteInt8:
  199. return EvalQuantizedInt8(context, node, data, input, filter, bias,
  200. output);
  201. case kTfLiteUInt8:
  202. return EvalQuantized(context, node, data, input, filter, bias, output);
  203. default:
  204. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  205. TfLiteTypeGetName(input->type), input->type);
  206. return kTfLiteError;
  207. }
  208. return kTfLiteOk;
  209. }
  210. } // namespace fully_connected
  211. TfLiteRegistration Register_FULLY_CONNECTED() {
  212. return {/*init=*/fully_connected::Init,
  213. /*free=*/nullptr,
  214. /*prepare=*/fully_connected::Prepare,
  215. /*invoke=*/fully_connected::Eval,
  216. /*profiling_string=*/nullptr,
  217. /*builtin_code=*/0,
  218. /*custom_name=*/nullptr,
  219. /*version=*/0};
  220. }
  221. } // namespace micro
  222. } // namespace ops
  223. } // namespace tflite