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 identity #392

Merged
merged 2 commits into from
Oct 24, 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
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
* [tensor.unsqueeze](framework/operators/tensor/tensor.unsqueeze.md)
* [tensor.sign](framework/operators/tensor/tensor.sign.md)
* [tensor.clip](framework/operators/tensor/tensor.clip.md)
* [tensor.identity](framework/operators/tensor/tensor.identity.md)
* [Neural Network](framework/operators/neural-network/README.md)
* [nn.relu](framework/operators/neural-network/nn.relu.md)
* [nn.leaky\_relu](framework/operators/neural-network/nn.leaky\_relu.md)
Expand Down
3 changes: 2 additions & 1 deletion docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ You can see below the list of current supported ONNX Operators:
| [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: |
| [Identity](operators/tensor/tensor.identity.md) | :white\_check\_mark: |

Current Operators support: **50/156 (32%)**
Current Operators support: **51/156 (33%)**
1 change: 1 addition & 0 deletions docs/framework/operators/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ use orion::operators::tensor::TensorTrait;
| [`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. |
| [`tensor.identity`](tensor.identity.md) | Return a Tensor with the same shape and contents as input. |

## Arithmetic Operations

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

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

Return a Tensor with the same shape and contents as input.

## Args

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

## Returns

A new `Tensor<T>` to copy input into.

## Example

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

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

fn identity_example() -> Tensor<FP16x16> {
let tensor = TensorTrait::<FP16x16>::new(
shape: array![2, 2].span(),
data: array![1, 2, 3, 4].span(),
);
let t_identity = tensor.identity();
t_identity
}
>>> [[1 2] [3 4]] // A Tensor with the same shape and contents as input
```
86 changes: 86 additions & 0 deletions nodegen/node/identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import numpy as np
from nodegen.node import RunAll
from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl


class Identity(RunAll):

@staticmethod
def identity_fP8x23():
def identity():
x = np.array([[1, 2], [3, 4]])
y = x

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

name = "identity_fP8x23"
make_node([x], [y], name)

make_test(
[x], y, "input_0.identity()", name)
identity()

@staticmethod
def identity_fP16x16():
def identity():
x = np.array([[1, 2], [3, 4]])
y = x

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

name = "identity_fP16x16"
make_node([x], [y], name)

make_test(
[x], y, "input_0.identity()", name)
identity()

@staticmethod
def identity_i8():
def identity():
x = np.array([[1, 2], [3, 4]])
y = x

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

name = "identity_i8"
make_node([x], [y], name)

make_test(
[x], y, "input_0.identity()", name)
identity()

@staticmethod
def identity_i32():
def identity():
x = np.array([[1, 2], [3, 4]])
y = x

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

name = "identity_i32"
make_node([x], [y], name)

make_test(
[x], y, "input_0.identity()", name)
identity()

@staticmethod
def identity_u32():
def identity():
x = np.array([[1, 2], [3, 4]])
y = x

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

name = "identity_u32"
make_node([x], [y], name)

make_test(
[x], y, "input_0.identity()", name)
identity()
44 changes: 44 additions & 0 deletions src/operators/tensor/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl TensorSerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Tensor<
/// unsqueeze - Inserts single-dimensional entries to the shape of an input tensor.
/// sign - Calculates the sign of the given input tensor element-wise.
/// clip - Clip operator limits the given input within an interval.
/// identity - Return a Tensor with the same shape and contents as input.
///
trait TensorTrait<T> {
/// # tensor.new
Expand Down Expand Up @@ -2711,6 +2712,41 @@ trait TensorTrait<T> {
/// ```
///
fn sign(self: @Tensor<T>) -> Tensor<T>;
/// # tensor.identity
///
/// ```rust
/// fn identity(self: @Tensor<T>) -> Tensor<T>;
/// ```
///
/// Return a Tensor with the same shape and contents as input.
///
/// ## Args
///
/// * `self`(`@Tensor<T>`) - Input tensor.
///
/// ## Returns
///
/// A new `Tensor<T>` to copy input into.
///
/// ## Example
///
/// ```rust
/// use array::{ArrayTrait, SpanTrait};
///
/// use orion::operators::tensor::{TensorTrait, Tensor, FP16x16Tensor};
///
/// fn identity_example() -> Tensor<FP16x16> {
/// let tensor = TensorTrait::<FP16x16>::new(
/// shape: array![2, 2].span(),
/// data: array![1, 2, 3, 4].span(),
/// );
/// let t_identity = tensor.identity();
/// t_identity
/// }
/// >>> [[1 2] [3 4]] // A Tensor with the same shape and contents as input
/// ```
///
fn identity(self: @Tensor<T>) -> Tensor<T>;
}

/// Cf: TensorTrait::new docstring
Expand Down Expand Up @@ -3285,3 +3321,11 @@ fn clip<

return Tensor::<T> { shape: *self.shape, data: return_data.span() };
}

/// Cf: TensorTrait::identity docstring
fn identity<T>
(
self: @Tensor<T>
) -> Tensor<T> {
Tensor::<T> { shape: *self.shape, data: *self.data }
}
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_fp16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ impl FP16x16Tensor of TensorTrait<FP16x16> {
fn clip(self: @Tensor<FP16x16>, min: Option<FP16x16>, max: Option<FP16x16>) -> Tensor<FP16x16> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<FP16x16>) -> Tensor<FP16x16> {
core::identity(self)
}

}

/// Implements addition for `Tensor<FP16x16>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_fp16x16wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ impl FP16x16WTensor of TensorTrait<FP16x16W> {
) -> Tensor<FP16x16W> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<FP16x16W>) -> Tensor<FP16x16W> {
core::identity(self)
}

}

/// Implements addition for `Tensor<FP16x16W>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_fp32x32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ impl FP32x32Tensor of TensorTrait<FP32x32> {
fn clip(self: @Tensor<FP32x32>, min: Option<FP32x32>, max: Option<FP32x32>) -> Tensor<FP32x32> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<FP32x32>) -> Tensor<FP32x32> {
core::identity(self)
}

}

/// Implements addition for `Tensor<FP32x32>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_fp64x64.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ impl FP64x64Tensor of TensorTrait<FP64x64> {
fn clip(self: @Tensor<FP64x64>, min: Option<FP64x64>, max: Option<FP64x64>) -> Tensor<FP64x64> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<FP64x64>) -> Tensor<FP64x64> {
core::identity(self)
}

}

/// Implements addition for `Tensor<FP64x64>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_fp8x23.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ impl FP8x23Tensor of TensorTrait<FP8x23> {
fn clip(self: @Tensor<FP8x23>, min: Option<FP8x23>, max: Option<FP8x23>) -> Tensor<FP8x23> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<FP8x23>) -> Tensor<FP8x23> {
core::identity(self)
}

}

/// Implements addition for `Tensor<FP8x23>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_fp8x23wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ impl FP8x23WTensor of TensorTrait<FP8x23W> {
fn clip(self: @Tensor<FP8x23W>, min: Option<FP8x23W>, max: Option<FP8x23W>) -> Tensor<FP8x23W> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<FP8x23W>) -> Tensor<FP8x23W> {
core::identity(self)
}

}

/// Implements addition for `Tensor<FP8x23W>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_i32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ impl I32Tensor of TensorTrait<i32> {
fn clip(self: @Tensor<i32>, min: Option<i32>, max: Option<i32>) -> Tensor<i32> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<i32>) -> Tensor<i32> {
core::identity(self)
}

}

/// Implements addition for `Tensor<i32>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_i8.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ impl I8Tensor of TensorTrait<i8> {
fn clip(self: @Tensor<i8>, min: Option<i8>, max: Option<i8>) -> Tensor<i8> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<i8>) -> Tensor<i8> {
core::identity(self)
}

}

/// Implements addition for `Tensor<i8>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions src/operators/tensor/implementations/tensor_u32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ impl U32Tensor of TensorTrait<u32> {
fn clip(self: @Tensor<u32>, min: Option<u32>, max: Option<u32>) -> Tensor<u32> {
core::clip(self, min, max)
}

fn identity(self: @Tensor<u32>) -> Tensor<u32> {
core::identity(self)
}

}

/// Implements addition for `Tensor<u32>` using the `Add` trait.
Expand Down
5 changes: 5 additions & 0 deletions tests/src/nodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ mod clip_i8_2d;
mod clip_i8_3d;
mod clip_u32_2d;
mod clip_u32_3d;
mod identity_fP16x16;
mod identity_fP8x23;
mod identity_i32;
mod identity_i8;
mod identity_u32;
mod thresholded_relu_fp16x16;
mod thresholded_relu_fp8x23;
mod hard_sigmoid_fp8x23;
Expand Down
19 changes: 19 additions & 0 deletions tests/src/nodes/identity_fP16x16.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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_identity_fP16x16() {
let input_0 = input_0::input_0();
let z = output_0::output_0();

let y = input_0.identity();

assert_eq(y, z);
}
18 changes: 18 additions & 0 deletions tests/src/nodes/identity_fP16x16/input_0.cairo
Original file line number Diff line number Diff line change
@@ -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<FP16x16> {
let mut shape = ArrayTrait::<usize>::new();
shape.append(2);
shape.append(2);

let mut data = ArrayTrait::new();
data.append(FP16x16 { mag: 1, sign: false });
data.append(FP16x16 { mag: 2, sign: false });
data.append(FP16x16 { mag: 3, sign: false });
data.append(FP16x16 { mag: 4, sign: false });
TensorTrait::new(shape.span(), data.span())
}
18 changes: 18 additions & 0 deletions tests/src/nodes/identity_fP16x16/output_0.cairo
Original file line number Diff line number Diff line change
@@ -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<FP16x16> {
let mut shape = ArrayTrait::<usize>::new();
shape.append(2);
shape.append(2);

let mut data = ArrayTrait::new();
data.append(FP16x16 { mag: 1, sign: false });
data.append(FP16x16 { mag: 2, sign: false });
data.append(FP16x16 { mag: 3, sign: false });
data.append(FP16x16 { mag: 4, sign: false });
TensorTrait::new(shape.span(), data.span())
}
Loading