Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with pointwise math when output tensor is zero-strided with correct size #289

Open
fmassa opened this issue Jul 10, 2015 · 2 comments

Comments

@fmassa
Copy link
Contributor

fmassa commented Jul 10, 2015

When reusing a Tensor as the output of a pointwise math operation (like cmul, add, cdiv etc), the result is not properly computed if the output Tensor has zero stride and has the correct size.

a = torch.Tensor(1,5):expand(5,5)
b = torch.rand(5,5)
c = torch.rand(5,5)

print(a:cmul(b,c)) -- computes the wrong multiplication
print(torch.cmul(b,c)) -- correct answer

it outputs

th> print(a:cmul(b,c)) -- computes the wrong multiplication
 0.1019  0.0165  0.0757  0.7305  0.0191
 0.1019  0.0165  0.0757  0.7305  0.0191
 0.1019  0.0165  0.0757  0.7305  0.0191
 0.1019  0.0165  0.0757  0.7305  0.0191
 0.1019  0.0165  0.0757  0.7305  0.0191
[torch.DoubleTensor of size 5x5]

th> print(torch.cmul(b,c)) -- correct answer
 0.0222  0.0103  0.0174  0.2752  0.1085
 0.2826  0.2686  0.0294  0.5893  0.6549
 0.2514  0.0066  0.0279  0.0920  0.0991
 0.6117  0.4126  0.1853  0.0232  0.4656
 0.1019  0.0165  0.0757  0.7305  0.0191
[torch.DoubleTensor of size 5x5]

This happens because the THTensor_(resizeAs) function doesn't take into account the fact that even though the sizes match, there can be a zero stride somewhere.

Maybe a solution would be checking in THTensor_(isSameSizeAs) if there is one of the strides==0 and returning 0 in this case ? I don't know if there could be side effects though.

@dominikgrewe
Copy link
Member

There are probably many functions out there that use THTensor_(isSameSizeAs) to determine if arguments have the correct size. If you now say that two arguments are different just because one has stride=0 these functions will fail (probably unnecessarily because for inputs stride=0 doesn't matter).

I agree that stride=0 is a problem for outputs though. Not sure how to fix it without checking for it in each function...

@fmassa
Copy link
Contributor Author

fmassa commented Jul 13, 2015

The solution I proposed doesn't solve the problem. Actually, even if THTensor_(rawResize) was called, as the sizes are the same it wouldn't modify the strides. We would need for example to reset the sizes if there is a zero stride before calling THTensor_(rawResize).

The way I see it, the problem lies within THTensor_(resize) and THTensor_(resizeAs). For example, the following snippet does not work as well

a = torch.rand(1,3):expand(3,3)
b = torch.rand(3,3)
a:resizeAs(b):copy(b);

it outputs, as before

th> print(a)
 0.9823  0.1666  0.0679
 0.9823  0.1666  0.0679
 0.9823  0.1666  0.0679
[torch.DoubleTensor of size 3x3]
th> print(b)
 0.3374  0.8114  0.3300
 0.9484  0.8672  0.0940
 0.9823  0.1666  0.0679
[torch.DoubleTensor of size 3x3]

Would you expect this current behaviour to be correct ?

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

No branches or pull requests

2 participants