KEMBAR78
torch.complex and torch.polar by wjgan7 · Pull Request #39617 · pytorch/pytorch · GitHub
Skip to content

Conversation

@wjgan7
Copy link
Contributor

@wjgan7 wjgan7 commented Jun 6, 2020

@wjgan7
Copy link
Contributor Author

wjgan7 commented Jun 6, 2020

@anjali411 @ezyang Hi, I saw the update about having the polar keyword, but was wondering, is something like what I wrote along the right track to implement torch.complex?

@dr-ci
Copy link

dr-ci bot commented Jun 6, 2020

💊 CI failures summary and remediations

As of commit a481888 (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 97 times.

@ngimel ngimel added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 10, 2020
@wjgan7 wjgan7 changed the title Initial complex code torch.complex and torch.complex_polar Jun 12, 2020
@wjgan7
Copy link
Contributor Author

wjgan7 commented Jun 12, 2020

Hi @anjali411 @mruberry, made some updates. A couple question/comments,

  • Right now Vec256 isn't supported, but I'm not sure if it's possible since a real and imaginary Vec256<float> will lead to two Vec256<c10::complex<float>>'s.
  • How could I go about supporting the function for CUDA?
  • For the derivatives, I saw a lot of entries in derivatives.yaml don't use a _backward kernel. I'm thinking about just adding an entry like
- name: complex(Tensor real, Tensor imag) -> Tensor
  real: grad
  imag: grad * c10::complex<float>{0.0, 1.0}

- name: complex_polar(Tensor abs, Tensor angle) -> Tensor
  abs: grad * (angle.cos() + c10::complex<float>{0.0, 1.0} * angle.sin())
  angle: grad * abs * (- angle.sin() + c10::complex<float>{0.0, 1.0} * angle.cos())

However, would it be better to have specific complex_backward and complex_polar_backward methods that do a more optimized version of this? (essentially same way how complex and complex_polar kernels are implemented right now)

@mruberry
Copy link
Collaborator

Hi @anjali411 @mruberry, made some updates. A couple question/comments,

  • Right now Vec256 isn't supported, but I'm not sure if it's possible since a real and imaginary Vec256<float> will lead to two Vec256<c10::complex<float>>'s.
  • How could I go about supporting the function for CUDA?
  • For the derivatives, I saw a lot of entries in derivatives.yaml don't use a _backward kernel. I'm thinking about just adding an entry like
- name: complex(Tensor real, Tensor imag) -> Tensor
  real: grad
  imag: grad * c10::complex<float>{0.0, 1.0}

- name: complex_polar(Tensor abs, Tensor angle) -> Tensor
  abs: grad * (angle.cos() + c10::complex<float>{0.0, 1.0} * angle.sin())
  angle: grad * abs * (- angle.sin() + c10::complex<float>{0.0, 1.0} * angle.cos())

However, would it be better to have specific complex_backward and complex_polar_backward methods that do a more optimized version of this? (essentially same way how complex and complex_polar kernels are implemented right now)

  • It's OK to not support Vec256.
  • You're halfway there. Check out the binary CUDA kernels that use TensorIterator in aten/src/ATen/native/cuda. For example
    void add_kernel_cuda(TensorIterator& iter, Scalar alpha_scalar) {
    .
  • I think it's OK to not write a custom backward kernel for now. Let's focus on correctness.

@mruberry
Copy link
Collaborator

Also see: https://github.com/pytorch/pytorch/pull/40246/files, which might give you some ideas for manipulating the complex types.

@wjgan7
Copy link
Contributor Author

wjgan7 commented Jun 28, 2020

@mruberry @anjali411 sorry for just getting back to this. I think backward won't work altogether since it's fitting a complex gradient into a float input tensor. This was making me wonder how this function should work. Should it be able to accept complex inputs as well (and just use their real components)? It also made me wonder if this function would really be useful, since my guess is once x and y are both complex, the speed advantage over x + 1j * y wouldn't be that great.

@anjali411
Copy link
Contributor

anjali411 commented Jun 29, 2020

@mruberry @anjali411 sorry for just getting back to this. I think backward won't work altogether since it's fitting a complex gradient into a float input tensor. This was making me wonder how this function should work. Should it be able to accept complex inputs as well (and just use their real components)? It also made me wonder if this function would really be useful, since my guess is once x and y are both complex, the speed advantage over x + 1j * y wouldn't be that great.

@wjgan7 I think backward definition should be similar to that of view_as_complex ( cc. @albanD ).

The function should only be able to expect floating point tensors (cc. @mruberry). For cpu, you can define a function in this file: aten/src/ATen/native/cpu/zmath.h which can be passed to the cpu_kernel. The function would accept two floating points a and b and return a c10::complex<T>(a, b). check out this PR as an example: #39955

As Mike mentioned, let's skip Vec256 implementation for this PR.

@albanD
Copy link
Collaborator

albanD commented Jun 29, 2020

For the backward formula, I think we can just wait on the formula Anjali is going to put in the doc and apply it here. That will contain all the real to complex/complex to real changes that need to be applied.

@wjgan7
Copy link
Contributor Author

wjgan7 commented Jun 30, 2020

@anjali411 I believe I already have the non-Vec256 implementation in native/cpu/ComplexHelperKernel.cpp. I can move that to native/cpu/zmath.h though. Where do you think I should have the higher level functions? Currently they are in native/ComplexHelper.cpp. Oops, I misunderstood what you meant, will do.

@albanD Sounds good, I can just follow what @mruberry said about the CUDA implementation and write tests for that then.

@anjali411
Copy link
Contributor

@wjgan7 any updates?

For some reason old branch isn't compiling properly anymore
@wjgan7
Copy link
Contributor Author

wjgan7 commented Jul 8, 2020

@anjali411 Apologies for delays, will try to get an update soon.

Copy link
Contributor

@anjali411 anjali411 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after the autograd tests have been moved from test_torch.py to test_autograd.py.

cc. @mruberry to take a final look!

Copy link
Contributor

@anjali411 anjali411 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synced with @albanD offline. We shouldn't add any new autograd formulas until JAX vs tf autograd issue is resolved so we should just add not_implemented("fn_name") in derivatives.yaml definitions.

@wjgan7
Copy link
Contributor Author

wjgan7 commented Aug 11, 2020

@anjali411 Not sure if I can view the failing CircleCI tests. Do you know what the issue is?

@anjali411
Copy link
Contributor

@wjgan7 they are just upstream failures, but if you rebase on the latest master, CI should be green.

Copy link
Contributor

@anjali411 anjali411 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :) thanks for the awesome work @wjgan7

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anjali411 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but please fix the "{out}" in the two doc updates.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anjali411 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@anjali411 merged this pull request in e437308.

@wjgan7
Copy link
Contributor Author

wjgan7 commented Aug 14, 2020

@anjali411 @mruberry Thanks for all the reviews, I learned a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants