-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #400 from Dl-Vv/feat-and
Feat: And
- Loading branch information
Showing
65 changed files
with
1,660 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#tensor.and | ||
|
||
```rust | ||
fn and(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<usize>; | ||
``` | ||
|
||
Computes the logical AND of two tensors element-wise. | ||
The input tensors must have either: | ||
* Exactly the same shape | ||
* The same number of dimensions and the length of each dimension is either a common length or 1. | ||
|
||
## Args | ||
|
||
* `self`(`@Tensor<T>`) - The first tensor to be compared | ||
* `other`(`@Tensor<T>`) - The second tensor to be compared | ||
|
||
## Panics | ||
|
||
* Panics if the shapes are not equal or broadcastable | ||
|
||
## Returns | ||
|
||
A new `Tensor<usize>` of booleans (0 or 1) with the same shape as the broadcasted inputs. | ||
|
||
## Examples | ||
|
||
Case 1: Compare tensors with same shape | ||
|
||
```rust | ||
use array::{ArrayTrait, SpanTrait}; | ||
|
||
use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; | ||
|
||
fn and_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(), | ||
); | ||
|
||
let tensor_2 = TensorTrait::<u32>::new( | ||
shape: array![3, 3].span(), data: array![0, 1, 2, 0, 1, 2, 0, 1, 2].span(), | ||
); | ||
|
||
return tensor_1.and(@tensor_2); | ||
} | ||
>>> [0,1,1,0,1,1,0,1,1] | ||
``` | ||
|
||
Case 2: Compare tensors with different shapes | ||
|
||
```rust | ||
use array::{ArrayTrait, SpanTrait}; | ||
|
||
use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; | ||
|
||
fn and_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(), | ||
); | ||
|
||
let tensor_2 = TensorTrait::<u32>::new( | ||
shape: array![1, 3].span(), data: array![0, 1, 2].span(), | ||
); | ||
|
||
return tensor_1.and(@tensor_2); | ||
} | ||
>>> [0,1,1,0,1,1,0,1,1] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl | ||
|
||
|
||
class And(RunAll): | ||
@staticmethod | ||
def and_u32(): | ||
def default(): | ||
x = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) | ||
y = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_u32" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
def broadcast(): | ||
x = np.random.randint(0, 6, (2, 2)).astype(np.uint32) | ||
y = np.random.randint(0, 6, (1, 2)).astype(np.uint32) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_u32_broadcast" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
default() | ||
broadcast() | ||
|
||
@staticmethod | ||
def and_i32(): | ||
def default(): | ||
x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.int32) | ||
y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.int32) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_i32" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
def broadcast(): | ||
x = np.random.randint(-3, 3, (2, 2)).astype(np.int32) | ||
y = np.random.randint(-3, 3, (1, 2)).astype(np.int32) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_i32_broadcast" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
default() | ||
broadcast() | ||
|
||
@staticmethod | ||
def and_i8(): | ||
def default(): | ||
x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.int8) | ||
y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.int8) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_i8" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
def broadcast(): | ||
x = np.random.randint(-3, 3, (2, 2)).astype(np.int8) | ||
y = np.random.randint(-3, 3, (1, 2)).astype(np.int8) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_i8_broadcast" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
default() | ||
broadcast() | ||
|
||
@staticmethod | ||
def and_fp8x23(): | ||
def default(): | ||
x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) | ||
y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, to_fp( | ||
x.flatten(), FixedImpl.FP8x23)) | ||
y = Tensor(Dtype.FP8x23, y.shape, to_fp( | ||
y.flatten(), FixedImpl.FP8x23)) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_fp8x23" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
def broadcast(): | ||
x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) | ||
y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, to_fp( | ||
x.flatten(), FixedImpl.FP8x23)) | ||
y = Tensor(Dtype.FP8x23, y.shape, to_fp( | ||
y.flatten(), FixedImpl.FP8x23)) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_fp8x23_broadcast" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
default() | ||
broadcast() | ||
|
||
@staticmethod | ||
def and_fp16x16(): | ||
def default(): | ||
x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) | ||
y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, to_fp( | ||
x.flatten(), FixedImpl.FP16x16)) | ||
y = Tensor(Dtype.FP16x16, y.shape, to_fp( | ||
y.flatten(), FixedImpl.FP16x16)) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_fp16x16" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
def broadcast(): | ||
x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) | ||
y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) | ||
z = np.logical_and(x, y) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, to_fp( | ||
x.flatten(), FixedImpl.FP16x16)) | ||
y = Tensor(Dtype.FP16x16, y.shape, to_fp( | ||
y.flatten(), FixedImpl.FP16x16)) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "and_fp16x16_broadcast" | ||
make_node([x, y], [z], name) | ||
make_test([x, y], z, "input_0.and(@input_1)", name) | ||
|
||
default() | ||
broadcast() |
Oops, something went wrong.