From 9ab6af62c325c55533190754a4d061c4183d75c6 Mon Sep 17 00:00:00 2001 From: 0x73e <132935850+0x73e@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:05:34 -0400 Subject: [PATCH 1/5] feat: neg operator --- src/numbers.cairo | 41 +++++++++++++++++++ src/operators/tensor/core.cairo | 41 +++++++++++++++++++ .../implementations/tensor_fp16x16.cairo | 4 ++ .../implementations/tensor_fp32x32.cairo | 4 ++ .../implementations/tensor_fp64x64.cairo | 4 ++ .../implementations/tensor_fp8x23.cairo | 4 ++ .../tensor/implementations/tensor_i32.cairo | 4 ++ .../tensor/implementations/tensor_i8.cairo | 4 ++ .../tensor/implementations/tensor_u32.cairo | 4 ++ src/operators/tensor/math.cairo | 1 + src/operators/tensor/math/neg.cairo | 32 +++++++++++++++ 11 files changed, 143 insertions(+) create mode 100644 src/operators/tensor/math/neg.cairo diff --git a/src/numbers.cairo b/src/numbers.cairo index 04ad6efa5..3a6ab7807 100644 --- a/src/numbers.cairo +++ b/src/numbers.cairo @@ -10,6 +10,7 @@ trait NumberTrait { fn new_unscaled(mag: MAG, sign: bool) -> T; fn from_felt(val: felt252) -> T; fn abs(self: T) -> T; + fn neg(self: T) -> T; fn ceil(self: T) -> T; fn exp(self: T) -> T; fn exp2(self: T) -> T; @@ -176,6 +177,10 @@ impl FP8x23Number of NumberTrait { core_fp8x23::abs(self) } + fn neg(self: FP8x23) -> FP8x23 { + core_fp8x23::neg(self) + } + fn min_value() -> FP8x23 { FP8x23 { mag: core_fp8x23::MAX, sign: true } } @@ -341,6 +346,10 @@ impl FP16x16Number of NumberTrait { core_fp16x16::abs(self) } + fn neg(self: FP16x16) -> FP16x16 { + core_fp16x16::neg(self) + } + fn min_value() -> FP16x16 { FP16x16 { mag: core_fp16x16::MAX, sign: true } } @@ -507,6 +516,10 @@ impl FP64x64Number of NumberTrait { fp64x64::core::abs(self) } + fn neg(self: FP64x64) -> FP64x64 { + fp64x64::core::neg(self) + } + fn min_value() -> FP64x64 { FP64x64 { mag: core_fp64x64::MAX, sign: true } } @@ -673,6 +686,10 @@ impl FP32x32Number of NumberTrait { fp32x32::core::abs(self) } + fn neg(self: FP32x32) -> FP32x32 { + fp32x32::core::neg(self) + } + fn min_value() -> FP32x32 { FP32x32 { mag: core_fp32x32::MAX, sign: true } } @@ -837,6 +854,10 @@ impl I8Number of NumberTrait { i8_core::i8_abs(self) } + fn neg(self: i8) -> i8 { + i8_core::i8_neg(self) + } + fn min_value() -> i8 { i8 { mag: 128, sign: true } } @@ -1009,6 +1030,10 @@ impl i16Number of NumberTrait { i16_core::i16_abs(self) } + fn neg(self: i16) -> i16 { + i16_core::i16_neg(self) + } + fn min_value() -> i16 { i16 { mag: 32768, sign: true } } @@ -1181,6 +1206,10 @@ impl i32Number of NumberTrait { i32_core::i32_abs(self) } + fn neg(self: i32) -> i32 { + i32_core::i32_neg(self) + } + fn min_value() -> i32 { i32 { mag: 2147483648, sign: true } } @@ -1353,6 +1382,10 @@ impl i64Number of NumberTrait { i64_core::i64_abs(self) } + fn neg(self: i64) -> i64 { + i64_core::i64_neg(self) + } + fn min_value() -> i64 { i64 { mag: 9223372036854775808, sign: true } } @@ -1526,6 +1559,10 @@ impl i128Number of NumberTrait { i128_core::i128_abs(self) } + fn neg(self: i128) -> i128 { + i128_core::i128_neg(self) + } + fn min_value() -> i128 { i128 { mag: 170141183460469231731687303715884105728, sign: true } } @@ -1696,6 +1733,10 @@ impl u32Number of NumberTrait { self } + fn neg(self: u32) -> u32 { + panic(array!['not supported']) + } + fn min_value() -> u32 { 0 } diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 535ed699c..4b9c9ed52 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -1234,6 +1234,47 @@ trait TensorTrait { /// ``` /// fn abs(self: @Tensor) -> Tensor; + /// #tensor.neg + /// + /// ```rust + /// fn neg(self: @Tensor) -> Tensor; + /// ``` + /// + /// Computes the negation of all elements in the input tensor. + /// + /// ## Args + /// + /// * `self`(`@Tensor`) - The input tensor. + /// + /// + /// ## Returns + /// + /// A new `Tensor` of the same shape as the input tensor with + /// the negation of all elements in the input tensor. + /// + /// ## Example + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, I32Tensor}; + /// use orion::numbers::{i32, IntegerTrait}; + /// + /// fn neg_example() -> Tensor { + /// let tensor = TensorTrait::new( + /// shape: array![3].span(), + /// data: array![ + /// IntegerTrait::new(1, true), IntegerTrait::new(2, true), IntegerTrait::new(3, false) + /// ] + /// .span(), + /// ); + /// + /// return tensor.neg(); + /// } + /// >>> [1, 2, -3] + /// ``` + /// + fn neg(self: @Tensor) -> Tensor; /// #tensor.ceil /// /// ```rust diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index ac803904e..1ad60086f 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -100,6 +100,10 @@ impl FP16x16Tensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { math::ceil::ceil(*self) } diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 19557eba1..f421b8113 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -101,6 +101,10 @@ impl FP32x32Tensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { math::ceil::ceil(*self) } diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index 686d319b1..c00da33fe 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -101,6 +101,10 @@ impl FP64x64Tensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { math::ceil::ceil(*self) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index f46e96fb5..956e3b2ef 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -100,6 +100,10 @@ impl FP8x23Tensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { math::ceil::ceil(*self) } diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index dd6ae6f59..ae631b90a 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -101,6 +101,10 @@ impl I32Tensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { panic(array!['not supported!']) } diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index a4518cefb..583871bd6 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -100,6 +100,10 @@ impl I8Tensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { panic(array!['not supported!']) } diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 4d26a8634..315aff0c5 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -100,6 +100,10 @@ impl U32Tensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { panic(array!['not supported!']) } diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index 5ac834280..2874b7e6e 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -32,3 +32,4 @@ mod sqrt; mod concat; mod gather; mod sign; +mod neg; diff --git a/src/operators/tensor/math/neg.cairo b/src/operators/tensor/math/neg.cairo new file mode 100644 index 000000000..b34ea16f0 --- /dev/null +++ b/src/operators/tensor/math/neg.cairo @@ -0,0 +1,32 @@ +use array::ArrayTrait; +use option::OptionTrait; +use array::SpanTrait; + +use orion::operators::tensor::core::{Tensor, TensorTrait}; +use orion::numbers::NumberTrait; + +/// Cf: TensorTrait::neg docstring +fn neg< + T, + MAG, + impl TTensor: TensorTrait, + impl TNumberTrait: NumberTrait, + impl TCopy: Copy, + impl TDrop: Drop +>( + mut z: Tensor +) -> Tensor { + let mut data_result = ArrayTrait::::new(); + loop { + match z.data.pop_front() { + Option::Some(item) => { + data_result.append((*item).neg()); + }, + Option::None(_) => { + break; + } + }; + }; + + return TensorTrait::::new(z.shape, data_result.span()); +} From 247e817e46180e605caa6e62fbce1ec708c97083 Mon Sep 17 00:00:00 2001 From: 0x73e <132935850+0x73e@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:43:19 -0400 Subject: [PATCH 2/5] add unit test --- nodegen/node/neg.py | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 nodegen/node/neg.py diff --git a/nodegen/node/neg.py b/nodegen/node/neg.py new file mode 100644 index 000000000..a78456428 --- /dev/null +++ b/nodegen/node/neg.py @@ -0,0 +1,55 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl + + +class Neg(RunAll): + @staticmethod + def neg_i32(): + x = np.random.randint(-127, 127, (2, 2)).astype(np.int32) + y = np.negative(x) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "neg_i32" + make_node([x], [y], name) + make_test([x], y, "input_0.neg()", name) + + @staticmethod + def neg_i8(): + x = np.random.randint(-127, 127, (2, 2)).astype(np.int8) + y = np.negative(x) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + + name = "neg_i8" + make_node([x], [y], name) + make_test([x], y, "input_0.neg()", name) + + @staticmethod + def neg_fp8x23(): + x = to_fp(np.random.randint(-127, 127, (2, 2) + ).astype(np.int64), FixedImpl.FP8x23) + y = np.negative(x) + + x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) + + name = "neg_fp8x23" + make_node([x], [y], name) + make_test([x], y, "input_0.neg()", name) + + @staticmethod + def neg_fp16x16(): + x = to_fp(np.random.randint(-127, 127, (2, 2) + ).astype(np.int64), FixedImpl.FP16x16) + y = np.negative(x) + + x = Tensor(Dtype.FP16x16, x.shape, x.flatten()) + y = Tensor(Dtype.FP16x16, y.shape, y.flatten()) + + name = "neg_fp16x16" + make_node([x], [y], name) + make_test([x], y, "input_0.neg()", name) From a632260adadc1349d925a944b98794636f198e16 Mon Sep 17 00:00:00 2001 From: 0x73e <132935850+0x73e@users.noreply.github.com> Date: Mon, 23 Oct 2023 05:12:11 -0400 Subject: [PATCH 3/5] docgen + nodegen --- docs/framework/operators/tensor/tensor.neg.md | 39 +++++++++++++++++++ tests/src/nodes.cairo | 4 ++ tests/src/nodes/neg_fp16x16.cairo | 20 ++++++++++ tests/src/nodes/neg_fp16x16/input_0.cairo | 18 +++++++++ tests/src/nodes/neg_fp16x16/output_0.cairo | 18 +++++++++ tests/src/nodes/neg_fp8x23.cairo | 20 ++++++++++ tests/src/nodes/neg_fp8x23/input_0.cairo | 18 +++++++++ tests/src/nodes/neg_fp8x23/output_0.cairo | 18 +++++++++ tests/src/nodes/neg_i32.cairo | 20 ++++++++++ tests/src/nodes/neg_i32/input_0.cairo | 17 ++++++++ tests/src/nodes/neg_i32/output_0.cairo | 17 ++++++++ tests/src/nodes/neg_i8.cairo | 20 ++++++++++ tests/src/nodes/neg_i8/input_0.cairo | 17 ++++++++ tests/src/nodes/neg_i8/output_0.cairo | 17 ++++++++ 14 files changed, 263 insertions(+) create mode 100644 docs/framework/operators/tensor/tensor.neg.md create mode 100644 tests/src/nodes/neg_fp16x16.cairo create mode 100644 tests/src/nodes/neg_fp16x16/input_0.cairo create mode 100644 tests/src/nodes/neg_fp16x16/output_0.cairo create mode 100644 tests/src/nodes/neg_fp8x23.cairo create mode 100644 tests/src/nodes/neg_fp8x23/input_0.cairo create mode 100644 tests/src/nodes/neg_fp8x23/output_0.cairo create mode 100644 tests/src/nodes/neg_i32.cairo create mode 100644 tests/src/nodes/neg_i32/input_0.cairo create mode 100644 tests/src/nodes/neg_i32/output_0.cairo create mode 100644 tests/src/nodes/neg_i8.cairo create mode 100644 tests/src/nodes/neg_i8/input_0.cairo create mode 100644 tests/src/nodes/neg_i8/output_0.cairo diff --git a/docs/framework/operators/tensor/tensor.neg.md b/docs/framework/operators/tensor/tensor.neg.md new file mode 100644 index 000000000..f5c9b3816 --- /dev/null +++ b/docs/framework/operators/tensor/tensor.neg.md @@ -0,0 +1,39 @@ +#tensor.neg + +```rust + fn neg(self: @Tensor) -> Tensor; +``` + +Computes the negation of all elements in the input tensor. + +## Args + +* `self`(`@Tensor`) - The input tensor. + + +## Returns + +A new `Tensor` of the same shape as the input tensor with +the negation of all elements in the input tensor. + +## Example + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, I32Tensor}; +use orion::numbers::{i32, IntegerTrait}; + +fn neg_example() -> Tensor { + let tensor = TensorTrait::new( + shape: array![3].span(), + data: array![ + IntegerTrait::new(1, true), IntegerTrait::new(2, true), IntegerTrait::new(3, false) + ] + .span(), + ); + + return tensor.neg(); +} +>>> [1, 2, -3] +``` diff --git a/tests/src/nodes.cairo b/tests/src/nodes.cairo index 398edc4bf..2d4cc383a 100644 --- a/tests/src/nodes.cairo +++ b/tests/src/nodes.cairo @@ -433,3 +433,7 @@ mod clip_i8_2d; mod clip_i8_3d; mod clip_u32_2d; mod clip_u32_3d; +mod neg_fp16x16; +mod neg_fp8x23; +mod neg_i32; +mod neg_i8; diff --git a/tests/src/nodes/neg_fp16x16.cairo b/tests/src/nodes/neg_fp16x16.cairo new file mode 100644 index 000000000..5c0731454 --- /dev/null +++ b/tests/src/nodes/neg_fp16x16.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_neg_fp16x16() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.neg(); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/neg_fp16x16/input_0.cairo b/tests/src/nodes/neg_fp16x16/input_0.cairo new file mode 100644 index 000000000..dc17867bd --- /dev/null +++ b/tests/src/nodes/neg_fp16x16/input_0.cairo @@ -0,0 +1,18 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP16x16Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP16x16; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 1507328, sign: true }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 2097152, sign: true }); + data.append(FP16x16 { mag: 7274496, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/neg_fp16x16/output_0.cairo b/tests/src/nodes/neg_fp16x16/output_0.cairo new file mode 100644 index 000000000..cac798f63 --- /dev/null +++ b/tests/src/nodes/neg_fp16x16/output_0.cairo @@ -0,0 +1,18 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP16x16Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP16x16; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 983040, sign: true }); + data.append(FP16x16 { mag: 2097152, sign: false }); + data.append(FP16x16 { mag: 7274496, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/neg_fp8x23.cairo b/tests/src/nodes/neg_fp8x23.cairo new file mode 100644 index 000000000..349c6fc08 --- /dev/null +++ b/tests/src/nodes/neg_fp8x23.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_neg_fp8x23() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.neg(); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/neg_fp8x23/input_0.cairo b/tests/src/nodes/neg_fp8x23/input_0.cairo new file mode 100644 index 000000000..5a59a5874 --- /dev/null +++ b/tests/src/nodes/neg_fp8x23/input_0.cairo @@ -0,0 +1,18 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP8x23Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP8x23; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 1015021568, sign: true }); + data.append(FP8x23 { mag: 109051904, sign: true }); + data.append(FP8x23 { mag: 637534208, sign: false }); + data.append(FP8x23 { mag: 92274688, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/neg_fp8x23/output_0.cairo b/tests/src/nodes/neg_fp8x23/output_0.cairo new file mode 100644 index 000000000..10fd45912 --- /dev/null +++ b/tests/src/nodes/neg_fp8x23/output_0.cairo @@ -0,0 +1,18 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP8x23Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP8x23; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 1015021568, sign: false }); + data.append(FP8x23 { mag: 109051904, sign: false }); + data.append(FP8x23 { mag: 637534208, sign: true }); + data.append(FP8x23 { mag: 92274688, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/neg_i32.cairo b/tests/src/nodes/neg_i32.cairo new file mode 100644 index 000000000..fa738e698 --- /dev/null +++ b/tests/src/nodes/neg_i32.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_neg_i32() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.neg(); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/neg_i32/input_0.cairo b/tests/src/nodes/neg_i32/input_0.cairo new file mode 100644 index 000000000..46b571d16 --- /dev/null +++ b/tests/src/nodes/neg_i32/input_0.cairo @@ -0,0 +1,17 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 105, sign: true }); + data.append(i32 { mag: 124, sign: true }); + data.append(i32 { mag: 53, sign: true }); + data.append(i32 { mag: 77, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/neg_i32/output_0.cairo b/tests/src/nodes/neg_i32/output_0.cairo new file mode 100644 index 000000000..de0636cda --- /dev/null +++ b/tests/src/nodes/neg_i32/output_0.cairo @@ -0,0 +1,17 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 105, sign: false }); + data.append(i32 { mag: 124, sign: false }); + data.append(i32 { mag: 53, sign: false }); + data.append(i32 { mag: 77, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/neg_i8.cairo b/tests/src/nodes/neg_i8.cairo new file mode 100644 index 000000000..decb2e9aa --- /dev/null +++ b/tests/src/nodes/neg_i8.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_neg_i8() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.neg(); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/neg_i8/input_0.cairo b/tests/src/nodes/neg_i8/input_0.cairo new file mode 100644 index 000000000..6f0c3c00a --- /dev/null +++ b/tests/src/nodes/neg_i8/input_0.cairo @@ -0,0 +1,17 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 89, sign: false }); + data.append(i8 { mag: 18, sign: true }); + data.append(i8 { mag: 113, sign: false }); + data.append(i8 { mag: 63, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/neg_i8/output_0.cairo b/tests/src/nodes/neg_i8/output_0.cairo new file mode 100644 index 000000000..e339959f3 --- /dev/null +++ b/tests/src/nodes/neg_i8/output_0.cairo @@ -0,0 +1,17 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 89, sign: true }); + data.append(i8 { mag: 18, sign: false }); + data.append(i8 { mag: 113, sign: true }); + data.append(i8 { mag: 63, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file From 298eac22d1a56c12b8364b40961609ddcbcc553a Mon Sep 17 00:00:00 2001 From: 0x73e <132935850+0x73e@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:32:52 -0400 Subject: [PATCH 4/5] add wide impls --- src/numbers.cairo | 8 ++++++++ .../tensor/implementations/tensor_fp16x16wide.cairo | 4 ++++ .../tensor/implementations/tensor_fp8x23wide.cairo | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/src/numbers.cairo b/src/numbers.cairo index 77afa1409..c861c39e8 100644 --- a/src/numbers.cairo +++ b/src/numbers.cairo @@ -346,6 +346,10 @@ impl FP8x23WNumber of NumberTrait { core_fp8x23wide::abs(self) } + fn neg(self: FP8x23W) -> FP8x23W { + core_fp8x23wide::neg(self) + } + fn min_value() -> FP8x23W { FP8x23W { mag: core_fp8x23wide::MAX, sign: true } } @@ -680,6 +684,10 @@ impl FP16x16WNumber of NumberTrait { core_fp16x16wide::abs(self) } + fn neg(self: FP16x16W) -> FP16x16W { + core_fp16x16wide::neg(self) + } + fn min_value() -> FP16x16W { FP16x16W { mag: core_fp16x16wide::MAX, sign: true } } diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 0a89fe72d..69a39703c 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -106,6 +106,10 @@ impl FP16x16WTensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { math::ceil::ceil(*self) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index 91ca9338d..6a9358a09 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -100,6 +100,10 @@ impl FP8x23WTensor of TensorTrait { math::abs::abs(*self) } + fn neg(self: @Tensor) -> Tensor { + math::neg::neg(*self) + } + fn ceil(self: @Tensor) -> Tensor { math::ceil::ceil(*self) } From f862acb7e2dbba7535608ec4fd00e6e363b24ce6 Mon Sep 17 00:00:00 2001 From: raphaelDkhn Date: Tue, 24 Oct 2023 10:45:54 +0300 Subject: [PATCH 5/5] add missing element in doc --- docs/SUMMARY.md | 1 + docs/framework/compatibility.md | 1 + docs/framework/operators/tensor/README.md | 1 + src/operators/tensor/core.cairo | 1 + 4 files changed, 4 insertions(+) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 07de3f249..50dac5b30 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -59,6 +59,7 @@ * [tensor.less](framework/operators/tensor/tensor.less.md) * [tensor.less\_equal](framework/operators/tensor/tensor.less\_equal.md) * [tensor.abs](framework/operators/tensor/tensor.abs.md) + * [tensor.neg](framework/operators/tensor/tensor.neg.md) * [tensor.ceil](framework/operators/tensor/tensor.ceil.md) * [tensor.cumsum](framework/operators/tensor/tensor.cumsum.md) * [tensor.sin](framework/operators/tensor/tensor.sin.md) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index a7b415cc6..41e40258d 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -18,6 +18,7 @@ You can see below the list of current supported ONNX Operators: | [Less](operators/tensor/tensor.less.md) | :white\_check\_mark: | | [LessOrEqual](operators/tensor/tensor.less\_equal.md) | :white\_check\_mark: | | [Abs](operators/tensor/tensor.abs.md) | :white\_check\_mark: | +| [Neg](operators/tensor/tensor.neg.md) | :white\_check\_mark: | | [Ceil](operators/tensor/tensor.ceil.md) | :white\_check\_mark: | | [Exp](operators/tensor/tensor.exp.md) | :white\_check\_mark: | | [Ln](operators/tensor/tensor.log.md) | :white\_check\_mark: | diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index b33f330c7..2e5d727d6 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -60,6 +60,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.exp`](tensor.exp.md) | Computes the exponential of all elements of the input tensor. | | [`tensor.log`](tensor.log.md) | Computes the natural log of all elements of the input tensor. | | [`tensor.abs`](tensor.abs.md) | Computes the absolute value of all elements in the input tensor. | +| [`tensor.neg`](tensor.neg.md) | Computes the negation of all elements in the input tensor. | | [`tensor.ceil`](tensor.ceil.md) | Rounds up the value of each element in the input tensor. | | [`tensor.sqrt`](tensor.sqrt.md) | Computes the square root of all elements of the input tensor. | | [`tensor.sin`](tensor.sin.md) | Computes the sine of all elements of the input tensor. | diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 4b9c9ed52..bfddbc8fc 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -56,6 +56,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde