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

Feat: SequenceLength #456

Merged
merged 5 commits into from
Nov 22, 2023
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
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
* [tensor.reduce\_sum\_square](framework/operators/tensor/tensor.reduce\_sum\_square.md)
* [tensor.reduce\_l2](framework/operators/tensor/tensor.reduce\_l2.md)
* [tensor.reduce\_l1](framework/operators/tensor/tensor.reduce\_l1.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)
Expand Down
1 change: 1 addition & 0 deletions docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ You can see below the list of current supported ONNX Operators:
| [ConstantOfShape](operators/tensor/tensor.constant_of_shape.md) | :white\_check\_mark: |
| [ReduceL1](operators/tensor/tensor.reduce\_l1.md) | :white\_check\_mark: |
| [ReduceL2](operators/tensor/tensor.reduce\_l2.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: |
| [Shrink](operators/tensor/tensor.shrink.md) | :white\_check\_mark: |
Expand Down
3 changes: 3 additions & 0 deletions docs/framework/operators/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ use orion::operators::tensor::TensorTrait;
| [`tensor.scatter`](tensor.scatter.md) | Produces a copy of input data, and updates value to values specified by updates at specific index positions specified by indices. |
| [`tensor.reduce_sum_square`](tensor.reduce\_sum\_square.md) | Computes the sum square of the input tensor's elements along the provided axes. |
| [`tensor.reduce_l2`](tensor.reduce\_l2.md) | Computes the L2 norm of the input tensor's elements along the provided axes. |
| [`tensor.sequence_length`](tensor.sequence\_length.md) | Returns the length of the input sequence. |
| [`tensor.shrink`](tensor.shrink.md) | Shrinks the input tensor element-wise to the output tensor with the same datatype and shape based on a defined formula. |
| [`tensor.sequence_empty`](tensor.sequence\_empty.md) | Returns an empty tensor 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. |
Expand Down
36 changes: 36 additions & 0 deletions docs/framework/operators/tensor/tensor.sequence_length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# tensor.sequence_length

```rust
fn sequence_length(self: Array<Tensor<T>>) -> Tensor<u32>;
```

Returns the length of the input sequence.

## Args

* `self`(`Array<Tensor<T>>`) - The input sequence.

## Returns

The length of the sequence as scalar, i.e. a tensor of shape [].

## Examples

Let's create new u32 Tensor with constant 42.

```rust
let mut sequence = ArrayTrait::new();

let mut shape = ArrayTrait::<usize>::new();
shape.append(1);
shape.append(2);

let mut data = ArrayTrait::new();
data.append(3);
data.append(1);

sequence.append(TensorTrait::new(shape.span(), data.span()));

sequence.sequence_length()
>>> [1]
```
173 changes: 173 additions & 0 deletions nodegen/node/sequence_length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import numpy as np
from nodegen.node import RunAll
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl


scalar = lambda x: Tensor(Dtype.U32, (), np.array([x]).astype(np.uint32).flatten())


class Sequence_length(RunAll):
@staticmethod
def sequence_length_u32():
def default():
sequence = []
tensor_cnt = np.random.randint(1, 10)
shape = np.random.randint(1, 4, 2)

for _ in range(tensor_cnt):
values = np.random.randint(0, 6, shape).astype(np.uint32)
tensor = Tensor(Dtype.U32, values.shape, values.flatten())

sequence.append(tensor)

name = "sequence_length_u32"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

def broadcast():
sequence = []
tensor_cnt = np.random.randint(1, 10)

for _ in range(tensor_cnt):
shape = np.random.randint(1, 4, 2)
values = np.random.randint(0, 6, shape).astype(np.uint32)
tensor = Tensor(Dtype.U32, values.shape, values.flatten())

sequence.append(tensor)

name = "sequence_length_u32_broadcast"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

default()
broadcast()

@staticmethod
def sequence_length_i32():
def default():
sequence = []
tensor_cnt = np.random.randint(1, 10)
shape = np.random.randint(1, 4, 2)

for _ in range(tensor_cnt):
values = np.random.randint(-6, 6, shape).astype(np.int32)
tensor = Tensor(Dtype.I32, values.shape, values.flatten())

sequence.append(tensor)

name = "sequence_length_i32"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

def broadcast():
sequence = []
tensor_cnt = np.random.randint(1, 10)

for _ in range(tensor_cnt):
shape = np.random.randint(1, 4, 2)
values = np.random.randint(-6, 6, shape).astype(np.int32)
tensor = Tensor(Dtype.I32, values.shape, values.flatten())

sequence.append(tensor)

name = "sequence_length_i32_broadcast"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

default()
broadcast()

@staticmethod
def sequence_length_i8():
def default():
sequence = []
tensor_cnt = np.random.randint(1, 10)
shape = np.random.randint(1, 4, 2)

for _ in range(tensor_cnt):
values = np.random.randint(-6, 6, shape).astype(np.int8)
tensor = Tensor(Dtype.I8, values.shape, values.flatten())

sequence.append(tensor)

name = "sequence_length_i8"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

def broadcast():
sequence = []
tensor_cnt = np.random.randint(1, 10)

for _ in range(tensor_cnt):
shape = np.random.randint(1, 4, 2)
values = np.random.randint(-6, 6, shape).astype(np.int8)
tensor = Tensor(Dtype.I8, values.shape, values.flatten())

sequence.append(tensor)

name = "sequence_length_i8_broadcast"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

default()
broadcast()

@staticmethod
def sequence_length_fp8x23():
def default():
sequence = []
tensor_cnt = np.random.randint(1, 10)
shape = np.random.randint(1, 4, 2)

for _ in range(tensor_cnt):
values = np.random.randint(-6, 6, shape).astype(np.float64)
tensor = Tensor(Dtype.FP8x23, values.shape, to_fp(values.flatten(), FixedImpl.FP8x23))

sequence.append(tensor)

name = "sequence_length_fp8x23"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

def broadcast():
sequence = []
tensor_cnt = np.random.randint(1, 10)

for _ in range(tensor_cnt):
shape = np.random.randint(1, 4, 2)
values = np.random.randint(-6, 6, shape).astype(np.float64)
tensor = Tensor(Dtype.FP8x23, values.shape, to_fp(values.flatten(), FixedImpl.FP8x23))

sequence.append(tensor)

name = "sequence_length_fp8x23_broadcast"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

default()
broadcast()

@staticmethod
def sequence_length_fp16x16():
def default():
sequence = []
tensor_cnt = np.random.randint(1, 10)
shape = np.random.randint(1, 4, 2)

for _ in range(tensor_cnt):
values = np.random.randint(-6, 6, shape).astype(np.float64)
tensor = Tensor(Dtype.FP16x16, values.shape, to_fp(values.flatten(), FixedImpl.FP16x16))

sequence.append(tensor)

name = "sequence_length_fp16x16"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

def broadcast():
sequence = []
tensor_cnt = np.random.randint(1, 10)

for _ in range(tensor_cnt):
shape = np.random.randint(1, 4, 2)
values = np.random.randint(-6, 6, shape).astype(np.float64)
tensor = Tensor(Dtype.FP16x16, values.shape, to_fp(values.flatten(), FixedImpl.FP16x16))

sequence.append(tensor)

name = "sequence_length_fp16x16_broadcast"
make_test([sequence], scalar(len(sequence)), "input_0.sequence_length()", name)

default()
broadcast()
39 changes: 39 additions & 0 deletions src/operators/tensor/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl TensorSerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Tensor<
/// scatter - Produces a copy of input data, and updates value to values specified by updates at specific index positions specified by indices.
/// reduce_sum_square - Computes the sum square of the input tensor's elements along the provided axes.
/// reduce_l2 - Computes the L2 norm of the input tensor's elements along the provided axes.
/// sequence_length - Returns the length of the input sequence.
/// sequence_insert - Insert a tensor into a sequence.
/// sequence_at - Outputs the tensor at the specified position in the input sequence.
/// sequence_construct - Constructs a tensor sequence containing the input tensors.
Expand Down Expand Up @@ -4377,6 +4378,44 @@ trait TensorTrait<T> {
/// ```
///
fn pow(self: @Tensor<T>, other: @Tensor<T>) -> Tensor<T>;
/// # tensor.sequence_length
///
/// ```rust
/// fn sequence_length(self: Array<Tensor<T>>) -> Tensor<u32>;
/// ```
///
/// Returns the length of the input sequence.
///
/// ## Args
///
/// * `self`(`Array<Tensor<T>>`) - The input sequence.
///
/// ## Returns
///
/// The length of the sequence as scalar, i.e. a tensor of shape [].
///
/// ## Examples
///
/// Let's create new u32 Tensor with constant 42.
///
/// ```rust
/// let mut sequence = ArrayTrait::new();
///
/// let mut shape = ArrayTrait::<usize>::new();
/// shape.append(1);
/// shape.append(2);
///
/// let mut data = ArrayTrait::new();
/// data.append(3);
/// data.append(1);
///
/// sequence.append(TensorTrait::new(shape.span(), data.span()));
///
/// sequence.sequence_length()
/// >>> [1]
/// ```
///
fn sequence_length(self: Array<Tensor<T>>) -> Tensor<u32>;
raphaelDkhn marked this conversation as resolved.
Show resolved Hide resolved
}

/// Cf: TensorTrait::new docstring
Expand Down
4 changes: 4 additions & 0 deletions src/operators/tensor/implementations/tensor_bool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ impl BoolTensor of TensorTrait<bool> {
constant_of_shape(shape, value)
}

fn sequence_length(self: Array<Tensor<bool>>) -> Tensor<u32> {
math::sequence_length::sequence_length(self)
}

fn sequence_at(sequence: Array<Tensor<bool>>, position: Tensor<i32>) -> Tensor<bool> {
math::sequence_at::sequence_at(sequence, position)
}
Expand Down
14 changes: 9 additions & 5 deletions src/operators/tensor/implementations/tensor_fp16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ impl FP16x16Tensor of TensorTrait<FP16x16> {
math::scatter::scatter(self, updates, indices, axis, reduction)
}

fn sequence_length(self: Array<Tensor<FP16x16>>) -> Tensor<u32> {
math::sequence_length::sequence_length(self)
}

fn shrink(self: Tensor<FP16x16>, bias: Option<FP16x16>, lambd: Option<FP16x16>) -> Tensor<FP16x16> {
math::shrink::shrink(self, bias, lambd)
}

fn sequence_at(sequence: Array<Tensor<FP16x16>>, position: Tensor<i32>) -> Tensor<FP16x16> {
math::sequence_at::sequence_at(sequence, position)
}
Expand All @@ -401,11 +409,7 @@ impl FP16x16Tensor of TensorTrait<FP16x16> {
math::sequence_construct::sequence_construct(tensors)
}

fn shrink(
self: Tensor<FP16x16>, bias: Option<FP16x16>, lambd: Option<FP16x16>
) -> Tensor<FP16x16> {
math::shrink::shrink(self, bias, lambd)
}


fn sequence_empty() -> Array<Tensor<FP16x16>> {
math::sequence_empty::sequence_empty::<FP16x16>()
Expand Down
14 changes: 8 additions & 6 deletions src/operators/tensor/implementations/tensor_fp16x16wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ impl FP16x16WTensor of TensorTrait<FP16x16W> {
math::reduce_l2::reduce_l2(self, axis, keepdims)
}

fn sequence_length(self: Array<Tensor<FP16x16W>>) -> Tensor<u32> {
math::sequence_length::sequence_length(self)
}

fn shrink(self: Tensor<FP16x16W>, bias: Option<FP16x16W>, lambd: Option<FP16x16W>) -> Tensor<FP16x16W> {
math::shrink::shrink(self, bias, lambd)
}

fn sequence_at(sequence: Array<Tensor<FP16x16W>>, position: Tensor<i32>) -> Tensor<FP16x16W> {
math::sequence_at::sequence_at(sequence, position)
}
Expand All @@ -371,12 +379,6 @@ impl FP16x16WTensor of TensorTrait<FP16x16W> {
math::sequence_construct::sequence_construct(tensors)
}

fn shrink(
self: Tensor<FP16x16W>, bias: Option<FP16x16W>, lambd: Option<FP16x16W>
) -> Tensor<FP16x16W> {
math::shrink::shrink(self, bias, lambd)
}

fn sequence_empty() -> Array<Tensor<FP16x16W>> {
math::sequence_empty::sequence_empty::<FP16x16W>()
}
Expand Down
14 changes: 8 additions & 6 deletions src/operators/tensor/implementations/tensor_fp32x32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@ impl FP32x32Tensor of TensorTrait<FP32x32> {
math::reduce_l2::reduce_l2(self, axis, keepdims)
}

fn sequence_length(self: Array<Tensor<FP32x32>>) -> Tensor<u32> {
math::sequence_length::sequence_length(self)
}

fn shrink(self: Tensor<FP32x32>, bias: Option<FP32x32>, lambd: Option<FP32x32>) -> Tensor<FP32x32> {
math::shrink::shrink(self, bias, lambd)
}

fn sequence_at(sequence: Array<Tensor<FP32x32>>, position: Tensor<i32>) -> Tensor<FP32x32> {
math::sequence_at::sequence_at(sequence, position)
}
Expand All @@ -402,12 +410,6 @@ impl FP32x32Tensor of TensorTrait<FP32x32> {
math::sequence_construct::sequence_construct(tensors)
}

fn shrink(
self: Tensor<FP32x32>, bias: Option<FP32x32>, lambd: Option<FP32x32>
) -> Tensor<FP32x32> {
math::shrink::shrink(self, bias, lambd)
}

fn sequence_empty() -> Array<Tensor<FP32x32>> {
math::sequence_empty::sequence_empty::<FP32x32>()
}
Expand Down
Loading