svdf.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright 2020 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/svdf.h"
  13. #include <math.h>
  14. #include "tensorflow/lite/c/builtin_op_data.h"
  15. #include "tensorflow/lite/c/common.h"
  16. #include "tensorflow/lite/kernels/internal/common.h"
  17. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  18. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  19. #include "tensorflow/lite/kernels/kernel_util.h"
  20. #include "tensorflow/lite/kernels/op_macros.h"
  21. #include "tensorflow/lite/micro/kernels/activation_utils.h"
  22. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  23. #include "tensorflow/lite/micro/micro_utils.h"
  24. namespace tflite {
  25. namespace {
  26. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  27. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  28. return context->AllocatePersistentBuffer(context, sizeof(OpData));
  29. }
  30. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  31. auto* params = reinterpret_cast<TfLiteSVDFParams*>(node->builtin_data);
  32. TFLITE_DCHECK(node->user_data != nullptr);
  33. const OpData& data = *(static_cast<const OpData*>(node->user_data));
  34. const TfLiteEvalTensor* input =
  35. tflite::micro::GetEvalInput(context, node, kSvdfInputTensor);
  36. const TfLiteEvalTensor* weights_feature =
  37. tflite::micro::GetEvalInput(context, node, kSvdfWeightsFeatureTensor);
  38. const TfLiteEvalTensor* weights_time =
  39. tflite::micro::GetEvalInput(context, node, kSvdfWeightsTimeTensor);
  40. const TfLiteEvalTensor* bias =
  41. (NumInputs(node) == 5)
  42. ? tflite::micro::GetEvalInput(context, node, kSvdfBiasTensor)
  43. : nullptr;
  44. TfLiteEvalTensor* activation_state = tflite::micro::GetMutableEvalInput(
  45. context, node, kSvdfInputActivationStateTensor);
  46. TfLiteEvalTensor* output =
  47. tflite::micro::GetEvalOutput(context, node, kSvdfOutputTensor);
  48. switch (weights_feature->type) {
  49. case kTfLiteFloat32: {
  50. EvalFloatSvdfReference(
  51. context, node, input, weights_feature, weights_time, bias, params,
  52. data.scratch_tensor_index, activation_state, output);
  53. return kTfLiteOk;
  54. break;
  55. }
  56. case kTfLiteInt8: {
  57. EvalIntegerSvdfReference(context, node, input, weights_feature,
  58. weights_time, bias, params, activation_state,
  59. output, data);
  60. return kTfLiteOk;
  61. break;
  62. }
  63. default:
  64. TF_LITE_KERNEL_LOG(context, "Type %s not currently supported.",
  65. TfLiteTypeGetName(weights_feature->type));
  66. return kTfLiteError;
  67. }
  68. return kTfLiteOk;
  69. }
  70. } // namespace
  71. TfLiteRegistration Register_SVDF() {
  72. return {/*init=*/Init,
  73. /*free=*/nullptr,
  74. /*prepare=*/PrepareSvdf,
  75. /*invoke=*/Eval,
  76. /*profiling_string=*/nullptr,
  77. /*builtin_code=*/0,
  78. /*custom_name=*/nullptr,
  79. /*version=*/0};
  80. }
  81. } // namespace tflite