kernel_util.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace tflite {
  20. namespace micro {
  21. // Returns a mutable tensor for a given input index. is_variable must be checked
  22. // during prepare when the full TfLiteTensor is available.
  23. TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context,
  24. const TfLiteNode* node, int index);
  25. // Returns the TfLiteEvalTensor struct for a given input index in a node.
  26. const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context,
  27. const TfLiteNode* node, int index);
  28. // Returns the TfLiteEvalTensor struct for a given output index in a node.
  29. TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context,
  30. const TfLiteNode* node, int index);
  31. // Returns data for a TfLiteEvalTensor struct.
  32. template <typename T>
  33. T* GetTensorData(TfLiteEvalTensor* tensor) {
  34. return tensor != nullptr ? reinterpret_cast<T*>(tensor->data.raw) : nullptr;
  35. }
  36. // Returns const data for a TfLiteEvalTensor struct.
  37. template <typename T>
  38. const T* GetTensorData(const TfLiteEvalTensor* tensor) {
  39. TFLITE_DCHECK(tensor != nullptr);
  40. return reinterpret_cast<const T*>(tensor->data.raw);
  41. }
  42. // Returns the shape of a TfLiteEvalTensor struct.
  43. const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor);
  44. // Return true if the given tensors have the same shape.
  45. bool HaveSameShapes(const TfLiteEvalTensor* input1,
  46. const TfLiteEvalTensor* input2);
  47. PaddingType RuntimePaddingType(TfLitePadding padding);
  48. // Relocate tensor dims from FlatBuffer to the persistent storage arena.
  49. // The old dims data is copied to the new storage area.
  50. // The tensor and eval_tensor must be the same tensor.
  51. // Only use during Prepare phase.
  52. TfLiteStatus CreateWritableTensorDimsWithCopy(TfLiteContext* context,
  53. TfLiteTensor* tensor,
  54. TfLiteEvalTensor* eval_tensor);
  55. // Returns a blob of payload data. The payload is subjected to interpretation by
  56. // the OP. This is the recommended API for an OP to get an external context. OP
  57. // should use this instead of directly calling GetExternalContext function in
  58. // context. Example usage:
  59. //
  60. // An application can set an external context through interpreter as below
  61. // interpreter->SetMicroExternalContext(pointer_to_your_payload);
  62. //
  63. // Inside an OP that needs this payload, it get the payload pointer by:
  64. // Prepare(TfliteContext * context) {
  65. // ...
  66. // payload_ptr =
  67. // reinterpret_cast<your_data_type>(GetMicroExternalContext(context))
  68. // ...
  69. // }
  70. //
  71. void* GetMicroExternalContext(TfLiteContext* context);
  72. } // namespace micro
  73. } // namespace tflite
  74. #endif // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_