From c5b5685e0ff5666db856931478ed4d5464f0ad38 Mon Sep 17 00:00:00 2001 From: lancenonce Date: Fri, 12 Apr 2024 12:55:19 +0300 Subject: [PATCH] impls --- src/operators/tensor/core.cairo | 50 ++++++++++++++++++- .../tensor/implementations/tensor_bool.cairo | 9 ++++ .../implementations/tensor_complex64.cairo | 9 ++++ .../implementations/tensor_fp16x16.cairo | 9 ++++ .../implementations/tensor_fp16x16wide.cairo | 9 ++++ .../implementations/tensor_fp32x32.cairo | 9 ++++ .../implementations/tensor_fp64x64.cairo | 9 ++++ .../implementations/tensor_fp8x23.cairo | 9 ++++ .../implementations/tensor_fp8x23wide.cairo | 9 ++++ .../tensor/implementations/tensor_i32.cairo | 9 ++++ .../tensor/implementations/tensor_i8.cairo | 9 ++++ .../tensor/implementations/tensor_u32.cairo | 9 ++++ src/operators/tensor/math.cairo | 1 + .../nodes/reduce_max_fp16x16_2D_axis_1.cairo | 3 +- tests/nodes/reduce_max_i32_2D_axis_1.cairo | 3 +- tests/nodes/reduce_max_i8_2D_axis_1.cairo | 3 +- tests/nodes/reduce_max_u32_2D_axis_1.cairo | 3 +- 17 files changed, 157 insertions(+), 5 deletions(-) diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index b313159c2..c1707e8c0 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -111,7 +111,6 @@ impl TensorSerde, impl TDrop: Drop> of Serde { keepdims: Option, noop_with_empty_axes: Option ) -> Tensor; + /// ## tensor.reduce_max + /// + /// ```rust + /// fn reduce_max(self: @Tensor, axes: Option>, keepdims: Option, noop_with_empty_axes: Option) -> Tensor; + ///``` + /// + /// Computes the max of the input tensor's elements along the provided axes. + /// + /// ## Args + /// + /// * `self`(`@Tensor`) - The input tensor. + /// * `axes`(`Option>`) - Optional input list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor if 'noop_with_empty_axes' is false, else act as an identity op when 'noop_with_empty_axes' is true. + /// * `keepdims`(`Option`) - Keep the reduced dimensions or not, default true means keep reduced dimensions. + /// * `noop_with_empty_axes`(`Option`) - Defines behavior if 'axes' is empty. Default behavior with 'false' is to reduce all axes. When axes is empty and this attribute is set to true, input tensor will not be reduced,and the output tensor would be equivalent to input tensor. + /// + /// ## Panics + /// + /// * Panics if axis is not in the range of the input tensor's dimensions. + /// + /// ## Returns + /// + /// A new `Tensor` instance with the specified axes reduced by maximum of its elements. + /// + /// ## Examples + /// + /// ```rust + /// use core::array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn reduce_max_example() -> Tensor { + /// let tensor = TensorTrait::::new( + /// shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), + /// ); + /// + /// // We can call `reduce_max` function as follows. + /// return tensor.reduce_max(axes: array![1].span(), + /// keepdims: Option::None(()), + /// noop_with_empty_axes: Option::None(())); + /// } + /// >>> [[2,3],[6,7]] + /// ``` + /// + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor; /// #tensor.pow /// /// ```rust diff --git a/src/operators/tensor/implementations/tensor_bool.cairo b/src/operators/tensor/implementations/tensor_bool.cairo index e8ca7e2d8..dab8a2bb6 100644 --- a/src/operators/tensor/implementations/tensor_bool.cairo +++ b/src/operators/tensor/implementations/tensor_bool.cairo @@ -409,6 +409,15 @@ impl BoolTensor of TensorTrait { panic(array!['not supported!']) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + panic(array!['not supported!']) + } + fn pow(self: @Tensor, other: @Tensor) -> Tensor { panic(array!['not supported!']) } diff --git a/src/operators/tensor/implementations/tensor_complex64.cairo b/src/operators/tensor/implementations/tensor_complex64.cairo index 24e64c923..24a4d20e0 100644 --- a/src/operators/tensor/implementations/tensor_complex64.cairo +++ b/src/operators/tensor/implementations/tensor_complex64.cairo @@ -432,6 +432,15 @@ impl Complex64Tensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + panic(array!['not supported!']) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index 58b36a4bc..41d9bfa01 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -466,6 +466,15 @@ impl FP16x16Tensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 89eb2d09b..3dee5f858 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -435,6 +435,15 @@ impl FP16x16WTensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 942680b81..3f4f5eead 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -463,6 +463,15 @@ impl FP32x32Tensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index be1cd04c3..dd66d4a12 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -463,6 +463,15 @@ impl FP64x64Tensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 19681e641..725363380 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -459,6 +459,15 @@ impl FP8x23Tensor of TensorTrait { panic(array!['not supported!']) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index bee949f2c..0e4ceecca 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -418,6 +418,15 @@ impl FP8x23WTensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 4caa2ad24..4701e5445 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -452,6 +452,15 @@ impl I32Tensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index 7c7770a77..f31901162 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -455,6 +455,15 @@ impl I8Tensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 1ad0ef8de..151b0a728 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -399,6 +399,15 @@ impl U32Tensor of TensorTrait { math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_max( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_max::reduce_max(self, axes, keepdims, noop_with_empty_axes) + } + fn reduce_min( self: @Tensor, axes: Option>, diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index b73f6d102..e10f5c893 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -49,6 +49,7 @@ mod bitwise_and; mod bitwise_xor; mod bitwise_or; mod gather_elements; +mod reduce_max; mod reduce_min; mod shrink; mod reduce_mean; diff --git a/tests/nodes/reduce_max_fp16x16_2D_axis_1.cairo b/tests/nodes/reduce_max_fp16x16_2D_axis_1.cairo index 8a073d402..99151031b 100644 --- a/tests/nodes/reduce_max_fp16x16_2D_axis_1.cairo +++ b/tests/nodes/reduce_max_fp16x16_2D_axis_1.cairo @@ -14,7 +14,8 @@ fn test_reduce_max_fp16x16_2D_axis_1() { let input_0 = input_0::input_0(); let z_0 = output_0::output_0(); - let y_0 = input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); + let y_0 = input_0 + .reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); assert_eq(y_0, z_0); } diff --git a/tests/nodes/reduce_max_i32_2D_axis_1.cairo b/tests/nodes/reduce_max_i32_2D_axis_1.cairo index a0bc54de0..149e17017 100644 --- a/tests/nodes/reduce_max_i32_2D_axis_1.cairo +++ b/tests/nodes/reduce_max_i32_2D_axis_1.cairo @@ -14,7 +14,8 @@ fn test_reduce_max_i32_2D_axis_1() { let input_0 = input_0::input_0(); let z_0 = output_0::output_0(); - let y_0 = input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); + let y_0 = input_0 + .reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); assert_eq(y_0, z_0); } diff --git a/tests/nodes/reduce_max_i8_2D_axis_1.cairo b/tests/nodes/reduce_max_i8_2D_axis_1.cairo index 84d6724ff..c38eb596e 100644 --- a/tests/nodes/reduce_max_i8_2D_axis_1.cairo +++ b/tests/nodes/reduce_max_i8_2D_axis_1.cairo @@ -14,7 +14,8 @@ fn test_reduce_max_i8_2D_axis_1() { let input_0 = input_0::input_0(); let z_0 = output_0::output_0(); - let y_0 = input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); + let y_0 = input_0 + .reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); assert_eq(y_0, z_0); } diff --git a/tests/nodes/reduce_max_u32_2D_axis_1.cairo b/tests/nodes/reduce_max_u32_2D_axis_1.cairo index e80fd00c2..bf0fcf5b0 100644 --- a/tests/nodes/reduce_max_u32_2D_axis_1.cairo +++ b/tests/nodes/reduce_max_u32_2D_axis_1.cairo @@ -14,7 +14,8 @@ fn test_reduce_max_u32_2D_axis_1() { let input_0 = input_0::input_0(); let z_0 = output_0::output_0(); - let y_0 = input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); + let y_0 = input_0 + .reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(())); assert_eq(y_0, z_0); }