Skip to content

Fix TestAutograd.test_pinverse not actually testing#9192

Closed
ssnl wants to merge 3 commits intopytorch:masterfrom
ssnl:autogradpinv
Closed

Fix TestAutograd.test_pinverse not actually testing#9192
ssnl wants to merge 3 commits intopytorch:masterfrom
ssnl:autogradpinv

Conversation

@ssnl
Copy link
Collaborator

@ssnl ssnl commented Jul 5, 2018

cc @vishwakftw

Also added a check if none of the input tensors in gradcheck have requires_grad=True.

@vishwakftw
Copy link
Contributor

Oops, I am so sorry about this. I thought gradcheck converted the tensors to requires_grad=True.

Copy link
Contributor

@vishwakftw vishwakftw left a comment

Choose a reason for hiding this comment

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

ONNX changes have come with this PR.

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 5, 2018

@vishwakftw nah, it's mostly my fault. having worked with gradcheck for 8 months and I still forget about this.

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 5, 2018

Thanks for the reminder! Fixed onnx.

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.

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 5, 2018

cc @fritzo @fehiepsi @apaszke on test_distributions changes.

In this patch, I added an error if none of the input args is a tensor that requires grad. In TestDistributions, we are grad checking some constructors on argument lists with no tensors. So I had to add samples as a grad checked input. Hope that this change is okay with you guys.

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 5, 2018

@pytorchbot retest this please

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.

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.

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.

Copy link
Collaborator

@fritzo fritzo left a comment

Choose a reason for hiding this comment

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

distributions changes LGTM

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 5, 2018

cc @t-vi gradcheck on einsum wasn't enabled. After fixing in this commit, there are three cases that fail the check. Do you happen to have some idea why it is happening? I will look into this in a couple days.

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 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

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.

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.

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 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@t-vi
Copy link
Collaborator

t-vi commented Jul 6, 2018

@ssnl Ha, thanks for catching this! I think this is as simple as having double:
With

                gradcheck_inps = tuple(t.detach().requires_grad_() for t in test[1:])

passses all three cases that were failing before.
An alternative is, of course, to use just add dtype=torch.double for the generated args.
Do you want me to send a diff or include it in yours?

@ssnl ssnl deleted the autogradpinv branch July 6, 2018 15:35
@ssnl
Copy link
Collaborator Author

ssnl commented Jul 6, 2018

@t-vi I remember that they fail with a 2x Jacobian difference, but I'll have to try again.

@t-vi
Copy link
Collaborator

t-vi commented Jul 6, 2018

The following get's me true 3x while without the double I get false 3x:

x = torch.randn(5)
y = torch.randn(7)
A = torch.randn(3, 5)
#             if test[0] not in {"i,i->", "i,i->i", "ij,ij->ij"}:
for test in [
    ("i,i->", x, x),            # dot
    ("i,i->i", x, x),           # vector element-wise mul
    ("ij,ij->ij", A, A), # matrix element-wise mul
    ]:
    def do_einsum(*args):
         return torch.einsum(test[0], args)
    gradcheck_inps = tuple(t.detach().double().requires_grad_() for t in test[1:])
    print (test[0], torch.autograd.gradcheck(do_einsum, gradcheck_inps, raise_exception=False))

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 8, 2018

I just tried again. I believe that the random input are already in double precision (the numpy allclose check fails otherwise). The gradcheck still fails with 2x difference for me:

# i,i->

RuntimeError: Jacobian mismatch for output 0 with respect to input 0,    
numerical:tensor([[ 0.6672],                                             
        [ 2.9573],                                                       
        [-1.9153],                                                       
        [ 1.4615],                                                       
        [-1.3014]], dtype=torch.float64)                                 
analytical:tensor([[ 0.3336],                                            
        [ 1.4787],                                                       
        [-0.9577],                                                       
        [ 0.7307],                                                       
        [-0.6507]], dtype=torch.float64)                                 

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 8, 2018

oh okay I know why. it's not a bug. it's just that these tests use multiples of same tensor, so numerical perturbation changes not one, but two tensors at the same time!

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 8, 2018

i'll fix that

@t-vi
Copy link
Collaborator

t-vi commented Jul 8, 2018

Ha. Does the detach() solve that, too?

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 9, 2018

No.. even if detach() is called, they still share storage.

@t-vi
Copy link
Collaborator

t-vi commented Jul 9, 2018

Ah, but the double() created a separate instance for me...

@ssnl
Copy link
Collaborator Author

ssnl commented Jul 9, 2018

@t-vi This line is probably the culprit:

torch.set_default_tensor_type('torch.DoubleTensor')

:P

goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
Summary:
cc vishwakftw

Also added a check if none of the input tensors in `gradcheck` have `requires_grad=True`.
Closes pytorch#9192

Differential Revision: D8739401

Pulled By: SsnL

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants