{"action":"created","issue":{"url":"https://api.github.com/repos/pytorch/pytorch/issues/7731","repository_url":"https://api.github.com/repos/pytorch/pytorch","labels_url":"https://api.github.com/repos/pytorch/pytorch/issues/7731/labels{/name}","comments_url":"https://api.github.com/repos/pytorch/pytorch/issues/7731/comments","events_url":"https://api.github.com/repos/pytorch/pytorch/issues/7731/events","html_url":"https://github.com/pytorch/pytorch/issues/7731","id":324959963,"number":7731,"title":"in-place Arithmetic assignment operators gives wrong answers","user":{"login":"calebh","id":542102,"avatar_url":"https://avatars2.githubusercontent.com/u/542102?v=4","gravatar_id":"","url":"https://api.github.com/users/calebh","html_url":"https://github.com/calebh","followers_url":"https://api.github.com/users/calebh/followers","following_url":"https://api.github.com/users/calebh/following{/other_user}","gists_url":"https://api.github.com/users/calebh/gists{/gist_id}","starred_url":"https://api.github.com/users/calebh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/calebh/subscriptions","organizations_url":"https://api.github.com/users/calebh/orgs","repos_url":"https://api.github.com/users/calebh/repos","events_url":"https://api.github.com/users/calebh/events{/privacy}","received_events_url":"https://api.github.com/users/calebh/received_events","type":"User","site_admin":false},"labels":[{"id":443484050,"url":"https://api.github.com/repos/pytorch/pytorch/labels/medium%20priority","name":"medium priority","color":"fbca04","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-05-21T15:32:19Z","updated_at":"2018-05-22T21:16:16Z","closed_at":null,"author_association":"NONE","body":"## Issue description\r\n\r\nUsing the +=, -=, *=, /= operators gives the wrong answer in some cases without any warning. The matrix multiply assignment operator @= seems to work fine. I have tested in both PyTorch 0.3.0.post4 and PyTorch 0.4.0 (the latest version at the time of this issue creation) on the CPU. I have not tested whether this bug happens with CUDA.\r\n\r\n## Code example\r\n\r\nI was able to produce a minimal case of this bug:\r\n\r\n```\r\nimport torch\r\n\r\n## Addition\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nt1 += t2\r\nprint(\"Incorrect answer\")\r\nprint(t1)\r\n\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nprint(\"Correct answer\")\r\nprint(t1 + t2)\r\n\r\n## Subtraction\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nt1 -= t2\r\nprint(\"Incorrect answer\")\r\nprint(t1)\r\n\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nprint(\"Correct answer\")\r\nprint(t1 - t2)\r\n\r\n## Multiplication\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nt1 *= t2\r\nprint(\"Incorrect answer\")\r\nprint(t1)\r\n\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nprint(\"Correct answer\")\r\nprint(t1 * t2)\r\n\r\n## Division\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nt1 /= t2\r\nprint(\"Incorrect answer\")\r\nprint(t1)\r\n\r\nx = torch.Tensor([[1],[2],[3]])\r\nt1 = x.expand(3, 3)\r\nt2 = x.t().expand(3, 3)\r\n\r\nprint(\"Correct answer\")\r\nprint(t1 / t2)\r\n```\r\n\r\n## Output\r\n```\r\nIncorrect answer\r\ntensor([[  7.,   7.,   7.],\r\n        [ 21.,  21.,  21.],\r\n        [ 62.,  62.,  62.]])\r\nCorrect answer\r\ntensor([[ 2.,  3.,  4.],\r\n        [ 3.,  4.,  5.],\r\n        [ 4.,  5.,  6.]])\r\nIncorrect answer\r\ntensor([[-5., -5., -5.],\r\n        [-3., -3., -3.],\r\n        [ 0.,  0.,  0.]])\r\nCorrect answer\r\ntensor([[ 0., -1., -2.],\r\n        [ 1.,  0., -1.],\r\n        [ 2.,  1.,  0.]])\r\nIncorrect answer\r\ntensor([[ 6.0000e+00,  6.0000e+00,  6.0000e+00],\r\n        [ 4.3200e+02,  4.3200e+02,  4.3200e+02],\r\n        [ 6.0466e+07,  6.0466e+07,  6.0466e+07]])\r\nCorrect answer\r\ntensor([[ 1.,  2.,  3.],\r\n        [ 2.,  4.,  6.],\r\n        [ 3.,  6.,  9.]])\r\nIncorrect answer\r\ntensor([[ 0.1667,  0.1667,  0.1667],\r\n        [ 0.3333,  0.3333,  0.3333],\r\n        [ 1.0000,  1.0000,  1.0000]])\r\nCorrect answer\r\ntensor([[ 1.0000,  0.5000,  0.3333],\r\n        [ 2.0000,  1.0000,  0.6667],\r\n        [ 3.0000,  1.5000,  1.0000]])\r\n```\r\n\r\n## System Info\r\nPyTorch version: 0.4.0\r\nIs debug build: No\r\nCUDA used to build PyTorch: 8.0.61\r\n\r\nOS: Ubuntu 17.10\r\nGCC version: (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0\r\nCMake version: version 3.9.1\r\n\r\nPython version: 3.6\r\nIs CUDA available: No\r\nCUDA runtime version: No CUDA\r\nGPU models and configuration: No CUDA\r\nNvidia driver version: No CUDA\r\ncuDNN version: No CUDA\r\n\r\nVersions of relevant libraries:\r\n[pip3] numpy (1.14.3)\r\n[pip3] torch (0.4.0)\r\n[conda] Could not collect"},"comment":{"url":"https://api.github.com/repos/pytorch/pytorch/issues/comments/391143490","html_url":"https://github.com/pytorch/pytorch/issues/7731#issuecomment-391143490","issue_url":"https://api.github.com/repos/pytorch/pytorch/issues/7731","id":391143490,"user":{"login":"fmassa","id":9110200,"avatar_url":"https://avatars2.githubusercontent.com/u/9110200?v=4","gravatar_id":"","url":"https://api.github.com/users/fmassa","html_url":"https://github.com/fmassa","followers_url":"https://api.github.com/users/fmassa/followers","following_url":"https://api.github.com/users/fmassa/following{/other_user}","gists_url":"https://api.github.com/users/fmassa/gists{/gist_id}","starred_url":"https://api.github.com/users/fmassa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fmassa/subscriptions","organizations_url":"https://api.github.com/users/fmassa/orgs","repos_url":"https://api.github.com/users/fmassa/repos","events_url":"https://api.github.com/users/fmassa/events{/privacy}","received_events_url":"https://api.github.com/users/fmassa/received_events","type":"User","site_admin":false},"created_at":"2018-05-22T21:16:16Z","updated_at":"2018-05-22T21:16:16Z","author_association":"MEMBER","body":"@calebh just to reiterate, `+=` is NOT syntactic sugar for `x = x + ...`. The same holds true for python lists for example, while one mutates elements in place, while the other creates a new list."}}
{"actor":{"display_login":"fmassa"}}