SpectralOpsUtils.h 3.1 KB

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