kernel_util.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Copyright 2020 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/common.h"
  16. #include "tensorflow/lite/kernels/internal/compatibility.h"
  17. #include "tensorflow/lite/kernels/internal/types.h"
  18. namespace tflite {
  19. namespace micro {
  20. // Returns a mutable tensor for a given input index. is_variable must be checked
  21. // during prepare when the full TfLiteTensor is available.
  22. inline TfLiteEvalTensor* GetMutableEvalInput(const TfLiteContext* context,
  23. const TfLiteNode* node,
  24. int index) {
  25. TFLITE_DCHECK(context != nullptr);
  26. TFLITE_DCHECK(node != nullptr);
  27. return context->GetEvalTensor(context, node->inputs->data[index]);
  28. }
  29. // Returns the TfLiteEvalTensor struct for a given input index in a node.
  30. inline const TfLiteEvalTensor* GetEvalInput(const TfLiteContext* context,
  31. const TfLiteNode* node, int index) {
  32. return GetMutableEvalInput(context, node, index);
  33. }
  34. // Returns the TfLiteEvalTensor struct for a given output index in a node.
  35. inline TfLiteEvalTensor* GetEvalOutput(const TfLiteContext* context,
  36. const TfLiteNode* node, int index) {
  37. TFLITE_DCHECK(context != nullptr);
  38. TFLITE_DCHECK(node != nullptr);
  39. return context->GetEvalTensor(context, node->outputs->data[index]);
  40. }
  41. // Returns data for a TfLiteEvalTensor struct.
  42. template <typename T>
  43. T* GetTensorData(TfLiteEvalTensor* tensor) {
  44. return tensor != nullptr ? reinterpret_cast<T*>(tensor->data.raw) : nullptr;
  45. }
  46. // Returns const data for a TfLiteEvalTensor struct.
  47. template <typename T>
  48. const T* GetTensorData(const TfLiteEvalTensor* tensor) {
  49. TFLITE_DCHECK(tensor != nullptr);
  50. return reinterpret_cast<const T*>(tensor->data.raw);
  51. }
  52. // Returns the shape of a TfLiteEvalTensor struct.
  53. inline const RuntimeShape GetTensorShape(const TfLiteEvalTensor* tensor) {
  54. if (tensor == nullptr) {
  55. return RuntimeShape();
  56. }
  57. TfLiteIntArray* dims = tensor->dims;
  58. const int dims_size = dims->size;
  59. const int32_t* dims_data = reinterpret_cast<const int32_t*>(dims->data);
  60. return RuntimeShape(dims_size, dims_data);
  61. }
  62. // Return true if the given tensors have the same shape.
  63. bool HaveSameShapes(const TfLiteEvalTensor* input1,
  64. const TfLiteEvalTensor* input2);
  65. } // namespace micro
  66. } // namespace tflite
  67. #endif // TENSORFLOW_LITE_MICRO_KERNELS_KERNEL_UTIL_H_