Skip to content

Conversation

@anjali411
Copy link
Contributor

@anjali411 anjali411 commented Aug 7, 2020

Stack from ghstack:

Differential Revision: D23056382

@anjali411 anjali411 requested review from ezyang and zasdfgbnm August 7, 2020 19:52
@anjali411 anjali411 added the module: complex Related to complex number support in PyTorch label Aug 7, 2020
@dr-ci
Copy link

dr-ci bot commented Aug 7, 2020

💊 CI failures summary and remediations

As of commit 1ff598c (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 64 times.

TODO: potentially add a fast path for complex dot

[ghstack-poisoned]
anjali411 added a commit that referenced this pull request Aug 7, 2020
ghstack-source-id: 1cd2eab
Pull Request resolved: #42745
TODO: potentially add a fast path for complex dot

[ghstack-poisoned]
TODO: potentially add a fast path for complex dot

[ghstack-poisoned]
anjali411 added a commit that referenced this pull request Aug 10, 2020
ghstack-source-id: 54e1eb7
Pull Request resolved: #42745

#if AT_BUILD_WITH_BLAS()
extern "C" double ddot_(int *n, double *x, int *incx, double *y, int *incy);
extern "C" void zdotu_(std::complex<double> *res, int *n, std::complex<double> *x, int *incx, std::complex<double> *y, int *incy);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://www.math.utah.edu/software/c-with-fortran.html#function-return-types

"...you should not expect to use Fortran functions that return types such as COMPLEX or COMPLEX*16. Write a SUBROUTINE interface to your Fortran function instead, and then invoke it as a void function from C or C++."

@anjali411 anjali411 requested review from zasdfgbnm and removed request for zasdfgbnm August 10, 2020 15:28
@anjali411 anjali411 changed the title Add dot product for complex tensors Add torch.dot for complex tensors Aug 10, 2020
anjali411 added a commit that referenced this pull request Aug 10, 2020
ghstack-source-id: 9357f67
Pull Request resolved: #42745

template <>
void dot<c10::complex<double>>(CUDABLAS_DOT_ARGTYPES(c10::complex<double>)) {
TORCH_CUDABLAS_CHECK(cublasZdotu(handle, n, reinterpret_cast<const cuDoubleComplex*>(x),
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I wonder if we shouldn't have some methods on c10::complex for doing pointery conversions like this. It would be nice to not have to be slinging reinterpret cast everywhere. (No action needed for PR)

anjali411 added a commit that referenced this pull request Aug 11, 2020
ghstack-source-id: 5d429e2
Pull Request resolved: #42745
anjali411 added a commit that referenced this pull request Aug 11, 2020
ghstack-source-id: f1c84e4
Pull Request resolved: #42745
anjali411 added a commit that referenced this pull request Aug 12, 2020
ghstack-source-id: dd93668
Pull Request resolved: #42745
@jeffdaily
Copy link
Collaborator

@anjali411 as discussed offline, the reason CPU results for the ROCm build are failing is because zdotu_ and cdotu_ symbols from our openblas install are getting linked and called instead of the static inline versions that wrap the cblas calls. Suggested change:

diff --git a/aten/src/ATen/native/BlasKernel.cpp b/aten/src/ATen/native/BlasKernel.cpp
index ef05cb8..1fe8a73 100644
--- a/aten/src/ATen/native/BlasKernel.cpp
+++ b/aten/src/ATen/native/BlasKernel.cpp
@@ -17,9 +17,6 @@ extern "C" void sgemv_(char *trans, int *m, int *n, float *alpha, float *a, int
 # define ffloat float
 #endif

-extern "C" ffloat sdot_(int *n, float *x, int *incx, float *y, int *incy);
-extern "C" void cdotu_(std::complex<float> *res, int *n, std::complex<float> *x, int *incx, std::complex<float> *y, int *incy);
-extern "C" void zdotu_(std::complex<double> *res, int *n, std::complex<double> *x, int *incx, std::complex<double> *y, int *incy);

 #ifdef BLAS_USE_CBLAS_DOT
 extern "C" float cblas_sdot(const int n, const float *x, const int incx, const float *y, const int incy);
@@ -40,6 +37,10 @@ static inline void zdotu_(std::complex<double> *res, const int *n, const std::co
   cblas_zdotu_sub(*n, x, *incx, y, *incy, res);
 }
 #endif // THBlas_cblas_dot_
+#else // BLAS_USE_CBLAS_DOT
+extern "C" ffloat sdot_(int *n, float *x, int *incx, float *y, int *incy);
+extern "C" void cdotu_(std::complex<float> *res, int *n, std::complex<float> *x, int *incx, std::complex<float> *y, int *incy);
+extern "C" void zdotu_(std::complex<double> *res, int *n, std::complex<double> *x, int *incx, std::complex<double> *y, int *incy);
 #endif // BLAS_USE_CBLAS_DOT
 #endif // AT_BUILD_WITH_BLAS

@jeffdaily
Copy link
Collaborator

BLAS development began in fortran, and the function calling convention differs between compilers and compiler flags, making it difficult to get correct when calling the fortran function from C. Sometimes the complex value is returned as a hidden positional argument as in your extern declaration, and sometimes it is returned like a regular C function call return. Since the openblas symbols were getting linked, and their extern declaration was incorrect, the function was getting called but there was a mismatch between the expected function signature and how it was declared.

anjali411 added a commit that referenced this pull request Aug 13, 2020
ghstack-source-id: dbad493
Pull Request resolved: #42745
@kshitij12345 kshitij12345 mentioned this pull request Aug 17, 2020
anjali411 added a commit that referenced this pull request Aug 17, 2020
ghstack-source-id: 91158b7
Pull Request resolved: #42745
@facebook-github-bot
Copy link
Contributor

@anjali411 merged this pull request in aab6660.

TH_EXTERNC void cblas_cdotu_sub(const int n, const void *x, const int incx, const void *y, const int incy, void *dotu);
TH_EXTERNC void cblas_zdotu_sub(const int n, const void *x, const int incx, const void *y, const int incy, void *dotu);

#ifndef THBlas_cblas_dot_
Copy link
Contributor

Choose a reason for hiding this comment

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

why are these symbols being defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed in #43148

@facebook-github-bot facebook-github-bot deleted the gh/anjali411/49/head branch August 21, 2020 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: complex Related to complex number support in PyTorch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants