conv.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright 2019 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/conv.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/conv.h"
  18. #include "tensorflow/lite/kernels/internal/reference/integer_ops/conv.h"
  19. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  20. #include "tensorflow/lite/kernels/kernel_util.h"
  21. #include "tensorflow/lite/kernels/padding.h"
  22. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  23. namespace tflite {
  24. namespace {
  25. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  26. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  27. return context->AllocatePersistentBuffer(context, sizeof(OpDataConv));
  28. }
  29. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  30. const TfLiteEvalTensor* input =
  31. tflite::micro::GetEvalInput(context, node, kConvInputTensor);
  32. const TfLiteEvalTensor* filter =
  33. tflite::micro::GetEvalInput(context, node, kConvWeightsTensor);
  34. const TfLiteEvalTensor* bias =
  35. (NumInputs(node) == 3)
  36. ? tflite::micro::GetEvalInput(context, node, kConvBiasTensor)
  37. : nullptr;
  38. TfLiteEvalTensor* output =
  39. tflite::micro::GetEvalOutput(context, node, kConvOutputTensor);
  40. TFLITE_DCHECK(node->builtin_data != nullptr);
  41. const auto& params =
  42. *(reinterpret_cast<TfLiteConvParams*>(node->builtin_data));
  43. TFLITE_DCHECK(node->user_data != nullptr);
  44. const auto& data = *(static_cast<const OpDataConv*>(node->user_data));
  45. TF_LITE_ENSURE_EQ(context, input->type, output->type);
  46. TF_LITE_ENSURE_MSG(context, input->type == filter->type,
  47. "Hybrid models are not supported on TFLite Micro.");
  48. switch (input->type) { // Already know in/out types are same.
  49. case kTfLiteFloat32: {
  50. tflite::reference_ops::Conv(
  51. ConvParamsFloat(params, data), tflite::micro::GetTensorShape(input),
  52. tflite::micro::GetTensorData<float>(input),
  53. tflite::micro::GetTensorShape(filter),
  54. tflite::micro::GetTensorData<float>(filter),
  55. tflite::micro::GetTensorShape(bias),
  56. tflite::micro::GetTensorData<float>(bias),
  57. tflite::micro::GetTensorShape(output),
  58. tflite::micro::GetTensorData<float>(output),
  59. tflite::micro::GetTensorShape(nullptr), nullptr);
  60. break;
  61. }
  62. case kTfLiteInt8: {
  63. reference_integer_ops::ConvPerChannel(
  64. ConvParamsQuantized(params, data), data.per_channel_output_multiplier,
  65. data.per_channel_output_shift, tflite::micro::GetTensorShape(input),
  66. tflite::micro::GetTensorData<int8_t>(input),
  67. tflite::micro::GetTensorShape(filter),
  68. tflite::micro::GetTensorData<int8_t>(filter),
  69. tflite::micro::GetTensorShape(bias),
  70. tflite::micro::GetTensorData<int32_t>(bias),
  71. tflite::micro::GetTensorShape(output),
  72. tflite::micro::GetTensorData<int8_t>(output));
  73. break;
  74. }
  75. default:
  76. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  77. TfLiteTypeGetName(input->type), input->type);
  78. return kTfLiteError;
  79. }
  80. return kTfLiteOk;
  81. }
  82. } // namespace
  83. TfLiteRegistration Register_CONV_2D() {
  84. return {/*init=*/Init,
  85. /*free=*/nullptr,
  86. /*prepare=*/ConvPrepare,
  87. /*invoke=*/Eval,
  88. /*profiling_string=*/nullptr,
  89. /*builtin_code=*/0,
  90. /*custom_name=*/nullptr,
  91. /*version=*/0};
  92. }
  93. } // namespace tflite