Commit Graph

21 Commits

Author SHA1 Message Date
Scott Wolchok
44cc873fba [PyTorch] Autoformat c10 (#56830)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56830

Opt into formatting on GitHub and format everything. This is a trial run before turning on formatting for more and eventually all of the codebase.

Test Plan: CI

Reviewed By: zertosh

Differential Revision: D27979080

fbshipit-source-id: a80f0c48691c08ae8ca0af06377b87e6a2351151
2021-04-30 21:23:28 -07:00
Nikita Shulga
91a809bbd7 [c10] Adjust macro check that detects if glibc++ use c99 csqrt (#55177)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55177

This fixes `warning: '_GLIBCXX11_USE_C99_COMPLEX' is not defined, evaluates to 0`, that would be raised if https://github.com/pytorch/pytorch/pull/54820 used with libstd++ compiled without USE_C99_COMPLEX support.

In `c++config.h` `_GLIBCXX_USE_C99_COMPLEX` is aliased to either `_GLIBCXX98_USE_C99_COMPLEX` or `_GLIBCXX11_USE_C99_COMPLEX` depending on  `__cplusplus` macro, as shown here:
0cf4813202/libstdc%2B%2B-v3/include/bits/c%2B%2Bconfig (L641-L647)

Abovementioned config file is generated by autoconf, that leaves macro undefined if feature is not used, so using conditional like `defined(_GLIBCXX_USE_C99_COMPLEX) && _GLIBCXX_USE_C99_COMPLEX == 0` would trigger undefined macro preprocessor warning.

Test Plan: CI

Reviewed By: Orvid

Differential Revision: D27517788

fbshipit-source-id: a6db98d21c9bd98205815641363b765a02399678
2021-04-02 13:20:30 -07:00
Orvid King
07602bf7e1 [caffe2] Use the CXX11 version of the USE_C99_COMPLEX macro
Summary: Because the bare CXX version forwards to this without checking if it's defined causing errors for builds with -Wundef enabled

Test Plan: contbuilds

Differential Revision: D27443462

fbshipit-source-id: 554a3c653aae14d19e35038ba000cf5330e6d679
2021-03-31 12:54:47 -07:00
Nikita Shulga
68af6d9565 Use custom sqrt if stdc++ does not fall back to C99 csqrt (#54820)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/54820

template implementation of std::sqrt() in libstdc++ yields incorrect results for `std::complex(-std::abs(x), -0.0)`, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89991
For example:
```
#include <iostream>
#include <complex>
int main() {
  std::cout << std::sqrt(std::complex<float>(-1.0f, -0.0f)) << std::endl;
}
```
prints `(0, -1)` if libstdc++ is compiled to use C99 csqrt/csqrtf fallback, but `(0, 1)` if configured not to use it.

Test Plan: CI

Reviewed By: luciang

Differential Revision: D27379302

fbshipit-source-id: 03f614fdb7ff734139736a2a5f6872cee0173bee
2021-03-29 09:05:48 -07:00
Nikita Shulga
4305609d66 Fix complex acos edge cases (#52287)
Summary:
Use `std::acos` even when avx2 is available
Add slow but accurate implementation of complex arc cosine based on
W. Kahan "Branch Cuts for Complex Elementary Functions" paper, where
cacos(z).re = 2*atan2(sqrt(1-z).re(), sqrt(1+z).re())
cacos(z).im = asinh((sqrt(conj(1+z))*sqrt(1-z)).im())

Fixes https://github.com/pytorch/pytorch/issues/42952

Pull Request resolved: https://github.com/pytorch/pytorch/pull/52287

Reviewed By: walterddr

Differential Revision: D26455027

Pulled By: malfet

fbshipit-source-id: a81ce1ba4953eff4d3c2a265ef9199896a67b240
2021-02-17 11:36:09 -08:00
Nikita Shulga
497b772547 Add custom implementation for csqrt if libc++ is used (#52018)
Summary:
libc++ implements csqrt using polar form of the number, which results in higher numerical error, if `arg` is close to 0, pi/2, pi, 3pi/4

Fixes https://github.com/pytorch/pytorch/issues/47500

Pull Request resolved: https://github.com/pytorch/pytorch/pull/52018

Reviewed By: walterddr

Differential Revision: D26359947

Pulled By: malfet

fbshipit-source-id: 8c9f4dc45948cb29c43230dcee9b030c2642d981
2021-02-11 11:53:52 -08:00
Xiang Gao
c7d79f35e3 Header rename complex_type.h -> complex.h (#39885)
Summary:
This file should have been renamed as `complex.h`, but unfortunately, it was named as `complex_type.h` due to a name clash with FBCode. Is this still the case and is it easy to resolve the name clash? Maybe related to the comment at https://github.com/pytorch/pytorch/pull/39834#issuecomment-642950012
Pull Request resolved: https://github.com/pytorch/pytorch/pull/39885

Differential Revision: D22018575

Pulled By: ezyang

fbshipit-source-id: e237ccedbe2b30c31aca028a5b4c8c063087a30f
2020-06-23 16:27:09 -07:00
Gao, Xiang
dea58a7660 [resubmit] Kill thrust::complex from log kernels (#40079)
Summary:
Use `::log` instead of `std::log` for better ROCm support.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/40079

Differential Revision: D22068554

Pulled By: pbelevich

fbshipit-source-id: a458ae34535a641832f816617387a45445e2fa48
2020-06-17 05:57:10 -07:00
Xiang Gao
eb358f49c2 Overload complex math functions on both :: and std:: (#39829)
Summary:
Because ROCm has bug on std:: functions.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/39829

Differential Revision: D22018430

Pulled By: anjali411

fbshipit-source-id: 671e158d3e3342394d1deaebd7ff011cce94c31a
2020-06-15 16:53:16 -07:00
Pavel Belevich
cf64af1ad2 Revert D22036002: [pytorch][PR] Kill thrust::complex from log kernels
Test Plan: revert-hammer

Differential Revision:
D22036002

Original commit changeset: 8852a833a0c7

fbshipit-source-id: 36d3c8d0e489f8a11a6e3e9d1ae162c192748037
2020-06-14 15:30:48 -07:00
Xiang Gao
4947ee3811 Kill thrust::complex from log kernels (#39902)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/39902

Differential Revision: D22036002

Pulled By: pbelevich

fbshipit-source-id: 8852a833a0c71343ae630754f00da35a66e05917
2020-06-14 11:44:28 -07:00
Hong Xu
df2d19723a c10/util/complex_math.h and c10/util/complex_utils.h should not be individually included (#39276)
Summary:
Add a compilation error if they are individually included. Devs should
instead include c10/util/complex_type.h (which includes these two files).
Pull Request resolved: https://github.com/pytorch/pytorch/pull/39276

Differential Revision: D21924922

Pulled By: ezyang

fbshipit-source-id: ad1034be5d9d694b18cc5f03a44f540f10de568c
2020-06-08 11:52:18 -07:00
Hong Xu
413f023784 Clean up cast from c10::complex<T> to thrust::complex<T>, and update the workaround CUDA version to <10.2 (#38941)
Summary:
I'm using CUDA 10.1 on Debian buster but I can still experience
compilation issues:

```
/usr/include/thrust/detail/complex/complex.inl(64): error: no suitable conversion function from "const c10::complex<float>" to "float" exists
          detected during:
            instantiation of "thrust::complex<T>::complex(const R &) [with T=float, R=c10::complex<float>]"
/home/hong/xusrc/pytorch/c10/util/complex_type.h(503): here
            instantiation of "T std::abs(const c10::complex<T> &) [with T=float]"
/home/hong/xusrc/pytorch/aten/src/ATen/native/cuda/AbsKernel.cu(17): here
            instantiation of "c10::complex<T> at::native::abs_wrapper(c10::complex<T>) [with T=float]"
/home/hong/xusrc/pytorch/aten/src/ATen/native/cuda/AbsKernel.cu(29): here

/usr/include/thrust/detail/complex/complex.inl(64): error: no suitable conversion function from "const c10::complex<double>" to "double" exists
          detected during:
            instantiation of "thrust::complex<T>::complex(const R &) [with T=double, R=c10::complex<double>]"
/home/hong/xusrc/pytorch/c10/util/complex_type.h(503): here
            instantiation of "T std::abs(const c10::complex<T> &) [with T=double]"
/home/hong/xusrc/pytorch/aten/src/ATen/native/cuda/AbsKernel.cu(17): here
            instantiation of "c10::complex<T> at::native::abs_wrapper(c10::complex<T>) [with T=double]"
/home/hong/xusrc/pytorch/aten/src/ATen/native/cuda/AbsKernel.cu(29): here

2 errors detected in the compilation of "/tmp/hong/tmpxft_00005893_00000000-6_AbsKernel.cpp1.ii".
CMake Error at torch_cuda_generated_AbsKernel.cu.o.Debug.cmake:281 (message):
  Error generating file
  /home/hong/xusrc/pytorch/build/caffe2/CMakeFiles/torch_cuda.dir/__/aten/src/ATen/native/cuda/./torch_cuda_generated_AbsKernel.cu.o
```

`nvcc --version`:

```
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Apr_24_19:10:27_PDT_2019
Cuda compilation tools, release 10.1, V10.1.168
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/38941

Differential Revision: D21818790

Pulled By: ezyang

fbshipit-source-id: a4bfcd8ae701f7c214bea0731c13a5f3587b7a98
2020-06-02 06:47:42 -07:00
Michael Ranieri
563bbeb890 fix undef CUDA_VERSION warning (#37866)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/37866

make sure not to check `CUDA_VERSION` if it is not defined

Test Plan: CI gree

Reviewed By: anjali411

Differential Revision: D21408844

fbshipit-source-id: 5a9afe372b3f1fbaf08a7c43fa3e0e654a569d5f
2020-05-05 16:31:24 -07:00
Xiang Gao
090ea775c9 Math functions of c10::complex should be overloaded as const reference (#37689)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/37689

It has to be this way, otherwise, we will not be able to use it in vec256 because the function pointers declared there are using const reference.

Test Plan: Imported from OSS

Differential Revision: D21394603

Pulled By: anjali411

fbshipit-source-id: daa075b86daaa694489c883d79950a41d6e996ba
2020-05-04 19:59:28 -07:00
Gao, Xiang
c5624e831d Add overloads of std:: math functions for c10::complex [resubmit] (#37468)
Summary:
This reverts commit d167a7f654.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/37468

Differential Revision: D21305110

Pulled By: anjali411

fbshipit-source-id: d1bdc9d9feac00331fc2b2b905d49f80bef680f9
2020-04-30 10:20:45 -07:00
Lu Fang
d167a7f654 Revert D21256854: [pytorch][PR] Add overloads of std:: math functions for c10::complex
Test Plan: revert-hammer

Differential Revision:
D21256854

Original commit changeset: 2112ba6b7992

fbshipit-source-id: b81c377f9cd33a493a63d1e666cbe6765516fca8
2020-04-27 13:23:34 -07:00
Gao, Xiang
6d409481b3 Add overloads of std:: math functions for c10::complex (#35725)
Summary:
Issue: https://github.com/pytorch/pytorch/issues/35284

~This depends on and contains https://github.com/pytorch/pytorch/pull/35524. Please review after the dependency gets merged and I will rebase to get a clean diff.~

The implementation of most functions follow the pattern

```C++
template<typename T>
C10_HOST_DEVICE c10::complex<T> some_function(c10::complex<T> x) {
#if defined(__CUDACC__) || defined(__HIPCC__)
  return static_cast<c10::complex<T>>(thrust::some_function(static_cast<thrust::complex<T>>(x)));
#else
  return static_cast<c10::complex<T>>(std::some_function(static_cast<std::complex<T>>(x)));
#endif
}
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/35725

Differential Revision: D21256854

Pulled By: ezyang

fbshipit-source-id: 2112ba6b79923450feafd7ebdc7184a3eaecadb6
2020-04-27 10:32:16 -07:00
Xiang Gao
20328f67bb Add core of c10::complex [resubmit] (#36626)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/36626

This reverts commit 9216c67c9e.

Test Plan: Imported from OSS

Differential Revision: D21140441

Pulled By: anjali411

fbshipit-source-id: 488530088e2ff87dc27e70d21ace88ff2967e7ab
2020-04-24 12:08:23 -07:00
Mike Ruberry
9216c67c9e Revert D21021677: [pytorch][PR] Add core of c10::complex
Test Plan: revert-hammer

Differential Revision:
D21021677

Original commit changeset: 9e144e581fa4

fbshipit-source-id: ce6a88fc71ec0134d0fc6ecdddc4c4db35f89b1f
2020-04-14 13:58:24 -07:00
Xiang Gao
25252816cf Add core of c10::complex (#35524)
Summary:
Step 0 of https://github.com/pytorch/pytorch/issues/35284

Reference: https://en.cppreference.com/w/cpp/numeric/complex
We are targeting C++20. The difference across C++ versions are mostly `constexpr` qualifiers, newer version has more function declared as `constexpr`

This PR adds the core of `c10::complex`, it includes
- standard constructors as in `std::complex`
- explicit conversion constructors converting from `std/thrust::complex` to `c10::complex`
- standard assignment operators as in `std::complex`
- conversion assignment operators converting from `std/thrust::complex` to `c10::complex`
- other standard operators as in `std::complex`
- standard methods as in `std::complex`
- explicit casting operators to std/thrust
- basic non-member functions as in `std::complex`:
  - arithmetic operators
  - `==`, `!=`
  - `<<`, `>>`
  - `std::real`, `std::imag`, `std::abs`, `std::arg`, `std::norm`, `std::conj`, `std::proj`, `std::polar`
    - Some of them are intentionally not completely implemented, these are marked as `TODO` and will be implemented in the future.

This PR does not include:
- overload of math functions

which will come in the next PR
Pull Request resolved: https://github.com/pytorch/pytorch/pull/35524

Differential Revision: D21021677

Pulled By: anjali411

fbshipit-source-id: 9e144e581fa4b2bee62d33adaf756ce5aadc0c71
2020-04-14 11:00:24 -07:00