logical.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2019 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. #include "tensorflow/lite/c/common.h"
  13. #include "tensorflow/lite/kernels/internal/reference/binary_function.h"
  14. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  15. #include "tensorflow/lite/kernels/op_macros.h"
  16. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  17. namespace tflite {
  18. namespace ops {
  19. namespace micro {
  20. namespace logical {
  21. namespace {
  22. // Input/output tensor index.
  23. constexpr int kInputTensor1 = 0;
  24. constexpr int kInputTensor2 = 1;
  25. constexpr int kOutputTensor = 0;
  26. TfLiteStatus LogicalImpl(TfLiteContext* context, TfLiteNode* node,
  27. bool (*func)(bool, bool)) {
  28. const TfLiteEvalTensor* input1 =
  29. tflite::micro::GetEvalInput(context, node, kInputTensor1);
  30. const TfLiteEvalTensor* input2 =
  31. tflite::micro::GetEvalInput(context, node, kInputTensor2);
  32. TfLiteEvalTensor* output =
  33. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  34. if (tflite::micro::HaveSameShapes(input1, input2)) {
  35. reference_ops::BinaryFunction<bool, bool, bool>(
  36. tflite::micro::GetTensorShape(input1),
  37. tflite::micro::GetTensorData<bool>(input1),
  38. tflite::micro::GetTensorShape(input2),
  39. tflite::micro::GetTensorData<bool>(input2),
  40. tflite::micro::GetTensorShape(output),
  41. tflite::micro::GetTensorData<bool>(output), func);
  42. } else {
  43. reference_ops::BroadcastBinaryFunction4DSlow<bool, bool, bool>(
  44. tflite::micro::GetTensorShape(input1),
  45. tflite::micro::GetTensorData<bool>(input1),
  46. tflite::micro::GetTensorShape(input2),
  47. tflite::micro::GetTensorData<bool>(input2),
  48. tflite::micro::GetTensorShape(output),
  49. tflite::micro::GetTensorData<bool>(output), func);
  50. }
  51. return kTfLiteOk;
  52. }
  53. bool LogicalOr(bool x, bool y) { return x || y; }
  54. TfLiteStatus LogicalOrEval(TfLiteContext* context, TfLiteNode* node) {
  55. return LogicalImpl(context, node, LogicalOr);
  56. }
  57. bool LogicalAnd(bool x, bool y) { return x && y; }
  58. TfLiteStatus LogicalAndEval(TfLiteContext* context, TfLiteNode* node) {
  59. return LogicalImpl(context, node, LogicalAnd);
  60. }
  61. } // namespace
  62. } // namespace logical
  63. TfLiteRegistration Register_LOGICAL_OR() {
  64. // Init, Free, Prepare, Eval are satisfying the Interface required by
  65. // TfLiteRegistration.
  66. return {/*init=*/nullptr,
  67. /*free=*/nullptr,
  68. /*prepare=*/nullptr,
  69. /*invoke=*/logical::LogicalOrEval,
  70. /*profiling_string=*/nullptr,
  71. /*builtin_code=*/0,
  72. /*custom_name=*/nullptr,
  73. /*version=*/0};
  74. }
  75. TfLiteRegistration Register_LOGICAL_AND() {
  76. // Init, Free, Prepare, Eval are satisfying the Interface required by
  77. // TfLiteRegistration.
  78. return {/*init=*/nullptr,
  79. /*free=*/nullptr,
  80. /*prepare=*/nullptr,
  81. /*invoke=*/logical::LogicalAndEval,
  82. /*profiling_string=*/nullptr,
  83. /*builtin_code=*/0,
  84. /*custom_name=*/nullptr,
  85. /*version=*/0};
  86. }
  87. } // namespace micro
  88. } // namespace ops
  89. } // namespace tflite