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

Refactor less #619

Merged
merged 3 commits into from
Mar 25, 2024
Merged
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
10 changes: 5 additions & 5 deletions docs/framework/operators/tensor/tensor.less.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#tensor.less

```rust
fn less(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<bool>;
fn less(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<i32>;
```

Check if each element of the first tensor is less than the corresponding element of the second tensor.
Expand Down Expand Up @@ -31,7 +31,7 @@ use core::array::{ArrayTrait, SpanTrait};

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

fn less_example() -> Tensor<usize> {
fn less_example() -> Tensor<i32> {
let tensor_1 = TensorTrait::<u32>::new(
shape: array![3, 3, 3].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7, 8].span(),
);
Expand All @@ -43,7 +43,7 @@ fn less_example() -> Tensor<usize> {
// We can call `less` function as follows.
return tensor_1.less(@tensor_2);
}
>>> [false,false,false,false,false,false,true,false,false]
>>> [0,0,0,0,0,0,1,0,0]
```

Case 2: Compare tensors with different shapes
Expand All @@ -53,7 +53,7 @@ use core::array::{ArrayTrait, SpanTrait};

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

fn less_example() -> Tensor<usize> {
fn less_example() -> Tensor<i32> {
let tensor_1 = TensorTrait::<u32>::new(
shape: array![3, 3, 3].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7, 8].span(),
);
Expand All @@ -63,5 +63,5 @@ fn less_example() -> Tensor<usize> {
// We can call `less` function as follows.
return tensor_1.less(@tensor_2);
}
>>> [false,false,false,false,false,false,false,true,true]
>>> [0,0,0,0,0,0,0,1,1]
```
20 changes: 10 additions & 10 deletions nodegen/node/less.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def default():

x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_u32"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -25,7 +25,7 @@ def broadcast():

x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_u32_broadcast"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -42,7 +42,7 @@ def default():

x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_i32"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -54,7 +54,7 @@ def broadcast():

x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_i32_broadcast"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -71,7 +71,7 @@ def default():

x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_i8"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -83,7 +83,7 @@ def broadcast():

x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_i8_broadcast"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -102,7 +102,7 @@ def default():
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_fp8x23"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -116,7 +116,7 @@ def broadcast():
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_fp8x23_broadcast"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -135,7 +135,7 @@ def default():
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_fp16x16"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand All @@ -149,7 +149,7 @@ def broadcast():
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
z = Tensor(Dtype.BOOL, z.shape, z.flatten())
z = Tensor(Dtype.I32, z.shape, z.flatten())

name = "less_fp16x16_broadcast"
make_test([x, y], z, "input_0.less(@input_1)", name)
Expand Down
12 changes: 6 additions & 6 deletions src/operators/tensor/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ trait TensorTrait<T> {
/// #tensor.less
///
/// ```rust
/// fn less(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<bool>;
/// fn less(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<i32>;
/// ```
///
/// Check if each element of the first tensor is less than the corresponding element of the second tensor.
Expand Down Expand Up @@ -1282,7 +1282,7 @@ trait TensorTrait<T> {
///
/// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor};
///
/// fn less_example() -> Tensor<usize> {
/// fn less_example() -> Tensor<i32> {
/// let tensor_1 = TensorTrait::<u32>::new(
/// shape: array![3, 3, 3].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7, 8].span(),
/// );
Expand All @@ -1294,7 +1294,7 @@ trait TensorTrait<T> {
/// // We can call `less` function as follows.
/// return tensor_1.less(@tensor_2);
/// }
/// >>> [false,false,false,false,false,false,true,false,false]
/// >>> [0,0,0,0,0,0,1,0,0]
/// ```
///
/// Case 2: Compare tensors with different shapes
Expand All @@ -1304,7 +1304,7 @@ trait TensorTrait<T> {
///
/// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor};
///
/// fn less_example() -> Tensor<usize> {
/// fn less_example() -> Tensor<i32> {
/// let tensor_1 = TensorTrait::<u32>::new(
/// shape: array![3, 3, 3].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7, 8].span(),
/// );
Expand All @@ -1314,10 +1314,10 @@ trait TensorTrait<T> {
/// // We can call `less` function as follows.
/// return tensor_1.less(@tensor_2);
/// }
/// >>> [false,false,false,false,false,false,false,true,true]
/// >>> [0,0,0,0,0,0,0,1,1]
/// ```
///
fn less(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<bool>;
fn less(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<i32>;
/// #tensor.less_equal
///
/// ```rust
Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_bool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl BoolTensor of TensorTrait<bool> {
panic(array!['not supported!'])
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Complex64Tensor of TensorTrait<complex64> {
panic(array!['not supported!'])
}

fn less(self: @Tensor<complex64>, other: @Tensor<complex64>) -> Tensor<bool> {
fn less(self: @Tensor<complex64>, other: @Tensor<complex64>) -> Tensor<i32> {
panic(array!['not supported!'])
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_fp16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl FP16x16Tensor of TensorTrait<FP16x16> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<FP16x16>, other: @Tensor<FP16x16>) -> Tensor<bool> {
fn less(self: @Tensor<FP16x16>, other: @Tensor<FP16x16>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl FP16x16WTensor of TensorTrait<FP16x16W> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<FP16x16W>, other: @Tensor<FP16x16W>) -> Tensor<bool> {
fn less(self: @Tensor<FP16x16W>, other: @Tensor<FP16x16W>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_fp32x32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl FP32x32Tensor of TensorTrait<FP32x32> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<FP32x32>, other: @Tensor<FP32x32>) -> Tensor<bool> {
fn less(self: @Tensor<FP32x32>, other: @Tensor<FP32x32>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_fp64x64.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl FP64x64Tensor of TensorTrait<FP64x64> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<FP64x64>, other: @Tensor<FP64x64>) -> Tensor<bool> {
fn less(self: @Tensor<FP64x64>, other: @Tensor<FP64x64>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_fp8x23.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl FP8x23Tensor of TensorTrait<FP8x23> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<FP8x23>, other: @Tensor<FP8x23>) -> Tensor<bool> {
fn less(self: @Tensor<FP8x23>, other: @Tensor<FP8x23>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl FP8x23WTensor of TensorTrait<FP8x23W> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<FP8x23W>, other: @Tensor<FP8x23W>) -> Tensor<bool> {
fn less(self: @Tensor<FP8x23W>, other: @Tensor<FP8x23W>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_i32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl I32Tensor of TensorTrait<i32> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<i32>, other: @Tensor<i32>) -> Tensor<bool> {
fn less(self: @Tensor<i32>, other: @Tensor<i32>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_i8.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl I8Tensor of TensorTrait<i8> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<i8>, other: @Tensor<i8>) -> Tensor<bool> {
fn less(self: @Tensor<i8>, other: @Tensor<i8>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/implementations/tensor_u32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl U32Tensor of TensorTrait<u32> {
math::greater_equal::greater_equal(self, other)
}

fn less(self: @Tensor<u32>, other: @Tensor<u32>) -> Tensor<bool> {
fn less(self: @Tensor<u32>, other: @Tensor<u32>) -> Tensor<i32> {
math::less::less(self, other)
}

Expand Down
10 changes: 5 additions & 5 deletions src/operators/tensor/math/less.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use orion::operators::tensor::{core::{Tensor, TensorTrait, unravel_index}, BoolTensor};
use orion::operators::tensor::{core::{Tensor, TensorTrait, unravel_index}, I32Tensor};
use orion::operators::tensor::helpers::{
broadcast_shape, broadcast_index_mapping, len_from_shape, check_compatibility
};

/// Cf: TensorTrait::less docstring
fn less<T, impl TPartialOrd: PartialOrd<T>, impl TCopy: Copy<T>, impl TDrop: Drop<T>>(
y: @Tensor<T>, z: @Tensor<T>
) -> Tensor<bool> {
) -> Tensor<i32> {
let broadcasted_shape = broadcast_shape(*y.shape, *z.shape);
let mut result: Array<bool> = array![];
let mut result: Array<i32> = array![];

let num_elements = len_from_shape(broadcasted_shape);

Expand All @@ -20,9 +20,9 @@ fn less<T, impl TPartialOrd: PartialOrd<T>, impl TCopy: Copy<T>, impl TDrop: Dro
let indices_other = broadcast_index_mapping(*z.shape, indices_broadcasted);

if *(*y.data)[indices_self] < *(*z.data)[indices_other] {
result.append(true);
result.append(1);
} else {
result.append(false);
result.append(0);
}

n += 1;
Expand Down
10 changes: 5 additions & 5 deletions tests/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// mod numbers;
// mod performance;
// mod tensor_core;
mod numbers;
mod performance;
mod tensor_core;
mod nodes;
// mod ml;
// mod operators;
mod ml;
mod operators;

Loading
Loading