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

Implement SequenceTrait #467

Merged
merged 10 commits into from
Jan 6, 2024
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
8 changes: 8 additions & 0 deletions docgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ fn main() {
doc_trait(trait_path, doc_path, label);
doc_functions(trait_path, doc_path, trait_name, label);

// SEQUENCE DOC
let trait_path = "src/operators/sequence/core.cairo";
let doc_path = "docs/framework/operators/sequence";
let label = "sequence";
let trait_name = "SequenceTrait";
doc_trait(trait_path, doc_path, label);
doc_functions(trait_path, doc_path, trait_name, label);

// FIXED POINT DOC
let trait_path = "src/numbers/fixed_point/core.cairo";
let doc_path = "docs/framework/numbers/fixed-point";
Expand Down
17 changes: 10 additions & 7 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,10 @@
* [tensor.reduce\_prod](framework/operators/tensor/tensor.reduce\_prod.md)
* [tensor.gather\_elements](framework/operators/tensor/tensor.gather\_elements.md)
* [tensor.gather\_nd](framework/operators/tensor/tensor.gather\_nd.md)
* [tensor.sequence\_length](framework/operators/tensor/tensor.sequence\_length.md)
* [tensor.sequence\_at](framework/operators/tensor/tensor.sequence\_at.md)
* [tensor.reduce\_min](framework/operators/tensor/tensor.reduce\_min.md)
* [tensor.sequence\_construct](framework/operators/tensor/tensor.sequence\_construct.md)
* [tensor.shrink](framework/operators/tensor/tensor.shrink.md)
* [tensor.sequence\_empty](framework/operators/tensor/tensor.sequence\_empty.md)
* [tensor.reduce\_mean](framework/operators/tensor/tensor.reduce\_mean.md)
* [tensor.pow](framework/operators/tensor/tensor.pow.md)
* [tensor.sequence\_erase](framework/operators/tensor/tensor.sequence\_erase.md)
* [tensor.sequence\_insert](framework/operators/tensor/tensor.sequence\_insert.md)
* [tensor.concat\_from\_sequence](framework/operators/tensor/tensor.concat\_from\_sequence.md)
* [tensor.is\_nan](framework/operators/tensor/tensor.is\_nan.md)
* [tensor.is\_inf](framework/operators/tensor/tensor.is\_inf.md)
* [tensor.not](framework/operators/tensor/tensor.not.md)
Expand All @@ -142,6 +135,16 @@
* [Machine Learning](framework/operators/machine-learning/README.md)
* [Tree Ensemble Classifier](framework/operators/machine-learning/tree-ensemble-classifier/README.md)
* [tree\_ensemble\_classifier.predict](framework/operators/machine-learning/tree-ensemble-classifier/tree\_ensemble\_classifier.predict.md)
* [Sequence](framework/operators/sequence/README.md)
* [sequence.sequence\_construct](framework/operators/sequence/sequence.sequence\_construct.md)
* [sequence.sequence\_empty](framework/operators/sequence/sequence.sequence\_empty.md)
* [tensor.sequence\_length](framework/operators/sequence/sequence.sequence\_length.md)
* [tensor.sequence\_at](framework/operators/sequence/sequence.sequence\_at.md)
* [tensor.sequence\_empty](framework/operators/sequence/sequence.sequence\_empty.md)
* [tensor.sequence\_erase](framework/operators/sequence/sequence.sequence\_erase.md)
* [tensor.sequence\_insert](framework/operators/sequence/sequence.sequence\_insert.md)
* [tensor.concat\_from\_sequence](framework/operators/sequence/sequence.concat\_from\_sequence.md)


## 🏛 Hub

Expand Down
20 changes: 10 additions & 10 deletions docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ You can see below the list of current supported ONNX Operators:
| [ReduceL1](operators/tensor/tensor.reduce\_l1.md) | :white\_check\_mark: |
| [ReduceL2](operators/tensor/tensor.reduce\_l2.md) | :white\_check\_mark: |
| [GatherElements](operators/tensor/tensor.gather/_elements.md) | :white\_check\_mark: |
| [SequenceLength](operators/tensor/tensor.sequence\_length.md) | :white\_check\_mark: |
| [SequenceAt](operators/tensor/tensor.sequence\_at.md) | :white\_check\_mark: |
| [SequenceConstruct](operators/tensor/tensor.sequence\_construct.md) | :white\_check\_mark: |
| [SequenceLength](operators/sequence/sequence.sequence\_length.md) | :white\_check\_mark: |
| [SequenceAt](operators/sequence/sequence.sequence\_at.md) | :white\_check\_mark: |
| [SequenceConstruct](operators/sequence/sequence.sequence\_construct.md) | :white\_check\_mark: |
| [Shrink](operators/tensor/tensor.shrink.md) | :white\_check\_mark: |
| [SequenceEmpty](operators/tensor/tensor.sequence\_empty.md) | :white\_check\_mark: |
| [SequenceEmpty](operators/sequence/sequence.sequence\_empty.md) | :white\_check\_mark: |
| [ReduceL2](operators/tensor/tensor.reduce\_l2.md) | :white\_check\_mark: |
| [SequenceErase](operators/tensor/tensor.sequence\_erase.md) | :white\_check\_mark: |
| [SequenceInsert](operators/tensor/tensor.sequence\_insert.md) | :white\_check\_mark: |
| [ConcatFromSequence](operators/tensor/tensor.concat\_from\_sequence.md) | :white\_check\_mark: |
| [SequenceErase](operators/sequence/sequence.sequence\_erase.md) | :white\_check\_mark: |
| [SequenceInsert](operators/sequence/sequence.sequence\_insert.md) | :white\_check\_mark: |
| [ConcatFromSequence](operators/sequence/sequence.concat\_from\_sequence.md) | :white\_check\_mark: |
| [IsNaN](operators/tensor/tensor.is\_nan.md) | :white\_check\_mark: |
| [IsInf](operators/tensor/tensor.is\_inf.md) | :white\_check\_mark: |
| [Not](operators/tensor/tensor.not.md) | :white\_check\_mark: |
| [GatherND](operators/tensor/tensor.gather/_nd.md) | :white\_check\_mark: |
| [ReduceLogSum](operators/tensor/tensor.reduce\_log\_sum.md) | :white\_check\_mark: |
| [Erf](operators/tensor/tensor.erf.md) | :white\_check\_mark: |
| [GatherND](operators/tensor/tensor.gather/_nd.md) | :white\_check\_mark: |
| [ReduceLogSum](operators/tensor/tensor.reduce\_log\_sum.md) | :white\_check\_mark: |
| [Erf](operators/tensor/tensor.erf.md) | :white\_check\_mark: |

Current Operators support: **96/156 (62%)**
32 changes: 32 additions & 0 deletions docs/framework/operators/sequence/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Sequence

A Sequence represents an array of tensors.

```rust
use orion::operators::sequence;
```

### Data types

Orion supports currently these `Sequence` types.

| Data type | dtype |
| ------------------------- | -------------------------------------------------------- |
| 32-bit integer (signed) | `Array<Tensor<i32>>` |
| 8-bit integer (signed) | `Array<Tensor<i8>>` |
| 32-bit integer (unsigned) | `Array<Tensor<u32>>` |
| Fixed point (signed) | `Array<Tensor<FP8x23 \| FP16x16 \| FP32x32 \| FP64x64>>` |

### Sequence**Trait**

`SequenceTrait` defines the operations that can be performed on a Sequence of tensors.

| function | description |
| --- | --- |
| [`sequence.sequence_construct`](sequence.sequence\_construct.md) | Constructs a tensor sequence containing the input tensors. |
| [`sequence.sequence_empty`](sequence.sequence\_empty.md) | Returns an empty tensor sequence. |
| [`sequence.sequence_length`](sequence.sequence\_length.md) | Returns the length of the input sequence. |
| [`sequence.sequence_insert`](sequence.sequence\_insert.md) | Insert a tensor into a sequence. |
| [`sequence.sequence_at`](sequence.sequence\_at.md) | Outputs the tensor at the specified position in the input sequence. |
| [`sequence.concat_from_sequence`](sequence.concat\_from\_sequence.md) | Concatenate a sequence of tensors into a single tensor. |

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## tensor.sequence_construct
## sequence.sequence_construct

```rust
fn sequence_construct(tensors: Array<Tensor<T>>) -> Array<Tensor<T>>;
Expand All @@ -21,14 +21,15 @@ A tensor sequence `Array<Tensor<T>>` containing the input tensors.
## Examples

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

use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor};
use orion::operators::sequence::SequenceTrait;

fn sequence_construct_example() -> Array<Tensor<usize>> {
let tensor1 = TensorTrait::new(shape: array![2, 2].span(), data: array![0, 1, 2, 3].span());
let tensor2 = TensorTrait::new(shape: array![2, 2].span(), data: array![4, 5, 6, 7].span());
let result = TensorTrait::sequence_construct(tensors: array![tensor1, tensor2]);
let result = SequenceTrait::sequence_construct(tensors: array![tensor1, tensor2]);
return result;
}
>>> [[0, 1, 2, 3], [4, 5, 6, 7]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tensor.sequence_empty
## sequence.sequence_empty

```rust
fn sequence_empty() -> Array<Tensor<T>>;
Expand All @@ -17,16 +17,17 @@ An empty `Array<Tensor<T>>` instance.
Let's create a new empty sequence.

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

use orion::operators::tensor::{
TensorTrait, // we import the trait
Tensor, // we import the type
U32Tensor // we import the implementation.
};
use orion::operators::sequence::SequenceTrait;

fn sequence_empty_example() -> Array<Tensor<u32>> {
let sequence = TensorTrait::sequence_empty();
let sequence = SequenceTrait::sequence_empty();

return sequence;
}
Expand Down
8 changes: 0 additions & 8 deletions docs/framework/operators/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,11 @@ use orion::operators::tensor::TensorTrait;
| [`tensor.reduce_l2`](tensor.reduce\_l2.md) | Computes the L2 norm of the input tensor's elements along the provided axes. |
| [`tensor.gather_elements`](tensor.gather\_elements.md) | GatherElements is an indexing operation that produces its output by indexing into the input data tensor at index positions determined by elements of the indices tensor. |
| [`tensor.reduce_min`](tensor.reduce\_min.md) | Computes the min of the input tensor's elements along the provided axes. |
| [`tensor.sequence_empty`](tensor.sequence\_empty.md) | Returns an empty tensor sequence. |
| [`tensor.sequence_length`](tensor.sequence\_length.md) | Returns the length of the input sequence. |
| [`tensor.sequence_insert`](tensor.sequence\_insert.md) | Insert a tensor into a sequence. |
| [`tensor.sequence_at`](tensor.sequence\_at.md) | Outputs the tensor at the specified position in the input sequence. |
| [`tensor.sequence_construct`](tensor.sequence\_construct.md) | Constructs a tensor sequence containing the input tensors. |
| [`tensor.shrink`](tensor.shrink.md) | Shrinks the input tensor element-wise to the output tensor. |
| [`tensor.reduce_mean`](tensor.reduce\_mean.md) | Computes the mean of the input tensor's elements along the provided axes. |
| [`tensor.pow`](tensor.pow.md) | Pow takes input data (Tensor) and exponent Tensor, and produces one output data (Tensor) where the function f(x) = x^exponent, is applied to the data tensor elementwise. |
| [`tensor.sequence_empty`](tensor.sequence\_empty.md) | Returns an empty tensor sequence. |
| [`tensor.binarizer`](tensor.binarizer.md) | Maps the values of a tensor element-wise to 0 or 1 based on the comparison against a threshold value. |
| [`tensor.array_feature_extractor`](tensor.array\_feature\_extractor.md) | Selects elements of the input tensor based on the indices passed applied to the last tensor axis. |
| [`tensor.reduce_min`](tensor.reduce\_min.md) | Computes the min of the input tensor's elements along the provided axes. |
| [`tensor.concat_from_sequence`](tensor.concat\_from\_sequence.md) | Concatenate a sequence of tensors into a single tensor. |
| [`tensor.is_nan`](tensor.is\_nan.md) | Returns which elements of the input are NaN. |
| [`tensor.is_inf`](tensor.is\_inf.md) | Maps infinity to true and other values to false. |
| [`tensor.not`](tensor.not.md) | Computes the logical negation of all elements in the input tensor. |
Expand Down
4 changes: 0 additions & 4 deletions docs/framework/operators/tensor/tensor.erf.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

Computes the mean of the input tensor's elements along the provided axes.

## Args

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

## Returns

A new `Tensor<T>` of the same shape as the input tensor with
Expand Down
8 changes: 0 additions & 8 deletions docs/framework/operators/tensor/tensor.gather_nd.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ Given data tensor of rank r >= 1, indices tensor of rank q >= 1, and batch_dims
## Returns

A new `Tensor<T>` .

## Example

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

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

fn gather_nd_example() -> Tensor<u32> {
let tensor = TensorTrait::<u32>::new(
shape: array![2, 2].span(),
Expand Down
22 changes: 21 additions & 1 deletion nodegen/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self, dtype: Dtype, shape: tuple, data: np.ndarray):
class Trait(Enum):
TENSOR = 'TENSOR'
NN = 'NN'
SEQUENCE = 'SEQUENCE'


def make_test(inputs: list[Tensor | Sequence], output: Tensor | Sequence, func_sig: str, name: str, trait: Trait = Trait.TENSOR):
Expand Down Expand Up @@ -186,7 +187,13 @@ def get_test_refs(dtype: Dtype, trait: Trait) -> list[str]:
if trait == Trait.NN and dtype == Dtype.BOOL:
raise Exception("NN trait does not support bool dtype")

dtype_ref = dtype_to_nn[dtype] if trait == Trait.NN else dtype_to_tensor[dtype]
if trait == Trait.NN:
dtype_ref = dtype_to_nn[dtype]
elif trait == Trait.SEQUENCE:
dtype_ref = dtype_to_sequence[dtype]
else:
dtype_ref = dtype_to_tensor[dtype]

refs = [
*trait_to_ref[trait],
*dtype_ref,
Expand Down Expand Up @@ -217,6 +224,10 @@ def find_all_types(tensors: list[Tensor | Sequence]) -> list[Dtype]:
"orion::numbers::FixedTrait",
"orion::operators::nn::NNTrait",
],
Trait.SEQUENCE: [
"array::{ArrayTrait, SpanTrait}",
"orion::operators::sequence::SequenceTrait",
],
}


Expand All @@ -239,6 +250,15 @@ def find_all_types(tensors: list[Tensor | Sequence]) -> list[Dtype]:
}


dtype_to_sequence = {
Dtype.U32: ["orion::operators::sequence::U32Sequence",],
Dtype.I32: ["orion::operators::sequence::I32Sequence",],
Dtype.I8: ["orion::operators::sequence::I8Sequence",],
Dtype.FP8x23: ["orion::operators::sequence::FP8x23Sequence",],
Dtype.FP16x16: ["orion::operators::sequence::FP16x16Sequence",],
}


dtype_to_partial_eq = {
Dtype.U32: ["orion::operators::tensor::U32TensorPartialEq",],
Dtype.I32: ["orion::operators::tensor::I32TensorPartialEq",],
Expand Down
Loading