logical.cc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/kernel_util.h"
  16. #include "tensorflow/lite/kernels/op_macros.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 TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
  29. const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
  30. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  31. if (HaveSameShapes(input1, input2)) {
  32. reference_ops::BinaryFunction<bool, bool, bool>(
  33. GetTensorShape(input1), GetTensorData<bool>(input1),
  34. GetTensorShape(input2), GetTensorData<bool>(input2),
  35. GetTensorShape(output), GetTensorData<bool>(output), func);
  36. } else {
  37. reference_ops::BroadcastBinaryFunction4DSlow<bool, bool, bool>(
  38. GetTensorShape(input1), GetTensorData<bool>(input1),
  39. GetTensorShape(input2), GetTensorData<bool>(input2),
  40. GetTensorShape(output), GetTensorData<bool>(output), func);
  41. }
  42. return kTfLiteOk;
  43. }
  44. bool LogicalOr(bool x, bool y) { return x || y; }
  45. TfLiteStatus LogicalOrEval(TfLiteContext* context, TfLiteNode* node) {
  46. return LogicalImpl(context, node, LogicalOr);
  47. }
  48. bool LogicalAnd(bool x, bool y) { return x && y; }
  49. TfLiteStatus LogicalAndEval(TfLiteContext* context, TfLiteNode* node) {
  50. return LogicalImpl(context, node, LogicalAnd);
  51. }
  52. } // namespace
  53. } // namespace logical
  54. TfLiteRegistration* Register_LOGICAL_OR() {
  55. // Init, Free, Prepare, Eval are satisfying the Interface required by
  56. // TfLiteRegistration.
  57. static TfLiteRegistration r = {/*init=*/nullptr,
  58. /*free=*/nullptr,
  59. /*prepare=*/nullptr,
  60. /*invoke=*/logical::LogicalOrEval,
  61. /*profiling_string=*/nullptr,
  62. /*builtin_code=*/0,
  63. /*custom_name=*/nullptr,
  64. /*version=*/0};
  65. return &r;
  66. }
  67. TfLiteRegistration* Register_LOGICAL_AND() {
  68. // Init, Free, Prepare, Eval are satisfying the Interface required by
  69. // TfLiteRegistration.
  70. static TfLiteRegistration r = {/*init=*/nullptr,
  71. /*free=*/nullptr,
  72. /*prepare=*/nullptr,
  73. /*invoke=*/logical::LogicalAndEval,
  74. /*profiling_string=*/nullptr,
  75. /*builtin_code=*/0,
  76. /*custom_name=*/nullptr,
  77. /*version=*/0};
  78. return &r;
  79. }
  80. } // namespace micro
  81. } // namespace ops
  82. } // namespace tflite