conv.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_CONV_H_
  13. #define TENSORFLOW_LITE_MICRO_KERNELS_CONV_H_
  14. #include <cstdint>
  15. #include "tensorflow/lite/c/builtin_op_data.h"
  16. #include "tensorflow/lite/c/common.h"
  17. #include "tensorflow/lite/kernels/internal/types.h"
  18. namespace tflite {
  19. struct OpDataConv {
  20. TfLitePaddingValues padding;
  21. // Cached tensor zero point values for quantized operations.
  22. int32_t input_zero_point;
  23. int32_t filter_zero_point;
  24. int32_t output_zero_point;
  25. // The scaling factor from input to output (aka the 'real multiplier') can
  26. // be represented as a fixed point multiplier plus a left shift.
  27. int32_t output_multiplier;
  28. int output_shift;
  29. // Per channel output multiplier and shift.
  30. int32_t* per_channel_output_multiplier;
  31. int32_t* per_channel_output_shift;
  32. // The range of the fused activation layer. For example for kNone and
  33. // uint8_t these would be 0 and 255.
  34. int32_t output_activation_min;
  35. int32_t output_activation_max;
  36. };
  37. extern const int kConvInputTensor;
  38. extern const int kConvWeightsTensor;
  39. extern const int kConvBiasTensor;
  40. extern const int kConvOutputTensor;
  41. extern const int kConvQuantizedDimension;
  42. // Returns a ConvParams struct with all the parameters needed for a
  43. // float computation.
  44. ConvParams ConvParamsFloat(const TfLiteConvParams& params,
  45. const OpDataConv& data);
  46. // Returns a ConvParams struct with all the parameters needed for a
  47. // quantized computation.
  48. ConvParams ConvParamsQuantized(const TfLiteConvParams& params,
  49. const OpDataConv& data);
  50. TfLiteStatus CalculateOpDataConv(TfLiteContext* context, TfLiteNode* node,
  51. const TfLiteConvParams& params, int width,
  52. int height, int filter_width,
  53. int filter_height, int out_width,
  54. int out_height, const TfLiteType data_type,
  55. OpDataConv* data);
  56. TfLiteStatus ConvPrepare(TfLiteContext* context, TfLiteNode* node);
  57. // This is the most generic TfLiteRegistration. The actual supported types may
  58. // still be target dependent. The only requirement is that every implementation
  59. // (reference or optimized) must define this function.
  60. TfLiteRegistration Register_CONV_2D();
  61. #if defined(XTENSA)
  62. // Returns a TfLiteRegistration struct for kernel variant that only supports
  63. // int8 activations and int8 weights and always calls the reference
  64. // implementation.
  65. TfLiteRegistration Register_CONV_2D_INT8REF();
  66. #else
  67. inline TfLiteRegistration Register_CONV_2D_INT8REF() {
  68. return Register_CONV_2D();
  69. }
  70. #endif
  71. #if defined(CMSIS_NN)
  72. // Returns a TfLiteRegistration struct for kernel variant that only supports
  73. // int8 activations and int8 weights and uses the latency optimized
  74. // implementations.
  75. TfLiteRegistration Register_CONV_2D_INT8();
  76. // Returns a TfLiteRegistration struct for kernel variant that only supports
  77. // int16 activations and int8 weights and uses the latency optimized
  78. // implementations.
  79. TfLiteRegistration Register_CONV_2D_INT16();
  80. #else
  81. inline TfLiteRegistration Register_CONV_2D_INT8() { return Register_CONV_2D(); }
  82. inline TfLiteRegistration Register_CONV_2D_INT16() {
  83. return Register_CONV_2D();
  84. }
  85. #endif
  86. } // namespace tflite
  87. #endif // TENSORFLOW_LITE_MICRO_KERNELS_CONV_H_