-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Doc note for complex #41252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doc note for complex #41252
Conversation
[ghstack-poisoned]
docs/source/complex_numbers.rst
Outdated
| Autograd Support (with and without view_as_real, view_as_complex) | ||
| ----------------------------------------------------------------- | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here might be a good places to talk about holomorphic functions. Maybe the "upshot" section of https://jax.readthedocs.io/en/latest/notebooks/autodiff_cookbook.html#Complex-numbers-and-differentiation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we just add it to the autograd doc and link that doc here?
docs/source/complex_numbers.rst
Outdated
| JIT | ||
| distributions | ||
|
|
||
| multiprocessing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any reason why this wouldn't work
docs/source/complex_numbers.rst
Outdated
|
|
||
| multiprocessing | ||
|
|
||
| distributed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If distributed doesn't work, it doesn't for any good reason, and we should fix it (shouldn't be hard)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Synced with @mrshenli and we will have to add mappings between complex and float dtypes for data transfer (because for eg. nccl doesn't support complex dtypes) and add some tests to ensure that it works correctly. These changes will be included as part of 1.7 because it's too last minute to add it in 1.6.
docs/source/complex_numbers.rst
Outdated
| by using Float Tensors with shape :math:`(..., 2)` where the last dimension contains the real and imaginary values. | ||
|
|
||
| We support many functions for Complex Tensors eg. SVD, QR etc. Operations on complex tensors are likely to be | ||
| faster than operations on float tensors mimicking them. We also have a `Vec256 class` for `ComplexFloat` and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vec256 is a bit internal and I probably wouldn't mention it in a user doc like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also ComplexFloat and ComplexDouble haven't been defined yet, but they also sound internal.
You can just say something like "Operations involving complex numbers in PyTorch are optimized to use vectorized assembly instructions and specialized kernels (e.g. LAPACK, CuBlas)."
I'm not really sure why you want to compare users writing their own functions -- why would they do that?
[ghstack-poisoned]
💊 CI failures summary and remediationsAs of commit cbc2df7 (more details on the Dr. CI page):
🕵️ 2 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
docs/source/complex_numbers.rst
Outdated
|
|
||
| Complex Numbers are numbers that can be expressed in the form :math:`a + bi`, where a and b are real numbers, | ||
| and *i* is a solution of the equation :math:`x^2 = −1`. Complex numbers frequently occur in mathematics and | ||
| engineering, especially in signal processing. The aim of introducing Complex Tensors is to provide a more natural |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this sentence describes the current state, but it's going to be out of date soon after you write it (i.e., we'll have complex support and torchaudio won't have hacks. You can just say something like "Complex Tensors are provided..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it's a little wordier, but "Tensors of complex dtype" is more descriptive/accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I believe "workaround" is two words when used as a verb.
docs/source/complex_numbers.rst
Outdated
| by using Float Tensors with shape :math:`(..., 2)` where the last dimension contains the real and imaginary values. | ||
|
|
||
| We support many functions for Complex Tensors eg. SVD, QR etc. Operations on complex tensors are likely to be | ||
| faster than operations on float tensors mimicking them. We also have a `Vec256 class` for `ComplexFloat` and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also ComplexFloat and ComplexDouble haven't been defined yet, but they also sound internal.
You can just say something like "Operations involving complex numbers in PyTorch are optimized to use vectorized assembly instructions and specialized kernels (e.g. LAPACK, CuBlas)."
I'm not really sure why you want to compare users writing their own functions -- why would they do that?
docs/source/complex_numbers.rst
Outdated
|
|
||
| <add code> | ||
|
|
||
| The following factory functions can be used to create complex tensors: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are there any factory functions that dont support complex tensors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
torch.arange, torch.range, torch.linspace, torch.logspace, torch.quantize_per_tensor, torch.quantize_per_channel
do you think it's better to just have a list of the factory functions not supported instead?
docs/source/complex_numbers.rst
Outdated
| Autograd | ||
| -------- | ||
|
|
||
| PyTorch supports Autograd for Complex Numbers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the core of this should go in the autograd section and you should link it from here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so should we move everything mentioned here (including the example) to autograd mechanics as well?
cc. @albanD
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is already in the autograd doc as well.
The list explaining what you get for different type of functions is good to have here I think.
Not sure what the code sample brings though.
[ghstack-poisoned]
docs/source/complex_numbers.rst
Outdated
| Complex Numbers | ||
| =============== | ||
|
|
||
| Complex Numbers are numbers that can be expressed in the form :math:`a + bi`, where a and b are real numbers, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that you use j in the autograd part of the doc. Maybe you want to make that consistent.
docs/source/complex_numbers.rst
Outdated
|
|
||
| Users who currently worked around the lack of complex tensors with real tensors of shape `(..., 2)` | ||
| can easily to switch using the complex tensors in their code using :func:`torch.view_as_complex` and | ||
| - :func:`torch.view_as_real` view functions: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that just saying "view functions" makes it clear to the user that these are views. Maybe you want to insist on it a bit more as it is fairly important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...:func:torch.view_as_complex and - :func:torch.view_as_real functions which involve zero copy. does that look better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or add a new setence: Note that these functions don't perform any copy and return a view of the input Tensor.
docs/source/complex_numbers.rst
Outdated
| For non-holomorphic functions, the gradient is evaluated as if it were holomorphic. | ||
| 2. :func:`torch.functional.backward` can be used to optimize :math:`C -> R` functions, like | ||
| real-values loss functions of complex parameters :math:`x` by taking steps in the direction | ||
| of conjugate of :math:`x.grad`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to double check the conjugate here!
Also if this is true, we might want to update our optimizers to do that when the parameters are complex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that's correct! the gradient definition that we are using does specify that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's already in the gradient definition, no need to do it again here.
docs/source/complex_numbers.rst
Outdated
|
|
||
| We do not support the following subsystems: | ||
|
|
||
| Quantization |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bullet list this?
|
what's the binary pt file? |
docs/source/complex_numbers.rst
Outdated
| and *i* is a solution of the equation :math:`x^2 = −1`. Complex numbers frequently occur in mathematics and | ||
| engineering, especially in signal processing. The aim of introducing tensors of complex dtypes is to provide | ||
| a more natural user experience for users and libraries (eg. TorchAudio) that currently work around the | ||
| lack of complex tensors by using Float Tensors with shape :math:`(..., 2)` where the last dimension contains |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like it's underselling the feature. The goal is to not just replace existing workarounds, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider introducing these existing workarounds in its own section later. Maybe a section like: "adapting existing code"?
| [-0.3773, 1.3487], | ||
| [-0.0861, -0.7981]]) | ||
|
|
||
| Accessing real and imag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really about acquiring just the real or imaginary part of the tensor, but the title seems like it's about the attributes themselves
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry I am not sure what you mean. can you provide an alternate suggestion?
[ghstack-poisoned]
sorry that got added by mistake while I was just locally testing save and load. removed it! |
[ghstack-poisoned]
[ghstack-poisoned]
docs/source/complex_numbers.rst
Outdated
| >>> x.grad, y.grad | ||
| tensor([-0.6815+0.5931j, 0.5333-1.0872j]) tensor([-0.4869+0.9011j, 0.3673+0.2007j]) | ||
| >>> x_.grad, y_.grad | ||
| tensortensor([-0.6815+0.5931j, 0.5333-1.0872j]) tensor([-0.4869+0.9011j, 0.3673+0.2007j]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like a copy paste bug tensortensor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed! (also updated the example to make it more readable)
ezyang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okey
docs/source/complex_numbers.rst
Outdated
| and *j* is a solution of the equation :math:`x^2 = −1`. Complex numbers frequently occur in mathematics and | ||
| engineering, especially in signal processing. Tensors of complex dtypes provide a more natural user experience | ||
| for users and libraries (eg. TorchAudio) that previously worked around the lack of complex tensors by using | ||
| float tensors with shape :math:`(..., 2)` where the last dimension contained the real and imaginary values. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: there still feels like there is some tense confusion here. Part of it is that there are plenty of libraries, TODAY, that represent complex as float tensors. So it should be present tense. Consider something like, "Tensors of complex dtypes provide a more natural user experience for working with complex numbers, compared with maintaining float tensors with shape :math:(..., 2) where the last dimension contains the real and imaginary values."
docs/source/complex_numbers.rst
Outdated
| memory efficient than operations on float tensors mimicking them. Operations involving complex numbers in | ||
| PyTorch are optimized to use vectorized assembly instructions and specialized kernels (e.g. LAPACK, CuBlas). | ||
| Thus using functions for complex tensors will provide performance benefits as opposed to users defining | ||
| their own functions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: You probably don't actually need this sentence
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed!
docs/source/complex_numbers.rst
Outdated
| .. note:: | ||
|
|
||
| The default dtype for complex tensors is determined by the default floating point dtype. | ||
| If the default floating point dtype is torch.float64 then complex numbers are inferred to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: backtick the code fragments
docs/source/complex_numbers.rst
Outdated
|
|
||
| PyTorch supports Autograd for Complex Tensors. The autograd APIs can be | ||
| used for both holomorphic and non-holomorphic functions. For non-holomorphic | ||
| functions, the gradient is evaluated as if it were holomorphic. For more details, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if it's meaningful to say "evaluated as if it were holomorphic"; what does that even mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha I meant to say the gradient evaluation for non-holomorphic functions are done assuming the input function is holomorphic. I'll re-phrase to make it clear
[ghstack-poisoned]
[ghstack-poisoned]
| All factory functions apart from :func:`torch.linspace`, :func:`torch.logspace`, and :func:`torch.arange` are | ||
| supported for complex tensors. | ||
|
|
||
| Transition from the old representation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you consolidate all the historical information in this section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah however I think some historical context in the introduction sets a better stage to truly justify "more natural user experience" and "faster and more memory efficient than operations on float tensors mimicking them".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has anyone made the argument that mimicking complex numbers is a better user experience? faster? I haven't personally come across those arguments, my impression is that everyone recognizes the previous behavior as a hack.
docs/source/complex_numbers.rst
Outdated
| :: | ||
| >>> # computes the complex dot product for complex vectors | ||
| >>> # represented as float vectors | ||
| >>> # math: for complex numbers a and b vdot(a, b) = a.conj() * b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
torch.sum(a.conj() * b)) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
synced offline: remove the code block from autograd section until we have a clear definition of functions that can be used with view_as_real and view_as_complex and for which we can ensure correct gradient computation.
[ghstack-poisoned]
Differential Revision: [D22553266](https://our.internmc.facebook.com/intern/diff/D22553266) [ghstack-poisoned]
|
@anjali411 merged this pull request in b9442bb. |
Summary: Pull Request resolved: #41252 Test Plan: Imported from OSS Reviewed By: albanD Differential Revision: D22553266 Pulled By: anjali411 fbshipit-source-id: f6dc409da048496d72b29b0976dfd3dd6645bc4d
Stack from ghstack:
Differential Revision: D22553266