reduce_functor.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "ultra_infer/function/eigen.h"
  16. namespace ultra_infer {
  17. namespace function {
  18. //////// Max Functor ///////
  19. struct MaxFunctor {
  20. template <typename X, typename Y, typename Dim>
  21. void operator()(const Eigen::DefaultDevice &dev, X *x, Y *y, const Dim &dim) {
  22. y->device(dev) = x->maximum(dim);
  23. }
  24. };
  25. //////// Min Functor ///////
  26. struct MinFunctor {
  27. template <typename X, typename Y, typename Dim>
  28. void operator()(const Eigen::DefaultDevice &dev, X *x, Y *y, const Dim &dim) {
  29. y->device(dev) = x->minimum(dim);
  30. }
  31. };
  32. //////// Sum Functor ///////
  33. struct SumFunctor {
  34. template <typename X, typename Y, typename Dim>
  35. void operator()(const Eigen::DefaultDevice &dev, X *x, Y *y, const Dim &dim) {
  36. y->device(dev) = x->sum(dim);
  37. }
  38. };
  39. //////// All Functor ///////
  40. struct AllFunctor {
  41. template <typename X, typename Y, typename Dim>
  42. void operator()(const Eigen::DefaultDevice &dev, X *x, Y *y, const Dim &dim) {
  43. y->device(dev) = x->all(dim);
  44. }
  45. };
  46. //////// Any Functor ///////
  47. struct AnyFunctor {
  48. template <typename X, typename Y, typename Dim>
  49. void operator()(const Eigen::DefaultDevice &dev, X *x, Y *y, const Dim &dim) {
  50. y->device(dev) = x->any(dim);
  51. }
  52. };
  53. //////// Mean Functor ///////
  54. struct MeanFunctor {
  55. template <typename X, typename Y, typename Dim>
  56. void operator()(const Eigen::DefaultDevice &dev, X *x, Y *y, const Dim &dim) {
  57. y->device(dev) = x->mean(dim);
  58. }
  59. };
  60. //////// Prod Functor ///////
  61. struct ProdFunctor {
  62. template <typename X, typename Y, typename Dim>
  63. void operator()(const Eigen::DefaultDevice &dev, X *x, Y *y, const Dim &dim) {
  64. y->device(dev) = x->prod(dim);
  65. }
  66. };
  67. } // namespace function
  68. } // namespace ultra_infer