-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from andresmayorca/develop
add round
- Loading branch information
Showing
24 changed files
with
337 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#tensor.round | ||
|
||
```rust | ||
fn round(self: @Tensor<T>) -> Tensor<T>; | ||
``` | ||
|
||
Computes the round value 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 round value of all elements in the input tensor. | ||
|
||
## Example | ||
|
||
```rust | ||
use array::{ArrayTrait, SpanTrait}; | ||
|
||
use orion::operators::tensor::{TensorTrait, Tensor, FP16x16Tensor}; | ||
use orion::numbers::{FixedTrait, FP16x16}; | ||
|
||
fn round_example() -> Tensor<FP16x16> { | ||
let tensor = TensorTrait::<FP16x16>::new( | ||
shape: array![3].span(), | ||
data: array![ | ||
FixedTrait::new(190054, false), // 2.9 | ||
] | ||
.span(), | ||
); | ||
|
||
return tensor.round(); | ||
} | ||
>>> [3] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl | ||
|
||
class Round(RunAll): | ||
|
||
@staticmethod | ||
def round_fp8x23(): | ||
x = np.array([0.1, 0.5, 0.9, 1.2, 1.5, 1.8, 2.3, 2.5, 2.7, -1.1, -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float64) | ||
y = np.array([0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, -1.0, -2.0, -2.0, -2.0, -3.0, -3.0]).astype(np.float64) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, to_fp(x.flatten(), FixedImpl.FP8x23)) | ||
y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23)) | ||
|
||
name = "round_fp8x23" | ||
make_node([x], [y], name) | ||
make_test([x], y, "input_0.round()", name) | ||
|
||
@staticmethod | ||
def round_fp16x16(): | ||
x = np.array([0.1, 0.5, 0.9, 1.2, 1.5, 1.8, 2.3, 2.5, 2.7, -1.1, -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float64) | ||
y = np.array([0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, -1.0, -2.0, -2.0, -2.0, -3.0, -3.0]).astype(np.float64) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, to_fp(x.flatten(), FixedImpl.FP16x16)) | ||
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16)) | ||
|
||
name = "round_fp16x16" | ||
make_node([x], [y], name) | ||
make_test([x], y, "input_0.round()", name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,4 @@ mod sign; | |
mod and; | ||
mod neg; | ||
mod where; | ||
mod round; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use array::ArrayTrait; | ||
use array::SpanTrait; | ||
use option::OptionTrait; | ||
use traits::Into; | ||
|
||
use orion::numbers::NumberTrait; | ||
use orion::operators::tensor::core::{Tensor, TensorTrait}; | ||
|
||
|
||
fn round< | ||
T, | ||
MAG, | ||
impl TNumberTrait: NumberTrait<T, MAG>, | ||
impl FTensor: TensorTrait<T>, | ||
impl FCopy: Copy<T>, | ||
impl FDrop: Drop<T> | ||
>( | ||
mut self: Tensor<T> | ||
) -> Tensor<T> { | ||
let mut result = ArrayTrait::new(); | ||
|
||
loop { | ||
match self.data.pop_front() { | ||
Option::Some(item) => { result.append((*item).round()); }, | ||
Option::None(_) => { break; } | ||
}; | ||
}; | ||
|
||
return TensorTrait::new(self.shape, result.span()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_round_fp16x16() { | ||
let input_0 = input_0::input_0(); | ||
let z = output_0::output_0(); | ||
|
||
let y = input_0.round(); | ||
|
||
assert_eq(y, z); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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(15); | ||
|
||
let mut data = ArrayTrait::new(); | ||
data.append(FP16x16 { mag: 6553, sign: false }); | ||
data.append(FP16x16 { mag: 32768, sign: false }); | ||
data.append(FP16x16 { mag: 58982, sign: false }); | ||
data.append(FP16x16 { mag: 78643, sign: false }); | ||
data.append(FP16x16 { mag: 98304, sign: false }); | ||
data.append(FP16x16 { mag: 117964, sign: false }); | ||
data.append(FP16x16 { mag: 150732, sign: false }); | ||
data.append(FP16x16 { mag: 163840, sign: false }); | ||
data.append(FP16x16 { mag: 176947, sign: false }); | ||
data.append(FP16x16 { mag: 72089, sign: true }); | ||
data.append(FP16x16 { mag: 98304, sign: true }); | ||
data.append(FP16x16 { mag: 124518, sign: true }); | ||
data.append(FP16x16 { mag: 144179, sign: true }); | ||
data.append(FP16x16 { mag: 163840, sign: true }); | ||
data.append(FP16x16 { mag: 183500, sign: true }); | ||
TensorTrait::new(shape.span(), data.span()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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(15); | ||
|
||
let mut data = ArrayTrait::new(); | ||
data.append(FP16x16 { mag: 0, sign: false }); | ||
data.append(FP16x16 { mag: 65536, sign: false }); | ||
data.append(FP16x16 { mag: 65536, sign: false }); | ||
data.append(FP16x16 { mag: 65536, sign: false }); | ||
data.append(FP16x16 { mag: 131072, sign: false }); | ||
data.append(FP16x16 { mag: 131072, sign: false }); | ||
data.append(FP16x16 { mag: 131072, sign: false }); | ||
data.append(FP16x16 { mag: 196608, sign: false }); | ||
data.append(FP16x16 { mag: 196608, sign: false }); | ||
data.append(FP16x16 { mag: 65536, sign: true }); | ||
data.append(FP16x16 { mag: 131072, sign: true }); | ||
data.append(FP16x16 { mag: 131072, sign: true }); | ||
data.append(FP16x16 { mag: 131072, sign: true }); | ||
data.append(FP16x16 { mag: 196608, sign: true }); | ||
data.append(FP16x16 { mag: 196608, sign: true }); | ||
TensorTrait::new(shape.span(), data.span()) | ||
} |
Oops, something went wrong.