Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d9e6676
Delegate Python ~ (invert operator) to Tensor.bitwise_not().
xuhdev Jun 28, 2019
d44e96b
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jun 28, 2019
6b040dd
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jun 30, 2019
0f0faf5
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jun 30, 2019
8b481cf
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 1, 2019
4b89b53
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 1, 2019
857ab08
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 1, 2019
1f44c8a
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 1, 2019
5169fc1
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 2, 2019
41f573b
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 2, 2019
3bd7095
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 2, 2019
8747935
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 2, 2019
c7587af
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 2, 2019
b75edc7
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 3, 2019
fd68eb6
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 3, 2019
df30999
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 3, 2019
1dd4705
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 3, 2019
4b91cdf
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
xuhdev Jul 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11276,10 +11276,6 @@ def test_bitwise_ops(self):
else:
self.assertFalse(x[idx] ^ y[idx])

invert_result = ~x
for idx in iter_indices(x):
self.assertEqual(1 - x[idx], invert_result[idx])

x_clone = x.clone()
x_clone &= y
self.assertEqual(x_clone, and_result)
Expand All @@ -11292,9 +11288,20 @@ def test_bitwise_ops(self):
x_clone ^= y
self.assertEqual(x_clone, xor_result)

def test_invert(self):
x = torch.ByteTensor([0, 1, 1])
self.assertEqual((~x).tolist(), [1, 0, 0])
def test_op_invert(self):
res = 0xffff - torch.arange(127, dtype=torch.int8)
for dtype in (torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64):
a = torch.arange(127, dtype=dtype)
self.assertEqual(res.type(dtype), ~a)

self.assertEqual(torch.tensor([True, False]),
~torch.tensor([False, True]))

# test exceptions
for dtype in(torch.half, torch.float, torch.double):
a = torch.zeros(10, dtype=dtype)
with self.assertRaises(TypeError):
b = ~a

def test_apply(self):
x = torch.arange(1, 6)
Expand Down
6 changes: 3 additions & 3 deletions tools/autograd/templates/python_variable_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ static PyObject * THPVariable_index_scalar(PyObject* self, PyObject* args) {
static Tensor dispatch_invert(const Tensor & self) {
AutoNoGIL no_gil;
OptionalDeviceGuard device_guard(device_of(self));
return 1 - self;
return self.bitwise_not();
}

static PyObject * THPVariable_invert(PyObject* self, PyObject* args) {
HANDLE_TH_ERRORS
auto& self_ = reinterpret_cast<THPVariable*>(self)->cdata;
if (self_.scalar_type() != at::kByte) {
throw TypeError("~ (operator.invert) is only implemented on byte tensors");
if (!isIntegralType(self_.scalar_type()) && self_.scalar_type() != at::kBool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why isn't bool an integral type? It's not different than uint1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's defined here. It's probably worth another independent PR for discussing it...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks like current usage and definition of isIntegralType are more of semantic sense (bool is technically integral, but semantically it is not used as an integer). I would lean toward keep it as it is now.

Examples:

throw TypeError("~ (operator.invert) is only implemented on integer and Boolean-type tensors");
}
return THPVariable_Wrap(dispatch_invert(self_));
END_HANDLE_TH_ERRORS
Expand Down