From: iurii zdebskyi <47012416+izdeby@users.noreply.github.com> Date: Wed, 10 Apr 2019 18:05:54 +0000 (-0700) Subject: Fixed bool Tensor value change bug (#19096) X-Git-Tag: accepted/tizen/6.5/unified/20211028.231830~289 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1858773c0c1395bc7fdccc16c7d5ad08b6d37682;p=platform%2Fupstream%2Fpytorch.git Fixed bool Tensor value change bug (#19096) Summary: Fixes #19077 Pull Request resolved: https://github.com/pytorch/pytorch/pull/19096 Differential Revision: D14871044 Pulled By: izdeby fbshipit-source-id: 61b12559c8c5b9613e00ba5933f478321ea80469 --- diff --git a/test/test_torch.py b/test/test_torch.py index 5adce86..ebfd34e 100644 --- a/test/test_torch.py +++ b/test/test_torch.py @@ -3014,6 +3014,13 @@ class _TestTorchMixin(object): self.assertTrue(x.is_cuda) torch.set_default_tensor_type(saved_type) + def test_bool_tensor_value_change(self): + for device in torch.testing.get_all_device_types(): + x = torch.tensor([True, False], dtype=torch.bool) + x[0] = False + x[1] = True + self.assertEqual(x, torch.tensor([False, True], dtype=torch.bool)) + def test_unfold_all_devices_and_dtypes(self): for device in torch.testing.get_all_device_types(): for dt in torch.testing.get_all_dtypes(): diff --git a/torch/csrc/autograd/python_variable_indexing.cpp b/torch/csrc/autograd/python_variable_indexing.cpp index 80461c4..eff82a9 100644 --- a/torch/csrc/autograd/python_variable_indexing.cpp +++ b/torch/csrc/autograd/python_variable_indexing.cpp @@ -114,7 +114,7 @@ static Variable valueToTensor(const at::Type & type, PyObject* value) { if (THPVariable_Check(value)) { return reinterpret_cast(value)->cdata; } - if (THPUtils_checkLong(value)) { + if (THPUtils_checkLong(value) || PyBool_Check(value)) { return at::scalar_tensor(Scalar(THPUtils_unpackLong(value)), type.options()); } if (PyFloat_Check(value)) {