Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat bitwise not #593

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
* [tensor.bitwise\_and](framework/operators/tensor/tensor.bitwise\_and.md)
* [tensor.bitwise\_xor](framework/operators/tensor/tensor.bitwise\_xor.md)
* [tensor.bitwise\_or](framework/operators/tensor/tensor.bitwise\_or.md)
* [tensor.bitwise\_not](framework/operators/tensor/tensor.bitwise\_not.md)
* [tensor.resize](framework/operators/tensor/tensor.resize.md)
* [tensor.round](framework/operators/tensor/tensor.round.md)
* [tensor.scatter](framework/operators/tensor/tensor.scatter.md)
Expand Down
3 changes: 2 additions & 1 deletion docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ You can see below the list of current supported ONNX Operators:
| [BitwiseAnd](operators/tensor/tensor.bitwise_and.md) | :white\_check\_mark: |
| [BitwiseOr](operators/tensor/tensor.bitwise_or.md) | :white\_check\_mark: |
| [BitwiseXor](operators/tensor/tensor.bitwise_xor.md) | :white\_check\_mark: |
| [BitwiseNot](operators/tensor/tensor.bitwise_not.md) | :white\_check\_mark: |
| [Resize](operators/tensor/tensor.resize.md) | :white\_check\_mark: |
| [Round](operators/tensor/tensor.round.md) | :white\_check\_mark: |
| [MaxInTensor](operators/tensor/tensor.max\_in\_tensor.md) | :white\_check\_mark: |
Expand Down Expand Up @@ -127,4 +128,4 @@ You can see below the list of current supported ONNX Operators:
| [RandomUniformLike](operators/tensor/tensor.tensor.random_uniform_like.md) | :white\_check\_mark: |
| [LabelEncoder](operators/tensor/tensor.label_encoder.md) | :white\_check\_mark: |

Current Operators support: **118/156 (75%)**
Current Operators support: **119/156 (75%)**
1 change: 1 addition & 0 deletions docs/framework/operators/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ use orion::operators::tensor::TensorTrait;
| [`tensor.bitwise_and`](tensor.bitwise\_and.md) | Computes the bitwise AND of two tensors element-wise. |
| [`tensor.bitwise_xor`](tensor.bitwise\_xor.md) | Computes the bitwise XOR of two tensors element-wise. |
| [`tensor.bitwise_or`](tensor.bitwise\_or.md) | Computes the bitwise OR of two tensors element-wise. |
| [`tensor.bitwise_not`](tensor.bitwise\_not.md) | Computes the bitwise NOT of tensor element-wise. |
| [`tensor.resize`](tensor.resize.md) | Resizes the input tensor. |
| [`tensor.round`](tensor.round.md) | Computes the round value of all elements in the input tensor. |
| [`tensor.reduce_l1`](tensor.reduce\_l1.md) | Computes the L1 norm of the input tensor's elements along the provided axes. |
Expand Down
33 changes: 33 additions & 0 deletions docs/framework/operators/tensor/tensor.bitwise_not.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#tensor.bitwise_not

```rust
fn bitwise_not(self: @Tensor<T>) -> Tensor<usize>;
```

Computes the bitwise NOT of the tensor element-wise.

## Args

* `self`(`@Tensor<T>`) - The the tensor to be bitwise NOTed


## Returns

A new `Tensor<T>` with the same shape as the broadcasted input.

## Example

```rust
use core::array::{ArrayTrait, SpanTrait};

use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor};

fn bitwise_not_example() -> Tensor<usize> {
let tensor_1 = TensorTrait::<u32>::new(
shape: array![3, 3].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7, 8].span(),
);

return tensor_1.bitwise_not();
}
>>> [4294967295,4294967294,4294967293,4294967292,4294967291,4294967290,4294967289,4294967288,4294967287]
```
53 changes: 53 additions & 0 deletions src/numbers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ trait NumberTrait<T, MAG> {
fn bitwise_or(lhs: T, rhs: T) -> T;
fn add(lhs: T, rhs: T) -> T;
fn sub(lhs: T, rhs: T) -> T;
fn bitwise_not(self: T) -> T;
}

use orion::numbers::fixed_point::implementations::fp8x23::core::{
Expand Down Expand Up @@ -291,6 +292,10 @@ impl FP8x23Number of NumberTrait<FP8x23, u32> {
fn sub(lhs: FP8x23, rhs: FP8x23) -> FP8x23 {
FP8x23Sub::sub(lhs, rhs)
}

fn bitwise_not(self: FP8x23) -> FP8x23 {
core_fp8x23::bitwise_not(self)
}
}

use orion::numbers::fixed_point::implementations::fp8x23wide::core::{
Expand Down Expand Up @@ -518,6 +523,10 @@ impl FP8x23WNumber of NumberTrait<FP8x23W, u64> {
fn sub(lhs: FP8x23W, rhs: FP8x23W) -> FP8x23W {
FP8x23WSub::sub(lhs, rhs)
}

fn bitwise_not(self: FP8x23W) -> FP8x23W {
core_fp8x23wide::bitwise_not(self)
}
}

use orion::numbers::fixed_point::implementations::fp16x16::core::{
Expand Down Expand Up @@ -745,6 +754,10 @@ impl FP16x16Number of NumberTrait<FP16x16, u32> {
fn sub(lhs: FP16x16, rhs: FP16x16) -> FP16x16 {
FP16x16Sub::sub(lhs, rhs)
}

fn bitwise_not(self: FP16x16) -> FP16x16 {
core_fp16x16::bitwise_not(self)
}
}

use orion::numbers::fixed_point::implementations::fp16x16wide::core::{
Expand Down Expand Up @@ -972,6 +985,10 @@ impl FP16x16WNumber of NumberTrait<FP16x16W, u64> {
fn sub(lhs: FP16x16W, rhs: FP16x16W) -> FP16x16W {
FP16x16WSub::sub(lhs, rhs)
}

fn bitwise_not(self: FP16x16W) -> FP16x16W {
core_fp16x16wide::bitwise_not(self)
}
}

use orion::numbers::fixed_point::implementations::fp64x64::core::{
Expand Down Expand Up @@ -1200,6 +1217,10 @@ impl FP64x64Number of NumberTrait<FP64x64, u128> {
fn sub(lhs: FP64x64, rhs: FP64x64) -> FP64x64 {
FP64x64Sub::sub(lhs, rhs)
}

fn bitwise_not(self: FP64x64) -> FP64x64 {
FP64x64Impl::bitwise_not(self)
}
}

use orion::numbers::fixed_point::implementations::fp32x32::core::{
Expand Down Expand Up @@ -1428,6 +1449,10 @@ impl FP32x32Number of NumberTrait<FP32x32, u64> {
fn sub(lhs: FP32x32, rhs: FP32x32) -> FP32x32 {
FP32x32Sub::sub(lhs, rhs)
}

fn bitwise_not(self: FP32x32) -> FP32x32 {
FP32x32Impl::bitwise_not(self)
}
}

impl I8Number of NumberTrait<i8, i8> {
Expand Down Expand Up @@ -1687,6 +1712,10 @@ impl I8Number of NumberTrait<i8, i8> {
fn sub(lhs: i8, rhs: i8) -> i8 {
lhs - rhs
}

fn bitwise_not(self: i8) -> i8 {
panic(array!['not supported!'])
}
}

impl I8Div of Div<i8> {
Expand Down Expand Up @@ -2049,6 +2078,10 @@ impl I16Number of NumberTrait<i16, i16> {
fn sub(lhs: i16, rhs: i16) -> i16 {
lhs - rhs
}

fn bitwise_not(self: i16) -> i16 {
panic(array!['not supported!'])
}
}

impl I16Div of Div<i16> {
Expand Down Expand Up @@ -2347,6 +2380,10 @@ impl I32Number of NumberTrait<i32, i32> {
fn sub(lhs: i32, rhs: i32) -> i32 {
lhs - rhs
}

fn bitwise_not(self: i32) -> i32 {
panic(array!['not supported!'])
}
}

impl I32Div of Div<i32> {
Expand Down Expand Up @@ -2661,6 +2698,10 @@ impl I64Number of NumberTrait<i64, i64> {
fn sub(lhs: i64, rhs: i64) -> i64 {
lhs - rhs
}

fn bitwise_not(self: i64) -> i64 {
panic(array!['not supported!'])
}
}

impl I64Div of Div<i64> {
Expand Down Expand Up @@ -2960,6 +3001,10 @@ impl I128Number of NumberTrait<i128, i128> {
fn sub(lhs: i128, rhs: i128) -> i128 {
lhs - rhs
}

fn bitwise_not(self: i128) -> i128 {
panic(array!['not supported!'])
}
}

impl I128Div of Div<i128> {
Expand Down Expand Up @@ -3246,6 +3291,10 @@ impl u32Number of NumberTrait<u32, u32> {
fn sub(lhs: u32, rhs: u32) -> u32 {
lhs - rhs
}

fn bitwise_not(self: u32) -> u32 {
~self
}
}


Expand Down Expand Up @@ -3483,6 +3532,10 @@ impl Complex64Number of NumberTrait<complex64, FP64x64> {
fn sub(lhs: complex64, rhs: complex64) -> complex64 {
Complex64Sub::sub(lhs, rhs)
}

fn bitwise_not(self: complex64) -> complex64 {
panic(array!['not supported!'])
}
}

impl U32IntoI32 of Into<u32, i32> {
Expand Down
1 change: 1 addition & 0 deletions src/numbers/fixed_point/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1144,4 +1144,5 @@ trait FixedTrait<T, MAG> {
fn is_inf(self: T) -> bool;
fn is_pos_inf(self: T) -> bool;
fn is_neg_inf(self: T) -> bool;
fn bitwise_not(self: T) -> T;
}
4 changes: 4 additions & 0 deletions src/numbers/fixed_point/implementations/fp16x16/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ impl FP16x16Impl of FixedTrait<FP16x16, u32> {
fn erf(self: FP16x16) -> FP16x16 {
erf::erf(self)
}

fn bitwise_not(self: FP16x16) -> FP16x16 {
FP16x16 { mag: ~self.mag, sign: !self.sign }
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ fn sign(a: FP16x16) -> FP16x16 {
}
}

fn bitwise_not(a: FP16x16) -> FP16x16 {
FixedTrait::new(~a.mag, !a.sign)
}

// Tests --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ impl FP16x16WImpl of FixedTrait<FP16x16W, u64> {
fn erf(self: FP16x16W) -> FP16x16W {
erf::erf(self)
}

fn bitwise_not(self: FP16x16W) -> FP16x16W {
FP16x16W { mag: ~self.mag, sign: !self.sign }
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ fn sign(a: FP16x16W) -> FP16x16W {
}
}

fn bitwise_not(a: FP16x16W) -> FP16x16W {
FixedTrait::new(~a.mag, !a.sign)
}

// Tests --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions src/numbers/fixed_point/implementations/fp32x32/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ impl FP32x32Impl of FixedTrait<FP32x32, u64> {
fn erf(self: FP32x32) -> FP32x32 {
erf::erf(self)
}

fn bitwise_not(self: FP32x32) -> FP32x32 {
FP32x32 { mag: ~self.mag, sign: !self.sign }
}
}

impl FP32x32Print of PrintTrait<FP32x32> {
Expand Down
4 changes: 4 additions & 0 deletions src/numbers/fixed_point/implementations/fp64x64/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ impl FP64x64Impl of FixedTrait<FP64x64, u128> {
fn erf(self: FP64x64) -> FP64x64 {
erf::erf(self)
}

fn bitwise_not(self: FP64x64) -> FP64x64 {
FP64x64 { mag: ~self.mag, sign: !self.sign }
}
}

impl FP64x64Print of PrintTrait<FP64x64> {
Expand Down
4 changes: 4 additions & 0 deletions src/numbers/fixed_point/implementations/fp8x23/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ impl FP8x23Impl of FixedTrait<FP8x23, u32> {
fn erf(self: FP8x23) -> FP8x23 {
erf::erf(self)
}

fn bitwise_not(self: FP8x23) -> FP8x23 {
FP8x23 { mag: ~self.mag, sign: !self.sign }
}
}

impl FP8x23Print of PrintTrait<FP8x23> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ fn sign(a: FP8x23) -> FP8x23 {
}
}

fn bitwise_not(a: FP8x23) -> FP8x23 {
FixedTrait::new(~a.mag, !a.sign)
}

// Tests --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions src/numbers/fixed_point/implementations/fp8x23wide/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ impl FP8x23WImpl of FixedTrait<FP8x23W, u64> {
fn erf(self: FP8x23W) -> FP8x23W {
erf::erf(self)
}

fn bitwise_not(self: FP8x23W) -> FP8x23W {
FP8x23W { mag: ~self.mag, sign: !self.sign }
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ fn sign(a: FP8x23W) -> FP8x23W {
}
}

fn bitwise_not(a: FP8x23W) -> FP8x23W {
FixedTrait::new(~a.mag, !a.sign)
}

// Tests --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
Expand Down
36 changes: 36 additions & 0 deletions src/operators/tensor/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl TensorSerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Tensor<
/// bitwise_and - Computes the bitwise AND of two tensors element-wise.
/// bitwise_xor - Computes the bitwise XOR of two tensors element-wise.
/// bitwise_or - Computes the bitwise OR of two tensors element-wise.
/// bitwise_not - Computes the bitwise NOT of tensor element-wise.
/// resize - Resizes the input tensor.
/// round - Computes the round value of all elements in the input tensor.
/// reduce_l1 - Computes the L1 norm of the input tensor's elements along the provided axes.
Expand Down Expand Up @@ -4049,6 +4050,41 @@ trait TensorTrait<T> {
/// ```
///
fn bitwise_xor(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<T>;
/// #tensor.bitwise_not
///
/// ```rust
/// fn bitwise_not(self: @Tensor<T>) -> Tensor<usize>;
/// ```
///
/// Computes the bitwise NOT of the tensor element-wise.
///
/// ## Args
///
/// * `self`(`@Tensor<T>`) - The the tensor to be bitwise NOTed
///
///
/// ## Returns
///
/// A new `Tensor<T>` with the same shape as the broadcasted input.
///
/// ## Example
///
/// ```rust
/// use core::array::{ArrayTrait, SpanTrait};
///
/// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor};
///
/// fn bitwise_not_example() -> Tensor<usize> {
/// let tensor_1 = TensorTrait::<u32>::new(
/// shape: array![3, 3].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7, 8].span(),
/// );
///
/// return tensor_1.bitwise_not();
/// }
/// >>> [4294967295,4294967294,4294967293,4294967292,4294967291,4294967290,4294967289,4294967288,4294967287]
/// ```
///
fn bitwise_not(self: @Tensor<T>) -> Tensor<T>;
/// ## tensor.reduce_l1
///
/// ```rust
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_bool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ impl BoolTensor of TensorTrait<bool> {
panic(array!['not supported!'])
}

fn bitwise_not(self: @Tensor<bool>) -> Tensor<bool> {
panic(array!['not supported!'])
}

fn reduce_l1(self: @Tensor<bool>, axis: usize, keepdims: bool) -> Tensor<bool> {
panic(array!['not supported!'])
}
Expand Down
Loading
Loading