From 5359f78755907ca6a8b2a6ebf95ce7f8b118dbca Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Thu, 26 Oct 2023 08:33:13 +0200 Subject: [PATCH 01/21] Rename old min operator & adapt repo --- docs/SUMMARY.md | 2 +- docs/framework/compatibility.md | 1 + docs/framework/operators/tensor/README.md | 2 +- .../{tensor.min.md => tensor.min_in_tensor.md} | 10 +++++----- src/operators/tensor/core.cairo | 14 +++++++------- .../tensor/implementations/tensor_fp16x16.cairo | 4 ++-- .../implementations/tensor_fp16x16wide.cairo | 4 ++-- .../tensor/implementations/tensor_fp32x32.cairo | 4 ++-- .../tensor/implementations/tensor_fp64x64.cairo | 4 ++-- .../tensor/implementations/tensor_fp8x23.cairo | 4 ++-- .../tensor/implementations/tensor_fp8x23wide.cairo | 4 ++-- .../tensor/implementations/tensor_i32.cairo | 4 ++-- .../tensor/implementations/tensor_i8.cairo | 4 ++-- .../tensor/implementations/tensor_u32.cairo | 4 ++-- src/operators/tensor/math.cairo | 2 +- .../tensor/math/{min.cairo => min_in_tensor.cairo} | 2 +- .../min/min_fp_test/min_fp16x16_test.cairo | 6 +++--- .../min/min_fp_test/min_fp8x23_test.cairo | 6 +++--- tests/src/tensor_core/min/min_i32_test.cairo | 6 +++--- tests/src/tensor_core/min/min_u32_test.cairo | 6 +++--- 20 files changed, 47 insertions(+), 46 deletions(-) rename docs/framework/operators/tensor/{tensor.min.md => tensor.min_in_tensor.md} (68%) rename src/operators/tensor/math/{min.cairo => min_in_tensor.cairo} (93%) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 98b1b414b..69deeada8 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -40,7 +40,7 @@ * [Tensor](framework/operators/tensor/README.md) * [tensor.new](framework/operators/tensor/tensor.new.md) * [tensor.at](framework/operators/tensor/tensor.at.md) - * [tensor.min](framework/operators/tensor/tensor.min.md) + * [tensor.min_in_tensor](framework/operators/tensor/tensor.min_in_tensor.md) * [tensor.max](framework/operators/tensor/tensor.max.md) * [tensor.stride](framework/operators/tensor/tensor.stride.md) * [tensor.ravel\_index](framework/operators/tensor/tensor.ravel\_index.md) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index 91457e120..8f8b9ba20 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -66,5 +66,6 @@ You can see below the list of current supported ONNX Operators: | [Xor](operators/tensor/tensor.xor.md) | :white\_check\_mark: | | [Or](operators/tensor/tensor.or.md) | :white\_check\_mark: | | [Gemm](operators/neural-network/nn.gemm.md) | :white\_check\_mark: | +| [MinInTensor](operators/tensor/tensor.min\_in\_tensor.md) | :white\_check\_mark: | Current Operators support: **60/156 (38%)** diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index 08dc96ec0..ecc3ea286 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -50,7 +50,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.xor`](tensor.xor.md) | Computes the logical XOR of two tensors element-wise. | | [`tensor.stride`](tensor.stride.md) | Computes the stride of each dimension in the tensor. | | [`tensor.onehot`](tensor.onehot.md) | Produces one-hot tensor based on input. | -| [`tensor.min`](tensor.min.md) | Returns the minimum value in the tensor. | +| [`tensor.min_in_tensor`](tensor.min\_in\_tensor.md) | Returns the minimum value in the tensor. | | [`tensor.max`](tensor.max.md) | Returns the maximum value in the tensor. | | [`tensor.reduce_sum`](tensor.reduce\_sum.md) | Reduces a tensor by summing its elements along a specified axis. | | [`tensor.argmax`](tensor.argmax.md) | Returns the index of the maximum value along the specified axis. | diff --git a/docs/framework/operators/tensor/tensor.min.md b/docs/framework/operators/tensor/tensor.min_in_tensor.md similarity index 68% rename from docs/framework/operators/tensor/tensor.min.md rename to docs/framework/operators/tensor/tensor.min_in_tensor.md index 037c4067f..65ac74034 100644 --- a/docs/framework/operators/tensor/tensor.min.md +++ b/docs/framework/operators/tensor/tensor.min_in_tensor.md @@ -1,7 +1,7 @@ -# tensor.min +# tensor.min_in_tensor ```rust - fn min(self: @Tensor) -> T; + fn min_in_tensor(self: @Tensor) -> T; ``` Returns the minimum value in the tensor. @@ -21,14 +21,14 @@ use array::{ArrayTrait, SpanTrait}; use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; -fn min_example() -> u32 { +fn min_in_tensor_example() -> u32 { let tensor = TensorTrait::new( shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), ); - // We can call `min` function as follows. - return tensor.min(); + // We can call `min_in_tensor` function as follows. + return tensor.min_in_tensor(); } >>> 0 ``` diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 1ad154e3f..8a62c7395 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -46,7 +46,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde { /// ``` /// fn at(self: @Tensor, indices: Span) -> T; - /// # tensor.min + /// # tensor.min_in_tensor /// /// ```rust - /// fn min(self: @Tensor) -> T; + /// fn min_in_tensor(self: @Tensor) -> T; /// ``` /// /// Returns the minimum value in the tensor. @@ -206,19 +206,19 @@ trait TensorTrait { /// /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; /// - /// fn min_example() -> u32 { + /// fn min_in_tensor_example() -> u32 { /// let tensor = TensorTrait::new( /// shape: array![2, 2, 2].span(), /// data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), /// ); /// - /// // We can call `min` function as follows. - /// return tensor.min(); + /// // We can call `min_in_tensor` function as follows. + /// return tensor.min_in_tensor(); /// } /// >>> 0 /// ``` /// - fn min(self: @Tensor) -> T; + fn min_in_tensor(self: @Tensor) -> T; /// # tensor.max /// /// ```rust diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index 0ac84fedc..e7e3d258b 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -20,8 +20,8 @@ impl FP16x16Tensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> FP16x16 { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> FP16x16 { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> FP16x16 { diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 101ad4d21..7ee112ad0 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -20,8 +20,8 @@ impl FP16x16WTensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> FP16x16W { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> FP16x16W { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> FP16x16W { diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 3d0c40dcd..a367fd3bc 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -21,8 +21,8 @@ impl FP32x32Tensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> FP32x32 { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> FP32x32 { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> FP32x32 { diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index 1754a8a4a..edb48aded 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -21,8 +21,8 @@ impl FP64x64Tensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> FP64x64 { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> FP64x64 { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> FP64x64 { diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 71c17cf6f..dedd92dfb 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -20,8 +20,8 @@ impl FP8x23Tensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> FP8x23 { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> FP8x23 { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> FP8x23 { diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index e81beb028..657ec83b1 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -20,8 +20,8 @@ impl FP8x23WTensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> FP8x23W { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> FP8x23W { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> FP8x23W { diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index d7b9c1548..b0a22c420 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -21,8 +21,8 @@ impl I32Tensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> i32 { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> i32 { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> i32 { diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index 9645c4be5..bc31c594c 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -20,8 +20,8 @@ impl I8Tensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> i8 { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> i8 { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> i8 { diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 7840c31af..5336bb5ec 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -20,8 +20,8 @@ impl U32Tensor of TensorTrait { *at_tensor(self, indices) } - fn min(self: @Tensor) -> u32 { - math::min::min_in_tensor::(*self.data) + fn min_in_tensor(self: @Tensor) -> u32 { + math::min_in_tensor::min_in_tensor::(*self.data) } fn max(self: @Tensor) -> u32 { diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index 36c79015b..90550d32d 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -1,4 +1,4 @@ -mod min; +mod min_in_tensor; mod max; mod reduce_sum; mod argmax; diff --git a/src/operators/tensor/math/min.cairo b/src/operators/tensor/math/min_in_tensor.cairo similarity index 93% rename from src/operators/tensor/math/min.cairo rename to src/operators/tensor/math/min_in_tensor.cairo index 518e0eb69..b9738db1f 100644 --- a/src/operators/tensor/math/min.cairo +++ b/src/operators/tensor/math/min_in_tensor.cairo @@ -3,7 +3,7 @@ use option::OptionTrait; use orion::numbers::NumberTrait; -/// Cf: TensorTrait::min docstring +/// Cf: TensorTrait::min_in_tensor docstring fn min_in_tensor< T, MAG, diff --git a/tests/src/tensor_core/min/min_fp_test/min_fp16x16_test.cairo b/tests/src/tensor_core/min/min_fp_test/min_fp16x16_test.cairo index 2e33ce90e..83eb77cb6 100644 --- a/tests/src/tensor_core/min/min_fp_test/min_fp16x16_test.cairo +++ b/tests/src/tensor_core/min/min_fp_test/min_fp16x16_test.cairo @@ -13,7 +13,7 @@ mod tensor_1D { fn tensor_min() { let tensor = fp_tensor_1x3_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } @@ -34,7 +34,7 @@ mod tensor_2D { fn tensor_min() { let tensor = fp_tensor_2x2_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } @@ -55,7 +55,7 @@ mod tensor_3D { fn tensor_min() { let tensor = fp_tensor_2x2x2_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } diff --git a/tests/src/tensor_core/min/min_fp_test/min_fp8x23_test.cairo b/tests/src/tensor_core/min/min_fp_test/min_fp8x23_test.cairo index 54a7bc7b3..35f60092f 100644 --- a/tests/src/tensor_core/min/min_fp_test/min_fp8x23_test.cairo +++ b/tests/src/tensor_core/min/min_fp_test/min_fp8x23_test.cairo @@ -13,7 +13,7 @@ mod tensor_1D { fn tensor_min() { let tensor = fp_tensor_1x3_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } @@ -34,7 +34,7 @@ mod tensor_2D { fn tensor_min() { let tensor = fp_tensor_2x2_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } @@ -55,7 +55,7 @@ mod tensor_3D { fn tensor_min() { let tensor = fp_tensor_2x2x2_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } diff --git a/tests/src/tensor_core/min/min_i32_test.cairo b/tests/src/tensor_core/min/min_i32_test.cairo index 5158369c0..e24d5b4d9 100644 --- a/tests/src/tensor_core/min/min_i32_test.cairo +++ b/tests/src/tensor_core/min/min_i32_test.cairo @@ -11,7 +11,7 @@ mod tensor_1D { fn tensor_min() { let tensor = i32_tensor_1x3_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } @@ -29,7 +29,7 @@ mod tensor_2D { fn tensor_min() { let tensor = i32_tensor_2x2_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } @@ -47,7 +47,7 @@ mod tensor_3D { fn tensor_min() { let tensor = i32_tensor_2x2x2_helper(); - let result = tensor.min().mag; + let result = tensor.min_in_tensor().mag; assert(result == 0, 'tensor.min = 0'); } } diff --git a/tests/src/tensor_core/min/min_u32_test.cairo b/tests/src/tensor_core/min/min_u32_test.cairo index 8bc1bb8c0..53802c6b9 100644 --- a/tests/src/tensor_core/min/min_u32_test.cairo +++ b/tests/src/tensor_core/min/min_u32_test.cairo @@ -11,7 +11,7 @@ mod tensor_1D { fn tensor_min() { let tensor = u32_tensor_1x3_helper(); - let result = tensor.min(); + let result = tensor.min_in_tensor(); assert(result == 0, 'tensor.min = 0'); } } @@ -29,7 +29,7 @@ mod tensor_2D { fn tensor_min() { let tensor = u32_tensor_2x2_helper(); - let result = tensor.min(); + let result = tensor.min_in_tensor(); assert(result == 0, 'tensor.min = 0'); } } @@ -47,7 +47,7 @@ mod tensor_3D { fn tensor_min() { let tensor = u32_tensor_2x2x2_helper(); - let result = tensor.min(); + let result = tensor.min_in_tensor(); assert(result == 0, 'tensor.min = 0'); } } From 9b61342e569ea47e5ba7bdaa4f89d5c3e9523a8d Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Thu, 26 Oct 2023 11:17:31 +0200 Subject: [PATCH 02/21] Implement 'min' operator --- src/operators/tensor/core.cairo | 1 + .../implementations/tensor_fp16x16.cairo | 4 ++ .../implementations/tensor_fp16x16wide.cairo | 4 ++ .../implementations/tensor_fp32x32.cairo | 4 ++ .../implementations/tensor_fp64x64.cairo | 4 ++ .../implementations/tensor_fp8x23.cairo | 4 ++ .../implementations/tensor_fp8x23wide.cairo | 4 ++ .../tensor/implementations/tensor_i32.cairo | 4 ++ .../tensor/implementations/tensor_i8.cairo | 4 ++ .../tensor/implementations/tensor_u32.cairo | 4 ++ src/operators/tensor/math.cairo | 1 + src/operators/tensor/math/min.cairo | 68 +++++++++++++++++++ 12 files changed, 106 insertions(+) create mode 100644 src/operators/tensor/math/min.cairo diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 8a62c7395..d31dfee54 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -219,6 +219,7 @@ trait TensorTrait { /// ``` /// fn min_in_tensor(self: @Tensor) -> T; + fn min(tensors: Span>) -> Tensor; /// # tensor.max /// /// ```rust diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index e7e3d258b..ccf5df837 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -24,6 +24,10 @@ impl FP16x16Tensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> FP16x16 { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 7ee112ad0..345f54210 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -24,6 +24,10 @@ impl FP16x16WTensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> FP16x16W { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index a367fd3bc..661fb2f85 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -25,6 +25,10 @@ impl FP32x32Tensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> FP32x32 { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index edb48aded..d4060e557 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -25,6 +25,10 @@ impl FP64x64Tensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> FP64x64 { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index dedd92dfb..fe61dc2bd 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -24,6 +24,10 @@ impl FP8x23Tensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> FP8x23 { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index 657ec83b1..4138f3921 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -24,6 +24,10 @@ impl FP8x23WTensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> FP8x23W { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index b0a22c420..fe1785afc 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -25,6 +25,10 @@ impl I32Tensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> i32 { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index bc31c594c..bd5e00eaf 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -24,6 +24,10 @@ impl I8Tensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> i8 { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 5336bb5ec..99e66f21b 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -24,6 +24,10 @@ impl U32Tensor of TensorTrait { math::min_in_tensor::min_in_tensor::(*self.data) } + fn min(tensors: Span>) -> Tensor { + math::min::min(tensors) + } + fn max(self: @Tensor) -> u32 { math::max::max_in_tensor(*self.data) } diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index 90550d32d..40cf79df8 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -1,4 +1,5 @@ mod min_in_tensor; +mod min; mod max; mod reduce_sum; mod argmax; diff --git a/src/operators/tensor/math/min.cairo b/src/operators/tensor/math/min.cairo new file mode 100644 index 000000000..b3f90b965 --- /dev/null +++ b/src/operators/tensor/math/min.cairo @@ -0,0 +1,68 @@ +use array::{ArrayTrait, SpanTrait}; + +use orion::numbers::NumberTrait; +use orion::operators::tensor::core::{Tensor, TensorTrait, unravel_index}; +use orion::operators::tensor::helpers::{ + broadcast_shape, broadcast_index_mapping, len_from_shape, check_compatibility +}; + +/// Cf: TensorTrait::min docstring +fn min< + T, + MAG, + impl TTensorTrait: TensorTrait, + impl TNumber: NumberTrait, + impl TCopy: Copy, + impl TDrop: Drop +>( + mut tensors: Span> +) -> Tensor { + + assert(tensors.len() >= 1, 'Input tensors must be >= 1'); + + let first_tensor = *tensors.at(0); + + if tensors.len() == 1 { + return first_tensor; + } + + let mut min_shape: Span = first_tensor.shape; + let mut min_data: Span = first_tensor.data; + + let mut tensor_counter: usize = 1; + + loop { + if tensor_counter > tensors.len() - 1 { + break; + } + + let mut new_min_data = ArrayTrait::::new(); + + let mut current_tensor = *tensors.at(tensor_counter); + + let mut broadcasted_shape = broadcast_shape(min_shape, current_tensor.shape); + + let num_elements = len_from_shape(broadcasted_shape); + let mut n: usize = 0; + loop { + let mut indices_broadcasted = unravel_index(n, broadcasted_shape); + + let mut indices_self = broadcast_index_mapping(min_shape, indices_broadcasted); + let mut indices_other = broadcast_index_mapping(current_tensor.shape, indices_broadcasted); + + let mut min_value = NumberTrait::min(*(min_data)[indices_self], *(current_tensor.data)[indices_other]); + new_min_data.append(min_value); + + n += 1; + if n == num_elements { + break (); + }; + }; + + min_shape = broadcasted_shape; + min_data = new_min_data.span(); + tensor_counter += 1; + }; + + return TensorTrait::::new(min_shape, min_data); +} \ No newline at end of file From 8d4206929a34e201f53d4810f84ceb518d1ef125 Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Fri, 27 Oct 2023 10:44:33 +0200 Subject: [PATCH 03/21] Add nodegen tests --- nodegen/node/min.py | 364 ++++++++++++++++++ tests/src/nodes.cairo | 20 + .../min_fp16x16_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../input_2.cairo | 15 + .../output_0.cairo | 18 + .../min_fp16x16_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../output_0.cairo | 18 + .../src/nodes/min_fp16x16_three_tensors.cairo | 24 ++ .../min_fp16x16_three_tensors/input_0.cairo | 42 ++ .../min_fp16x16_three_tensors/input_1.cairo | 42 ++ .../min_fp16x16_three_tensors/input_2.cairo | 42 ++ .../min_fp16x16_three_tensors/output_0.cairo | 42 ++ tests/src/nodes/min_fp16x16_two_tensors.cairo | 22 ++ .../min_fp16x16_two_tensors/input_0.cairo | 42 ++ .../min_fp16x16_two_tensors/input_1.cairo | 42 ++ .../min_fp16x16_two_tensors/output_0.cairo | 42 ++ .../min_fp8x23_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../input_2.cairo | 15 + .../output_0.cairo | 18 + .../min_fp8x23_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../output_0.cairo | 18 + .../src/nodes/min_fp8x23_three_tensors.cairo | 24 ++ .../min_fp8x23_three_tensors/input_0.cairo | 42 ++ .../min_fp8x23_three_tensors/input_1.cairo | 42 ++ .../min_fp8x23_three_tensors/input_2.cairo | 42 ++ .../min_fp8x23_three_tensors/output_0.cairo | 42 ++ tests/src/nodes/min_fp8x23_two_tensors.cairo | 22 ++ .../min_fp8x23_two_tensors/input_0.cairo | 42 ++ .../min_fp8x23_two_tensors/input_1.cairo | 42 ++ .../min_fp8x23_two_tensors/output_0.cairo | 42 ++ .../min_i32_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../input_2.cairo | 14 + .../output_0.cairo | 17 + .../nodes/min_i32_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../output_0.cairo | 17 + tests/src/nodes/min_i32_three_tensors.cairo | 24 ++ .../nodes/min_i32_three_tensors/input_0.cairo | 41 ++ .../nodes/min_i32_three_tensors/input_1.cairo | 41 ++ .../nodes/min_i32_three_tensors/input_2.cairo | 41 ++ .../min_i32_three_tensors/output_0.cairo | 41 ++ tests/src/nodes/min_i32_two_tensors.cairo | 22 ++ .../nodes/min_i32_two_tensors/input_0.cairo | 41 ++ .../nodes/min_i32_two_tensors/input_1.cairo | 41 ++ .../nodes/min_i32_two_tensors/output_0.cairo | 41 ++ .../min_i8_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../input_2.cairo | 14 + .../output_0.cairo | 17 + .../nodes/min_i8_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../output_0.cairo | 17 + tests/src/nodes/min_i8_three_tensors.cairo | 24 ++ .../nodes/min_i8_three_tensors/input_0.cairo | 41 ++ .../nodes/min_i8_three_tensors/input_1.cairo | 41 ++ .../nodes/min_i8_three_tensors/input_2.cairo | 41 ++ .../nodes/min_i8_three_tensors/output_0.cairo | 41 ++ tests/src/nodes/min_i8_two_tensors.cairo | 22 ++ .../nodes/min_i8_two_tensors/input_0.cairo | 41 ++ .../nodes/min_i8_two_tensors/input_1.cairo | 41 ++ .../nodes/min_i8_two_tensors/output_0.cairo | 41 ++ .../min_u32_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 16 + .../input_1.cairo | 14 + .../input_2.cairo | 13 + .../output_0.cairo | 16 + .../nodes/min_u32_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 16 + .../input_1.cairo | 14 + .../output_0.cairo | 16 + tests/src/nodes/min_u32_three_tensors.cairo | 24 ++ .../nodes/min_u32_three_tensors/input_0.cairo | 40 ++ .../nodes/min_u32_three_tensors/input_1.cairo | 40 ++ .../nodes/min_u32_three_tensors/input_2.cairo | 40 ++ .../min_u32_three_tensors/output_0.cairo | 40 ++ tests/src/nodes/min_u32_two_tensors.cairo | 22 ++ .../nodes/min_u32_two_tensors/input_0.cairo | 40 ++ .../nodes/min_u32_two_tensors/input_1.cairo | 40 ++ .../nodes/min_u32_two_tensors/output_0.cairo | 40 ++ 92 files changed, 2853 insertions(+) create mode 100644 nodegen/node/min.py create mode 100644 tests/src/nodes/min_fp16x16_broadcast_three_tensors.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_two_tensors.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp16x16_broadcast_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_fp16x16_three_tensors.cairo create mode 100644 tests/src/nodes/min_fp16x16_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp16x16_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp16x16_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_fp16x16_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_fp16x16_two_tensors.cairo create mode 100644 tests/src/nodes/min_fp16x16_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp16x16_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp16x16_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_three_tensors.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_two_tensors.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp8x23_broadcast_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_three_tensors.cairo create mode 100644 tests/src/nodes/min_fp8x23_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp8x23_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_fp8x23_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_two_tensors.cairo create mode 100644 tests/src/nodes/min_fp8x23_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_fp8x23_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_fp8x23_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_three_tensors.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_two_tensors.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i32_broadcast_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i32_three_tensors.cairo create mode 100644 tests/src/nodes/min_i32_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i32_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i32_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_i32_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i32_two_tensors.cairo create mode 100644 tests/src/nodes/min_i32_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i32_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i32_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_three_tensors.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_two_tensors.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i8_broadcast_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i8_three_tensors.cairo create mode 100644 tests/src/nodes/min_i8_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i8_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i8_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_i8_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_i8_two_tensors.cairo create mode 100644 tests/src/nodes/min_i8_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_i8_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_i8_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_three_tensors.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_two_tensors.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_u32_broadcast_two_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_u32_three_tensors.cairo create mode 100644 tests/src/nodes/min_u32_three_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_u32_three_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_u32_three_tensors/input_2.cairo create mode 100644 tests/src/nodes/min_u32_three_tensors/output_0.cairo create mode 100644 tests/src/nodes/min_u32_two_tensors.cairo create mode 100644 tests/src/nodes/min_u32_two_tensors/input_0.cairo create mode 100644 tests/src/nodes/min_u32_two_tensors/input_1.cairo create mode 100644 tests/src/nodes/min_u32_two_tensors/output_0.cairo diff --git a/nodegen/node/min.py b/nodegen/node/min.py new file mode 100644 index 000000000..a6299cea0 --- /dev/null +++ b/nodegen/node/min.py @@ -0,0 +1,364 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl, Trait + +class Min(RunAll): + + @staticmethod + def min_u32_two_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + z = np.minimum(x, y) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + + name = "min_u32_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.uint32) + y = np.random.randint(0, 6, (1, 2)).astype(np.uint32) + z = np.minimum(x, y) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + + name = "min_u32_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def min_i32_two_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + z = np.minimum(x, y) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + + name = "min_i32_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int32) + y = np.random.randint(0, 6, (1, 2)).astype(np.int32) + z = np.minimum(x, y) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + + name = "min_i32_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def min_i8_two_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + z = np.minimum(x, y) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + + name = "min_i8_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int8) + y = np.random.randint(0, 6, (1, 2)).astype(np.int8) + z = np.minimum(x, y) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + + name = "min_i8_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def min_fp8x23_two_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.minimum(x, y) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + + name = "min_fp8x23_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.minimum(x, y) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + + name = "min_fp8x23_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def min_fp16x16_two_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.minimum(x, y) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + + name = "min_fp16x16_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.minimum(x, y) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + + name = "min_fp16x16_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::min(array![input_0, input_1].span());", name) + + default() + broadcast() + + + @staticmethod + def min_u32_three_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + z = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + m = Tensor(Dtype.U32, m.shape, m.flatten()) + + name = "min_u32_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.uint32) + y = np.random.randint(0, 6, (1, 2)).astype(np.uint32) + z = np.random.randint(0, 6, (1, 1)).astype(np.uint32) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + m = Tensor(Dtype.U32, m.shape, m.flatten()) + + name = "min_u32_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def min_i32_three_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + z = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + m = Tensor(Dtype.I32, m.shape, m.flatten()) + + name = "min_i32_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int32) + y = np.random.randint(0, 6, (1, 2)).astype(np.int32) + z = np.random.randint(0, 6, (1, 1)).astype(np.int32) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + m = Tensor(Dtype.I32, m.shape, m.flatten()) + + name = "min_i32_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def min_i8_three_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + z = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + m = Tensor(Dtype.I8, m.shape, m.flatten()) + + name = "min_i8_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int8) + y = np.random.randint(0, 6, (1, 2)).astype(np.int8) + z = np.random.randint(0, 6, (1, 1)).astype(np.int8) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + m = Tensor(Dtype.I8, m.shape, m.flatten()) + + name = "min_i8_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def min_fp8x23_three_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + m = Tensor(Dtype.FP8x23, m.shape, to_fp( + m.flatten(), FixedImpl.FP8x23)) + + name = "min_fp8x23_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.random.randint(-3, 3, (1, 1)).astype(np.float64) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + m = Tensor(Dtype.FP8x23, m.shape, to_fp( + m.flatten(), FixedImpl.FP8x23)) + + name = "min_fp8x23_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def min_fp16x16_three_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + m = Tensor(Dtype.FP16x16, m.shape, to_fp( + m.flatten(), FixedImpl.FP16x16)) + + name = "min_fp16x16_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.random.randint(-3, 3, (1, 1)).astype(np.float64) + m = np.minimum(np.minimum(x, y), z) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + m = Tensor(Dtype.FP16x16, m.shape, to_fp( + m.flatten(), FixedImpl.FP16x16)) + + name = "min_fp16x16_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::min(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() diff --git a/tests/src/nodes.cairo b/tests/src/nodes.cairo index 466b0e664..519bad9a4 100644 --- a/tests/src/nodes.cairo +++ b/tests/src/nodes.cairo @@ -464,3 +464,23 @@ mod gemm_default_vector_bias; mod gemm_default_no_bias; mod gemm_transposeA; mod gemm_transposeB; +mod min_fp16x16_three_tensors; +mod min_fp16x16_broadcast_three_tensors; +mod min_fp16x16_two_tensors; +mod min_fp16x16_broadcast_two_tensors; +mod min_fp8x23_three_tensors; +mod min_fp8x23_broadcast_three_tensors; +mod min_fp8x23_two_tensors; +mod min_fp8x23_broadcast_two_tensors; +mod min_i32_three_tensors; +mod min_i32_broadcast_three_tensors; +mod min_i32_two_tensors; +mod min_i32_broadcast_two_tensors; +mod min_i8_three_tensors; +mod min_i8_broadcast_three_tensors; +mod min_i8_two_tensors; +mod min_i8_broadcast_two_tensors; +mod min_u32_three_tensors; +mod min_u32_broadcast_three_tensors; +mod min_u32_two_tensors; +mod min_u32_broadcast_two_tensors; diff --git a/tests/src/nodes/min_fp16x16_broadcast_three_tensors.cairo b/tests/src/nodes/min_fp16x16_broadcast_three_tensors.cairo new file mode 100644 index 000000000..c71edce86 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp16x16_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_0.cairo b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..bf27fbdf0 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_1.cairo b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..d7359bc64 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_2.cairo b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..406f91365 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,15 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_three_tensors/output_0.cairo b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..98ef140fa --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_two_tensors.cairo b/tests/src/nodes/min_fp16x16_broadcast_two_tensors.cairo new file mode 100644 index 000000000..3b998ca1a --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp16x16_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_0.cairo b/tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..c1c0930a4 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_1.cairo b/tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..d7359bc64 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_broadcast_two_tensors/output_0.cairo b/tests/src/nodes/min_fp16x16_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..0e8b2aebb --- /dev/null +++ b/tests/src/nodes/min_fp16x16_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_three_tensors.cairo b/tests/src/nodes/min_fp16x16_three_tensors.cairo new file mode 100644 index 000000000..8a5d60711 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp16x16_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_three_tensors/input_0.cairo b/tests/src/nodes/min_fp16x16_three_tensors/input_0.cairo new file mode 100644 index 000000000..747696a57 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_three_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_three_tensors/input_1.cairo b/tests/src/nodes/min_fp16x16_three_tensors/input_1.cairo new file mode 100644 index 000000000..22d5848ef --- /dev/null +++ b/tests/src/nodes/min_fp16x16_three_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_three_tensors/input_2.cairo b/tests/src/nodes/min_fp16x16_three_tensors/input_2.cairo new file mode 100644 index 000000000..02084123c --- /dev/null +++ b/tests/src/nodes/min_fp16x16_three_tensors/input_2.cairo @@ -0,0 +1,42 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_three_tensors/output_0.cairo b/tests/src/nodes/min_fp16x16_three_tensors/output_0.cairo new file mode 100644 index 000000000..6931f714a --- /dev/null +++ b/tests/src/nodes/min_fp16x16_three_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_two_tensors.cairo b/tests/src/nodes/min_fp16x16_two_tensors.cairo new file mode 100644 index 000000000..25adc490e --- /dev/null +++ b/tests/src/nodes/min_fp16x16_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp16x16_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_two_tensors/input_0.cairo b/tests/src/nodes/min_fp16x16_two_tensors/input_0.cairo new file mode 100644 index 000000000..3cc3af9cb --- /dev/null +++ b/tests/src/nodes/min_fp16x16_two_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_two_tensors/input_1.cairo b/tests/src/nodes/min_fp16x16_two_tensors/input_1.cairo new file mode 100644 index 000000000..230c3bfd0 --- /dev/null +++ b/tests/src/nodes/min_fp16x16_two_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp16x16_two_tensors/output_0.cairo b/tests/src/nodes/min_fp16x16_two_tensors/output_0.cairo new file mode 100644 index 000000000..785211ceb --- /dev/null +++ b/tests/src/nodes/min_fp16x16_two_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_three_tensors.cairo b/tests/src/nodes/min_fp8x23_broadcast_three_tensors.cairo new file mode 100644 index 000000000..2596fabad --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp8x23_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_0.cairo b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..c5eee64f3 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_1.cairo b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..5ddfee383 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_2.cairo b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..87ac9a1aa --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,15 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_three_tensors/output_0.cairo b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..6a9b1f878 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_two_tensors.cairo b/tests/src/nodes/min_fp8x23_broadcast_two_tensors.cairo new file mode 100644 index 000000000..b231c5d76 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp8x23_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_0.cairo b/tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..06a77989a --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_1.cairo b/tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..538d00e70 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_broadcast_two_tensors/output_0.cairo b/tests/src/nodes/min_fp8x23_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..dbb598ae0 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_three_tensors.cairo b/tests/src/nodes/min_fp8x23_three_tensors.cairo new file mode 100644 index 000000000..676f8f312 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp8x23_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_three_tensors/input_0.cairo b/tests/src/nodes/min_fp8x23_three_tensors/input_0.cairo new file mode 100644 index 000000000..9716ed3b9 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_three_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_three_tensors/input_1.cairo b/tests/src/nodes/min_fp8x23_three_tensors/input_1.cairo new file mode 100644 index 000000000..49311a2b0 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_three_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_three_tensors/input_2.cairo b/tests/src/nodes/min_fp8x23_three_tensors/input_2.cairo new file mode 100644 index 000000000..9b8b0650e --- /dev/null +++ b/tests/src/nodes/min_fp8x23_three_tensors/input_2.cairo @@ -0,0 +1,42 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_three_tensors/output_0.cairo b/tests/src/nodes/min_fp8x23_three_tensors/output_0.cairo new file mode 100644 index 000000000..af08ef421 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_three_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_two_tensors.cairo b/tests/src/nodes/min_fp8x23_two_tensors.cairo new file mode 100644 index 000000000..01d2724ea --- /dev/null +++ b/tests/src/nodes/min_fp8x23_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_fp8x23_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_two_tensors/input_0.cairo b/tests/src/nodes/min_fp8x23_two_tensors/input_0.cairo new file mode 100644 index 000000000..293eb3e83 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_two_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_two_tensors/input_1.cairo b/tests/src/nodes/min_fp8x23_two_tensors/input_1.cairo new file mode 100644 index 000000000..76f71b638 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_two_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_fp8x23_two_tensors/output_0.cairo b/tests/src/nodes/min_fp8x23_two_tensors/output_0.cairo new file mode 100644 index 000000000..c0e8bbc56 --- /dev/null +++ b/tests/src/nodes/min_fp8x23_two_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_three_tensors.cairo b/tests/src/nodes/min_i32_broadcast_three_tensors.cairo new file mode 100644 index 000000000..74ad7c090 --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i32_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_three_tensors/input_0.cairo b/tests/src/nodes/min_i32_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..3ffb54363 --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_three_tensors/input_1.cairo b/tests/src/nodes/min_i32_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..97515f83e --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_three_tensors/input_2.cairo b/tests/src/nodes/min_i32_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..2573224ed --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_three_tensors/output_0.cairo b/tests/src/nodes/min_i32_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..3031cb2bf --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_two_tensors.cairo b/tests/src/nodes/min_i32_broadcast_two_tensors.cairo new file mode 100644 index 000000000..43e5f99e0 --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i32_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_two_tensors/input_0.cairo b/tests/src/nodes/min_i32_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..b9cd5f819 --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_two_tensors/input_1.cairo b/tests/src/nodes/min_i32_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..ab5754cf2 --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_broadcast_two_tensors/output_0.cairo b/tests/src/nodes/min_i32_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..fa1f29299 --- /dev/null +++ b/tests/src/nodes/min_i32_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_three_tensors.cairo b/tests/src/nodes/min_i32_three_tensors.cairo new file mode 100644 index 000000000..adb4350db --- /dev/null +++ b/tests/src/nodes/min_i32_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i32_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_three_tensors/input_0.cairo b/tests/src/nodes/min_i32_three_tensors/input_0.cairo new file mode 100644 index 000000000..a255474d1 --- /dev/null +++ b/tests/src/nodes/min_i32_three_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_three_tensors/input_1.cairo b/tests/src/nodes/min_i32_three_tensors/input_1.cairo new file mode 100644 index 000000000..33568613e --- /dev/null +++ b/tests/src/nodes/min_i32_three_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_three_tensors/input_2.cairo b/tests/src/nodes/min_i32_three_tensors/input_2.cairo new file mode 100644 index 000000000..9b822fc6d --- /dev/null +++ b/tests/src/nodes/min_i32_three_tensors/input_2.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_three_tensors/output_0.cairo b/tests/src/nodes/min_i32_three_tensors/output_0.cairo new file mode 100644 index 000000000..9b0a9b23a --- /dev/null +++ b/tests/src/nodes/min_i32_three_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, 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/tests/src/nodes/min_i32_two_tensors.cairo b/tests/src/nodes/min_i32_two_tensors.cairo new file mode 100644 index 000000000..ae444f6e7 --- /dev/null +++ b/tests/src/nodes/min_i32_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i32_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_two_tensors/input_0.cairo b/tests/src/nodes/min_i32_two_tensors/input_0.cairo new file mode 100644 index 000000000..518ef5c24 --- /dev/null +++ b/tests/src/nodes/min_i32_two_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_two_tensors/input_1.cairo b/tests/src/nodes/min_i32_two_tensors/input_1.cairo new file mode 100644 index 000000000..42bdad0c9 --- /dev/null +++ b/tests/src/nodes/min_i32_two_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i32_two_tensors/output_0.cairo b/tests/src/nodes/min_i32_two_tensors/output_0.cairo new file mode 100644 index 000000000..af1b203d5 --- /dev/null +++ b/tests/src/nodes/min_i32_two_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_three_tensors.cairo b/tests/src/nodes/min_i8_broadcast_three_tensors.cairo new file mode 100644 index 000000000..4b67ffc7d --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i8_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_three_tensors/input_0.cairo b/tests/src/nodes/min_i8_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..254b04d0a --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_three_tensors/input_1.cairo b/tests/src/nodes/min_i8_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..4fc8ae5a6 --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_three_tensors/input_2.cairo b/tests/src/nodes/min_i8_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..f472bb1d6 --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_three_tensors/output_0.cairo b/tests/src/nodes/min_i8_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..9e4345995 --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_two_tensors.cairo b/tests/src/nodes/min_i8_broadcast_two_tensors.cairo new file mode 100644 index 000000000..a533c5930 --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i8_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_two_tensors/input_0.cairo b/tests/src/nodes/min_i8_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..563c07736 --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_two_tensors/input_1.cairo b/tests/src/nodes/min_i8_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..060e0a9ec --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_broadcast_two_tensors/output_0.cairo b/tests/src/nodes/min_i8_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..86ec9707b --- /dev/null +++ b/tests/src/nodes/min_i8_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_three_tensors.cairo b/tests/src/nodes/min_i8_three_tensors.cairo new file mode 100644 index 000000000..cee72024b --- /dev/null +++ b/tests/src/nodes/min_i8_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i8_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_three_tensors/input_0.cairo b/tests/src/nodes/min_i8_three_tensors/input_0.cairo new file mode 100644 index 000000000..92635527e --- /dev/null +++ b/tests/src/nodes/min_i8_three_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_three_tensors/input_1.cairo b/tests/src/nodes/min_i8_three_tensors/input_1.cairo new file mode 100644 index 000000000..09e7ab7d1 --- /dev/null +++ b/tests/src/nodes/min_i8_three_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_three_tensors/input_2.cairo b/tests/src/nodes/min_i8_three_tensors/input_2.cairo new file mode 100644 index 000000000..6784429c8 --- /dev/null +++ b/tests/src/nodes/min_i8_three_tensors/input_2.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_three_tensors/output_0.cairo b/tests/src/nodes/min_i8_three_tensors/output_0.cairo new file mode 100644 index 000000000..86ccc411f --- /dev/null +++ b/tests/src/nodes/min_i8_three_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + 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: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_two_tensors.cairo b/tests/src/nodes/min_i8_two_tensors.cairo new file mode 100644 index 000000000..8f13d360a --- /dev/null +++ b/tests/src/nodes/min_i8_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_i8_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_two_tensors/input_0.cairo b/tests/src/nodes/min_i8_two_tensors/input_0.cairo new file mode 100644 index 000000000..edcba1f05 --- /dev/null +++ b/tests/src/nodes/min_i8_two_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_two_tensors/input_1.cairo b/tests/src/nodes/min_i8_two_tensors/input_1.cairo new file mode 100644 index 000000000..805f2971a --- /dev/null +++ b/tests/src/nodes/min_i8_two_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_i8_two_tensors/output_0.cairo b/tests/src/nodes/min_i8_two_tensors/output_0.cairo new file mode 100644 index 000000000..4cfdc62b3 --- /dev/null +++ b/tests/src/nodes/min_i8_two_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + 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 }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_three_tensors.cairo b/tests/src/nodes/min_u32_broadcast_three_tensors.cairo new file mode 100644 index 000000000..b76ced612 --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_u32_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_three_tensors/input_0.cairo b/tests/src/nodes/min_u32_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..e1fde6172 --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,16 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(5); + data.append(5); + data.append(4); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_three_tensors/input_1.cairo b/tests/src/nodes/min_u32_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..4f6ed249f --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_three_tensors/input_2.cairo b/tests/src/nodes/min_u32_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..9b8b57621 --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,13 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_three_tensors/output_0.cairo b/tests/src/nodes/min_u32_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..5af544845 --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,16 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(3); + data.append(3); + data.append(3); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_two_tensors.cairo b/tests/src/nodes/min_u32_broadcast_two_tensors.cairo new file mode 100644 index 000000000..9fe666a9e --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_u32_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_two_tensors/input_0.cairo b/tests/src/nodes/min_u32_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..a173baaf1 --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,16 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(5); + data.append(4); + data.append(5); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_two_tensors/input_1.cairo b/tests/src/nodes/min_u32_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..447f95446 --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(5); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_broadcast_two_tensors/output_0.cairo b/tests/src/nodes/min_u32_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..51197a3e8 --- /dev/null +++ b/tests/src/nodes/min_u32_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,16 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(5); + data.append(3); + data.append(5); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_three_tensors.cairo b/tests/src/nodes/min_u32_three_tensors.cairo new file mode 100644 index 000000000..958888c62 --- /dev/null +++ b/tests/src/nodes/min_u32_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_u32_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_three_tensors/input_0.cairo b/tests/src/nodes/min_u32_three_tensors/input_0.cairo new file mode 100644 index 000000000..30bcab8cc --- /dev/null +++ b/tests/src/nodes/min_u32_three_tensors/input_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(4); + data.append(5); + data.append(1); + data.append(2); + data.append(4); + data.append(3); + data.append(1); + data.append(2); + data.append(3); + data.append(0); + data.append(0); + data.append(3); + data.append(3); + data.append(4); + data.append(1); + data.append(0); + data.append(5); + data.append(3); + data.append(4); + data.append(3); + data.append(0); + data.append(1); + data.append(3); + data.append(3); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_three_tensors/input_1.cairo b/tests/src/nodes/min_u32_three_tensors/input_1.cairo new file mode 100644 index 000000000..73f220714 --- /dev/null +++ b/tests/src/nodes/min_u32_three_tensors/input_1.cairo @@ -0,0 +1,40 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(3); + data.append(1); + data.append(5); + data.append(3); + data.append(4); + data.append(5); + data.append(2); + data.append(1); + data.append(5); + data.append(5); + data.append(2); + data.append(3); + data.append(3); + data.append(1); + data.append(3); + data.append(1); + data.append(4); + data.append(3); + data.append(2); + data.append(4); + data.append(2); + data.append(2); + data.append(0); + data.append(5); + data.append(5); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_three_tensors/input_2.cairo b/tests/src/nodes/min_u32_three_tensors/input_2.cairo new file mode 100644 index 000000000..1f682b4a7 --- /dev/null +++ b/tests/src/nodes/min_u32_three_tensors/input_2.cairo @@ -0,0 +1,40 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(5); + data.append(1); + data.append(3); + data.append(4); + data.append(3); + data.append(5); + data.append(2); + data.append(4); + data.append(2); + data.append(3); + data.append(3); + data.append(1); + data.append(4); + data.append(5); + data.append(2); + data.append(3); + data.append(0); + data.append(1); + data.append(3); + data.append(1); + data.append(3); + data.append(1); + data.append(0); + data.append(1); + data.append(0); + data.append(1); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_three_tensors/output_0.cairo b/tests/src/nodes/min_u32_three_tensors/output_0.cairo new file mode 100644 index 000000000..7a47c65c1 --- /dev/null +++ b/tests/src/nodes/min_u32_three_tensors/output_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(1); + data.append(4); + data.append(1); + data.append(2); + data.append(2); + data.append(2); + data.append(1); + data.append(2); + data.append(3); + data.append(0); + data.append(0); + data.append(3); + data.append(1); + data.append(3); + data.append(0); + data.append(0); + data.append(3); + data.append(1); + data.append(3); + data.append(1); + data.append(0); + data.append(0); + data.append(0); + data.append(1); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_two_tensors.cairo b/tests/src/nodes/min_u32_two_tensors.cairo new file mode 100644 index 000000000..94ce36d7a --- /dev/null +++ b/tests/src/nodes/min_u32_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_min_u32_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::min(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_two_tensors/input_0.cairo b/tests/src/nodes/min_u32_two_tensors/input_0.cairo new file mode 100644 index 000000000..c20d52a0a --- /dev/null +++ b/tests/src/nodes/min_u32_two_tensors/input_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(5); + data.append(5); + data.append(4); + data.append(1); + data.append(2); + data.append(1); + data.append(4); + data.append(2); + data.append(2); + data.append(3); + data.append(2); + data.append(0); + data.append(5); + data.append(3); + data.append(5); + data.append(4); + data.append(3); + data.append(0); + data.append(5); + data.append(3); + data.append(2); + data.append(5); + data.append(5); + data.append(4); + data.append(4); + data.append(3); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_two_tensors/input_1.cairo b/tests/src/nodes/min_u32_two_tensors/input_1.cairo new file mode 100644 index 000000000..d4709f0df --- /dev/null +++ b/tests/src/nodes/min_u32_two_tensors/input_1.cairo @@ -0,0 +1,40 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(2); + data.append(2); + data.append(4); + data.append(1); + data.append(3); + data.append(3); + data.append(0); + data.append(5); + data.append(1); + data.append(5); + data.append(4); + data.append(1); + data.append(2); + data.append(5); + data.append(5); + data.append(5); + data.append(5); + data.append(2); + data.append(1); + data.append(2); + data.append(3); + data.append(5); + data.append(0); + data.append(4); + data.append(3); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/src/nodes/min_u32_two_tensors/output_0.cairo b/tests/src/nodes/min_u32_two_tensors/output_0.cairo new file mode 100644 index 000000000..fc5f86d90 --- /dev/null +++ b/tests/src/nodes/min_u32_two_tensors/output_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(2); + data.append(2); + data.append(1); + data.append(1); + data.append(1); + data.append(3); + data.append(0); + data.append(2); + data.append(1); + data.append(2); + data.append(0); + data.append(1); + data.append(2); + data.append(5); + data.append(4); + data.append(3); + data.append(0); + data.append(2); + data.append(1); + data.append(2); + data.append(3); + data.append(5); + data.append(0); + data.append(4); + data.append(3); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file From 15cf55c519f44b7cd604bfe5d3fa1e1e3dccde39 Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Fri, 27 Oct 2023 12:18:54 +0200 Subject: [PATCH 04/21] Add docs --- docs/SUMMARY.md | 1 + docs/framework/compatibility.md | 1 + docs/framework/operators/tensor/tensor.min.md | 63 ++++++++++++++++++ src/operators/tensor/core.cairo | 64 +++++++++++++++++++ src/operators/tensor/math/min.cairo | 2 +- 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 docs/framework/operators/tensor/tensor.min.md diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 69deeada8..8efe95845 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -41,6 +41,7 @@ * [tensor.new](framework/operators/tensor/tensor.new.md) * [tensor.at](framework/operators/tensor/tensor.at.md) * [tensor.min_in_tensor](framework/operators/tensor/tensor.min_in_tensor.md) + * [tensor.min](framework/operators/tensor/tensor.min.md) * [tensor.max](framework/operators/tensor/tensor.max.md) * [tensor.stride](framework/operators/tensor/tensor.stride.md) * [tensor.ravel\_index](framework/operators/tensor/tensor.ravel\_index.md) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index 8f8b9ba20..0e81e0b60 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -67,5 +67,6 @@ You can see below the list of current supported ONNX Operators: | [Or](operators/tensor/tensor.or.md) | :white\_check\_mark: | | [Gemm](operators/neural-network/nn.gemm.md) | :white\_check\_mark: | | [MinInTensor](operators/tensor/tensor.min\_in\_tensor.md) | :white\_check\_mark: | +| [Min](operators/tensor/tensor.min.md) | :white\_check\_mark: | Current Operators support: **60/156 (38%)** diff --git a/docs/framework/operators/tensor/tensor.min.md b/docs/framework/operators/tensor/tensor.min.md new file mode 100644 index 000000000..50c45e8ad --- /dev/null +++ b/docs/framework/operators/tensor/tensor.min.md @@ -0,0 +1,63 @@ +# tensor.min + +```rust + fn min(tensors: Span>) -> Tensor; +``` + +Returns the element-wise minumum values from a list of input tensors +The input tensors must have either: +* Exactly the same shape +* The same number of dimensions and the length of each dimension is either a common length or 1. + +## Args + +* `tensors`(` Span>,`) - Array of the input tensors + +## Returns + +A new `Tensor` containing the element-wise minimum values + +## Panics + +* Panics if the shapes are not equal or broadcastable +* Panics if tensor array length is not >= 1 + +## Examples + +Case 1: Process tensors with same shape + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + +fn min_example() -> Tensor { + 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![0, 3, 1, 2].span(),); + let result = TensorTrait::min(tensors: array![tensor1, tensor2].span()); + return result; +} +>>> [0, 1, 1, 2] + + result.shape +>>> (2, 2) +``` + +Case 2: Process tensors with different shapes + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + +fn min_example() -> Tensor { + let tensor1 = TensorTrait::new(shape: array![2, 2].span(), data: array![0, 1, 2, 3].span(),); + let tensor2 = TensorTrait::new(shape: array![1, 2].span(), data: array![1, 4].span(),); + let result = TensorTrait::min(tensors: array![tensor1, tensor2].span()); + return result; +} +>>> [0, 1, 1, 4] + + result.shape +>>> (2, 2) +``` diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index d31dfee54..5abd71bf9 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -219,6 +219,70 @@ trait TensorTrait { /// ``` /// fn min_in_tensor(self: @Tensor) -> T; + /// # tensor.min + /// + /// ```rust + /// fn min(tensors: Span>) -> Tensor; + /// ``` + /// + /// Returns the element-wise minumum values from a list of input tensors + /// The input tensors must have either: + /// * Exactly the same shape + /// * The same number of dimensions and the length of each dimension is either a common length or 1. + /// + /// ## Args + /// + /// * `tensors`(` Span>,`) - Array of the input tensors + /// + /// ## Returns + /// + /// A new `Tensor` containing the element-wise minimum values + /// + /// ## Panics + /// + /// * Panics if tensor array is empty + /// * Panics if the shapes are not equal or broadcastable + /// + /// ## Examples + /// + /// Case 1: Process tensors with same shape + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn min_example() -> Tensor { + /// 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![0, 3, 1, 2].span(),); + /// let result = TensorTrait::min(tensors: array![tensor1, tensor2].span()); + /// return result; + /// } + /// >>> [0, 1, 1, 2] + /// + /// result.shape + /// >>> (2, 2) + /// ``` + /// + /// Case 2: Process tensors with different shapes + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn min_example() -> Tensor { + /// let tensor1 = TensorTrait::new(shape: array![2, 2].span(), data: array![0, 1, 2, 3].span(),); + /// let tensor2 = TensorTrait::new(shape: array![1, 2].span(), data: array![1, 4].span(),); + /// let result = TensorTrait::min(tensors: array![tensor1, tensor2].span()); + /// return result; + /// } + /// >>> [0, 1, 1, 4] + /// + /// result.shape + /// >>> (2, 2) + /// ``` + /// fn min(tensors: Span>) -> Tensor; /// # tensor.max /// diff --git a/src/operators/tensor/math/min.cairo b/src/operators/tensor/math/min.cairo index b3f90b965..3577e69c0 100644 --- a/src/operators/tensor/math/min.cairo +++ b/src/operators/tensor/math/min.cairo @@ -15,7 +15,7 @@ fn min< impl TCopy: Copy, impl TDrop: Drop >( - mut tensors: Span> + tensors: Span> ) -> Tensor { assert(tensors.len() >= 1, 'Input tensors must be >= 1'); From 70402c5e7f24dbeea6126713cc727e13761f0bec Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Fri, 27 Oct 2023 14:35:29 +0200 Subject: [PATCH 05/21] Rename old max operator & adapt repo --- docs/SUMMARY.md | 2 +- docs/framework/compatibility.md | 1 + docs/framework/operators/tensor/README.md | 2 +- .../{tensor.max.md => tensor.max_in_tensor.md} | 10 +++++----- src/operators/tensor/core.cairo | 14 +++++++------- .../tensor/implementations/tensor_fp16x16.cairo | 4 ++-- .../implementations/tensor_fp16x16wide.cairo | 4 ++-- .../tensor/implementations/tensor_fp32x32.cairo | 4 ++-- .../tensor/implementations/tensor_fp64x64.cairo | 4 ++-- .../tensor/implementations/tensor_fp8x23.cairo | 4 ++-- .../tensor/implementations/tensor_fp8x23wide.cairo | 4 ++-- .../tensor/implementations/tensor_i32.cairo | 4 ++-- .../tensor/implementations/tensor_i8.cairo | 4 ++-- .../tensor/implementations/tensor_u32.cairo | 4 ++-- src/operators/tensor/math.cairo | 2 +- .../tensor/math/{max.cairo => max_in_tensor.cairo} | 2 +- .../max/max_fp_test/max_fp16x16_test.cairo | 6 +++--- .../max/max_fp_test/max_fp8x23_test.cairo | 6 +++--- tests/tensor_core/max/max_i32_test.cairo | 6 +++--- tests/tensor_core/max/max_u32_test.cairo | 6 +++--- 20 files changed, 47 insertions(+), 46 deletions(-) rename docs/framework/operators/tensor/{tensor.max.md => tensor.max_in_tensor.md} (68%) rename src/operators/tensor/math/{max.cairo => max_in_tensor.cairo} (93%) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 409c1715e..9189b06c2 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -41,7 +41,7 @@ * [tensor.new](framework/operators/tensor/tensor.new.md) * [tensor.at](framework/operators/tensor/tensor.at.md) * [tensor.min](framework/operators/tensor/tensor.min.md) - * [tensor.max](framework/operators/tensor/tensor.max.md) + * [tensor.max\_in\_tensor](framework/operators/tensor/tensor.max\_in\_tensor.md) * [tensor.stride](framework/operators/tensor/tensor.stride.md) * [tensor.ravel\_index](framework/operators/tensor/tensor.ravel\_index.md) * [tensor.unravel\_index](framework/operators/tensor/tensor.unravel\_index.md) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index 29d537ff6..710191606 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -67,5 +67,6 @@ You can see below the list of current supported ONNX Operators: | [Or](operators/tensor/tensor.or.md) | :white\_check\_mark: | | [Gemm](operators/neural-network/nn.gemm.md) | :white\_check\_mark: | | [Where](operators/tensor/tensor.where.md) | :white\_check\_mark: | +| [MaxInTensor](operators/tensor/tensor.max\_in\_tensor.md) | :white\_check\_mark: | Current Operators support: **61/156 (39%)** diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index 361401570..d92d13560 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -51,7 +51,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.stride`](tensor.stride.md) | Computes the stride of each dimension in the tensor. | | [`tensor.onehot`](tensor.onehot.md) | Produces one-hot tensor based on input. | | [`tensor.min`](tensor.min.md) | Returns the minimum value in the tensor. | -| [`tensor.max`](tensor.max.md) | Returns the maximum value in the tensor. | +| [`tensor.max_in_tensor`](tensor.max\_in\_tensor.md) | Returns the maximum value in the tensor. | | [`tensor.reduce_sum`](tensor.reduce\_sum.md) | Reduces a tensor by summing its elements along a specified axis. | | [`tensor.argmax`](tensor.argmax.md) | Returns the index of the maximum value along the specified axis. | | [`tensor.argmin`](tensor.argmin.md) | Returns the index of the minimum value along the specified axis. | diff --git a/docs/framework/operators/tensor/tensor.max.md b/docs/framework/operators/tensor/tensor.max_in_tensor.md similarity index 68% rename from docs/framework/operators/tensor/tensor.max.md rename to docs/framework/operators/tensor/tensor.max_in_tensor.md index e115cc559..08ac0f82f 100644 --- a/docs/framework/operators/tensor/tensor.max.md +++ b/docs/framework/operators/tensor/tensor.max_in_tensor.md @@ -1,7 +1,7 @@ -# tensor.max +# tensor.max_in_tensor ```rust - fn max(self: @Tensor) -> T; + fn max_in_tensor(self: @Tensor) -> T; ``` Returns the maximum value in the tensor. @@ -21,13 +21,13 @@ use array::{ArrayTrait, SpanTrait}; use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; -fn max_example() -> u32 { +fn max_in_tensor_example() -> u32 { let tensor = TensorTrait::new( shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), ); - // We can call `max` function as follows. - return tensor.max(); + // We can call `max_in_tensor` function as follows. + return tensor.max_in_tensor(); } >>> 7 ``` diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 94a9baac7..3e236d6ed 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -47,7 +47,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde { /// ``` /// fn min(self: @Tensor) -> T; - /// # tensor.max + /// # tensor.max_in_tensor /// /// ```rust - /// fn max(self: @Tensor) -> T; + /// fn max_in_tensor(self: @Tensor) -> T; /// ``` /// /// Returns the maximum value in the tensor. @@ -243,18 +243,18 @@ trait TensorTrait { /// /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; /// - /// fn max_example() -> u32 { + /// fn max_in_tensor_example() -> u32 { /// let tensor = TensorTrait::new( /// shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), /// ); /// - /// // We can call `max` function as follows. - /// return tensor.max(); + /// // We can call `max_in_tensor` function as follows. + /// return tensor.max_in_tensor(); /// } /// >>> 7 /// ``` /// - fn max(self: @Tensor) -> T; + fn max_in_tensor(self: @Tensor) -> T; /// # tensor.stride /// /// ```rust diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index f726c8d1e..323a6de26 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -24,8 +24,8 @@ impl FP16x16Tensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> FP16x16 { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> FP16x16 { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index c4f2de169..584ce0ed7 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -24,8 +24,8 @@ impl FP16x16WTensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> FP16x16W { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> FP16x16W { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 5f78a86f6..cb9910a71 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -25,8 +25,8 @@ impl FP32x32Tensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> FP32x32 { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> FP32x32 { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index d9e540293..3859b71dc 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -25,8 +25,8 @@ impl FP64x64Tensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> FP64x64 { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> FP64x64 { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 153f179ef..13f6e16b8 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -24,8 +24,8 @@ impl FP8x23Tensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> FP8x23 { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> FP8x23 { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index f594eec4d..d976c8288 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -24,8 +24,8 @@ impl FP8x23WTensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> FP8x23W { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> FP8x23W { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 057fb6853..289378c23 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -25,8 +25,8 @@ impl I32Tensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> i32 { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> i32 { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index ae2662f0d..08fef58a0 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -24,8 +24,8 @@ impl I8Tensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> i8 { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> i8 { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index fc83d35c3..719a7ff27 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -24,8 +24,8 @@ impl U32Tensor of TensorTrait { math::min::min_in_tensor::(*self.data) } - fn max(self: @Tensor) -> u32 { - math::max::max_in_tensor(*self.data) + fn max_in_tensor(self: @Tensor) -> u32 { + math::max_in_tensor::max_in_tensor(*self.data) } fn stride(self: @Tensor) -> Span { diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index d8c5a56f4..232218a76 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -1,5 +1,5 @@ mod min; -mod max; +mod max_in_tensor; mod reduce_sum; mod argmax; mod argmin; diff --git a/src/operators/tensor/math/max.cairo b/src/operators/tensor/math/max_in_tensor.cairo similarity index 93% rename from src/operators/tensor/math/max.cairo rename to src/operators/tensor/math/max_in_tensor.cairo index 15580c7d7..1abaf5545 100644 --- a/src/operators/tensor/math/max.cairo +++ b/src/operators/tensor/math/max_in_tensor.cairo @@ -3,7 +3,7 @@ use option::OptionTrait; use orion::numbers::NumberTrait; -/// Cf: TensorTrait::max docstring +/// Cf: TensorTrait::max_in_tensor docstring fn max_in_tensor< T, MAG, diff --git a/tests/tensor_core/max/max_fp_test/max_fp16x16_test.cairo b/tests/tensor_core/max/max_fp_test/max_fp16x16_test.cairo index 160fb29b7..8458d12fd 100644 --- a/tests/tensor_core/max/max_fp_test/max_fp16x16_test.cairo +++ b/tests/tensor_core/max/max_fp_test/max_fp16x16_test.cairo @@ -13,7 +13,7 @@ mod tensor_1D { fn tensor_max() { let tensor = fp_tensor_1x3_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == FixedTrait::::new_unscaled(2, false).mag, 'tensor.max = 2'); } } @@ -34,7 +34,7 @@ mod tensor_2D { fn tensor_max() { let tensor = fp_tensor_2x2_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == FixedTrait::::new_unscaled(3, false).mag, 'tensor.max = 3'); } } @@ -55,7 +55,7 @@ mod tensor_3D { fn tensor_max() { let tensor = fp_tensor_2x2x2_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == FixedTrait::::new_unscaled(7, false).mag, 'tensor.max = 7'); } } diff --git a/tests/tensor_core/max/max_fp_test/max_fp8x23_test.cairo b/tests/tensor_core/max/max_fp_test/max_fp8x23_test.cairo index 2ff1b744c..f9facb1fc 100644 --- a/tests/tensor_core/max/max_fp_test/max_fp8x23_test.cairo +++ b/tests/tensor_core/max/max_fp_test/max_fp8x23_test.cairo @@ -13,7 +13,7 @@ mod tensor_1D { fn tensor_max() { let tensor = fp_tensor_1x3_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == FixedTrait::::new_unscaled(2, false).mag, 'tensor.max = 2'); } } @@ -34,7 +34,7 @@ mod tensor_2D { fn tensor_max() { let tensor = fp_tensor_2x2_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == FixedTrait::::new_unscaled(3, false).mag, 'tensor.max = 3'); } } @@ -55,7 +55,7 @@ mod tensor_3D { fn tensor_max() { let tensor = fp_tensor_2x2x2_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == FixedTrait::::new_unscaled(7, false).mag, 'tensor.max = 7'); } } diff --git a/tests/tensor_core/max/max_i32_test.cairo b/tests/tensor_core/max/max_i32_test.cairo index 3e67df42c..17f9fdc9b 100644 --- a/tests/tensor_core/max/max_i32_test.cairo +++ b/tests/tensor_core/max/max_i32_test.cairo @@ -11,7 +11,7 @@ mod tensor_1D { fn tensor_max() { let tensor = i32_tensor_1x3_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == 2, 'tensor.max = 2'); } } @@ -29,7 +29,7 @@ mod tensor_2D { fn tensor_max() { let tensor = i32_tensor_2x2_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == 3, 'tensor.max = 3'); } } @@ -47,7 +47,7 @@ mod tensor_3D { fn tensor_max() { let tensor = i32_tensor_2x2x2_helper(); - let result = tensor.max().mag; + let result = tensor.max_in_tensor().mag; assert(result == 7, 'tensor.max = 7'); } } diff --git a/tests/tensor_core/max/max_u32_test.cairo b/tests/tensor_core/max/max_u32_test.cairo index a3ee99018..ddbb50280 100644 --- a/tests/tensor_core/max/max_u32_test.cairo +++ b/tests/tensor_core/max/max_u32_test.cairo @@ -11,7 +11,7 @@ mod tensor_1D { fn tensor_max() { let tensor = u32_tensor_1x3_helper(); - let result = tensor.max(); + let result = tensor.max_in_tensor(); assert(result == 2, 'tensor.max = 2'); } } @@ -29,7 +29,7 @@ mod tensor_2D { fn tensor_max() { let tensor = u32_tensor_2x2_helper(); - let result = tensor.max(); + let result = tensor.max_in_tensor(); assert(result == 3, 'tensor.max = 3'); } } @@ -47,7 +47,7 @@ mod tensor_3D { fn tensor_max() { let tensor = u32_tensor_2x2x2_helper(); - let result = tensor.max(); + let result = tensor.max_in_tensor(); assert(result == 7, 'tensor.max = 7'); } } From d45c381435f7e3defd166fa356b3b13d346095df Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Fri, 27 Oct 2023 15:03:09 +0200 Subject: [PATCH 06/21] Implement ONNX max operator --- src/operators/tensor/core.cairo | 1 + .../implementations/tensor_fp16x16.cairo | 4 ++ .../implementations/tensor_fp16x16wide.cairo | 4 ++ .../implementations/tensor_fp32x32.cairo | 4 ++ .../implementations/tensor_fp64x64.cairo | 4 ++ .../implementations/tensor_fp8x23.cairo | 4 ++ .../implementations/tensor_fp8x23wide.cairo | 4 ++ .../tensor/implementations/tensor_i32.cairo | 4 ++ .../tensor/implementations/tensor_i8.cairo | 4 ++ .../tensor/implementations/tensor_u32.cairo | 4 ++ src/operators/tensor/math.cairo | 1 + src/operators/tensor/math/max.cairo | 68 +++++++++++++++++++ 12 files changed, 106 insertions(+) create mode 100644 src/operators/tensor/math/max.cairo diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 3e236d6ed..f911e3f43 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -255,6 +255,7 @@ trait TensorTrait { /// ``` /// fn max_in_tensor(self: @Tensor) -> T; + fn max(tensors: Span>) -> Tensor; /// # tensor.stride /// /// ```rust diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index 323a6de26..8414ef75b 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -28,6 +28,10 @@ impl FP16x16Tensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 584ce0ed7..112c823e4 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -28,6 +28,10 @@ impl FP16x16WTensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index cb9910a71..c7f830e1d 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -29,6 +29,10 @@ impl FP32x32Tensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index 3859b71dc..11baa829d 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -29,6 +29,10 @@ impl FP64x64Tensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 13f6e16b8..09879f011 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -28,6 +28,10 @@ impl FP8x23Tensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index d976c8288..6a7de84f8 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -28,6 +28,10 @@ impl FP8x23WTensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 289378c23..e1eaa1cfe 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -29,6 +29,10 @@ impl I32Tensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index 08fef58a0..c7cd836ea 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -28,6 +28,10 @@ impl I8Tensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 719a7ff27..f5e684f0e 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -28,6 +28,10 @@ impl U32Tensor of TensorTrait { math::max_in_tensor::max_in_tensor(*self.data) } + fn max(tensors: Span>) -> Tensor { + math::max::max(tensors) + } + fn stride(self: @Tensor) -> Span { stride(*self.shape) } diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index 232218a76..dddd2e176 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -1,5 +1,6 @@ mod min; mod max_in_tensor; +mod max; mod reduce_sum; mod argmax; mod argmin; diff --git a/src/operators/tensor/math/max.cairo b/src/operators/tensor/math/max.cairo new file mode 100644 index 000000000..5ed703408 --- /dev/null +++ b/src/operators/tensor/math/max.cairo @@ -0,0 +1,68 @@ +use array::{ArrayTrait, SpanTrait}; + +use orion::numbers::NumberTrait; +use orion::operators::tensor::core::{Tensor, TensorTrait, unravel_index}; +use orion::operators::tensor::helpers::{ + broadcast_shape, broadcast_index_mapping, len_from_shape, check_compatibility +}; + +/// Cf: TensorTrait::max docstring +fn max< + T, + MAG, + impl TTensorTrait: TensorTrait, + impl TNumber: NumberTrait, + impl TCopy: Copy, + impl TDrop: Drop +>( + tensors: Span> +) -> Tensor { + + assert(tensors.len() >= 1, 'Input tensors must be >= 1'); + + let first_tensor = *tensors.at(0); + + if tensors.len() == 1 { + return first_tensor; + } + + let mut max_shape: Span = first_tensor.shape; + let mut max_data: Span = first_tensor.data; + + let mut tensor_counter: usize = 1; + + loop { + if tensor_counter > tensors.len() - 1 { + break; + } + + let mut new_max_data = ArrayTrait::::new(); + + let mut current_tensor = *tensors.at(tensor_counter); + + let mut broadcasted_shape = broadcast_shape(max_shape, current_tensor.shape); + + let num_elements = len_from_shape(broadcasted_shape); + let mut n: usize = 0; + loop { + let mut indices_broadcasted = unravel_index(n, broadcasted_shape); + + let mut indices_self = broadcast_index_mapping(max_shape, indices_broadcasted); + let mut indices_other = broadcast_index_mapping(current_tensor.shape, indices_broadcasted); + + let mut max_value = NumberTrait::max(*(max_data)[indices_self], *(current_tensor.data)[indices_other]); + new_max_data.append(max_value); + + n += 1; + if n == num_elements { + break (); + }; + }; + + max_shape = broadcasted_shape; + max_data = new_max_data.span(); + tensor_counter += 1; + }; + + return TensorTrait::::new(max_shape, max_data); +} \ No newline at end of file From 7464c08d9b3c5184071d12a282c71023977672c7 Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Fri, 27 Oct 2023 15:46:01 +0200 Subject: [PATCH 07/21] Add tests --- nodegen/node/max.py | 364 ++++++++++++++++++ tests/nodes.cairo | 21 +- .../max_fp16x16_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../input_2.cairo | 15 + .../output_0.cairo | 18 + .../max_fp16x16_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../output_0.cairo | 18 + tests/nodes/max_fp16x16_three_tensors.cairo | 24 ++ .../max_fp16x16_three_tensors/input_0.cairo | 42 ++ .../max_fp16x16_three_tensors/input_1.cairo | 42 ++ .../max_fp16x16_three_tensors/input_2.cairo | 42 ++ .../max_fp16x16_three_tensors/output_0.cairo | 42 ++ tests/nodes/max_fp16x16_two_tensors.cairo | 22 ++ .../max_fp16x16_two_tensors/input_0.cairo | 42 ++ .../max_fp16x16_two_tensors/input_1.cairo | 42 ++ .../max_fp16x16_two_tensors/output_0.cairo | 42 ++ .../max_fp8x23_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../input_2.cairo | 15 + .../output_0.cairo | 18 + .../max_fp8x23_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 18 + .../input_1.cairo | 16 + .../output_0.cairo | 18 + tests/nodes/max_fp8x23_three_tensors.cairo | 24 ++ .../max_fp8x23_three_tensors/input_0.cairo | 42 ++ .../max_fp8x23_three_tensors/input_1.cairo | 42 ++ .../max_fp8x23_three_tensors/input_2.cairo | 42 ++ .../max_fp8x23_three_tensors/output_0.cairo | 42 ++ tests/nodes/max_fp8x23_two_tensors.cairo | 22 ++ .../max_fp8x23_two_tensors/input_0.cairo | 42 ++ .../max_fp8x23_two_tensors/input_1.cairo | 42 ++ .../max_fp8x23_two_tensors/output_0.cairo | 42 ++ .../max_i32_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../input_2.cairo | 14 + .../output_0.cairo | 17 + .../nodes/max_i32_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../output_0.cairo | 17 + tests/nodes/max_i32_three_tensors.cairo | 24 ++ .../nodes/max_i32_three_tensors/input_0.cairo | 41 ++ .../nodes/max_i32_three_tensors/input_1.cairo | 41 ++ .../nodes/max_i32_three_tensors/input_2.cairo | 41 ++ .../max_i32_three_tensors/output_0.cairo | 41 ++ tests/nodes/max_i32_two_tensors.cairo | 22 ++ tests/nodes/max_i32_two_tensors/input_0.cairo | 41 ++ tests/nodes/max_i32_two_tensors/input_1.cairo | 41 ++ .../nodes/max_i32_two_tensors/output_0.cairo | 41 ++ .../max_i8_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../input_2.cairo | 14 + .../output_0.cairo | 17 + .../nodes/max_i8_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 17 + .../input_1.cairo | 15 + .../output_0.cairo | 17 + tests/nodes/max_i8_three_tensors.cairo | 24 ++ .../nodes/max_i8_three_tensors/input_0.cairo | 41 ++ .../nodes/max_i8_three_tensors/input_1.cairo | 41 ++ .../nodes/max_i8_three_tensors/input_2.cairo | 41 ++ .../nodes/max_i8_three_tensors/output_0.cairo | 41 ++ tests/nodes/max_i8_two_tensors.cairo | 22 ++ tests/nodes/max_i8_two_tensors/input_0.cairo | 41 ++ tests/nodes/max_i8_two_tensors/input_1.cairo | 41 ++ tests/nodes/max_i8_two_tensors/output_0.cairo | 41 ++ .../max_u32_broadcast_three_tensors.cairo | 24 ++ .../input_0.cairo | 16 + .../input_1.cairo | 14 + .../input_2.cairo | 13 + .../output_0.cairo | 16 + .../nodes/max_u32_broadcast_two_tensors.cairo | 22 ++ .../input_0.cairo | 16 + .../input_1.cairo | 14 + .../output_0.cairo | 16 + tests/nodes/max_u32_three_tensors.cairo | 24 ++ .../nodes/max_u32_three_tensors/input_0.cairo | 40 ++ .../nodes/max_u32_three_tensors/input_1.cairo | 40 ++ .../nodes/max_u32_three_tensors/input_2.cairo | 40 ++ .../max_u32_three_tensors/output_0.cairo | 40 ++ tests/nodes/max_u32_two_tensors.cairo | 22 ++ tests/nodes/max_u32_two_tensors/input_0.cairo | 40 ++ tests/nodes/max_u32_two_tensors/input_1.cairo | 40 ++ .../nodes/max_u32_two_tensors/output_0.cairo | 40 ++ 92 files changed, 2853 insertions(+), 1 deletion(-) create mode 100644 nodegen/node/max.py create mode 100644 tests/nodes/max_fp16x16_broadcast_three_tensors.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_two_tensors.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp16x16_broadcast_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_fp16x16_three_tensors.cairo create mode 100644 tests/nodes/max_fp16x16_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp16x16_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp16x16_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_fp16x16_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_fp16x16_two_tensors.cairo create mode 100644 tests/nodes/max_fp16x16_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp16x16_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp16x16_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_three_tensors.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_two_tensors.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp8x23_broadcast_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_fp8x23_three_tensors.cairo create mode 100644 tests/nodes/max_fp8x23_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp8x23_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp8x23_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_fp8x23_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_fp8x23_two_tensors.cairo create mode 100644 tests/nodes/max_fp8x23_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_fp8x23_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_fp8x23_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_i32_broadcast_three_tensors.cairo create mode 100644 tests/nodes/max_i32_broadcast_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_i32_broadcast_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_i32_broadcast_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_i32_broadcast_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_i32_broadcast_two_tensors.cairo create mode 100644 tests/nodes/max_i32_broadcast_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_i32_broadcast_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_i32_broadcast_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_i32_three_tensors.cairo create mode 100644 tests/nodes/max_i32_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_i32_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_i32_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_i32_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_i32_two_tensors.cairo create mode 100644 tests/nodes/max_i32_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_i32_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_i32_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_i8_broadcast_three_tensors.cairo create mode 100644 tests/nodes/max_i8_broadcast_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_i8_broadcast_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_i8_broadcast_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_i8_broadcast_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_i8_broadcast_two_tensors.cairo create mode 100644 tests/nodes/max_i8_broadcast_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_i8_broadcast_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_i8_broadcast_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_i8_three_tensors.cairo create mode 100644 tests/nodes/max_i8_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_i8_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_i8_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_i8_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_i8_two_tensors.cairo create mode 100644 tests/nodes/max_i8_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_i8_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_i8_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_u32_broadcast_three_tensors.cairo create mode 100644 tests/nodes/max_u32_broadcast_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_u32_broadcast_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_u32_broadcast_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_u32_broadcast_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_u32_broadcast_two_tensors.cairo create mode 100644 tests/nodes/max_u32_broadcast_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_u32_broadcast_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_u32_broadcast_two_tensors/output_0.cairo create mode 100644 tests/nodes/max_u32_three_tensors.cairo create mode 100644 tests/nodes/max_u32_three_tensors/input_0.cairo create mode 100644 tests/nodes/max_u32_three_tensors/input_1.cairo create mode 100644 tests/nodes/max_u32_three_tensors/input_2.cairo create mode 100644 tests/nodes/max_u32_three_tensors/output_0.cairo create mode 100644 tests/nodes/max_u32_two_tensors.cairo create mode 100644 tests/nodes/max_u32_two_tensors/input_0.cairo create mode 100644 tests/nodes/max_u32_two_tensors/input_1.cairo create mode 100644 tests/nodes/max_u32_two_tensors/output_0.cairo diff --git a/nodegen/node/max.py b/nodegen/node/max.py new file mode 100644 index 000000000..b1079a30c --- /dev/null +++ b/nodegen/node/max.py @@ -0,0 +1,364 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl, Trait + +class Max(RunAll): + + @staticmethod + def max_u32_two_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + z = np.maximum(x, y) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + + name = "max_u32_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.uint32) + y = np.random.randint(0, 6, (1, 2)).astype(np.uint32) + z = np.maximum(x, y) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + + name = "max_u32_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def max_i32_two_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + z = np.maximum(x, y) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + + name = "max_i32_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int32) + y = np.random.randint(0, 6, (1, 2)).astype(np.int32) + z = np.maximum(x, y) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + + name = "max_i32_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def max_i8_two_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + z = np.maximum(x, y) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + + name = "max_i8_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int8) + y = np.random.randint(0, 6, (1, 2)).astype(np.int8) + z = np.maximum(x, y) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + + name = "max_i8_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def max_fp8x23_two_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.maximum(x, y) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + + name = "max_fp8x23_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.maximum(x, y) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + + name = "max_fp8x23_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + default() + broadcast() + + @staticmethod + def max_fp16x16_two_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.maximum(x, y) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + + name = "max_fp16x16_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.maximum(x, y) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + + name = "max_fp16x16_broadcast_two_tensors" + make_node([x, y], [z], name) + make_test([x, y], z, "TensorTrait::max(array![input_0, input_1].span());", name) + + default() + broadcast() + + + @staticmethod + def max_u32_three_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + z = np.random.randint(0, 6, (3, 3, 3)).astype(np.uint32) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + m = Tensor(Dtype.U32, m.shape, m.flatten()) + + name = "max_u32_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.uint32) + y = np.random.randint(0, 6, (1, 2)).astype(np.uint32) + z = np.random.randint(0, 6, (1, 1)).astype(np.uint32) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + z = Tensor(Dtype.U32, z.shape, z.flatten()) + m = Tensor(Dtype.U32, m.shape, m.flatten()) + + name = "max_u32_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def max_i32_three_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + z = np.random.randint(0, 6, (3, 3, 3)).astype(np.int32) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + m = Tensor(Dtype.I32, m.shape, m.flatten()) + + name = "max_i32_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int32) + y = np.random.randint(0, 6, (1, 2)).astype(np.int32) + z = np.random.randint(0, 6, (1, 1)).astype(np.int32) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + z = Tensor(Dtype.I32, z.shape, z.flatten()) + m = Tensor(Dtype.I32, m.shape, m.flatten()) + + name = "max_i32_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def max_i8_three_tensors(): + def default(): + x = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + y = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + z = np.random.randint(0, 6, (3, 3, 3)).astype(np.int8) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + m = Tensor(Dtype.I8, m.shape, m.flatten()) + + name = "max_i8_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(0, 6, (2, 2)).astype(np.int8) + y = np.random.randint(0, 6, (1, 2)).astype(np.int8) + z = np.random.randint(0, 6, (1, 1)).astype(np.int8) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + z = Tensor(Dtype.I8, z.shape, z.flatten()) + m = Tensor(Dtype.I8, m.shape, m.flatten()) + + name = "max_i8_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def max_fp8x23_three_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + m = Tensor(Dtype.FP8x23, m.shape, to_fp( + m.flatten(), FixedImpl.FP8x23)) + + name = "max_fp8x23_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.random.randint(-3, 3, (1, 1)).astype(np.float64) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + z = Tensor(Dtype.FP8x23, z.shape, to_fp( + z.flatten(), FixedImpl.FP8x23)) + m = Tensor(Dtype.FP8x23, m.shape, to_fp( + m.flatten(), FixedImpl.FP8x23)) + + name = "max_fp8x23_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() + + @staticmethod + def max_fp16x16_three_tensors(): + def default(): + x = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + y = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + z = np.random.randint(-3, 3, (3, 3, 3)).astype(np.float64) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + m = Tensor(Dtype.FP16x16, m.shape, to_fp( + m.flatten(), FixedImpl.FP16x16)) + + name = "max_fp16x16_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + def broadcast(): + x = np.random.randint(-3, 3, (2, 2)).astype(np.float64) + y = np.random.randint(-3, 3, (1, 2)).astype(np.float64) + z = np.random.randint(-3, 3, (1, 1)).astype(np.float64) + m = np.maximum(np.maximum(x, y), z) + + x = Tensor(Dtype.FP16x16, x.shape, to_fp( + x.flatten(), FixedImpl.FP16x16)) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + z = Tensor(Dtype.FP16x16, z.shape, to_fp( + z.flatten(), FixedImpl.FP16x16)) + m = Tensor(Dtype.FP16x16, m.shape, to_fp( + m.flatten(), FixedImpl.FP16x16)) + + name = "max_fp16x16_broadcast_three_tensors" + make_node([x, y, z], [m], name) + make_test([x, y, z], m, "TensorTrait::max(array![input_0, input_1, input_2].span());", name) + + default() + broadcast() \ No newline at end of file diff --git a/tests/nodes.cairo b/tests/nodes.cairo index d867b2277..2cf13d5f1 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -474,4 +474,23 @@ mod where_i8; mod where_i8_broadcast; mod where_u32; mod where_u32_broadcast; - +mod max_fp16x16_three_tensors; +mod max_fp16x16_broadcast_three_tensors; +mod max_fp16x16_two_tensors; +mod max_fp16x16_broadcast_two_tensors; +mod max_fp8x23_three_tensors; +mod max_fp8x23_broadcast_three_tensors; +mod max_fp8x23_two_tensors; +mod max_fp8x23_broadcast_two_tensors; +mod max_i32_three_tensors; +mod max_i32_broadcast_three_tensors; +mod max_i32_two_tensors; +mod max_i32_broadcast_two_tensors; +mod max_i8_three_tensors; +mod max_i8_broadcast_three_tensors; +mod max_i8_two_tensors; +mod max_i8_broadcast_two_tensors; +mod max_u32_three_tensors; +mod max_u32_broadcast_three_tensors; +mod max_u32_two_tensors; +mod max_u32_broadcast_two_tensors; diff --git a/tests/nodes/max_fp16x16_broadcast_three_tensors.cairo b/tests/nodes/max_fp16x16_broadcast_three_tensors.cairo new file mode 100644 index 000000000..2660e4676 --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp16x16_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_three_tensors/input_0.cairo b/tests/nodes/max_fp16x16_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..fc8d87773 --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_three_tensors/input_1.cairo b/tests/nodes/max_fp16x16_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..352adab6a --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_three_tensors/input_2.cairo b/tests/nodes/max_fp16x16_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..406f91365 --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,15 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_three_tensors/output_0.cairo b/tests/nodes/max_fp16x16_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..b743e5482 --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_two_tensors.cairo b/tests/nodes/max_fp16x16_broadcast_two_tensors.cairo new file mode 100644 index 000000000..b5ac6fdcd --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp16x16_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_two_tensors/input_0.cairo b/tests/nodes/max_fp16x16_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..92152cd37 --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_two_tensors/input_1.cairo b/tests/nodes/max_fp16x16_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..4cc6309d7 --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_broadcast_two_tensors/output_0.cairo b/tests/nodes/max_fp16x16_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..37ead1117 --- /dev/null +++ b/tests/nodes/max_fp16x16_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_three_tensors.cairo b/tests/nodes/max_fp16x16_three_tensors.cairo new file mode 100644 index 000000000..8ca7dfebe --- /dev/null +++ b/tests/nodes/max_fp16x16_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp16x16_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_three_tensors/input_0.cairo b/tests/nodes/max_fp16x16_three_tensors/input_0.cairo new file mode 100644 index 000000000..bb6a7efe8 --- /dev/null +++ b/tests/nodes/max_fp16x16_three_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_three_tensors/input_1.cairo b/tests/nodes/max_fp16x16_three_tensors/input_1.cairo new file mode 100644 index 000000000..71edb4da9 --- /dev/null +++ b/tests/nodes/max_fp16x16_three_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_three_tensors/input_2.cairo b/tests/nodes/max_fp16x16_three_tensors/input_2.cairo new file mode 100644 index 000000000..9845fefbe --- /dev/null +++ b/tests/nodes/max_fp16x16_three_tensors/input_2.cairo @@ -0,0 +1,42 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_three_tensors/output_0.cairo b/tests/nodes/max_fp16x16_three_tensors/output_0.cairo new file mode 100644 index 000000000..139ae932a --- /dev/null +++ b/tests/nodes/max_fp16x16_three_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_two_tensors.cairo b/tests/nodes/max_fp16x16_two_tensors.cairo new file mode 100644 index 000000000..98b522b3d --- /dev/null +++ b/tests/nodes/max_fp16x16_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp16x16_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_two_tensors/input_0.cairo b/tests/nodes/max_fp16x16_two_tensors/input_0.cairo new file mode 100644 index 000000000..1e70f825c --- /dev/null +++ b/tests/nodes/max_fp16x16_two_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_two_tensors/input_1.cairo b/tests/nodes/max_fp16x16_two_tensors/input_1.cairo new file mode 100644 index 000000000..c766b1157 --- /dev/null +++ b/tests/nodes/max_fp16x16_two_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp16x16_two_tensors/output_0.cairo b/tests/nodes/max_fp16x16_two_tensors/output_0.cairo new file mode 100644 index 000000000..66b666290 --- /dev/null +++ b/tests/nodes/max_fp16x16_two_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_three_tensors.cairo b/tests/nodes/max_fp8x23_broadcast_three_tensors.cairo new file mode 100644 index 000000000..2fdd60ff2 --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp8x23_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_three_tensors/input_0.cairo b/tests/nodes/max_fp8x23_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..2fa35f877 --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_three_tensors/input_1.cairo b/tests/nodes/max_fp8x23_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..fdcd11d36 --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_three_tensors/input_2.cairo b/tests/nodes/max_fp8x23_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..4a9a3a6e3 --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,15 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 25165824, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_three_tensors/output_0.cairo b/tests/nodes/max_fp8x23_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..78bcbc1ed --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_two_tensors.cairo b/tests/nodes/max_fp8x23_broadcast_two_tensors.cairo new file mode 100644 index 000000000..b40477513 --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp8x23_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_two_tensors/input_0.cairo b/tests/nodes/max_fp8x23_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..89cffdd5e --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,18 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_two_tensors/input_1.cairo b/tests/nodes/max_fp8x23_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..297233473 --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,16 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_broadcast_two_tensors/output_0.cairo b/tests/nodes/max_fp8x23_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..0506f1c0f --- /dev/null +++ b/tests/nodes/max_fp8x23_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,18 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_three_tensors.cairo b/tests/nodes/max_fp8x23_three_tensors.cairo new file mode 100644 index 000000000..d5cc6b030 --- /dev/null +++ b/tests/nodes/max_fp8x23_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp8x23_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_three_tensors/input_0.cairo b/tests/nodes/max_fp8x23_three_tensors/input_0.cairo new file mode 100644 index 000000000..f0f1d9253 --- /dev/null +++ b/tests/nodes/max_fp8x23_three_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_three_tensors/input_1.cairo b/tests/nodes/max_fp8x23_three_tensors/input_1.cairo new file mode 100644 index 000000000..f4611bf3c --- /dev/null +++ b/tests/nodes/max_fp8x23_three_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_three_tensors/input_2.cairo b/tests/nodes/max_fp8x23_three_tensors/input_2.cairo new file mode 100644 index 000000000..6eeb29edb --- /dev/null +++ b/tests/nodes/max_fp8x23_three_tensors/input_2.cairo @@ -0,0 +1,42 @@ +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_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_three_tensors/output_0.cairo b/tests/nodes/max_fp8x23_three_tensors/output_0.cairo new file mode 100644 index 000000000..e0318aad1 --- /dev/null +++ b/tests/nodes/max_fp8x23_three_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_two_tensors.cairo b/tests/nodes/max_fp8x23_two_tensors.cairo new file mode 100644 index 000000000..b54d193d6 --- /dev/null +++ b/tests/nodes/max_fp8x23_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_fp8x23_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_two_tensors/input_0.cairo b/tests/nodes/max_fp8x23_two_tensors/input_0.cairo new file mode 100644 index 000000000..038c7c361 --- /dev/null +++ b/tests/nodes/max_fp8x23_two_tensors/input_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_two_tensors/input_1.cairo b/tests/nodes/max_fp8x23_two_tensors/input_1.cairo new file mode 100644 index 000000000..da45b3378 --- /dev/null +++ b/tests/nodes/max_fp8x23_two_tensors/input_1.cairo @@ -0,0 +1,42 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_fp8x23_two_tensors/output_0.cairo b/tests/nodes/max_fp8x23_two_tensors/output_0.cairo new file mode 100644 index 000000000..797bfa996 --- /dev/null +++ b/tests/nodes/max_fp8x23_two_tensors/output_0.cairo @@ -0,0 +1,42 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_three_tensors.cairo b/tests/nodes/max_i32_broadcast_three_tensors.cairo new file mode 100644 index 000000000..af4660c1d --- /dev/null +++ b/tests/nodes/max_i32_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i32_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_three_tensors/input_0.cairo b/tests/nodes/max_i32_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..1c894ed65 --- /dev/null +++ b/tests/nodes/max_i32_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_three_tensors/input_1.cairo b/tests/nodes/max_i32_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..d4cfc3152 --- /dev/null +++ b/tests/nodes/max_i32_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_three_tensors/input_2.cairo b/tests/nodes/max_i32_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..2573224ed --- /dev/null +++ b/tests/nodes/max_i32_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_three_tensors/output_0.cairo b/tests/nodes/max_i32_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..5494fd3b3 --- /dev/null +++ b/tests/nodes/max_i32_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_two_tensors.cairo b/tests/nodes/max_i32_broadcast_two_tensors.cairo new file mode 100644 index 000000000..25d864bba --- /dev/null +++ b/tests/nodes/max_i32_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i32_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_two_tensors/input_0.cairo b/tests/nodes/max_i32_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..65fc1651c --- /dev/null +++ b/tests/nodes/max_i32_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_two_tensors/input_1.cairo b/tests/nodes/max_i32_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..b75480afa --- /dev/null +++ b/tests/nodes/max_i32_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_broadcast_two_tensors/output_0.cairo b/tests/nodes/max_i32_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..ffc6f4c24 --- /dev/null +++ b/tests/nodes/max_i32_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_three_tensors.cairo b/tests/nodes/max_i32_three_tensors.cairo new file mode 100644 index 000000000..186d5e2c5 --- /dev/null +++ b/tests/nodes/max_i32_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i32_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i32_three_tensors/input_0.cairo b/tests/nodes/max_i32_three_tensors/input_0.cairo new file mode 100644 index 000000000..e5fdee508 --- /dev/null +++ b/tests/nodes/max_i32_three_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_three_tensors/input_1.cairo b/tests/nodes/max_i32_three_tensors/input_1.cairo new file mode 100644 index 000000000..960743a74 --- /dev/null +++ b/tests/nodes/max_i32_three_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_three_tensors/input_2.cairo b/tests/nodes/max_i32_three_tensors/input_2.cairo new file mode 100644 index 000000000..f90517109 --- /dev/null +++ b/tests/nodes/max_i32_three_tensors/input_2.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_three_tensors/output_0.cairo b/tests/nodes/max_i32_three_tensors/output_0.cairo new file mode 100644 index 000000000..376bd0a58 --- /dev/null +++ b/tests/nodes/max_i32_three_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_two_tensors.cairo b/tests/nodes/max_i32_two_tensors.cairo new file mode 100644 index 000000000..18678b57e --- /dev/null +++ b/tests/nodes/max_i32_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I32Tensor; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i32_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i32_two_tensors/input_0.cairo b/tests/nodes/max_i32_two_tensors/input_0.cairo new file mode 100644 index 000000000..59ba3b966 --- /dev/null +++ b/tests/nodes/max_i32_two_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 0, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_two_tensors/input_1.cairo b/tests/nodes/max_i32_two_tensors/input_1.cairo new file mode 100644 index 000000000..6615ef39a --- /dev/null +++ b/tests/nodes/max_i32_two_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I32Tensor; +use orion::numbers::{IntegerTrait, i32}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i32_two_tensors/output_0.cairo b/tests/nodes/max_i32_two_tensors/output_0.cairo new file mode 100644 index 000000000..7eded96da --- /dev/null +++ b/tests/nodes/max_i32_two_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i32 { mag: 1, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 2, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 5, sign: false }); + data.append(i32 { mag: 3, sign: false }); + data.append(i32 { mag: 4, sign: false }); + data.append(i32 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_three_tensors.cairo b/tests/nodes/max_i8_broadcast_three_tensors.cairo new file mode 100644 index 000000000..65ea53cc9 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i8_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_three_tensors/input_0.cairo b/tests/nodes/max_i8_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..80c86eb24 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_three_tensors/input_1.cairo b/tests/nodes/max_i8_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..e029fbc53 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_three_tensors/input_2.cairo b/tests/nodes/max_i8_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..05fe8ff14 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_three_tensors/output_0.cairo b/tests/nodes/max_i8_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..381efaf32 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_two_tensors.cairo b/tests/nodes/max_i8_broadcast_two_tensors.cairo new file mode 100644 index 000000000..6a2f8b42f --- /dev/null +++ b/tests/nodes/max_i8_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i8_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_two_tensors/input_0.cairo b/tests/nodes/max_i8_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..ea28aa087 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_two_tensors/input_1.cairo b/tests/nodes/max_i8_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..e029fbc53 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,15 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_broadcast_two_tensors/output_0.cairo b/tests/nodes/max_i8_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..273837ef7 --- /dev/null +++ b/tests/nodes/max_i8_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,17 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_three_tensors.cairo b/tests/nodes/max_i8_three_tensors.cairo new file mode 100644 index 000000000..40b83fbcc --- /dev/null +++ b/tests/nodes/max_i8_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i8_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i8_three_tensors/input_0.cairo b/tests/nodes/max_i8_three_tensors/input_0.cairo new file mode 100644 index 000000000..07fbd1690 --- /dev/null +++ b/tests/nodes/max_i8_three_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_three_tensors/input_1.cairo b/tests/nodes/max_i8_three_tensors/input_1.cairo new file mode 100644 index 000000000..47afadf00 --- /dev/null +++ b/tests/nodes/max_i8_three_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_three_tensors/input_2.cairo b/tests/nodes/max_i8_three_tensors/input_2.cairo new file mode 100644 index 000000000..e37ded791 --- /dev/null +++ b/tests/nodes/max_i8_three_tensors/input_2.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_three_tensors/output_0.cairo b/tests/nodes/max_i8_three_tensors/output_0.cairo new file mode 100644 index 000000000..5814fe790 --- /dev/null +++ b/tests/nodes/max_i8_three_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_two_tensors.cairo b/tests/nodes/max_i8_two_tensors.cairo new file mode 100644 index 000000000..9f6ee682d --- /dev/null +++ b/tests/nodes/max_i8_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_i8_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_i8_two_tensors/input_0.cairo b/tests/nodes/max_i8_two_tensors/input_0.cairo new file mode 100644 index 000000000..ec6c88799 --- /dev/null +++ b/tests/nodes/max_i8_two_tensors/input_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_two_tensors/input_1.cairo b/tests/nodes/max_i8_two_tensors/input_1.cairo new file mode 100644 index 000000000..bc48996a9 --- /dev/null +++ b/tests/nodes/max_i8_two_tensors/input_1.cairo @@ -0,0 +1,41 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_i8_two_tensors/output_0.cairo b/tests/nodes/max_i8_two_tensors/output_0.cairo new file mode 100644 index 000000000..889fc62c1 --- /dev/null +++ b/tests/nodes/max_i8_two_tensors/output_0.cairo @@ -0,0 +1,41 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 1, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_three_tensors.cairo b/tests/nodes/max_u32_broadcast_three_tensors.cairo new file mode 100644 index 000000000..a378bd9a7 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_u32_broadcast_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_three_tensors/input_0.cairo b/tests/nodes/max_u32_broadcast_three_tensors/input_0.cairo new file mode 100644 index 000000000..108d66672 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_three_tensors/input_0.cairo @@ -0,0 +1,16 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(5); + data.append(2); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_three_tensors/input_1.cairo b/tests/nodes/max_u32_broadcast_three_tensors/input_1.cairo new file mode 100644 index 000000000..a904f4e58 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_three_tensors/input_1.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_three_tensors/input_2.cairo b/tests/nodes/max_u32_broadcast_three_tensors/input_2.cairo new file mode 100644 index 000000000..ab0ca7fb6 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_three_tensors/input_2.cairo @@ -0,0 +1,13 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_three_tensors/output_0.cairo b/tests/nodes/max_u32_broadcast_three_tensors/output_0.cairo new file mode 100644 index 000000000..0f21da101 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_three_tensors/output_0.cairo @@ -0,0 +1,16 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(5); + data.append(4); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_two_tensors.cairo b/tests/nodes/max_u32_broadcast_two_tensors.cairo new file mode 100644 index 000000000..ddcd35e04 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_u32_broadcast_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_two_tensors/input_0.cairo b/tests/nodes/max_u32_broadcast_two_tensors/input_0.cairo new file mode 100644 index 000000000..e7dcaeaf3 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_two_tensors/input_0.cairo @@ -0,0 +1,16 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(5); + data.append(5); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_two_tensors/input_1.cairo b/tests/nodes/max_u32_broadcast_two_tensors/input_1.cairo new file mode 100644 index 000000000..b09e9c973 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_two_tensors/input_1.cairo @@ -0,0 +1,14 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_broadcast_two_tensors/output_0.cairo b/tests/nodes/max_u32_broadcast_two_tensors/output_0.cairo new file mode 100644 index 000000000..8354b7151 --- /dev/null +++ b/tests/nodes/max_u32_broadcast_two_tensors/output_0.cairo @@ -0,0 +1,16 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(5); + data.append(5); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_three_tensors.cairo b/tests/nodes/max_u32_three_tensors.cairo new file mode 100644 index 000000000..655f2a713 --- /dev/null +++ b/tests/nodes/max_u32_three_tensors.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_u32_three_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1, input_2].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_u32_three_tensors/input_0.cairo b/tests/nodes/max_u32_three_tensors/input_0.cairo new file mode 100644 index 000000000..e428995ce --- /dev/null +++ b/tests/nodes/max_u32_three_tensors/input_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(1); + data.append(1); + data.append(4); + data.append(1); + data.append(1); + data.append(2); + data.append(3); + data.append(5); + data.append(5); + data.append(1); + data.append(1); + data.append(2); + data.append(5); + data.append(0); + data.append(4); + data.append(1); + data.append(1); + data.append(4); + data.append(4); + data.append(3); + data.append(2); + data.append(1); + data.append(0); + data.append(1); + data.append(0); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_three_tensors/input_1.cairo b/tests/nodes/max_u32_three_tensors/input_1.cairo new file mode 100644 index 000000000..edb019a72 --- /dev/null +++ b/tests/nodes/max_u32_three_tensors/input_1.cairo @@ -0,0 +1,40 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(5); + data.append(5); + data.append(0); + data.append(2); + data.append(1); + data.append(2); + data.append(3); + data.append(1); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(5); + data.append(4); + data.append(5); + data.append(5); + data.append(5); + data.append(4); + data.append(2); + data.append(5); + data.append(1); + data.append(4); + data.append(4); + data.append(5); + data.append(0); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_three_tensors/input_2.cairo b/tests/nodes/max_u32_three_tensors/input_2.cairo new file mode 100644 index 000000000..cdd6dd700 --- /dev/null +++ b/tests/nodes/max_u32_three_tensors/input_2.cairo @@ -0,0 +1,40 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(2); + data.append(5); + data.append(5); + data.append(1); + data.append(3); + data.append(5); + data.append(0); + data.append(2); + data.append(2); + data.append(4); + data.append(4); + data.append(5); + data.append(4); + data.append(2); + data.append(5); + data.append(5); + data.append(4); + data.append(4); + data.append(5); + data.append(1); + data.append(4); + data.append(5); + data.append(2); + data.append(5); + data.append(4); + data.append(4); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_three_tensors/output_0.cairo b/tests/nodes/max_u32_three_tensors/output_0.cairo new file mode 100644 index 000000000..b8b2b72aa --- /dev/null +++ b/tests/nodes/max_u32_three_tensors/output_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(5); + data.append(5); + data.append(4); + data.append(3); + data.append(5); + data.append(2); + data.append(3); + data.append(5); + data.append(5); + data.append(4); + data.append(5); + data.append(4); + data.append(5); + data.append(5); + data.append(5); + data.append(5); + data.append(5); + data.append(5); + data.append(4); + data.append(5); + data.append(5); + data.append(4); + data.append(5); + data.append(5); + data.append(4); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_two_tensors.cairo b/tests/nodes/max_u32_two_tensors.cairo new file mode 100644 index 000000000..faed3d640 --- /dev/null +++ b/tests/nodes/max_u32_two_tensors.cairo @@ -0,0 +1,22 @@ +mod input_0; +mod input_1; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_max_u32_two_tensors() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let z = output_0::output_0(); + + let y = TensorTrait::max(array![input_0, input_1].span()); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/max_u32_two_tensors/input_0.cairo b/tests/nodes/max_u32_two_tensors/input_0.cairo new file mode 100644 index 000000000..dde213565 --- /dev/null +++ b/tests/nodes/max_u32_two_tensors/input_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(3); + data.append(1); + data.append(0); + data.append(2); + data.append(0); + data.append(0); + data.append(4); + data.append(1); + data.append(1); + data.append(4); + data.append(4); + data.append(3); + data.append(2); + data.append(4); + data.append(2); + data.append(4); + data.append(5); + data.append(1); + data.append(3); + data.append(3); + data.append(3); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(5); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_two_tensors/input_1.cairo b/tests/nodes/max_u32_two_tensors/input_1.cairo new file mode 100644 index 000000000..bdd40bb01 --- /dev/null +++ b/tests/nodes/max_u32_two_tensors/input_1.cairo @@ -0,0 +1,40 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(2); + data.append(2); + data.append(0); + data.append(0); + data.append(5); + data.append(1); + data.append(1); + data.append(1); + data.append(2); + data.append(0); + data.append(4); + data.append(2); + data.append(1); + data.append(5); + data.append(1); + data.append(4); + data.append(3); + data.append(0); + data.append(3); + data.append(1); + data.append(1); + data.append(3); + data.append(5); + data.append(4); + data.append(4); + data.append(2); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/max_u32_two_tensors/output_0.cairo b/tests/nodes/max_u32_two_tensors/output_0.cairo new file mode 100644 index 000000000..e585ea8dc --- /dev/null +++ b/tests/nodes/max_u32_two_tensors/output_0.cairo @@ -0,0 +1,40 @@ +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(3); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(3); + data.append(2); + data.append(0); + data.append(2); + data.append(5); + data.append(1); + data.append(4); + data.append(1); + data.append(2); + data.append(4); + data.append(4); + data.append(3); + data.append(2); + data.append(5); + data.append(2); + data.append(4); + data.append(5); + data.append(1); + data.append(3); + data.append(3); + data.append(3); + data.append(3); + data.append(5); + data.append(4); + data.append(4); + data.append(5); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file From f6535ed1c4e78d13b07b165a549e9247dc818c52 Mon Sep 17 00:00:00 2001 From: Daniel Voronov Date: Fri, 27 Oct 2023 16:52:46 +0200 Subject: [PATCH 08/21] Add docs --- docs/SUMMARY.md | 1 + docs/framework/compatibility.md | 1 + docs/framework/operators/tensor/README.md | 1 + docs/framework/operators/tensor/tensor.max.md | 63 ++++++++++++++++++ src/operators/tensor/core.cairo | 64 +++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 docs/framework/operators/tensor/tensor.max.md diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 9189b06c2..8cc333a72 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -42,6 +42,7 @@ * [tensor.at](framework/operators/tensor/tensor.at.md) * [tensor.min](framework/operators/tensor/tensor.min.md) * [tensor.max\_in\_tensor](framework/operators/tensor/tensor.max\_in\_tensor.md) + * [tensor.max](framework/operators/tensor/tensor.max.md) * [tensor.stride](framework/operators/tensor/tensor.stride.md) * [tensor.ravel\_index](framework/operators/tensor/tensor.ravel\_index.md) * [tensor.unravel\_index](framework/operators/tensor/tensor.unravel\_index.md) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index 710191606..2f13fee51 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -68,5 +68,6 @@ You can see below the list of current supported ONNX Operators: | [Gemm](operators/neural-network/nn.gemm.md) | :white\_check\_mark: | | [Where](operators/tensor/tensor.where.md) | :white\_check\_mark: | | [MaxInTensor](operators/tensor/tensor.max\_in\_tensor.md) | :white\_check\_mark: | +| [Max](operators/tensor/tensor.max.md) | :white\_check\_mark: | Current Operators support: **61/156 (39%)** diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index d92d13560..f2b70b3a5 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -52,6 +52,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.onehot`](tensor.onehot.md) | Produces one-hot tensor based on input. | | [`tensor.min`](tensor.min.md) | Returns the minimum value in the tensor. | | [`tensor.max_in_tensor`](tensor.max\_in\_tensor.md) | Returns the maximum value in the tensor. | +| [`tensor.max`](tensor.max.md) | Returns the element-wise maximum values from a list of input tensors. | | [`tensor.reduce_sum`](tensor.reduce\_sum.md) | Reduces a tensor by summing its elements along a specified axis. | | [`tensor.argmax`](tensor.argmax.md) | Returns the index of the maximum value along the specified axis. | | [`tensor.argmin`](tensor.argmin.md) | Returns the index of the minimum value along the specified axis. | diff --git a/docs/framework/operators/tensor/tensor.max.md b/docs/framework/operators/tensor/tensor.max.md new file mode 100644 index 000000000..966405862 --- /dev/null +++ b/docs/framework/operators/tensor/tensor.max.md @@ -0,0 +1,63 @@ +# tensor.max + +```rust + fn max(tensors: Span>) -> Tensor; +``` + +Returns the element-wise maximum values from a list of input tensors +The input tensors must have either: +* Exactly the same shape +* The same number of dimensions and the length of each dimension is either a common length or 1. + +## Args + +* `tensors`(` Span>,`) - Array of the input tensors + +## Returns + +A new `Tensor` containing the element-wise maximum values + +## Panics + +* Panics if tensor array is empty +* Panics if the shapes are not equal or broadcastable + +## Examples + +Case 1: Process tensors with same shape + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + +fn max_example() -> Tensor { + 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![0, 3, 1, 2].span(),); + let result = TensorTrait::max(tensors: array![tensor1, tensor2].span()); + return result; +} +>>> [0, 3, 2, 3] + + result.shape +>>> (2, 2) +``` + +Case 2: Process tensors with different shapes + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + +fn max_example() -> Tensor { + let tensor1 = TensorTrait::new(shape: array![2, 2].span(), data: array![0, 1, 2, 3].span(),); + let tensor2 = TensorTrait::new(shape: array![1, 2].span(), data: array![1, 4].span(),); + let result = TensorTrait::max(tensors: array![tensor1, tensor2].span()); + return result; +} +>>> [1, 4, 2, 4] + + result.shape +>>> (2, 2) +``` diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index f911e3f43..1f4b8b49e 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -255,6 +255,70 @@ trait TensorTrait { /// ``` /// fn max_in_tensor(self: @Tensor) -> T; + /// # tensor.max + /// + /// ```rust + /// fn max(tensors: Span>) -> Tensor; + /// ``` + /// + /// Returns the element-wise maximum values from a list of input tensors + /// The input tensors must have either: + /// * Exactly the same shape + /// * The same number of dimensions and the length of each dimension is either a common length or 1. + /// + /// ## Args + /// + /// * `tensors`(` Span>,`) - Array of the input tensors + /// + /// ## Returns + /// + /// A new `Tensor` containing the element-wise maximum values + /// + /// ## Panics + /// + /// * Panics if tensor array is empty + /// * Panics if the shapes are not equal or broadcastable + /// + /// ## Examples + /// + /// Case 1: Process tensors with same shape + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn max_example() -> Tensor { + /// 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![0, 3, 1, 2].span(),); + /// let result = TensorTrait::max(tensors: array![tensor1, tensor2].span()); + /// return result; + /// } + /// >>> [0, 3, 2, 3] + /// + /// result.shape + /// >>> (2, 2) + /// ``` + /// + /// Case 2: Process tensors with different shapes + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn max_example() -> Tensor { + /// let tensor1 = TensorTrait::new(shape: array![2, 2].span(), data: array![0, 1, 2, 3].span(),); + /// let tensor2 = TensorTrait::new(shape: array![1, 2].span(), data: array![1, 4].span(),); + /// let result = TensorTrait::max(tensors: array![tensor1, tensor2].span()); + /// return result; + /// } + /// >>> [1, 4, 2, 4] + /// + /// result.shape + /// >>> (2, 2) + /// ``` + /// fn max(tensors: Span>) -> Tensor; /// # tensor.stride /// From 28da521c6dc5c0bb0d857b54d3842516136594fd Mon Sep 17 00:00:00 2001 From: andresmayorca Date: Mon, 30 Oct 2023 14:13:36 -0400 Subject: [PATCH 09/21] add round --- Scarb.lock | 2 +- docs/SUMMARY.md | 1 + docs/framework/compatibility.md | 1 + docs/framework/operators/tensor/README.md | 1 + .../operators/tensor/tensor.round.md | 39 +++++++++++++++++ nodegen/node/round.py | 29 +++++++++++++ src/operators/tensor/core.cairo | 43 ++++++++++++++++++- .../implementations/tensor_fp16x16.cairo | 5 +++ .../implementations/tensor_fp16x16wide.cairo | 4 ++ .../implementations/tensor_fp32x32.cairo | 4 ++ .../implementations/tensor_fp64x64.cairo | 4 ++ .../implementations/tensor_fp8x23.cairo | 4 ++ .../implementations/tensor_fp8x23wide.cairo | 7 ++- .../tensor/implementations/tensor_i32.cairo | 4 ++ .../tensor/implementations/tensor_i8.cairo | 4 ++ .../tensor/implementations/tensor_u32.cairo | 4 ++ src/operators/tensor/math.cairo | 1 + src/operators/tensor/math/round.cairo | 30 +++++++++++++ tests/nodes.cairo | 3 +- tests/nodes/round_fp16x16.cairo | 20 +++++++++ tests/nodes/round_fp16x16/input_0.cairo | 28 ++++++++++++ tests/nodes/round_fp16x16/output_0.cairo | 28 ++++++++++++ tests/nodes/round_fp8x23.cairo | 20 +++++++++ tests/nodes/round_fp8x23/input_0.cairo | 28 ++++++++++++ tests/nodes/round_fp8x23/output_0.cairo | 28 ++++++++++++ 25 files changed, 338 insertions(+), 4 deletions(-) create mode 100644 docs/framework/operators/tensor/tensor.round.md create mode 100644 nodegen/node/round.py create mode 100644 src/operators/tensor/math/round.cairo create mode 100644 tests/nodes/round_fp16x16.cairo create mode 100644 tests/nodes/round_fp16x16/input_0.cairo create mode 100644 tests/nodes/round_fp16x16/output_0.cairo create mode 100644 tests/nodes/round_fp8x23.cairo create mode 100644 tests/nodes/round_fp8x23/input_0.cairo create mode 100644 tests/nodes/round_fp8x23/output_0.cairo diff --git a/Scarb.lock b/Scarb.lock index 85e5484e6..65dbec62d 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -13,7 +13,7 @@ source = "git+https://github.com/raphaelDkhn/cubit.git#e6331ebf98c5d5f442a0e5ede [[package]] name = "orion" -version = "0.1.2" +version = "0.1.5" dependencies = [ "alexandria_data_structures", "cubit", diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 409c1715e..1d2f26df4 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -90,6 +90,7 @@ * [tensor.identity](framework/operators/tensor/tensor.identity.md) * [tensor.and](framework/operators/tensor/tensor.and.md) * [tensor.where](framework/operators/tensor/tensor.where.md) + * [tensor.round](framework/operators/tensor/tensor.round.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) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index 29d537ff6..185abbbd1 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -67,5 +67,6 @@ You can see below the list of current supported ONNX Operators: | [Or](operators/tensor/tensor.or.md) | :white\_check\_mark: | | [Gemm](operators/neural-network/nn.gemm.md) | :white\_check\_mark: | | [Where](operators/tensor/tensor.where.md) | :white\_check\_mark: | +| [Round](operators/tensor/tensor.round.md) | :white\_check\_mark: | Current Operators support: **61/156 (39%)** diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index 361401570..bc238603b 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -86,6 +86,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.and`](tensor.and.md) | Computes the logical AND of two tensors element-wise. | | [`tensor.identity`](tensor.identity.md) | Return a Tensor with the same shape and contents as input. | | [`tensor.where`](tensor.where.md) | Return elements chosen from x or y depending on condition. | +| [`tensor.round`](tensor.round.md) | Computes the round value of all elements in the input tensor. | ## Arithmetic Operations diff --git a/docs/framework/operators/tensor/tensor.round.md b/docs/framework/operators/tensor/tensor.round.md new file mode 100644 index 000000000..9a6c949eb --- /dev/null +++ b/docs/framework/operators/tensor/tensor.round.md @@ -0,0 +1,39 @@ +#tensor.round + +```rust + fn round(self: @Tensor) -> Tensor; +``` + +Computes the round value of all elements in the input tensor. + +## Args + +* `self`(`@Tensor`) - The input tensor. + + +## Returns + +A new `Tensor` of the same shape as the input tensor with +the round value of all elements in the input tensor. + +## Example + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, FP16x16Tensor}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn round_example() -> Tensor { + let tensor = TensorTrait::::new( + shape: array![3].span(), + data: array![ + FixedTrait::new(190054, false), // 2.9 + ] + .span(), + ); + + return tensor.round(); +} +>>> [3] +``` diff --git a/nodegen/node/round.py b/nodegen/node/round.py new file mode 100644 index 000000000..1d12c3fdc --- /dev/null +++ b/nodegen/node/round.py @@ -0,0 +1,29 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl + +class Round(RunAll): + + @staticmethod + def round_fp8x23(): + x = np.array([0.1, 0.5, 0.9, 1.2, 1.5, 1.8, 2.3, 2.5, 2.7, -1.1, -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float64) + y = np.array([0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, -1.0, -2.0, -2.0, -2.0, -3.0, -3.0]).astype(np.float64) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp(x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23)) + + name = "round_fp8x23" + make_node([x], [y], name) + make_test([x], y, "input_0.round()", name) + + @staticmethod + def round_fp16x16(): + x = np.array([0.1, 0.5, 0.9, 1.2, 1.5, 1.8, 2.3, 2.5, 2.7, -1.1, -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float64) + y = np.array([0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, -1.0, -2.0, -2.0, -2.0, -3.0, -3.0]).astype(np.float64) + + 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 = "round_fp16x16" + make_node([x], [y], name) + make_test([x], y, "input_0.round()", name) \ No newline at end of file diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 94a9baac7..c541e264e 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -82,7 +82,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde { /// # tensor.new /// @@ -2868,6 +2868,47 @@ trait TensorTrait { /// ``` /// fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor; + /// #tensor.round + /// + /// ```rust + /// fn round(self: @Tensor) -> Tensor; + /// ``` + /// + /// Computes the round value of all elements in the input tensor. + /// + /// ## Args + /// + /// * `self`(`@Tensor`) - The input tensor. + /// + /// + /// ## Returns + /// + /// A new `Tensor` of the same shape as the input tensor with + /// the round value of all elements in the input tensor. + /// + /// ## Example + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, FP16x16Tensor}; + /// use orion::numbers::{FixedTrait, FP16x16}; + /// + /// fn round_example() -> Tensor { + /// let tensor = TensorTrait::::new( + /// shape: array![3].span(), + /// data: array![ + /// FixedTrait::new(190054, false), // 2.9 + /// ] + /// .span(), + /// ); + /// + /// return tensor.round(); + /// } + /// >>> [3] + /// ``` + /// + fn round(self: @Tensor) -> Tensor; } /// Cf: TensorTrait::new docstring diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index f726c8d1e..79e6981c5 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -245,6 +245,11 @@ impl FP16x16Tensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } + } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index c4f2de169..743aa943b 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -255,6 +255,10 @@ impl FP16x16WTensor of TensorTrait { ) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } } /// 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 5f78a86f6..8901e1597 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -246,6 +246,10 @@ impl FP32x32Tensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } } /// 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 d9e540293..518bad9ce 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -246,6 +246,10 @@ impl FP64x64Tensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } } /// 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 153f179ef..949268629 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -245,6 +245,10 @@ impl FP8x23Tensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index f594eec4d..7efd77e8d 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -245,7 +245,12 @@ impl FP8x23WTensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } -} + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } + +} /// Implements addition for `Tensor` using the `Add` trait. impl FP8x23WTensorAdd< diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 057fb6853..ef1243f77 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -244,6 +244,10 @@ impl I32Tensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } } /// 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 ae2662f0d..ae898e7fc 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -243,6 +243,10 @@ impl I8Tensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } } /// 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 fc83d35c3..8ecc9130f 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -237,6 +237,10 @@ impl U32Tensor of TensorTrait { fn where(self: @Tensor, x: @Tensor, y: @Tensor) -> Tensor { math::where::where(self, x, y) } + + fn round(self: @Tensor) -> Tensor { + math::round::round(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index d8c5a56f4..22c110cbd 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -35,3 +35,4 @@ mod sign; mod and; mod neg; mod where; +mod round; \ No newline at end of file diff --git a/src/operators/tensor/math/round.cairo b/src/operators/tensor/math/round.cairo new file mode 100644 index 000000000..9af729b17 --- /dev/null +++ b/src/operators/tensor/math/round.cairo @@ -0,0 +1,30 @@ +use array::ArrayTrait; +use array::SpanTrait; +use option::OptionTrait; +use traits::Into; + +use orion::numbers::NumberTrait; +use orion::operators::tensor::core::{Tensor, TensorTrait}; + + +fn round< + T, + MAG, + impl TNumberTrait: NumberTrait, + impl FTensor: TensorTrait, + impl FCopy: Copy, + impl FDrop: Drop +>( + mut self: Tensor +) -> Tensor { + let mut result = ArrayTrait::new(); + + loop { + match self.data.pop_front() { + Option::Some(item) => { result.append((*item).round()); }, + Option::None(_) => { break; } + }; + }; + + return TensorTrait::new(self.shape, result.span()); +} diff --git a/tests/nodes.cairo b/tests/nodes.cairo index d867b2277..e71e404ab 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -474,4 +474,5 @@ mod where_i8; mod where_i8_broadcast; mod where_u32; mod where_u32_broadcast; - +mod round_fp16x16; +mod round_fp8x23; diff --git a/tests/nodes/round_fp16x16.cairo b/tests/nodes/round_fp16x16.cairo new file mode 100644 index 000000000..02a279685 --- /dev/null +++ b/tests/nodes/round_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::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_round_fp16x16() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.round(); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/round_fp16x16/input_0.cairo b/tests/nodes/round_fp16x16/input_0.cairo new file mode 100644 index 000000000..68229762b --- /dev/null +++ b/tests/nodes/round_fp16x16/input_0.cairo @@ -0,0 +1,28 @@ +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(15); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 6553, sign: false }); + data.append(FP16x16 { mag: 32768, sign: false }); + data.append(FP16x16 { mag: 58982, sign: false }); + data.append(FP16x16 { mag: 78643, sign: false }); + data.append(FP16x16 { mag: 98304, sign: false }); + data.append(FP16x16 { mag: 117964, sign: false }); + data.append(FP16x16 { mag: 150732, sign: false }); + data.append(FP16x16 { mag: 163840, sign: false }); + data.append(FP16x16 { mag: 176947, sign: false }); + data.append(FP16x16 { mag: 72089, sign: true }); + data.append(FP16x16 { mag: 98304, sign: true }); + data.append(FP16x16 { mag: 124518, sign: true }); + data.append(FP16x16 { mag: 144179, sign: true }); + data.append(FP16x16 { mag: 163840, sign: true }); + data.append(FP16x16 { mag: 183500, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/round_fp16x16/output_0.cairo b/tests/nodes/round_fp16x16/output_0.cairo new file mode 100644 index 000000000..e303e30e4 --- /dev/null +++ b/tests/nodes/round_fp16x16/output_0.cairo @@ -0,0 +1,28 @@ +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(15); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: false }); + data.append(FP16x16 { mag: 196608, sign: false }); + data.append(FP16x16 { mag: 65536, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 131072, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 196608, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/round_fp8x23.cairo b/tests/nodes/round_fp8x23.cairo new file mode 100644 index 000000000..e09a76fd2 --- /dev/null +++ b/tests/nodes/round_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::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_round_fp8x23() { + let input_0 = input_0::input_0(); + let z = output_0::output_0(); + + let y = input_0.round(); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/round_fp8x23/input_0.cairo b/tests/nodes/round_fp8x23/input_0.cairo new file mode 100644 index 000000000..d812ae1a2 --- /dev/null +++ b/tests/nodes/round_fp8x23/input_0.cairo @@ -0,0 +1,28 @@ +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(15); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 838860, sign: false }); + data.append(FP8x23 { mag: 4194304, sign: false }); + data.append(FP8x23 { mag: 7549747, sign: false }); + data.append(FP8x23 { mag: 10066329, sign: false }); + data.append(FP8x23 { mag: 12582912, sign: false }); + data.append(FP8x23 { mag: 15099494, sign: false }); + data.append(FP8x23 { mag: 19293798, sign: false }); + data.append(FP8x23 { mag: 20971520, sign: false }); + data.append(FP8x23 { mag: 22649241, sign: false }); + data.append(FP8x23 { mag: 9227468, sign: true }); + data.append(FP8x23 { mag: 12582912, sign: true }); + data.append(FP8x23 { mag: 15938355, sign: true }); + data.append(FP8x23 { mag: 18454937, sign: true }); + data.append(FP8x23 { mag: 20971520, sign: true }); + data.append(FP8x23 { mag: 23488102, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/round_fp8x23/output_0.cairo b/tests/nodes/round_fp8x23/output_0.cairo new file mode 100644 index 000000000..d5cc73f5f --- /dev/null +++ b/tests/nodes/round_fp8x23/output_0.cairo @@ -0,0 +1,28 @@ +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(15); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + data.append(FP8x23 { mag: 25165824, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file From 11d5c739c5aac98c2e36f7605adb49a51383e32d Mon Sep 17 00:00:00 2001 From: chachaleo Date: Tue, 24 Oct 2023 15:48:08 +0700 Subject: [PATCH 10/21] qlinearmatmul --- src/operators/tensor.cairo | 5 + src/operators/tensor/core.cairo | 37 +++ src/operators/tensor/helpers.cairo | 24 +- .../implementations/tensor_fp16x16.cairo | 17 ++ .../implementations/tensor_fp32x32.cairo | 17 ++ .../implementations/tensor_fp64x64.cairo | 17 ++ .../implementations/tensor_fp8x23.cairo | 17 ++ .../tensor/implementations/tensor_i32.cairo | 18 ++ .../tensor/implementations/tensor_i8.cairo | 18 ++ .../tensor/implementations/tensor_u32.cairo | 6 + src/operators/tensor/quantization.cairo | 1 + .../tensor/quantization/qlinear_matmul.cairo | 217 ++++++++++++++++++ tests/operators/qlinearmatmul_test.cairo | 195 ++++++++++++++++ 13 files changed, 581 insertions(+), 8 deletions(-) create mode 100644 src/operators/tensor/quantization/qlinear_matmul.cairo create mode 100644 tests/operators/qlinearmatmul_test.cairo diff --git a/src/operators/tensor.cairo b/src/operators/tensor.cairo index 976102b52..30403831f 100644 --- a/src/operators/tensor.cairo +++ b/src/operators/tensor.cairo @@ -12,6 +12,11 @@ use orion::operators::tensor::implementations::tensor_fp8x23::{ FP8x23TensorPartialEq, }; +use orion::operators::tensor::implementations::tensor_fp32x32::{ + FP32x32Tensor, FP32x32TensorAdd, FP32x32TensorSub, FP32x32TensorMul, FP32x32TensorDiv, + FP32x32TensorPartialEq, +}; + use orion::operators::tensor::implementations::tensor_fp16x16::{ FP16x16Tensor, FP16x16TensorAdd, FP16x16TensorSub, FP16x16TensorMul, FP16x16TensorDiv, FP16x16TensorPartialEq, diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 94a9baac7..6b12ee5a7 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -2405,6 +2405,43 @@ trait TensorTrait { fn dequantize_linear( self: @Tensor, x_scale: @Tensor, x_zero_point: @Tensor ) -> Tensor::; + /// # tensor.qlinear_matmul + /// + /// ```rust + /// fn qlinear_matmul(self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor) -> Tensor::; + /// ``` + /// + /// Multiplies quantized Tensors + /// + /// It consumes two quantized input tensors, their scales and zero points, scale and zero point of output, and computes the quantized output. + /// The quantization formula is y = saturate((x / y_scale) + y_zero_point). + /// It perfoms the multiplication of the two vectors once dequantized. If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes. + /// Then return the quantization of the result of the multiplication. + /// Scale and zero point must have same shape and the same type. They must be either scalar (per tensor) or N-D tensor (per row for 'a' and per column for 'b'). + /// Scalar refers to per tensor quantization whereas N-D refers to per row or per column quantization. + /// + /// ## Args + /// + /// * `self`(`@Tensor`) - The first tensor to be multiplied (a). + /// * `a_scale`(`@Tensor`) - Scale for input `a`. + /// * `a_zero_point`(`@Tensor`) - Zero point for input `a`. + /// * `b`(`@Tensor`) - The second tensor to be multiplied + /// * `b_scale`(`@Tensor`) - Scale for input `b`. + /// * `b_zero_point`(`@Tensor`) - Zero point for input `b`. + /// * `y_scale`(`@Tensor`) - Scale for outut. + /// * `y_zero_point`(`@Tensor`) - Zero point for output. + /// + /// ## Returns + /// + /// A new `Tensor`, containing the quantized result of the multiplication of the dequantized inputs. + /// + /// ## Type Constraints + /// + /// u32 tensor, not supported. + /// + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor::; /// # tensor.slice /// /// ```rust diff --git a/src/operators/tensor/helpers.cairo b/src/operators/tensor/helpers.cairo index 471ec7e4e..c13459513 100644 --- a/src/operators/tensor/helpers.cairo +++ b/src/operators/tensor/helpers.cairo @@ -261,17 +261,25 @@ fn broadcast_shape(mut shape1: Span, mut shape2: Span) -> Span { dim1 = *item; }, - Option::None(_) => { if shape1.len() == 0 && shape2.len() == 0 { - break (); - }; } + Option::Some(item) => { + dim1 = *item; + }, + Option::None(_) => { + if shape1.len() == 0 && shape2.len() == 0 { + break (); + }; + } }; match shape2.pop_front() { - Option::Some(item) => { dim2 = *item; }, - Option::None(_) => { if shape1.len() == 0 && shape2.len() == 0 { - break (); - }; } + Option::Some(item) => { + dim2 = *item; + }, + Option::None(_) => { + if shape1.len() == 0 && shape2.len() == 0 { + break (); + }; + } }; let broadcasted_dim = u32_max(dim1, dim2); diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index f726c8d1e..518a44973 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -198,6 +198,23 @@ impl FP16x16Tensor of TensorTrait { quantization::dequantize_linear::dequantize_linear(self, x_scale, x_zero_point) } + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + quantization::qlinear_matmul::qlinear_matmul( + self, + a_scale, + a_zero_point, + b, + b_scale, + b_zero_point, + y_scale, + y_zero_point, + NumberTrait::new_unscaled(128, true), + NumberTrait::new_unscaled(127, false) + ) + } + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 5f78a86f6..a3b6c1e72 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -199,6 +199,23 @@ impl FP32x32Tensor of TensorTrait { quantization::dequantize_linear::dequantize_linear(self, x_scale, x_zero_point) } + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + quantization::qlinear_matmul::qlinear_matmul( + self, + a_scale, + a_zero_point, + b, + b_scale, + b_zero_point, + y_scale, + y_zero_point, + NumberTrait::new_unscaled(128, true), + NumberTrait::new_unscaled(127, false) + ) + } + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index d9e540293..6a802b67d 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -199,6 +199,23 @@ impl FP64x64Tensor of TensorTrait { quantization::dequantize_linear::dequantize_linear(self, x_scale, x_zero_point) } + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + quantization::qlinear_matmul::qlinear_matmul( + self, + a_scale, + a_zero_point, + b, + b_scale, + b_zero_point, + y_scale, + y_zero_point, + NumberTrait::new_unscaled(128, true), + NumberTrait::new_unscaled(127, false) + ) + } + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 153f179ef..0bf738fa8 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -198,6 +198,23 @@ impl FP8x23Tensor of TensorTrait { quantization::dequantize_linear::dequantize_linear(self, x_scale, x_zero_point) } + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + quantization::qlinear_matmul::qlinear_matmul( + self, + a_scale, + a_zero_point, + b, + b_scale, + b_zero_point, + y_scale, + y_zero_point, + NumberTrait::new_unscaled(128, true), + NumberTrait::new_unscaled(127, false) + ) + } + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 057fb6853..c5d349a5c 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -199,6 +199,24 @@ impl I32Tensor of TensorTrait { quantization::dequantize_linear::dequantize_linear(self, x_scale, x_zero_point) } + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + quantization::qlinear_matmul::qlinear_matmul( + self, + a_scale, + a_zero_point, + b, + b_scale, + b_zero_point, + y_scale, + y_zero_point, + NumberTrait::new_unscaled(128, true), + NumberTrait::new_unscaled(127, false) + ) + } + + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index ae2662f0d..438964f30 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -196,8 +196,26 @@ impl I8Tensor of TensorTrait { self: @Tensor, x_scale: @Tensor, x_zero_point: @Tensor ) -> Tensor:: { quantization::dequantize_linear::dequantize_linear(self, x_scale, x_zero_point) + } + + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + quantization::qlinear_matmul::qlinear_matmul( + self, + a_scale, + a_zero_point, + b, + b_scale, + b_zero_point, + y_scale, + y_zero_point, + NumberTrait::new_unscaled(128, true), + NumberTrait::new_unscaled(127, false) + ) } + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index fc83d35c3..4ec329c69 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -192,6 +192,12 @@ impl U32Tensor of TensorTrait { panic(array!['not supported!']) } + fn qlinear_matmul( + self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/quantization.cairo b/src/operators/tensor/quantization.cairo index 0ed0c12d8..62b428766 100644 --- a/src/operators/tensor/quantization.cairo +++ b/src/operators/tensor/quantization.cairo @@ -1,2 +1,3 @@ mod quantize_linear; mod dequantize_linear; +mod qlinear_matmul; \ No newline at end of file diff --git a/src/operators/tensor/quantization/qlinear_matmul.cairo b/src/operators/tensor/quantization/qlinear_matmul.cairo new file mode 100644 index 000000000..5409b8a11 --- /dev/null +++ b/src/operators/tensor/quantization/qlinear_matmul.cairo @@ -0,0 +1,217 @@ +use array::ArrayTrait; +use array::SpanTrait; +use option::OptionTrait; + +use orion::numbers::{NumberTrait}; +use orion::operators::tensor::quantization::dequantize_linear::dequantize_linear; +use orion::operators::tensor::quantization::quantize_linear::quantize_linear; +use orion::operators::tensor::{TensorTrait, Tensor}; + + +/// Cf: TensorTrait::qlinear_matmul docstring +fn qlinear_matmul< + T, + MAG, + Q, + impl TTensor: TensorTrait, + impl QTensor: TensorTrait, + impl QIntoT: Into, + impl QTensorIntoTTensor: Into, Tensor>, + impl TAdd: Add, + impl TSub: Sub, + impl TDiv: Div, + impl TMul: Mul, + impl TTensorSub: Sub>, + impl TTensorMul: Mul>, + impl TTensorDiv: Div>, + impl TPartialOrd: PartialOrd, + impl TNumber: NumberTrait, + impl TTryInto: TryInto, + //impl TTensorTryInto: TryInto, Tensor>, + impl TAddEq: AddEq, + impl TCopy: Copy, + impl TDrop: Drop, + impl QCopy: Copy, + impl QDrop: Drop, +>( + a: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor, + min: T, + max: T +) -> Tensor { + let a_shape = *a.shape; + let b_shape = *b.shape; + + let a_ndim = (a_shape).len(); + let b_ndim = (b_shape).len(); + + //! Case: Both tensors are max 2-dimensional + if a_ndim <= 2 && b_ndim <= 2 { + let mut dequantized_a = dequantize_linear(@(*a), a_scale, a_zero_point); + let mut dequantized_b = dequantize_linear(@(*b), b_scale, b_zero_point); + + let mut x = dequantized_a.matmul(@dequantized_b); + + return quantize_linear(@x, y_scale, y_zero_point, min, max); + } + + // (D1, D2, M, K) * (D1, D2, K, N) -> (D1, D2, M, N) + assert(a_ndim == b_ndim, 'dim missmatch'); + let mut dequantized_a = dequantize_linear(@(*a), a_scale, a_zero_point); + let mut dequantized_b = dequantize_linear(@(*b), b_scale, b_zero_point); + let mut x_shape = ArrayTrait::::new(); + let mut x_data = ArrayTrait::::new(); + + assert(a_shape[a_ndim - 1] == b_shape[b_ndim - 2], 'incompatible dim for matmul'); + + let m = *a_shape[a_ndim - 2]; + let k = *a_shape[a_ndim - 1]; + let n = *b_shape[b_ndim - 1]; + + let mut a_shape_reduced = ArrayTrait::::new(); + a_shape_reduced.append(m); + a_shape_reduced.append(k); + + let mut b_shape_reduced = ArrayTrait::::new(); + b_shape_reduced.append(k); + b_shape_reduced.append(n); + + let mut i = 0; + loop { + if i == stride(a_shape) / (m * k) { + break; + }; + + result_updates( + @subtensor(@dequantized_a, i * (m * k), a_shape_reduced.span()), + @subtensor(@dequantized_b, i * (k * n), b_shape_reduced.span()), + ref x_data + ); + i += 1; + }; + x_shape(ref x_shape, a_shape, m, n); + let x = TensorTrait::new(x_shape.span(), x_data.span()); + return quantize_linear(@x, y_scale, y_zero_point, min, max); +} + +fn x_shape(ref x_data: Array, mut shape: Span, m: usize, n: usize) { + loop { + if shape.len() == 2 { + break; + } + match shape.pop_front() { + Option::Some(elem) => { + x_data.append(*elem); + }, + Option::None(_) => { + break; + } + }; + }; + x_data.append(m); + x_data.append(n); +} + +fn stride(mut shape: Span) -> usize { + let shape_len = shape.len(); + assert(shape_len > 0, 'shape cannot be empty'); + + let mut accumulated: usize = 1; + loop { + match shape.pop_back() { + Option::Some(i) => { + accumulated *= *i; + }, + Option::None(_) => { + break; + } + }; + }; + return accumulated; +} + +fn subtensor< + T, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop +>( + x: @Tensor, start: usize, shape: Span +) -> Tensor:: { + let mut data = ArrayTrait::::new(); + let mut stride = stride(shape); + let mut i = 0; + + loop { + if i == stride { + break; + } + data.append(*x.data[start + i]); + i += 1; + }; + return TensorTrait::new(shape, data.span()); +} + + +fn result_updates< + T, + MAG, + impl TTensor: TensorTrait, + impl TNumber: NumberTrait, + impl TMul: Mul, + impl TAddEq: AddEq, + impl TCopy: Copy, + impl TDrop: Drop +>( + mat1: @Tensor, mat2: @Tensor, ref result_data: Array +) { + let m = *mat1.shape[0]; + let n = *mat1.shape[1]; + let p = *mat2.shape[1]; + + let mat1 = *mat1.data; + let mat2 = *mat2.data; + + let mut result_shape = ArrayTrait::new(); + result_shape.append(m); + result_shape.append(p); + + let mut i = 0_usize; + loop { + if i == m { + break (); + } + + let mut j = 0_usize; + loop { + if j == p { + break (); + } + + let mut sum: T = NumberTrait::zero(); + let mut k = 0_usize; + loop { + if k == n { + break (); + } + + let mat1_index = i * n + k; + let mat2_index = k * p + j; + sum += *mat1[mat1_index] * *mat2[mat2_index]; + + k += 1; + }; + + result_data.append(sum); + j += 1; + }; + + i += 1; + }; +} diff --git a/tests/operators/qlinearmatmul_test.cairo b/tests/operators/qlinearmatmul_test.cairo new file mode 100644 index 000000000..4229de666 --- /dev/null +++ b/tests/operators/qlinearmatmul_test.cairo @@ -0,0 +1,195 @@ +use debug::PrintTrait; +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{ + TensorTrait, Tensor, I8Tensor, I32Tensor, U32Tensor, FP16x16Tensor, FP32x32Tensor +}; +use orion::numbers::{FP16x16, FP16x16Impl, FP32x32, FP32x32Impl, FixedTrait}; +use orion::numbers::{NumberTrait, IntegerTrait}; +use orion::numbers::{i8, i32}; + + +#[test] +#[available_gas(200000000000)] +fn qlinearmatmul_2D_test() { + let a = TensorTrait::::new( + shape: array![2, 4].span(), + data: array![ + IntegerTrait::::new(1_u8, true), + IntegerTrait::::new(2_u8, true), + IntegerTrait::::new(3_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(5_u8, true), + IntegerTrait::::new(6_u8, true), + IntegerTrait::::new(7_u8, true), + IntegerTrait::::new(8_u8, true) + ] + .span(), + ); + let b = TensorTrait::::new( + shape: array![4, 3].span(), + data: array![ + IntegerTrait::::new(2_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(6_u8, true), + IntegerTrait::::new(8_u8, true), + IntegerTrait::::new(10_u8, true), + IntegerTrait::::new(12_u8, true), + IntegerTrait::::new(14_u8, true), + IntegerTrait::::new(16_u8, true), + IntegerTrait::::new(18_u8, true), + IntegerTrait::::new(20_u8, true), + IntegerTrait::::new(22_u8, true), + IntegerTrait::::new(24_u8, true) + ] + .span(), + ); + + let a_scale = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(2000, false)].span(), + ); + let a_zero_point = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), + ); + let b_scale = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(2500, false)].span(), + ); + let b_zero_point = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), + ); + + let y_scale = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(3000, false)].span(), + ); + let y_zero_point = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), + ); + + let actual_output = a + .qlinear_matmul( + @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point + ); + let expected_output = TensorTrait::::new( + shape: array![2, 3].span(), + data: array![ + IntegerTrait::::new(3_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(8_u8, true), + IntegerTrait::::new(9_u8, true), + IntegerTrait::::new(10_u8, true) + ] + .span(), + ); + + assert((*actual_output.data[0]).into() == 3, '*result[0] == 3'); + assert((*actual_output.data[1]).into() == 4, '*result[1] == 4'); + assert((*actual_output.data[2]).into() == 4, '*result[2] == 4'); + assert((*actual_output.data[3]).into() == 8, '*result[3] == 8'); + assert((*actual_output.data[4]).into() == 9, '*result[4] == 9'); + assert((*actual_output.data[5]).into() == 10, '*result[5] == 10'); + +} + + +#[test] +#[available_gas(200000000000)] +fn qlinearmatmul_3D_test() { + let a = TensorTrait::::new( + shape: array![2, 2, 3].span(), + data: array![ + IntegerTrait::::new(1_u8, true), + IntegerTrait::::new(2_u8, true), + IntegerTrait::::new(2_u8, true), + IntegerTrait::::new(3_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(5_u8, true), + IntegerTrait::::new(6_u8, true), + IntegerTrait::::new(6_u8, true), + IntegerTrait::::new(7_u8, true), + IntegerTrait::::new(8_u8, true), + IntegerTrait::::new(8_u8, true) + ] + .span(), + ); + let b = TensorTrait::::new( + shape: array![2, 3, 2].span(), + data: array![ + IntegerTrait::::new(2_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(6_u8, true), + IntegerTrait::::new(8_u8, true), + IntegerTrait::::new(10_u8, true), + IntegerTrait::::new(12_u8, true), + IntegerTrait::::new(2_u8, true), + IntegerTrait::::new(4_u8, true), + IntegerTrait::::new(6_u8, true), + IntegerTrait::::new(8_u8, true), + IntegerTrait::::new(10_u8, true), + IntegerTrait::::new(12_u8, true) + ] + .span(), + ); + + let a_scale = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(20000, false)].span(), + ); + let a_zero_point = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), + ); + let b_scale = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(25000, false)].span(), + ); + let b_zero_point = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), + ); + + let y_scale = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(30000, false)].span(), + ); + let y_zero_point = TensorTrait::::new( + shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), + ); + + let actual_output = a + .qlinear_matmul( + @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point + ); + let expected_output = TensorTrait::::new( + shape: array![2, 2, 2].span(), + data: array![ + IntegerTrait::::new(8_u8, true), + IntegerTrait::::new(11_u8, true), + IntegerTrait::::new(17_u8, true), + IntegerTrait::::new(23_u8, true), + IntegerTrait::::new(26_u8, true), + IntegerTrait::::new(35_u8, true), + IntegerTrait::::new(36_u8, true), + IntegerTrait::::new(47_u8, true) + ] + .span(), + ); + assert((*actual_output.data[0]).into() == 8, '*result[0] == 8'); + assert((*actual_output.data[1]).into() == 11, '*result[1] == 11'); + assert((*actual_output.data[2]).into() == 17, '*result[2] == 17'); + assert((*actual_output.data[3]).into() == 23, '*result[3] == 23'); + assert((*actual_output.data[4]).into() == 26, '*result[4] == 26'); + assert((*actual_output.data[5]).into() == 35, '*result[5] == 35'); + assert((*actual_output.data[6]).into() == 36, '*result[6] == 36'); + assert((*actual_output.data[7]).into() == 47, '*result[7] == 47'); + +} + +fn print_span(mut span: Span) { + loop { + match span.pop_front() { + Option::Some(i) => { + (*i.mag).print(); + }, + Option::None(_) => { + break; + } + }; + }; +} From c6fcdb61210dee9e4fd5ac6eb2dcdd4fff727dc2 Mon Sep 17 00:00:00 2001 From: chachaleo Date: Tue, 31 Oct 2023 17:37:56 +0700 Subject: [PATCH 11/21] fix: format --- src/operators/tensor/core.cairo | 9 +- src/operators/tensor/helpers.cairo | 24 ++-- .../implementations/tensor_fp16x16.cairo | 9 +- .../implementations/tensor_fp32x32.cairo | 9 +- .../implementations/tensor_fp64x64.cairo | 11 +- .../implementations/tensor_fp8x23.cairo | 9 +- .../tensor/implementations/tensor_i32.cairo | 9 +- .../tensor/implementations/tensor_i8.cairo | 13 ++- .../tensor/implementations/tensor_u32.cairo | 9 +- src/operators/tensor/quantization.cairo | 2 +- .../tensor/quantization/qlinear_matmul.cairo | 23 +--- tests/operators/qlinearmatmul_test.cairo | 106 +++++++++--------- 12 files changed, 137 insertions(+), 96 deletions(-) diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 6b12ee5a7..7db84915c 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -2440,7 +2440,14 @@ trait TensorTrait { /// u32 tensor, not supported. /// fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor::; /// # tensor.slice /// diff --git a/src/operators/tensor/helpers.cairo b/src/operators/tensor/helpers.cairo index c13459513..471ec7e4e 100644 --- a/src/operators/tensor/helpers.cairo +++ b/src/operators/tensor/helpers.cairo @@ -261,25 +261,17 @@ fn broadcast_shape(mut shape1: Span, mut shape2: Span) -> Span { - dim1 = *item; - }, - Option::None(_) => { - if shape1.len() == 0 && shape2.len() == 0 { - break (); - }; - } + Option::Some(item) => { dim1 = *item; }, + Option::None(_) => { if shape1.len() == 0 && shape2.len() == 0 { + break (); + }; } }; match shape2.pop_front() { - Option::Some(item) => { - dim2 = *item; - }, - Option::None(_) => { - if shape1.len() == 0 && shape2.len() == 0 { - break (); - }; - } + Option::Some(item) => { dim2 = *item; }, + Option::None(_) => { if shape1.len() == 0 && shape2.len() == 0 { + break (); + }; } }; let broadcasted_dim = u32_max(dim1, dim2); diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index 518a44973..b0663a391 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -199,7 +199,14 @@ impl FP16x16Tensor of TensorTrait { } fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor:: { quantization::qlinear_matmul::qlinear_matmul( self, diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index a3b6c1e72..5180ef25c 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -200,7 +200,14 @@ impl FP32x32Tensor of TensorTrait { } fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor:: { quantization::qlinear_matmul::qlinear_matmul( self, diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index 6a802b67d..0915f50de 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -200,7 +200,14 @@ impl FP64x64Tensor of TensorTrait { } fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor:: { quantization::qlinear_matmul::qlinear_matmul( self, @@ -215,7 +222,7 @@ impl FP64x64Tensor of TensorTrait { NumberTrait::new_unscaled(127, false) ) } - + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 0bf738fa8..c0d5857f2 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -199,7 +199,14 @@ impl FP8x23Tensor of TensorTrait { } fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor:: { quantization::qlinear_matmul::qlinear_matmul( self, diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index c5d349a5c..2e95da7a8 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -200,7 +200,14 @@ impl I32Tensor of TensorTrait { } fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor:: { quantization::qlinear_matmul::qlinear_matmul( self, diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index 438964f30..8162e705d 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -196,10 +196,17 @@ impl I8Tensor of TensorTrait { self: @Tensor, x_scale: @Tensor, x_zero_point: @Tensor ) -> Tensor:: { quantization::dequantize_linear::dequantize_linear(self, x_scale, x_zero_point) - } - + } + fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor:: { quantization::qlinear_matmul::qlinear_matmul( self, diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 4ec329c69..06f32636c 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -193,7 +193,14 @@ impl U32Tensor of TensorTrait { } fn qlinear_matmul( - self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor ) -> Tensor:: { panic(array!['not supported!']) } diff --git a/src/operators/tensor/quantization.cairo b/src/operators/tensor/quantization.cairo index 62b428766..88baf9e3f 100644 --- a/src/operators/tensor/quantization.cairo +++ b/src/operators/tensor/quantization.cairo @@ -1,3 +1,3 @@ mod quantize_linear; mod dequantize_linear; -mod qlinear_matmul; \ No newline at end of file +mod qlinear_matmul; diff --git a/src/operators/tensor/quantization/qlinear_matmul.cairo b/src/operators/tensor/quantization/qlinear_matmul.cairo index 5409b8a11..b9d734a9b 100644 --- a/src/operators/tensor/quantization/qlinear_matmul.cairo +++ b/src/operators/tensor/quantization/qlinear_matmul.cairo @@ -106,12 +106,8 @@ fn x_shape(ref x_data: Array, mut shape: Span, m: usize, n: usize) break; } match shape.pop_front() { - Option::Some(elem) => { - x_data.append(*elem); - }, - Option::None(_) => { - break; - } + Option::Some(elem) => { x_data.append(*elem); }, + Option::None(_) => { break; } }; }; x_data.append(m); @@ -125,23 +121,14 @@ fn stride(mut shape: Span) -> usize { let mut accumulated: usize = 1; loop { match shape.pop_back() { - Option::Some(i) => { - accumulated *= *i; - }, - Option::None(_) => { - break; - } + Option::Some(i) => { accumulated *= *i; }, + Option::None(_) => { break; } }; }; return accumulated; } -fn subtensor< - T, - impl TTensor: TensorTrait, - impl TCopy: Copy, - impl TDrop: Drop ->( +fn subtensor, impl TCopy: Copy, impl TDrop: Drop>( x: @Tensor, start: usize, shape: Span ) -> Tensor:: { let mut data = ArrayTrait::::new(); diff --git a/tests/operators/qlinearmatmul_test.cairo b/tests/operators/qlinearmatmul_test.cairo index 4229de666..555555eaf 100644 --- a/tests/operators/qlinearmatmul_test.cairo +++ b/tests/operators/qlinearmatmul_test.cairo @@ -12,7 +12,9 @@ use orion::numbers::{i8, i32}; #[test] #[available_gas(200000000000)] fn qlinearmatmul_2D_test() { - let a = TensorTrait::::new( + let a = TensorTrait::< + i8 + >::new( shape: array![2, 4].span(), data: array![ IntegerTrait::::new(1_u8, true), @@ -26,7 +28,9 @@ fn qlinearmatmul_2D_test() { ] .span(), ); - let b = TensorTrait::::new( + let b = TensorTrait::< + i8 + >::new( shape: array![4, 3].span(), data: array![ IntegerTrait::::new(2_u8, true), @@ -45,31 +49,33 @@ fn qlinearmatmul_2D_test() { .span(), ); - let a_scale = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(2000, false)].span(), - ); - let a_zero_point = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), - ); - let b_scale = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(2500, false)].span(), - ); - let b_zero_point = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), - ); + let a_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(2000, false)].span(),); + let a_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); + let b_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(2500, false)].span(),); + let b_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); - let y_scale = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(3000, false)].span(), - ); - let y_zero_point = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), - ); + let y_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(3000, false)].span(),); + let y_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); let actual_output = a .qlinear_matmul( @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point ); - let expected_output = TensorTrait::::new( + let expected_output = TensorTrait::< + i8 + >::new( shape: array![2, 3].span(), data: array![ IntegerTrait::::new(3_u8, true), @@ -88,14 +94,15 @@ fn qlinearmatmul_2D_test() { assert((*actual_output.data[3]).into() == 8, '*result[3] == 8'); assert((*actual_output.data[4]).into() == 9, '*result[4] == 9'); assert((*actual_output.data[5]).into() == 10, '*result[5] == 10'); - } #[test] #[available_gas(200000000000)] fn qlinearmatmul_3D_test() { - let a = TensorTrait::::new( + let a = TensorTrait::< + i8 + >::new( shape: array![2, 2, 3].span(), data: array![ IntegerTrait::::new(1_u8, true), @@ -113,7 +120,9 @@ fn qlinearmatmul_3D_test() { ] .span(), ); - let b = TensorTrait::::new( + let b = TensorTrait::< + i8 + >::new( shape: array![2, 3, 2].span(), data: array![ IntegerTrait::::new(2_u8, true), @@ -132,31 +141,33 @@ fn qlinearmatmul_3D_test() { .span(), ); - let a_scale = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(20000, false)].span(), - ); - let a_zero_point = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), - ); - let b_scale = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(25000, false)].span(), - ); - let b_zero_point = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), - ); + let a_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(20000, false)].span(),); + let a_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); + let b_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(25000, false)].span(),); + let b_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); - let y_scale = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(30000, false)].span(), - ); - let y_zero_point = TensorTrait::::new( - shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(), - ); + let y_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(30000, false)].span(),); + let y_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); let actual_output = a .qlinear_matmul( @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point ); - let expected_output = TensorTrait::::new( + let expected_output = TensorTrait::< + i8 + >::new( shape: array![2, 2, 2].span(), data: array![ IntegerTrait::::new(8_u8, true), @@ -178,18 +189,13 @@ fn qlinearmatmul_3D_test() { assert((*actual_output.data[5]).into() == 35, '*result[5] == 35'); assert((*actual_output.data[6]).into() == 36, '*result[6] == 36'); assert((*actual_output.data[7]).into() == 47, '*result[7] == 47'); - } fn print_span(mut span: Span) { loop { match span.pop_front() { - Option::Some(i) => { - (*i.mag).print(); - }, - Option::None(_) => { - break; - } + Option::Some(i) => { (*i.mag).print(); }, + Option::None(_) => { break; } }; }; } From ab4230a52eb8dbd392e7d55275e40b3c964487b3 Mon Sep 17 00:00:00 2001 From: raphaelDkhn Date: Tue, 31 Oct 2023 12:39:28 +0200 Subject: [PATCH 12/21] Update .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index b69dbeef7..5a0c79267 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ target __pycache__ neural_network/.venv + +Scarb.lock \ No newline at end of file From 759b964f1d87d0eceacc483ac25da6d251b00c0b Mon Sep 17 00:00:00 2001 From: raphaelDkhn Date: Tue, 31 Oct 2023 12:44:00 +0200 Subject: [PATCH 13/21] Delete Scarb.lock --- Scarb.lock | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 Scarb.lock diff --git a/Scarb.lock b/Scarb.lock deleted file mode 100644 index 85e5484e6..000000000 --- a/Scarb.lock +++ /dev/null @@ -1,20 +0,0 @@ -# Code generated by scarb DO NOT EDIT. -version = 1 - -[[package]] -name = "alexandria_data_structures" -version = "0.1.0" -source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=f37d73d#f37d73d8a8248e4d8dc65de3949333e30bda022f" - -[[package]] -name = "cubit" -version = "1.2.0" -source = "git+https://github.com/raphaelDkhn/cubit.git#e6331ebf98c5d5f442a0e5edefe0b367c8e270d9" - -[[package]] -name = "orion" -version = "0.1.2" -dependencies = [ - "alexandria_data_structures", - "cubit", -] From 986fa066cce34ac7b672c471bdd83520ba174102 Mon Sep 17 00:00:00 2001 From: raphaelDkhn Date: Tue, 31 Oct 2023 12:45:36 +0200 Subject: [PATCH 14/21] Update nodes.cairo --- tests/nodes.cairo | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/nodes.cairo b/tests/nodes.cairo index 38dfca67f..1d27a00f8 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -484,22 +484,6 @@ mod min_u32_three_tensors; mod min_u32_broadcast_three_tensors; mod min_u32_two_tensors; mod min_u32_broadcast_two_tensors; -mod thresholded_relu_fp16x16; -mod thresholded_relu_fp8x23; -mod hard_sigmoid_fp8x23; -mod hard_sigmoid_fp16x16; -mod neg_fp16x16; -mod neg_fp8x23; -mod neg_i32; -mod neg_i8; -mod gemm_all_attributes; -mod gemm_alpha; -mod gemm_beta; -mod gemm_default_matrix_bias; -mod gemm_default_vector_bias; -mod gemm_default_no_bias; -mod gemm_transposeA; -mod gemm_transposeB; mod where_fp16x16; mod where_fp16x16_broadcast; mod where_fp8x23; From 14fadf2edee8a8248703bc4f88c8aaba62d3137d Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Tue, 31 Oct 2023 11:20:38 +0000 Subject: [PATCH 15/21] docs: update README.md [skip ci] --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e547c851..a89893233 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ # Orion: An Open-source Framework for Validity and ZK ML ✨ -[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors-) +[![All Contributors](https://img.shields.io/badge/all_contributors-21-orange.svg?style=flat-square)](#contributors-) Orion is an open-source, community-driven framework dedicated to Provable Machine Learning. It provides essential components and a new ONNX runtime for building verifiable Machine Learning models using [STARKs](https://starkware.co/stark/). @@ -93,6 +93,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d 0xfulanito
0xfulanito

💻 0x73e
0x73e

💻 Thomas S. Bauer
Thomas S. Bauer

💻 + Andres
Andres

💻 From 4603416f28817ac128f2f121ede41d29b538ca78 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Tue, 31 Oct 2023 11:20:38 +0000 Subject: [PATCH 16/21] docs: update .all-contributorsrc [skip ci] --- .all-contributorsrc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 6e798d849..84be25e8a 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -188,6 +188,15 @@ "contributions": [ "code" ] + }, + { + "login": "andresmayorca", + "name": "Andres", + "avatar_url": "https://avatars.githubusercontent.com/u/70079260?v=4", + "profile": "http://andresmayorca.github.io", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, From 7fa67c7696234b80b155bd9696a440e09fa5eccb Mon Sep 17 00:00:00 2001 From: chachaleo Date: Wed, 1 Nov 2023 11:59:30 +0700 Subject: [PATCH 17/21] fix: implem qmatmul for wide tensors --- Scarb.lock | 2 +- .../tensor/implementations/tensor_fp16x16wide.cairo | 13 +++++++++++++ .../tensor/implementations/tensor_fp8x23wide.cairo | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Scarb.lock b/Scarb.lock index 85e5484e6..65dbec62d 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -13,7 +13,7 @@ source = "git+https://github.com/raphaelDkhn/cubit.git#e6331ebf98c5d5f442a0e5ede [[package]] name = "orion" -version = "0.1.2" +version = "0.1.5" dependencies = [ "alexandria_data_structures", "cubit", diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index c4f2de169..d28fec8ec 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -204,6 +204,19 @@ impl FP16x16WTensor of TensorTrait { panic(array!['not supported!']) } + fn qlinear_matmul( + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + fn slice( self: @Tensor, starts: Span, diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index f594eec4d..6370e1461 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -198,6 +198,19 @@ impl FP8x23WTensor of TensorTrait { panic(array!['not supported!']) } + fn qlinear_matmul( + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + fn slice( self: @Tensor, starts: Span, From 7bed25b2d7f5a1fddd9d12c3ffe281e4ca454347 Mon Sep 17 00:00:00 2001 From: Hakeem Kazeem Date: Fri, 3 Nov 2023 11:00:28 +0100 Subject: [PATCH 18/21] Scatter Elements Operator --- docs/CHANGELOG.md | 6 + docs/SUMMARY.md | 1 + docs/framework/compatibility.md | 3 +- docs/framework/operators/tensor/README.md | 1 + docs/framework/operators/tensor/tensor.min.md | 2 +- .../operators/tensor/tensor.scatter.md | 62 +++ .../operators/tensor/tensor.where.md | 2 +- nodegen/node/scatter.py | 439 ++++++++++++++++++ src/operators/tensor/core.cairo | 65 +++ .../implementations/tensor_fp16x16.cairo | 6 + .../implementations/tensor_fp16x16wide.cairo | 6 + .../implementations/tensor_fp32x32.cairo | 6 + .../implementations/tensor_fp64x64.cairo | 7 + .../implementations/tensor_fp8x23.cairo | 6 + .../implementations/tensor_fp8x23wide.cairo | 6 + .../tensor/implementations/tensor_i32.cairo | 6 + .../tensor/implementations/tensor_i8.cairo | 6 + .../tensor/implementations/tensor_u32.cairo | 6 + src/operators/tensor/math.cairo | 3 +- src/operators/tensor/math/scatter.cairo | 275 +++++++++++ tests/nodes.cairo | 12 + tests/nodes/scatter_fp16x16_3d_axis1.cairo | 24 + .../scatter_fp16x16_3d_axis1/input_0.cairo | 23 + .../scatter_fp16x16_3d_axis1/input_1.cairo | 23 + .../scatter_fp16x16_3d_axis1/input_2.cairo | 21 + .../scatter_fp16x16_3d_axis1/output_0.cairo | 23 + .../nodes/scatter_fp16x16_3d_axis1_add.cairo | 24 + .../input_0.cairo | 23 + .../input_1.cairo | 23 + .../input_2.cairo | 21 + .../output_0.cairo | 23 + tests/nodes/scatter_fp16x16_3d_default.cairo | 24 + .../scatter_fp16x16_3d_default/input_0.cairo | 23 + .../scatter_fp16x16_3d_default/input_1.cairo | 23 + .../scatter_fp16x16_3d_default/input_2.cairo | 21 + .../scatter_fp16x16_3d_default/output_0.cairo | 23 + tests/nodes/scatter_fp8x23_axis1.cairo | 24 + .../nodes/scatter_fp8x23_axis1/input_0.cairo | 23 + .../nodes/scatter_fp8x23_axis1/input_1.cairo | 23 + .../nodes/scatter_fp8x23_axis1/input_2.cairo | 21 + .../nodes/scatter_fp8x23_axis1/output_0.cairo | 23 + tests/nodes/scatter_fp8x23_default.cairo | 24 + .../scatter_fp8x23_default/input_0.cairo | 23 + .../scatter_fp8x23_default/input_1.cairo | 23 + .../scatter_fp8x23_default/input_2.cairo | 21 + .../scatter_fp8x23_default/output_0.cairo | 23 + tests/nodes/scatter_fp8x23_mul.cairo | 24 + tests/nodes/scatter_fp8x23_mul/input_0.cairo | 23 + tests/nodes/scatter_fp8x23_mul/input_1.cairo | 23 + tests/nodes/scatter_fp8x23_mul/input_2.cairo | 21 + tests/nodes/scatter_fp8x23_mul/output_0.cairo | 23 + tests/nodes/scatter_i8_axis1.cairo | 24 + tests/nodes/scatter_i8_axis1/input_0.cairo | 22 + tests/nodes/scatter_i8_axis1/input_1.cairo | 22 + tests/nodes/scatter_i8_axis1/input_2.cairo | 21 + tests/nodes/scatter_i8_axis1/output_0.cairo | 22 + tests/nodes/scatter_i8_axis1_max.cairo | 24 + .../nodes/scatter_i8_axis1_max/input_0.cairo | 22 + .../nodes/scatter_i8_axis1_max/input_1.cairo | 22 + .../nodes/scatter_i8_axis1_max/input_2.cairo | 21 + .../nodes/scatter_i8_axis1_max/output_0.cairo | 22 + tests/nodes/scatter_i8_default.cairo | 24 + tests/nodes/scatter_i8_default/input_0.cairo | 22 + tests/nodes/scatter_i8_default/input_1.cairo | 22 + tests/nodes/scatter_i8_default/input_2.cairo | 21 + tests/nodes/scatter_i8_default/output_0.cairo | 22 + tests/nodes/scatter_u32_add.cairo | 24 + tests/nodes/scatter_u32_add/input_0.cairo | 21 + tests/nodes/scatter_u32_add/input_1.cairo | 21 + tests/nodes/scatter_u32_add/input_2.cairo | 21 + tests/nodes/scatter_u32_add/output_0.cairo | 21 + tests/nodes/scatter_u32_axis1.cairo | 24 + tests/nodes/scatter_u32_axis1/input_0.cairo | 21 + tests/nodes/scatter_u32_axis1/input_1.cairo | 21 + tests/nodes/scatter_u32_axis1/input_2.cairo | 21 + tests/nodes/scatter_u32_axis1/output_0.cairo | 21 + tests/nodes/scatter_u32_default.cairo | 24 + tests/nodes/scatter_u32_default/input_0.cairo | 21 + tests/nodes/scatter_u32_default/input_1.cairo | 21 + tests/nodes/scatter_u32_default/input_2.cairo | 21 + .../nodes/scatter_u32_default/output_0.cairo | 21 + 81 files changed, 2263 insertions(+), 4 deletions(-) create mode 100644 docs/framework/operators/tensor/tensor.scatter.md create mode 100644 nodegen/node/scatter.py create mode 100644 src/operators/tensor/math/scatter.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1/input_0.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1/input_1.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1/input_2.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1/output_0.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1_add.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1_add/input_0.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1_add/input_1.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1_add/input_2.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_axis1_add/output_0.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_default.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_default/input_0.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_default/input_1.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_default/input_2.cairo create mode 100644 tests/nodes/scatter_fp16x16_3d_default/output_0.cairo create mode 100644 tests/nodes/scatter_fp8x23_axis1.cairo create mode 100644 tests/nodes/scatter_fp8x23_axis1/input_0.cairo create mode 100644 tests/nodes/scatter_fp8x23_axis1/input_1.cairo create mode 100644 tests/nodes/scatter_fp8x23_axis1/input_2.cairo create mode 100644 tests/nodes/scatter_fp8x23_axis1/output_0.cairo create mode 100644 tests/nodes/scatter_fp8x23_default.cairo create mode 100644 tests/nodes/scatter_fp8x23_default/input_0.cairo create mode 100644 tests/nodes/scatter_fp8x23_default/input_1.cairo create mode 100644 tests/nodes/scatter_fp8x23_default/input_2.cairo create mode 100644 tests/nodes/scatter_fp8x23_default/output_0.cairo create mode 100644 tests/nodes/scatter_fp8x23_mul.cairo create mode 100644 tests/nodes/scatter_fp8x23_mul/input_0.cairo create mode 100644 tests/nodes/scatter_fp8x23_mul/input_1.cairo create mode 100644 tests/nodes/scatter_fp8x23_mul/input_2.cairo create mode 100644 tests/nodes/scatter_fp8x23_mul/output_0.cairo create mode 100644 tests/nodes/scatter_i8_axis1.cairo create mode 100644 tests/nodes/scatter_i8_axis1/input_0.cairo create mode 100644 tests/nodes/scatter_i8_axis1/input_1.cairo create mode 100644 tests/nodes/scatter_i8_axis1/input_2.cairo create mode 100644 tests/nodes/scatter_i8_axis1/output_0.cairo create mode 100644 tests/nodes/scatter_i8_axis1_max.cairo create mode 100644 tests/nodes/scatter_i8_axis1_max/input_0.cairo create mode 100644 tests/nodes/scatter_i8_axis1_max/input_1.cairo create mode 100644 tests/nodes/scatter_i8_axis1_max/input_2.cairo create mode 100644 tests/nodes/scatter_i8_axis1_max/output_0.cairo create mode 100644 tests/nodes/scatter_i8_default.cairo create mode 100644 tests/nodes/scatter_i8_default/input_0.cairo create mode 100644 tests/nodes/scatter_i8_default/input_1.cairo create mode 100644 tests/nodes/scatter_i8_default/input_2.cairo create mode 100644 tests/nodes/scatter_i8_default/output_0.cairo create mode 100644 tests/nodes/scatter_u32_add.cairo create mode 100644 tests/nodes/scatter_u32_add/input_0.cairo create mode 100644 tests/nodes/scatter_u32_add/input_1.cairo create mode 100644 tests/nodes/scatter_u32_add/input_2.cairo create mode 100644 tests/nodes/scatter_u32_add/output_0.cairo create mode 100644 tests/nodes/scatter_u32_axis1.cairo create mode 100644 tests/nodes/scatter_u32_axis1/input_0.cairo create mode 100644 tests/nodes/scatter_u32_axis1/input_1.cairo create mode 100644 tests/nodes/scatter_u32_axis1/input_2.cairo create mode 100644 tests/nodes/scatter_u32_axis1/output_0.cairo create mode 100644 tests/nodes/scatter_u32_default.cairo create mode 100644 tests/nodes/scatter_u32_default/input_0.cairo create mode 100644 tests/nodes/scatter_u32_default/input_1.cairo create mode 100644 tests/nodes/scatter_u32_default/input_2.cairo create mode 100644 tests/nodes/scatter_u32_default/output_0.cairo diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index fd3cfa146..be5423456 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] - 2023-11-03 + +## Added +- Scatter Elements Operator. + ## [Unreleased] - 2023-09-27 ## Added diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index ea9613ce3..392314d11 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -93,6 +93,7 @@ * [tensor.and](framework/operators/tensor/tensor.and.md) * [tensor.where](framework/operators/tensor/tensor.where.md) * [tensor.round](framework/operators/tensor/tensor.round.md) + * [tensor.scatter](framework/operators/tensor/tensor.scatter.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) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index d6dd0b35f..539427782 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -72,5 +72,6 @@ You can see below the list of current supported ONNX Operators: | [Round](operators/tensor/tensor.round.md) | :white\_check\_mark: | | [MaxInTensor](operators/tensor/tensor.max\_in\_tensor.md) | :white\_check\_mark: | | [Max](operators/tensor/tensor.max.md) | :white\_check\_mark: | +| [Scatter](operators/tensor/scatter.max.md) | :white\_check\_mark: | -Current Operators support: **61/156 (39%)** +Current Operators support: **62/156 (39%)** diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index 48b1650ad..747a89f60 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -89,6 +89,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.identity`](tensor.identity.md) | Return a Tensor with the same shape and contents as input. | | [`tensor.where`](tensor.where.md) | Return elements chosen from x or y depending on condition. | | [`tensor.round`](tensor.round.md) | Computes the round value of all elements in the input tensor. | +| [`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. | ## Arithmetic Operations diff --git a/docs/framework/operators/tensor/tensor.min.md b/docs/framework/operators/tensor/tensor.min.md index 50c45e8ad..31157c2dc 100644 --- a/docs/framework/operators/tensor/tensor.min.md +++ b/docs/framework/operators/tensor/tensor.min.md @@ -19,8 +19,8 @@ A new `Tensor` containing the element-wise minimum values ## Panics +* Panics if tensor array is empty * Panics if the shapes are not equal or broadcastable -* Panics if tensor array length is not >= 1 ## Examples diff --git a/docs/framework/operators/tensor/tensor.scatter.md b/docs/framework/operators/tensor/tensor.scatter.md new file mode 100644 index 000000000..2420f2ddf --- /dev/null +++ b/docs/framework/operators/tensor/tensor.scatter.md @@ -0,0 +1,62 @@ +# tensor.scatter + +```rust + fn scatter(self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) -> Tensor; +``` + +Produces a copy of input data, and updates value to values specified by updates at specific index positions specified by indices. + +## Args + +* `self`(`@Tensor`) - The input tensor. +* `updates`(`Tensor`) - The inupdates tensor. +* `indices`(`Tensor`) - Tensor of indices. +* `axis`(`Option`) - Axis to scatter on. Default: axis=0. +* `reduction`(`Option`) - Reduction operation. Default: reduction='none'. + +## Panics + +* Panics if index values are not within bounds [-s, s-1] along axis of size s. + +## Returns + +A new `Tensor` . + +## Example + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + +fn scatter_example() -> Tensor { + let tensor = TensorTrait::::new( + shape: array![3, 5].span(), + data: array![[ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0]].span(), + ); + let updates = TensorTrait::::new( + shape: array![3, 3].span(), + data: array![[ 1, 2, 3], + [ 4, 5, 6], + [ 7, 8, 9]].span(), + ); + let indices = TensorTrait::::new( + shape: array![3, 3].span(), + data: array![[ 0, 1, 2], + [ 2, 0, 1], + [ 1, 0, 1]].span(), + ); + + return tensor.scatter( + updates: updates + indices: indices, + axis: Option::None(()), + reduction: Option::None(()), + ); +} +>>> [[ 1, 8, 0, 0, 0], + [ 7, 2, 9, 0, 0], + [ 4, 0, 3, 0, 0]] +``` diff --git a/docs/framework/operators/tensor/tensor.where.md b/docs/framework/operators/tensor/tensor.where.md index 02d3b0059..187f12ecb 100644 --- a/docs/framework/operators/tensor/tensor.where.md +++ b/docs/framework/operators/tensor/tensor.where.md @@ -37,7 +37,7 @@ fn where_example() -> Tensor { let tensor_x = TensorTrait::::new( shape: array![2, 2].span(), data: array![2, 4, 6, 8].span(), ); - + let tensor_y = TensorTrait::::new( shape: array![2, 2].span(), data: array![1, 3, 5, 9].span(), ); diff --git a/nodegen/node/scatter.py b/nodegen/node/scatter.py new file mode 100644 index 000000000..3e833a840 --- /dev/null +++ b/nodegen/node/scatter.py @@ -0,0 +1,439 @@ +import numpy as np +import torch +from nodegen.node import RunAll +from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl, Trait + +# The below ScatterElements' numpy implementation is from https://stackoverflow.com/a/46204790/11767360 +def scatter_elements(data, indices, updates, axis=0, reduction="none"): # type: ignore + if axis < 0: + axis = data.ndim + axis + + idx_xsection_shape = indices.shape[:axis] + indices.shape[axis + 1 :] + + def make_slice(arr, axis, i): # type: ignore + slc = [slice(None)] * arr.ndim + slc[axis] = i + return slc + + def unpack(packed): # type: ignore + unpacked = packed[0] + for i in range(1, len(packed)): + unpacked = unpacked, packed[i] + return unpacked + + def make_indices_for_duplicate(idx): # type: ignore + final_idx = [] + for i in range(len(idx[0])): + final_idx.append(tuple(idx_element[i] for idx_element in idx)) + return list(final_idx) + + # We use indices and axis parameters to create idx + # idx is in a form that can be used as a NumPy advanced indices for scattering of updates param. in data + idx = [ + [ + unpack(np.indices(idx_xsection_shape).reshape(indices.ndim - 1, -1)), + indices[tuple(make_slice(indices, axis, i))].reshape(1, -1)[0], + ] + for i in range(indices.shape[axis]) + ] + idx = list(np.concatenate(idx, axis=1)) + idx.insert(axis, idx.pop()) + + # updates_idx is a NumPy advanced indices for indexing of elements in the updates + updates_idx = list(idx) + updates_idx.pop(axis) + updates_idx.insert( + axis, np.repeat(np.arange(indices.shape[axis]), np.prod(idx_xsection_shape)) + ) + + scattered = np.copy(data) + if reduction == "none": + scattered[tuple(idx)] = updates[tuple(updates_idx)] + else: + idx, updates_idx = make_indices_for_duplicate(idx), make_indices_for_duplicate( + updates_idx + ) + for iter, idx_set in enumerate(idx): + if reduction == "add": + scattered[idx_set] += updates[updates_idx[iter]] + elif reduction == "mul": + scattered[idx_set] *= updates[updates_idx[iter]] + elif reduction == "max": + scattered[idx_set] = np.maximum( + scattered[idx_set], updates[updates_idx[iter]] + ) + elif reduction == "min": + scattered[idx_set] = np.minimum( + scattered[idx_set], updates[updates_idx[iter]] + ) + return scattered + +class Scatter(RunAll): + + @staticmethod + def scatter_fp16x16(): + + def scatter(): + def default(): + x1 = np.zeros((3, 3)).astype(np.int64) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 0, 'none') + + x1 = Tensor(Dtype.FP16x16, x1.shape, to_fp(x1.flatten(), FixedImpl.FP16x16)) + x2 = Tensor(Dtype.FP16x16, x2.shape, to_fp(x2.flatten(), FixedImpl.FP16x16)) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + + name = "scatter_fp16x16_3d_default" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))", + file_name= name) + + def axis_1(): + x1 = np.zeros((3, 3)).astype(np.int64) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'none') + + x1 = Tensor(Dtype.FP16x16, x1.shape, to_fp(x1.flatten(), FixedImpl.FP16x16)) + x2 = Tensor(Dtype.FP16x16, x2.shape, to_fp(x2.flatten(), FixedImpl.FP16x16)) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + + name = "scatter_fp16x16_3d_axis1" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none'))", + file_name= name) + + def axis_1_add(): + x1 = np.zeros((3, 3)).astype(np.int64) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'add') + + x1 = Tensor(Dtype.FP16x16, x1.shape, to_fp(x1.flatten(), FixedImpl.FP16x16)) + x2 = Tensor(Dtype.FP16x16, x2.shape, to_fp(x2.flatten(), FixedImpl.FP16x16)) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.FP16x16, y.shape, to_fp( + y.flatten(), FixedImpl.FP16x16)) + + name = "scatter_fp16x16_3d_axis1_add" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('add'))", + file_name= name) + + default() + axis_1() + axis_1_add() + scatter() + + @staticmethod + def scatter_fp8x23(): + + def scatter(): + def default(): + + x1 = np.zeros((3, 3)).astype(np.int64) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 0, 'none') + + x1 = Tensor(Dtype.FP8x23, x1.shape, to_fp(x1.flatten(), FixedImpl.FP8x23)) + x2 = Tensor(Dtype.FP8x23, x2.shape, to_fp(x2.flatten(), FixedImpl.FP8x23)) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23)) + + name = "scatter_fp8x23_default" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))", + file_name= name) + + + def axis1(): + x1 = np.zeros((3, 3)).astype(np.int64) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'none') + + x1 = Tensor(Dtype.FP8x23, x1.shape, to_fp(x1.flatten(), FixedImpl.FP8x23)) + x2 = Tensor(Dtype.FP8x23, x2.shape, to_fp(x2.flatten(), FixedImpl.FP8x23)) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23)) + + name = "scatter_fp8x23_axis1" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none'))", + file_name= name) + + def axis1_mul(): + x1 = np.zeros((3, 3)).astype(np.int64) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 0, 'mul') + + x1 = Tensor(Dtype.FP8x23, x1.shape, to_fp(x1.flatten(), FixedImpl.FP8x23)) + x2 = Tensor(Dtype.FP8x23, x2.shape, to_fp(x2.flatten(), FixedImpl.FP8x23)) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23)) + + name = "scatter_fp8x23_mul" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('mul'))", + file_name= name) + + default() + axis1() + axis1_mul() + scatter() + + @staticmethod + def scatter_i8(): + + def scatter_3D(): + def default(): + x1 = np.zeros((3, 3)).astype(np.int8) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int8) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 0, 'none') + + x1 = Tensor(Dtype.I8, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.I8, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + + name = "scatter_i8_default" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))", + file_name= name) + + def axis1(): + x1 = np.zeros((3, 3)).astype(np.int8) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int8) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'none') + + x1 = Tensor(Dtype.I8, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.I8, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + + name = "scatter_i8_axis1" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none'))", + file_name= name) + + + def axis1_max(): + x1 = np.zeros((3, 3)).astype(np.int8) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int8) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'max') + + x1 = Tensor(Dtype.I8, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.I8, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + + name = "scatter_i8_axis1_max" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('max'))", + file_name= name) + + default() + axis1() + axis1_max() + scatter_3D() + + + @staticmethod + def scatter_i32(): + def scatter_3D(): + def default(): + x1 = np.zeros((3, 3)).astype(np.int32) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int32) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 0, 'none') + + x1 = Tensor(Dtype.I32, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.I32, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "scatter_i8_default" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))", + file_name= name) + + def axis1(): + x1 = np.zeros((3, 3)).astype(np.int32) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int32) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'none') + + x1 = Tensor(Dtype.I32, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.I32, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "scatter_i8_axis1" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none'))", + file_name= name) + + def axis_min(): + x1 = np.zeros((3, 3)).astype(np.int32) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int32) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'min') + + x1 = Tensor(Dtype.I32, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.I32, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "scatter_i8_default" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('min'))", + file_name= name) + + default() + axis1() + axis_min() + scatter_3D() + + + @staticmethod + def scatter_u32(): + + def scatter_3D(): + def default(): + x1 = np.zeros((3, 3)).astype(np.uint32) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.uint32) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 0, 'none') + + x1 = Tensor(Dtype.U32, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.U32, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "scatter_u32_default" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))", + file_name= name) + + + def axis1(): + x1 = np.zeros((3, 3)).astype(np.uint32) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.uint32) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 1, 'none') + + x1 = Tensor(Dtype.U32, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.U32, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "scatter_u32_axis1" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none'))", + file_name= name) + + def axis_add(): + x1 = np.zeros((3, 3)).astype(np.uint32) + x2 = np.arange(1, 10).reshape((3, 3)).astype(np.uint32) + x3 = np.array( + [[0,1,2], + [2,0,1], + [1,0,1]], + ) + y = scatter_elements(x1, x3, x2, 0, 'add') + + x1 = Tensor(Dtype.U32, x1.shape, x1.flatten()) + x2 = Tensor(Dtype.U32, x2.shape, x2.flatten()) + x3 = Tensor(Dtype.U32, x3.shape, x3.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "scatter_u32_add" + make_node([x1, x2, x3], [y], name) + make_test( + inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('add'))", + file_name= name) + + default() + axis1() + axis_add() + scatter_3D() \ No newline at end of file diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 44303e8c9..ef05481cb 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -85,6 +85,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde { /// # tensor.new /// @@ -3041,6 +3042,70 @@ trait TensorTrait { /// ``` /// fn round(self: @Tensor) -> Tensor; + /// # tensor.scatter + /// + /// ```rust + /// fn scatter(self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) -> Tensor; + /// ``` + /// + /// Produces a copy of input data, and updates value to values specified by updates at specific index positions specified by indices. + /// + /// ## Args + /// + /// * `self`(`@Tensor`) - The input tensor. + /// * `updates`(`Tensor`) - The inupdates tensor. + /// * `indices`(`Tensor`) - Tensor of indices. + /// * `axis`(`Option`) - Axis to scatter on. Default: axis=0. + /// * `reduction`(`Option`) - Reduction operation. Default: reduction='none'. + /// + /// ## Panics + /// + /// * Panics if index values are not within bounds [-s, s-1] along axis of size s. + /// + /// ## Returns + /// + /// A new `Tensor` . + /// + /// ## Example + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn scatter_example() -> Tensor { + /// let tensor = TensorTrait::::new( + /// shape: array![3, 5].span(), + /// data: array![[ 0, 0, 0, 0, 0], + /// [ 0, 0, 0, 0, 0], + /// [ 0, 0, 0, 0, 0]].span(), + /// ); + /// let updates = TensorTrait::::new( + /// shape: array![3, 3].span(), + /// data: array![[ 1, 2, 3], + /// [ 4, 5, 6], + /// [ 7, 8, 9]].span(), + /// ); + /// let indices = TensorTrait::::new( + /// shape: array![3, 3].span(), + /// data: array![[ 0, 1, 2], + /// [ 2, 0, 1], + /// [ 1, 0, 1]].span(), + /// ); + /// + /// return tensor.scatter( + /// updates: updates + /// indices: indices, + /// axis: Option::None(()), + /// reduction: Option::None(()), + /// ); + /// } + /// >>> [[ 1, 8, 0, 0, 0], + /// [ 7, 2, 9, 0, 0], + /// [ 4, 0, 3, 0, 0]] + /// ``` + /// + fn scatter(self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) -> Tensor; } /// Cf: TensorTrait::new docstring diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index 8a85d5683..838a88c9f 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -258,6 +258,12 @@ impl FP16x16Tensor of TensorTrait { math::round::round(*self) } + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } + } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 26a5c8060..8f6b25eda 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -267,6 +267,12 @@ impl FP16x16WTensor of TensorTrait { fn round(self: @Tensor) -> Tensor { math::round::round(*self) } + + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } } /// 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 e80b2946d..8ecf6f469 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -258,6 +258,12 @@ impl FP32x32Tensor of TensorTrait { fn round(self: @Tensor) -> Tensor { math::round::round(*self) } + + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } } /// 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 80189b701..cbbb12ea4 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -258,6 +258,13 @@ impl FP64x64Tensor of TensorTrait { fn round(self: @Tensor) -> Tensor { math::round::round(*self) } + + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } + } /// 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 98bf543b6..715c71efb 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -257,6 +257,12 @@ impl FP8x23Tensor of TensorTrait { fn round(self: @Tensor) -> Tensor { math::round::round(*self) } + + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index bb40c169b..6f34f4ff5 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -258,6 +258,12 @@ impl FP8x23WTensor of TensorTrait { math::round::round(*self) } + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } + } /// 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 988e81ab5..1140ceb1f 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -256,6 +256,12 @@ impl I32Tensor of TensorTrait { fn round(self: @Tensor) -> Tensor { math::round::round(*self) } + + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } } /// 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 bc1de11ca..5e538e0aa 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -255,6 +255,12 @@ impl I8Tensor of TensorTrait { fn round(self: @Tensor) -> Tensor { math::round::round(*self) } + + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } } /// 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 d5747877d..e4572deaf 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -249,6 +249,12 @@ impl U32Tensor of TensorTrait { fn round(self: @Tensor) -> Tensor { math::round::round(*self) } + + fn scatter( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option) + -> Tensor { + math::scatter::scatter(self, updates, indices, axis, reduction) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index 69473df94..79404dcd5 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -37,4 +37,5 @@ mod sign; mod and; mod neg; mod where; -mod round; \ No newline at end of file +mod round; +mod scatter; \ No newline at end of file diff --git a/src/operators/tensor/math/scatter.cairo b/src/operators/tensor/math/scatter.cairo new file mode 100644 index 000000000..ea26f1b7e --- /dev/null +++ b/src/operators/tensor/math/scatter.cairo @@ -0,0 +1,275 @@ +use alexandria_data_structures::array_ext::SpanTraitExt; +use array::ArrayTrait; +use array::SpanTrait; + +use core::traits::Into; +use debug::PrintTrait; +use core::traits::TryInto; +use core::serde::Serde; +use core::traits::Destruct; +use option::OptionTrait; + +use orion::numbers::NumberTrait; +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; +use dict::Felt252DictTrait; +use nullable::{nullable_from_box, match_nullable, FromNullableResult}; +/// Cf: TensorTrait::scatter docstring +fn scatter< T, impl TTensorTrait: TensorTrait, impl TCopy: Copy, impl TDrop: Drop, impl TAddEq: AddEq, + impl TMulEq: MulEq, impl TPartialOrd: PartialOrd, impl TPartialEq: PartialEq,>( + self: @Tensor, updates: Tensor, indices: Tensor, axis: Option, reduction: Option +) -> Tensor { + let mut axis = match axis { + Option::Some(val) => val, + Option::None(_) => 0 + }; + + let reduction = match reduction { + Option::Some(val) => val, + Option::None(_) => 'none' + }; + + let data_rank = (*self.shape).len(); + let indices_rank = (indices.shape).len(); + let updates_rank = (updates.shape).len(); + + assert((data_rank == updates_rank) & (updates_rank == indices_rank), 'must be same rank'); + let data_shape = *self.shape; + let ind_max = indices.data.max().unwrap(); + assert(ind_max < *data_shape.at(axis), 'index is out of bound'); + + let data_shape = *self.shape; + let mut indices_shape = indices.shape; + let updates_shape = updates.shape; + assert((*indices_shape[0] == *updates_shape[0]) & (*indices_shape[1] == *updates_shape[1]), 'shape must be same'); + + let mut output_data = ArrayTrait::new(); + let mut data_indices = indices.data; + let mut data_updates = updates.data; + let mut indices_updates: Felt252Dict = Default::default(); + let mut indices_updates_reduction: Felt252Dict>> = Default::default(); + + let mut data_shape_copy = data_shape; + let mut indices_shape_copy = indices_shape; + + let data_loop_first = *data_shape_copy.pop_front().unwrap(); + let indices_loop_first = *indices_shape_copy.pop_front().unwrap(); + + let mut indices_loop: usize = 1; + let mut data_loop:usize = 1; + + if (axis == 0) { + loop { + match indices_shape_copy.pop_front() { + Option::Some(val) => { + let d = *val; + indices_loop *= *val; + }, + Option::None(_) => { + break; + } + }; + }; + + loop { + match data_shape_copy.pop_front() { + Option::Some(val) => { + let d = *val; + data_loop *= *val; + }, + Option::None(_) => { + break; + } + }; + }; + } + + let mut transpose = false; + if ((data_rank > 2) & (axis == 1)) { + let index = indices.transpose(axes: array![0, 2, 1].span()); + let update = updates.transpose(axes: array![0, 2, 1].span()); + data_indices = index.data; + data_updates = update.data; + indices_shape = index.shape; + axis = 2; + transpose = true; + } + + if (axis == (data_rank - 1) ){ + data_loop = *data_shape_copy.pop_back().unwrap(); + indices_loop = *indices_shape_copy.pop_back().unwrap(); + } + + let mut total_count: usize = 0; + let mut shift = 0; + + loop { + let mut i: usize = 0; + let mut result:usize = 0; + + match data_indices.pop_front() { + Option::Some(val) => { + let value = total_count+1; + + if (axis == 0) { + let column = total_count % indices_loop; + result = (*val * data_loop) + (column); + if ((result % *data_shape.at(data_rank -1)) != total_count % *indices_shape.at(data_rank -1)){ + result += (*data_shape.at(data_rank -1) - *indices_shape.at(data_rank -1)); + } + } + + if( axis == (data_rank - 1)) { + let mut row = total_count / indices_loop; + if ((data_rank > 2) & (row % *data_shape.at(1) >= *indices_shape.at(1))){ + shift = ( *data_shape.at(1) - *indices_shape.at(1)); + } + + result = *val + (data_loop * (row + shift)); + } + + if (reduction == 'none') { + indices_updates.insert(result.into(), value.into()); + } + else { + let mut arr = ArrayTrait::new(); + + let val = indices_updates_reduction.get(result.into()); + let mut a = ArrayTrait::new(); + let mut span = match match_nullable(val) { + FromNullableResult::Null(()) => a.span(), + FromNullableResult::NotNull(val) => val.unbox(), + }; + + loop { + match span.pop_front() { + Option::Some(val) => { + arr.append(*val); + }, + Option::None(_) => { + break; + } + }; + }; + arr.append(total_count); + indices_updates_reduction.insert(result.into(), nullable_from_box(BoxTrait::new(arr.span()))); + } + total_count += 1; + }, + Option::None(_) => { + break; + } + }; + }; + + let mut data = *self.data; + let mut i: usize = 0; + loop { + match data.pop_front() { + Option::Some(val) => { + if (reduction == 'none') { + let value = indices_updates.get(i.into()); + if (value == 0) { + output_data.append(*val); + } + else { + let data_value = data_updates[value-1]; + output_data.append(*data_value); + } + } + else { + let value = indices_updates_reduction.get(i.into()); + let mut a = ArrayTrait::new(); + let mut span = match match_nullable(value) { + FromNullableResult::Null(()) => a.span(), + FromNullableResult::NotNull(value) => value.unbox(), + }; + + if (span.len() == 0) { + output_data.append(*val); + } + + else { + // let mut result = *data_updates.at(*span.pop_front().unwrap()); + let mut result = *val; + + + if (reduction == 'add') { + loop { + match span.pop_front() { + Option::Some(val) => { + result += *data_updates[*val]; + }, + Option::None(_) => { + break; + } + }; + }; + output_data.append(result); + } + + if (reduction == 'mul') { + loop { + match span.pop_front() { + Option::Some(val) => { + result *= *data_updates[*val]; + }, + Option::None(_) => { + break; + } + }; + }; + output_data.append(result); + } + + if (reduction == 'max') { + loop { + match span.pop_front() { + Option::Some(val) => { + let holder = *data_updates[*val]; + if (holder > result){ + result = holder; + } + }, + Option::None(_) => { + break; + } + }; + }; + output_data.append(result); + } + + if (reduction == 'min') { + loop { + match span.pop_front() { + Option::Some(val) => { + let holder = *data_updates[*val]; + if (holder < result){ + result = holder; + } + }, + Option::None(_) => { + break; + } + }; + }; + output_data.append(result); + } + } + } + + i+=1; + }, + Option::None(_) => { + break; + } + }; + }; + + let mut output_tensor = TensorTrait::::new(*self.shape, output_data.span()); + + if (transpose == true) { + output_tensor = output_tensor.transpose(axes: array![0, 2, 1].span()) + } + + return output_tensor; +} diff --git a/tests/nodes.cairo b/tests/nodes.cairo index 4a6d7c8cb..c0950a35f 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -516,3 +516,15 @@ mod max_u32_three_tensors; mod max_u32_broadcast_three_tensors; mod max_u32_two_tensors; mod max_u32_broadcast_two_tensors; +mod scatter_fp16x16_3d_default; +mod scatter_fp16x16_3d_axis1; +mod scatter_fp16x16_3d_axis1_add; +mod scatter_fp8x23_default; +mod scatter_fp8x23_axis1; +mod scatter_fp8x23_mul; +mod scatter_i8_default; +mod scatter_i8_axis1; +mod scatter_i8_axis1_max; +mod scatter_u32_default; +mod scatter_u32_axis1; +mod scatter_u32_add; \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1.cairo b/tests/nodes/scatter_fp16x16_3d_axis1.cairo new file mode 100644 index 000000000..ddd2ed77a --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_fp16x16_3d_axis1() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1/input_0.cairo b/tests/nodes/scatter_fp16x16_3d_axis1/input_0.cairo new file mode 100644 index 000000000..90579e5d8 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1/input_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1/input_1.cairo b/tests/nodes/scatter_fp16x16_3d_axis1/input_1.cairo new file mode 100644 index 000000000..0b71e00bc --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1/input_1.cairo @@ -0,0 +1,23 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + 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 }); + data.append(FP16x16 { mag: 262144, sign: false }); + data.append(FP16x16 { mag: 327680, sign: false }); + data.append(FP16x16 { mag: 393216, sign: false }); + data.append(FP16x16 { mag: 458752, sign: false }); + data.append(FP16x16 { mag: 524288, sign: false }); + data.append(FP16x16 { mag: 589824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1/input_2.cairo b/tests/nodes/scatter_fp16x16_3d_axis1/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1/output_0.cairo b/tests/nodes/scatter_fp16x16_3d_axis1/output_0.cairo new file mode 100644 index 000000000..e450321a6 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1/output_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + 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 }); + data.append(FP16x16 { mag: 327680, sign: false }); + data.append(FP16x16 { mag: 393216, sign: false }); + data.append(FP16x16 { mag: 262144, sign: false }); + data.append(FP16x16 { mag: 524288, sign: false }); + data.append(FP16x16 { mag: 589824, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1_add.cairo b/tests/nodes/scatter_fp16x16_3d_axis1_add.cairo new file mode 100644 index 000000000..c8027a1a5 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1_add.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_fp16x16_3d_axis1_add() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('add')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1_add/input_0.cairo b/tests/nodes/scatter_fp16x16_3d_axis1_add/input_0.cairo new file mode 100644 index 000000000..90579e5d8 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1_add/input_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1_add/input_1.cairo b/tests/nodes/scatter_fp16x16_3d_axis1_add/input_1.cairo new file mode 100644 index 000000000..0b71e00bc --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1_add/input_1.cairo @@ -0,0 +1,23 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + 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 }); + data.append(FP16x16 { mag: 262144, sign: false }); + data.append(FP16x16 { mag: 327680, sign: false }); + data.append(FP16x16 { mag: 393216, sign: false }); + data.append(FP16x16 { mag: 458752, sign: false }); + data.append(FP16x16 { mag: 524288, sign: false }); + data.append(FP16x16 { mag: 589824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1_add/input_2.cairo b/tests/nodes/scatter_fp16x16_3d_axis1_add/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1_add/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_axis1_add/output_0.cairo b/tests/nodes/scatter_fp16x16_3d_axis1_add/output_0.cairo new file mode 100644 index 000000000..47d99b23f --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_axis1_add/output_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + 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 }); + data.append(FP16x16 { mag: 327680, sign: false }); + data.append(FP16x16 { mag: 393216, sign: false }); + data.append(FP16x16 { mag: 262144, sign: false }); + data.append(FP16x16 { mag: 524288, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_default.cairo b/tests/nodes/scatter_fp16x16_3d_default.cairo new file mode 100644 index 000000000..ebc06014d --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_default.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP16x16Tensor; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_fp16x16_3d_default() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_default/input_0.cairo b/tests/nodes/scatter_fp16x16_3d_default/input_0.cairo new file mode 100644 index 000000000..90579e5d8 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_default/input_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_default/input_1.cairo b/tests/nodes/scatter_fp16x16_3d_default/input_1.cairo new file mode 100644 index 000000000..0b71e00bc --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_default/input_1.cairo @@ -0,0 +1,23 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + 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 }); + data.append(FP16x16 { mag: 262144, sign: false }); + data.append(FP16x16 { mag: 327680, sign: false }); + data.append(FP16x16 { mag: 393216, sign: false }); + data.append(FP16x16 { mag: 458752, sign: false }); + data.append(FP16x16 { mag: 524288, sign: false }); + data.append(FP16x16 { mag: 589824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_default/input_2.cairo b/tests/nodes/scatter_fp16x16_3d_default/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_default/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp16x16_3d_default/output_0.cairo b/tests/nodes/scatter_fp16x16_3d_default/output_0.cairo new file mode 100644 index 000000000..9edd74528 --- /dev/null +++ b/tests/nodes/scatter_fp16x16_3d_default/output_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 524288, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 458752, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 589824, sign: false }); + data.append(FP16x16 { mag: 262144, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 196608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_axis1.cairo b/tests/nodes/scatter_fp8x23_axis1.cairo new file mode 100644 index 000000000..31ec2ea7c --- /dev/null +++ b/tests/nodes/scatter_fp8x23_axis1.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_fp8x23_axis1() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_axis1/input_0.cairo b/tests/nodes/scatter_fp8x23_axis1/input_0.cairo new file mode 100644 index 000000000..4b7c73dcb --- /dev/null +++ b/tests/nodes/scatter_fp8x23_axis1/input_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_axis1/input_1.cairo b/tests/nodes/scatter_fp8x23_axis1/input_1.cairo new file mode 100644 index 000000000..a36342547 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_axis1/input_1.cairo @@ -0,0 +1,23 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + data.append(FP8x23 { mag: 33554432, sign: false }); + data.append(FP8x23 { mag: 41943040, sign: false }); + data.append(FP8x23 { mag: 50331648, sign: false }); + data.append(FP8x23 { mag: 58720256, sign: false }); + data.append(FP8x23 { mag: 67108864, sign: false }); + data.append(FP8x23 { mag: 75497472, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_axis1/input_2.cairo b/tests/nodes/scatter_fp8x23_axis1/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_axis1/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_axis1/output_0.cairo b/tests/nodes/scatter_fp8x23_axis1/output_0.cairo new file mode 100644 index 000000000..888cee255 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_axis1/output_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + data.append(FP8x23 { mag: 41943040, sign: false }); + data.append(FP8x23 { mag: 50331648, sign: false }); + data.append(FP8x23 { mag: 33554432, sign: false }); + data.append(FP8x23 { mag: 67108864, sign: false }); + data.append(FP8x23 { mag: 75497472, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_default.cairo b/tests/nodes/scatter_fp8x23_default.cairo new file mode 100644 index 000000000..66e5a6064 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_default.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_fp8x23_default() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_default/input_0.cairo b/tests/nodes/scatter_fp8x23_default/input_0.cairo new file mode 100644 index 000000000..4b7c73dcb --- /dev/null +++ b/tests/nodes/scatter_fp8x23_default/input_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_default/input_1.cairo b/tests/nodes/scatter_fp8x23_default/input_1.cairo new file mode 100644 index 000000000..a36342547 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_default/input_1.cairo @@ -0,0 +1,23 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + data.append(FP8x23 { mag: 33554432, sign: false }); + data.append(FP8x23 { mag: 41943040, sign: false }); + data.append(FP8x23 { mag: 50331648, sign: false }); + data.append(FP8x23 { mag: 58720256, sign: false }); + data.append(FP8x23 { mag: 67108864, sign: false }); + data.append(FP8x23 { mag: 75497472, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_default/input_2.cairo b/tests/nodes/scatter_fp8x23_default/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_default/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_default/output_0.cairo b/tests/nodes/scatter_fp8x23_default/output_0.cairo new file mode 100644 index 000000000..6c198c3af --- /dev/null +++ b/tests/nodes/scatter_fp8x23_default/output_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 67108864, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 58720256, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 75497472, sign: false }); + data.append(FP8x23 { mag: 33554432, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_mul.cairo b/tests/nodes/scatter_fp8x23_mul.cairo new file mode 100644 index 000000000..8f6a9e5ae --- /dev/null +++ b/tests/nodes/scatter_fp8x23_mul.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::FP8x23Tensor; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_fp8x23_mul() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('mul')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_mul/input_0.cairo b/tests/nodes/scatter_fp8x23_mul/input_0.cairo new file mode 100644 index 000000000..4b7c73dcb --- /dev/null +++ b/tests/nodes/scatter_fp8x23_mul/input_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_mul/input_1.cairo b/tests/nodes/scatter_fp8x23_mul/input_1.cairo new file mode 100644 index 000000000..a36342547 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_mul/input_1.cairo @@ -0,0 +1,23 @@ +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_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + data.append(FP8x23 { mag: 33554432, sign: false }); + data.append(FP8x23 { mag: 41943040, sign: false }); + data.append(FP8x23 { mag: 50331648, sign: false }); + data.append(FP8x23 { mag: 58720256, sign: false }); + data.append(FP8x23 { mag: 67108864, sign: false }); + data.append(FP8x23 { mag: 75497472, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_mul/input_2.cairo b/tests/nodes/scatter_fp8x23_mul/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_mul/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_fp8x23_mul/output_0.cairo b/tests/nodes/scatter_fp8x23_mul/output_0.cairo new file mode 100644 index 000000000..73c873293 --- /dev/null +++ b/tests/nodes/scatter_fp8x23_mul/output_0.cairo @@ -0,0 +1,23 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1.cairo b/tests/nodes/scatter_i8_axis1.cairo new file mode 100644 index 000000000..aeed6cea2 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_i8_axis1() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1/input_0.cairo b/tests/nodes/scatter_i8_axis1/input_0.cairo new file mode 100644 index 000000000..abbf36d26 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1/input_0.cairo @@ -0,0 +1,22 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1/input_1.cairo b/tests/nodes/scatter_i8_axis1/input_1.cairo new file mode 100644 index 000000000..13224f105 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1/input_1.cairo @@ -0,0 +1,22 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 6, sign: false }); + data.append(i8 { mag: 7, sign: false }); + data.append(i8 { mag: 8, sign: false }); + data.append(i8 { mag: 9, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1/input_2.cairo b/tests/nodes/scatter_i8_axis1/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1/output_0.cairo b/tests/nodes/scatter_i8_axis1/output_0.cairo new file mode 100644 index 000000000..173bb1617 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1/output_0.cairo @@ -0,0 +1,22 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 6, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 8, sign: false }); + data.append(i8 { mag: 9, sign: false }); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1_max.cairo b/tests/nodes/scatter_i8_axis1_max.cairo new file mode 100644 index 000000000..0b1cc537c --- /dev/null +++ b/tests/nodes/scatter_i8_axis1_max.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_i8_axis1_max() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('max')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1_max/input_0.cairo b/tests/nodes/scatter_i8_axis1_max/input_0.cairo new file mode 100644 index 000000000..abbf36d26 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1_max/input_0.cairo @@ -0,0 +1,22 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1_max/input_1.cairo b/tests/nodes/scatter_i8_axis1_max/input_1.cairo new file mode 100644 index 000000000..13224f105 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1_max/input_1.cairo @@ -0,0 +1,22 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 6, sign: false }); + data.append(i8 { mag: 7, sign: false }); + data.append(i8 { mag: 8, sign: false }); + data.append(i8 { mag: 9, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1_max/input_2.cairo b/tests/nodes/scatter_i8_axis1_max/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1_max/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_axis1_max/output_0.cairo b/tests/nodes/scatter_i8_axis1_max/output_0.cairo new file mode 100644 index 000000000..173bb1617 --- /dev/null +++ b/tests/nodes/scatter_i8_axis1_max/output_0.cairo @@ -0,0 +1,22 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 6, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 8, sign: false }); + data.append(i8 { mag: 9, sign: false }); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_default.cairo b/tests/nodes/scatter_i8_default.cairo new file mode 100644 index 000000000..b0d10c89d --- /dev/null +++ b/tests/nodes/scatter_i8_default.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::I8Tensor; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_i8_default() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_default/input_0.cairo b/tests/nodes/scatter_i8_default/input_0.cairo new file mode 100644 index 000000000..abbf36d26 --- /dev/null +++ b/tests/nodes/scatter_i8_default/input_0.cairo @@ -0,0 +1,22 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 0, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_default/input_1.cairo b/tests/nodes/scatter_i8_default/input_1.cairo new file mode 100644 index 000000000..13224f105 --- /dev/null +++ b/tests/nodes/scatter_i8_default/input_1.cairo @@ -0,0 +1,22 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::I8Tensor; +use orion::numbers::{IntegerTrait, i8}; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 3, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 5, sign: false }); + data.append(i8 { mag: 6, sign: false }); + data.append(i8 { mag: 7, sign: false }); + data.append(i8 { mag: 8, sign: false }); + data.append(i8 { mag: 9, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_default/input_2.cairo b/tests/nodes/scatter_i8_default/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_i8_default/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_i8_default/output_0.cairo b/tests/nodes/scatter_i8_default/output_0.cairo new file mode 100644 index 000000000..9f1cf79dd --- /dev/null +++ b/tests/nodes/scatter_i8_default/output_0.cairo @@ -0,0 +1,22 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(i8 { mag: 1, sign: false }); + data.append(i8 { mag: 8, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 7, sign: false }); + data.append(i8 { mag: 2, sign: false }); + data.append(i8 { mag: 9, sign: false }); + data.append(i8 { mag: 4, sign: false }); + data.append(i8 { mag: 0, sign: false }); + data.append(i8 { mag: 3, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_add.cairo b/tests/nodes/scatter_u32_add.cairo new file mode 100644 index 000000000..5c28a9394 --- /dev/null +++ b/tests/nodes/scatter_u32_add.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_u32_add() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('add')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_add/input_0.cairo b/tests/nodes/scatter_u32_add/input_0.cairo new file mode 100644 index 000000000..13ba41a86 --- /dev/null +++ b/tests/nodes/scatter_u32_add/input_0.cairo @@ -0,0 +1,21 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_add/input_1.cairo b/tests/nodes/scatter_u32_add/input_1.cairo new file mode 100644 index 000000000..feb2dabc8 --- /dev/null +++ b/tests/nodes/scatter_u32_add/input_1.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_add/input_2.cairo b/tests/nodes/scatter_u32_add/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_u32_add/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_add/output_0.cairo b/tests/nodes/scatter_u32_add/output_0.cairo new file mode 100644 index 000000000..924837c12 --- /dev/null +++ b/tests/nodes/scatter_u32_add/output_0.cairo @@ -0,0 +1,21 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(13); + data.append(0); + data.append(7); + data.append(2); + data.append(15); + data.append(4); + data.append(0); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_axis1.cairo b/tests/nodes/scatter_u32_axis1.cairo new file mode 100644 index 000000000..784c28787 --- /dev/null +++ b/tests/nodes/scatter_u32_axis1.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_u32_axis1() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_axis1/input_0.cairo b/tests/nodes/scatter_u32_axis1/input_0.cairo new file mode 100644 index 000000000..13ba41a86 --- /dev/null +++ b/tests/nodes/scatter_u32_axis1/input_0.cairo @@ -0,0 +1,21 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_axis1/input_1.cairo b/tests/nodes/scatter_u32_axis1/input_1.cairo new file mode 100644 index 000000000..feb2dabc8 --- /dev/null +++ b/tests/nodes/scatter_u32_axis1/input_1.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_axis1/input_2.cairo b/tests/nodes/scatter_u32_axis1/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_u32_axis1/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_axis1/output_0.cairo b/tests/nodes/scatter_u32_axis1/output_0.cairo new file mode 100644 index 000000000..2a2d51eda --- /dev/null +++ b/tests/nodes/scatter_u32_axis1/output_0.cairo @@ -0,0 +1,21 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(5); + data.append(6); + data.append(4); + data.append(8); + data.append(9); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_default.cairo b/tests/nodes/scatter_u32_default.cairo new file mode 100644 index 000000000..fd2607de7 --- /dev/null +++ b/tests/nodes/scatter_u32_default.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod input_1; +mod input_2; +mod output_0; + + +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::TensorTrait; +use orion::operators::tensor::U32Tensor; +use orion::operators::tensor::U32TensorPartialEq; +use orion::utils::assert_eq; + +#[test] +#[available_gas(2000000000)] +fn test_scatter_u32_default() { + let input_0 = input_0::input_0(); + let input_1 = input_1::input_1(); + let input_2 = input_2::input_2(); + let z = output_0::output_0(); + + let y = input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none')); + + assert_eq(y, z); +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_default/input_0.cairo b/tests/nodes/scatter_u32_default/input_0.cairo new file mode 100644 index 000000000..13ba41a86 --- /dev/null +++ b/tests/nodes/scatter_u32_default/input_0.cairo @@ -0,0 +1,21 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + data.append(0); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_default/input_1.cairo b/tests/nodes/scatter_u32_default/input_1.cairo new file mode 100644 index 000000000..feb2dabc8 --- /dev/null +++ b/tests/nodes/scatter_u32_default/input_1.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_1() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_default/input_2.cairo b/tests/nodes/scatter_u32_default/input_2.cairo new file mode 100644 index 000000000..4fd383db2 --- /dev/null +++ b/tests/nodes/scatter_u32_default/input_2.cairo @@ -0,0 +1,21 @@ +use array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32Tensor; + +fn input_2() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(2); + data.append(0); + data.append(1); + data.append(1); + data.append(0); + data.append(1); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file diff --git a/tests/nodes/scatter_u32_default/output_0.cairo b/tests/nodes/scatter_u32_default/output_0.cairo new file mode 100644 index 000000000..3bea6d3a7 --- /dev/null +++ b/tests/nodes/scatter_u32_default/output_0.cairo @@ -0,0 +1,21 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(8); + data.append(0); + data.append(7); + data.append(2); + data.append(9); + data.append(4); + data.append(0); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} \ No newline at end of file From 6aa1e5fd69ca11d6feb4f510f20b52fe1283bdd3 Mon Sep 17 00:00:00 2001 From: chachaleo Date: Sun, 5 Nov 2023 12:12:33 +0700 Subject: [PATCH 19/21] modif from review + addition of an example --- docs/SUMMARY.md | 1 + docs/framework/compatibility.md | 1 + docs/framework/operators/tensor/README.md | 1 + .../operators/tensor/tensor.qlinear_matmul.md | 95 ++++++++++++ .../operators/tensor/tensor.where.md | 2 +- src/operators/tensor/core.cairo | 64 ++++++++- .../implementations/tensor_fp16x16wide.cairo | 2 +- tests/operators.cairo | 1 + ...l_test.cairo => qlinear_matmul_test.cairo} | 136 +++++++++++------- 9 files changed, 250 insertions(+), 53 deletions(-) create mode 100644 docs/framework/operators/tensor/tensor.qlinear_matmul.md rename tests/operators/{qlinearmatmul_test.cairo => qlinear_matmul_test.cairo} (66%) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 409c1715e..f0fe18ad6 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -82,6 +82,7 @@ * [tensor.gather](framework/operators/tensor/tensor.gather.md) * [tensor.quantize\_linear](framework/operators/tensor/tensor.quantize\_linear.md) * [tensor.dequantize\_linear](framework/operators/tensor/tensor.dequantize\_linear.md) + * [tensor.qlinear\_matmul](framework/operators/tensor/tensor.qlinear\_matmul.md) * [tensor.nonzero](framework/operators/tensor/tensor.nonzero.md) * [tensor.squeeze](framework/operators/tensor/tensor.squeeze.md) * [tensor.unsqueeze](framework/operators/tensor/tensor.unsqueeze.md) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index 29d537ff6..9eba44bee 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -56,6 +56,7 @@ You can see below the list of current supported ONNX Operators: | [Gather](operators/tensor/tensor.gather.md) | :white\_check\_mark: | | [QuantizeLinear](operators/tensor/tensor.quantize\_linear.md) | :white\_check\_mark: | | [DequantizeLinear](operators/tensor/tensor.quantize\_linear.md) | :white\_check\_mark: | +| [QLinearMatmul](operators/tensor/tensor.qlinear\_matmul.md) | :white\_check\_mark: | | [Nonzero](operators/tensor/tensor.nonzero.md) | :white\_check\_mark: | | [Squeeze](operators/tensor/tensor.squeeze.md) | :white\_check\_mark: | | [Unsqueeze](operators/tensor/tensor.unsqueeze.md) | :white\_check\_mark: | diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index 361401570..bbd592608 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -77,6 +77,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.concat`](tensor.concat.md) | Concatenate a list of tensors into a single tensor. | | [`tensor.quantize_linear`](tensor.quantize\_linear.md) | Quantizes a Tensor to i8 using linear quantization. | | [`tensor.dequantize_linear`](tensor.dequantize\_linear.md) | Dequantizes an i8 Tensor using linear dequantization. | +| [`tensor.qlinear_matmul`](tensor.qlinear\_matmul.md) | Performs the product of two quantized i8 Tensors. | | [`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. | diff --git a/docs/framework/operators/tensor/tensor.qlinear_matmul.md b/docs/framework/operators/tensor/tensor.qlinear_matmul.md new file mode 100644 index 000000000..df66f92e4 --- /dev/null +++ b/docs/framework/operators/tensor/tensor.qlinear_matmul.md @@ -0,0 +1,95 @@ +# tensor.qlinear_matmul + +```rust + fn qlinear_matmul(self: @Tensor, a_scale: @Tensor, a_zero_point: @Tensor, b: @Tensor, b_scale: @Tensor, b_zero_point: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor) -> Tensor::; +``` + +Multiplies quantized Tensors + +It consumes two quantized input tensors, their scales and zero points, scale and zero point of output, and computes the quantized output. +The quantization formula is y = saturate((x / y_scale) + y_zero_point). +It perfoms the multiplication of the two vectors once dequantized. If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes. +Then return the quantization of the result of the multiplication. +Scale and zero point must have same shape and the same type. They must be either scalar (per tensor) or N-D tensor (per row for 'a' and per column for 'b'). +Scalar refers to per tensor quantization whereas N-D refers to per row or per column quantization. + +## Args + +* `self`(`@Tensor`) - The first tensor to be multiplied (a). +* `a_scale`(`@Tensor`) - Scale for input `a`. +* `a_zero_point`(`@Tensor`) - Zero point for input `a`. +* `b`(`@Tensor`) - The second tensor to be multiplied +* `b_scale`(`@Tensor`) - Scale for input `b`. +* `b_zero_point`(`@Tensor`) - Zero point for input `b`. +* `y_scale`(`@Tensor`) - Scale for outut. +* `y_zero_point`(`@Tensor`) - Zero point for output. + +## Returns + +A new `Tensor`, containing the quantized result of the multiplication of the dequantized inputs. + +## Type Constraints + +u32 tensor, not supported. + +## Example + +```rust +use array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, I8Tensor, FP16x16Tensor}; +use orion::numbers::{i8, FP16x16, FP16x16Impl, IntegerTrait, FixedTrait}; +fn qlinear_matmul_example() -> Tensor { + let a = TensorTrait::< + i8 + >::new( + shape: array![2, 3].span(), + data: array![ + IntegerTrait::::new(3_u8, false), + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(5_u8, false), + IntegerTrait::::new(2_u8, false), + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(3_u8, false) + ] + .span(), + ); + let b = TensorTrait::< + i8 + >::new( + shape: array![3, 1].span(), + data: array![ + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(8_u8, false), + IntegerTrait::::new(4_u8, false) + ] + .span(), + ); + + let a_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(131072, false)].span(),); + let a_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(65536, false)].span(),); + let b_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(16384, false)].span(),); + let b_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); + + let y_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(393216, false)].span(),); + let y_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(655360, false)].span(),); + + return a + .qlinear_matmul( + @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point + ); +} +>>> [14, 13] +``` \ No newline at end of file diff --git a/docs/framework/operators/tensor/tensor.where.md b/docs/framework/operators/tensor/tensor.where.md index 02d3b0059..187f12ecb 100644 --- a/docs/framework/operators/tensor/tensor.where.md +++ b/docs/framework/operators/tensor/tensor.where.md @@ -37,7 +37,7 @@ fn where_example() -> Tensor { let tensor_x = TensorTrait::::new( shape: array![2, 2].span(), data: array![2, 4, 6, 8].span(), ); - + let tensor_y = TensorTrait::::new( shape: array![2, 2].span(), data: array![1, 3, 5, 9].span(), ); diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 7db84915c..06fe72ef3 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -73,6 +73,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde { /// ## Type Constraints /// /// u32 tensor, not supported. - /// + /// + /// ## Example + /// + /// ```rust + /// use array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, I8Tensor, FP16x16Tensor}; + /// use orion::numbers::{i8, FP16x16, FP16x16Impl, IntegerTrait, FixedTrait}; + /// fn qlinear_matmul_example() -> Tensor { + /// let a = TensorTrait::< + /// i8 + /// >::new( + /// shape: array![2, 3].span(), + /// data: array![ + /// IntegerTrait::::new(3_u8, false), + /// IntegerTrait::::new(4_u8, false), + /// IntegerTrait::::new(5_u8, false), + /// IntegerTrait::::new(2_u8, false), + /// IntegerTrait::::new(4_u8, false), + /// IntegerTrait::::new(3_u8, false) + /// ] + /// .span(), + /// ); + /// let b = TensorTrait::< + /// i8 + /// >::new( + /// shape: array![3, 1].span(), + /// data: array![ + /// IntegerTrait::::new(4_u8, false), + /// IntegerTrait::::new(8_u8, false), + /// IntegerTrait::::new(4_u8, false) + /// ] + /// .span(), + /// ); + /// + /// let a_scale = TensorTrait::< + /// FP16x16 + /// >::new(shape: array![1].span(), data: array![FixedTrait::::new(131072, false)].span(),); + /// let a_zero_point = TensorTrait::< + /// FP16x16 + /// >::new(shape: array![1].span(), data: array![FixedTrait::::new(65536, false)].span(),); + /// let b_scale = TensorTrait::< + /// FP16x16 + /// >::new(shape: array![1].span(), data: array![FixedTrait::::new(16384, false)].span(),); + /// let b_zero_point = TensorTrait::< + /// FP16x16 + /// >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); + /// + /// let y_scale = TensorTrait::< + /// FP16x16 + /// >::new(shape: array![1].span(), data: array![FixedTrait::::new(393216, false)].span(),); + /// let y_zero_point = TensorTrait::< + /// FP16x16 + /// >::new(shape: array![1].span(), data: array![FixedTrait::::new(655360, false)].span(),); + /// + /// return a + /// .qlinear_matmul( + /// @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point + /// ); + /// } + /// >>> [14, 13] + /// ``` fn qlinear_matmul( self: @Tensor, a_scale: @Tensor, diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index d28fec8ec..0515baea0 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -216,7 +216,7 @@ impl FP16x16WTensor of TensorTrait { ) -> Tensor:: { panic(array!['not supported!']) } - + fn slice( self: @Tensor, starts: Span, diff --git a/tests/operators.cairo b/tests/operators.cairo index d84e29548..7642e31f3 100644 --- a/tests/operators.cairo +++ b/tests/operators.cairo @@ -1 +1,2 @@ mod transpose_test; +mod qlinear_matmul_test; diff --git a/tests/operators/qlinearmatmul_test.cairo b/tests/operators/qlinear_matmul_test.cairo similarity index 66% rename from tests/operators/qlinearmatmul_test.cairo rename to tests/operators/qlinear_matmul_test.cairo index 555555eaf..b666518a4 100644 --- a/tests/operators/qlinearmatmul_test.cairo +++ b/tests/operators/qlinear_matmul_test.cairo @@ -17,14 +17,14 @@ fn qlinearmatmul_2D_test() { >::new( shape: array![2, 4].span(), data: array![ - IntegerTrait::::new(1_u8, true), - IntegerTrait::::new(2_u8, true), - IntegerTrait::::new(3_u8, true), - IntegerTrait::::new(4_u8, true), - IntegerTrait::::new(5_u8, true), - IntegerTrait::::new(6_u8, true), - IntegerTrait::::new(7_u8, true), - IntegerTrait::::new(8_u8, true) + IntegerTrait::::new(1_u8, false), + IntegerTrait::::new(2_u8, false), + IntegerTrait::::new(3_u8, false), + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(5_u8, false), + IntegerTrait::::new(6_u8, false), + IntegerTrait::::new(7_u8, false), + IntegerTrait::::new(8_u8, false) ] .span(), ); @@ -33,18 +33,18 @@ fn qlinearmatmul_2D_test() { >::new( shape: array![4, 3].span(), data: array![ - IntegerTrait::::new(2_u8, true), - IntegerTrait::::new(4_u8, true), - IntegerTrait::::new(6_u8, true), - IntegerTrait::::new(8_u8, true), - IntegerTrait::::new(10_u8, true), - IntegerTrait::::new(12_u8, true), - IntegerTrait::::new(14_u8, true), - IntegerTrait::::new(16_u8, true), - IntegerTrait::::new(18_u8, true), - IntegerTrait::::new(20_u8, true), - IntegerTrait::::new(22_u8, true), - IntegerTrait::::new(24_u8, true) + IntegerTrait::::new(2_u8, false), + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(6_u8, false), + IntegerTrait::::new(8_u8, false), + IntegerTrait::::new(10_u8, false), + IntegerTrait::::new(12_u8, false), + IntegerTrait::::new(14_u8, false), + IntegerTrait::::new(16_u8, false), + IntegerTrait::::new(18_u8, false), + IntegerTrait::::new(20_u8, false), + IntegerTrait::::new(22_u8, false), + IntegerTrait::::new(24_u8, false) ] .span(), ); @@ -73,20 +73,6 @@ fn qlinearmatmul_2D_test() { .qlinear_matmul( @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point ); - let expected_output = TensorTrait::< - i8 - >::new( - shape: array![2, 3].span(), - data: array![ - IntegerTrait::::new(3_u8, true), - IntegerTrait::::new(4_u8, true), - IntegerTrait::::new(4_u8, true), - IntegerTrait::::new(8_u8, true), - IntegerTrait::::new(9_u8, true), - IntegerTrait::::new(10_u8, true) - ] - .span(), - ); assert((*actual_output.data[0]).into() == 3, '*result[0] == 3'); assert((*actual_output.data[1]).into() == 4, '*result[1] == 4'); @@ -165,22 +151,7 @@ fn qlinearmatmul_3D_test() { .qlinear_matmul( @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point ); - let expected_output = TensorTrait::< - i8 - >::new( - shape: array![2, 2, 2].span(), - data: array![ - IntegerTrait::::new(8_u8, true), - IntegerTrait::::new(11_u8, true), - IntegerTrait::::new(17_u8, true), - IntegerTrait::::new(23_u8, true), - IntegerTrait::::new(26_u8, true), - IntegerTrait::::new(35_u8, true), - IntegerTrait::::new(36_u8, true), - IntegerTrait::::new(47_u8, true) - ] - .span(), - ); + assert((*actual_output.data[0]).into() == 8, '*result[0] == 8'); assert((*actual_output.data[1]).into() == 11, '*result[1] == 11'); assert((*actual_output.data[2]).into() == 17, '*result[2] == 17'); @@ -191,6 +162,71 @@ fn qlinearmatmul_3D_test() { assert((*actual_output.data[7]).into() == 47, '*result[7] == 47'); } + +#[test] +#[available_gas(200000000000)] +fn test_example_doc() { + let a = TensorTrait::< + i8 + >::new( + shape: array![2, 3].span(), + data: array![ + IntegerTrait::::new(3_u8, false), + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(5_u8, false), + IntegerTrait::::new(2_u8, false), + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(3_u8, false) + ] + .span(), + ); + let b = TensorTrait::< + i8 + >::new( + shape: array![3, 1].span(), + data: array![ + IntegerTrait::::new(4_u8, false), + IntegerTrait::::new(8_u8, false), + IntegerTrait::::new(4_u8, false) + ] + .span(), + ); + + let a_scale = TensorTrait::< + FP16x16 + >::new( + shape: array![1].span(), data: array![FixedTrait::::new(131072, false)].span(), + ); + let a_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(65536, false)].span(),); + let b_scale = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(16384, false)].span(),); + let b_zero_point = TensorTrait::< + FP16x16 + >::new(shape: array![1].span(), data: array![FixedTrait::::new(0, false)].span(),); + + let y_scale = TensorTrait::< + FP16x16 + >::new( + shape: array![1].span(), data: array![FixedTrait::::new(393216, false)].span(), + ); + let y_zero_point = TensorTrait::< + FP16x16 + >::new( + shape: array![1].span(), data: array![FixedTrait::::new(655360, false)].span(), + ); + + let actual_output = a + .qlinear_matmul( + @a_scale, @a_zero_point, @b, @b_scale, @b_zero_point, @y_scale, @y_zero_point + ); + + assert((*actual_output.data[0]).into() == 14, '*result[0] == 14'); + assert((*actual_output.data[1]).into() == 13, '*result[1] == 13'); +} + fn print_span(mut span: Span) { loop { match span.pop_front() { From bc4847403346d2f47dae61d18e793ebc307da3cb Mon Sep 17 00:00:00 2001 From: raphaelDkhn Date: Sun, 5 Nov 2023 19:14:06 +0200 Subject: [PATCH 20/21] fix typo --- docs/framework/operators/tensor/tensor.scatter.md | 2 +- src/operators/tensor/core.cairo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/framework/operators/tensor/tensor.scatter.md b/docs/framework/operators/tensor/tensor.scatter.md index 2420f2ddf..439e7ab0b 100644 --- a/docs/framework/operators/tensor/tensor.scatter.md +++ b/docs/framework/operators/tensor/tensor.scatter.md @@ -9,7 +9,7 @@ Produces a copy of input data, and updates value to values specified by updates ## Args * `self`(`@Tensor`) - The input tensor. -* `updates`(`Tensor`) - The inupdates tensor. +* `updates`(`Tensor`) - The updates tensor. * `indices`(`Tensor`) - Tensor of indices. * `axis`(`Option`) - Axis to scatter on. Default: axis=0. * `reduction`(`Option`) - Reduction operation. Default: reduction='none'. diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index ef05481cb..990ccedf7 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -3053,7 +3053,7 @@ trait TensorTrait { /// ## Args /// /// * `self`(`@Tensor`) - The input tensor. - /// * `updates`(`Tensor`) - The inupdates tensor. + /// * `updates`(`Tensor`) - The updates tensor. /// * `indices`(`Tensor`) - Tensor of indices. /// * `axis`(`Option`) - Axis to scatter on. Default: axis=0. /// * `reduction`(`Option`) - Reduction operation. Default: reduction='none'. From 3215cc83154bfb7eb9b9e6ed1bce55583994479a Mon Sep 17 00:00:00 2001 From: raphaelDkhn Date: Sun, 5 Nov 2023 19:19:08 +0200 Subject: [PATCH 21/21] Update compatibility.md --- docs/framework/compatibility.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/framework/compatibility.md b/docs/framework/compatibility.md index a8bfaf12d..e5f7f39cf 100644 --- a/docs/framework/compatibility.md +++ b/docs/framework/compatibility.md @@ -75,4 +75,4 @@ You can see below the list of current supported ONNX Operators: | [Max](operators/tensor/tensor.max.md) | :white\_check\_mark: | | [Scatter](operators/tensor/scatter.max.md) | :white\_check\_mark: | -Current Operators support: **62/156 (39%)** +Current Operators support: **68/156 (43%)**