kernel_util.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright 2021 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_KERNEL_UTIL_H_
  13. #define TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_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/compatibility.h"
  18. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  19. #include "tensorflow/lite/kernels/internal/types.h"
  20. #include "tensorflow/lite/micro/micro_context.h"
  21. #include "tensorflow/lite/micro/micro_error_reporter.h"
  22. namespace tflite {
  23. namespace micro {
  24. TfLiteRegistration RegisterOp(
  25. void* (*init)(TfLiteContext* context, const char* buffer, size_t length),
  26. TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node),
  27. TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node),
  28. void (*free)(TfLiteContext* context, void* buffer) = nullptr);
  29. // Prints out n bytes in a int8_t buffer as hex
  30. void PrintNBytes(const int8_t* tensor_data, int n_bytes,
  31. const char* prefix = nullptr);
  32. // Prints out the the n bytes in a TfLiteEvalTensor as hex
  33. void PrintNBytes(const TfLiteEvalTensor* tensor, int n_bytes,
  34. const char* prefix = nullptr);
  35. // Prints out the the n bytes in a TfLiteTensor as hex
  36. void PrintNBytes(const TfLiteTensor* tensor, int n_bytes,
  37. const char* prefix = nullptr);
  38. // Returns a mutable tensor for a given input index. is_variable must be checked
  39. // during prepare when the full TfLiteTensor is available.
  40. TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context,
  41. const TfLiteNode* node, int index);
  42. // Returns the TfLiteEvalTensor struct for a given input index in a node.
  43. const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context,
  44. const TfLiteNode* node, int index);
  45. // Returns the TfLiteEvalTensor struct for a given output index in a node.
  46. TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context,
  47. const TfLiteNode* node, int index);
  48. // Returns data for a TfLiteEvalTensor struct that are expected to exist.
  49. template <typename T>
  50. T* GetTensorData(TfLiteEvalTensor* tensor) {
  51. TFLITE_DCHECK(tensor != nullptr);
  52. return reinterpret_cast<T*>(tensor->data.raw);
  53. }
  54. // Returns const data for a TfLiteEvalTensor struct that are expected to exist.
  55. template <typename T>
  56. const T* GetTensorData(const TfLiteEvalTensor* tensor) {
  57. TFLITE_DCHECK(tensor != nullptr);
  58. return reinterpret_cast<const T*>(tensor->data.raw);
  59. }
  60. // Returns data for a TfLiteEvalTensor struct that could be null.
  61. template <typename T>
  62. T* GetOptionalTensorData(TfLiteEvalTensor* tensor) {
  63. return tensor == nullptr ? nullptr : reinterpret_cast<T*>(tensor->data.raw);
  64. }
  65. // Returns const data for a TfLiteEvalTensor struct that could be null.
  66. template <typename T>
  67. const T* GetOptionalTensorData(const TfLiteEvalTensor* tensor) {
  68. return tensor == nullptr ? nullptr
  69. : reinterpret_cast<const T*>(tensor->data.raw);
  70. }
  71. // Returns the shape of a TfLiteEvalTensor struct.
  72. const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor);
  73. // Return true if the given tensors have the same shape.
  74. bool HaveSameShapes(const TfLiteEvalTensor* input1,
  75. const TfLiteEvalTensor* input2);
  76. PaddingType RuntimePaddingType(TfLitePadding padding);
  77. // Relocate tensor dims from FlatBuffer to the persistent storage arena.
  78. // The old dims data is copied to the new storage area.
  79. // The tensor and eval_tensor must be the same tensor.
  80. // Only use during Prepare phase.
  81. TfLiteStatus CreateWritableTensorDimsWithCopy(TfLiteContext* context,
  82. TfLiteTensor* tensor,
  83. TfLiteEvalTensor* eval_tensor);
  84. // Copy all op input tensors to op output tensors. Requires all op input tensor
  85. // shapes and types to be identical to op output tensor shapes and types.
  86. TfLiteStatus CopyOpInputsToOpOutputs(TfLiteContext* context, TfLiteNode* node);
  87. // Copy all op input tensors to subgraph input tensors. Requires all op input
  88. // tensor shapes and types to be identical to subgraph input tensor shapes and
  89. // types.
  90. TfLiteStatus CopyOpInputsToSubgraphInputs(TfLiteContext* context,
  91. TfLiteNode* node,
  92. MicroGraph* graph_info,
  93. int subgraph_idx,
  94. int first_tensor_idx);
  95. // Copy all op output tensors to subgraph input tensors. Requires all op output
  96. // tensor shapes and types to be identical to subgraph input tensor shapes and
  97. // types.
  98. TfLiteStatus CopyOpOutputsToSubgraphInputs(TfLiteContext* context,
  99. TfLiteNode* node,
  100. MicroGraph* graph_info,
  101. int subgraph_idx);
  102. // Copy all subgraph output tensors to op outputs. Requires all subgraph output
  103. // tensor shapes and types to be identical to op output tensor shapes and types.
  104. TfLiteStatus CopySubgraphOutputsToOpOutputs(TfLiteContext* context,
  105. TfLiteNode* node,
  106. MicroGraph* graph_info,
  107. int subgraph_idx);
  108. } // namespace micro
  109. } // namespace tflite
  110. #endif // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_