ivy icon indicating copy to clipboard operation
ivy copied to clipboard

Bitwise operations for torch frontend

Open ahmedo42 opened this issue 2 years ago • 1 comments

#4072 #4073 #4074 #4075 #4076

The shift operations unit tests may not pass because we handle shift differently in the backend see: https://github.com/unifyai/ivy/discussions/2190#discussioncomment-3566729

Since shifting by a number larger than or equal to the width of the integer is undefined behavior in C it's not clear to me how to replicate the exact behavior as the frontend in this edge case.

ahmedo42 avatar Sep 06 '22 08:09 ahmedo42

You can add a check to see if the shift number is larger than or equal to the number of bits in that dtype. If it is larger, you can just return 0. Otherwise, you can make a call to ivy's shift function like what is currently being done.

That's not enough to replicate the Pytorch behavior, I don't quite understand how Pytorch deals with this under the hood, for example:

import ivy
import torch
ivy.set_backend("torch")
t = torch.tensor(-1, dtype=torch.int8)
x = torch.tensor(100, dtype=torch.int8)
print(torch.bitwise_left_shift(t, x)) #tensor(-16, dtype=torch.uint8)
print(ivy.bitwise_left_shift(t,x)) #ivy.array(-128)

ahmedo42 avatar Sep 09 '22 20:09 ahmedo42