Skip to content

[pytorch][PR] Conversions to and from complex numbers.#11420

Closed
ezyang wants to merge 4 commits intomasterfrom
export-D9697878
Closed

[pytorch][PR] Conversions to and from complex numbers.#11420
ezyang wants to merge 4 commits intomasterfrom
export-D9697878

Conversation

@ezyang
Copy link
Contributor

@ezyang ezyang commented Sep 8, 2018

Stack:
    :black_circle:  #11420 [pytorch][PR] Conversions to and from complex numbers.  💚

Surprisingly tricky! Here are the major pieces:

  • We grow a even yet more ludicrous macro
    AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF
    which does what it says on the tin. This is because I was
    too lazy to figure out how to define the necessary conversions
    in and out of ComplexHalf without triggering ambiguity problems.
    It doesn't seem to be as simple as just Half. Leave it for
    when someone actually wants this.

  • Scalar now can hold std::complex. Internally, it is
    stored as double[2] because nvcc chokes on a non-POD type
    inside a union.

  • overflow() checking is generalized to work with complex.
    When converting to std::complex, all we need to do is check
    for overflow against T. When converting from complex, we
    must check (1) if To is not complex, that imag() == 0
    and (2) for overflow componentwise.

  • convert() is generalized to work with complex<->real conversions.
    Complex to real drops the imaginary component; we rely on
    overflow checking to tell if this actually loses fidelity. To get
    the specializations and overloads to work out, we introduce
    a new Converter class that actually is specializable.

  • Complex scalars convert into Python complex numbers

  • This probably fixes complex tensor printing, but there is no way
    to test this right now.

Signed-off-by: Edward Z. Yang ezyang@fb.com
GitHub Author: Edward Z. Yang ezyang@fb.com

Differential Revision: D9697878

Differential Revision: D9697878
Differential Version: 57285377
Differential Revision: D9697878
Differential Version: 57300313
Differential Revision: D9697878
Differential Version: 57307194
Differential Revision: D9697878
Differential Version: 57307800
zdevito pushed a commit to zdevito/ATen that referenced this pull request Sep 9, 2018
Summary:
Pull Request resolved: pytorch/pytorch#11420

Surprisingly tricky!  Here are the major pieces:

- We grow a even yet more ludicrous macro
  AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF
  which does what it says on the tin.  This is because I was
  too lazy to figure out how to define the necessary conversions
  in and out of ComplexHalf without triggering ambiguity problems.
  It doesn't seem to be as simple as just Half.  Leave it for
  when someone actually wants this.

- Scalar now can hold std::complex<double>.  Internally, it is
  stored as double[2] because nvcc chokes on a non-POD type
  inside a union.

- overflow() checking is generalized to work with complex.
  When converting *to* std::complex<T>, all we need to do is check
  for overflow against T.  When converting *from* complex, we
  must check (1) if To is not complex, that imag() == 0
  and (2) for overflow componentwise.

- convert() is generalized to work with complex<->real conversions.
  Complex to real drops the imaginary component; we rely on
  overflow checking to tell if this actually loses fidelity. To get
  the specializations and overloads to work out, we introduce
  a new Converter class that actually is specializable.

- Complex scalars convert into Python complex numbers

- This probably fixes complex tensor printing, but there is no way
  to test this right now.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Reviewed By: cpuhrsch

Differential Revision: D9697878

Pulled By: ezyang

fbshipit-source-id: 181519e56bbab67ed1e5b49c691b873e124d7946
PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this pull request Sep 11, 2018
Summary:
Pull Request resolved: pytorch#11420

Surprisingly tricky!  Here are the major pieces:

- We grow a even yet more ludicrous macro
  AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF
  which does what it says on the tin.  This is because I was
  too lazy to figure out how to define the necessary conversions
  in and out of ComplexHalf without triggering ambiguity problems.
  It doesn't seem to be as simple as just Half.  Leave it for
  when someone actually wants this.

- Scalar now can hold std::complex<double>.  Internally, it is
  stored as double[2] because nvcc chokes on a non-POD type
  inside a union.

- overflow() checking is generalized to work with complex.
  When converting *to* std::complex<T>, all we need to do is check
  for overflow against T.  When converting *from* complex, we
  must check (1) if To is not complex, that imag() == 0
  and (2) for overflow componentwise.

- convert() is generalized to work with complex<->real conversions.
  Complex to real drops the imaginary component; we rely on
  overflow checking to tell if this actually loses fidelity. To get
  the specializations and overloads to work out, we introduce
  a new Converter class that actually is specializable.

- Complex scalars convert into Python complex numbers

- This probably fixes complex tensor printing, but there is no way
  to test this right now.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Reviewed By: cpuhrsch

Differential Revision: D9697878

Pulled By: ezyang

fbshipit-source-id: 181519e56bbab67ed1e5b49c691b873e124d7946
@soumith soumith deleted the export-D9697878 branch February 21, 2019 23:25
@ezyang ezyang added the merged label Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant