diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index d7dc544df..7a68d2ecd 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -53,5 +53,6 @@ You can see below the list of current supported ONNX Operators: | [QuantizeLinear](operators/tensor/tensor.quantize\_linear.md) | :white\_check\_mark: | | [DequantizeLinear](operators/tensor/tensor.quantize\_linear.md) | :white\_check\_mark: | | [Nonzero](operators/tensor/tensor.nonzero.md) | :white\_check\_mark: | +| [Squeeze](operators/tensor/tensor.squeeze.md) | :white\_check\_mark: | -Current Operators support: **45/156 (29%)** +Current Operators support: **48/156 (30%)** diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index d9ff7e9b0..0bf6019d3 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -78,6 +78,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.dequantize_linear`](tensor.dequantize\_linear.md) | Dequantizes an i8 Tensor using linear dequantization. | | [`tensor.gather`](tensor.gather.md) | Gather entries of the axis dimension of data. | | [`tensor.nonzero`](tensor.nonzero.md) | Produces indices of the elements that are non-zero (in row-major order - by dimension). | +| [`tensor.squeeze`](tensor.squeeze.md) | Removes dimensions of size 1 from the shape of a tensor. | ## Arithmetic Operations diff --git a/docs/framework/operators/tensor/tensor.squeeze.md b/docs/framework/operators/tensor/tensor.squeeze.md new file mode 100644 index 000000000..28076d678 --- /dev/null +++ b/docs/framework/operators/tensor/tensor.squeeze.md @@ -0,0 +1,35 @@ +# tensor.squeeze + +```rust + fn squeeze(self: @Tensor, axes: Option>) -> Tensor; +``` + +Removes dimensions of size 1 from the shape of a tensor. + +## Args + +* `self`(`@Tensor`) - Tensor of data to calculate non-zero indices. +* `axes`(`Option>`) - List of integers indicating the dimensions to squeeze. + +## Returns + +A new `Tensor` Reshaped tensor with same data as input. + +## Example + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + +fn squeeze_example() -> Tensor { + let tensor = TensorTrait::::new( + shape: array![1, 2, 1, 2, 1].span(), + data: array![1, 1, 1, 1].span(), + ); + + return tensor.squeeze(axes: Option::None(()); +} +>>> [[1 1] + [1 1]] +``` diff --git a/nodegen/node/squeeze.py b/nodegen/node/squeeze.py new file mode 100644 index 000000000..81661833e --- /dev/null +++ b/nodegen/node/squeeze.py @@ -0,0 +1,84 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl + + +class Squeeze(RunAll): + @staticmethod + def squeeze_i8(): + def squeeze(): + x = np.ones((1, 2, 1, 2, 1), dtype=np.int8) + y = np.ones((2, 2, 1), dtype=np.int8) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + + name = "squeeze_i8" + make_node([x], [y], name) + make_test( + [x], y, "input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span()))", name) + squeeze() + + @staticmethod + def squeeze_i32(): + def squeeze(): + x = np.ones((1, 2, 1, 2, 1), dtype=np.int32) + y = np.ones((2, 2, 1), dtype=np.int32) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "squeeze_i32" + make_node([x], [y], name) + make_test( + [x], y, "input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span()))", name) + squeeze() + + @staticmethod + def squeeze_u32(): + def squeeze(): + x = np.ones((1, 2, 1, 2, 1), dtype=np.uint32) + y = np.ones((2, 2, 1), dtype=np.uint32) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "squeeze_u32" + make_node([x], [y], name) + make_test( + [x], y, "input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span()))", name) + squeeze() + + @staticmethod + def squeeze_fP16x16(): + def squeeze(): + x = to_fp(np.random.randint(0, 255, (1, 2, 1, 2, 1) + ).astype(np.int64), FixedImpl.FP16x16) + y = to_fp(np.random.randint(0, 255, (2, 2, 1) + ).astype(np.int64), FixedImpl.FP16x16) + + x = Tensor(Dtype.FP16x16, x.shape, x.flatten()) + y = Tensor(Dtype.FP16x16, y.shape, y.flatten()) + + name = "squeeze_fP16x16" + make_node([x], [y], name) + make_test( + [x], y, "input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span()))", name) + squeeze() + + @staticmethod + def squeeze_fP8x23(): + def squeeze(): + x = to_fp(np.random.randint(0, 255, (1, 2, 1, 2, 1) + ).astype(np.int64), FixedImpl.FP8x23) + y = to_fp(np.random.randint(0, 255, (2, 2, 1) + ).astype(np.int64), FixedImpl.FP8x23) + + x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) + + name = "squeeze_fP8x23" + make_node([x], [y], name) + make_test( + [x], y, "input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span()))", name) + squeeze() \ No newline at end of file diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 25aebbb4a..6151a3c77 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -5,7 +5,7 @@ use option::OptionTrait; use alexandria_data_structures::array_ext::{SpanTraitExt}; use orion::operators::tensor::helpers::{len_from_shape, check_shape}; -use orion::numbers::{i8, NumberTrait}; +use orion::numbers::{i8, i32, NumberTrait}; #[derive(Copy, Drop)] struct Tensor { @@ -74,6 +74,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde { /// # tensor.new @@ -2502,6 +2503,46 @@ trait TensorTrait { fn gather( self: @Tensor, indices: Tensor, axis: Option ) -> Tensor ; + /// # tensor.squeeze + /// + /// ```rust + /// fn squeeze(self: @Tensor, axes: Option>) -> Tensor; + /// ``` + /// + /// Removes dimensions of size 1 from the shape of a tensor. + /// + /// ## Args + /// + /// * `self`(`@Tensor`) - Tensor of data to calculate non-zero indices. + /// * `axes`(`Option>`) - List of integers indicating the dimensions to squeeze. + /// + /// ## Returns + /// + /// A new `Tensor` Reshaped tensor with same data as input. + /// + /// ## Example + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn squeeze_example() -> Tensor { + /// let tensor = TensorTrait::::new( + /// shape: array![1, 2, 1, 2, 1].span(), + /// data: array![1, 1, 1, 1].span(), + /// ); + /// + /// return tensor.squeeze(axes: Option::None(()); + /// } + /// >>> [[1 1] + /// [1 1]] + /// ``` + /// + fn squeeze( + self: @Tensor, + axes: Option> + ) -> Tensor; } @@ -2874,4 +2915,74 @@ fn nonzero, impl TPartialEq: PartialEq, }; return Tensor:: {shape: array![(*self.shape).len(), stop_k + 1].span(), data: output_data.span()}; +} + +/// Cf: TensorTrait::squeeze docstring +fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + + let target_shape = match axes { + Option::Some(mut axes) => { + let mut axis_squeezed = 0; + let mut shape = *self.shape; + loop { + match axes.pop_front() { + Option::Some(axis) => { + let mut reshape: Array = ArrayTrait::new(); + let mut index = 0_usize; + let axis = if *axis.sign { + assert(*axis.mag <= (*self.shape).len(), 'axis out of accepted range'); + (*self.shape).len() - *axis.mag + } else { + assert(*axis.mag < (*self.shape).len(), 'axis out of accepted range'); + *axis.mag + }; + + loop { + match shape.pop_front() { + Option::Some(shape) => { + let squeezed = if axis >= axis_squeezed { + axis - axis_squeezed + } else { + axis + }; + if index == squeezed { + assert(*shape == 1, 'shape entry not equal to one'); + axis_squeezed += 1; + } else { + reshape.append(*shape); + } + }, + Option::None(_) => { + break; + }, + }; + index += 1; + }; + shape = reshape.span(); + }, + Option::None(_) => { + break shape; + }, + }; + } + }, + Option::None(_) => { + let mut reshape: Array = ArrayTrait::new(); + let mut shape = *self.shape; + loop { + match shape.pop_front() { + Option::Some(shape) => { + if *shape != 1 { + reshape.append(*shape); + } + }, + Option::None(_) => { + break reshape.span(); + }, + }; + } + }, + }; + + return Tensor::{ shape: target_shape, data: *self.data }; } \ No newline at end of file diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index a78655444..a89ca3439 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -8,7 +8,7 @@ use orion::operators::tensor::core::{ new_tensor, stride, Tensor, TensorTrait, ravel_index, unravel_index, reshape, at_tensor, }; use orion::operators::tensor::{math, linalg, quantization, core}; -use orion::numbers::{i8, NumberTrait, FP16x16}; +use orion::numbers::{i8, i32, NumberTrait, FP16x16}; use orion::operators::tensor::implementations::{tensor_i8::I8Tensor, tensor_u32::U32Tensor}; impl FP16x16Tensor of TensorTrait { @@ -213,6 +213,10 @@ impl FP16x16Tensor of TensorTrait { fn nonzero(self: @Tensor) -> Tensor { core::nonzero(self) } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core::squeeze(self, axes) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index e5d0e2345..9780040ae 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -8,7 +8,7 @@ use orion::operators::tensor::core::{ new_tensor, stride, Tensor, TensorTrait, ravel_index, unravel_index, reshape, at_tensor, }; use orion::operators::tensor::{math, linalg, quantization, core}; -use orion::numbers::{i8, NumberTrait, FP32x32, FP32x32Impl}; +use orion::numbers::{i8, i32, NumberTrait, FP32x32, FP32x32Impl}; use orion::numbers::fixed_point::implementations::fp32x32::core::ONE; use orion::operators::tensor::implementations::{tensor_i8::I8Tensor, tensor_u32::U32Tensor}; @@ -214,6 +214,10 @@ impl FP32x32Tensor of TensorTrait { fn nonzero(self: @Tensor) -> Tensor { core::nonzero(self) } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core::squeeze(self, axes) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index 0e36f6563..0a60d416b 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -8,7 +8,7 @@ use orion::operators::tensor::core::{ new_tensor, stride, Tensor, TensorTrait, ravel_index, unravel_index, reshape, at_tensor, }; use orion::operators::tensor::{math, linalg, quantization, core}; -use orion::numbers::{i8, NumberTrait, FP64x64, FP64x64Impl}; +use orion::numbers::{i8, i32, NumberTrait, FP64x64, FP64x64Impl}; use orion::numbers::fixed_point::implementations::fp64x64::core::ONE; use orion::operators::tensor::implementations::{tensor_i8::I8Tensor, tensor_u32::U32Tensor}; @@ -214,6 +214,10 @@ impl FP64x64Tensor of TensorTrait { fn nonzero(self: @Tensor) -> Tensor { core::nonzero(self) } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core::squeeze(self, axes) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 5dc4a086d..3c11131e3 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -8,7 +8,7 @@ use orion::operators::tensor::core::{ new_tensor, stride, Tensor, TensorTrait, ravel_index, unravel_index, reshape, at_tensor, }; use orion::operators::tensor::{math, linalg, quantization, core}; -use orion::numbers::{i8, NumberTrait, FP8x23}; +use orion::numbers::{i8, i32, NumberTrait, FP8x23}; use orion::operators::tensor::implementations::{tensor_i8::I8Tensor, tensor_u32::U32Tensor}; impl FP8x23Tensor of TensorTrait { @@ -213,6 +213,10 @@ impl FP8x23Tensor of TensorTrait { fn nonzero(self: @Tensor) -> Tensor { core::nonzero(self) } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core::squeeze(self, axes) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 3b86966ba..85baab671 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -212,6 +212,10 @@ impl I32Tensor of TensorTrait { fn nonzero(self: @Tensor) -> Tensor { core::nonzero(self) } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core::squeeze(self, axes) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index fdaa342a2..7b7b3ab7c 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -8,7 +8,7 @@ use orion::operators::tensor::core::{ new_tensor, stride, Tensor, TensorTrait, ravel_index, unravel_index, reshape, at_tensor, }; use orion::operators::tensor::{math, linalg, quantization, core}; -use orion::numbers::{i8, NumberTrait}; +use orion::numbers::{i8, i32, NumberTrait}; use orion::operators::tensor::implementations::tensor_u32::U32Tensor; impl I8Tensor of TensorTrait { @@ -211,6 +211,10 @@ impl I8Tensor of TensorTrait { fn nonzero(self: @Tensor) -> Tensor { core::nonzero(self) } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core::squeeze(self, axes) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 0ebe65282..e4e78535e 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -8,7 +8,7 @@ use orion::operators::tensor::core::{ new_tensor, stride, Tensor, TensorTrait, ravel_index, unravel_index, reshape, at_tensor, }; use orion::operators::tensor::{math, linalg, quantization, core}; -use orion::numbers::{i8, NumberTrait}; +use orion::numbers::{i8, i32, NumberTrait}; use orion::operators::tensor::implementations::tensor_i8::I8Tensor; impl U32Tensor of TensorTrait { @@ -205,6 +205,10 @@ impl U32Tensor of TensorTrait { fn nonzero(self: @Tensor) -> Tensor { core::nonzero(self) } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core::squeeze(self, axes) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/tests/nodes.cairo b/src/tests/nodes.cairo index 6bae1f201..525bc030a 100644 --- a/src/tests/nodes.cairo +++ b/src/tests/nodes.cairo @@ -403,3 +403,8 @@ mod nonzero_i8_2d; mod nonzero_i8_3d; mod nonzero_u32_2d; mod nonzero_u32_3d; +mod squeeze_fP16x16; +mod squeeze_fP8x23; +mod squeeze_i32; +mod squeeze_i8; +mod squeeze_u32; \ No newline at end of file diff --git a/src/tests/nodes/squeeze_fP16x16.cairo b/src/tests/nodes/squeeze_fP16x16.cairo new file mode 100644 index 000000000..9823e3a75 --- /dev/null +++ b/src/tests/nodes/squeeze_fP16x16.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::numbers::signed_integer::i32::{i32, IntegerTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_squeeze_fP16x16() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span())); + + assert(y.shape == z.shape, 'shapes do not match'); +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_fP16x16/input_0.cairo b/src/tests/nodes/squeeze_fP16x16/input_0.cairo new file mode 100644 index 000000000..de3aa667f --- /dev/null +++ b/src/tests/nodes/squeeze_fP16x16/input_0.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP16x16Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP16x16; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + shape.append(1); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 10485760, sign: false }); + data.append(FP16x16 { mag: 14942208, sign: false }); + data.append(FP16x16 { mag: 4456448, sign: false }); + data.append(FP16x16 { mag: 10944512, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_fP16x16/output_0.cairo b/src/tests/nodes/squeeze_fP16x16/output_0.cairo new file mode 100644 index 000000000..193144ed6 --- /dev/null +++ b/src/tests/nodes/squeeze_fP16x16/output_0.cairo @@ -0,0 +1,19 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP16x16Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP16x16; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 5505024, sign: false }); + data.append(FP16x16 { mag: 4980736, sign: false }); + data.append(FP16x16 { mag: 3997696, sign: false }); + data.append(FP16x16 { mag: 9830400, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_fP8x23.cairo b/src/tests/nodes/squeeze_fP8x23.cairo new file mode 100644 index 000000000..91d55351f --- /dev/null +++ b/src/tests/nodes/squeeze_fP8x23.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::numbers::signed_integer::i32::{i32, IntegerTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_squeeze_fP8x23() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span())); + + assert(y.shape == z.shape, 'shapes do not match'); +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_fP8x23/input_0.cairo b/src/tests/nodes/squeeze_fP8x23/input_0.cairo new file mode 100644 index 000000000..3b504573a --- /dev/null +++ b/src/tests/nodes/squeeze_fP8x23/input_0.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP8x23Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP8x23; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + shape.append(1); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 545259520, sign: false }); + data.append(FP8x23 { mag: 897581056, sign: false }); + data.append(FP8x23 { mag: 1367343104, sign: false }); + data.append(FP8x23 { mag: 226492416, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_fP8x23/output_0.cairo b/src/tests/nodes/squeeze_fP8x23/output_0.cairo new file mode 100644 index 000000000..ec58532c5 --- /dev/null +++ b/src/tests/nodes/squeeze_fP8x23/output_0.cairo @@ -0,0 +1,19 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::FP8x23Tensor; +use orion::numbers::FixedTrait; +use orion::numbers::FP8x23; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 1585446912, sign: false }); + data.append(FP8x23 { mag: 1476395008, sign: false }); + data.append(FP8x23 { mag: 528482304, sign: false }); + data.append(FP8x23 { mag: 1862270976, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_i32.cairo b/src/tests/nodes/squeeze_i32.cairo new file mode 100644 index 000000000..5560fd51d --- /dev/null +++ b/src/tests/nodes/squeeze_i32.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::numbers::signed_integer::i32::{i32, IntegerTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_squeeze_i32() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span())); + + assert(y.shape == z.shape, 'shapes do not match'); +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_i32/input_0.cairo b/src/tests/nodes/squeeze_i32/input_0.cairo new file mode 100644 index 000000000..50ea72ff9 --- /dev/null +++ b/src/tests/nodes/squeeze_i32/input_0.cairo @@ -0,0 +1,20 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + shape.append(1); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_i32/output_0.cairo b/src/tests/nodes/squeeze_i32/output_0.cairo new file mode 100644 index 000000000..a6bc3cb74 --- /dev/null +++ b/src/tests/nodes/squeeze_i32/output_0.cairo @@ -0,0 +1,18 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_i8.cairo b/src/tests/nodes/squeeze_i8.cairo new file mode 100644 index 000000000..88fa67881 --- /dev/null +++ b/src/tests/nodes/squeeze_i8.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::numbers::signed_integer::i32::{i32, IntegerTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_squeeze_i8() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span())); + + assert(y.shape == z.shape, 'shapes do not match'); +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_i8/input_0.cairo b/src/tests/nodes/squeeze_i8/input_0.cairo new file mode 100644 index 000000000..83c6ac908 --- /dev/null +++ b/src/tests/nodes/squeeze_i8/input_0.cairo @@ -0,0 +1,20 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + shape.append(1); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_i8/output_0.cairo b/src/tests/nodes/squeeze_i8/output_0.cairo new file mode 100644 index 000000000..e2e1261ee --- /dev/null +++ b/src/tests/nodes/squeeze_i8/output_0.cairo @@ -0,0 +1,18 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_u32.cairo b/src/tests/nodes/squeeze_u32.cairo new file mode 100644 index 000000000..474b18bc7 --- /dev/null +++ b/src/tests/nodes/squeeze_u32.cairo @@ -0,0 +1,42 @@ +mod input_0; +mod output_0; +mod output_non_axes; +mod output_negatives; + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::numbers::signed_integer::i32::{i32, IntegerTrait}; + +// Non Axes parameters +fn non_axes() { + let input_0 = input_0::input_0(); + let none_axes = input_0.squeeze(Option::None(())); + let z = output_non_axes::non_axes(); + assert(none_axes.shape == z.shape, 'shapes do not match (non axes)'); +} + +// Negatives Axes +fn negatives() { + let input_0 = input_0::input_0(); + let negatives = input_0.squeeze(Option::Some(array![i32 { mag: 5, sign: true }, i32 { mag: 3, sign: true }].span())); + let z = output_negatives::negatives(); + assert(negatives.shape == z.shape, 'shapes do not match (negatives)'); +} + +#[test] +#[available_gas(2000000000)] +fn test_squeeze_u32() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.squeeze(Option::Some(array![i32 { mag: 0, sign: false }, i32 { mag: 2, sign: false }].span())); + + assert(y.shape == z.shape, 'shapes do not match'); + + non_axes(); + + negatives(); + +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_u32/input_0.cairo b/src/tests/nodes/squeeze_u32/input_0.cairo new file mode 100644 index 000000000..c39e9fa26 --- /dev/null +++ b/src/tests/nodes/squeeze_u32/input_0.cairo @@ -0,0 +1,19 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + shape.append(1); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(1); + data.append(1); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_u32/output_0.cairo b/src/tests/nodes/squeeze_u32/output_0.cairo new file mode 100644 index 000000000..8ae3234d4 --- /dev/null +++ b/src/tests/nodes/squeeze_u32/output_0.cairo @@ -0,0 +1,17 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(1); + data.append(1); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_u32/output_negatives.cairo b/src/tests/nodes/squeeze_u32/output_negatives.cairo new file mode 100644 index 000000000..3e20c7f69 --- /dev/null +++ b/src/tests/nodes/squeeze_u32/output_negatives.cairo @@ -0,0 +1,17 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn negatives() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(1); + data.append(1); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/src/tests/nodes/squeeze_u32/output_non_axes.cairo b/src/tests/nodes/squeeze_u32/output_non_axes.cairo new file mode 100644 index 000000000..78dc51c72 --- /dev/null +++ b/src/tests/nodes/squeeze_u32/output_non_axes.cairo @@ -0,0 +1,16 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn non_axes() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(1); + data.append(1); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file