binary_function.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_
  14. #include "tensorflow/lite/kernels/internal/common.h"
  15. #include "tensorflow/lite/kernels/internal/compatibility.h"
  16. #include "tensorflow/lite/kernels/internal/types.h"
  17. namespace tflite {
  18. namespace reference_ops {
  19. // TODO(ycling): Refactoring. Remove BroadcastLogical and use the more
  20. // generalized and efficient BroadcastBinaryFunction.
  21. //
  22. // Also appears to duplicate MinimumMaximum.
  23. //
  24. // R: Result type. T1: Input 1 type. T2: Input 2 type.
  25. template <typename R, typename T1, typename T2>
  26. inline void BroadcastBinaryFunction4DSlow(
  27. const RuntimeShape& unextended_input1_shape, const T1* input1_data,
  28. const RuntimeShape& unextended_input2_shape, const T2* input2_data,
  29. const RuntimeShape& unextended_output_shape, R* output_data,
  30. R (*func)(T1, T2)) {
  31. TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4);
  32. TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), 4);
  33. TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4);
  34. const RuntimeShape output_shape =
  35. RuntimeShape::ExtendedShape(4, unextended_output_shape);
  36. NdArrayDesc<4> desc1;
  37. NdArrayDesc<4> desc2;
  38. NdArrayDescsForElementwiseBroadcast(unextended_input1_shape,
  39. unextended_input2_shape, &desc1, &desc2);
  40. for (int b = 0; b < output_shape.Dims(0); ++b) {
  41. for (int y = 0; y < output_shape.Dims(1); ++y) {
  42. for (int x = 0; x < output_shape.Dims(2); ++x) {
  43. for (int c = 0; c < output_shape.Dims(3); ++c) {
  44. auto out_idx = Offset(output_shape, b, y, x, c);
  45. auto in1_idx = SubscriptToIndex(desc1, b, y, x, c);
  46. auto in2_idx = SubscriptToIndex(desc2, b, y, x, c);
  47. auto in1_val = input1_data[in1_idx];
  48. auto in2_val = input2_data[in2_idx];
  49. output_data[out_idx] = func(in1_val, in2_val);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. // R: Result type. T1: Input 1 type. T2: Input 2 type.
  56. // TODO(renjieliu): Refactor other binary functions to use this one.
  57. template <typename R, typename T1, typename T2>
  58. inline void BinaryFunction(const RuntimeShape& input1_shape,
  59. const T1* input1_data,
  60. const RuntimeShape& input2_shape,
  61. const T2* input2_data,
  62. const RuntimeShape& output_shape, R* output_data,
  63. R (*func)(T1, T2)) {
  64. const int flat_size =
  65. MatchingFlatSize(input1_shape, input2_shape, output_shape);
  66. for (int i = 0; i < flat_size; ++i) {
  67. output_data[i] = func(input1_data[i], input2_data[i]);
  68. }
  69. }
  70. } // namespace reference_ops
  71. } // namespace tflite
  72. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_