math.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "ultra_infer/function/math.h"
  15. #include "ultra_infer/function/eigen.h"
  16. #include "ultra_infer/function/math_functor.h"
  17. namespace ultra_infer {
  18. namespace function {
  19. #define DEFINE_ACTIVATION_KERNEL(name, functor_class) \
  20. template <typename T> void name##Kernel(const FDTensor &x, FDTensor *out) { \
  21. functor_class<T> functor; \
  22. ActivationImpl<T, functor_class<T>>(x, out, functor); \
  23. }
  24. template <typename T, typename Functor>
  25. void ActivationImpl(const FDTensor &X, FDTensor *Out, const Functor &functor) {
  26. FDASSERT(Out != nullptr, "Output Out should not be nullptr");
  27. FDTensor out_tmp;
  28. auto x = EigenVector<T>::Flatten(X);
  29. out_tmp.Allocate(X.Shape(), X.Dtype());
  30. auto out = EigenVector<T>::Flatten(out_tmp);
  31. const auto &dev = *EigenDeviceWrapper::GetInstance()->GetDevice();
  32. functor(dev, x, out);
  33. *Out = std::move(out_tmp);
  34. }
  35. DEFINE_ACTIVATION_KERNEL(Sqrt, SqrtFunctor)
  36. DEFINE_ACTIVATION_KERNEL(Log, LogFunctor)
  37. DEFINE_ACTIVATION_KERNEL(Round, RoundFunctor)
  38. DEFINE_ACTIVATION_KERNEL(Exp, ExpFunctor)
  39. DEFINE_ACTIVATION_KERNEL(Abs, AbsFunctor)
  40. DEFINE_ACTIVATION_KERNEL(Ceil, CeilFunctor)
  41. DEFINE_ACTIVATION_KERNEL(Floor, FloorFunctor)
  42. void Sqrt(const FDTensor &x, FDTensor *out) {
  43. FD_VISIT_FLOAT_TYPES(x.dtype, "SqrtKernel",
  44. ([&] { SqrtKernel<data_t>(x, out); }));
  45. }
  46. void Log(const FDTensor &x, FDTensor *out) {
  47. FD_VISIT_FLOAT_TYPES(x.dtype, "LogKernel",
  48. ([&] { LogKernel<data_t>(x, out); }));
  49. }
  50. void Round(const FDTensor &x, FDTensor *out) {
  51. FD_VISIT_FLOAT_TYPES(x.dtype, "RoundKernel",
  52. ([&] { RoundKernel<data_t>(x, out); }));
  53. }
  54. void Exp(const FDTensor &x, FDTensor *out) {
  55. FD_VISIT_FLOAT_TYPES(x.dtype, "ExpKernel",
  56. ([&] { ExpKernel<data_t>(x, out); }));
  57. }
  58. void Abs(const FDTensor &x, FDTensor *out) {
  59. FD_VISIT_FLOAT_TYPES(x.dtype, "AbsKernel",
  60. ([&] { AbsKernel<data_t>(x, out); }));
  61. }
  62. void Ceil(const FDTensor &x, FDTensor *out) {
  63. FD_VISIT_FLOAT_TYPES(x.dtype, "CeilKernel",
  64. ([&] { CeilKernel<data_t>(x, out); }));
  65. }
  66. void Floor(const FDTensor &x, FDTensor *out) {
  67. FD_VISIT_FLOAT_TYPES(x.dtype, "FloorKernel",
  68. ([&] { FloorKernel<data_t>(x, out); }));
  69. }
  70. } // namespace function
  71. } // namespace ultra_infer