Skip to content

Commit

Permalink
Merge pull request #560 from HappyTomatoo/feat/Window
Browse files Browse the repository at this point in the history
feat: Implement Window correlation operators:
  • Loading branch information
raphaelDkhn authored Feb 11, 2024
2 parents 66cdb6b + be72b39 commit ebdc188
Show file tree
Hide file tree
Showing 52 changed files with 1,693 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@
* [tensor.optional](framework/operators/tensor/tensor.optional.md)
* [tensor.reverse_sequence](framework/operators/tensor/tensor.reverse_sequence.md)
* [tensor.split_to_sequence](framework/operators/tensor/tensor.split_to_sequence.md)
* [tensor.range](framework/operators/tensor/tensor.range.md)
* [tensor.hann_window](framework/operators/tensor/tensor.hann_window.md)
* [tensor.hamming_window](framework/operators/tensor/tensor.hamming_window.md)
* [tensor.blackman_window](framework/operators/tensor/tensor.blackman_window.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
4 changes: 4 additions & 0 deletions docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@ You can see below the list of current supported ONNX Operators:
| [Optional](operators/tensor/tensor.optional.md) | :white\_check\_mark: |
| [ReverseSequence](operators/tensor/tensor.reverse_sequence.md) | :white\_check\_mark: |
| [SplitToSequence](operators/tensor/tensor.split_to_sequence.md) | :white\_check\_mark: |
| [Range](operators/tensor/tensor.range.md) | :white\_check\_mark: |
| [HannWindow](operators/tensor/tensor.tensor.hann_window.md) | :white\_check\_mark: |
| [HammingWindow](operators/tensor/tensor.tensor.hamming_window.md) | :white\_check\_mark: |
| [BlackmanWindow](operators/tensor/tensor.tensor.blackman_window.md) | :white\_check\_mark: |

Current Operators support: **98/156 (62%)**
5 changes: 5 additions & 0 deletions docs/framework/operators/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ use orion::operators::tensor::TensorTrait;
| [`tensor.erf`](tensor.erf.md) | Computes the error function of the given input tensor element-wise. |
| [`tensor.layer_normalization`](tensor.layer\_normalization.md) | computes the layer normalization of the input tensor. |
| [`tensor.split`](tensor.split.md) | Split a tensor into a list of tensors, along the specified ‘axis’. |
| [`tensor.split_to_sequence`](tensor.split\_to\_sequence.md) | Split a tensor into a sequence of tensors, along the specified ‘axis’. |
| [`tensor.range`](tensor.range.md) | Generate a tensor containing a sequence of numbers that begin at start and extends by increments of delta up to limit (exclusive). |
| [`tensor.hann_window`](tensor.hann\_window.md) | Generates a Hann window as described in the paper https://ieeexplore.ieee.org/document/1455106. |
| [`tensor.hamming_window`](tensor.hamming\_window.md) | Generates a Hamming window as described in the paper https://ieeexplore.ieee.org/document/1455106. |
| [`tensor.blackman_window`](tensor.blackman\_window.md) | Generates a Blackman window as described in the paper https://ieeexplore.ieee.org/document/1455106. |
| [`tensor.reverse_sequence`](tensor.reverse\_sequence.md) | Reverse batch of sequences having different lengths specified by sequence_lens. |
| [`tensor.optional`](tensor.optional.md) | Constructs an optional-type value containing either an empty optional of a certain type specified by the attribute, or a non-empty value containing the input element. |
| [`tensor.dynamic_quantize_linear`](tensor.dynamic\_quantize\_linear.md) | Computes the Scale, Zero Point and FP32->8Bit conversion of FP32 Input data. |
Expand Down
32 changes: 32 additions & 0 deletions docs/framework/operators/tensor/tensor.blackman_window.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# tensor.blackman_window

```rust
fn blackman_window(size: T, periodic: Option<usize>) -> Tensor<T>;
```

Generates a Blackman window as described in the paper https://ieeexplore.ieee.org/document/1455106.


* `size`(`T`) - A scalar value indicating the length of the window.
* `periodic`(Option<usize>) - If 1, returns a window to be used as periodic function. If 0, return a symmetric window. When 'periodic' is specified, hann computes a window of length size + 1 and returns the first size points. The default value is 1.

## Returns

A Blackman window with length: size. The output has the shape: [size].

## Examples

```rust
use core::array::{ArrayTrait, SpanTrait};
use orion::operators::tensor::FP8x23TensorPartialEq;
use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd};
use orion::operators::tensor::{TensorTrait, Tensor};
use orion::utils::{assert_eq, assert_seq_eq};
use orion::numbers::{FixedTrait, FP8x23};


fn blackman_window_example() -> Tensor<FP8x23> {
return TensorTrait::blackman_window(FP8x23 { mag: 33554432, sign: false }, Option::Some(0)); // size: 4
}
>>> [0 0.36 0.36 0]
```
32 changes: 32 additions & 0 deletions docs/framework/operators/tensor/tensor.hamming_window.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# tensor.hamming_window

```rust
fn hamming_window(size: T, periodic: Option<usize>) -> Tensor<T>;
```

Generates a Hamming window as described in the paper https://ieeexplore.ieee.org/document/1455106.


* `size`(`T`) - A scalar value indicating the length of the window.
* `periodic`(Option<usize>) - If 1, returns a window to be used as periodic function. If 0, return a symmetric window. When 'periodic' is specified, hann computes a window of length size + 1 and returns the first size points. The default value is 1.

## Returns

A Hamming window with length: size. The output has the shape: [size].

## Examples

```rust
use core::array::{ArrayTrait, SpanTrait};
use orion::operators::tensor::FP8x23TensorPartialEq;
use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd};
use orion::operators::tensor::{TensorTrait, Tensor};
use orion::utils::{assert_eq, assert_seq_eq};
use orion::numbers::{FixedTrait, FP8x23};


fn hamming_window_example() -> Tensor<FP8x23> {
return TensorTrait::hamming_window(FP8x23 { mag: 33554432, sign: false }, Option::Some(0)); // size: 4
}
>>> [729444 6473817 6473817 729444]
```
32 changes: 32 additions & 0 deletions docs/framework/operators/tensor/tensor.hann_window.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# tensor.hann_window

```rust
fn hann_window(size: T, periodic: Option<usize>) -> Tensor<T>;
```

Generates a Hann window as described in the paper https://ieeexplore.ieee.org/document/1455106.


* `size`(`T`) - A scalar value indicating the length of the window.
* `periodic`(Option<usize>) - If 1, returns a window to be used as periodic function. If 0, return a symmetric window. When 'periodic' is specified, hann computes a window of length size + 1 and returns the first size points. The default value is 1.

## Returns

A Hann window with length: size. The output has the shape: [size].

## Examples

```rust
use core::array::{ArrayTrait, SpanTrait};
use orion::operators::tensor::FP8x23TensorPartialEq;
use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd};
use orion::operators::tensor::{TensorTrait, Tensor};
use orion::utils::{assert_eq, assert_seq_eq};
use orion::numbers::{FixedTrait, FP8x23};


fn hann_window_example() -> Tensor<FP8x23> {
return TensorTrait::hann_window(FP8x23 { mag: 33554432, sign: false }, Option::Some(0)); // size: 4
}
>>> [0 6291455 6291456 0]
```
33 changes: 33 additions & 0 deletions docs/framework/operators/tensor/tensor.range.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# tensor.range

```rust
fn range(start: T, end: T, step: T) -> Tensor<T>;
```

Generate a tensor containing a sequence of numbers that begin at start and extends by increments of delta up to limit (exclusive).


* `start`(`T`) - First entry for the range of output values.
* `end`(`T`) - Exclusive upper limit for the range of output values.
* `step `(`T`) - Value to step by.

## Returns

A 1-D tensor with same type as the inputs containing generated range of values.

## Examples

```rust
use core::array::{ArrayTrait, SpanTrait};
use orion::operators::tensor::I32TensorPartialEq;
use orion::operators::tensor::{TensorTrait, Tensor};
use orion::operators::tensor::{I32Tensor, I32TensorAdd};
use orion::utils::{assert_eq, assert_seq_eq};
use orion::numbers::NumberTrait;


fn range_example() -> Tensor<i32> {
return TensorTrait::range(21,2,-3);
}
>>> [21 18 15 12 9 6 3]
```
13 changes: 9 additions & 4 deletions docs/framework/operators/tensor/tensor.reverse_sequence.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# tensor.reverse_sequence

```rust
fn reverse_sequence(self: @Array<Tensor<T>>, sequence_lens: @Tensor<i32>, batch_axis: Option<usize>, time_axis: Option<usize>) ->
Array<Tensor<T>>;
fn reverse_sequence(self: @Tensor<T>, sequence_lens: @Tensor<i32>, batch_axis: Option<usize>, time_axis: Option<usize>) ->
Tensor<T>;
```

Reverse batch of sequences having different lengths specified by sequence_lens.
Expand Down Expand Up @@ -34,11 +34,16 @@ fn reverse_sequence_example() -> Tensor<u32> {
0, 1, 2, 3, 4, 5, 6, 7,8,9,10,11,12,13,14,15,16
].span(),
);
let sequence_lens = TensorTrait::<usize>::new(array![4,4].span(), array![1,2,3,4].span());
let sequence_lens = TensorTrait::<usize>::new(array![4].span(), array![1,2,3,4].span());
let batch_axis = Option::Some(0);
let time_axis = Option::Some(1);
// We can call `split` function as follows.
return tensor.reverse_sequence(sequence_lens, batch_axis, time_axis);
}
>>> [0,1,2,3,5,4,6,7,10,9,8,11,15,14,13,12]
>>> [
[0,1,2,3],
[5,4,6,7],
[10,9,8,11],
[15,14,13,12]
]
```
130 changes: 130 additions & 0 deletions nodegen/node/blackman_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import numpy as np
from nodegen.node import RunAll
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl, Trait, get_data_statement

def blackman_window(size, output_datatype=None, periodic=None) -> np.ndarray: # type: ignore
if periodic == 1:
N_1 = size
else:
N_1 = size - 1
ni = np.arange(size, dtype=output_datatype)
alpha = 0.42
beta = 0.08
y = np.cos((ni * (np.float64(np.pi).astype(output_datatype) * 2)) / N_1).astype(output_datatype) * (-0.5)
y += np.cos((ni * (np.float64(np.pi).astype(output_datatype) * 4)) / N_1) * beta
y += alpha
return y.astype(output_datatype)

class Blackman_window(RunAll):

@staticmethod
# We test here with fp8x23 implementation.
def fp8x23():
args = [3]
# x = np.float64(4)
args_str = get_data_statement(to_fp(np.array(args).flatten(), FixedImpl.FP8x23), Dtype.FP8x23)
y = blackman_window(*args, np.float64)

# Convert the floats values in `y` to fixed points with `to_fp` method:
y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23))

# Define the name of the generated folder.
name = "blackman_window_fp8x23"
# Invoke `make_test` method to generate corresponding Cairo tests:
make_test(
[], # List of input tensors.
y, # The expected output result.
f"TensorTrait::blackman_window({','.join(args_str)}, Option::Some(0))", # The code signature.
name # The name of the generated folder.
)

@staticmethod
# We test here with fp16x16 implementation.
def fp16x16():
print(get_data_statement(to_fp(np.array([np.pi]).flatten(), FixedImpl.FP16x16), Dtype.FP16x16))
args = [3]
# x = np.float64(4)
args_str = get_data_statement(to_fp(np.array(args).flatten(), FixedImpl.FP16x16), Dtype.FP16x16)
y = blackman_window(*args, np.float16, 1)
# Convert the floats values in `y` to fixed points with `to_fp` method:
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))

# Define the name of the generated folder.
name = "blackman_window_fp16x16"
# Invoke `make_test` method to generate corresponding Cairo tests:
make_test(
[], # List of input tensors.
y, # The expected output result.
f"TensorTrait::blackman_window({','.join(args_str)}, Option::Some(1))", # The code signature.
name # The name of the generated folder.
)

# @staticmethod
# # We test here with i8 implementation.
# def i8():
# print(get_data_statement(np.array([np.pi]).flatten(), Dtype.I8))
# args = [5]
# # x = np.float64(4)
# args_str = get_data_statement(np.array(args).flatten(), Dtype.I8)
# y = blackman_window(*args, np.int8)
# print(y)

# # Convert the floats values in `y` to fixed points with `to_fp` method:
# y = Tensor(Dtype.I8, y.shape, y.flatten())

# # Define the name of the generated folder.
# name = "blackman_window_i8"
# # Invoke `make_test` method to generate corresponding Cairo tests:
# make_test(
# [], # List of input tensors.
# y, # The expected output result.
# f"TensorTrait::blackman_window({','.join(args_str)}, Option::Some(1))", # The code signature.
# name # The name of the generated folder.
# )

# @staticmethod
# # We test here with i32 implementation.
# def i32():
# print(get_data_statement(np.array([np.pi]).flatten(), Dtype.I32))
# args = [4]
# # x = np.float64(4)
# args_str = get_data_statement(np.array(args).flatten(), Dtype.I32)
# y = blackman_window(*args, np.int32)
# print(y)

# # Convert the floats values in `y` to fixed points with `to_fp` method:
# y = Tensor(Dtype.I32, y.shape, y.flatten())

# # Define the name of the generated folder.
# name = "blackman_window_i32"
# # Invoke `make_test` method to generate corresponding Cairo tests:
# make_test(
# [], # List of input tensors.
# y, # The expected output result.
# f"TensorTrait::blackman_window({','.join(args_str)}, Option::Some(0))", # The code signature.
# name # The name of the generated folder.
# )

# @staticmethod
# # We test here with u32 implementation.
# def u32():
# print(get_data_statement(np.array([np.pi]).flatten(), Dtype.U32))
# args = [4]
# # x = np.float64(4)
# args_str = get_data_statement(np.array(args).flatten(), Dtype.U32)
# y = blackman_window(*args, np.uint32)
# print(y)

# # Convert the floats values in `y` to fixed points with `to_fp` method:
# y = Tensor(Dtype.U32, y.shape, y.flatten())

# # Define the name of the generated folder.
# name = "blackman_window_u32"
# # Invoke `make_test` method to generate corresponding Cairo tests:
# make_test(
# [], # List of input tensors.
# y, # The expected output result.
# f"TensorTrait::blackman_window({','.join(args_str)}, Option::Some(0))", # The code signature.
# name # The name of the generated folder.
# )

Loading

0 comments on commit ebdc188

Please sign in to comment.