-
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 #598 from canacechan/EyeLike
Feat:EyeLike
- Loading branch information
Showing
35 changed files
with
700 additions
and
2 deletions.
There are no files selected for viewing
Binary file added
BIN
+83.4 KB
docs/.gitbook/assets/Capture d#U2019#U00e9cran 2023-06-08 #U00e0 17.59.46.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+83.4 KB
docs/.gitbook/assets/Capture d#U2019e#U0301cran 2023-06-08 a#U0300 17.59.46.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,44 @@ | ||
# TensorTrait::eye_like | ||
|
||
```rust | ||
fn eye_like(self: @Tensor<T>, k: Option<i32>) -> Tensor<T>; | ||
``` | ||
|
||
Generate a 2D tensor (matrix) with ones on the diagonal and zeros everywhere else. Only 2D tensors are supported, i.e. input T1 must be of rank 2. The shape of the output tensor is the same as the input tensor. By default, the main diagonal is populated with ones, but attribute 'k' can be used to populate upper or lower diagonals. | ||
|
||
## Args | ||
|
||
* `self`(`@Tensor<T>`) - 2D input tensor to copy shape, and optionally, type information from. | ||
* `k`(Option<i32>) - (Optional) Index of the diagonal to be populated with ones. Default is 0. If T2 is the output, this op sets T2[i, i+k] = 1. k = 0 populates the main diagonal, k > 0 populates an upper diagonal, and k < 0 populates a lower diagonal. | ||
|
||
## Returns | ||
|
||
* Output tensor, same shape as input tensor. | ||
|
||
## Examples | ||
|
||
```rust | ||
use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; | ||
use core::array::{ArrayTrait, SpanTrait}; | ||
use orion::operators::tensor::{TensorTrait, Tensor}; | ||
use orion::utils::{assert_eq, assert_seq_eq}; | ||
use orion::operators::tensor::FP8x23TensorPartialEq; | ||
use orion::numbers::{FixedTrait, FP8x23}; | ||
|
||
|
||
fn example() -> Tensor<FP8x23> { | ||
let mut shape = ArrayTrait::<usize>::new(); | ||
shape.append(2); | ||
shape.append(2); | ||
|
||
let mut data = ArrayTrait::new(); | ||
data.append(FP8x23 { mag: 16777216, sign: true }); | ||
data.append(FP8x23 { mag: 16777216, sign: false }); | ||
data.append(FP8x23 { mag: 16777216, sign: true }); | ||
data.append(FP8x23 { mag: 16777216, sign: false }); | ||
let tensor1 = TensorTrait::new(shape.span(), data.span()); | ||
|
||
return tensor1.eye_like(Option::Some(0)); | ||
} | ||
>>> [1, 0, 0, 1] | ||
``` |
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,89 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl, Trait, get_data_statement | ||
|
||
def eye_like(data, k=None) -> np.ndarray: # type: ignore | ||
if data is None: | ||
_dtype = np.float32 | ||
else: | ||
_dtype = data.dtype | ||
shape = data.shape | ||
if len(shape) == 1: | ||
sh = (shape[0], shape[0]) | ||
elif len(shape) == 2: | ||
sh = shape | ||
else: | ||
raise RuntimeError(f"EyeLike only accept 1D or 2D tensors not {shape!r}.") | ||
return np.eye(*sh, k=k, dtype=_dtype) # type: ignore | ||
|
||
class Eye_like(RunAll): | ||
|
||
@staticmethod | ||
# We test here with fp8x23 implementation. | ||
def fp8x23(): | ||
x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) | ||
k = 0 | ||
y = eye_like(x, k) | ||
|
||
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 = "eye_like_fp8x23" | ||
make_test([x], y, f"input_0.eye_like(Option::Some({k}))", name) | ||
|
||
@staticmethod | ||
# We test here with fp16x16 implementation. | ||
def fp16x16(): | ||
x = np.random.randint(-3, 3, (4, 7)).astype(np.float64) | ||
k = 3 | ||
y = eye_like(x, k) | ||
|
||
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 = "eye_like_fp16x16" | ||
make_test([x], y, f"input_0.eye_like(Option::Some({k}))", name) | ||
|
||
@staticmethod | ||
# We test here with i8 implementation. | ||
def i8(): | ||
x = np.random.randint(0, 6, (5, 3)).astype(np.int8) | ||
k = -2 | ||
y = eye_like(x, k) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
|
||
name = "eye_like_i8" | ||
make_test([x], y, f"input_0.eye_like(Option::Some({k}))", name) | ||
|
||
@staticmethod | ||
# We test here with i32 implementation. | ||
def i32(): | ||
x = np.random.randint(0, 6, (5, 8)).astype(np.int32) | ||
k = 9 | ||
y = eye_like(x, k) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
|
||
name = "eye_like_i32" | ||
make_test([x], y, f"input_0.eye_like(Option::Some({k}))", name) | ||
|
||
@staticmethod | ||
# We test here with u32 implementation. | ||
def u32(): | ||
x = np.random.randint(0, 6, (1, 2)).astype(np.uint32) | ||
k = -7 | ||
y = eye_like(x, k) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
|
||
name = "eye_like_u32" | ||
make_test([x], y, f"input_0.eye_like(Option::Some({k}))", 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
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 |
---|---|---|
|
@@ -68,3 +68,4 @@ mod hann_window; | |
mod hamming_window; | ||
mod blackman_window; | ||
mod scatter_nd; | ||
mod eye_like; |
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,61 @@ | ||
use orion::numbers::fixed_point::core::FixedTrait; | ||
use orion::numbers::NumberTrait; | ||
use orion::operators::tensor::core::{Tensor, TensorTrait}; | ||
|
||
fn eye_like< | ||
T, | ||
MAG, | ||
impl TTensor: TensorTrait<T>, | ||
impl TNumber: NumberTrait<T, MAG>, | ||
impl TAdd: Add<T>, | ||
impl TSub: Sub<T>, | ||
impl TMul: Mul<T>, | ||
impl TDiv: Div<T>, | ||
impl TTensorAdd: Add<Tensor<T>>, | ||
impl TPartialOrd: PartialOrd<T>, | ||
impl TAddEq: AddEq<T>, | ||
impl TCopy: Copy<T>, | ||
impl TDrop: Drop<T>, | ||
>( | ||
self: @Tensor<T>, k: Option<i32> | ||
) -> Tensor<T> { | ||
assert((*self.shape).len() == 2 || (*self.shape).len() == 1, 'Unexpected shape.'); | ||
let mut shape = *self.shape; | ||
if (*self.shape).len()==1 { | ||
shape = (array![*(*self.shape).at(0), *(*self.shape).at(0)]).span(); | ||
}; | ||
let M = *shape.at(1); | ||
let K = match k { | ||
Option::Some(val) => val, | ||
Option::None => 0, | ||
}; | ||
let len = *(shape.at(0)) * (*(shape.at(1))); | ||
let mut i: usize = 0; | ||
let mut arr: Array<T> = array![]; | ||
while i != len { | ||
arr.append(NumberTrait::zero()); | ||
i += 1; | ||
}; | ||
if (K >= M.try_into().unwrap()) { | ||
return TensorTrait::<T>::new(shape, arr.span()); | ||
}; | ||
let mut j: usize = 0; | ||
if (K < 0){ | ||
j = (-(K)).try_into().unwrap() * M; | ||
} else { | ||
j = K.try_into().unwrap(); | ||
}; | ||
let end: usize = (M.try_into().unwrap() - K).try_into().unwrap() * M; | ||
i = 0; | ||
arr = array![]; | ||
while i != len { | ||
if (i == j && j < end) { | ||
arr.append(NumberTrait::one()); | ||
j += (M + 1); | ||
} else { | ||
arr.append(NumberTrait::zero()); | ||
}; | ||
i += 1; | ||
}; | ||
return TensorTrait::<T>::new(shape, arr.span()); | ||
} |
Oops, something went wrong.