LogAddExp.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include <c10/util/complex.h>
  3. #include <ATen/NumericUtils.h>
  4. namespace at { namespace native {
  5. inline namespace CPU_CAPABILITY {
  6. // custom min and max to be used in logcumsumexp for complex arguments
  7. template <typename scalar_t>
  8. std::pair<c10::complex<scalar_t>, c10::complex<scalar_t>> _logcumsumexp_minmax(c10::complex<scalar_t> x, c10::complex<scalar_t> y) {
  9. if (at::_isnan(y)) { // either real is nan or imag is nan
  10. return std::make_pair(y, y);
  11. } else if (at::_isnan(x)) { // either real is nan or imag is nan
  12. return std::make_pair(x, x);
  13. } else {
  14. return (x.real() < y.real()) ? std::make_pair(x, y) : std::make_pair(y, x);
  15. }
  16. }
  17. template <typename scalar_t>
  18. scalar_t _log_add_exp_helper(scalar_t x, scalar_t y) {
  19. // Reference : https://www.tensorflow.org/api_docs/python/tf/math/cumulative_logsumexp
  20. scalar_t min = at::_isnan(y) ? y : std::min(x, y); // std::min returns first arg if one of the args is nan
  21. scalar_t max = at::_isnan(y) ? y : std::max(x, y); // std::max returns first arg if one of the args is nan
  22. if (min != max || std::isfinite(min)) {
  23. // nan will be propagated here
  24. return std::log1p(std::exp(min - max)) + max;
  25. } else {
  26. // special case to correctly handle infinite cases
  27. return x;
  28. }
  29. }
  30. template <typename scalar_t>
  31. c10::complex<scalar_t> _log_add_exp_helper(const c10::complex<scalar_t>& x, const c10::complex<scalar_t>& y) {
  32. auto [min, max] = _logcumsumexp_minmax<scalar_t>(x, y);
  33. auto min_real = std::real(min);
  34. auto max_real = std::real(max);
  35. if (at::_isnan(min)) { // either real is nan or imag is nan
  36. // handling the "infectious" NaNs
  37. return {std::numeric_limits<scalar_t>::quiet_NaN(), std::numeric_limits<scalar_t>::quiet_NaN()};
  38. } else if (!std::isfinite(min_real) && (min_real == max_real)) {
  39. if (min_real < 0) {
  40. // handle the -inf case, the imaginary part here does not really matter as the exp(value)
  41. // will be around 0.0 and the angle (i.e. the imaginary part) cannot be determined.
  42. // It does not matter if we're taking the exp of this value
  43. return min;
  44. } else {
  45. // handle the +inf case, we don't need the special precision for log1p for small values
  46. // and to avoid producing nan in case of real(max) == real(min) == +inf
  47. return std::log(std::exp(min) + std::exp(max));
  48. }
  49. } else {
  50. return std::log1p(std::exp(min - max)) + max;
  51. }
  52. }
  53. } // end namespace
  54. }} //end at::native