Skip to content

Conversation

@animesht
Copy link

#7102

Moved _gesv_single and _gesv_single_out to ATen

@animesht animesht changed the title [1702][fb_bootcamp] Moved Gesv to ATen [7102][fb_bootcamp] Moved Gesv to ATen Jul 24, 2018
if (!batched) {
Tensor A_;
Tensor b_;
return _gesv_single_helper(self, A, b_, A_);

This comment was marked as off-topic.

ssnl
ssnl previously requested changes Jul 24, 2018
Copy link
Collaborator

@ssnl ssnl left a comment

Choose a reason for hiding this comment

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

_gesv_single_helper doesn't work for cuda. _th_gesv_single isn't removed.

Copy link
Collaborator

@ssnl ssnl left a comment

Choose a reason for hiding this comment

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

need to remove thc gesv code too.

ALLOCATE_ARRAY(info_array, magma_int_t, 1, result0);
ALLOCATE_ARRAY(ipiv_array, magma_int_t*, 1, result0);

magmaGesvBatched<scalar_t>(

This comment was marked as off-topic.

@ssnl
Copy link
Collaborator

ssnl commented Jul 25, 2018

test failure still looks legit

@ssnl
Copy link
Collaborator

ssnl commented Jul 26, 2018

@pytorchbot retest this please

AT_DISPATCH_FLOATING_TYPES(self.type(), "gesv", [&]{
auto A_ptr = result1.data<scalar_t>();
auto b_ptr = result0.data<scalar_t>();
auto ipiv = at::empty({bx}, result0.type().toScalarType(kInt));

This comment was marked as off-topic.

CPU: _gesv_helper_cpu
CUDA: _gesv_helper_cuda

- func: _gesv_single(Tensor self, Tensor A) -> (Tensor, Tensor)

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@animesht
Copy link
Author

@pytorchbot retest this please

Copy link
Collaborator

@ssnl ssnl left a comment

Choose a reason for hiding this comment

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

Overall looks great! Some small nits.


std::tuple<Tensor&,Tensor&> gesv_out(
Tensor& solution, Tensor& lu, const Tensor& self, const Tensor& A) {
Tensor& result0, Tensor& result1, const Tensor& self, const Tensor& A) {

This comment was marked as off-topic.

- func: gesv(Tensor self, Tensor A) -> (Tensor, Tensor)

- func: gesv_out(Tensor solution, Tensor lu, Tensor self, Tensor A) -> (Tensor, Tensor)
- func: gesv_out(Tensor result0, Tensor result1, Tensor self, Tensor A) -> (Tensor, Tensor)

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

- name: _gesv_single(Tensor self, Tensor A)
self: gesv_backward_self(grad, self, A)
A: gesv_backward_A(grad, self, A, solution)
A: gesv_backward_A(grad, self, A, result0)

This comment was marked as off-topic.

return std::tuple<Tensor,Tensor>(b_working_copy, A_working_copy);
}

std::tuple<Tensor&,Tensor&> _gesv_single_out_cuda(Tensor& result0, Tensor& result1,

This comment was marked as off-topic.

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.

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

int info;
int* ipiv;

result1 = A.t().clone();

This comment was marked as off-topic.

This comment was marked as off-topic.

"compilation. Please rebuild with MAGMA.");
#else
int64_t bx = self.size(0);
int64_t by = (self.dim() == 1) ? 1 : self.size(1);

This comment was marked as off-topic.

(long long)A.size(0), (long long)A.size(1));
} else if (A.size(0) != self.size(0)) {
AT_ERROR("A,B size incompatible - A has %ld "
"rows, B has %ld cols", A.size(0), self.size(0));

This comment was marked as off-topic.

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

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

This looks good, @animesht! I had a few minor comments and questions, in particular:

  1. As @ssnl mentioned, it would be nice if we could rename (result0, result1) to (solution, lu) (or whatever the ordering is) if the code generation allows for it.
    2) Tensors sent into the magma/lapack gesv bindings are required to be in column-major format: I'm not sure if the .t().clone() that is being done is sufficient for this nvm, this is fine!

int64_t by = (self.dim() == 1) ? 1 : self.size(1);
int info = 0;

result1 = A.t().clone();

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

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.

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

@animesht
Copy link
Author

animesht commented Jul 27, 2018

  • Renamed result0, result1
  • Fixed docs to mention that B can be 1D
  • Addressed other nits

* - clone and copy input_tensor.t() to output_tensor (same tensor)
* b) &input_tensor != &output_tensor:
* - copy input_tensor.t() to output_tensor
*/

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

AT_ERROR("gesv: MAGMA library not found in "
"compilation. Please rebuild with MAGMA.");
#else
/* See Gesv.cpp for comments */

This comment was marked as off-topic.

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

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

Haven't gone through the code in detail yet. I left some comments on refactoring the code to be a little nicer

@animesht
Copy link
Author

@zou3519, addressed your comments and refactored the implementation to Gesv.h and made it more compact. Hope this looks better!

cc @ssnl

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.

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

@yf225
Copy link
Contributor

yf225 commented Aug 21, 2018

@pytorchbot retest this please

* (i) &in == &out: copy in.t().clone() to out (same tensor)
* (ii) &in != &out: copy in.t() to out
*/
static inline void prepareIOTensors(

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

*/
static inline void prepareIOTensors(
const Tensor& in, Tensor& out, Tensor& temp,
int64_t& x, int64_t& y) {

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

* Before passing pointers to Lapack, we need to ensure that these pointers
* represent Fortran-contiguous tensors in column-major format
*
* Cases:

This comment was marked as off-topic.

* (ii) for 2D matrices, .t_() represents their column-major format
*
* Before passing pointers to Lapack, we need to ensure that these pointers
* represent Fortran-contiguous tensors in column-major format

This comment was marked as off-topic.

* copy the buffer to the output tensor.
*
* 2) out.t() is contiguous:
* (i) &in == &out: use out.data() as is. Do nothing

This comment was marked as off-topic.

This comment was marked as off-topic.

// view potential 1D `in` as 2D
auto in_t = in.view({x, y}).t_();

if (!out_tc && !out.is_contiguous() && out_correct_shape) {

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

Tensor temp_sol;
Tensor temp_lu;
prepareTensorsForLapack(self, sol, temp_sol);
prepareTensorsForLapack(A, lu, temp_lu);

This comment was marked as off-topic.

This comment was marked as off-topic.

prepareTensorsForLapack(A, lu, temp_lu);

AT_DISPATCH_FLOATING_TYPES(self.type(), "gesv", [&]{
const int64_t bx = sol.size(0);

This comment was marked as off-topic.

@yf225
Copy link
Contributor

yf225 commented Aug 28, 2018

ping @animesht :)

@animesht
Copy link
Author

Addressed most comments @zou3519 . Still need to add the tests for each case to test_torch.py, I actually had these as uncommitted python files in my old devgpu which I'm locked out of :(

Hoping for that to come back up soon, otherwise I can rewrite those and add them

@animesht
Copy link
Author

animesht commented Sep 6, 2018

@zou3519 added tests for all cases. Can I get a codeowner review?

@fmassa
Copy link
Member

fmassa commented Nov 6, 2018

This got superseded by #6100

@fmassa fmassa closed this Nov 6, 2018
@soumith soumith mentioned this pull request Dec 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.