maximum_minimum.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright 2017 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_MAXIMUM_MINIMUM_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_
  14. #include "tensorflow/lite/kernels/internal/common.h"
  15. #include "tensorflow/lite/kernels/internal/types.h"
  16. namespace tflite {
  17. namespace reference_ops {
  18. template <typename T, typename Op, int N = 5>
  19. void MaximumMinimumBroadcastSlow(const RuntimeShape& unextended_input1_shape,
  20. const T* input1_data,
  21. const RuntimeShape& unextended_input2_shape,
  22. const T* input2_data,
  23. const RuntimeShape& unextended_output_shape,
  24. T* output_data, Op op) {
  25. // Uses element-wise calculation if broadcast is not required.
  26. if (unextended_input1_shape == unextended_input2_shape) {
  27. const int flat_size =
  28. MatchingElementsSize(unextended_input1_shape, unextended_input2_shape,
  29. unextended_output_shape);
  30. for (int i = 0; i < flat_size; ++i) {
  31. output_data[i] = op(input1_data[i], input2_data[i]);
  32. }
  33. } else {
  34. TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), N);
  35. TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), N);
  36. TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), N);
  37. NdArrayDesc<N> desc1;
  38. NdArrayDesc<N> desc2;
  39. NdArrayDesc<N> output_desc;
  40. NdArrayDescsForElementwiseBroadcast(
  41. unextended_input1_shape, unextended_input2_shape, &desc1, &desc2);
  42. CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape),
  43. &output_desc);
  44. auto maxmin_func = [&](int indexes[N]) {
  45. output_data[SubscriptToIndex(output_desc, indexes)] =
  46. op(input1_data[SubscriptToIndex(desc1, indexes)],
  47. input2_data[SubscriptToIndex(desc2, indexes)]);
  48. };
  49. NDOpsHelper<N>(output_desc, maxmin_func);
  50. }
  51. }
  52. } // namespace reference_ops
  53. } // namespace tflite
  54. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_