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: add sign #226

Merged
merged 3 commits into from
Oct 6, 2023
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
3 changes: 3 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [int.abs](framework/numbers/signed-integer/int.abs.md)
* [int.max](framework/numbers/signed-integer/int.max.md)
* [int.min](framework/numbers/signed-integer/int.min.md)
* [int.sign](framework/numbers/signed-integer/int.sign.md)
* [Fixed Point](framework/numbers/fixed-point/README.md)
* [fp.new](framework/numbers/fixed-point/fp.new.md)
* [fp.new\_unscaled](framework/numbers/fixed-point/fp.new\_unscaled.md)
Expand All @@ -34,6 +35,7 @@
* [fp.sqrt](framework/numbers/fixed-point/fp.sqrt.md)
* [fp.sin](framework/numbers/fixed-point/fp.sin.md)
* [fp.atan](framework/numbers/fixed-point/fp.atan.md)
* [fp.sign](framework/numbers/fixed-point/fp.sign.md)
* [Operators](framework/operators/README.md)
* [Tensor](framework/operators/tensor/README.md)
* [tensor.new](framework/operators/tensor/tensor.new.md)
Expand Down Expand Up @@ -82,6 +84,7 @@
* [tensor.nonzero](framework/operators/tensor/tensor.nonzero.md)
* [tensor.squeeze](framework/operators/tensor/tensor.squeeze.md)
* [tensor.unsqueeze](framework/operators/tensor/tensor.unsqueeze.md)
* [tensor.sign](framework/operators/tensor/tensor.sign.md)
* [tensor.clip](framework/operators/tensor/tensor.clip.md)
* [Neural Network](framework/operators/neural-network/README.md)
* [nn.relu](framework/operators/neural-network/nn.relu.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 @@ -55,6 +55,7 @@ You can see below the list of current supported ONNX Operators:
| [Nonzero](operators/tensor/tensor.nonzero.md) | :white\_check\_mark: |
| [Squeeze](operators/tensor/tensor.squeeze.md) | :white\_check\_mark: |
| [Unsqueeze](operators/tensor/tensor.unsqueeze.md) | :white\_check\_mark: |
| [Sign](operators/tensor/tensor.sign.md) | :white\_check\_mark: |
| [Clip](operators/tensor/tensor.clip.md) | :white\_check\_mark: |

Current Operators support: **50/156 (32%)**
1 change: 1 addition & 0 deletions docs/framework/numbers/fixed-point/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use orion::numbers::fixed_point::core::FixedTrait;
| [`fp.cosh`](fp.cosh.md) | Returns the value of the hyperbolic cosine of the fixed point number. |
| [`fp.sinh`](fp.sinh.md) | Returns the value of the hyperbolic sine of the fixed point number. |
| [`fp.tanh`](fp.tanh.md) | Returns the value of the hyperbolic tangent of the fixed point number. |
| [`fp.sign`](fp.sign.md) | Returns the element-wise indication of the sign of the input fixed point number. |

### Arithmetic & Comparison operators

Expand Down
30 changes: 30 additions & 0 deletions docs/framework/numbers/fixed-point/fp.sign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# fp.sign

```rust
fn sign(self: T) -> T;
```

Returns the element-wise indication of the sign of the input fixed point number.

## Args

* `self`(`T`) - The input fixed point

## Returns

The element-wise indication of the sign of the input fixed point number.

## Examples

```rust
use orion::numbers::{FP16x16, FP16x16Impl, FixedTrait};

fn sign_fp_example() -> FP16x16 {
// We instantiate fixed point here.
let fp = FixedTrait::new_unscaled(2, true);

// We can call `sign` function as follows.
fp.sign()
}
>>> {mag: 65536, sign: true} // = -1
```
1 change: 1 addition & 0 deletions docs/framework/numbers/signed-integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use orion::numbers::signed_integer::IntegerTrait;
| [`int.abs`](int.abs.md) | Computes the absolute value of the given `signed_integer` |
| [`int.max`](int.max.md) | Returns the maximum between two `signed_integer` |
| [`int.min`](int.min.md) | Returns the minimum between two `signed_integer` |
| [`int.sign`](int.sign.md) | Returns an element-wise indication of the given `signed_integer` |

### Arithmetic & Comparison operators

Expand Down
29 changes: 29 additions & 0 deletions docs/framework/numbers/signed-integer/int.sign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# int.sign

```rust
fn sign(self: T, other: T) -> T;
```

Returns an element-wise indication of the given signed_integer.

## Args

`self`(`T`) - The input value to which the signed value is applied.

## Returns

An element-wise indication of the sign of a number.

## Examples


```rust
fn sign_example() -> i32 {
// We instantiate signed integer here.
let a = IntegerTrait::<i32>::new(42, true);

// We can call `sign` function as follows.
a.sign()
}
>>> {mag: 1, sign: true}
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Orion supports currently only fixed point data types for `TreeRegressorTrait`.
| --- | --- |
| [`tree.fit`](tree.fit.md) | Constructs a decision tree regressor based on the provided data and target values. |
| [`tree.predict`](tree.predict.md) | Given a set of features, predicts the target value using the constructed decision tree. |

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tree.predict
# TreeRegressorTrait::predict

```rust
```rust
fn predict(ref self: TreeNode<T>, features: Span<T>) -> T;
```

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 @@ -80,6 +80,7 @@ use orion::operators::tensor::TensorTrait;
| [`tensor.nonzero`](tensor.nonzero.md) | Produces indices of the elements that are non-zero (in row-major order - by dimension). |
| [`tensor.squeeze`](tensor.squeeze.md) | Removes dimensions of size 1 from the shape of a tensor. |
| [`tensor.unsqueeze`](tensor.unsqueeze.md) | Inserts single-dimensional entries to the shape of an input tensor. |
| [`tensor.sign`](tensor.sign.md) | Calculates the sign of the given input tensor element-wise. |
| [`tensor.clip`](tensor.clip.md) | Clip operator limits the given input within an interval. |

## Arithmetic Operations
Expand Down
34 changes: 34 additions & 0 deletions docs/framework/operators/tensor/tensor.sign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# tensor.sign

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

Calculates the sign of the given input tensor element-wise.
If input > 0, output 1. if input < 0, output -1. if input == 0, output 0.

## Args

* `self`(`@Tensor<T>`) - Tensor of data to calculates the sign of the given input tensor element-wise.

## Returns

A new `Tensor<T>` of the same shape as the input tensor with The sign of the input tensor computed element-wise.

## Example

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

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

fn sign_example() -> Tensor<FP8x23> {
let tensor = TensorTrait::<FP8x23>::new(
shape: array![11].span(),
data: array![-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5].span(),
);

return tensor.sign();
}
>>> [-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]
```
82 changes: 82 additions & 0 deletions nodegen/node/sign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import numpy as np
from nodegen.node import RunAll
from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl

class Sign(RunAll):
@staticmethod
def sign_i8():
def sign():
x = np.array(range(-5, 6)).astype(np.int8)
y = np.array([-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]).astype(np.int8)

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

name = "sign_i8"
make_node([x], [y], name)
make_test(
[x], y, "input_0.sign()", name)
sign()

@staticmethod
def sign_i32():
def sign():
x = np.array(range(-5, 6)).astype(np.int32)
y = np.array([-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]).astype(np.int32)

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

name = "sign_i32"
make_node([x], [y], name)
make_test(
[x], y, "input_0.sign()", name)
sign()

@staticmethod
def sign_fail():
def sign():

x = np.array(range(-5, 6)).astype(np.int32)
y = np.array([1, -1, -1, -1, -1, 0, 1, 1, 1, 1, -1]).astype(np.int32)

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

name = "sign_fail"
make_node([x], [y], name)
make_test(
[x], y, "input_0.sign()", name)
sign()

@staticmethod
def sign_fP16x16():
def sign():

x = to_fp (np.array(range(-5, 6)).astype(np.int64), FixedImpl.FP16x16)
y = to_fp (np.array([-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]).astype(np.int64), FixedImpl.FP16x16)

x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())

name = "sign_fP16x16"
make_node([x], [y], name)
make_test(
[x], y, "input_0.sign()", name)
sign()

@staticmethod
def sign_fP8x23():
def sign():

x = to_fp (np.array(range(-5, 6)).astype(np.int64), FixedImpl.FP8x23)
y = to_fp (np.array([-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]).astype(np.int64), FixedImpl.FP8x23)

x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())

name = "sign_fP8x23"
make_node([x], [y], name)
make_test(
[x], y, "input_0.sign()", name)
sign()
41 changes: 41 additions & 0 deletions src/numbers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ trait NumberTrait<T, MAG> {
fn is_neg(self: T) -> bool;
fn xor(lhs: T, rhs: T) -> bool;
fn or(lhs: T, rhs: T) -> bool;
fn sign(self: T) -> T;
}

use orion::numbers::fixed_point::implementations::fp8x23::core::{FP8x23Impl, FP8x23};
Expand Down Expand Up @@ -206,6 +207,10 @@ impl FP8x23Number of NumberTrait<FP8x23, u32> {
fn or(lhs: FP8x23, rhs: FP8x23) -> bool {
comp_fp8x23::or(lhs, rhs)
}

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

use orion::numbers::fixed_point::implementations::fp16x16::core::{FP16x16Impl, FP16x16};
Expand Down Expand Up @@ -367,6 +372,10 @@ impl FP16x16Number of NumberTrait<FP16x16, u32> {
fn or(lhs: FP16x16, rhs: FP16x16) -> bool {
comp_fp16x16::or(lhs, rhs)
}

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

use orion::numbers::fixed_point::implementations::fp64x64::core::{FP64x64Impl, FP64x64};
Expand Down Expand Up @@ -529,6 +538,10 @@ impl FP64x64Number of NumberTrait<FP64x64, u128> {
fn or(lhs: FP64x64, rhs: FP64x64) -> bool {
comp_fp64x64::or(lhs, rhs)
}

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

use orion::numbers::fixed_point::implementations::fp32x32::core::{FP32x32Impl, FP32x32};
Expand Down Expand Up @@ -691,6 +704,10 @@ impl FP32x32Number of NumberTrait<FP32x32, u64> {
fn or(lhs: FP32x32, rhs: FP32x32) -> bool {
comp_fp32x32::or(lhs, rhs)
}

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

use orion::numbers::signed_integer::i8 as i8_core;
Expand Down Expand Up @@ -859,6 +876,10 @@ impl I8Number of NumberTrait<i8, u8> {
return true;
}
}

fn sign(self: i8) -> i8 {
i8_core::i8_sign(self)
}
}

use orion::numbers::signed_integer::i16 as i16_core;
Expand Down Expand Up @@ -1027,6 +1048,10 @@ impl i16Number of NumberTrait<i16, u16> {
return true;
}
}

fn sign(self: i16) -> i16 {
i16_core::i16_sign(self)
}
}

use orion::numbers::signed_integer::i32 as i32_core;
Expand Down Expand Up @@ -1195,6 +1220,10 @@ impl i32Number of NumberTrait<i32, u32> {
return true;
}
}

fn sign(self: i32) -> i32 {
i32_core::i32_sign(self)
}
}

use orion::numbers::signed_integer::i64 as i64_core;
Expand Down Expand Up @@ -1363,6 +1392,10 @@ impl i64Number of NumberTrait<i64, u64> {
return true;
}
}

fn sign(self: i64) -> i64 {
i64_core::i64_sign(self)
}
}

use orion::numbers::signed_integer::i128 as i128_core;
Expand Down Expand Up @@ -1532,6 +1565,10 @@ impl i128Number of NumberTrait<i128, u128> {
return true;
}
}

fn sign(self: i128) -> i128 {
i128_core::i128_sign(self)
}
}

impl u32Number of NumberTrait<u32, u32> {
Expand Down Expand Up @@ -1706,4 +1743,8 @@ impl u32Number of NumberTrait<u32, u32> {
return true;
}
}

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