fully_connected.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright 2022 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/micro/kernels/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/fully_connected.h"
  18. #include "tensorflow/lite/kernels/internal/reference/integer_ops/fully_connected.h"
  19. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  20. #include "tensorflow/lite/kernels/kernel_util.h"
  21. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  22. namespace tflite {
  23. namespace {
  24. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  25. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  26. return context->AllocatePersistentBuffer(context,
  27. sizeof(OpDataFullyConnected));
  28. }
  29. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  30. MicroContext* micro_context = GetMicroContext(context);
  31. TFLITE_DCHECK(node->user_data != nullptr);
  32. TFLITE_DCHECK(node->builtin_data != nullptr);
  33. auto* data = static_cast<OpDataFullyConnected*>(node->user_data);
  34. const auto params =
  35. static_cast<const TfLiteFullyConnectedParams*>(node->builtin_data);
  36. TfLiteTensor* input =
  37. micro_context->AllocateTempInputTensor(node, kFullyConnectedInputTensor);
  38. TF_LITE_ENSURE(context, input != nullptr);
  39. TfLiteTensor* filter = micro_context->AllocateTempInputTensor(
  40. node, kFullyConnectedWeightsTensor);
  41. TF_LITE_ENSURE(context, filter != nullptr);
  42. TfLiteTensor* bias =
  43. micro_context->AllocateTempInputTensor(node, kFullyConnectedBiasTensor);
  44. TfLiteTensor* output = micro_context->AllocateTempOutputTensor(
  45. node, kFullyConnectedOutputTensor);
  46. TF_LITE_ENSURE(context, output != nullptr);
  47. TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
  48. TF_LITE_ENSURE_OK(context, CalculateOpDataFullyConnected(
  49. context, params->activation, input->type,
  50. input, filter, bias, output, data));
  51. micro_context->DeallocateTempTfLiteTensor(input);
  52. micro_context->DeallocateTempTfLiteTensor(filter);
  53. if (bias != nullptr) {
  54. micro_context->DeallocateTempTfLiteTensor(bias);
  55. }
  56. micro_context->DeallocateTempTfLiteTensor(output);
  57. return kTfLiteOk;
  58. }
  59. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  60. TFLITE_DCHECK(node->builtin_data != nullptr);
  61. const auto* params =
  62. static_cast<const TfLiteFullyConnectedParams*>(node->builtin_data);
  63. const TfLiteEvalTensor* input =
  64. tflite::micro::GetEvalInput(context, node, kFullyConnectedInputTensor);
  65. const TfLiteEvalTensor* filter =
  66. tflite::micro::GetEvalInput(context, node, kFullyConnectedWeightsTensor);
  67. const TfLiteEvalTensor* bias =
  68. tflite::micro::GetEvalInput(context, node, kFullyConnectedBiasTensor);
  69. TfLiteEvalTensor* output =
  70. tflite::micro::GetEvalOutput(context, node, kFullyConnectedOutputTensor);
  71. TFLITE_DCHECK(node->user_data != nullptr);
  72. const auto& data =
  73. *(static_cast<const OpDataFullyConnected*>(node->user_data));
  74. // Checks in Prepare ensure input, output and filter types are all the same.
  75. switch (input->type) {
  76. case kTfLiteFloat32: {
  77. const float* bias_data =
  78. nullptr != bias ? tflite::micro::GetTensorData<float>(bias) : nullptr;
  79. tflite::reference_ops::FullyConnected(
  80. FullyConnectedParamsFloat(params->activation),
  81. tflite::micro::GetTensorShape(input),
  82. tflite::micro::GetTensorData<float>(input),
  83. tflite::micro::GetTensorShape(filter),
  84. tflite::micro::GetTensorData<float>(filter),
  85. tflite::micro::GetTensorShape(bias), bias_data,
  86. tflite::micro::GetTensorShape(output),
  87. tflite::micro::GetTensorData<float>(output));
  88. break;
  89. }
  90. case kTfLiteInt8: {
  91. const int32_t* bias_data =
  92. nullptr != bias ? tflite::micro::GetTensorData<int32_t>(bias)
  93. : nullptr;
  94. tflite::reference_integer_ops::FullyConnected(
  95. FullyConnectedParamsQuantized(data),
  96. tflite::micro::GetTensorShape(input),
  97. tflite::micro::GetTensorData<int8_t>(input),
  98. tflite::micro::GetTensorShape(filter),
  99. tflite::micro::GetTensorData<int8_t>(filter),
  100. tflite::micro::GetTensorShape(bias), bias_data,
  101. tflite::micro::GetTensorShape(output),
  102. tflite::micro::GetTensorData<int8_t>(output));
  103. break;
  104. }
  105. case kTfLiteInt16: {
  106. const int64_t* bias_data =
  107. nullptr != bias ? tflite::micro::GetTensorData<int64_t>(bias)
  108. : nullptr;
  109. tflite::reference_integer_ops::FullyConnected(
  110. FullyConnectedParamsQuantized(data),
  111. tflite::micro::GetTensorShape(input),
  112. tflite::micro::GetTensorData<int16_t>(input),
  113. tflite::micro::GetTensorShape(filter),
  114. tflite::micro::GetTensorData<int8_t>(filter),
  115. tflite::micro::GetTensorShape(bias), bias_data,
  116. tflite::micro::GetTensorShape(output),
  117. tflite::micro::GetTensorData<int16_t>(output));
  118. break;
  119. }
  120. default: {
  121. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  122. TfLiteTypeGetName(input->type), input->type);
  123. return kTfLiteError;
  124. }
  125. }
  126. return kTfLiteOk;
  127. }
  128. } // namespace
  129. TfLiteRegistration Register_FULLY_CONNECTED() {
  130. return tflite::micro::RegisterOp(Init, Prepare, Eval);
  131. }
  132. } // namespace tflite