Skip to content

Conversation

@zasdfgbnm
Copy link
Collaborator

@zasdfgbnm zasdfgbnm commented Nov 11, 2019

Currently, the dynamic casting mechanism is implemented assuming no support of complex on GPU. This will no longer be true in the soon future.

#29547 could clear some clang warning but the complex support on GPU is still not complete:

  • fetch is not supported
  • casting between complex64 and complex128 is not supported
  • complex scalar types are not tested

This PR is what should be done for type promotion in order to add support to complex dtype on GPU, as suggested in #755 (comment)

Note that what is newly added here in this PR is not tested due to the lack of basic support of complex dtypes (I can not construct a complex tensor). But his PR shouldn't break any existing part of PyTorch.

For the merge this PR, consider two options:

  • We could merge this PR now so that @dylanbespalko could conveniently work based on master, if there is something wrong here not found by code review, @dylanbespalko would find when adding complex integration.
  • Or, we could just leave this PR open, don't merge it. But then @dylanbespalko might need to manually apply this to his branch in order to support type promotion of complex.

@zasdfgbnm zasdfgbnm requested a review from ezyang November 11, 2019 23:49
@zasdfgbnm zasdfgbnm mentioned this pull request Nov 11, 2019
10 tasks
Comment on lines 106 to 126
template<typename dest_t, typename complex_src_t>
struct MaybeReal {
static C10_HOST_DEVICE inline typename complex_src_t::value_type maybe_real(complex_src_t src) {
return src.real();
}
};

template<typename complex_src_t>
struct MaybeReal<std::complex<float>, complex_src_t> {
static C10_HOST_DEVICE inline complex_src_t maybe_real(complex_src_t src) {
return src;
}
};

template<typename complex_src_t>
struct MaybeReal<std::complex<double>, complex_src_t> {
static C10_HOST_DEVICE inline complex_src_t maybe_real(complex_src_t src) {
return src;
}
};

Copy link
Contributor

Choose a reason for hiding this comment

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

@zasdfgbnm

Thanks for adding MaybeReal. I'm surprised that other scalar_t types like Bool, Half, BFFloat16 can call the scalar_t::real() method, but I guess it worked for std::real(scalar_t)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dylanbespalko No, the way it is used makes sure that complex_src_t could only be std::complex<something_t>.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, you could really help me out by adding this change to c10/util/TypeCast.h::line48::52. I think it should be:

template <typename dest_t, typename src_t>
C10_HOST_DEVICE inline dest_t static_cast_with_inter_type(src_t src) {
  return static_cast<dest_t>(
    static_cast<inter_copy_type_t<dest_t>>(MaybeReal<dest_t, src_t>(src)));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see. Clever...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dylanbespalko Thanks for the review. That makes great sense. And I will add that.

@dylanbespalko
Copy link
Contributor

@zasdfgbnm, @ezyang

I would prefer if you accept these changes before mine. I will pull the update from master.

@zasdfgbnm
Copy link
Collaborator Author

@dylanbespalko I have moved logics into static_cast_with_inter_type. Could you please take another look and either approve or request change on this PR?

@dylanbespalko
Copy link
Contributor

@dylanbespalko I have moved logics into static_cast_with_inter_type. Could you please take another look and either approve or request change on this PR?

Looks good to me.

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.

@ezyang is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@zasdfgbnm zasdfgbnm deleted the enable-complex-for-dynamic-casting branch November 12, 2019 17:59
@facebook-github-bot
Copy link
Contributor

@ezyang merged this pull request in 450949c.

facebook-github-bot pushed a commit that referenced this pull request Nov 13, 2019
Summary:
In-tree changes to pytorch to support complex numbers are being submitted here.
Out-of-tree support for complex numbers is here: [pytorch-cpu-strided-complex extension](https://gitlab.com/pytorch-complex/pytorch-cpu-strided-complex)

- [x]  Replaced std:real(a) with a.real() in kernel level code.
- [x]  Fixed Vec256_base implementation of complex ops so that it works correctly on Non-AVX devices.
- [ ]  Clean up CopyKernel after #29612 is approved.
zasdfgbnm is fixing this issue in #29612. This should be added first.

cc: iotamudelta, ezyang, bddppq
Pull Request resolved: #29607

Differential Revision: D18451046

Pulled By: ezyang

fbshipit-source-id: b9dcd8e25e91cab13bd131b070d027b090cdedc9
zdevito pushed a commit to zdevito/ATen that referenced this pull request Nov 13, 2019
Summary:
In-tree changes to pytorch to support complex numbers are being submitted here.
Out-of-tree support for complex numbers is here: [pytorch-cpu-strided-complex extension](https://gitlab.com/pytorch-complex/pytorch-cpu-strided-complex)

- [x]  Replaced std:real(a) with a.real() in kernel level code.
- [x]  Fixed Vec256_base implementation of complex ops so that it works correctly on Non-AVX devices.
- [ ]  Clean up CopyKernel after pytorch/pytorch#29612 is approved.
zasdfgbnm is fixing this issue in pytorch/pytorch#29612. This should be added first.

cc: iotamudelta, ezyang, bddppq
Pull Request resolved: pytorch/pytorch#29607

Differential Revision: D18451046

Pulled By: ezyang

fbshipit-source-id: b9dcd8e25e91cab13bd131b070d027b090cdedc9
facebook-github-bot pushed a commit that referenced this pull request Nov 13, 2019
Summary:
After #29612 get merged, `static_cast_with_inter_type` can now automatically convert complex types to its real values, therefore there is no need to do it inside copy kernel.

This should wait until #29612 get merged, otherwise it won't pass CI.
Pull Request resolved: #29631

Differential Revision: D18485676

Pulled By: ezyang

fbshipit-source-id: 0bbfd551e3d3010f87eef0fce23a1f8a094b7d31
zdevito pushed a commit to zdevito/ATen that referenced this pull request Nov 13, 2019
Summary:
After pytorch/pytorch#29612 get merged, `static_cast_with_inter_type` can now automatically convert complex types to its real values, therefore there is no need to do it inside copy kernel.

This should wait until pytorch/pytorch#29612 get merged, otherwise it won't pass CI.
Pull Request resolved: pytorch/pytorch#29631

Differential Revision: D18485676

Pulled By: ezyang

fbshipit-source-id: 0bbfd551e3d3010f87eef0fce23a1f8a094b7d31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants