svdf.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef TENSORFLOW_LITE_MICRO_KERNELS_SVDF_H_
  13. #define TENSORFLOW_LITE_MICRO_KERNELS_SVDF_H_
  14. #include "tensorflow/lite/c/builtin_op_data.h"
  15. #include "tensorflow/lite/c/common.h"
  16. namespace tflite {
  17. struct OpDataSvdf {
  18. int32_t effective_scale_1_a;
  19. int32_t effective_scale_2_a;
  20. // b versions of each scale are kept at int since the numbers are just the
  21. // shift value - typically between [-32, 32].
  22. int effective_scale_1_b;
  23. int effective_scale_2_b;
  24. int scratch_tensor_index;
  25. int scratch_output_tensor_index;
  26. // Cached tensor zero point values for quantized operations.
  27. int input_zero_point;
  28. int output_zero_point;
  29. int activation_state_zero_point;
  30. };
  31. // Input tensors.
  32. extern const int kSvdfInputTensor;
  33. extern const int kSvdfWeightsFeatureTensor;
  34. extern const int kSvdfWeightsTimeTensor;
  35. extern const int kSvdfBiasTensor;
  36. // This is a variable tensor, and will be modified by this op.
  37. extern const int kSvdfInputActivationStateTensor;
  38. // Output tensor.
  39. extern const int kSvdfOutputTensor;
  40. void EvalInt8SvdfReference(TfLiteContext* context, TfLiteNode* node,
  41. const TfLiteEvalTensor* input_tensor,
  42. const TfLiteEvalTensor* weights_feature_tensor,
  43. const TfLiteEvalTensor* weights_time_tensor,
  44. const TfLiteEvalTensor* bias_tensor,
  45. const TfLiteSVDFParams* params,
  46. TfLiteEvalTensor* activation_state_tensor,
  47. TfLiteEvalTensor* output_tensor,
  48. const OpDataSvdf& data);
  49. // TODO(#523): remove 16-bit code when no longer needed.
  50. void EvalInt16SvdfReference(TfLiteContext* context, TfLiteNode* node,
  51. const TfLiteEvalTensor* input_tensor,
  52. const TfLiteEvalTensor* weights_feature_tensor,
  53. const TfLiteEvalTensor* weights_time_tensor,
  54. const TfLiteEvalTensor* bias_tensor,
  55. const TfLiteSVDFParams* params,
  56. TfLiteEvalTensor* activation_state_tensor,
  57. TfLiteEvalTensor* output_tensor,
  58. const OpDataSvdf& data);
  59. void EvalFloatSvdfReference(
  60. TfLiteContext* context, TfLiteNode* node, const TfLiteEvalTensor* input,
  61. const TfLiteEvalTensor* weights_feature,
  62. const TfLiteEvalTensor* weights_time, const TfLiteEvalTensor* bias,
  63. const TfLiteSVDFParams* params, int scratch_tensor_index,
  64. TfLiteEvalTensor* activation_state, TfLiteEvalTensor* output);
  65. TfLiteStatus PrepareSvdf(TfLiteContext* context, TfLiteNode* node);
  66. // This is the most generic TfLiteRegistration. The actual supported types may
  67. // still be target dependent. The only requirement is that every implementation
  68. // (reference or optimized) must define this function.
  69. TfLiteRegistration Register_SVDF();
  70. #if defined(HEXAGON) || defined(CMSIS_NN)
  71. TfLiteRegistration Register_SVDF_INT8();
  72. #else
  73. // Note that while this block gets used for both reference and optimized kernels
  74. // that do not have any specialized implementations, the only goal here is to
  75. // define fallback implementation that allow reference kernels to still be used
  76. // from applications that call a more specific kernel variant.
  77. inline TfLiteRegistration Register_SVDF_INT8() { return Register_SVDF(); }
  78. #endif
  79. } // namespace tflite
  80. #endif // TENSORFLOW_LITE_MICRO_KERNELS_SVDF_H_