kernel_util.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/types.h"
  19. #include "tensorflow/lite/micro/micro_context.h"
  20. namespace tflite {
  21. namespace micro {
  22. // Returns a mutable tensor for a given input index. is_variable must be checked
  23. // during prepare when the full TfLiteTensor is available.
  24. TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context,
  25. const TfLiteNode* node, int index);
  26. // Returns the TfLiteEvalTensor struct for a given input index in a node.
  27. const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context,
  28. const TfLiteNode* node, int index);
  29. // Returns the TfLiteEvalTensor struct for a given output index in a node.
  30. TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context,
  31. const TfLiteNode* node, int index);
  32. // Returns data for a TfLiteEvalTensor struct.
  33. template <typename T>
  34. T* GetTensorData(TfLiteEvalTensor* tensor) {
  35. return tensor != nullptr ? reinterpret_cast<T*>(tensor->data.raw) : nullptr;
  36. }
  37. // Returns const data for a TfLiteEvalTensor struct.
  38. template <typename T>
  39. const T* GetTensorData(const TfLiteEvalTensor* tensor) {
  40. TFLITE_DCHECK(tensor != nullptr);
  41. return reinterpret_cast<const T*>(tensor->data.raw);
  42. }
  43. // Returns the shape of a TfLiteEvalTensor struct.
  44. const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor);
  45. // Return true if the given tensors have the same shape.
  46. bool HaveSameShapes(const TfLiteEvalTensor* input1,
  47. const TfLiteEvalTensor* input2);
  48. PaddingType RuntimePaddingType(TfLitePadding padding);
  49. // Relocate tensor dims from FlatBuffer to the persistent storage arena.
  50. // The old dims data is copied to the new storage area.
  51. // The tensor and eval_tensor must be the same tensor.
  52. // Only use during Prepare phase.
  53. TfLiteStatus CreateWritableTensorDimsWithCopy(TfLiteContext* context,
  54. TfLiteTensor* tensor,
  55. TfLiteEvalTensor* eval_tensor);
  56. // Copy all op input tensors to op output tensors. Requires all op input tensor
  57. // shapes and types to be identical to op output tensor shapes and types.
  58. TfLiteStatus CopyOpInputsToOpOutputs(TfLiteContext* context, TfLiteNode* node);
  59. // Copy all op input tensors to subgraph input tensors. Requires all op input
  60. // tensor shapes and types to be identical to subgraph input tensor shapes and
  61. // types.
  62. TfLiteStatus CopyOpInputsToSubgraphInputs(TfLiteContext* context,
  63. TfLiteNode* node,
  64. MicroGraph* graph_info,
  65. int subgraph_idx,
  66. int first_tensor_idx);
  67. // Copy all op output tensors to subgraph input tensors. Requires all op output
  68. // tensor shapes and types to be identical to subgraph input tensor shapes and
  69. // types.
  70. TfLiteStatus CopyOpOutputsToSubgraphInputs(TfLiteContext* context,
  71. TfLiteNode* node,
  72. MicroGraph* graph_info,
  73. int subgraph_idx);
  74. // Copy all subgraph output tensors to op outputs. Requires all subgraph output
  75. // tensor shapes and types to be identical to op output tensor shapes and types.
  76. TfLiteStatus CopySubgraphOutputsToOpOutputs(TfLiteContext* context,
  77. TfLiteNode* node,
  78. MicroGraph* graph_info,
  79. int subgraph_idx);
  80. } // namespace micro
  81. } // namespace tflite
  82. #endif // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_