Skip to content

Commit

Permalink
Merge pull request #388 from 0x73e/feat/neg
Browse files Browse the repository at this point in the history
feat: neg operator
  • Loading branch information
raphaelDkhn authored Oct 24, 2023
2 parents 89d637c + f862acb commit 4db8a55
Show file tree
Hide file tree
Showing 31 changed files with 481 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
1 change: 1 addition & 0 deletions docs/framework/operators/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
39 changes: 39 additions & 0 deletions docs/framework/operators/tensor/tensor.neg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#tensor.neg

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

Computes the negation of all elements in the input tensor.

## Args

* `self`(`@Tensor<T>`) - The input tensor.


## Returns

A new `Tensor<T>` 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<i32> {
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]
```
55 changes: 55 additions & 0 deletions nodegen/node/neg.py
Original file line number Diff line number Diff line change
@@ -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)
49 changes: 49 additions & 0 deletions src/numbers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ trait NumberTrait<T, MAG> {
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;
Expand Down Expand Up @@ -176,6 +177,10 @@ impl FP8x23Number of NumberTrait<FP8x23, u32> {
core_fp8x23::abs(self)
}

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

fn min_value() -> FP8x23 {
FP8x23 { mag: core_fp8x23::MAX, sign: true }
}
Expand Down Expand Up @@ -341,6 +346,10 @@ impl FP8x23WNumber of NumberTrait<FP8x23W, u64> {
core_fp8x23wide::abs(self)
}

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

fn min_value() -> FP8x23W {
FP8x23W { mag: core_fp8x23wide::MAX, sign: true }
}
Expand Down Expand Up @@ -506,6 +515,10 @@ impl FP16x16Number of NumberTrait<FP16x16, u32> {
core_fp16x16::abs(self)
}

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

fn min_value() -> FP16x16 {
FP16x16 { mag: core_fp16x16::MAX, sign: true }
}
Expand Down Expand Up @@ -671,6 +684,10 @@ impl FP16x16WNumber of NumberTrait<FP16x16W, u64> {
core_fp16x16wide::abs(self)
}

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

fn min_value() -> FP16x16W {
FP16x16W { mag: core_fp16x16wide::MAX, sign: true }
}
Expand Down Expand Up @@ -837,6 +854,10 @@ impl FP64x64Number of NumberTrait<FP64x64, u128> {
fp64x64::core::abs(self)
}

fn neg(self: FP64x64) -> FP64x64 {
fp64x64::core::neg(self)
}

fn min_value() -> FP64x64 {
FP64x64 { mag: core_fp64x64::MAX, sign: true }
}
Expand Down Expand Up @@ -1003,6 +1024,10 @@ impl FP32x32Number of NumberTrait<FP32x32, u64> {
fp32x32::core::abs(self)
}

fn neg(self: FP32x32) -> FP32x32 {
fp32x32::core::neg(self)
}

fn min_value() -> FP32x32 {
FP32x32 { mag: core_fp32x32::MAX, sign: true }
}
Expand Down Expand Up @@ -1167,6 +1192,10 @@ impl I8Number of NumberTrait<i8, u8> {
i8_core::i8_abs(self)
}

fn neg(self: i8) -> i8 {
i8_core::i8_neg(self)
}

fn min_value() -> i8 {
i8 { mag: 128, sign: true }
}
Expand Down Expand Up @@ -1339,6 +1368,10 @@ impl i16Number of NumberTrait<i16, u16> {
i16_core::i16_abs(self)
}

fn neg(self: i16) -> i16 {
i16_core::i16_neg(self)
}

fn min_value() -> i16 {
i16 { mag: 32768, sign: true }
}
Expand Down Expand Up @@ -1511,6 +1544,10 @@ impl i32Number of NumberTrait<i32, u32> {
i32_core::i32_abs(self)
}

fn neg(self: i32) -> i32 {
i32_core::i32_neg(self)
}

fn min_value() -> i32 {
i32 { mag: 2147483648, sign: true }
}
Expand Down Expand Up @@ -1683,6 +1720,10 @@ impl i64Number of NumberTrait<i64, u64> {
i64_core::i64_abs(self)
}

fn neg(self: i64) -> i64 {
i64_core::i64_neg(self)
}

fn min_value() -> i64 {
i64 { mag: 9223372036854775808, sign: true }
}
Expand Down Expand Up @@ -1856,6 +1897,10 @@ impl i128Number of NumberTrait<i128, u128> {
i128_core::i128_abs(self)
}

fn neg(self: i128) -> i128 {
i128_core::i128_neg(self)
}

fn min_value() -> i128 {
i128 { mag: 170141183460469231731687303715884105728, sign: true }
}
Expand Down Expand Up @@ -2026,6 +2071,10 @@ impl u32Number of NumberTrait<u32, u32> {
self
}

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

fn min_value() -> u32 {
0
}
Expand Down
42 changes: 42 additions & 0 deletions src/operators/tensor/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl TensorSerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Tensor<
/// exp - Computes the exponential of all elements of the input tensor.
/// log - Computes the natural log of all elements of the input tensor.
/// abs - Computes the absolute value of all elements in the input tensor.
/// neg - Computes the negation of all elements in the input tensor.
/// ceil - Rounds up the value of each element in the input tensor.
/// sqrt - Computes the square root of all elements of the input tensor.
/// sin - Computes the sine of all elements of the input tensor.
Expand Down Expand Up @@ -1234,6 +1235,47 @@ trait TensorTrait<T> {
/// ```
///
fn abs(self: @Tensor<T>) -> Tensor<T>;
/// #tensor.neg
///
/// ```rust
/// fn neg(self: @Tensor<T>) -> Tensor<T>;
/// ```
///
/// Computes the negation of all elements in the input tensor.
///
/// ## Args
///
/// * `self`(`@Tensor<T>`) - The input tensor.
///
///
/// ## Returns
///
/// A new `Tensor<T>` 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<i32> {
/// 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<T>) -> Tensor<T>;
/// #tensor.ceil
///
/// ```rust
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_fp16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl FP16x16Tensor of TensorTrait<FP16x16> {
math::abs::abs(*self)
}

fn neg(self: @Tensor<FP16x16>) -> Tensor<FP16x16> {
math::neg::neg(*self)
}

fn ceil(self: @Tensor<FP16x16>) -> Tensor<FP16x16> {
math::ceil::ceil(*self)
}
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_fp16x16wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ impl FP16x16WTensor of TensorTrait<FP16x16W> {
math::abs::abs(*self)
}

fn neg(self: @Tensor<FP16x16W>) -> Tensor<FP16x16W> {
math::neg::neg(*self)
}

fn ceil(self: @Tensor<FP16x16W>) -> Tensor<FP16x16W> {
math::ceil::ceil(*self)
}
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_fp32x32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl FP32x32Tensor of TensorTrait<FP32x32> {
math::abs::abs(*self)
}

fn neg(self: @Tensor<FP32x32>) -> Tensor<FP32x32> {
math::neg::neg(*self)
}

fn ceil(self: @Tensor<FP32x32>) -> Tensor<FP32x32> {
math::ceil::ceil(*self)
}
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_fp64x64.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl FP64x64Tensor of TensorTrait<FP64x64> {
math::abs::abs(*self)
}

fn neg(self: @Tensor<FP64x64>) -> Tensor<FP64x64> {
math::neg::neg(*self)
}

fn ceil(self: @Tensor<FP64x64>) -> Tensor<FP64x64> {
math::ceil::ceil(*self)
}
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_fp8x23.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl FP8x23Tensor of TensorTrait<FP8x23> {
math::abs::abs(*self)
}

fn neg(self: @Tensor<FP8x23>) -> Tensor<FP8x23> {
math::neg::neg(*self)
}

fn ceil(self: @Tensor<FP8x23>) -> Tensor<FP8x23> {
math::ceil::ceil(*self)
}
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_fp8x23wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl FP8x23WTensor of TensorTrait<FP8x23W> {
math::abs::abs(*self)
}

fn neg(self: @Tensor<FP8x23W>) -> Tensor<FP8x23W> {
math::neg::neg(*self)
}

fn ceil(self: @Tensor<FP8x23W>) -> Tensor<FP8x23W> {
math::ceil::ceil(*self)
}
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_i32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl I32Tensor of TensorTrait<i32> {
math::abs::abs(*self)
}

fn neg(self: @Tensor<i32>) -> Tensor<i32> {
math::neg::neg(*self)
}

fn ceil(self: @Tensor<i32>) -> Tensor<i32> {
panic(array!['not supported!'])
}
Expand Down
Loading

0 comments on commit 4db8a55

Please sign in to comment.