SpectralOpsUtils.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include <string>
  3. #include <stdexcept>
  4. #include <sstream>
  5. #include <c10/core/ScalarType.h>
  6. #include <c10/util/ArrayRef.h>
  7. #include <c10/util/Exception.h>
  8. #include <ATen/native/DispatchStub.h>
  9. #include <ATen/core/TensorBase.h>
  10. namespace at::native {
  11. // Normalization types used in _fft_with_size
  12. enum class fft_norm_mode {
  13. none, // No normalization
  14. by_root_n, // Divide by sqrt(signal_size)
  15. by_n, // Divide by signal_size
  16. };
  17. // NOTE [ Fourier Transform Conjugate Symmetry ]
  18. //
  19. // Real-to-complex Fourier transform satisfies the conjugate symmetry. That is,
  20. // assuming X is the transformed K-dimensionsal signal, we have
  21. //
  22. // X[i_1, ..., i_K] = X[j_i, ..., j_K]*,
  23. //
  24. // where j_k = (N_k - i_k) mod N_k, N_k being the signal size at dim k,
  25. // * is the conjugate operator.
  26. //
  27. // Therefore, in such cases, FFT libraries return only roughly half of the
  28. // values to avoid redundancy:
  29. //
  30. // X[:, :, ..., :floor(N / 2) + 1]
  31. //
  32. // This is also the assumption in cuFFT and MKL. In ATen SpectralOps, such
  33. // halved signal will also be returned by default (flag onesided=True).
  34. // The following infer_ft_real_to_complex_onesided_size function calculates the
  35. // onesided size from the twosided size.
  36. //
  37. // Note that this loses some information about the size of signal at last
  38. // dimension. E.g., both 11 and 10 maps to 6. Hence, the following
  39. // infer_ft_complex_to_real_onesided_size function takes in optional parameter
  40. // to infer the twosided size from given onesided size.
  41. //
  42. // cuFFT doc: http://docs.nvidia.com/cuda/cufft/index.html#multi-dimensional
  43. // MKL doc: https://software.intel.com/en-us/mkl-developer-reference-c-dfti-complex-storage-dfti-real-storage-dfti-conjugate-even-storage#CONJUGATE_EVEN_STORAGE
  44. inline int64_t infer_ft_real_to_complex_onesided_size(int64_t real_size) {
  45. return (real_size / 2) + 1;
  46. }
  47. inline int64_t infer_ft_complex_to_real_onesided_size(int64_t complex_size,
  48. int64_t expected_size=-1) {
  49. int64_t base = (complex_size - 1) * 2;
  50. if (expected_size < 0) {
  51. return base + 1;
  52. } else if (base == expected_size) {
  53. return base;
  54. } else if (base + 1 == expected_size) {
  55. return base + 1;
  56. } else {
  57. std::ostringstream ss;
  58. ss << "expected real signal size " << expected_size << " is incompatible "
  59. << "with onesided complex frequency size " << complex_size;
  60. AT_ERROR(ss.str());
  61. }
  62. }
  63. using fft_fill_with_conjugate_symmetry_fn =
  64. void (*)(ScalarType dtype, IntArrayRef mirror_dims, IntArrayRef half_sizes,
  65. IntArrayRef in_strides, const void* in_data,
  66. IntArrayRef out_strides, void* out_data);
  67. DECLARE_DISPATCH(fft_fill_with_conjugate_symmetry_fn, fft_fill_with_conjugate_symmetry_stub);
  68. // In real-to-complex transform, cuFFT and MKL only fill half of the values
  69. // due to conjugate symmetry. This function fills in the other half of the full
  70. // fft by using the Hermitian symmetry in the signal.
  71. // self should be the shape of the full signal and dims.back() should be the
  72. // one-sided dimension.
  73. // See NOTE [ Fourier Transform Conjugate Symmetry ]
  74. TORCH_API void _fft_fill_with_conjugate_symmetry_(const Tensor& self, IntArrayRef dims);
  75. } // namespace at::native