Skip to content

Tensor.add_() returns different values on different runs using some inputs (using CPU). #14463

@sakhar

Description

@sakhar

🐛 Bug

When using the CPU only (no CUDA), the in-place add function torch.Tensor.add_() returns different values on different calls (with trivial differences) using some inputs. However, torch.Tensor.add() does not.

To Reproduce

  1. use this numpy array as an example https://raw.githubusercontent.com/sakhar/pytorch_add_bug/master/values-np.p
    or values-np.p.zip (unzip).

  2. try it with add_():

for i in range(10):
    values = pickle.load(open('values-np.p','rb'))
    x = torch.from_numpy(values[0])
    y = torch.from_numpy(values[1])
    print(x.add_(-0.7,y).data.detach().numpy()[-4])

results:

using add_():
-0.09407266
-0.094072655
-0.09407266
-0.094072655
-0.09407266
-0.09407266
-0.094072655
-0.094072655
-0.09407266
-0.09407266
  1. with add():
for i in range(10):
    values = pickle.load(open('values-np.p','rb'))
    x = torch.from_numpy(values[0])
    y = torch.from_numpy(values[1])
    print(x.add(-0.7,y).data.detach().numpy()[-4])

results:

using add():
-0.094072655
-0.094072655
-0.094072655
-0.094072655
-0.094072655
-0.094072655
-0.094072655
-0.094072655
-0.094072655
-0.094072655

Environment


 - PyTorch Version: 0.4.1
 - OS: Mac OSX 10.12.6
 - How you installed PyTorch: pip
 - Python version: 2.7
 - CUDA/cuDNN version: No CUDA
 - GPU models and configuration: No CUDA

- Versions of relevant libraries:
[pip] numpy (1.14.5)
[pip] torch (0.4.1)
[pip] torchvision (0.2.1)

cc @ezyang @gchanan @zou3519

Metadata

Metadata

Assignees

No one assigned

    Labels

    high prioritymodule: numerical-stabilityProblems related to numerical stability of operationstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions