Bitwise-XOR of all elements in a multi-dimensional Tensor

Hi everyone!
Does PyTorch provide a way to do the bitwise-xor of all elements in a tensor and return the output value?

I need to do this for a tensor of tensors, hence need a way to parallelize this computation, instead of using loops. Any kind of help would be great.

Thanks in advance.

Do you mean

import torch

x = torch.rand(2, 3, 4, 5) > 0.5
y = torch.rand(3, 4, 5) > 0.5
print(x)
print(y)
z = x | y
print(z)

Hi @Eta_C
Thanks for replying.
No, actually I have to do something like this:

import torch

# Sample tensor
X = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
lst = []
for i in X:
    XorI = 0    
    for elem in i:
        XorI = elem ^ XorI    
    lst.append(XorI)

But without loops.

P.S. It would be better if there’s way for more than 2 dimensions as well, where I can reduce in 1 specified direction.

Sorry, since pytorch only have logical_xor and bitwise_xor, I have to find some “math expression” for this operator. See here, it is too complicated!

I think the best way is to make a pytorch extension.

1 Like

See here , it is too complicated!

Yeah, this one seems a bit too complicated. :worried:

I think the best way is to make a pytorch extension .

I never knew about this feature of PyTorch. Can give it a try!

Thanks, @Eta_C :smile: ; though let’s keep the discussion open for any other possible solutions.