forked from gizatechxyz/orion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
381 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
## tensor.expand | ||
|
||
```rust | ||
fn expand(self: @Tensor<T>, Tensor: Span<usize>,) -> Tensor<T>; | ||
``` | ||
|
||
Broadcast the input tensor following the given shape and the broadcast rule. The broadcast rule is similar to numpy.array(input) * numpy.ones(shape): Dimensions are right alignment; Two corresponding dimensions must have the same value, or one of them is equal to 1. | ||
|
||
## Args | ||
|
||
* `self`(`@Tensor<T>`) - The input tensor. | ||
* `shape`(`Tensor<usize>`) - A 1-D tensor indicates the shape you want to expand to, following the broadcast rule | ||
|
||
## Panics | ||
|
||
* If the shape doesn't follow the broadcast rule. | ||
|
||
## Returns | ||
|
||
A new `Tensor<T>` result of the expansion. | ||
|
||
## Examples | ||
|
||
```rust | ||
use orion::operators::tensor::{FP16x16Tensor}; | ||
use orion::operators::tensor::{TensorTrait, Tensor}; | ||
use core::array::{ArrayTrait, SpanTrait}; | ||
use orion::operators::tensor::{U32Tensor}; | ||
use orion::numbers::FP16x16; | ||
|
||
fn test_expand() -> Tensor<FP16x16> { | ||
let mut shape = ArrayTrait::<usize>::new(); | ||
shape.append(1); | ||
shape.append(3); | ||
shape.append(1); | ||
|
||
let mut data = ArrayTrait::new(); | ||
data.append(FP16x16 { mag: 65536, sign: false }); | ||
data.append(FP16x16 { mag: 131072, sign: false }); | ||
data.append(FP16x16 { mag: 196608, sign: false }); | ||
let mut X = TensorTrait::new(shape.span(), data.span()); | ||
|
||
return X.expand(TensorTrait::new(array![3].span(),array![2, 1, 6].span())); | ||
|
||
} | ||
|
||
>>> [[[1. 1. 1. 1. 1. 1.] | ||
[2. 2. 2. 2. 2. 2.] | ||
[3. 3. 3. 3. 3. 3.]] | ||
|
||
[[1. 1. 1. 1. 1. 1.] | ||
[2. 2. 2. 2. 2. 2.] | ||
[3. 3. 3. 3. 3. 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,32 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl, Trait | ||
|
||
class Expand(RunAll): | ||
@staticmethod | ||
def expand_with_broadcast() -> None: | ||
shape = [1, 3, 1] | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape) | ||
|
||
new_shape = [2, 1, 6] | ||
y = x * np.ones(new_shape, dtype=np.float32) | ||
|
||
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 = "expand_with_broadcast" | ||
make_test([x], y, "input_0.expand(TensorTrait::new(array![3].span(),array![2, 1, 6].span()))", name) | ||
|
||
@staticmethod | ||
def expand_without_broadcast() -> None: | ||
shape = [3, 1] | ||
new_shape = [3, 4] | ||
|
||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape) | ||
y = x * np.ones(new_shape, dtype=np.float32) | ||
|
||
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 = "expand_without_broadcast" | ||
make_test([x], y, "input_0.expand(TensorTrait::new(array![2].span(),array![3, 4].span()))", 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ mod split; | |
mod split_to_sequence; | ||
mod reverse_sequence; | ||
mod optional; | ||
mod expand; |
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,36 @@ | ||
use core::array::ArrayTrait; | ||
use orion::numbers::NumberTrait; | ||
use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; | ||
use orion::operators::tensor::helpers::check_compatibility; | ||
//use orion::operators::nn::helpers::prod; | ||
|
||
/// Cf: TensorTrait::expand docstring | ||
fn expand<T, MAG, +TensorTrait<T>, +NumberTrait<T, MAG>, +Copy<T>, +Drop<T>, +Mul<Tensor<T>>,>( | ||
X: @Tensor<T>, shape: Tensor<usize>, | ||
) -> Tensor<T> { | ||
check_compatibility((*X).shape, shape.data); | ||
|
||
let mut ones = ArrayTrait::new(); | ||
let dim = prod(shape.data); | ||
|
||
let mut i = 0; | ||
while i != dim { | ||
ones.append(NumberTrait::one()); | ||
i += 1; | ||
}; | ||
|
||
return *X * TensorTrait::new(shape.data, ones.span()); | ||
} | ||
|
||
///from orion::operators::nn::helpers::prod; -> delete when nn refactor merged | ||
fn prod<T, MAG, +Drop<T>, +Copy<T>, +NumberTrait<T, MAG>, +TensorTrait<T>, +MulEq<T>,>( | ||
mut a: Span<T> | ||
) -> T { | ||
let mut prod = NumberTrait::one(); | ||
loop { | ||
match a.pop_front() { | ||
Option::Some(v) => { prod *= *v; }, | ||
Option::None => { break prod; } | ||
}; | ||
} | ||
} |
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,22 @@ | ||
mod input_0; | ||
mod output_0; | ||
|
||
|
||
use orion::utils::{assert_eq, assert_seq_eq}; | ||
use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; | ||
use orion::operators::tensor::FP16x16TensorPartialEq; | ||
use orion::operators::tensor::{TensorTrait, Tensor}; | ||
use core::array::{ArrayTrait, SpanTrait}; | ||
use orion::operators::tensor::{U32Tensor}; | ||
|
||
#[test] | ||
#[available_gas(2000000000)] | ||
fn test_expand_with_broadcast() { | ||
let input_0 = input_0::input_0(); | ||
let z_0 = output_0::output_0(); | ||
|
||
let y_0 = input_0.expand(TensorTrait::new(array![3].span(), array![2, 1, 6].span())); | ||
|
||
assert_eq(y_0, z_0); | ||
} | ||
|
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,17 @@ | ||
use core::array::{ArrayTrait, SpanTrait}; | ||
use orion::operators::tensor::{TensorTrait, Tensor}; | ||
use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; | ||
use orion::numbers::{FixedTrait, FP16x16}; | ||
|
||
fn input_0() -> Tensor<FP16x16> { | ||
let mut shape = ArrayTrait::<usize>::new(); | ||
shape.append(1); | ||
shape.append(3); | ||
shape.append(1); | ||
|
||
let mut data = ArrayTrait::new(); | ||
data.append(FP16x16 { mag: 65536, sign: false }); | ||
data.append(FP16x16 { mag: 131072, sign: false }); | ||
data.append(FP16x16 { mag: 196608, sign: false }); | ||
TensorTrait::new(shape.span(), data.span()) | ||
} |
Oops, something went wrong.