From 9e9729532869ed5d434387db075fa4f4afc6bca2 Mon Sep 17 00:00:00 2001 From: chachaleo Date: Sat, 24 Feb 2024 10:44:37 +0100 Subject: [PATCH] feat: max pool --- .../linear_classifier.predict.md | 4 +- .../linear_regressor.predict.md | 8 +- .../tree_ensemble_classifier.predict.md | 4 +- .../tree_ensemble_regressor.predict.md | 4 +- .../operators/neural-network/nn.col2im.md | 1 - .../operators/neural-network/nn.max_pool.md | 107 ++ nodegen/node/max_pool.py | 1263 +++++++++++++++++ .../tree_ensemble_classifier.cairo | 8 +- .../tree_ensemble_regressor.cairo | 12 +- src/operators/nn.cairo | 3 + src/operators/nn/common.cairo | 14 + src/operators/nn/core.cairo | 121 ++ src/operators/nn/functional.cairo | 2 + src/operators/nn/functional/common_pool.cairo | 945 ++++++++++++ src/operators/nn/functional/gemm.cairo | 2 +- src/operators/nn/functional/max_pool.cairo | 1084 ++++++++++++++ .../nn/implementations/nn_fp16x16.cairo | 27 + .../nn/implementations/nn_fp32x32.cairo | 18 + .../nn/implementations/nn_fp64x64.cairo | 18 + .../nn/implementations/nn_fp8x23.cairo | 27 + src/operators/nn/implementations/nn_i32.cairo | 15 + src/operators/nn/implementations/nn_i8.cairo | 16 + src/operators/nn/implementations/nn_u32.cairo | 16 + tests/lib.cairo | 2 + tests/nodes.cairo | 13 + tests/nodes/maxpool_1d.cairo | 30 + tests/nodes/maxpool_1d/input_0.cairo | 110 ++ tests/nodes/maxpool_1d/output_0.cairo | 62 + tests/nodes/maxpool_1d_default.cairo | 31 + tests/nodes/maxpool_1d_default/input_0.cairo | 110 ++ tests/nodes/maxpool_1d_default/output_0.cairo | 107 ++ tests/nodes/maxpool_2d.cairo | 30 + tests/nodes/maxpool_2d/input_0.cairo | 40 + tests/nodes/maxpool_2d/output_0.cairo | 19 + tests/nodes/maxpool_2d_ceil.cairo | 30 + tests/nodes/maxpool_2d_ceil/input_0.cairo | 31 + tests/nodes/maxpool_2d_ceil/output_0.cairo | 19 + tests/nodes/maxpool_2d_constraint_index.cairo | 25 + .../maxpool_2d_constraint_index/input_0.cairo | 40 + .../output_0.cairo | 19 + tests/nodes/maxpool_2d_default.cairo | 30 + tests/nodes/maxpool_2d_default/input_0.cairo | 207 +++ tests/nodes/maxpool_2d_default/output_0.cairo | 162 +++ tests/nodes/maxpool_2d_dilations.cairo | 30 + .../nodes/maxpool_2d_dilations/input_0.cairo | 31 + .../nodes/maxpool_2d_dilations/output_0.cairo | 19 + tests/nodes/maxpool_2d_pads_default.cairo | 30 + .../maxpool_2d_pads_default/input_0.cairo | 40 + .../maxpool_2d_pads_default/output_0.cairo | 40 + .../nodes/maxpool_2d_same_lower_default.cairo | 31 + .../input_0.cairo | 207 +++ .../output_0.cairo | 207 +++ tests/nodes/maxpool_2d_same_upper.cairo | 31 + .../nodes/maxpool_2d_same_upper/input_0.cairo | 40 + .../maxpool_2d_same_upper/output_0.cairo | 24 + .../nodes/maxpool_2d_same_upper_default.cairo | 31 + .../input_0.cairo | 207 +++ .../output_0.cairo | 207 +++ tests/nodes/maxpool_3d_dilations.cairo | 30 + .../nodes/maxpool_3d_dilations/input_0.cairo | 80 ++ .../nodes/maxpool_3d_dilations/output_0.cairo | 24 + tests/nodes/maxpool_4d_dilations.cairo | 30 + .../nodes/maxpool_4d_dilations/input_0.cairo | 785 ++++++++++ .../nodes/maxpool_4d_dilations/output_0.cairo | 65 + 64 files changed, 7008 insertions(+), 17 deletions(-) create mode 100644 docs/framework/operators/neural-network/nn.max_pool.md create mode 100644 nodegen/node/max_pool.py create mode 100644 src/operators/nn/common.cairo create mode 100644 src/operators/nn/functional/common_pool.cairo create mode 100644 src/operators/nn/functional/max_pool.cairo create mode 100644 tests/nodes/maxpool_1d.cairo create mode 100644 tests/nodes/maxpool_1d/input_0.cairo create mode 100644 tests/nodes/maxpool_1d/output_0.cairo create mode 100644 tests/nodes/maxpool_1d_default.cairo create mode 100644 tests/nodes/maxpool_1d_default/input_0.cairo create mode 100644 tests/nodes/maxpool_1d_default/output_0.cairo create mode 100644 tests/nodes/maxpool_2d.cairo create mode 100644 tests/nodes/maxpool_2d/input_0.cairo create mode 100644 tests/nodes/maxpool_2d/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_ceil.cairo create mode 100644 tests/nodes/maxpool_2d_ceil/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_ceil/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_constraint_index.cairo create mode 100644 tests/nodes/maxpool_2d_constraint_index/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_constraint_index/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_default.cairo create mode 100644 tests/nodes/maxpool_2d_default/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_default/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_dilations.cairo create mode 100644 tests/nodes/maxpool_2d_dilations/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_dilations/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_pads_default.cairo create mode 100644 tests/nodes/maxpool_2d_pads_default/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_pads_default/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_same_lower_default.cairo create mode 100644 tests/nodes/maxpool_2d_same_lower_default/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_same_lower_default/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_same_upper.cairo create mode 100644 tests/nodes/maxpool_2d_same_upper/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_same_upper/output_0.cairo create mode 100644 tests/nodes/maxpool_2d_same_upper_default.cairo create mode 100644 tests/nodes/maxpool_2d_same_upper_default/input_0.cairo create mode 100644 tests/nodes/maxpool_2d_same_upper_default/output_0.cairo create mode 100644 tests/nodes/maxpool_3d_dilations.cairo create mode 100644 tests/nodes/maxpool_3d_dilations/input_0.cairo create mode 100644 tests/nodes/maxpool_3d_dilations/output_0.cairo create mode 100644 tests/nodes/maxpool_4d_dilations.cairo create mode 100644 tests/nodes/maxpool_4d_dilations/input_0.cairo create mode 100644 tests/nodes/maxpool_4d_dilations/output_0.cairo diff --git a/docs/framework/operators/machine-learning/linear-classifier/linear_classifier.predict.md b/docs/framework/operators/machine-learning/linear-classifier/linear_classifier.predict.md index aec154f68..7ed30f236 100644 --- a/docs/framework/operators/machine-learning/linear-classifier/linear_classifier.predict.md +++ b/docs/framework/operators/machine-learning/linear-classifier/linear_classifier.predict.md @@ -1,7 +1,7 @@ # LinearClassifierTrait::predict ```rust - fn predict(ref self: LinearClassifier, X: Tensor) -> Tensor; + fn predict(classifier: LinearClassifier, X: Tensor) -> Tensor; ``` Linear Classifier. Performs the linear classification. @@ -85,7 +85,7 @@ fn linear_classifier_helper( fn linear_classifier_multi_softmax() -> (Span, Tensor) { let (mut classifier, X) = linear_classifier_helper(POST_TRANSFORM::SOFTMAX); - let (labels, mut scores) = LinearClassifierTrait::predict(ref classifier, X); + let (labels, mut scores) = LinearClassifierTrait::predict(classifier, X); (labels, scores) } diff --git a/docs/framework/operators/machine-learning/linear-regressor/linear_regressor.predict.md b/docs/framework/operators/machine-learning/linear-regressor/linear_regressor.predict.md index f1bd38831..6c40ac930 100644 --- a/docs/framework/operators/machine-learning/linear-regressor/linear_regressor.predict.md +++ b/docs/framework/operators/machine-learning/linear-regressor/linear_regressor.predict.md @@ -1,14 +1,14 @@ # LinearRegressorTrait::predict ```rust - fn predict(ref self: LinearRegressor, X: Tensor) -> Tensor; + fn predict(regressor: LinearRegressor, X: Tensor) -> Tensor; ``` Linear Regressor. Performs the generalized linear regression evaluation. ## Args -* `self`: LinearRegressor - A LinearRegressor object. +* `regressor`: LinearRegressor - A LinearRegressor object. * `X`: Input 2D tensor. ## Returns @@ -68,7 +68,7 @@ fn example_linear_regressor() -> Tensor { post_transform }; - let scores = LinearRegressorTrait::predict(ref regressor, X); + let scores = LinearRegressorTrait::predict(regressor, X); scores } @@ -120,7 +120,7 @@ fn example_linear_regressor_2() -> Tensor { post_transform }; - let scores = LinearRegressorTrait::predict(ref regressor, X); + let scores = LinearRegressorTrait::predict(regressor, X); scores } diff --git a/docs/framework/operators/machine-learning/tree-ensemble-classifier/tree_ensemble_classifier.predict.md b/docs/framework/operators/machine-learning/tree-ensemble-classifier/tree_ensemble_classifier.predict.md index 6d839e873..c38f3e46d 100644 --- a/docs/framework/operators/machine-learning/tree-ensemble-classifier/tree_ensemble_classifier.predict.md +++ b/docs/framework/operators/machine-learning/tree-ensemble-classifier/tree_ensemble_classifier.predict.md @@ -1,7 +1,7 @@ # TreeEnsembleClassifier::predict ```rust - fn predict(ref self: TreeEnsembleClassifier, X: Tensor) -> (Span, MutMatrix::); + fn predict(classifier: TreeEnsembleClassifier, X: Tensor) -> (Span, MutMatrix::); ``` Tree Ensemble classifier. Returns the top class for each of N inputs. @@ -185,7 +185,7 @@ fn tree_ensemble_classifier_helper( fn test_tree_ensemble_classifier_multi_pt_softmax() -> (Span, MutMatrix::) { let (mut classifier, X) = tree_ensemble_classifier_helper(POST_TRANSFORM::SOFTMAX); - let (labels, scores) = TreeEnsembleClassifierTrait::predict(ref classifier, X); + let (labels, scores) = TreeEnsembleClassifierTrait::predict(classifier, X); (labels, scores) } diff --git a/docs/framework/operators/machine-learning/tree-ensemble-regressor/tree_ensemble_regressor.predict.md b/docs/framework/operators/machine-learning/tree-ensemble-regressor/tree_ensemble_regressor.predict.md index 812115971..243bda558 100644 --- a/docs/framework/operators/machine-learning/tree-ensemble-regressor/tree_ensemble_regressor.predict.md +++ b/docs/framework/operators/machine-learning/tree-ensemble-regressor/tree_ensemble_regressor.predict.md @@ -1,7 +1,7 @@ # TreeEnsembleRegressor::predict ```rust - fn predict(ref self: TreeEnsembleRegressor, X: Tensor) -> (Span, MutMatrix::); + fn predict(regressor: TreeEnsembleRegressor, X: Tensor) -> (Span, MutMatrix::); ``` Tree Ensemble regressor. Returns the regressed values for each input in N. @@ -160,7 +160,7 @@ fn tree_ensemble_regressor_helper( fn test_tree_ensemble_regressor_SUM() -> MutMatrix:: { let (mut regressor, X) = tree_ensemble_regressor_helper(AGGREGATE_FUNCTION::SUM); - let mut res = TreeEnsembleRegressorTrait::predict(ref regressor, X); + let mut res = TreeEnsembleRegressorTrait::predict(regressor, X); res } >>> diff --git a/docs/framework/operators/neural-network/nn.col2im.md b/docs/framework/operators/neural-network/nn.col2im.md index fd5e82ffa..6c7b1af05 100644 --- a/docs/framework/operators/neural-network/nn.col2im.md +++ b/docs/framework/operators/neural-network/nn.col2im.md @@ -1,4 +1,3 @@ - # NNTrait::col2im ```rust diff --git a/docs/framework/operators/neural-network/nn.max_pool.md b/docs/framework/operators/neural-network/nn.max_pool.md new file mode 100644 index 000000000..eb21f4c8c --- /dev/null +++ b/docs/framework/operators/neural-network/nn.max_pool.md @@ -0,0 +1,107 @@ + +# NNTrait::max_pool + +```rust + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, +) -> (Tensor, Option>); +``` + +MaxPool consumes an input tensor X and applies max pooling across the tensor according to kernel sizes, stride sizes, and pad lengths. max pooling consisting of computing the max on all values of a subset of the input tensor according to the kernel size and downsampling the data into the output tensor Y for further processing. The output spatial shape is calculated differently depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized. + +## Args + +* `X`(`@Tensor`) - Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. +* `auto_pad`(`Option`) - Default is NOTSET, auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. NOTSET means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. +* `ceil_mode`(`Option`) - Default is 1, Whether to use ceil or floor (default) to compute the output shape. +* `dilations`(`Option>`) - Dilation value along each spatial axis of the filter. If not present, the dilation defaults to 1 along each spatial axis. +* `kernel_shape`(`Span`) - The size of the kernel along each axis. +* `pads`(`Option>`) - Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis. +* `storage_order`(`Option`) - Default is 0, The storage order of the tensor. 0 is row major, and 1 is column major. +* `strides`(`Option>`) - Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis. +* `output_len`(`Option`) - Default is 1, If set to 2, return the indices tensor. + +## Returns + +A `Tensor` that contains the result of the max pool. +A `Option>` with the indices tensor from max pooling across the input tensor. The dimensions of indices are the same as output tensor. +## Examples + +```rust +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::nn::FP16x16NN; +use orion::numbers::FP16x16; +use orion::operators::tensor::{Tensor, TensorTrait, FP16x16Tensor}; + + +fn example_max_pool() -> (Tensor, Option>) { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(5); + shape.append(5); + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + data.append(FP16x16 { mag: 1114112, sign: false }); + data.append(FP16x16 { mag: 1179648, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1376256, sign: false }); + data.append(FP16x16 { mag: 1441792, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + let mut X = TensorTrait::new(shape.span(), data.span()); + return NNTrait::max_pool( + @X, + Option::None, + Option::None, + Option::None, + array![5, 5, 5].span(), + Option::Some(array![2, 2, 2, 2].span()), + Option::None, + Option::None, + 1 + ); + +} + +>>> ([ + [ + [ + [13, 14, 15, 15, 15], + [18, 19, 20, 20, 20], + [23, 24, 25, 25, 25], + [23, 24, 25, 25, 25], + [23, 24, 25, 25, 25], + ] + ] + ], + Option::None) + + +```` diff --git a/nodegen/node/max_pool.py b/nodegen/node/max_pool.py new file mode 100644 index 000000000..9786209a2 --- /dev/null +++ b/nodegen/node/max_pool.py @@ -0,0 +1,1263 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl, Trait + +import numpy as np +import numpy as np + +from typing import Tuple, Union +from onnx.reference.ops._op_common_pool import CommonPool + + +def max_pool( + x, + auto_pad=None, + ceil_mode=None, + dilations=None, + kernel_shape=None, + pads=None, + storage_order=None, + strides=None, + output_len=None +): + if ( + dilations is not None + and (min(dilations) != max(dilations) or min(dilations) != 1) + ) or ( + strides is not None and (min(strides) != max(strides) or min(strides) != 1) + ): + return _max_pool( + x, + auto_pad=auto_pad, + ceil_mode=ceil_mode, + dilations=dilations, + kernel_shape=kernel_shape, + pads=pads, + storage_order=storage_order, + strides=strides, + output_len=output_len + ) + + return common_pool( + "MAX", + 0, + x, + auto_pad=auto_pad, + ceil_mode=ceil_mode, + dilations=dilations, + kernel_shape=kernel_shape, + pads=pads, + strides=strides, + p=1 + ) +def _max_pool( # type: ignore + + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + pads, + storage_order, + strides, + output_len +): + if pads is None: + pads = [0 for i in range(len(kernel_shape) * 2)] + if strides is None: + strides = [1 for i in range(len(kernel_shape))] + if dilations is None: + dilations = [1 for i in range(len(kernel_shape))] + n_dims = len(kernel_shape) + new_pads = np.array([(pads[i], pads[i + n_dims]) for i in range(n_dims)]) + input_spatial_shape = x.shape[2:] + output_spatial_shape = [0 for s in input_spatial_shape] + if ceil_mode: + for i in range(len(input_spatial_shape)): + output_spatial_shape[i] = int( + np.ceil( + ( + input_spatial_shape[i] + + new_pads[i].sum() + - ((kernel_shape[i] - 1) * dilations[i] + 1) + ) + / strides[i] + + 1 + ) + ) + need_to_reduce_out_size_in_ceil_mode = ( + output_spatial_shape[i] - 1 + ) * strides[i] >= input_spatial_shape[i] + new_pads[i][0] + if need_to_reduce_out_size_in_ceil_mode: + output_spatial_shape[i] -= 1 + else: + for i in range(len(input_spatial_shape)): + output_spatial_shape[i] = int( + np.floor( + ( + input_spatial_shape[i] + + new_pads[i].sum() + - ((kernel_shape[i] - 1) * dilations[i] + 1) + ) + / strides[i] + + 1 + ) + ) + if auto_pad and auto_pad != "NOTSET": + # Deprecated attribute + if auto_pad in ("SAME_UPPER", "SAME_LOWER"): + for i in range(len(input_spatial_shape)): + if auto_pad == "SAME_UPPER": + output_spatial_shape[i] = int( + np.ceil(input_spatial_shape[i] / strides[i]) + ) + else: + output_spatial_shape[i] = int( + np.floor(input_spatial_shape[i] / strides[i]) + ) + pad_i = ( + (output_spatial_shape[i] - 1) * strides[i] + + ((kernel_shape[i] - 1) * dilations[i] + 1) + - input_spatial_shape[i] + ) + new_pads[i, 0] = pad_i // 2 + new_pads[i, 1] = pad_i - new_pads[i, 0] + else: + for i in range(len(input_spatial_shape)): + output_spatial_shape[i] = int( + np.ceil( + ( + input_spatial_shape[i] + - ((kernel_shape[i] - 1) * dilations[i] + 1) + + 1 + ) + / strides[i] + ) + ) + if len(input_spatial_shape) == 1: + return _max_pool_1d( + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len + ) + if len(input_spatial_shape) == 2: + return _max_pool_2d( + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len + ) + if len(input_spatial_shape) == 3: + return _max_pool_3d( + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len + ) + return _max_pool_nd( + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len + ) +def _max_pool_1d( # type: ignore + + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len +): + global_pooling = False + y_dims = x.shape[:2] + tuple(output_spatial_shape) + y = np.zeros(y_dims, dtype=x.dtype) + indices = np.full(y_dims, dtype=np.int64, fill_value=-1) + x_dims = x.shape + channels = x_dims[1] + height = x_dims[2] + pooled_height = y_dims[2] + total_channels = x_dims[0] * channels + stride_h = 1 if global_pooling else strides[0] + x_step = height + y_step = pooled_height + dilation_h = dilations[0] + X_data = x.ravel() + Y_data = y.ravel() + I_data = indices.ravel() + def iteration(c): + x_d = c * x_step + y_d = c * y_step + i_d = c * y_step + for ph in range(pooled_height): + hstart = ph * stride_h - new_pads[0, 0] + hend = hstart + kernel_shape[0] * dilation_h + Yh = None + h_index = -1 + for h in range(hstart, hend, dilation_h): + if h < 0 or h >= height: + continue + if Yh is None or X_data[x_d + h] > Yh: + Yh = X_data[x_d + h] + h_index = h + Y_data[y_d + ph] = Yh + I_data[i_d + ph] = c * x_step + h_index + for c in range(total_channels): + iteration(c) + if output_len == 1: # type: ignore + return (Y_data.reshape(y_dims),) + return (Y_data.reshape(y_dims), I_data.reshape(y_dims)) +def _max_pool_2d( # type: ignore + + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len +): + global_pooling = False + y_dims = x.shape[:2] + tuple(output_spatial_shape) + y = np.zeros(y_dims, dtype=x.dtype) + indices = np.full(y_dims, dtype=np.int64, fill_value=-1) + x_dims = x.shape + channels = x_dims[1] + height = x_dims[2] + width = x_dims[3] if len(kernel_shape) > 1 else 1 + pooled_height = y_dims[2] + pooled_width = y_dims[3] if len(kernel_shape) > 1 else 1 + total_channels = x_dims[0] * channels + stride_h = 1 if global_pooling else strides[0] + stride_w = 1 if global_pooling else strides[1] + x_step = height * width + y_step = pooled_height * pooled_width + dilation_h = dilations[0] + dilation_w = dilations[1] + X_data = x.ravel() + Y_data = y.ravel() + I_data = indices.ravel() + def iteration(c): # type: ignore + x_d = c * x_step # X_data + y_d = c * y_step # Y_data + for ph in range(pooled_height): + hstart = ph * stride_h - new_pads[0, 0] + hend = hstart + kernel_shape[0] * dilation_h + for pw in range(pooled_width): + wstart = pw * stride_w - new_pads[1, 0] + wend = wstart + kernel_shape[1] * dilation_w + + pool_index = ph * pooled_width + pw + Yh = None + h_index = -1 + w_index = -1 + for h in range(hstart, hend, dilation_h): + if h < 0 or h >= height: + continue + for w in range(wstart, wend, dilation_w): + if w < 0 or w >= width: + continue + input_index = h * width + w + if input_index < 0 or input_index > X_data.shape[0]: + continue + if Yh is None or X_data[x_d + input_index] > Yh: + Yh = X_data[x_d + input_index] + h_index = h + w_index = w + if Yh is None: + continue + Y_data[y_d + pool_index] = Yh + I_data[y_d + pool_index] = ( + c * x_step + h_index * width + w_index + if storage_order == 0 + else c * x_step + h_index + w_index * height + ) + for c in range(total_channels): + iteration(c) + if output_len == 1: # type: ignore + return (Y_data.reshape(y_dims),) + return (Y_data.reshape(y_dims), I_data.reshape(y_dims)) +def _max_pool_3d( # type: ignore + + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len +): + global_pooling = False + y_dims = x.shape[:2] + tuple(output_spatial_shape) + y = np.zeros(y_dims, dtype=x.dtype) + indices = np.full(y_dims, dtype=np.int64, fill_value=-1) + x_dims = x.shape + channels = x_dims[1] + height = x_dims[2] + width = x_dims[3] if len(kernel_shape) > 1 else 1 + depth = x_dims[4] if len(kernel_shape) > 2 else 1 + pooled_height = y_dims[2] + pooled_width = y_dims[3] if len(kernel_shape) > 1 else 1 + pooled_depth = y_dims[4] if len(kernel_shape) > 2 else 1 + total_channels = x_dims[0] * channels + stride_h = 1 if global_pooling else strides[0] + stride_w = 1 if global_pooling else strides[1] + stride_d = 1 if global_pooling else strides[2] + x_step = height * width * depth + y_step = pooled_height * pooled_width * pooled_depth + dilation_h = dilations[0] + dilation_w = dilations[1] + dilation_d = dilations[2] + X_data = x.ravel() + Y_data = y.ravel() + I_data = indices.ravel() + def iteration(c): + x_d = c * x_step + y_d = c * y_step + i_d = c * y_step + for ph in range(pooled_height): + hstart = ph * stride_h - new_pads[0, 0] + hend = hstart + kernel_shape[0] * dilation_h + for pw in range(pooled_width): + wstart = pw * stride_w - new_pads[1, 0] + wend = wstart + kernel_shape[1] * dilation_w + for pd in range(pooled_depth): + dstart = pd * stride_d - new_pads[2, 0] + dend = dstart + kernel_shape[2] * dilation_d + pool_index = ( + ph * pooled_width * pooled_depth + pw * pooled_depth + pd + ) + Yh = None + h_index = -1 + w_index = -1 + d_index = -1 + for h in range(hstart, hend, dilation_h): + if h < 0 or h >= height: + continue + for w in range(wstart, wend, dilation_w): + if w < 0 or w >= width: + continue + for d in range(dstart, dend, dilation_d): + if d < 0 or d >= depth: + continue + input_index = h * width * depth + w * depth + d + if Yh is None or X_data[x_d + input_index] > Yh: + Yh = X_data[x_d + input_index] + h_index = h + w_index = w + d_index = d + + + Y_data[y_d + pool_index] = Yh + I_data[i_d + pool_index] = ( + ( + c * x_step + + h_index * width * depth + + w_index * depth + + d_index + ) + if storage_order == 0 + else ( + c * x_step + + h_index + + w_index * height + + d_index * height * width + ) + ) + for c in range(total_channels): + iteration(c) + if output_len == 1: # type: ignore + return (Y_data.reshape(y_dims),) + return (Y_data.reshape(y_dims), I_data.reshape(y_dims)) +def stride(arr): + stride = np.zeros(len(arr)) + acc = 1 + for i in range(len(arr)): + stride[i] = acc + acc *= arr[-(i + 1)] + return np.flip(stride) +def reverse_stride(arr): + stride = np.zeros(len(arr)) + acc = 1 + for i in range(len(arr)): + acc *= arr[i] + stride[i] = acc + + return stride + + +def _max_pool_nd( # type: ignore + + x, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + new_pads, + storage_order, + strides, + output_spatial_shape, + output_len +): + nd = len(x.shape[2:]) + y_dims = x.shape[:2] + tuple(output_spatial_shape) + y = np.zeros(y_dims, dtype=x.dtype) + indices = np.full(y_dims, dtype=np.int64, fill_value=-1) + x_dims = x.shape + channels = x_dims[1] + x_stride = stride(x.shape) + y_stride = stride(y_dims) + total_channels = x_dims[0] * channels + x_step = x_stride[1] + y_step = y_stride[1] + X_data = x.ravel() + Y_data = y.ravel() + I_data = indices.ravel() + def iteration(c): + x_d = int(c * x_step) + y_d = int(c * y_step) + + for p in range(int(y_step)): + pool_index = p + flatten_index = p + + nstart = np.zeros(nd) + nend = np.zeros(nd) + nstep = np.zeros(nd) + + for n in range(nd): + pn, rem = divmod(flatten_index, y_stride[n + 2]) + flatten_index = rem + + ns = pn * strides[n] - new_pads[n, 0] + nstart[n] = ns + nend[n] = ns + kernel_shape[n] * dilations[n] + + nstep[n] = np.ceil((nend[n] - ns) / dilations[n]) + + nstride = stride(nstep) + max_iter = int(nstep[0] * nstride[0]) + n_index = np.full(y_dims, dtype=np.int64, fill_value=-1) + Yh = None + + for i in range(max_iter): + flatten_index = i + is_outside = False + input_index = 0 + + i_index = np.zeros(nd) + + for n in range(nd): + item, rem = divmod(flatten_index, nstride[n]) + flatten_index = rem + + item_ = item * dilations[n] + nstart[n] + if item_ < 0 or item_ >= x.shape[2 + n]: + is_outside = True + i_index[n] = item_ + input_index += item_ * x_stride[2 + n] + + input_index = int(input_index) + if is_outside == False: + if input_index < 0 or input_index > X_data.shape[0]: + continue + if Yh is None or X_data[x_d + input_index] > Yh: + Yh = X_data[x_d + input_index] + n_index = i_index + + + Y_data[y_d + p] = Yh + + for c in range(total_channels): + iteration(c) + if output_len == 1: # type: ignore + return (Y_data.reshape(y_dims),) + return (Y_data.reshape(y_dims), I_data.reshape(y_dims)) + + + +import itertools +import math +from typing import Sequence, Tuple, Union +import numpy as np + + + +def get_pad_shape( + auto_pad: str, + input_spatial_shape: Sequence[int], + kernel_spatial_shape: Sequence[int], + strides_spatial: Sequence[int], + output_spatial_shape: Sequence[int], +) -> Sequence[int]: + spatial_dims = len(input_spatial_shape) + pad_shape = [0] * spatial_dims + strides_spatial = strides_spatial or [1] * spatial_dims + if auto_pad in ("SAME_UPPER", "SAME_LOWER"): + for i in range(spatial_dims): + pad_shape[i] = ( + (output_spatial_shape[i] - 1) * strides_spatial[i] + + kernel_spatial_shape[i] + - input_spatial_shape[i] + ) + elif auto_pad == "VALID": + pass + + return pad_shape +def get_pad_with_auto_pad(auto_pad: str, pad_shape: Sequence[int]) -> Sequence[int]: + spatial_dims = len(pad_shape) + if auto_pad == "SAME_UPPER": + pads = [pad_shape[i] // 2 for i in range(spatial_dims)] + [ + pad_shape[i] - pad_shape[i] // 2 for i in range(spatial_dims) + ] + elif auto_pad == "SAME_LOWER": + pads = [pad_shape[i] - pad_shape[i] // 2 for i in range(spatial_dims)] + [ + pad_shape[i] // 2 for i in range(spatial_dims) + ] + else: + pads = [0] * spatial_dims * 2 # no padding + return pads + +def get_output_shape_explicit_padding( + pads: Sequence[int], + input_spatial_shape: Sequence[int], + kernel_spatial_shape: Sequence[int], + strides_spatial: Sequence[int], + dilations: Union[Sequence[int], None] = None, + ceil_mode: bool = False, +) -> Tuple[Sequence[int], Sequence[int]]: + + output_spatial_shape = [0] * len(input_spatial_shape) + pads = pads or [0] * len(input_spatial_shape) * 2 + strides_spatial = strides_spatial or [1] * len(input_spatial_shape) + dims = len(input_spatial_shape) + if dilations is None: + dilations = np.ones([dims], dtype=np.int64) + + for dim in range(dims): + dim_size = ( + input_spatial_shape[dim] + + pads[dim] + + pads[dims + dim] + - dilations[dim] * (kernel_spatial_shape[dim] - 1) + - 1 + ) / strides_spatial[dim] + 1 + + if ceil_mode: + output_spatial_shape[dim] = int(np.ceil(dim_size)) + else: + output_spatial_shape[dim] = int(np.floor(dim_size)) + + pads_spatial_shape_new = pads[:] + for dim in range(dims): + sliding_window_size = (kernel_spatial_shape[dim] - 1) * dilations[dim] + 1 + actual_padded_input_size = (output_spatial_shape[dim] - 1) * strides_spatial[ + dim + ] + sliding_window_size + extra_pad = ( + actual_padded_input_size + - input_spatial_shape[dim] + - pads[dim] + - pads[dims + dim] + ) + if extra_pad > 0: + pads_spatial_shape_new[dim] += extra_pad // 2 + pads_spatial_shape_new[dims + dim] += extra_pad - extra_pad // 2 + + return output_spatial_shape, pads_spatial_shape_new + +def get_output_shape_auto_pad( + auto_pad: str, + input_spatial_shape: Sequence[int], + kernel_spatial_shape: Sequence[int], + strides_spatial: Sequence[int], +) -> Sequence[int]: + strides_spatial = strides_spatial or [1] * len(input_spatial_shape) + out_shape = [0] * len(input_spatial_shape) + for i in range(len(input_spatial_shape)): + if auto_pad in ("SAME_UPPER", "SAME_LOWER"): + out_shape[i] = ( + math.floor((input_spatial_shape[i] - 1) / strides_spatial[i]) + 1 + ) + elif auto_pad == "VALID": + out_shape[i] = ( + math.floor( + (input_spatial_shape[i] - kernel_spatial_shape[i]) + / strides_spatial[i] + ) + + 1 + ) + else: + raise ValueError( + "auto_pad can only be NOTSET, SAME_UPPER, SAME_LOWER, or VALID" + ) + + return out_shape + +def lp_pool(x: np.array, p: int) -> float: + y = 0 + for v in np.nditer(x): + y += abs(v) ** p + return y ** (1.0 / p) + +def pool( + padded: np.ndarray, + x_shape: Sequence[int], + kernel: Sequence[int], + strides: Sequence[int], + out_shape: Sequence[int], + pooling_type: str, + pads: Union[Sequence[int], None] = None, + dilations: Union[Sequence[int], None] = None, + count_include_pad: int = 0, + p: int = 1, +) -> np.ndarray: + spatial_size = len(x_shape) - 2 + y = np.zeros([x_shape[0], x_shape[1], *list(out_shape)], dtype=padded.dtype) + if dilations is None: + dilations = np.ones([spatial_size], dtype=np.int64) + if pads is None: + pads = np.zeros([spatial_size * 2], dtype=np.int64) + elif len(pads) == 1: + pads = pads * spatial_size * 2 + strides = strides or [1] * spatial_size + + def lp_pool_p(x): + return lp_pool(x, p) + + + + for shape in itertools.product( + range(x_shape[0]), + range(x_shape[1]), + *[ + range( + int( + ( + x_shape[i + 2] + + pads[i] + + pads[i + spatial_size] + - (1 + (kernel[i] - 1) * dilations[i]) + ) + / strides[i] + + 1 + ) + ) + for i in range(spatial_size) + ], + ): + window = padded[shape[0], shape[1]] + window_vals = np.array( + [ + window[i] + for i in list( + itertools.product( + *[ + range( + strides[i] * shape[i + 2], + strides[i] * shape[i + 2] + + (1 + (kernel[i] - 1) * dilations[i]), + dilations[i], + ) + for i in range(spatial_size) + ] + ) + ) + ] + ) + if pooling_type == "AVG": + f = np.average + elif pooling_type == "MAX": + f = np.max + elif pooling_type == "LPPOOL": + f = lp_pool_p + else: + raise NotImplementedError( + f"Pooling type {pooling_type} does not support. Should be AVG, MAX" + ) + + if count_include_pad == 1 and (pooling_type in {"AVG", "LPPOOL"}): + y[shape] = f(window_vals) + else: + y[shape] = f(window_vals[np.where(~np.isnan(window_vals))]) + return y + + +def common_pool( + pooling_type, + count_include_pad, + x, + auto_pad=None, + ceil_mode=None, + dilations=None, + kernel_shape=None, + pads=None, + strides=None, + p=None, +): + x_shape = np.shape(x) + pading_value = np.nan if pooling_type == "MAX" or count_include_pad == 0 else 0 + if auto_pad in ["SAME_UPPER", "SAME_LOWER", "VALID"]: + assert ( + ceil_mode is None or ceil_mode == 0 + ), "ceil_mode is not supported with auto_pad" + out_shape = get_output_shape_auto_pad( + auto_pad, x.shape[2:], kernel_shape, strides + ) + pads_shape = get_pad_shape( + auto_pad, x_shape[2:], kernel_shape, strides, out_shape + ) + pads = get_pad_with_auto_pad(auto_pad, pads_shape) + n_dims = len(pads) // 2 + pads_np = [(pads[i], pads[i + n_dims]) for i in range(n_dims)] + padded = np.pad( + x, + ((0, 0), (0, 0), *pads_np), + mode="constant", + constant_values=pading_value, + ) + y = pool( + padded, + x_shape, + kernel_shape, + strides, + out_shape, + pooling_type, + pads, + dilations, + count_include_pad, + p, + ) + return (y,) + else: + out_shape, pads = get_output_shape_explicit_padding( + pads, x_shape[2:], kernel_shape, strides, dilations, ceil_mode + ) + # convert pads from [x1_begin, x2_begin,...,x1_end, x2_end,...] to [(x1_begin, x1_end), (x2_begin, x2_end),...] + n_dims = len(pads) // 2 + pads_np = [(pads[i], pads[i + n_dims]) for i in range(n_dims)] + padded = np.pad( + x, + ((0, 0), (0, 0), *pads_np), + mode="constant", + constant_values=pading_value, + ) + y = pool( + padded, + x_shape, + kernel_shape, + strides, + out_shape, + pooling_type, + pads, + dilations, + count_include_pad, + p, + ) + return (y,) + +class Max_pool(RunAll): + + @staticmethod + def export_maxpool_1d() -> None: + + x = np.random.randn(1, 3, 32).astype(np.float32) + kernel_shape = np.array([2]) + strides = np.array([2]) + padded = x + y = max_pool(padded, kernel_shape=kernel_shape, strides=strides,output_len=1) + y = np.array(y[0]) + + 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 = "maxpool_1d" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![2].span())," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_1d_default() -> None: + + x = np.random.randn(1, 3, 32).astype(np.float32) + kernel_shape = np.array([2]) + padded = x + y = max_pool(padded, kernel_shape=kernel_shape,output_len=1) + y = np.array(y[0]) + + 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 = "maxpool_1d_default" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + + @staticmethod + def export_maxpool_2d() -> None: + x = np.array( + [ + [ + [ + [1, 2, 3, 4, 5], + [6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25], + ] + ] + ] + ).astype(np.float32) + + kernel_shape=(2, 2) + strides=(2, 2) + padded = x + y = max_pool(padded,strides = strides,kernel_shape=kernel_shape,output_len=1) + y = np.array(y[0]) + + 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 = "maxpool_2d" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![2, 2].span())," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_2d_default() -> None: + x = np.random.randn(1, 3, 8, 8).astype(np.float32) + kernel_shape = (2, 2) + padded = x + y = max_pool(padded, kernel_shape=kernel_shape, output_len=1) + y = np.array(y[0]) + + 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 = "maxpool_2d_default" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + + def export_maxpool_2d_pads_default() -> None: + x = np.array( + [ + [ + [ + [1, 2, 3, 4, 5], + [6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25], + ] + ] + ] + ).astype(np.uint8) + kernel_shape=(5, 5) + pads=(2, 2, 2, 2) + padded = x + y = max_pool(padded,pads = pads,kernel_shape=kernel_shape,output_len=1) + y = np.array(y[0]) + + + 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 = "maxpool_2d_pads_default" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![5, 5].span()," + func_sig += "Option::Some(array![2, 2, 2, 2].span())," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_2d_constraint_index() -> None: + x = np.array( + [ + [ + [ + [1, 2, 3, 4, 5], + [6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25], + ] + ] + ] + ).astype(np.float32) + y = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32) + z = np.array([[[[6, 16], [8, 18]]]]).astype(np.int64) + + kernel_shape=(2, 2) + strides=(2, 2) + padded = x + (y, z) = max_pool(padded,strides = strides,kernel_shape=kernel_shape,output_len=2, storage_order=1) + + y = np.array(y) + z = np.array(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.I32, z.shape, z.flatten()) + + + name = "maxpool_2d_constraint_index" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::Some(1)," + func_sig += "Option::Some(array![2, 2].span())," + func_sig += "1)" + make_test( + [x], z, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_2d_same_upper() -> None: + x = np.array( + [ + [ + [ + [1, 2, 3, 4, 5], + [6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25], + ] + ] + ] + ).astype(np.float32) + + kernel_shape=(3, 3) + strides=(2, 2) + padded = x + y = max_pool(padded,strides = strides,kernel_shape=kernel_shape,auto_pad="SAME_UPPER") + y = np.array(y[0]) + + 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 = "maxpool_2d_same_upper" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::Some(AUTO_PAD::SAME_UPPER)," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![3, 3].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![2, 2].span())," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_2d_same_upper_default() -> None: + x = np.random.randn(1, 3, 8, 8).astype(np.float32) + kernel_shape = (2, 2) + padded = x + y = max_pool(padded,auto_pad="SAME_UPPER", kernel_shape=kernel_shape, output_len=1) + y = np.array(y[0]) + + 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 = "maxpool_2d_same_upper_default" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::Some(AUTO_PAD::SAME_UPPER)," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_2d_same_lower_default() -> None: + x = np.random.randn(1, 3, 8, 8).astype(np.float32) + x_shape = np.shape(x) + kernel_shape = (2, 2) + padded = x + y = max_pool(padded,auto_pad="SAME_LOWER", kernel_shape=kernel_shape, output_len=1) + y = np.array(y[0]) + + + 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 = "maxpool_2d_same_lower_default" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::Some(AUTO_PAD::SAME_LOWER)," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "array![2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_2d_ceil() -> None: + x = np.array( + [ + [ + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ] + ] + ] + ).astype(np.float32) + y = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32) + + kernel_shape = (3, 3) + strides = (2, 2) + padded = x + y = max_pool(padded,strides = strides, ceil_mode = True,kernel_shape=kernel_shape, output_len=1) + y = np.array(y[0]) + + + 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 = "maxpool_2d_ceil" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::Some(1)," + func_sig += "Option::None," + func_sig += "array![3, 3].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![2, 2].span())," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_2d_dilations() -> None: + x = np.array( + [ + [ + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ] + ] + ] + ).astype(np.float32) + + kernel_shape = (2 , 2) + dilations = (2, 2) + padded = x + y = max_pool(padded,dilations = dilations, ceil_mode = True,kernel_shape=kernel_shape, output_len=1) + y = np.array(y[0]) + + 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 = "maxpool_2d_dilations" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![2, 2].span())," + func_sig += "array![2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_3d_dilations() -> None: + + x = np.array( + [ + [ + [ + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ], + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ], + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ], + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ], + ] + ] + ] + ).astype(np.float32) + kernel_shape=(2, 2, 2) + strides=(1, 1, 1) + dilations=(2, 2, 2) + padded = x + y = max_pool(padded, dilations=dilations, kernel_shape=kernel_shape, strides=strides,output_len=1) + y = np.array(y[0]) + + + 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 = "maxpool_3d_dilations" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![2, 2, 2].span())," + func_sig += "array![2, 2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![1, 1, 1].span())," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + @staticmethod + def export_maxpool_4d_dilations() -> None: + x = np.random.randn(1, 3, 4, 4, 4, 4).astype(np.float32) + x_shape = np.shape(x) + kernel_shape = (2, 2, 2, 2) + strides = (1, 1, 1, 1) + dilations = (2, 2, 2, 2) + padded = x + y = max_pool(padded,dilations = dilations, ceil_mode = True,kernel_shape=kernel_shape, output_len=1) + y = np.array(y[0]) + + 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 = "maxpool_4d_dilations" + func_sig = "NNTrait::max_pool(" + func_sig += "@input_0," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::Some(array![2, 2, 2, 2].span())," + func_sig += "array![2, 2, 2, 2].span()," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "Option::None," + func_sig += "1)" + make_test( + [x], y, func_sig, name, Trait.NN) + + + + + \ No newline at end of file diff --git a/src/operators/ml/tree_ensemble/tree_ensemble_classifier.cairo b/src/operators/ml/tree_ensemble/tree_ensemble_classifier.cairo index c9edfd51c..4cc3c3134 100644 --- a/src/operators/ml/tree_ensemble/tree_ensemble_classifier.cairo +++ b/src/operators/ml/tree_ensemble/tree_ensemble_classifier.cairo @@ -240,7 +240,9 @@ trait TreeEnsembleClassifierTrait { /// ]) /// ``` /// - fn predict(classifier: TreeEnsembleClassifier, X: Tensor) -> (Span, MutMatrix::); + fn predict( + classifier: TreeEnsembleClassifier, X: Tensor + ) -> (Span, MutMatrix::); } impl TreeEnsembleClassifierImpl< @@ -259,7 +261,9 @@ impl TreeEnsembleClassifierImpl< +Div, +Mul > of TreeEnsembleClassifierTrait { - fn predict(classifier: TreeEnsembleClassifier, X: Tensor) -> (Span, MutMatrix::) { + fn predict( + classifier: TreeEnsembleClassifier, X: Tensor + ) -> (Span, MutMatrix::) { let mut classifier = classifier; let leaves_index = classifier.ensemble.leave_index_tree(X); let n_classes = classifier.classlabels.len(); diff --git a/src/operators/ml/tree_ensemble/tree_ensemble_regressor.cairo b/src/operators/ml/tree_ensemble/tree_ensemble_regressor.cairo index 136af9aa0..3f9998e96 100644 --- a/src/operators/ml/tree_ensemble/tree_ensemble_regressor.cairo +++ b/src/operators/ml/tree_ensemble/tree_ensemble_regressor.cairo @@ -299,12 +299,18 @@ impl TreeEnsembleRegressorImpl< let mut t_index = t_index.span(); match regressor.aggregate_function { - AGGREGATE_FUNCTION::SUM => { compute_res_SUM(ref regressor, ref res, ref t_index, i); }, + AGGREGATE_FUNCTION::SUM => { + compute_res_SUM(ref regressor, ref res, ref t_index, i); + }, AGGREGATE_FUNCTION::AVERAGE => { compute_res_AVERAGE(ref regressor, ref res, ref t_index, n_trees, i); }, - AGGREGATE_FUNCTION::MIN => { compute_res_MIN(ref regressor, ref res, ref t_index, i); }, - AGGREGATE_FUNCTION::MAX => { compute_res_MAX(ref regressor, ref res, ref t_index, i); }, + AGGREGATE_FUNCTION::MIN => { + compute_res_MIN(ref regressor, ref res, ref t_index, i); + }, + AGGREGATE_FUNCTION::MAX => { + compute_res_MAX(ref regressor, ref res, ref t_index, i); + }, }; i += 1; }; diff --git a/src/operators/nn.cairo b/src/operators/nn.cairo index 625e63216..42755a102 100644 --- a/src/operators/nn.cairo +++ b/src/operators/nn.cairo @@ -1,6 +1,9 @@ mod core; mod implementations; mod functional; +mod common; + +use orion::operators::nn::common::{AUTO_PAD, POOLING_TYPE}; use orion::operators::nn::core::NNTrait; diff --git a/src/operators/nn/common.cairo b/src/operators/nn/common.cairo new file mode 100644 index 000000000..d10ad8430 --- /dev/null +++ b/src/operators/nn/common.cairo @@ -0,0 +1,14 @@ +#[derive(Copy, Drop)] +enum AUTO_PAD { + NOTSET, + SAME_UPPER, + SAME_LOWER, + VALID +} + +#[derive(Copy, Drop)] +enum POOLING_TYPE { + AVG, + LPPOOL, + MAX, +} diff --git a/src/operators/nn/core.cairo b/src/operators/nn/core.cairo index 93f9242c0..032880942 100644 --- a/src/operators/nn/core.cairo +++ b/src/operators/nn/core.cairo @@ -1,4 +1,5 @@ use orion::operators::tensor::core::Tensor; +use orion::operators::nn::AUTO_PAD; /// Trait /// @@ -1304,4 +1305,124 @@ trait NNTrait { mode: Option, padding_mode: Option, ) -> Tensor; + /// + /// # NNTrait::max_pool + /// + /// ```rust + /// fn max_pool( + /// X: @Tensor, + /// auto_pad: Option, + /// ceil_mode: Option, + /// dilations: Option>, + /// kernel_shape: Span, + /// pads: Option>, + /// storage_order: Option, + /// strides: Option>, + /// output_len: usize, + /// ) -> (Tensor, Option>); + /// ``` + /// + /// MaxPool consumes an input tensor X and applies max pooling across the tensor according to kernel sizes, stride sizes, and pad lengths. max pooling consisting of computing the max on all values of a subset of the input tensor according to the kernel size and downsampling the data into the output tensor Y for further processing. The output spatial shape is calculated differently depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized. + /// + /// ## Args + /// + /// * `X`(`@Tensor`) - Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. + /// * `auto_pad`(`Option`) - Default is NOTSET, auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. NOTSET means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. + /// * `ceil_mode`(`Option`) - Default is 1, Whether to use ceil or floor (default) to compute the output shape. + /// * `dilations`(`Option>`) - Dilation value along each spatial axis of the filter. If not present, the dilation defaults to 1 along each spatial axis. + /// * `kernel_shape`(`Span`) - The size of the kernel along each axis. + /// * `pads`(`Option>`) - Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis. + /// * `storage_order`(`Option`) - Default is 0, The storage order of the tensor. 0 is row major, and 1 is column major. + /// * `strides`(`Option>`) - Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis. + /// * `output_len`(`Option`) - Default is 1, If set to 2, return the indices tensor. + /// + /// ## Returns + /// + /// A `Tensor` that contains the result of the max pool. + /// A `Option>` with the indices tensor from max pooling across the input tensor. The dimensions of indices are the same as output tensor. + /// ## Examples + /// + /// ```rust + /// use orion::operators::nn::NNTrait; + /// use orion::numbers::FixedTrait; + /// use orion::operators::nn::FP16x16NN; + /// use orion::numbers::FP16x16; + /// use orion::operators::tensor::{Tensor, TensorTrait, FP16x16Tensor}; + /// + /// + /// fn example_max_pool() -> (Tensor, Option>) { + /// let mut shape = ArrayTrait::::new(); + /// shape.append(1); + /// shape.append(1); + /// shape.append(5); + /// shape.append(5); + /// 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 }); + /// data.append(FP16x16 { mag: 655360, sign: false }); + /// data.append(FP16x16 { mag: 720896, sign: false }); + /// data.append(FP16x16 { mag: 786432, sign: false }); + /// data.append(FP16x16 { mag: 851968, sign: false }); + /// data.append(FP16x16 { mag: 917504, sign: false }); + /// data.append(FP16x16 { mag: 983040, sign: false }); + /// data.append(FP16x16 { mag: 1048576, sign: false }); + /// data.append(FP16x16 { mag: 1114112, sign: false }); + /// data.append(FP16x16 { mag: 1179648, sign: false }); + /// data.append(FP16x16 { mag: 1245184, sign: false }); + /// data.append(FP16x16 { mag: 1310720, sign: false }); + /// data.append(FP16x16 { mag: 1376256, sign: false }); + /// data.append(FP16x16 { mag: 1441792, sign: false }); + /// data.append(FP16x16 { mag: 1507328, sign: false }); + /// data.append(FP16x16 { mag: 1572864, sign: false }); + /// data.append(FP16x16 { mag: 1638400, sign: false }); + /// let mut X = TensorTrait::new(shape.span(), data.span()); + /// return NNTrait::max_pool( + /// @X, + /// Option::None, + /// Option::None, + /// Option::None, + /// array![5, 5, 5].span(), + /// Option::Some(array![2, 2, 2, 2].span()), + /// Option::None, + /// Option::None, + /// 1 + /// ); + /// + /// } + /// + /// >>> ([ + /// [ + /// [ + /// [13, 14, 15, 15, 15], + /// [18, 19, 20, 20, 20], + /// [23, 24, 25, 25, 25], + /// [23, 24, 25, 25, 25], + /// [23, 24, 25, 25, 25], + /// ] + /// ] + /// ], + /// Option::None) + /// + /// + /// ```` + /// + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>); } diff --git a/src/operators/nn/functional.cairo b/src/operators/nn/functional.cairo index 45e1c1ec9..f02570148 100644 --- a/src/operators/nn/functional.cairo +++ b/src/operators/nn/functional.cairo @@ -16,3 +16,5 @@ mod conv_transpose; mod depth_to_space; mod space_to_depth; mod conv; +mod max_pool; +mod common_pool; diff --git a/src/operators/nn/functional/common_pool.cairo b/src/operators/nn/functional/common_pool.cairo new file mode 100644 index 000000000..02d8826ce --- /dev/null +++ b/src/operators/nn/functional/common_pool.cairo @@ -0,0 +1,945 @@ +use core::clone::Clone; +use core::option::OptionTrait; +use core::array::ArrayTrait; +use orion::numbers::NumberTrait; +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor, I32Tensor}; +use orion::operators::vec::{NullableVec, NullableVecImpl}; +use orion::operators::tensor::core::{stride}; +use core::debug::PrintTrait; +use core::traits::Into; +use orion::numbers::{U32IntoI32, I32IntoU32, I32Div, I32Number}; +use orion::numbers::FP16x16; +use orion::operators::nn::{AUTO_PAD, POOLING_TYPE}; + + +fn common_pool< + T, + MAG, + +TensorTrait, + +NumberTrait, + +Copy, + +Drop, + +Add, + +Mul, + +Sub, + +Div, + +AddEq, + +PrintTrait, + +PartialOrd, + +PartialEq, + +TryInto, + +Into, + +Into, + +Rem, + +Neg, + +SubEq, +>( + pooling_type: POOLING_TYPE, + count_include_pad: usize, + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + strides: Option>, + p: usize, +) -> (Tensor, Option>) { + let padding_value: T = match pooling_type { + POOLING_TYPE::AVG => { + let padding_value = if count_include_pad == 0 { + NumberTrait::min_value() + } else { + NumberTrait::zero() + }; + padding_value + }, + POOLING_TYPE::LPPOOL => { + let padding_value = if count_include_pad == 0 { + NumberTrait::min_value() + } else { + NumberTrait::zero() + }; + padding_value + }, + POOLING_TYPE::MAX => { NumberTrait::min_value() }, + }; + + let ceil_mode = match ceil_mode { + Option::Some(ceil_mode) => { ceil_mode }, + Option::None => { 0 }, + }; + + let auto_pad = match auto_pad { + Option::Some(auto_pad) => auto_pad, + Option::None => AUTO_PAD::NOTSET, + }; + + let (out_shape, pads, padded) = match auto_pad { + AUTO_PAD::NOTSET => { + let (out_shape, pads) = get_output_shape_explicit_padding( + pads, + SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2), + kernel_shape, + strides, + dilations, + ceil_mode + ); + let padded = pad_constant_value(X, padding_value, pads); + (out_shape, pads, padded) + }, + AUTO_PAD::SAME_UPPER => { + assert(ceil_mode == 0, 'ceil mode not supp with autopad'); + let out_shape = get_output_shape_auto_pad( + auto_pad, + SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2), + kernel_shape, + strides + ); + let pads_shape = get_pad_shape( + auto_pad, + SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2), + kernel_shape, + strides, + out_shape + ); + + let pads = get_pad_with_auto_pad(auto_pad, pads_shape); + + let padded = pad_constant_value(X, padding_value, pads); + (out_shape, pads, padded) + }, + AUTO_PAD::SAME_LOWER => { + assert(ceil_mode == 0, 'ceil mode not supp with autopad'); + let out_shape = get_output_shape_auto_pad( + auto_pad, + SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2), + kernel_shape, + strides + ); + let pads_shape = get_pad_shape( + auto_pad, + SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2), + kernel_shape, + strides, + out_shape + ); + + let pads = get_pad_with_auto_pad(auto_pad, pads_shape); + + let padded = pad_constant_value(X, padding_value, pads); + (out_shape, pads, padded) + }, + AUTO_PAD::VALID => { + assert(ceil_mode == 0, 'ceil mode not supp with autopad'); + let out_shape = get_output_shape_auto_pad( + auto_pad, + SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2), + kernel_shape, + strides + ); + let pads_shape = get_pad_shape( + auto_pad, + SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2), + kernel_shape, + strides, + out_shape + ); + + let pads = get_pad_with_auto_pad(auto_pad, pads_shape); + + let padded = pad_constant_value(X, padding_value, pads); + (out_shape, pads, padded) + }, + }; + + return ( + pool( + @padded, + (*X).shape, + kernel_shape, + strides, + out_shape, + pooling_type, + pads, + dilations, + count_include_pad, + p, + ), + Option::None + ); +} + + +fn pool< + T, + MAG, + +TensorTrait, + +NumberTrait, + +Copy, + +Drop, + +Add, + +Mul, + +Sub, + +Div, + +AddEq, + +PrintTrait, + +PartialOrd, + +PartialEq, + +TryInto, + +Into, + +Into, + +Rem, + +Neg, + +SubEq, +>( + padded: @Tensor, + x_shape: Span, + kernel: Span, + strides: Option>, + out_shape: Span, + pooling_type: POOLING_TYPE, + pads: Span, + dilations: Option>, + count_include_pad: usize, + p: usize, +) -> Tensor { + let n_dims = x_shape.len() - 2; + let mut y = NullableVecImpl::new(); + + let dilations = match dilations { + Option::Some(dilations) => dilations, + Option::None => { + let mut dilations = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + dilations.append(1); + i += 1; + }; + dilations.span() + }, + }; + + let strides = match strides { + Option::Some(strides) => strides, + Option::None => { + let mut strides = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + strides.append(1); + i += 1; + }; + strides.span() + }, + }; + let mut y_shape = array![*x_shape.at(0), *x_shape.at(1)]; + let mut i = 0; + loop { + if i == n_dims { + break; + } + let a: T = NumberTrait::new_unscaled( + (*x_shape.at(i + 2) + *pads.at(i) + *pads.at(i + n_dims)).into(), false + ); + let b: T = NumberTrait::new_unscaled( + ((1 + (*kernel.at(i) - 1) * *dilations.at(i))).into(), false + ); + let c: T = NumberTrait::new_unscaled((*strides.at(i)).into(), false); + y_shape.append(NumberTrait::floor(((a - b) / c + NumberTrait::one())).try_into().unwrap()); + i += 1; + }; + let y_stride = stride(y_shape.span()); + let padded_stride = stride(*padded.shape); + let mut all_coords = get_all_coords(y_shape.span()); + + loop { + match all_coords.pop_front() { + Option::Some(coord) => { + let coord = *coord; + let window = SpanTrait::slice( + *padded.data, + *coord.at(0) * *padded_stride.at(0) + *coord.at(1) * *padded_stride.at(1), + *padded_stride.at(1) + ); + let window_stride = SpanTrait::slice(padded_stride, 2, n_dims); + let mut window_vals = ArrayTrait::new(); + + let mut all_indices = ArrayTrait::new(); + + let mut i = 0; + loop { + if i == n_dims { + break; + } + let start = *strides.at(i) * *coord.at(i + 2); + let end = start + 1 + (*kernel.at(i) - 1) * *dilations.at(i); + let step = *dilations.at(i); + + all_indices.append(arange(start, end, step)); + + i += 1; + }; + + let mut all_indices = cartesian(all_indices.span()); + + loop { + match all_indices.pop_front() { + Option::Some(index) => { + let flatten_index = flatten_index((*index), window_stride); + + window_vals.append(*window.at(flatten_index)); + }, + Option::None => { break; } + } + }; + match pooling_type { + POOLING_TYPE::AVG => { + let flatten_index = flatten_index(coord, y_stride); + + if count_include_pad == 1 { + y.set(flatten_index, average(window_vals.span())); + } else { + y.set(flatten_index, average(window_vals.span())); + } + }, + POOLING_TYPE::LPPOOL => { panic(array!['supported soon']) }, + POOLING_TYPE::MAX => { + let flatten_index = flatten_index(coord, y_stride); + + y.set(flatten_index, max(window_vals.span())); + } + } + }, + Option::None => { break; }, + } + }; + let mut y_data = ArrayTrait::new(); + let mut i = 0; + loop { + if i == y.len() { + break; + } + y_data.append(y.at(i)); + i += 1; + }; + return TensorTrait::new(y_shape.span(), y_data.span()); +} + +fn get_output_shape_auto_pad( + auto_pad: AUTO_PAD, + input_spatial_shape: Span, + kernel_spatial_shape: Span, + strides_spatial: Option>, +) -> Span { + let n_dims = input_spatial_shape.len(); + let mut out_shape = ArrayTrait::new(); + + let strides_spatial = match strides_spatial { + Option::Some(strides_spatial) => strides_spatial, + Option::None => { + let mut strides_spatial = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + strides_spatial.append(1); + i += 1; + }; + strides_spatial.span() + }, + }; + + match auto_pad { + AUTO_PAD::NOTSET => { panic(array!['not supported!']) }, + AUTO_PAD::SAME_UPPER => { + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + out_shape.append((*input_spatial_shape.at(i) - 1) / *strides_spatial.at(i) + 1); + i += 1; + }; + out_shape.span() + }, + AUTO_PAD::SAME_LOWER => { + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + out_shape.append((*input_spatial_shape.at(i) - 1) / *strides_spatial.at(i) + 1); + i += 1; + }; + out_shape.span() + }, + AUTO_PAD::VALID => { + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + out_shape + .append( + (*input_spatial_shape.at(i) - *kernel_spatial_shape.at(i)) + / *strides_spatial.at(i) + + 1 + ); + i += 1; + }; + out_shape.span() + }, + } +} + +fn get_output_shape_explicit_padding( + pads: Option>, + input_spatial_shape: Span, + kernel_spatial_shape: Span, + strides_spatial: Option>, + dilations: Option>, + ceil_mode: usize, +) -> (Span, Span) { + let n_dims = input_spatial_shape.len(); + let pads = match pads { + Option::Some(pads) => pads, + Option::None => { + let mut pads = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + pads.append(0); + pads.append(0); + i += 1; + }; + pads.span() + }, + }; + let dilations = match dilations { + Option::Some(dilations) => dilations, + Option::None => { + let mut dilations = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + dilations.append(1); + i += 1; + }; + dilations.span() + }, + }; + let strides_spatial = match strides_spatial { + Option::Some(strides_spatial) => strides_spatial, + Option::None => { + let mut strides_spatial = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + strides_spatial.append(1); + i += 1; + }; + strides_spatial.span() + }, + }; + let mut output_spatial_shape = ArrayTrait::::new(); + + let mut d = 0; + loop { + if d == n_dims { + break; + } + let dim_num: FP16x16 = NumberTrait::new_unscaled( + (*input_spatial_shape.at(d) + + *pads.at(d) + + *pads.at(d + n_dims) + - *dilations.at(d) * (*kernel_spatial_shape.at(d) - 1) + - 1) + .into(), + false + ); + let dim_den = NumberTrait::new_unscaled((*strides_spatial.at(d) + 1).into(), false); + + let dim_size = dim_num / dim_den; + + let oss = if ceil_mode == 1 { + NumberTrait::ceil(dim_size) + } else { + NumberTrait::floor(dim_size) + }; + output_spatial_shape.append(oss.try_into().unwrap()); + + d += 1; + }; + let output_spatial_shape = output_spatial_shape.span(); + + let mut pads_spatial_shape_new_1 = ArrayTrait::new(); + let mut pads_spatial_shape_new_2 = ArrayTrait::new(); + + let mut d = 0; + loop { + if d == n_dims { + break; + } + let sliding_window_size = (*kernel_spatial_shape.at(d) - 1) * *dilations.at(d) + 1; + let actual_padded_input_size = (*output_spatial_shape.at(d) - 1) * *strides_spatial.at(d) + + sliding_window_size; + let extra_pad_sub = I32Number::new( + (*input_spatial_shape.at(d) + *pads.at(d) + *pads.at(d + n_dims)).into(), false + ); + let extra_pad = I32Number::new((actual_padded_input_size).into(), false) - extra_pad_sub; + + if extra_pad > 0 { + pads_spatial_shape_new_1.append(*pads.at(d) + extra_pad.into() / 2); + pads_spatial_shape_new_2.append(*pads.at(d) + extra_pad.into() - extra_pad.into() / 2); + } else { + pads_spatial_shape_new_1.append(*pads.at(d)); + pads_spatial_shape_new_2.append(*pads.at(d + n_dims)); + }; + d += 1; + }; + + let mut pads_spatial_shape_new = ArrayTrait::new(); + pads_spatial_shape_new.append_span(pads_spatial_shape_new_1.span()); + pads_spatial_shape_new.append_span(pads_spatial_shape_new_2.span()); + + return (output_spatial_shape, pads_spatial_shape_new.span()); +} + + +fn get_pad_shape( + auto_pad: AUTO_PAD, + input_spatial_shape: Span, + kernel_spatial_shape: Span, + strides_spatial: Option>, + output_spatial_shape: Span, +) -> Span { + let spatial_dims = input_spatial_shape.len(); + let mut pad_shape = ArrayTrait::new(); + + let strides_spatial = match strides_spatial { + Option::Some(strides_spatial) => strides_spatial, + Option::None => { + let mut strides_spatial = ArrayTrait::new(); + let mut i = 0; + loop { + if i == spatial_dims { + break; + } + strides_spatial.append(1); + i += 1; + }; + strides_spatial.span() + }, + }; + + match auto_pad { + AUTO_PAD::NOTSET => { panic(array!['not supported!']) }, + AUTO_PAD::SAME_UPPER => { + let mut i = 0; + loop { + if i == spatial_dims { + break; + } + pad_shape + .append( + (*output_spatial_shape.at(i) - 1) * *strides_spatial.at(i) + + *kernel_spatial_shape.at(i) + - *input_spatial_shape.at(i) + ); + i += 1; + }; + pad_shape.span() + }, + AUTO_PAD::SAME_LOWER => { + let mut i = 0; + loop { + if i == spatial_dims { + break; + } + pad_shape + .append( + (*output_spatial_shape.at(i) - 1) * *strides_spatial.at(i) + + *kernel_spatial_shape.at(i) + - *input_spatial_shape.at(i) + ); + i += 1; + }; + pad_shape.span() + }, + AUTO_PAD::VALID => { + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + pad_shape.append(0); + i += 1; + }; + pad_shape.span() + }, + } +} + + +fn get_pad_with_auto_pad(auto_pad: AUTO_PAD, mut pad_shape: Span,) -> Span { + let spatial_dims = pad_shape.len(); + let mut pads = ArrayTrait::new(); + + match auto_pad { + AUTO_PAD::NOTSET => { array![].span() }, + AUTO_PAD::SAME_UPPER => { + let mut pads_1 = ArrayTrait::new(); + let mut pads_2 = ArrayTrait::new(); + + loop { + match pad_shape.pop_front() { + Option::Some(v) => { + pads_1.append(*v / 2); + pads_2.append(*v - *v / 2); + }, + Option::None => { + pads.append_span(pads_1.span()); + pads.append_span(pads_2.span()); + break pads.span(); + } + } + } + }, + AUTO_PAD::SAME_LOWER => { + let mut pads_1 = ArrayTrait::new(); + let mut pads_2 = ArrayTrait::new(); + + loop { + match pad_shape.pop_front() { + Option::Some(v) => { + pads_1.append(*v - *v / 2); + pads_2.append(*v / 2); + }, + Option::None => { + pads.append_span(pads_1.span()); + pads.append_span(pads_2.span()); + break pads.span(); + } + } + } + }, + AUTO_PAD::VALID => { + let mut i = 0; + loop { + if i == spatial_dims { + break; + } + pads.append(0); + pads.append(0); + i += 1; + }; + pads.span() + }, + } +} + +// X dimension : N x C x d1 x ... x dn, Padding on dimensions d1, ..., dn +fn pad_constant_value< + T, MAG, +TensorTrait, +NumberTrait, +Copy, +Drop, +PrintTrait +>( + mut X: @Tensor, constant_value: T, pads: Span +) -> Tensor { + let n_dims = pads.len() / 2; + let N = *(*X).shape.at(0); + let C = *(*X).shape.at(1); + + let mut padded_shape = array![N, C]; + + let mut i = 0; + loop { + if i == n_dims { + break; + } + padded_shape.append(*(*X).shape.at(i + 2) + *pads.at(i) + *pads.at(i + n_dims)); + i += 1; + }; + let x_stride = stride((*X).shape); + let padded_stride = stride(padded_shape.span()); + + let window_len = *x_stride.at(1); + let full_len = *padded_shape.at(0) * *padded_stride.at(0); + + let mut x_padded = full(full_len, constant_value); + + let total_channel = N * C; + + let mut c = 0; + loop { + if c == total_channel { + break; + } + + let mut i = 0; + loop { + if i == window_len { + break; + } + let mut padded_index = c * *padded_stride.at(1); + let mut flatten_index = i; + + let mut n = 0; + loop { + if n == n_dims { + break; + } + let (ind, rem) = DivRem::div_rem( + flatten_index, (*x_stride.at(2 + n)).try_into().unwrap() + ); + flatten_index = rem; + padded_index += (ind + *pads.at(n)) * *padded_stride.at(2 + n); + n += 1; + }; + + x_padded.set(padded_index, *(*X).data.at(c * window_len + i)); + i += 1; + }; + c += 1; + }; + + let mut padded = ArrayTrait::new(); + let mut i = 0; + loop { + if i == x_padded.len() { + break; + } + padded.append(x_padded.at(i)); + i += 1; + }; + return TensorTrait::new(padded_shape.span(), padded.span()); +} + + +// return a span of len ceil((end - start) / step) +fn full, +NumberTrait, +Copy, +Drop,>( + len: usize, fill_value: T +) -> NullableVec { + let mut full = NullableVecImpl::new(); + let mut i = 0; + loop { + if i == len { + break; + } + full.set(i, fill_value); + i += 1; + }; + return full; +} + + +fn flatten_index(index: Span, stride: Span) -> usize { + let mut flatten_index = 0; + let n = index.len(); + + let mut i = 0; + loop { + if i == n { + break; + } + flatten_index += *index.at(i) * *stride.at(i); + i += 1; + }; + + return flatten_index; +} + + +fn get_all_coords(shape: Span) -> Span> { + let mut all_indices = ArrayTrait::new(); + + let mut i = 0; + loop { + if i == shape.len() { + break; + } + all_indices.append(arange(0, *shape.at(i), 1)); + i += 1; + }; + + return cartesian(all_indices.span()); +} + +fn cartesian(mut arrays: Span>,) -> Span> { + let mut n = 1; + let mut i = arrays.len() - 1; + loop { + n = n * (*(arrays.at(i))).len(); + if i == 0 { + break; + } + i -= 1; + }; + + let mut i = 0; + let mut size_arrays = ArrayTrait::new(); + loop { + if i == arrays.len() { + break; + } + size_arrays.append((*(arrays.at(i))).len()); + + i += 1; + }; + let size_arrays = size_arrays.span(); + let mut output_arrays = ArrayTrait::>::new(); + let mut m = n; + + let mut i = 0; + loop { + if i == arrays.len() { + break; + } + m = m / (*(arrays.at(i))).len(); + let mut out = repeat(*(arrays.at(i)), m); + out = repeat_2(out, size_arrays, i); + + output_arrays.append(out); + i += 1; + }; + let output_arrays = output_arrays.span(); + + let mut i = 0; + let mut ret = ArrayTrait::new(); + loop { + if i == n { + break; + } + let mut j = 0; + let mut x = ArrayTrait::new(); + loop { + if j == arrays.len() { + break; + } + + x.append(*(output_arrays.at(j)).at(i)); + j += 1; + }; + ret.append(x.span()); + i += 1; + }; + + return ret.span(); +} + + +fn repeat_2(mut array: Array, size_array: Span, index: usize) -> Array { + let mut size = array.len(); + let mut i = 0; + loop { + if i == index { + break; + } + let mut j = 1; + loop { + if j == *size_array.at(index - 1 - i) { + break; + } + let mut k = 0; + loop { + if k == size { + break; + } + array.append(*array.at(k)); + k += 1; + }; + j += 1; + }; + size = size * *size_array.at(index - 1 - i); + i += 1; + }; + array +} + +fn repeat(array: Span, m: usize,) -> Array { + let mut out = ArrayTrait::new(); + let mut j = 0; + loop { + if j == array.len() { + break; + } + let mut k = 0; + loop { + if k == m { + break; + } + out.append(*array.at(j)); + k += 1; + }; + j += 1; + }; + + out +} + + +fn arange(start: usize, end: usize, step: usize) -> Span { + let mut arr = ArrayTrait::new(); + let mut i = start; + loop { + if i >= end { + break; + } + arr.append(i); + i += step; + }; + return arr.span(); +} + + +fn max, +Drop, +Copy, +PartialOrd,>(mut a: Span) -> T { + assert(a.len() > 0, 'span cannot be empty'); + + let mut max = *a.at(0); + loop { + match a.pop_front() { + Option::Some(v) => { if *v > max { + max = *v; + }; }, + Option::None => { break max; } + }; + } +} + + +fn average< + T, + MAG, + +NumberTrait, + +Into, + +AddEq, + +Drop, + +Copy, + +PartialOrd, + +Div +>( + mut a: Span +) -> T { + assert(a.len() > 0, 'span cannot be empty'); + + let mut sum = *a.at(0); + let n = NumberTrait::new_unscaled((a.len()).into(), false); + loop { + match a.pop_front() { + Option::Some(v) => { sum += *v; }, + Option::None => { break sum / n; } + }; + } +} + diff --git a/src/operators/nn/functional/gemm.cairo b/src/operators/nn/functional/gemm.cairo index e5b997731..b67754301 100644 --- a/src/operators/nn/functional/gemm.cairo +++ b/src/operators/nn/functional/gemm.cairo @@ -52,4 +52,4 @@ fn gemm< }, Option::None(_) => { return mul_by_scalar(@A.matmul(@B), alpha); } } -} \ No newline at end of file +} diff --git a/src/operators/nn/functional/max_pool.cairo b/src/operators/nn/functional/max_pool.cairo new file mode 100644 index 000000000..69e060a2b --- /dev/null +++ b/src/operators/nn/functional/max_pool.cairo @@ -0,0 +1,1084 @@ +use core::clone::Clone; +use core::option::OptionTrait; +use core::array::ArrayTrait; +use orion::numbers::NumberTrait; +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor, I32Tensor}; +use orion::operators::vec::{NullableVec, NullableVecImpl}; +use orion::operators::tensor::core::{stride}; +use core::debug::PrintTrait; +use core::traits::Into; +use orion::numbers::{U32IntoI32, I32IntoU32, I32Div, I32Number}; + +use orion::operators::nn::functional::common_pool::{common_pool}; +use orion::operators::nn::{AUTO_PAD, POOLING_TYPE}; + +/// Cf: NNTrait::max_pool docstring +fn max_pool< + T, + MAG, + +TensorTrait, + +NumberTrait, + +Copy, + +Drop, + +Add, + +Mul, + +Sub, + +Div, + +AddEq, + +PrintTrait, + +PartialOrd, + +PartialEq, + +TryInto, + +Into, + +Into, + +Rem, + +Neg, + +SubEq, +>( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, +) -> (Tensor, Option>) { + match dilations { + Option::Some(dilations) => { + if (min(dilations) != max(dilations) || min(dilations) != 1) { + max_pool_implementation( + X, + auto_pad, + ceil_mode, + Option::Some(dilations), + kernel_shape, + pads, + storage_order, + strides, + output_len, + ) + } else { + match strides { + Option::Some(strides) => { + if (min(strides) != max(strides) || min(strides) != 1) { + max_pool_implementation( + X, + auto_pad, + ceil_mode, + Option::Some(dilations), + kernel_shape, + pads, + storage_order, + Option::Some(strides), + output_len, + ) + } else { + common_pool( + POOLING_TYPE::MAX, + 0, + X, + auto_pad, + ceil_mode, + Option::Some(dilations), + kernel_shape, + pads, + Option::Some(strides), + 1, + ) + } + }, + Option::None => { + common_pool( + POOLING_TYPE::MAX, + 0, + X, + auto_pad, + ceil_mode, + Option::Some(dilations), + kernel_shape, + pads, + Option::None, + 1, + ) + }, + } + } + }, + Option::None => { + match strides { + Option::Some(strides) => { + if (min(strides) != max(strides) || min(strides) != 1) { + max_pool_implementation( + X, + auto_pad, + ceil_mode, + Option::None, + kernel_shape, + pads, + storage_order, + Option::Some(strides), + output_len, + ) + } else { + common_pool( + POOLING_TYPE::MAX, + 0, + X, + auto_pad, + ceil_mode, + Option::None, + kernel_shape, + pads, + Option::Some(strides), + 1, + ) + } + }, + Option::None => { + common_pool( + POOLING_TYPE::MAX, + 0, + X, + auto_pad, + ceil_mode, + Option::None, + kernel_shape, + pads, + Option::None, + 1, + ) + }, + } + } + } +} + + +fn max_pool_implementation< + T, + MAG, + +TensorTrait, + +NumberTrait, + +Copy, + +Drop, + +Add, + +Mul, + +Sub, + +Div, + +AddEq, + +PrintTrait, + +PartialOrd, + +PartialEq, + +TryInto, + +Into, + +Into, + +Rem, + +Neg, + +SubEq, +>( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, +) -> (Tensor, Option>) { + assert((*X).shape.len() >= 3, 'X must have at least 3 dim'); + let n_dims = kernel_shape.len(); + + let pads = match pads { + Option::Some(pads) => pads, + Option::None => { + let mut pads = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + pads.append(0); + pads.append(0); + i += 1; + }; + pads.span() + }, + }; + let dilations = match dilations { + Option::Some(dilations) => dilations, + Option::None => { + let mut dilations = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + dilations.append(1); + i += 1; + }; + dilations.span() + }, + }; + let strides = match strides { + Option::Some(strides) => strides, + Option::None => { + let mut strides = ArrayTrait::new(); + let mut i = 0; + loop { + if i == n_dims { + break; + } + strides.append(1); + i += 1; + }; + strides.span() + }, + }; + + let auto_pad = match auto_pad { + Option::Some(auto_pad) => auto_pad, + Option::None => AUTO_PAD::NOTSET, + }; + + let storage_order = match storage_order { + Option::Some(storage_order) => storage_order, + Option::None => 0, + }; + + let input_spatial_shape = SpanTrait::slice((*X).shape, 2, (*X).shape.len() - 2); + + let ceil_mode = match ceil_mode { + Option::Some(ceil_mode) => ceil_mode, + Option::None => 0, + }; + + let output_spatial_shape = if ceil_mode == 1 { + let mut output_spatial_shape = ArrayTrait::::new(); + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + let oss: T = NumberTrait::ceil( + (NumberTrait::new_unscaled( + (*input_spatial_shape.at(i) + *pads.at(i) + *pads.at(i + n_dims)).into(), false + ) + - NumberTrait::new_unscaled( + ((*kernel_shape.at(i) - 1) * *dilations.at(i) + 1).into(), false + )) + / NumberTrait::new_unscaled((*strides.at(i)).into(), false) + + NumberTrait::one() + ); + + let need_to_reduce_out_size_in_ceil_mode = (oss.try_into().unwrap() - 1) + * *strides.at(i) >= *input_spatial_shape.at(i) + + *pads.at(i); + if need_to_reduce_out_size_in_ceil_mode { + output_spatial_shape.append(oss.try_into().unwrap() - 1); + } else { + output_spatial_shape.append(oss.try_into().unwrap()); + }; + i += 1; + }; + + output_spatial_shape.span() + } else { + let mut output_spatial_shape = ArrayTrait::::new(); + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + let oss: T = NumberTrait::floor( + (NumberTrait::new_unscaled( + (*input_spatial_shape.at(i) + *pads.at(i) + *pads.at(i + n_dims)).into(), false + ) + - NumberTrait::new_unscaled( + ((*kernel_shape.at(i) - 1) * *dilations.at(i) + 1).into(), false + )) + / NumberTrait::new_unscaled((*strides.at(i)).into(), false) + + NumberTrait::one() + ); + output_spatial_shape.append(oss.try_into().unwrap()); + i += 1; + }; + output_spatial_shape.span() + }; + + let (pads, output_spatial_shape) = match auto_pad { + AUTO_PAD::NOTSET => { (pads, output_spatial_shape) }, + AUTO_PAD::SAME_UPPER => { + let mut output_spatial_shape = ArrayTrait::::new(); + let mut pad_1 = ArrayTrait::new(); + let mut pad_2 = ArrayTrait::new(); + let mut pads = ArrayTrait::new(); + + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + let oss: T = NumberTrait::ceil( + NumberTrait::new_unscaled((*input_spatial_shape.at(i)).into(), false) + / NumberTrait::new_unscaled((*strides.at(i)).into(), false) + ); + output_spatial_shape.append(oss.try_into().unwrap()); + + let pad_i = (*output_spatial_shape[i] - 1) * *strides[i] + + ((*kernel_shape[i] - 1) * *dilations[i] + 1) + - *input_spatial_shape[i]; + + pad_1.append(pad_i / 2); + pad_2.append(pad_i - (pad_i / 2)); + + i += 1; + }; + + pads.append_span(pad_1.span()); + pads.append_span(pad_2.span()); + + (pads.span(), output_spatial_shape.span()) + }, + AUTO_PAD::SAME_LOWER => { + let mut output_spatial_shape = ArrayTrait::::new(); + let mut pad_1 = ArrayTrait::new(); + let mut pad_2 = ArrayTrait::new(); + let mut pads = ArrayTrait::new(); + + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + + let oss: T = NumberTrait::floor( + NumberTrait::new_unscaled((*input_spatial_shape.at(i)).into(), false) + / NumberTrait::new_unscaled((*strides.at(i)).into(), false) + ); + output_spatial_shape.append(oss.try_into().unwrap()); + + let pad_i = (*output_spatial_shape[i] - 1) * *strides[i] + + ((*kernel_shape[i] - 1) * *dilations[i] + 1) + - *input_spatial_shape[i]; + + pad_1.append(pad_i / 2); + pad_2.append(pad_i - (pad_i / 2)); + + i += 1; + }; + + pads.append_span(pad_1.span()); + pads.append_span(pad_2.span()); + + (pads.span(), output_spatial_shape.span()) + }, + AUTO_PAD::VALID => { + let mut output_spatial_shape = ArrayTrait::::new(); + let mut i = 0; + loop { + if i == input_spatial_shape.len() { + break; + } + let oss: T = NumberTrait::ceil( + (NumberTrait::new_unscaled((*input_spatial_shape.at(i)).into(), false) + - NumberTrait::new_unscaled( + ((*kernel_shape.at(i) - 1) * *dilations.at(i) + 1).into(), false + ) + + NumberTrait::one()) + / NumberTrait::new_unscaled((*strides.at(i)).into(), false) + ); + output_spatial_shape.append(oss.try_into().unwrap()); + + i += 1; + }; + + (pads, output_spatial_shape.span()) + }, + }; + + let nd = input_spatial_shape.len(); + if nd == 1 { + return max_pool_1d( + X, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + pads, + storage_order, + strides, + output_spatial_shape, + output_len + ); + } + if nd == 2 { + return max_pool_2d( + X, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + pads, + storage_order, + strides, + output_spatial_shape, + output_len + ); + } + if nd == 3 { + return max_pool_3d( + X, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + pads, + storage_order, + strides, + output_spatial_shape, + output_len + ); + } + + return max_pool_nd( + X, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + pads, + storage_order, + strides, + output_spatial_shape, + output_len + ); +} + + +fn max_pool_1d, +NumberTrait, +Copy, +Drop, +PartialOrd,>( + X: @Tensor, + auto_pad: AUTO_PAD, + ceil_mode: usize, + dilations: Span, + kernel_shape: Span, + pads: Span, + storage_order: usize, + strides: Span, + output_spatial_shape: Span, + output_len: usize, +) -> (Tensor, Option>) { + let mut y_dims = ArrayTrait::new(); + y_dims.append_span(SpanTrait::slice((*X).shape, 0, 2)); + y_dims.append_span(output_spatial_shape); + + let N = *(*X).shape.at(0); + let C = *(*X).shape.at(1); + + let x_step = *(*X).shape.at(2); + let y_step = *y_dims.at(2); + + let total_channels = N * C; + + let stride_h = I32Number::new((*strides.at(0)).into(), false); + let dilation_h = I32Number::new((*dilations.at(0)).into(), false); + let ks_h = I32Number::new((*kernel_shape.at(0)).into(), false); + let pad_h = I32Number::new((*pads.at(0)).into(), false); + + let mut Y_data = ArrayTrait::new(); + let mut I_data = ArrayTrait::new(); + + let mut c = 0; + loop { + if c == total_channels { + break; + } + let x_d = c * x_step; + + let mut ph = 0; + loop { + if ph == y_step { + break; + } + let hstart = I32Number::new((ph).into(), false) * stride_h - pad_h; + let hend = hstart + ks_h * dilation_h; + + let mut h_index = I32Number::new(1, true); + let mut Yh: T = NumberTrait::min_value(); + + let mut h = hstart; + loop { + if h >= hend { + break; + } + if h >= 0 && h < x_step.into() { + if *(*X).data.at(x_d + h.into()) > Yh { + h_index = h.into(); + Yh = (*(*X).data.at(x_d + h.into())); + } + } + h += dilation_h; + }; + + Y_data.append(Yh); + I_data.append((c * x_step).into() + h_index); + + ph += 1; + }; + c += 1; + }; + if output_len == 1 { + return (TensorTrait::new(y_dims.span(), Y_data.span()), Option::>::None); + } + return ( + TensorTrait::new(y_dims.span(), Y_data.span()), + Option::Some(TensorTrait::new(y_dims.span(), I_data.span())) + ); +} + +fn max_pool_2d< + T, + MAG, + +TensorTrait, + +NumberTrait, + +Copy, + +Drop, + +PartialOrd, + +PartialEq, + +PrintTrait +>( + X: @Tensor, + auto_pad: AUTO_PAD, + ceil_mode: usize, + dilations: Span, + kernel_shape: Span, + pads: Span, + storage_order: usize, + strides: Span, + output_spatial_shape: Span, + output_len: usize, +) -> (Tensor, Option>) { + let mut y_dims = ArrayTrait::new(); + y_dims.append_span(SpanTrait::slice((*X).shape, 0, 2)); + y_dims.append_span(output_spatial_shape); + + let N = *(*X).shape.at(0); + let C = *(*X).shape.at(1); + let H = *(*X).shape.at(2); + let W = *(*X).shape.at(3); + + let pooled_H = *y_dims.at(2); + let pooled_W = *y_dims.at(3); + + let x_step = H * W; + + let total_channels = N * C; + + let stride_h = I32Number::new((*strides.at(0)).into(), false); + let stride_w = I32Number::new((*strides.at(1)).into(), false); + + let dilation_h = I32Number::new((*dilations.at(0)).into(), false); + let dilation_w = I32Number::new((*dilations.at(1)).into(), false); + + let ks_h = I32Number::new((*kernel_shape.at(0)).into(), false); + let ks_w = I32Number::new((*kernel_shape.at(1)).into(), false); + + let pad_h = I32Number::new((*pads.at(0)).into(), false); + let pad_w = I32Number::new((*pads.at(1)).into(), false); + + let mut Y_data = ArrayTrait::new(); + let mut I_data = ArrayTrait::new(); + + let X_len = (*X).data.len(); + + let mut c = 0; + loop { + if c == total_channels { + break; + } + let x_d = c * x_step; + + let mut ph = 0; + loop { + if ph == pooled_H { + break; + } + let hstart = I32Number::new((ph).into(), false) * stride_h - pad_h; + let hend = hstart + ks_h * dilation_h; + + let mut pw = 0; + loop { + if pw == pooled_W { + break; + } + let wstart = I32Number::new((pw).into(), false) * stride_w - pad_w; + let wend = wstart + ks_w * dilation_w; + + let mut h_index = I32Number::new(1, true); + let mut w_index = I32Number::new(1, true); + + let mut Yh: T = NumberTrait::min_value(); + + let mut h = hstart; + loop { + if h >= hend { + break; + } + if h >= 0 && h < H.into() { + let mut w = wstart; + loop { + if w >= wend { + break; + } + if w >= 0 && w < W.into() { + let input_index = h * W.into() + w; + if input_index >= 0 && input_index < X_len.into() { + if *(*X).data.at(x_d + input_index.into()) > Yh { + h_index = h.into(); + w_index = w.into(); + Yh = (*(*X).data.at(x_d + input_index.into())); + } + } + } + w += dilation_w; + }; + }; + h += dilation_h; + }; + + if Yh != NumberTrait::::min_value() { + Y_data.append(Yh); + if storage_order == 0 { + I_data.append((c * x_step).into() + h_index * W.into() + w_index); + } else { + I_data.append((c * x_step).into() + h_index + w_index * H.into()); + } + } + pw += 1; + }; + ph += 1; + }; + c += 1; + }; + + if output_len == 1 { + return (TensorTrait::new(y_dims.span(), Y_data.span()), Option::>::None); + } + return ( + TensorTrait::new(y_dims.span(), Y_data.span()), + Option::Some(TensorTrait::new(y_dims.span(), I_data.span())) + ); +} + +fn max_pool_3d< + T, + MAG, + +TensorTrait, + +NumberTrait, + +Copy, + +Drop, + +PartialOrd, + +PartialEq, + +PrintTrait +>( + X: @Tensor, + auto_pad: AUTO_PAD, + ceil_mode: usize, + dilations: Span, + kernel_shape: Span, + pads: Span, + storage_order: usize, + strides: Span, + output_spatial_shape: Span, + output_len: usize, +) -> (Tensor, Option>) { + let mut y_dims = ArrayTrait::new(); + y_dims.append_span(SpanTrait::slice((*X).shape, 0, 2)); + y_dims.append_span(output_spatial_shape); + + let N = *(*X).shape.at(0); + let C = *(*X).shape.at(1); + let H = *(*X).shape.at(2); + let W = *(*X).shape.at(3); + let D = *(*X).shape.at(4); + + let pooled_H = *y_dims.at(2); + let pooled_W = *y_dims.at(3); + let pooled_D = *y_dims.at(4); + + let x_step = H * W * D; + + let total_channels = N * C; + + let stride_h = I32Number::new((*strides.at(0)).into(), false); + let stride_w = I32Number::new((*strides.at(1)).into(), false); + let stride_d = I32Number::new((*strides.at(2)).into(), false); + + let dilation_h = I32Number::new((*dilations.at(0)).into(), false); + let dilation_w = I32Number::new((*dilations.at(1)).into(), false); + let dilation_d = I32Number::new((*dilations.at(2)).into(), false); + + let ks_h = I32Number::new((*kernel_shape.at(0)).into(), false); + let ks_w = I32Number::new((*kernel_shape.at(1)).into(), false); + let ks_d = I32Number::new((*kernel_shape.at(2)).into(), false); + + let pad_h = I32Number::new((*pads.at(0)).into(), false); + let pad_w = I32Number::new((*pads.at(1)).into(), false); + let pad_d = I32Number::new((*pads.at(2)).into(), false); + + let mut Y_data = ArrayTrait::new(); + let mut I_data = ArrayTrait::new(); + + let X_len = (*X).data.len(); + + let mut c = 0; + loop { + if c == total_channels { + break; + } + let x_d = c * x_step; + + let mut ph = 0; + loop { + if ph == pooled_H { + break; + } + let hstart = I32Number::new((ph).into(), false) * stride_h - pad_h; + let hend = hstart + ks_h * dilation_h; + + let mut pw = 0; + loop { + if pw == pooled_W { + break; + } + let wstart = I32Number::new((pw).into(), false) * stride_w - pad_w; + let wend = wstart + ks_w * dilation_w; + + let mut pd = 0; + loop { + if pd == pooled_D { + break; + } + let dstart = I32Number::new((pd).into(), false) * stride_d - pad_d; + let dend = dstart + ks_d * dilation_d; + + let mut h_index = I32Number::new(1, true); + let mut w_index = I32Number::new(1, true); + let mut d_index = I32Number::new(1, true); + + let mut Yh: T = NumberTrait::min_value(); + + let mut h = hstart; + let mut Yh = loop { + if h >= hend { + break Yh; + } + if h >= 0 && h < H.into() { + let mut w = wstart; + loop { + if w >= wend { + break Yh; + } + if w >= 0 && w < W.into() { + let mut d = dstart; + loop { + if d >= dend { + break; + } + if d >= 0 && d < D.into() { + let input_index = h * W.into() * D.into() + + w * D.into() + + d; + if input_index >= 0 && input_index < X_len.into() { + if *(*X).data.at(x_d + input_index.into()) > Yh { + h_index = h.into(); + w_index = w.into(); + d_index = d.into(); + Yh = (*(*X).data.at(x_d + input_index.into())); + } + } + } + d += dilation_d; + }; + }; + w += dilation_w; + }; + }; + h += dilation_h; + }; + Y_data.append(Yh); + + if storage_order == 0 { + I_data + .append( + (c * x_step).into() + + h_index * W.into() * D.into() + + w_index * D.into() + + d_index + ); + } else { + I_data + .append( + (c * x_step).into() + + h_index + + w_index * H.into() + + d_index * H.into() * W.into() + ); + } + pd += 1; + }; + pw += 1; + }; + ph += 1; + }; + c += 1; + }; + + if output_len == 1 { + return (TensorTrait::new(y_dims.span(), Y_data.span()), Option::>::None); + } + return ( + TensorTrait::new(y_dims.span(), Y_data.span()), + Option::Some(TensorTrait::new(y_dims.span(), I_data.span())) + ); +} + + +fn max_pool_nd< + T, + MAG, + +TensorTrait, + +NumberTrait, + +Copy, + +Drop, + +PartialOrd, + +PartialEq, + +PrintTrait, + +TryInto, + +Into, + +Div +>( + X: @Tensor, + auto_pad: AUTO_PAD, + ceil_mode: usize, + dilations: Span, + kernel_shape: Span, + pads: Span, + storage_order: usize, + strides: Span, + output_spatial_shape: Span, + output_len: usize, +) -> (Tensor, Option>) { + let nd = (*X).shape.len() - 2; + + let mut y_dims = ArrayTrait::new(); + y_dims.append_span(SpanTrait::slice((*X).shape, 0, 2)); + y_dims.append_span(output_spatial_shape); + + let N = *(*X).shape.at(0); + let C = *(*X).shape.at(1); + + let x_stride = stride((*X).shape); + let y_stride = stride(y_dims.span()); + + let i_stride_storage_order_1 = if storage_order == 1 { + let i_stride_storage_order_1 = reverse_stride(SpanTrait::slice((*X).shape, 2, nd)); + i_stride_storage_order_1 + } else { + array![].span() + }; + + let x_step = *x_stride.at(1); + let y_step = *y_stride.at(1); + + let total_channels = N * C; + + let stride_n: Span = u32_span_into_i32_span(strides); + let dilation_n: Span = u32_span_into_i32_span(dilations); + let ks_n: Span = u32_span_into_i32_span(kernel_shape); + let pad_n: Span = u32_span_into_i32_span(pads); + + let mut Y_data = ArrayTrait::new(); + let mut I_data = ArrayTrait::new(); + + let X_len = (*X).data.len(); + + let mut c = 0; + loop { + if c == total_channels { + break; + } + let x_d = c * x_step; + + let mut p = 0; + loop { + if p == y_step { + break; + } + + let mut flatten_index = p; + + let mut nstart = ArrayTrait::new(); + let mut nend = ArrayTrait::new(); + let mut nstep = ArrayTrait::::new(); + + let mut n = 0; + loop { + if n == nd { + break; + } + let (pn, rem) = DivRem::div_rem( + flatten_index, (*y_stride.at(2 + n)).try_into().unwrap() + ); + flatten_index = rem; + + let ns = pn.into() * *stride_n.at(n) - *pad_n.at(n); + nstart.append(ns); + nend.append(ns + *ks_n.at(n) * *dilation_n.at(n)); + + let a: T = NumberTrait::new_unscaled(((*nend.at(n) - ns)).into(), false); + let b: T = NumberTrait::new_unscaled((*dilation_n.at(n)).into(), false); + nstep.append(NumberTrait::ceil(a / b).try_into().unwrap()); + n += 1; + }; + + let nstart = nstart.span(); + let nstride = stride(nstep.span()); + let max_iter = *nstep.at(0) * *nstride.at(0); + + let mut n_index = array![I32Number::new(1, true)].span(); + + let mut Yh: T = NumberTrait::min_value(); + + let mut i = 0; + let Yh = loop { + if i == max_iter { + break Yh; + } + let mut flatten_index = i; + let mut is_outside = false; + let mut i_index = ArrayTrait::new(); + let mut input_index = I32Number::zero(); + + let mut n = 0; + loop { + if n == nd { + break Yh; + } + let (item, rem) = DivRem::div_rem( + flatten_index, (*nstride.at(n)).try_into().unwrap() + ); + flatten_index = rem; + + let item_ = item.into() * *dilation_n.at(n) + *nstart.at(n); + if item_ < 0 || item_ >= (*(*X).shape.at(2 + n)).into() { + is_outside = true; + }; + i_index.append(item_); + input_index += item_ * (*x_stride.at(2 + n)).into(); + + n += 1; + }; + + if !is_outside { + if input_index >= 0 && input_index < X_len.into() { + if *(*X).data.at(x_d + input_index.into()) > Yh { + n_index = i_index.span().clone(); + Yh = (*(*X).data.at(x_d + input_index.into())); + }; + }; + }; + i += 1; + }; + Y_data.append(Yh); + + if storage_order == 0 { + let mut index = 0; + let mut n = 0; + loop { + if n == nd { + break; + } + index += *n_index.at(n) * (*x_stride.at(2 + n)).into(); + n += 1; + }; + I_data.append((c * x_step).into() + index); + } else { + let mut index = 0; + let mut n = nd; + loop { + if n == 0 { + break; + } + index += *n_index.at(n - 1) * (*i_stride_storage_order_1.at(nd - n)).into(); + n -= 1; + }; + I_data.append((c * x_step).into() + index); + } + p += 1; + }; + c += 1; + }; + if output_len == 1 { + return (TensorTrait::new(y_dims.span(), Y_data.span()), Option::>::None); + } + return (TensorTrait::new(y_dims.span(), Y_data.span()), Option::>::None); +} + + +fn u32_span_into_i32_span(mut x: Span) -> Span { + let mut res = ArrayTrait::new(); + + loop { + match x.pop_front() { + Option::Some(v) => { res.append((*v).into()); }, + Option::None => { break res.span(); } + }; + } +} + +fn reverse_stride(mut a: Span) -> Span { + let mut prod = 1; + let mut arr = ArrayTrait::new(); + loop { + match a.pop_front() { + Option::Some(v) => { + prod *= *v; + arr.append(prod); + }, + Option::None => { break arr.span(); } + }; + } +} + + +fn min(mut a: Span) -> usize { + assert(a.len() > 0, 'span cannot be empty'); + + let mut min = *a.at(0); + loop { + match a.pop_front() { + Option::Some(v) => { if *v < min { + min = *v; + }; }, + Option::None => { break min; } + }; + } +} + + +fn max(mut a: Span) -> usize { + assert(a.len() > 0, 'span cannot be empty'); + + let mut max = *a.at(0); + loop { + match a.pop_front() { + Option::Some(v) => { if *v > max { + max = *v; + }; }, + Option::None => { break max; } + }; + } +} diff --git a/src/operators/nn/implementations/nn_fp16x16.cairo b/src/operators/nn/implementations/nn_fp16x16.cairo index edf7b59c7..aa6a7b6ce 100644 --- a/src/operators/nn/implementations/nn_fp16x16.cairo +++ b/src/operators/nn/implementations/nn_fp16x16.cairo @@ -13,6 +13,9 @@ use orion::numbers::fixed_point::implementations::fp16x16wide::core::{ use orion::operators::tensor::implementations::tensor_fp16x16wide::{ FP16x16WTensor, FP16x16WTensorDiv, FP16x16WTensorAdd }; +use orion::numbers::I32IntoU32; +use orion::operators::tensor::implementations::tensor_i32::I32Tensor; +use orion::operators::nn::AUTO_PAD; impl FP16x16NN of NNTrait { fn relu(tensor: @Tensor) -> Tensor { @@ -145,4 +148,28 @@ impl FP16x16NN of NNTrait { ) -> Tensor { functional::conv::conv(X, W, B, auto_pad, dilations, group, kernel_shape, pads, strides) } + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>) { + functional::max_pool::max_pool( + X, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + pads, + storage_order, + strides, + output_len + ) + } } diff --git a/src/operators/nn/implementations/nn_fp32x32.cairo b/src/operators/nn/implementations/nn_fp32x32.cairo index db99af01b..3054dd1ad 100644 --- a/src/operators/nn/implementations/nn_fp32x32.cairo +++ b/src/operators/nn/implementations/nn_fp32x32.cairo @@ -7,6 +7,9 @@ use orion::numbers::fixed_point::implementations::fp32x32::core::{FP32x32, FP32x use orion::operators::tensor::implementations::tensor_fp32x32::{ FP32x32Tensor, FP32x32TensorDiv, FP32x32TensorAdd }; +use orion::numbers::I32IntoU32; +use orion::operators::tensor::implementations::tensor_i32::I32Tensor; +use orion::operators::nn::AUTO_PAD; impl FP32x32NN of NNTrait { fn relu(tensor: @Tensor) -> Tensor { @@ -139,4 +142,19 @@ impl FP32x32NN of NNTrait { ) -> Tensor { functional::conv::conv(X, W, B, auto_pad, dilations, group, kernel_shape, pads, strides) } + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>) { + //functional::max_pool::max_pool(X, auto_pad, ceil_mode, dilations,kernel_shape, pads, storage_order, strides, output_len) + panic(array!['not supported!']) + } } diff --git a/src/operators/nn/implementations/nn_fp64x64.cairo b/src/operators/nn/implementations/nn_fp64x64.cairo index 935af584c..a378cfb70 100644 --- a/src/operators/nn/implementations/nn_fp64x64.cairo +++ b/src/operators/nn/implementations/nn_fp64x64.cairo @@ -7,6 +7,9 @@ use orion::numbers::fixed_point::implementations::fp64x64::core::{FP64x64, FP64x use orion::operators::tensor::implementations::tensor_fp64x64::{ FP64x64Tensor, FP64x64TensorDiv, FP64x64TensorAdd }; +//use orion::numbers::I32IntoU64; +use orion::operators::tensor::implementations::tensor_i32::I32Tensor; +use orion::operators::nn::AUTO_PAD; impl FP64x64NN of NNTrait { fn relu(tensor: @Tensor) -> Tensor { @@ -139,4 +142,19 @@ impl FP64x64NN of NNTrait { ) -> Tensor { functional::conv::conv(X, W, B, auto_pad, dilations, group, kernel_shape, pads, strides) } + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>) { + //functional::max_pool::max_pool(X, auto_pad, ceil_mode, dilations,kernel_shape, pads, storage_order, strides, output_len) + panic(array!['not supported!']) + } } diff --git a/src/operators/nn/implementations/nn_fp8x23.cairo b/src/operators/nn/implementations/nn_fp8x23.cairo index 842c115f9..add955fd9 100644 --- a/src/operators/nn/implementations/nn_fp8x23.cairo +++ b/src/operators/nn/implementations/nn_fp8x23.cairo @@ -11,6 +11,9 @@ use orion::numbers::fixed_point::implementations::fp8x23wide::core::{ FP8x23WImpl, FP8x23WTryIntoFP8x23, FP8x23W, FP8x23IntoFP8x23W }; use orion::operators::tensor::implementations::tensor_fp8x23wide::{FP8x23WTensor}; +use orion::numbers::I32IntoU32; +use orion::operators::tensor::implementations::tensor_i32::I32Tensor; +use orion::operators::nn::AUTO_PAD; impl FP8x23NN of NNTrait { fn relu(tensor: @Tensor) -> Tensor { @@ -141,4 +144,28 @@ impl FP8x23NN of NNTrait { ) -> Tensor { functional::conv::conv(X, W, B, auto_pad, dilations, group, kernel_shape, pads, strides) } + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>) { + functional::max_pool::max_pool( + X, + auto_pad, + ceil_mode, + dilations, + kernel_shape, + pads, + storage_order, + strides, + output_len + ) + } } diff --git a/src/operators/nn/implementations/nn_i32.cairo b/src/operators/nn/implementations/nn_i32.cairo index 13a670746..0156fc5f5 100644 --- a/src/operators/nn/implementations/nn_i32.cairo +++ b/src/operators/nn/implementations/nn_i32.cairo @@ -4,6 +4,7 @@ use orion::operators::tensor::core::Tensor; use orion::operators::nn::core::NNTrait; use orion::operators::nn::functional; use orion::operators::tensor::implementations::tensor_i32::{I32Tensor, I32TensorAdd}; +use orion::operators::nn::AUTO_PAD; impl I32NN of NNTrait { fn relu(tensor: @Tensor) -> Tensor { @@ -132,4 +133,18 @@ impl I32NN of NNTrait { ) -> Tensor { functional::conv::conv(X, W, B, auto_pad, dilations, group, kernel_shape, pads, strides) } + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>) { + panic(array!['not supported!']) + } } diff --git a/src/operators/nn/implementations/nn_i8.cairo b/src/operators/nn/implementations/nn_i8.cairo index 5d359a91c..284dd5ee1 100644 --- a/src/operators/nn/implementations/nn_i8.cairo +++ b/src/operators/nn/implementations/nn_i8.cairo @@ -4,6 +4,8 @@ use orion::operators::tensor::core::Tensor; use orion::operators::nn::core::NNTrait; use orion::operators::nn::functional; use orion::operators::tensor::implementations::tensor_i8::{I8Tensor, I8TensorAdd}; +use orion::operators::tensor::implementations::tensor_i32::I32Tensor; +use orion::operators::nn::AUTO_PAD; impl I8NN of NNTrait { fn relu(tensor: @Tensor) -> Tensor { @@ -132,4 +134,18 @@ impl I8NN of NNTrait { ) -> Tensor { functional::conv::conv(X, W, B, auto_pad, dilations, group, kernel_shape, pads, strides) } + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>) { + panic(array!['not supported!']) + } } diff --git a/src/operators/nn/implementations/nn_u32.cairo b/src/operators/nn/implementations/nn_u32.cairo index 9e4038fe4..1ebfb3bec 100644 --- a/src/operators/nn/implementations/nn_u32.cairo +++ b/src/operators/nn/implementations/nn_u32.cairo @@ -4,6 +4,8 @@ use orion::operators::tensor::core::Tensor; use orion::operators::nn::core::NNTrait; use orion::operators::nn::functional; use orion::operators::tensor::implementations::tensor_u32::{U32Tensor, U32TensorAdd}; +use orion::operators::tensor::implementations::tensor_i32::I32Tensor; +use orion::operators::nn::AUTO_PAD; impl U32NN of NNTrait { fn relu(tensor: @Tensor) -> Tensor { @@ -132,4 +134,18 @@ impl U32NN of NNTrait { ) -> Tensor { functional::conv::conv(X, W, B, auto_pad, dilations, group, kernel_shape, pads, strides) } + + fn max_pool( + X: @Tensor, + auto_pad: Option, + ceil_mode: Option, + dilations: Option>, + kernel_shape: Span, + pads: Option>, + storage_order: Option, + strides: Option>, + output_len: usize, + ) -> (Tensor, Option>) { + panic(array!['not supported!']) + } } diff --git a/tests/lib.cairo b/tests/lib.cairo index f5cecb77d..250436c9f 100644 --- a/tests/lib.cairo +++ b/tests/lib.cairo @@ -5,3 +5,5 @@ mod nodes; mod ml; mod operators; + + diff --git a/tests/nodes.cairo b/tests/nodes.cairo index 8814cfb80..2133d4f46 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -1039,3 +1039,16 @@ mod conv_2D_with_autopad_same; mod conv_2D_with_strides_asymmetric_padding; mod conv_2D_with_strides_with_padding; mod conv_4D_with_padding; +mod maxpool_2d; +mod maxpool_1d; +mod maxpool_1d_default; +mod maxpool_2d_ceil; +mod maxpool_2d_constraint_index; +mod maxpool_2d_default; +mod maxpool_2d_dilations; +mod maxpool_2d_pads_default; +mod maxpool_2d_same_lower_default; +mod maxpool_2d_same_upper; +mod maxpool_2d_same_upper_default; +mod maxpool_3d_dilations; +mod maxpool_4d_dilations; diff --git a/tests/nodes/maxpool_1d.cairo b/tests/nodes/maxpool_1d.cairo new file mode 100644 index 000000000..e9ef475f8 --- /dev/null +++ b/tests/nodes/maxpool_1d.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_1d() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::None, + array![2].span(), + Option::None, + Option::None, + Option::Some(array![2].span()), + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_1d/input_0.cairo b/tests/nodes/maxpool_1d/input_0.cairo new file mode 100644 index 000000000..3e92bf1d2 --- /dev/null +++ b/tests/nodes/maxpool_1d/input_0.cairo @@ -0,0 +1,110 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(32); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 61884, sign: true }); + data.append(FP16x16 { mag: 66202, sign: true }); + data.append(FP16x16 { mag: 63102, sign: false }); + data.append(FP16x16 { mag: 70834, sign: false }); + data.append(FP16x16 { mag: 84277, sign: true }); + data.append(FP16x16 { mag: 15414, sign: true }); + data.append(FP16x16 { mag: 69938, sign: true }); + data.append(FP16x16 { mag: 63283, sign: true }); + data.append(FP16x16 { mag: 43864, sign: false }); + data.append(FP16x16 { mag: 80777, sign: false }); + data.append(FP16x16 { mag: 62331, sign: false }); + data.append(FP16x16 { mag: 67865, sign: true }); + data.append(FP16x16 { mag: 24630, sign: true }); + data.append(FP16x16 { mag: 36245, sign: false }); + data.append(FP16x16 { mag: 134644, sign: true }); + data.append(FP16x16 { mag: 43793, sign: false }); + data.append(FP16x16 { mag: 86411, sign: false }); + data.append(FP16x16 { mag: 86146, sign: true }); + data.append(FP16x16 { mag: 16382, sign: true }); + data.append(FP16x16 { mag: 146017, sign: false }); + data.append(FP16x16 { mag: 7647, sign: true }); + data.append(FP16x16 { mag: 45163, sign: true }); + data.append(FP16x16 { mag: 104406, sign: false }); + data.append(FP16x16 { mag: 45462, sign: false }); + data.append(FP16x16 { mag: 86222, sign: false }); + data.append(FP16x16 { mag: 9912, sign: true }); + data.append(FP16x16 { mag: 22960, sign: true }); + data.append(FP16x16 { mag: 55123, sign: true }); + data.append(FP16x16 { mag: 124655, sign: true }); + data.append(FP16x16 { mag: 31465, sign: false }); + data.append(FP16x16 { mag: 61922, sign: false }); + data.append(FP16x16 { mag: 163238, sign: true }); + data.append(FP16x16 { mag: 34228, sign: true }); + data.append(FP16x16 { mag: 4475, sign: false }); + data.append(FP16x16 { mag: 56673, sign: true }); + data.append(FP16x16 { mag: 90552, sign: true }); + data.append(FP16x16 { mag: 66213, sign: false }); + data.append(FP16x16 { mag: 214831, sign: true }); + data.append(FP16x16 { mag: 77997, sign: true }); + data.append(FP16x16 { mag: 704, sign: true }); + data.append(FP16x16 { mag: 33211, sign: true }); + data.append(FP16x16 { mag: 12139, sign: true }); + data.append(FP16x16 { mag: 18185, sign: false }); + data.append(FP16x16 { mag: 90981, sign: true }); + data.append(FP16x16 { mag: 37230, sign: false }); + data.append(FP16x16 { mag: 15860, sign: true }); + data.append(FP16x16 { mag: 38407, sign: true }); + data.append(FP16x16 { mag: 16248, sign: false }); + data.append(FP16x16 { mag: 109129, sign: false }); + data.append(FP16x16 { mag: 52730, sign: true }); + data.append(FP16x16 { mag: 48858, sign: false }); + data.append(FP16x16 { mag: 72554, sign: false }); + data.append(FP16x16 { mag: 89600, sign: false }); + data.append(FP16x16 { mag: 61833, sign: true }); + data.append(FP16x16 { mag: 9863, sign: false }); + data.append(FP16x16 { mag: 2754, sign: false }); + data.append(FP16x16 { mag: 85035, sign: false }); + data.append(FP16x16 { mag: 47440, sign: true }); + data.append(FP16x16 { mag: 176235, sign: false }); + data.append(FP16x16 { mag: 77741, sign: false }); + data.append(FP16x16 { mag: 18683, sign: true }); + data.append(FP16x16 { mag: 8069, sign: true }); + data.append(FP16x16 { mag: 30891, sign: true }); + data.append(FP16x16 { mag: 26682, sign: true }); + data.append(FP16x16 { mag: 32658, sign: true }); + data.append(FP16x16 { mag: 1956, sign: true }); + data.append(FP16x16 { mag: 96803, sign: false }); + data.append(FP16x16 { mag: 61321, sign: false }); + data.append(FP16x16 { mag: 33065, sign: true }); + data.append(FP16x16 { mag: 59893, sign: false }); + data.append(FP16x16 { mag: 157662, sign: false }); + data.append(FP16x16 { mag: 52334, sign: true }); + data.append(FP16x16 { mag: 46043, sign: true }); + data.append(FP16x16 { mag: 152484, sign: false }); + data.append(FP16x16 { mag: 27460, sign: false }); + data.append(FP16x16 { mag: 1553, sign: false }); + data.append(FP16x16 { mag: 29415, sign: true }); + data.append(FP16x16 { mag: 26375, sign: false }); + data.append(FP16x16 { mag: 21889, sign: false }); + data.append(FP16x16 { mag: 80932, sign: true }); + data.append(FP16x16 { mag: 2233, sign: false }); + data.append(FP16x16 { mag: 42479, sign: false }); + data.append(FP16x16 { mag: 1156, sign: false }); + data.append(FP16x16 { mag: 158107, sign: false }); + data.append(FP16x16 { mag: 34271, sign: false }); + data.append(FP16x16 { mag: 86694, sign: true }); + data.append(FP16x16 { mag: 89544, sign: true }); + data.append(FP16x16 { mag: 29105, sign: true }); + data.append(FP16x16 { mag: 21337, sign: false }); + data.append(FP16x16 { mag: 12441, sign: true }); + data.append(FP16x16 { mag: 24034, sign: true }); + data.append(FP16x16 { mag: 24040, sign: true }); + data.append(FP16x16 { mag: 73971, sign: true }); + data.append(FP16x16 { mag: 700, sign: false }); + data.append(FP16x16 { mag: 99358, sign: true }); + data.append(FP16x16 { mag: 109591, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_1d/output_0.cairo b/tests/nodes/maxpool_1d/output_0.cairo new file mode 100644 index 000000000..97ac3b8de --- /dev/null +++ b/tests/nodes/maxpool_1d/output_0.cairo @@ -0,0 +1,62 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(16); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 61884, sign: true }); + data.append(FP16x16 { mag: 70834, sign: false }); + data.append(FP16x16 { mag: 15414, sign: true }); + data.append(FP16x16 { mag: 63283, sign: true }); + data.append(FP16x16 { mag: 80777, sign: false }); + data.append(FP16x16 { mag: 62331, sign: false }); + data.append(FP16x16 { mag: 36245, sign: false }); + data.append(FP16x16 { mag: 43793, sign: false }); + data.append(FP16x16 { mag: 86411, sign: false }); + data.append(FP16x16 { mag: 146017, sign: false }); + data.append(FP16x16 { mag: 7647, sign: true }); + data.append(FP16x16 { mag: 104406, sign: false }); + data.append(FP16x16 { mag: 86222, sign: false }); + data.append(FP16x16 { mag: 22960, sign: true }); + data.append(FP16x16 { mag: 31465, sign: false }); + data.append(FP16x16 { mag: 61922, sign: false }); + data.append(FP16x16 { mag: 4475, sign: false }); + data.append(FP16x16 { mag: 56673, sign: true }); + data.append(FP16x16 { mag: 66213, sign: false }); + data.append(FP16x16 { mag: 704, sign: true }); + data.append(FP16x16 { mag: 12139, sign: true }); + data.append(FP16x16 { mag: 18185, sign: false }); + data.append(FP16x16 { mag: 37230, sign: false }); + data.append(FP16x16 { mag: 16248, sign: false }); + data.append(FP16x16 { mag: 109129, sign: false }); + data.append(FP16x16 { mag: 72554, sign: false }); + data.append(FP16x16 { mag: 89600, sign: false }); + data.append(FP16x16 { mag: 9863, sign: false }); + data.append(FP16x16 { mag: 85035, sign: false }); + data.append(FP16x16 { mag: 176235, sign: false }); + data.append(FP16x16 { mag: 8069, sign: true }); + data.append(FP16x16 { mag: 26682, sign: true }); + data.append(FP16x16 { mag: 1956, sign: true }); + data.append(FP16x16 { mag: 96803, sign: false }); + data.append(FP16x16 { mag: 59893, sign: false }); + data.append(FP16x16 { mag: 157662, sign: false }); + data.append(FP16x16 { mag: 152484, sign: false }); + data.append(FP16x16 { mag: 27460, sign: false }); + data.append(FP16x16 { mag: 26375, sign: false }); + data.append(FP16x16 { mag: 21889, sign: false }); + data.append(FP16x16 { mag: 42479, sign: false }); + data.append(FP16x16 { mag: 158107, sign: false }); + data.append(FP16x16 { mag: 34271, sign: false }); + data.append(FP16x16 { mag: 29105, sign: true }); + data.append(FP16x16 { mag: 21337, sign: false }); + data.append(FP16x16 { mag: 24034, sign: true }); + data.append(FP16x16 { mag: 700, sign: false }); + data.append(FP16x16 { mag: 109591, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_1d_default.cairo b/tests/nodes/maxpool_1d_default.cairo new file mode 100644 index 000000000..d036694fa --- /dev/null +++ b/tests/nodes/maxpool_1d_default.cairo @@ -0,0 +1,31 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_1d_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::None, + array![2].span(), + Option::None, + Option::None, + Option::None, + 1 + ); + + assert_eq(y_0, z_0); +} + diff --git a/tests/nodes/maxpool_1d_default/input_0.cairo b/tests/nodes/maxpool_1d_default/input_0.cairo new file mode 100644 index 000000000..f9522826d --- /dev/null +++ b/tests/nodes/maxpool_1d_default/input_0.cairo @@ -0,0 +1,110 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(32); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 50224, sign: true }); + data.append(FP16x16 { mag: 2534, sign: false }); + data.append(FP16x16 { mag: 1978, sign: false }); + data.append(FP16x16 { mag: 53529, sign: false }); + data.append(FP16x16 { mag: 171944, sign: true }); + data.append(FP16x16 { mag: 47778, sign: false }); + data.append(FP16x16 { mag: 85540, sign: true }); + data.append(FP16x16 { mag: 3998, sign: false }); + data.append(FP16x16 { mag: 5615, sign: false }); + data.append(FP16x16 { mag: 1394, sign: true }); + data.append(FP16x16 { mag: 74940, sign: false }); + data.append(FP16x16 { mag: 32499, sign: false }); + data.append(FP16x16 { mag: 13610, sign: false }); + data.append(FP16x16 { mag: 147171, sign: false }); + data.append(FP16x16 { mag: 4356, sign: true }); + data.append(FP16x16 { mag: 90349, sign: true }); + data.append(FP16x16 { mag: 96528, sign: false }); + data.append(FP16x16 { mag: 108927, sign: true }); + data.append(FP16x16 { mag: 10457, sign: true }); + data.append(FP16x16 { mag: 2548, sign: false }); + data.append(FP16x16 { mag: 48359, sign: false }); + data.append(FP16x16 { mag: 25137, sign: false }); + data.append(FP16x16 { mag: 31065, sign: false }); + data.append(FP16x16 { mag: 83420, sign: false }); + data.append(FP16x16 { mag: 58282, sign: false }); + data.append(FP16x16 { mag: 71330, sign: false }); + data.append(FP16x16 { mag: 14944, sign: false }); + data.append(FP16x16 { mag: 95778, sign: true }); + data.append(FP16x16 { mag: 52231, sign: true }); + data.append(FP16x16 { mag: 1629, sign: false }); + data.append(FP16x16 { mag: 86604, sign: false }); + data.append(FP16x16 { mag: 24073, sign: false }); + data.append(FP16x16 { mag: 54993, sign: true }); + data.append(FP16x16 { mag: 87393, sign: true }); + data.append(FP16x16 { mag: 83491, sign: true }); + data.append(FP16x16 { mag: 11108, sign: false }); + data.append(FP16x16 { mag: 118783, sign: true }); + data.append(FP16x16 { mag: 119405, sign: true }); + data.append(FP16x16 { mag: 66301, sign: false }); + data.append(FP16x16 { mag: 128037, sign: false }); + data.append(FP16x16 { mag: 2385, sign: true }); + data.append(FP16x16 { mag: 31954, sign: true }); + data.append(FP16x16 { mag: 30235, sign: false }); + data.append(FP16x16 { mag: 34919, sign: false }); + data.append(FP16x16 { mag: 69026, sign: false }); + data.append(FP16x16 { mag: 25820, sign: true }); + data.append(FP16x16 { mag: 80142, sign: false }); + data.append(FP16x16 { mag: 71641, sign: false }); + data.append(FP16x16 { mag: 72810, sign: true }); + data.append(FP16x16 { mag: 23490, sign: false }); + data.append(FP16x16 { mag: 17323, sign: true }); + data.append(FP16x16 { mag: 24532, sign: true }); + data.append(FP16x16 { mag: 66044, sign: false }); + data.append(FP16x16 { mag: 44213, sign: true }); + data.append(FP16x16 { mag: 8164, sign: true }); + data.append(FP16x16 { mag: 32326, sign: false }); + data.append(FP16x16 { mag: 43120, sign: true }); + data.append(FP16x16 { mag: 181, sign: true }); + data.append(FP16x16 { mag: 18666, sign: true }); + data.append(FP16x16 { mag: 8560, sign: true }); + data.append(FP16x16 { mag: 15235, sign: true }); + data.append(FP16x16 { mag: 25524, sign: false }); + data.append(FP16x16 { mag: 97926, sign: true }); + data.append(FP16x16 { mag: 83401, sign: true }); + data.append(FP16x16 { mag: 10862, sign: false }); + data.append(FP16x16 { mag: 13170, sign: true }); + data.append(FP16x16 { mag: 14320, sign: false }); + data.append(FP16x16 { mag: 82805, sign: false }); + data.append(FP16x16 { mag: 11320, sign: false }); + data.append(FP16x16 { mag: 36914, sign: false }); + data.append(FP16x16 { mag: 476, sign: false }); + data.append(FP16x16 { mag: 26739, sign: true }); + data.append(FP16x16 { mag: 27204, sign: false }); + data.append(FP16x16 { mag: 135386, sign: true }); + data.append(FP16x16 { mag: 179608, sign: false }); + data.append(FP16x16 { mag: 38394, sign: true }); + data.append(FP16x16 { mag: 124283, sign: false }); + data.append(FP16x16 { mag: 17926, sign: false }); + data.append(FP16x16 { mag: 199614, sign: false }); + data.append(FP16x16 { mag: 21143, sign: false }); + data.append(FP16x16 { mag: 58284, sign: false }); + data.append(FP16x16 { mag: 44246, sign: true }); + data.append(FP16x16 { mag: 58693, sign: false }); + data.append(FP16x16 { mag: 39360, sign: false }); + data.append(FP16x16 { mag: 79614, sign: true }); + data.append(FP16x16 { mag: 36430, sign: false }); + data.append(FP16x16 { mag: 19447, sign: true }); + data.append(FP16x16 { mag: 10755, sign: false }); + data.append(FP16x16 { mag: 3572, sign: true }); + data.append(FP16x16 { mag: 23011, sign: true }); + data.append(FP16x16 { mag: 12359, sign: false }); + data.append(FP16x16 { mag: 33072, sign: true }); + data.append(FP16x16 { mag: 80505, sign: true }); + data.append(FP16x16 { mag: 25351, sign: false }); + data.append(FP16x16 { mag: 84321, sign: true }); + data.append(FP16x16 { mag: 39865, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_1d_default/output_0.cairo b/tests/nodes/maxpool_1d_default/output_0.cairo new file mode 100644 index 000000000..94eee9956 --- /dev/null +++ b/tests/nodes/maxpool_1d_default/output_0.cairo @@ -0,0 +1,107 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(31); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 2534, sign: false }); + data.append(FP16x16 { mag: 2534, sign: false }); + data.append(FP16x16 { mag: 53529, sign: false }); + data.append(FP16x16 { mag: 53529, sign: false }); + data.append(FP16x16 { mag: 47778, sign: false }); + data.append(FP16x16 { mag: 47778, sign: false }); + data.append(FP16x16 { mag: 3998, sign: false }); + data.append(FP16x16 { mag: 5615, sign: false }); + data.append(FP16x16 { mag: 5615, sign: false }); + data.append(FP16x16 { mag: 74940, sign: false }); + data.append(FP16x16 { mag: 74940, sign: false }); + data.append(FP16x16 { mag: 32499, sign: false }); + data.append(FP16x16 { mag: 147171, sign: false }); + data.append(FP16x16 { mag: 147171, sign: false }); + data.append(FP16x16 { mag: 4356, sign: true }); + data.append(FP16x16 { mag: 96528, sign: false }); + data.append(FP16x16 { mag: 96528, sign: false }); + data.append(FP16x16 { mag: 10457, sign: true }); + data.append(FP16x16 { mag: 2548, sign: false }); + data.append(FP16x16 { mag: 48359, sign: false }); + data.append(FP16x16 { mag: 48359, sign: false }); + data.append(FP16x16 { mag: 31065, sign: false }); + data.append(FP16x16 { mag: 83420, sign: false }); + data.append(FP16x16 { mag: 83420, sign: false }); + data.append(FP16x16 { mag: 71330, sign: false }); + data.append(FP16x16 { mag: 71330, sign: false }); + data.append(FP16x16 { mag: 14944, sign: false }); + data.append(FP16x16 { mag: 52231, sign: true }); + data.append(FP16x16 { mag: 1629, sign: false }); + data.append(FP16x16 { mag: 86604, sign: false }); + data.append(FP16x16 { mag: 86604, sign: false }); + data.append(FP16x16 { mag: 54993, sign: true }); + data.append(FP16x16 { mag: 83491, sign: true }); + data.append(FP16x16 { mag: 11108, sign: false }); + data.append(FP16x16 { mag: 11108, sign: false }); + data.append(FP16x16 { mag: 118783, sign: true }); + data.append(FP16x16 { mag: 66301, sign: false }); + data.append(FP16x16 { mag: 128037, sign: false }); + data.append(FP16x16 { mag: 128037, sign: false }); + data.append(FP16x16 { mag: 2385, sign: true }); + data.append(FP16x16 { mag: 30235, sign: false }); + data.append(FP16x16 { mag: 34919, sign: false }); + data.append(FP16x16 { mag: 69026, sign: false }); + data.append(FP16x16 { mag: 69026, sign: false }); + data.append(FP16x16 { mag: 80142, sign: false }); + data.append(FP16x16 { mag: 80142, sign: false }); + data.append(FP16x16 { mag: 71641, sign: false }); + data.append(FP16x16 { mag: 23490, sign: false }); + data.append(FP16x16 { mag: 23490, sign: false }); + data.append(FP16x16 { mag: 17323, sign: true }); + data.append(FP16x16 { mag: 66044, sign: false }); + data.append(FP16x16 { mag: 66044, sign: false }); + data.append(FP16x16 { mag: 8164, sign: true }); + data.append(FP16x16 { mag: 32326, sign: false }); + data.append(FP16x16 { mag: 32326, sign: false }); + data.append(FP16x16 { mag: 181, sign: true }); + data.append(FP16x16 { mag: 181, sign: true }); + data.append(FP16x16 { mag: 8560, sign: true }); + data.append(FP16x16 { mag: 8560, sign: true }); + data.append(FP16x16 { mag: 25524, sign: false }); + data.append(FP16x16 { mag: 25524, sign: false }); + data.append(FP16x16 { mag: 83401, sign: true }); + data.append(FP16x16 { mag: 10862, sign: false }); + data.append(FP16x16 { mag: 14320, sign: false }); + data.append(FP16x16 { mag: 82805, sign: false }); + data.append(FP16x16 { mag: 82805, sign: false }); + data.append(FP16x16 { mag: 36914, sign: false }); + data.append(FP16x16 { mag: 36914, sign: false }); + data.append(FP16x16 { mag: 476, sign: false }); + data.append(FP16x16 { mag: 27204, sign: false }); + data.append(FP16x16 { mag: 27204, sign: false }); + data.append(FP16x16 { mag: 179608, sign: false }); + data.append(FP16x16 { mag: 179608, sign: false }); + data.append(FP16x16 { mag: 124283, sign: false }); + data.append(FP16x16 { mag: 124283, sign: false }); + data.append(FP16x16 { mag: 199614, sign: false }); + data.append(FP16x16 { mag: 199614, sign: false }); + data.append(FP16x16 { mag: 58284, sign: false }); + data.append(FP16x16 { mag: 58284, sign: false }); + data.append(FP16x16 { mag: 58693, sign: false }); + data.append(FP16x16 { mag: 58693, sign: false }); + data.append(FP16x16 { mag: 39360, sign: false }); + data.append(FP16x16 { mag: 36430, sign: false }); + data.append(FP16x16 { mag: 36430, sign: false }); + data.append(FP16x16 { mag: 10755, sign: false }); + data.append(FP16x16 { mag: 10755, sign: false }); + data.append(FP16x16 { mag: 3572, sign: true }); + data.append(FP16x16 { mag: 12359, sign: false }); + data.append(FP16x16 { mag: 12359, sign: false }); + data.append(FP16x16 { mag: 33072, sign: true }); + data.append(FP16x16 { mag: 25351, sign: false }); + data.append(FP16x16 { mag: 25351, sign: false }); + data.append(FP16x16 { mag: 39865, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d.cairo b/tests/nodes/maxpool_2d.cairo new file mode 100644 index 000000000..20e768eb7 --- /dev/null +++ b/tests/nodes/maxpool_2d.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::None, + array![2, 2].span(), + Option::None, + Option::None, + Option::Some(array![2, 2].span()), + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d/input_0.cairo b/tests/nodes/maxpool_2d/input_0.cairo new file mode 100644 index 000000000..28d2d90c9 --- /dev/null +++ b/tests/nodes/maxpool_2d/input_0.cairo @@ -0,0 +1,40 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(5); + shape.append(5); + + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + data.append(FP16x16 { mag: 1114112, sign: false }); + data.append(FP16x16 { mag: 1179648, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1376256, sign: false }); + data.append(FP16x16 { mag: 1441792, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d/output_0.cairo b/tests/nodes/maxpool_2d/output_0.cairo new file mode 100644 index 000000000..91ca1ea93 --- /dev/null +++ b/tests/nodes/maxpool_2d/output_0.cairo @@ -0,0 +1,19 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 458752, sign: false }); + data.append(FP16x16 { mag: 589824, sign: false }); + data.append(FP16x16 { mag: 1114112, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_ceil.cairo b/tests/nodes/maxpool_2d_ceil.cairo new file mode 100644 index 000000000..24f84ea08 --- /dev/null +++ b/tests/nodes/maxpool_2d_ceil.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d_ceil() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::Some(1), + Option::None, + array![3, 3].span(), + Option::None, + Option::None, + Option::Some(array![2, 2].span()), + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d_ceil/input_0.cairo b/tests/nodes/maxpool_2d_ceil/input_0.cairo new file mode 100644 index 000000000..29b195332 --- /dev/null +++ b/tests/nodes/maxpool_2d_ceil/input_0.cairo @@ -0,0 +1,31 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(4); + shape.append(4); + + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_ceil/output_0.cairo b/tests/nodes/maxpool_2d_ceil/output_0.cairo new file mode 100644 index 000000000..af7270750 --- /dev/null +++ b/tests/nodes/maxpool_2d_ceil/output_0.cairo @@ -0,0 +1,19 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_constraint_index.cairo b/tests/nodes/maxpool_2d_constraint_index.cairo new file mode 100644 index 000000000..72f1294ad --- /dev/null +++ b/tests/nodes/maxpool_2d_constraint_index.cairo @@ -0,0 +1,25 @@ +//mod input_0; +//mod output_0; +// +// +//use orion::operators::nn::NNTrait; +//use orion::operators::tensor::U32TensorPartialEq; +//use orion::numbers::FixedTrait; +//use orion::operators::tensor::I32TensorPartialEq; +//use orion::utils::{assert_eq, assert_seq_eq}; +//use orion::operators::nn::FP16x16NN; +//use orion::operators::nn::U32NN; +// +// +//#[test] +//#[available_gas(2000000000)] +//fn test_maxpool_2d_constraint_index() { +// let input_0 = input_0::input_0(); +// let z_0 = output_0::output_0(); +// +// let (_, y_0) = NNTrait::max_pool(@input_0,Option::None,Option::None,Option::None,array![2, 2].span(),Option::None,Option::Some(1),Option::Some(array![2, 2].span()),1); +// +// assert_eq(y_0.unwrap(), z_0); +//} + + diff --git a/tests/nodes/maxpool_2d_constraint_index/input_0.cairo b/tests/nodes/maxpool_2d_constraint_index/input_0.cairo new file mode 100644 index 000000000..28d2d90c9 --- /dev/null +++ b/tests/nodes/maxpool_2d_constraint_index/input_0.cairo @@ -0,0 +1,40 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(5); + shape.append(5); + + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + data.append(FP16x16 { mag: 1114112, sign: false }); + data.append(FP16x16 { mag: 1179648, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1376256, sign: false }); + data.append(FP16x16 { mag: 1441792, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_constraint_index/output_0.cairo b/tests/nodes/maxpool_2d_constraint_index/output_0.cairo new file mode 100644 index 000000000..872d5499f --- /dev/null +++ b/tests/nodes/maxpool_2d_constraint_index/output_0.cairo @@ -0,0 +1,19 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(6); + data.append(16); + data.append(8); + data.append(18); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_default.cairo b/tests/nodes/maxpool_2d_default.cairo new file mode 100644 index 000000000..8ad969018 --- /dev/null +++ b/tests/nodes/maxpool_2d_default.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::None, + array![2, 2].span(), + Option::None, + Option::None, + Option::None, + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d_default/input_0.cairo b/tests/nodes/maxpool_2d_default/input_0.cairo new file mode 100644 index 000000000..1950ff197 --- /dev/null +++ b/tests/nodes/maxpool_2d_default/input_0.cairo @@ -0,0 +1,207 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(8); + shape.append(8); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 70171, sign: true }); + data.append(FP16x16 { mag: 66033, sign: true }); + data.append(FP16x16 { mag: 34368, sign: true }); + data.append(FP16x16 { mag: 85619, sign: true }); + data.append(FP16x16 { mag: 10533, sign: false }); + data.append(FP16x16 { mag: 6665, sign: true }); + data.append(FP16x16 { mag: 62906, sign: false }); + data.append(FP16x16 { mag: 4088, sign: true }); + data.append(FP16x16 { mag: 48834, sign: true }); + data.append(FP16x16 { mag: 77568, sign: false }); + data.append(FP16x16 { mag: 72118, sign: false }); + data.append(FP16x16 { mag: 63744, sign: true }); + data.append(FP16x16 { mag: 97321, sign: false }); + data.append(FP16x16 { mag: 51751, sign: false }); + data.append(FP16x16 { mag: 49419, sign: true }); + data.append(FP16x16 { mag: 1074, sign: true }); + data.append(FP16x16 { mag: 33390, sign: true }); + data.append(FP16x16 { mag: 49420, sign: false }); + data.append(FP16x16 { mag: 76230, sign: true }); + data.append(FP16x16 { mag: 90932, sign: true }); + data.append(FP16x16 { mag: 51418, sign: true }); + data.append(FP16x16 { mag: 4380, sign: true }); + data.append(FP16x16 { mag: 160417, sign: false }); + data.append(FP16x16 { mag: 93830, sign: true }); + data.append(FP16x16 { mag: 49619, sign: true }); + data.append(FP16x16 { mag: 24892, sign: false }); + data.append(FP16x16 { mag: 60389, sign: false }); + data.append(FP16x16 { mag: 82115, sign: false }); + data.append(FP16x16 { mag: 27484, sign: true }); + data.append(FP16x16 { mag: 9373, sign: false }); + data.append(FP16x16 { mag: 13065, sign: true }); + data.append(FP16x16 { mag: 3608, sign: false }); + data.append(FP16x16 { mag: 12179, sign: false }); + data.append(FP16x16 { mag: 47764, sign: false }); + data.append(FP16x16 { mag: 45483, sign: true }); + data.append(FP16x16 { mag: 73567, sign: false }); + data.append(FP16x16 { mag: 108526, sign: false }); + data.append(FP16x16 { mag: 76992, sign: true }); + data.append(FP16x16 { mag: 88006, sign: true }); + data.append(FP16x16 { mag: 121995, sign: true }); + data.append(FP16x16 { mag: 30997, sign: true }); + data.append(FP16x16 { mag: 37845, sign: true }); + data.append(FP16x16 { mag: 41773, sign: false }); + data.append(FP16x16 { mag: 25693, sign: false }); + data.append(FP16x16 { mag: 2257, sign: false }); + data.append(FP16x16 { mag: 21906, sign: true }); + data.append(FP16x16 { mag: 5291, sign: false }); + data.append(FP16x16 { mag: 24583, sign: true }); + data.append(FP16x16 { mag: 60049, sign: true }); + data.append(FP16x16 { mag: 9991, sign: false }); + data.append(FP16x16 { mag: 104858, sign: false }); + data.append(FP16x16 { mag: 55871, sign: true }); + data.append(FP16x16 { mag: 54598, sign: true }); + data.append(FP16x16 { mag: 167306, sign: true }); + data.append(FP16x16 { mag: 35180, sign: false }); + data.append(FP16x16 { mag: 196014, sign: true }); + data.append(FP16x16 { mag: 49285, sign: true }); + data.append(FP16x16 { mag: 50669, sign: true }); + data.append(FP16x16 { mag: 11523, sign: true }); + data.append(FP16x16 { mag: 9496, sign: true }); + data.append(FP16x16 { mag: 55801, sign: false }); + data.append(FP16x16 { mag: 104529, sign: false }); + data.append(FP16x16 { mag: 637, sign: true }); + data.append(FP16x16 { mag: 93676, sign: true }); + data.append(FP16x16 { mag: 24789, sign: false }); + data.append(FP16x16 { mag: 7861, sign: false }); + data.append(FP16x16 { mag: 51137, sign: false }); + data.append(FP16x16 { mag: 73137, sign: true }); + data.append(FP16x16 { mag: 99812, sign: false }); + data.append(FP16x16 { mag: 62976, sign: true }); + data.append(FP16x16 { mag: 17193, sign: false }); + data.append(FP16x16 { mag: 30532, sign: true }); + data.append(FP16x16 { mag: 8014, sign: false }); + data.append(FP16x16 { mag: 47501, sign: true }); + data.append(FP16x16 { mag: 66682, sign: true }); + data.append(FP16x16 { mag: 95646, sign: false }); + data.append(FP16x16 { mag: 20504, sign: true }); + data.append(FP16x16 { mag: 4688, sign: true }); + data.append(FP16x16 { mag: 26672, sign: false }); + data.append(FP16x16 { mag: 88843, sign: false }); + data.append(FP16x16 { mag: 100847, sign: false }); + data.append(FP16x16 { mag: 128504, sign: true }); + data.append(FP16x16 { mag: 58079, sign: true }); + data.append(FP16x16 { mag: 42461, sign: false }); + data.append(FP16x16 { mag: 20574, sign: true }); + data.append(FP16x16 { mag: 94980, sign: false }); + data.append(FP16x16 { mag: 123767, sign: true }); + data.append(FP16x16 { mag: 81525, sign: true }); + data.append(FP16x16 { mag: 100345, sign: false }); + data.append(FP16x16 { mag: 27527, sign: false }); + data.append(FP16x16 { mag: 162338, sign: false }); + data.append(FP16x16 { mag: 90315, sign: true }); + data.append(FP16x16 { mag: 98283, sign: false }); + data.append(FP16x16 { mag: 51291, sign: false }); + data.append(FP16x16 { mag: 15507, sign: false }); + data.append(FP16x16 { mag: 129858, sign: true }); + data.append(FP16x16 { mag: 70575, sign: false }); + data.append(FP16x16 { mag: 717, sign: true }); + data.append(FP16x16 { mag: 27936, sign: true }); + data.append(FP16x16 { mag: 24785, sign: false }); + data.append(FP16x16 { mag: 47070, sign: false }); + data.append(FP16x16 { mag: 53060, sign: true }); + data.append(FP16x16 { mag: 71736, sign: true }); + data.append(FP16x16 { mag: 23701, sign: true }); + data.append(FP16x16 { mag: 73045, sign: true }); + data.append(FP16x16 { mag: 90496, sign: true }); + data.append(FP16x16 { mag: 100675, sign: true }); + data.append(FP16x16 { mag: 7795, sign: true }); + data.append(FP16x16 { mag: 39581, sign: false }); + data.append(FP16x16 { mag: 3716, sign: true }); + data.append(FP16x16 { mag: 76732, sign: true }); + data.append(FP16x16 { mag: 43912, sign: true }); + data.append(FP16x16 { mag: 19320, sign: true }); + data.append(FP16x16 { mag: 22545, sign: true }); + data.append(FP16x16 { mag: 27599, sign: false }); + data.append(FP16x16 { mag: 32793, sign: true }); + data.append(FP16x16 { mag: 47706, sign: true }); + data.append(FP16x16 { mag: 96112, sign: true }); + data.append(FP16x16 { mag: 34764, sign: false }); + data.append(FP16x16 { mag: 77647, sign: true }); + data.append(FP16x16 { mag: 35485, sign: false }); + data.append(FP16x16 { mag: 5584, sign: true }); + data.append(FP16x16 { mag: 11917, sign: false }); + data.append(FP16x16 { mag: 37395, sign: true }); + data.append(FP16x16 { mag: 38246, sign: true }); + data.append(FP16x16 { mag: 34063, sign: true }); + data.append(FP16x16 { mag: 20168, sign: false }); + data.append(FP16x16 { mag: 72849, sign: false }); + data.append(FP16x16 { mag: 40801, sign: false }); + data.append(FP16x16 { mag: 42674, sign: false }); + data.append(FP16x16 { mag: 22630, sign: true }); + data.append(FP16x16 { mag: 76034, sign: false }); + data.append(FP16x16 { mag: 62973, sign: false }); + data.append(FP16x16 { mag: 116410, sign: false }); + data.append(FP16x16 { mag: 1951, sign: true }); + data.append(FP16x16 { mag: 33165, sign: true }); + data.append(FP16x16 { mag: 46154, sign: true }); + data.append(FP16x16 { mag: 50498, sign: true }); + data.append(FP16x16 { mag: 5557, sign: true }); + data.append(FP16x16 { mag: 15958, sign: true }); + data.append(FP16x16 { mag: 55572, sign: true }); + data.append(FP16x16 { mag: 116353, sign: true }); + data.append(FP16x16 { mag: 104928, sign: true }); + data.append(FP16x16 { mag: 12275, sign: false }); + data.append(FP16x16 { mag: 2617, sign: true }); + data.append(FP16x16 { mag: 13198, sign: false }); + data.append(FP16x16 { mag: 71218, sign: true }); + data.append(FP16x16 { mag: 8582, sign: false }); + data.append(FP16x16 { mag: 34259, sign: false }); + data.append(FP16x16 { mag: 32055, sign: false }); + data.append(FP16x16 { mag: 18660, sign: false }); + data.append(FP16x16 { mag: 5926, sign: true }); + data.append(FP16x16 { mag: 2802, sign: true }); + data.append(FP16x16 { mag: 71274, sign: false }); + data.append(FP16x16 { mag: 37167, sign: false }); + data.append(FP16x16 { mag: 8185, sign: true }); + data.append(FP16x16 { mag: 53587, sign: true }); + data.append(FP16x16 { mag: 24956, sign: false }); + data.append(FP16x16 { mag: 47492, sign: true }); + data.append(FP16x16 { mag: 30685, sign: true }); + data.append(FP16x16 { mag: 65599, sign: false }); + data.append(FP16x16 { mag: 110444, sign: false }); + data.append(FP16x16 { mag: 11800, sign: false }); + data.append(FP16x16 { mag: 21534, sign: true }); + data.append(FP16x16 { mag: 4907, sign: true }); + data.append(FP16x16 { mag: 67101, sign: false }); + data.append(FP16x16 { mag: 38260, sign: true }); + data.append(FP16x16 { mag: 61924, sign: true }); + data.append(FP16x16 { mag: 7527, sign: false }); + data.append(FP16x16 { mag: 49451, sign: false }); + data.append(FP16x16 { mag: 182905, sign: true }); + data.append(FP16x16 { mag: 16395, sign: true }); + data.append(FP16x16 { mag: 67153, sign: true }); + data.append(FP16x16 { mag: 31050, sign: false }); + data.append(FP16x16 { mag: 5364, sign: true }); + data.append(FP16x16 { mag: 68197, sign: true }); + data.append(FP16x16 { mag: 60008, sign: false }); + data.append(FP16x16 { mag: 77429, sign: true }); + data.append(FP16x16 { mag: 6129, sign: false }); + data.append(FP16x16 { mag: 89537, sign: true }); + data.append(FP16x16 { mag: 46834, sign: false }); + data.append(FP16x16 { mag: 60579, sign: false }); + data.append(FP16x16 { mag: 66521, sign: false }); + data.append(FP16x16 { mag: 64292, sign: false }); + data.append(FP16x16 { mag: 115133, sign: true }); + data.append(FP16x16 { mag: 94534, sign: false }); + data.append(FP16x16 { mag: 39659, sign: false }); + data.append(FP16x16 { mag: 67484, sign: true }); + data.append(FP16x16 { mag: 20442, sign: true }); + data.append(FP16x16 { mag: 54691, sign: false }); + data.append(FP16x16 { mag: 81798, sign: false }); + data.append(FP16x16 { mag: 89422, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_default/output_0.cairo b/tests/nodes/maxpool_2d_default/output_0.cairo new file mode 100644 index 000000000..fc25d182a --- /dev/null +++ b/tests/nodes/maxpool_2d_default/output_0.cairo @@ -0,0 +1,162 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(7); + shape.append(7); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 77568, sign: false }); + data.append(FP16x16 { mag: 77568, sign: false }); + data.append(FP16x16 { mag: 72118, sign: false }); + data.append(FP16x16 { mag: 97321, sign: false }); + data.append(FP16x16 { mag: 97321, sign: false }); + data.append(FP16x16 { mag: 62906, sign: false }); + data.append(FP16x16 { mag: 62906, sign: false }); + data.append(FP16x16 { mag: 77568, sign: false }); + data.append(FP16x16 { mag: 77568, sign: false }); + data.append(FP16x16 { mag: 72118, sign: false }); + data.append(FP16x16 { mag: 97321, sign: false }); + data.append(FP16x16 { mag: 97321, sign: false }); + data.append(FP16x16 { mag: 160417, sign: false }); + data.append(FP16x16 { mag: 160417, sign: false }); + data.append(FP16x16 { mag: 49420, sign: false }); + data.append(FP16x16 { mag: 60389, sign: false }); + data.append(FP16x16 { mag: 82115, sign: false }); + data.append(FP16x16 { mag: 82115, sign: false }); + data.append(FP16x16 { mag: 9373, sign: false }); + data.append(FP16x16 { mag: 160417, sign: false }); + data.append(FP16x16 { mag: 160417, sign: false }); + data.append(FP16x16 { mag: 47764, sign: false }); + data.append(FP16x16 { mag: 60389, sign: false }); + data.append(FP16x16 { mag: 82115, sign: false }); + data.append(FP16x16 { mag: 108526, sign: false }); + data.append(FP16x16 { mag: 108526, sign: false }); + data.append(FP16x16 { mag: 9373, sign: false }); + data.append(FP16x16 { mag: 3608, sign: false }); + data.append(FP16x16 { mag: 47764, sign: false }); + data.append(FP16x16 { mag: 47764, sign: false }); + data.append(FP16x16 { mag: 73567, sign: false }); + data.append(FP16x16 { mag: 108526, sign: false }); + data.append(FP16x16 { mag: 108526, sign: false }); + data.append(FP16x16 { mag: 5291, sign: false }); + data.append(FP16x16 { mag: 5291, sign: false }); + data.append(FP16x16 { mag: 9991, sign: false }); + data.append(FP16x16 { mag: 104858, sign: false }); + data.append(FP16x16 { mag: 104858, sign: false }); + data.append(FP16x16 { mag: 25693, sign: false }); + data.append(FP16x16 { mag: 2257, sign: false }); + data.append(FP16x16 { mag: 35180, sign: false }); + data.append(FP16x16 { mag: 35180, sign: false }); + data.append(FP16x16 { mag: 9991, sign: false }); + data.append(FP16x16 { mag: 104858, sign: false }); + data.append(FP16x16 { mag: 104858, sign: false }); + data.append(FP16x16 { mag: 55801, sign: false }); + data.append(FP16x16 { mag: 104529, sign: false }); + data.append(FP16x16 { mag: 104529, sign: false }); + data.append(FP16x16 { mag: 35180, sign: false }); + data.append(FP16x16 { mag: 24789, sign: false }); + data.append(FP16x16 { mag: 51137, sign: false }); + data.append(FP16x16 { mag: 95646, sign: false }); + data.append(FP16x16 { mag: 99812, sign: false }); + data.append(FP16x16 { mag: 99812, sign: false }); + data.append(FP16x16 { mag: 26672, sign: false }); + data.append(FP16x16 { mag: 88843, sign: false }); + data.append(FP16x16 { mag: 100847, sign: false }); + data.append(FP16x16 { mag: 47501, sign: true }); + data.append(FP16x16 { mag: 95646, sign: false }); + data.append(FP16x16 { mag: 95646, sign: false }); + data.append(FP16x16 { mag: 94980, sign: false }); + data.append(FP16x16 { mag: 94980, sign: false }); + data.append(FP16x16 { mag: 88843, sign: false }); + data.append(FP16x16 { mag: 100847, sign: false }); + data.append(FP16x16 { mag: 162338, sign: false }); + data.append(FP16x16 { mag: 162338, sign: false }); + data.append(FP16x16 { mag: 98283, sign: false }); + data.append(FP16x16 { mag: 98283, sign: false }); + data.append(FP16x16 { mag: 94980, sign: false }); + data.append(FP16x16 { mag: 15507, sign: false }); + data.append(FP16x16 { mag: 100345, sign: false }); + data.append(FP16x16 { mag: 162338, sign: false }); + data.append(FP16x16 { mag: 162338, sign: false }); + data.append(FP16x16 { mag: 98283, sign: false }); + data.append(FP16x16 { mag: 98283, sign: false }); + data.append(FP16x16 { mag: 51291, sign: false }); + data.append(FP16x16 { mag: 15507, sign: false }); + data.append(FP16x16 { mag: 70575, sign: false }); + data.append(FP16x16 { mag: 717, sign: true }); + data.append(FP16x16 { mag: 24785, sign: false }); + data.append(FP16x16 { mag: 47070, sign: false }); + data.append(FP16x16 { mag: 47070, sign: false }); + data.append(FP16x16 { mag: 3716, sign: true }); + data.append(FP16x16 { mag: 23701, sign: true }); + data.append(FP16x16 { mag: 19320, sign: true }); + data.append(FP16x16 { mag: 27599, sign: false }); + data.append(FP16x16 { mag: 27599, sign: false }); + data.append(FP16x16 { mag: 39581, sign: false }); + data.append(FP16x16 { mag: 39581, sign: false }); + data.append(FP16x16 { mag: 34764, sign: false }); + data.append(FP16x16 { mag: 34764, sign: false }); + data.append(FP16x16 { mag: 35485, sign: false }); + data.append(FP16x16 { mag: 27599, sign: false }); + data.append(FP16x16 { mag: 27599, sign: false }); + data.append(FP16x16 { mag: 32793, sign: true }); + data.append(FP16x16 { mag: 34063, sign: true }); + data.append(FP16x16 { mag: 34764, sign: false }); + data.append(FP16x16 { mag: 72849, sign: false }); + data.append(FP16x16 { mag: 42674, sign: false }); + data.append(FP16x16 { mag: 42674, sign: false }); + data.append(FP16x16 { mag: 76034, sign: false }); + data.append(FP16x16 { mag: 76034, sign: false }); + data.append(FP16x16 { mag: 116410, sign: false }); + data.append(FP16x16 { mag: 116410, sign: false }); + data.append(FP16x16 { mag: 12275, sign: false }); + data.append(FP16x16 { mag: 13198, sign: false }); + data.append(FP16x16 { mag: 13198, sign: false }); + data.append(FP16x16 { mag: 8582, sign: false }); + data.append(FP16x16 { mag: 34259, sign: false }); + data.append(FP16x16 { mag: 34259, sign: false }); + data.append(FP16x16 { mag: 32055, sign: false }); + data.append(FP16x16 { mag: 18660, sign: false }); + data.append(FP16x16 { mag: 71274, sign: false }); + data.append(FP16x16 { mag: 71274, sign: false }); + data.append(FP16x16 { mag: 37167, sign: false }); + data.append(FP16x16 { mag: 34259, sign: false }); + data.append(FP16x16 { mag: 34259, sign: false }); + data.append(FP16x16 { mag: 32055, sign: false }); + data.append(FP16x16 { mag: 18660, sign: false }); + data.append(FP16x16 { mag: 110444, sign: false }); + data.append(FP16x16 { mag: 110444, sign: false }); + data.append(FP16x16 { mag: 37167, sign: false }); + data.append(FP16x16 { mag: 4907, sign: true }); + data.append(FP16x16 { mag: 67101, sign: false }); + data.append(FP16x16 { mag: 67101, sign: false }); + data.append(FP16x16 { mag: 30685, sign: true }); + data.append(FP16x16 { mag: 110444, sign: false }); + data.append(FP16x16 { mag: 110444, sign: false }); + data.append(FP16x16 { mag: 11800, sign: false }); + data.append(FP16x16 { mag: 4907, sign: true }); + data.append(FP16x16 { mag: 67101, sign: false }); + data.append(FP16x16 { mag: 67101, sign: false }); + data.append(FP16x16 { mag: 5364, sign: true }); + data.append(FP16x16 { mag: 60008, sign: false }); + data.append(FP16x16 { mag: 49451, sign: false }); + data.append(FP16x16 { mag: 6129, sign: false }); + data.append(FP16x16 { mag: 46834, sign: false }); + data.append(FP16x16 { mag: 60579, sign: false }); + data.append(FP16x16 { mag: 66521, sign: false }); + data.append(FP16x16 { mag: 66521, sign: false }); + data.append(FP16x16 { mag: 94534, sign: false }); + data.append(FP16x16 { mag: 94534, sign: false }); + data.append(FP16x16 { mag: 39659, sign: false }); + data.append(FP16x16 { mag: 46834, sign: false }); + data.append(FP16x16 { mag: 60579, sign: false }); + data.append(FP16x16 { mag: 81798, sign: false }); + data.append(FP16x16 { mag: 89422, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_dilations.cairo b/tests/nodes/maxpool_2d_dilations.cairo new file mode 100644 index 000000000..dc8686055 --- /dev/null +++ b/tests/nodes/maxpool_2d_dilations.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d_dilations() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::Some(array![2, 2].span()), + array![2, 2].span(), + Option::None, + Option::None, + Option::None, + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d_dilations/input_0.cairo b/tests/nodes/maxpool_2d_dilations/input_0.cairo new file mode 100644 index 000000000..29b195332 --- /dev/null +++ b/tests/nodes/maxpool_2d_dilations/input_0.cairo @@ -0,0 +1,31 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(4); + shape.append(4); + + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_dilations/output_0.cairo b/tests/nodes/maxpool_2d_dilations/output_0.cairo new file mode 100644 index 000000000..af7270750 --- /dev/null +++ b/tests/nodes/maxpool_2d_dilations/output_0.cairo @@ -0,0 +1,19 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_pads_default.cairo b/tests/nodes/maxpool_2d_pads_default.cairo new file mode 100644 index 000000000..103017375 --- /dev/null +++ b/tests/nodes/maxpool_2d_pads_default.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d_pads_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::None, + array![5, 5].span(), + Option::Some(array![2, 2, 2, 2].span()), + Option::None, + Option::None, + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d_pads_default/input_0.cairo b/tests/nodes/maxpool_2d_pads_default/input_0.cairo new file mode 100644 index 000000000..28d2d90c9 --- /dev/null +++ b/tests/nodes/maxpool_2d_pads_default/input_0.cairo @@ -0,0 +1,40 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(5); + shape.append(5); + + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + data.append(FP16x16 { mag: 1114112, sign: false }); + data.append(FP16x16 { mag: 1179648, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1376256, sign: false }); + data.append(FP16x16 { mag: 1441792, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_pads_default/output_0.cairo b/tests/nodes/maxpool_2d_pads_default/output_0.cairo new file mode 100644 index 000000000..c31cfdd9f --- /dev/null +++ b/tests/nodes/maxpool_2d_pads_default/output_0.cairo @@ -0,0 +1,40 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(5); + shape.append(5); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1179648, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_same_lower_default.cairo b/tests/nodes/maxpool_2d_same_lower_default.cairo new file mode 100644 index 000000000..e947cc879 --- /dev/null +++ b/tests/nodes/maxpool_2d_same_lower_default.cairo @@ -0,0 +1,31 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::nn::FP16x16NN; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::operators::nn::AUTO_PAD; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d_same_lower_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::Some(AUTO_PAD::SAME_LOWER), + Option::None, + Option::None, + array![2, 2].span(), + Option::None, + Option::None, + Option::None, + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d_same_lower_default/input_0.cairo b/tests/nodes/maxpool_2d_same_lower_default/input_0.cairo new file mode 100644 index 000000000..2d3b05884 --- /dev/null +++ b/tests/nodes/maxpool_2d_same_lower_default/input_0.cairo @@ -0,0 +1,207 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(8); + shape.append(8); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 138703, sign: true }); + data.append(FP16x16 { mag: 17593, sign: false }); + data.append(FP16x16 { mag: 37316, sign: true }); + data.append(FP16x16 { mag: 18101, sign: true }); + data.append(FP16x16 { mag: 100611, sign: true }); + data.append(FP16x16 { mag: 659, sign: false }); + data.append(FP16x16 { mag: 100276, sign: false }); + data.append(FP16x16 { mag: 38390, sign: true }); + data.append(FP16x16 { mag: 33210, sign: false }); + data.append(FP16x16 { mag: 61895, sign: true }); + data.append(FP16x16 { mag: 7680, sign: true }); + data.append(FP16x16 { mag: 13113, sign: false }); + data.append(FP16x16 { mag: 72129, sign: true }); + data.append(FP16x16 { mag: 79353, sign: false }); + data.append(FP16x16 { mag: 4830, sign: false }); + data.append(FP16x16 { mag: 37310, sign: true }); + data.append(FP16x16 { mag: 59023, sign: false }); + data.append(FP16x16 { mag: 59144, sign: false }); + data.append(FP16x16 { mag: 20810, sign: true }); + data.append(FP16x16 { mag: 191104, sign: false }); + data.append(FP16x16 { mag: 41831, sign: false }); + data.append(FP16x16 { mag: 85357, sign: true }); + data.append(FP16x16 { mag: 68515, sign: false }); + data.append(FP16x16 { mag: 28484, sign: false }); + data.append(FP16x16 { mag: 45074, sign: false }); + data.append(FP16x16 { mag: 44778, sign: true }); + data.append(FP16x16 { mag: 36984, sign: false }); + data.append(FP16x16 { mag: 66850, sign: false }); + data.append(FP16x16 { mag: 112661, sign: false }); + data.append(FP16x16 { mag: 23651, sign: true }); + data.append(FP16x16 { mag: 79272, sign: false }); + data.append(FP16x16 { mag: 154926, sign: true }); + data.append(FP16x16 { mag: 10887, sign: true }); + data.append(FP16x16 { mag: 6880, sign: true }); + data.append(FP16x16 { mag: 59713, sign: true }); + data.append(FP16x16 { mag: 36990, sign: true }); + data.append(FP16x16 { mag: 47134, sign: true }); + data.append(FP16x16 { mag: 103368, sign: false }); + data.append(FP16x16 { mag: 94963, sign: true }); + data.append(FP16x16 { mag: 9558, sign: true }); + data.append(FP16x16 { mag: 141332, sign: true }); + data.append(FP16x16 { mag: 32922, sign: true }); + data.append(FP16x16 { mag: 14154, sign: true }); + data.append(FP16x16 { mag: 138698, sign: false }); + data.append(FP16x16 { mag: 26096, sign: false }); + data.append(FP16x16 { mag: 91856, sign: false }); + data.append(FP16x16 { mag: 31118, sign: true }); + data.append(FP16x16 { mag: 508, sign: false }); + data.append(FP16x16 { mag: 35988, sign: false }); + data.append(FP16x16 { mag: 9381, sign: true }); + data.append(FP16x16 { mag: 10816, sign: true }); + data.append(FP16x16 { mag: 28140, sign: false }); + data.append(FP16x16 { mag: 10298, sign: true }); + data.append(FP16x16 { mag: 68132, sign: false }); + data.append(FP16x16 { mag: 80322, sign: false }); + data.append(FP16x16 { mag: 88352, sign: false }); + data.append(FP16x16 { mag: 100098, sign: false }); + data.append(FP16x16 { mag: 53069, sign: false }); + data.append(FP16x16 { mag: 155072, sign: false }); + data.append(FP16x16 { mag: 22128, sign: true }); + data.append(FP16x16 { mag: 172627, sign: true }); + data.append(FP16x16 { mag: 20198, sign: false }); + data.append(FP16x16 { mag: 3764, sign: true }); + data.append(FP16x16 { mag: 34532, sign: false }); + data.append(FP16x16 { mag: 81111, sign: true }); + data.append(FP16x16 { mag: 38033, sign: true }); + data.append(FP16x16 { mag: 25795, sign: true }); + data.append(FP16x16 { mag: 53914, sign: true }); + data.append(FP16x16 { mag: 58934, sign: true }); + data.append(FP16x16 { mag: 74080, sign: true }); + data.append(FP16x16 { mag: 53723, sign: true }); + data.append(FP16x16 { mag: 10926, sign: false }); + data.append(FP16x16 { mag: 84619, sign: false }); + data.append(FP16x16 { mag: 188693, sign: false }); + data.append(FP16x16 { mag: 37774, sign: false }); + data.append(FP16x16 { mag: 33855, sign: false }); + data.append(FP16x16 { mag: 86756, sign: false }); + data.append(FP16x16 { mag: 47341, sign: false }); + data.append(FP16x16 { mag: 16804, sign: false }); + data.append(FP16x16 { mag: 19410, sign: true }); + data.append(FP16x16 { mag: 4857, sign: true }); + data.append(FP16x16 { mag: 144907, sign: false }); + data.append(FP16x16 { mag: 19674, sign: false }); + data.append(FP16x16 { mag: 7336, sign: true }); + data.append(FP16x16 { mag: 83249, sign: true }); + data.append(FP16x16 { mag: 13101, sign: true }); + data.append(FP16x16 { mag: 13796, sign: false }); + data.append(FP16x16 { mag: 23641, sign: true }); + data.append(FP16x16 { mag: 61764, sign: true }); + data.append(FP16x16 { mag: 28933, sign: true }); + data.append(FP16x16 { mag: 79450, sign: false }); + data.append(FP16x16 { mag: 43751, sign: false }); + data.append(FP16x16 { mag: 99475, sign: false }); + data.append(FP16x16 { mag: 99879, sign: true }); + data.append(FP16x16 { mag: 16143, sign: false }); + data.append(FP16x16 { mag: 19630, sign: true }); + data.append(FP16x16 { mag: 119844, sign: false }); + data.append(FP16x16 { mag: 4729, sign: false }); + data.append(FP16x16 { mag: 40092, sign: false }); + data.append(FP16x16 { mag: 52497, sign: false }); + data.append(FP16x16 { mag: 28145, sign: true }); + data.append(FP16x16 { mag: 31321, sign: false }); + data.append(FP16x16 { mag: 47437, sign: false }); + data.append(FP16x16 { mag: 59558, sign: false }); + data.append(FP16x16 { mag: 154091, sign: false }); + data.append(FP16x16 { mag: 60540, sign: false }); + data.append(FP16x16 { mag: 46907, sign: true }); + data.append(FP16x16 { mag: 84671, sign: false }); + data.append(FP16x16 { mag: 56013, sign: false }); + data.append(FP16x16 { mag: 58264, sign: true }); + data.append(FP16x16 { mag: 20243, sign: false }); + data.append(FP16x16 { mag: 147219, sign: true }); + data.append(FP16x16 { mag: 36880, sign: false }); + data.append(FP16x16 { mag: 80459, sign: true }); + data.append(FP16x16 { mag: 52556, sign: false }); + data.append(FP16x16 { mag: 176520, sign: true }); + data.append(FP16x16 { mag: 89561, sign: true }); + data.append(FP16x16 { mag: 45221, sign: false }); + data.append(FP16x16 { mag: 37020, sign: false }); + data.append(FP16x16 { mag: 88532, sign: true }); + data.append(FP16x16 { mag: 99592, sign: false }); + data.append(FP16x16 { mag: 6673, sign: true }); + data.append(FP16x16 { mag: 20497, sign: true }); + data.append(FP16x16 { mag: 48790, sign: false }); + data.append(FP16x16 { mag: 63481, sign: true }); + data.append(FP16x16 { mag: 93939, sign: true }); + data.append(FP16x16 { mag: 10523, sign: true }); + data.append(FP16x16 { mag: 90627, sign: false }); + data.append(FP16x16 { mag: 15429, sign: false }); + data.append(FP16x16 { mag: 9882, sign: false }); + data.append(FP16x16 { mag: 88221, sign: false }); + data.append(FP16x16 { mag: 103220, sign: false }); + data.append(FP16x16 { mag: 18470, sign: false }); + data.append(FP16x16 { mag: 116464, sign: true }); + data.append(FP16x16 { mag: 45172, sign: true }); + data.append(FP16x16 { mag: 28246, sign: false }); + data.append(FP16x16 { mag: 62933, sign: true }); + data.append(FP16x16 { mag: 80332, sign: false }); + data.append(FP16x16 { mag: 21278, sign: true }); + data.append(FP16x16 { mag: 56583, sign: true }); + data.append(FP16x16 { mag: 34590, sign: true }); + data.append(FP16x16 { mag: 48885, sign: true }); + data.append(FP16x16 { mag: 38070, sign: true }); + data.append(FP16x16 { mag: 51209, sign: true }); + data.append(FP16x16 { mag: 44554, sign: false }); + data.append(FP16x16 { mag: 75396, sign: true }); + data.append(FP16x16 { mag: 162232, sign: true }); + data.append(FP16x16 { mag: 85388, sign: true }); + data.append(FP16x16 { mag: 77567, sign: true }); + data.append(FP16x16 { mag: 46076, sign: false }); + data.append(FP16x16 { mag: 1258, sign: false }); + data.append(FP16x16 { mag: 75938, sign: true }); + data.append(FP16x16 { mag: 19808, sign: true }); + data.append(FP16x16 { mag: 3602, sign: true }); + data.append(FP16x16 { mag: 26122, sign: true }); + data.append(FP16x16 { mag: 48685, sign: true }); + data.append(FP16x16 { mag: 67709, sign: true }); + data.append(FP16x16 { mag: 15860, sign: false }); + data.append(FP16x16 { mag: 59382, sign: false }); + data.append(FP16x16 { mag: 39707, sign: true }); + data.append(FP16x16 { mag: 3979, sign: true }); + data.append(FP16x16 { mag: 14954, sign: false }); + data.append(FP16x16 { mag: 94433, sign: true }); + data.append(FP16x16 { mag: 24674, sign: false }); + data.append(FP16x16 { mag: 32149, sign: true }); + data.append(FP16x16 { mag: 96812, sign: false }); + data.append(FP16x16 { mag: 32335, sign: true }); + data.append(FP16x16 { mag: 26743, sign: false }); + data.append(FP16x16 { mag: 64580, sign: false }); + data.append(FP16x16 { mag: 17788, sign: true }); + data.append(FP16x16 { mag: 25898, sign: false }); + data.append(FP16x16 { mag: 36605, sign: false }); + data.append(FP16x16 { mag: 78960, sign: true }); + data.append(FP16x16 { mag: 117485, sign: true }); + data.append(FP16x16 { mag: 23270, sign: true }); + data.append(FP16x16 { mag: 1539, sign: false }); + data.append(FP16x16 { mag: 46000, sign: false }); + data.append(FP16x16 { mag: 110506, sign: true }); + data.append(FP16x16 { mag: 37096, sign: false }); + data.append(FP16x16 { mag: 24200, sign: true }); + data.append(FP16x16 { mag: 51581, sign: false }); + data.append(FP16x16 { mag: 17036, sign: false }); + data.append(FP16x16 { mag: 21576, sign: true }); + data.append(FP16x16 { mag: 61805, sign: false }); + data.append(FP16x16 { mag: 30579, sign: false }); + data.append(FP16x16 { mag: 23251, sign: true }); + data.append(FP16x16 { mag: 37590, sign: true }); + data.append(FP16x16 { mag: 30907, sign: false }); + data.append(FP16x16 { mag: 10479, sign: false }); + data.append(FP16x16 { mag: 15777, sign: true }); + data.append(FP16x16 { mag: 12917, sign: false }); + data.append(FP16x16 { mag: 109290, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_same_lower_default/output_0.cairo b/tests/nodes/maxpool_2d_same_lower_default/output_0.cairo new file mode 100644 index 000000000..b272a29cb --- /dev/null +++ b/tests/nodes/maxpool_2d_same_lower_default/output_0.cairo @@ -0,0 +1,207 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(8); + shape.append(8); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 138703, sign: true }); + data.append(FP16x16 { mag: 17593, sign: false }); + data.append(FP16x16 { mag: 17593, sign: false }); + data.append(FP16x16 { mag: 18101, sign: true }); + data.append(FP16x16 { mag: 18101, sign: true }); + data.append(FP16x16 { mag: 659, sign: false }); + data.append(FP16x16 { mag: 100276, sign: false }); + data.append(FP16x16 { mag: 100276, sign: false }); + data.append(FP16x16 { mag: 33210, sign: false }); + data.append(FP16x16 { mag: 33210, sign: false }); + data.append(FP16x16 { mag: 17593, sign: false }); + data.append(FP16x16 { mag: 13113, sign: false }); + data.append(FP16x16 { mag: 13113, sign: false }); + data.append(FP16x16 { mag: 79353, sign: false }); + data.append(FP16x16 { mag: 100276, sign: false }); + data.append(FP16x16 { mag: 100276, sign: false }); + data.append(FP16x16 { mag: 59023, sign: false }); + data.append(FP16x16 { mag: 59144, sign: false }); + data.append(FP16x16 { mag: 59144, sign: false }); + data.append(FP16x16 { mag: 191104, sign: false }); + data.append(FP16x16 { mag: 191104, sign: false }); + data.append(FP16x16 { mag: 79353, sign: false }); + data.append(FP16x16 { mag: 79353, sign: false }); + data.append(FP16x16 { mag: 68515, sign: false }); + data.append(FP16x16 { mag: 59023, sign: false }); + data.append(FP16x16 { mag: 59144, sign: false }); + data.append(FP16x16 { mag: 59144, sign: false }); + data.append(FP16x16 { mag: 191104, sign: false }); + data.append(FP16x16 { mag: 191104, sign: false }); + data.append(FP16x16 { mag: 112661, sign: false }); + data.append(FP16x16 { mag: 79272, sign: false }); + data.append(FP16x16 { mag: 79272, sign: false }); + data.append(FP16x16 { mag: 45074, sign: false }); + data.append(FP16x16 { mag: 45074, sign: false }); + data.append(FP16x16 { mag: 36984, sign: false }); + data.append(FP16x16 { mag: 66850, sign: false }); + data.append(FP16x16 { mag: 112661, sign: false }); + data.append(FP16x16 { mag: 112661, sign: false }); + data.append(FP16x16 { mag: 103368, sign: false }); + data.append(FP16x16 { mag: 79272, sign: false }); + data.append(FP16x16 { mag: 10887, sign: true }); + data.append(FP16x16 { mag: 6880, sign: true }); + data.append(FP16x16 { mag: 6880, sign: true }); + data.append(FP16x16 { mag: 138698, sign: false }); + data.append(FP16x16 { mag: 138698, sign: false }); + data.append(FP16x16 { mag: 103368, sign: false }); + data.append(FP16x16 { mag: 103368, sign: false }); + data.append(FP16x16 { mag: 508, sign: false }); + data.append(FP16x16 { mag: 35988, sign: false }); + data.append(FP16x16 { mag: 35988, sign: false }); + data.append(FP16x16 { mag: 9381, sign: true }); + data.append(FP16x16 { mag: 138698, sign: false }); + data.append(FP16x16 { mag: 138698, sign: false }); + data.append(FP16x16 { mag: 91856, sign: false }); + data.append(FP16x16 { mag: 91856, sign: false }); + data.append(FP16x16 { mag: 88352, sign: false }); + data.append(FP16x16 { mag: 100098, sign: false }); + data.append(FP16x16 { mag: 100098, sign: false }); + data.append(FP16x16 { mag: 155072, sign: false }); + data.append(FP16x16 { mag: 155072, sign: false }); + data.append(FP16x16 { mag: 28140, sign: false }); + data.append(FP16x16 { mag: 68132, sign: false }); + data.append(FP16x16 { mag: 80322, sign: false }); + data.append(FP16x16 { mag: 88352, sign: false }); + data.append(FP16x16 { mag: 81111, sign: true }); + data.append(FP16x16 { mag: 38033, sign: true }); + data.append(FP16x16 { mag: 25795, sign: true }); + data.append(FP16x16 { mag: 25795, sign: true }); + data.append(FP16x16 { mag: 53914, sign: true }); + data.append(FP16x16 { mag: 58934, sign: true }); + data.append(FP16x16 { mag: 53723, sign: true }); + data.append(FP16x16 { mag: 10926, sign: false }); + data.append(FP16x16 { mag: 84619, sign: false }); + data.append(FP16x16 { mag: 188693, sign: false }); + data.append(FP16x16 { mag: 188693, sign: false }); + data.append(FP16x16 { mag: 37774, sign: false }); + data.append(FP16x16 { mag: 86756, sign: false }); + data.append(FP16x16 { mag: 86756, sign: false }); + data.append(FP16x16 { mag: 47341, sign: false }); + data.append(FP16x16 { mag: 16804, sign: false }); + data.append(FP16x16 { mag: 84619, sign: false }); + data.append(FP16x16 { mag: 188693, sign: false }); + data.append(FP16x16 { mag: 188693, sign: false }); + data.append(FP16x16 { mag: 37774, sign: false }); + data.append(FP16x16 { mag: 86756, sign: false }); + data.append(FP16x16 { mag: 86756, sign: false }); + data.append(FP16x16 { mag: 47341, sign: false }); + data.append(FP16x16 { mag: 16804, sign: false }); + data.append(FP16x16 { mag: 4857, sign: true }); + data.append(FP16x16 { mag: 144907, sign: false }); + data.append(FP16x16 { mag: 144907, sign: false }); + data.append(FP16x16 { mag: 79450, sign: false }); + data.append(FP16x16 { mag: 99475, sign: false }); + data.append(FP16x16 { mag: 99475, sign: false }); + data.append(FP16x16 { mag: 16143, sign: false }); + data.append(FP16x16 { mag: 16143, sign: false }); + data.append(FP16x16 { mag: 119844, sign: false }); + data.append(FP16x16 { mag: 119844, sign: false }); + data.append(FP16x16 { mag: 79450, sign: false }); + data.append(FP16x16 { mag: 79450, sign: false }); + data.append(FP16x16 { mag: 99475, sign: false }); + data.append(FP16x16 { mag: 99475, sign: false }); + data.append(FP16x16 { mag: 47437, sign: false }); + data.append(FP16x16 { mag: 59558, sign: false }); + data.append(FP16x16 { mag: 154091, sign: false }); + data.append(FP16x16 { mag: 154091, sign: false }); + data.append(FP16x16 { mag: 60540, sign: false }); + data.append(FP16x16 { mag: 84671, sign: false }); + data.append(FP16x16 { mag: 84671, sign: false }); + data.append(FP16x16 { mag: 56013, sign: false }); + data.append(FP16x16 { mag: 47437, sign: false }); + data.append(FP16x16 { mag: 59558, sign: false }); + data.append(FP16x16 { mag: 154091, sign: false }); + data.append(FP16x16 { mag: 154091, sign: false }); + data.append(FP16x16 { mag: 60540, sign: false }); + data.append(FP16x16 { mag: 84671, sign: false }); + data.append(FP16x16 { mag: 84671, sign: false }); + data.append(FP16x16 { mag: 56013, sign: false }); + data.append(FP16x16 { mag: 45221, sign: false }); + data.append(FP16x16 { mag: 37020, sign: false }); + data.append(FP16x16 { mag: 99592, sign: false }); + data.append(FP16x16 { mag: 99592, sign: false }); + data.append(FP16x16 { mag: 52556, sign: false }); + data.append(FP16x16 { mag: 52556, sign: false }); + data.append(FP16x16 { mag: 48790, sign: false }); + data.append(FP16x16 { mag: 45221, sign: false }); + data.append(FP16x16 { mag: 45221, sign: false }); + data.append(FP16x16 { mag: 90627, sign: false }); + data.append(FP16x16 { mag: 15429, sign: false }); + data.append(FP16x16 { mag: 15429, sign: false }); + data.append(FP16x16 { mag: 88221, sign: false }); + data.append(FP16x16 { mag: 103220, sign: false }); + data.append(FP16x16 { mag: 103220, sign: false }); + data.append(FP16x16 { mag: 18470, sign: false }); + data.append(FP16x16 { mag: 45172, sign: true }); + data.append(FP16x16 { mag: 28246, sign: false }); + data.append(FP16x16 { mag: 15429, sign: false }); + data.append(FP16x16 { mag: 80332, sign: false }); + data.append(FP16x16 { mag: 88221, sign: false }); + data.append(FP16x16 { mag: 103220, sign: false }); + data.append(FP16x16 { mag: 103220, sign: false }); + data.append(FP16x16 { mag: 18470, sign: false }); + data.append(FP16x16 { mag: 38070, sign: true }); + data.append(FP16x16 { mag: 28246, sign: false }); + data.append(FP16x16 { mag: 44554, sign: false }); + data.append(FP16x16 { mag: 80332, sign: false }); + data.append(FP16x16 { mag: 80332, sign: false }); + data.append(FP16x16 { mag: 21278, sign: true }); + data.append(FP16x16 { mag: 34590, sign: true }); + data.append(FP16x16 { mag: 46076, sign: false }); + data.append(FP16x16 { mag: 46076, sign: false }); + data.append(FP16x16 { mag: 1258, sign: false }); + data.append(FP16x16 { mag: 44554, sign: false }); + data.append(FP16x16 { mag: 44554, sign: false }); + data.append(FP16x16 { mag: 3602, sign: true }); + data.append(FP16x16 { mag: 26122, sign: true }); + data.append(FP16x16 { mag: 48685, sign: true }); + data.append(FP16x16 { mag: 46076, sign: false }); + data.append(FP16x16 { mag: 59382, sign: false }); + data.append(FP16x16 { mag: 59382, sign: false }); + data.append(FP16x16 { mag: 3979, sign: true }); + data.append(FP16x16 { mag: 14954, sign: false }); + data.append(FP16x16 { mag: 14954, sign: false }); + data.append(FP16x16 { mag: 24674, sign: false }); + data.append(FP16x16 { mag: 24674, sign: false }); + data.append(FP16x16 { mag: 96812, sign: false }); + data.append(FP16x16 { mag: 96812, sign: false }); + data.append(FP16x16 { mag: 59382, sign: false }); + data.append(FP16x16 { mag: 64580, sign: false }); + data.append(FP16x16 { mag: 64580, sign: false }); + data.append(FP16x16 { mag: 25898, sign: false }); + data.append(FP16x16 { mag: 36605, sign: false }); + data.append(FP16x16 { mag: 36605, sign: false }); + data.append(FP16x16 { mag: 96812, sign: false }); + data.append(FP16x16 { mag: 96812, sign: false }); + data.append(FP16x16 { mag: 26743, sign: false }); + data.append(FP16x16 { mag: 64580, sign: false }); + data.append(FP16x16 { mag: 64580, sign: false }); + data.append(FP16x16 { mag: 37096, sign: false }); + data.append(FP16x16 { mag: 37096, sign: false }); + data.append(FP16x16 { mag: 51581, sign: false }); + data.append(FP16x16 { mag: 51581, sign: false }); + data.append(FP16x16 { mag: 17036, sign: false }); + data.append(FP16x16 { mag: 61805, sign: false }); + data.append(FP16x16 { mag: 46000, sign: false }); + data.append(FP16x16 { mag: 46000, sign: false }); + data.append(FP16x16 { mag: 37096, sign: false }); + data.append(FP16x16 { mag: 37096, sign: false }); + data.append(FP16x16 { mag: 51581, sign: false }); + data.append(FP16x16 { mag: 51581, sign: false }); + data.append(FP16x16 { mag: 17036, sign: false }); + data.append(FP16x16 { mag: 61805, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_same_upper.cairo b/tests/nodes/maxpool_2d_same_upper.cairo new file mode 100644 index 000000000..45b47735b --- /dev/null +++ b/tests/nodes/maxpool_2d_same_upper.cairo @@ -0,0 +1,31 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; +use orion::operators::nn::AUTO_PAD; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d_same_upper() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::Some(AUTO_PAD::SAME_UPPER), + Option::None, + Option::None, + array![3, 3].span(), + Option::None, + Option::None, + Option::Some(array![2, 2].span()), + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d_same_upper/input_0.cairo b/tests/nodes/maxpool_2d_same_upper/input_0.cairo new file mode 100644 index 000000000..28d2d90c9 --- /dev/null +++ b/tests/nodes/maxpool_2d_same_upper/input_0.cairo @@ -0,0 +1,40 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(5); + shape.append(5); + + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + data.append(FP16x16 { mag: 1114112, sign: false }); + data.append(FP16x16 { mag: 1179648, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1376256, sign: false }); + data.append(FP16x16 { mag: 1441792, sign: false }); + data.append(FP16x16 { mag: 1507328, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_same_upper/output_0.cairo b/tests/nodes/maxpool_2d_same_upper/output_0.cairo new file mode 100644 index 000000000..43239cab2 --- /dev/null +++ b/tests/nodes/maxpool_2d_same_upper/output_0.cairo @@ -0,0 +1,24 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 458752, sign: false }); + data.append(FP16x16 { mag: 589824, sign: false }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 1114112, sign: false }); + data.append(FP16x16 { mag: 1245184, sign: false }); + data.append(FP16x16 { mag: 1310720, sign: false }); + data.append(FP16x16 { mag: 1441792, sign: false }); + data.append(FP16x16 { mag: 1572864, sign: false }); + data.append(FP16x16 { mag: 1638400, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_same_upper_default.cairo b/tests/nodes/maxpool_2d_same_upper_default.cairo new file mode 100644 index 000000000..9bb855712 --- /dev/null +++ b/tests/nodes/maxpool_2d_same_upper_default.cairo @@ -0,0 +1,31 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; +use orion::operators::nn::AUTO_PAD; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_2d_same_upper_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::Some(AUTO_PAD::SAME_UPPER), + Option::None, + Option::None, + array![2, 2].span(), + Option::None, + Option::None, + Option::None, + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_2d_same_upper_default/input_0.cairo b/tests/nodes/maxpool_2d_same_upper_default/input_0.cairo new file mode 100644 index 000000000..fdaea89f1 --- /dev/null +++ b/tests/nodes/maxpool_2d_same_upper_default/input_0.cairo @@ -0,0 +1,207 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(8); + shape.append(8); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 52692, sign: true }); + data.append(FP16x16 { mag: 28547, sign: false }); + data.append(FP16x16 { mag: 61903, sign: false }); + data.append(FP16x16 { mag: 117176, sign: true }); + data.append(FP16x16 { mag: 55156, sign: true }); + data.append(FP16x16 { mag: 120666, sign: true }); + data.append(FP16x16 { mag: 11052, sign: true }); + data.append(FP16x16 { mag: 8291, sign: false }); + data.append(FP16x16 { mag: 30701, sign: true }); + data.append(FP16x16 { mag: 17967, sign: false }); + data.append(FP16x16 { mag: 88529, sign: false }); + data.append(FP16x16 { mag: 47477, sign: true }); + data.append(FP16x16 { mag: 2290, sign: false }); + data.append(FP16x16 { mag: 92642, sign: false }); + data.append(FP16x16 { mag: 25259, sign: false }); + data.append(FP16x16 { mag: 4575, sign: true }); + data.append(FP16x16 { mag: 2693, sign: false }); + data.append(FP16x16 { mag: 105488, sign: true }); + data.append(FP16x16 { mag: 60124, sign: true }); + data.append(FP16x16 { mag: 46704, sign: true }); + data.append(FP16x16 { mag: 59101, sign: false }); + data.append(FP16x16 { mag: 49138, sign: true }); + data.append(FP16x16 { mag: 19003, sign: false }); + data.append(FP16x16 { mag: 17818, sign: false }); + data.append(FP16x16 { mag: 67683, sign: true }); + data.append(FP16x16 { mag: 77933, sign: true }); + data.append(FP16x16 { mag: 12737, sign: true }); + data.append(FP16x16 { mag: 65213, sign: false }); + data.append(FP16x16 { mag: 97849, sign: true }); + data.append(FP16x16 { mag: 49974, sign: false }); + data.append(FP16x16 { mag: 102583, sign: true }); + data.append(FP16x16 { mag: 117996, sign: false }); + data.append(FP16x16 { mag: 43197, sign: false }); + data.append(FP16x16 { mag: 87131, sign: true }); + data.append(FP16x16 { mag: 165019, sign: false }); + data.append(FP16x16 { mag: 35679, sign: true }); + data.append(FP16x16 { mag: 234, sign: false }); + data.append(FP16x16 { mag: 26030, sign: false }); + data.append(FP16x16 { mag: 4122, sign: false }); + data.append(FP16x16 { mag: 47426, sign: false }); + data.append(FP16x16 { mag: 22922, sign: true }); + data.append(FP16x16 { mag: 117833, sign: true }); + data.append(FP16x16 { mag: 100009, sign: false }); + data.append(FP16x16 { mag: 4360, sign: true }); + data.append(FP16x16 { mag: 38570, sign: false }); + data.append(FP16x16 { mag: 163610, sign: false }); + data.append(FP16x16 { mag: 27943, sign: false }); + data.append(FP16x16 { mag: 46610, sign: false }); + data.append(FP16x16 { mag: 27879, sign: false }); + data.append(FP16x16 { mag: 90383, sign: true }); + data.append(FP16x16 { mag: 14715, sign: true }); + data.append(FP16x16 { mag: 96467, sign: true }); + data.append(FP16x16 { mag: 9921, sign: false }); + data.append(FP16x16 { mag: 77435, sign: false }); + data.append(FP16x16 { mag: 54601, sign: true }); + data.append(FP16x16 { mag: 2255, sign: true }); + data.append(FP16x16 { mag: 1811, sign: true }); + data.append(FP16x16 { mag: 27154, sign: false }); + data.append(FP16x16 { mag: 28554, sign: false }); + data.append(FP16x16 { mag: 68574, sign: false }); + data.append(FP16x16 { mag: 10013, sign: false }); + data.append(FP16x16 { mag: 54722, sign: true }); + data.append(FP16x16 { mag: 51289, sign: true }); + data.append(FP16x16 { mag: 104200, sign: true }); + data.append(FP16x16 { mag: 72679, sign: true }); + data.append(FP16x16 { mag: 49102, sign: false }); + data.append(FP16x16 { mag: 73473, sign: false }); + data.append(FP16x16 { mag: 12392, sign: true }); + data.append(FP16x16 { mag: 13918, sign: false }); + data.append(FP16x16 { mag: 25166, sign: false }); + data.append(FP16x16 { mag: 54632, sign: false }); + data.append(FP16x16 { mag: 71299, sign: true }); + data.append(FP16x16 { mag: 79231, sign: true }); + data.append(FP16x16 { mag: 20472, sign: false }); + data.append(FP16x16 { mag: 19723, sign: false }); + data.append(FP16x16 { mag: 44396, sign: true }); + data.append(FP16x16 { mag: 45735, sign: false }); + data.append(FP16x16 { mag: 73626, sign: false }); + data.append(FP16x16 { mag: 1061, sign: true }); + data.append(FP16x16 { mag: 17482, sign: true }); + data.append(FP16x16 { mag: 20656, sign: true }); + data.append(FP16x16 { mag: 69032, sign: true }); + data.append(FP16x16 { mag: 7840, sign: false }); + data.append(FP16x16 { mag: 1006, sign: false }); + data.append(FP16x16 { mag: 65113, sign: true }); + data.append(FP16x16 { mag: 56413, sign: false }); + data.append(FP16x16 { mag: 28968, sign: true }); + data.append(FP16x16 { mag: 52619, sign: false }); + data.append(FP16x16 { mag: 4590, sign: false }); + data.append(FP16x16 { mag: 15977, sign: true }); + data.append(FP16x16 { mag: 40501, sign: true }); + data.append(FP16x16 { mag: 2693, sign: false }); + data.append(FP16x16 { mag: 55620, sign: false }); + data.append(FP16x16 { mag: 6900, sign: true }); + data.append(FP16x16 { mag: 13408, sign: false }); + data.append(FP16x16 { mag: 55598, sign: true }); + data.append(FP16x16 { mag: 13670, sign: false }); + data.append(FP16x16 { mag: 4231, sign: true }); + data.append(FP16x16 { mag: 47002, sign: true }); + data.append(FP16x16 { mag: 60663, sign: false }); + data.append(FP16x16 { mag: 26283, sign: false }); + data.append(FP16x16 { mag: 156112, sign: true }); + data.append(FP16x16 { mag: 9884, sign: true }); + data.append(FP16x16 { mag: 6926, sign: true }); + data.append(FP16x16 { mag: 8429, sign: true }); + data.append(FP16x16 { mag: 3327, sign: true }); + data.append(FP16x16 { mag: 45839, sign: true }); + data.append(FP16x16 { mag: 57187, sign: false }); + data.append(FP16x16 { mag: 110913, sign: true }); + data.append(FP16x16 { mag: 62795, sign: true }); + data.append(FP16x16 { mag: 109207, sign: false }); + data.append(FP16x16 { mag: 13215, sign: false }); + data.append(FP16x16 { mag: 38528, sign: true }); + data.append(FP16x16 { mag: 59562, sign: false }); + data.append(FP16x16 { mag: 26280, sign: true }); + data.append(FP16x16 { mag: 162194, sign: false }); + data.append(FP16x16 { mag: 61452, sign: false }); + data.append(FP16x16 { mag: 120157, sign: true }); + data.append(FP16x16 { mag: 50927, sign: false }); + data.append(FP16x16 { mag: 47813, sign: false }); + data.append(FP16x16 { mag: 62074, sign: false }); + data.append(FP16x16 { mag: 163638, sign: false }); + data.append(FP16x16 { mag: 21818, sign: true }); + data.append(FP16x16 { mag: 90475, sign: false }); + data.append(FP16x16 { mag: 32112, sign: true }); + data.append(FP16x16 { mag: 23172, sign: false }); + data.append(FP16x16 { mag: 71023, sign: true }); + data.append(FP16x16 { mag: 16348, sign: false }); + data.append(FP16x16 { mag: 28131, sign: false }); + data.append(FP16x16 { mag: 68181, sign: false }); + data.append(FP16x16 { mag: 192465, sign: false }); + data.append(FP16x16 { mag: 22889, sign: false }); + data.append(FP16x16 { mag: 16486, sign: false }); + data.append(FP16x16 { mag: 105533, sign: false }); + data.append(FP16x16 { mag: 6657, sign: true }); + data.append(FP16x16 { mag: 37792, sign: true }); + data.append(FP16x16 { mag: 48157, sign: true }); + data.append(FP16x16 { mag: 66040, sign: false }); + data.append(FP16x16 { mag: 12294, sign: true }); + data.append(FP16x16 { mag: 60649, sign: true }); + data.append(FP16x16 { mag: 42113, sign: true }); + data.append(FP16x16 { mag: 88843, sign: false }); + data.append(FP16x16 { mag: 3531, sign: false }); + data.append(FP16x16 { mag: 2708, sign: false }); + data.append(FP16x16 { mag: 93290, sign: true }); + data.append(FP16x16 { mag: 128, sign: false }); + data.append(FP16x16 { mag: 119266, sign: true }); + data.append(FP16x16 { mag: 37523, sign: false }); + data.append(FP16x16 { mag: 74254, sign: true }); + data.append(FP16x16 { mag: 155781, sign: true }); + data.append(FP16x16 { mag: 39485, sign: false }); + data.append(FP16x16 { mag: 16925, sign: false }); + data.append(FP16x16 { mag: 143362, sign: false }); + data.append(FP16x16 { mag: 77348, sign: false }); + data.append(FP16x16 { mag: 61642, sign: true }); + data.append(FP16x16 { mag: 134754, sign: false }); + data.append(FP16x16 { mag: 65160, sign: false }); + data.append(FP16x16 { mag: 48174, sign: true }); + data.append(FP16x16 { mag: 23831, sign: false }); + data.append(FP16x16 { mag: 70016, sign: true }); + data.append(FP16x16 { mag: 38627, sign: true }); + data.append(FP16x16 { mag: 71569, sign: true }); + data.append(FP16x16 { mag: 53240, sign: false }); + data.append(FP16x16 { mag: 123500, sign: false }); + data.append(FP16x16 { mag: 38397, sign: false }); + data.append(FP16x16 { mag: 105854, sign: false }); + data.append(FP16x16 { mag: 151153, sign: false }); + data.append(FP16x16 { mag: 30204, sign: false }); + data.append(FP16x16 { mag: 85564, sign: true }); + data.append(FP16x16 { mag: 13085, sign: false }); + data.append(FP16x16 { mag: 55281, sign: true }); + data.append(FP16x16 { mag: 11452, sign: false }); + data.append(FP16x16 { mag: 94698, sign: false }); + data.append(FP16x16 { mag: 113647, sign: false }); + data.append(FP16x16 { mag: 72099, sign: false }); + data.append(FP16x16 { mag: 9025, sign: false }); + data.append(FP16x16 { mag: 11985, sign: true }); + data.append(FP16x16 { mag: 159746, sign: false }); + data.append(FP16x16 { mag: 19273, sign: true }); + data.append(FP16x16 { mag: 23362, sign: false }); + data.append(FP16x16 { mag: 11488, sign: true }); + data.append(FP16x16 { mag: 86897, sign: true }); + data.append(FP16x16 { mag: 17484, sign: false }); + data.append(FP16x16 { mag: 35937, sign: false }); + data.append(FP16x16 { mag: 16572, sign: true }); + data.append(FP16x16 { mag: 47800, sign: true }); + data.append(FP16x16 { mag: 134172, sign: true }); + data.append(FP16x16 { mag: 14711, sign: true }); + data.append(FP16x16 { mag: 70361, sign: true }); + data.append(FP16x16 { mag: 33675, sign: true }); + data.append(FP16x16 { mag: 8412, sign: true }); + data.append(FP16x16 { mag: 59966, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_2d_same_upper_default/output_0.cairo b/tests/nodes/maxpool_2d_same_upper_default/output_0.cairo new file mode 100644 index 000000000..74103d3a1 --- /dev/null +++ b/tests/nodes/maxpool_2d_same_upper_default/output_0.cairo @@ -0,0 +1,207 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(8); + shape.append(8); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 28547, sign: false }); + data.append(FP16x16 { mag: 88529, sign: false }); + data.append(FP16x16 { mag: 88529, sign: false }); + data.append(FP16x16 { mag: 2290, sign: false }); + data.append(FP16x16 { mag: 92642, sign: false }); + data.append(FP16x16 { mag: 92642, sign: false }); + data.append(FP16x16 { mag: 25259, sign: false }); + data.append(FP16x16 { mag: 8291, sign: false }); + data.append(FP16x16 { mag: 17967, sign: false }); + data.append(FP16x16 { mag: 88529, sign: false }); + data.append(FP16x16 { mag: 88529, sign: false }); + data.append(FP16x16 { mag: 59101, sign: false }); + data.append(FP16x16 { mag: 92642, sign: false }); + data.append(FP16x16 { mag: 92642, sign: false }); + data.append(FP16x16 { mag: 25259, sign: false }); + data.append(FP16x16 { mag: 17818, sign: false }); + data.append(FP16x16 { mag: 2693, sign: false }); + data.append(FP16x16 { mag: 12737, sign: true }); + data.append(FP16x16 { mag: 65213, sign: false }); + data.append(FP16x16 { mag: 65213, sign: false }); + data.append(FP16x16 { mag: 59101, sign: false }); + data.append(FP16x16 { mag: 49974, sign: false }); + data.append(FP16x16 { mag: 117996, sign: false }); + data.append(FP16x16 { mag: 117996, sign: false }); + data.append(FP16x16 { mag: 43197, sign: false }); + data.append(FP16x16 { mag: 165019, sign: false }); + data.append(FP16x16 { mag: 165019, sign: false }); + data.append(FP16x16 { mag: 65213, sign: false }); + data.append(FP16x16 { mag: 49974, sign: false }); + data.append(FP16x16 { mag: 49974, sign: false }); + data.append(FP16x16 { mag: 117996, sign: false }); + data.append(FP16x16 { mag: 117996, sign: false }); + data.append(FP16x16 { mag: 43197, sign: false }); + data.append(FP16x16 { mag: 165019, sign: false }); + data.append(FP16x16 { mag: 165019, sign: false }); + data.append(FP16x16 { mag: 38570, sign: false }); + data.append(FP16x16 { mag: 163610, sign: false }); + data.append(FP16x16 { mag: 163610, sign: false }); + data.append(FP16x16 { mag: 47426, sign: false }); + data.append(FP16x16 { mag: 47426, sign: false }); + data.append(FP16x16 { mag: 27879, sign: false }); + data.append(FP16x16 { mag: 100009, sign: false }); + data.append(FP16x16 { mag: 100009, sign: false }); + data.append(FP16x16 { mag: 38570, sign: false }); + data.append(FP16x16 { mag: 163610, sign: false }); + data.append(FP16x16 { mag: 163610, sign: false }); + data.append(FP16x16 { mag: 46610, sign: false }); + data.append(FP16x16 { mag: 46610, sign: false }); + data.append(FP16x16 { mag: 27879, sign: false }); + data.append(FP16x16 { mag: 28554, sign: false }); + data.append(FP16x16 { mag: 68574, sign: false }); + data.append(FP16x16 { mag: 68574, sign: false }); + data.append(FP16x16 { mag: 77435, sign: false }); + data.append(FP16x16 { mag: 77435, sign: false }); + data.append(FP16x16 { mag: 2255, sign: true }); + data.append(FP16x16 { mag: 2255, sign: true }); + data.append(FP16x16 { mag: 27154, sign: false }); + data.append(FP16x16 { mag: 28554, sign: false }); + data.append(FP16x16 { mag: 68574, sign: false }); + data.append(FP16x16 { mag: 68574, sign: false }); + data.append(FP16x16 { mag: 10013, sign: false }); + data.append(FP16x16 { mag: 51289, sign: true }); + data.append(FP16x16 { mag: 51289, sign: true }); + data.append(FP16x16 { mag: 104200, sign: true }); + data.append(FP16x16 { mag: 49102, sign: false }); + data.append(FP16x16 { mag: 73473, sign: false }); + data.append(FP16x16 { mag: 73473, sign: false }); + data.append(FP16x16 { mag: 45735, sign: false }); + data.append(FP16x16 { mag: 73626, sign: false }); + data.append(FP16x16 { mag: 73626, sign: false }); + data.append(FP16x16 { mag: 54632, sign: false }); + data.append(FP16x16 { mag: 17482, sign: true }); + data.append(FP16x16 { mag: 20472, sign: false }); + data.append(FP16x16 { mag: 20472, sign: false }); + data.append(FP16x16 { mag: 19723, sign: false }); + data.append(FP16x16 { mag: 45735, sign: false }); + data.append(FP16x16 { mag: 73626, sign: false }); + data.append(FP16x16 { mag: 73626, sign: false }); + data.append(FP16x16 { mag: 52619, sign: false }); + data.append(FP16x16 { mag: 52619, sign: false }); + data.append(FP16x16 { mag: 4590, sign: false }); + data.append(FP16x16 { mag: 7840, sign: false }); + data.append(FP16x16 { mag: 7840, sign: false }); + data.append(FP16x16 { mag: 55620, sign: false }); + data.append(FP16x16 { mag: 56413, sign: false }); + data.append(FP16x16 { mag: 56413, sign: false }); + data.append(FP16x16 { mag: 52619, sign: false }); + data.append(FP16x16 { mag: 52619, sign: false }); + data.append(FP16x16 { mag: 13670, sign: false }); + data.append(FP16x16 { mag: 4231, sign: true }); + data.append(FP16x16 { mag: 60663, sign: false }); + data.append(FP16x16 { mag: 60663, sign: false }); + data.append(FP16x16 { mag: 55620, sign: false }); + data.append(FP16x16 { mag: 13408, sign: false }); + data.append(FP16x16 { mag: 13408, sign: false }); + data.append(FP16x16 { mag: 6926, sign: true }); + data.append(FP16x16 { mag: 13670, sign: false }); + data.append(FP16x16 { mag: 3327, sign: true }); + data.append(FP16x16 { mag: 60663, sign: false }); + data.append(FP16x16 { mag: 60663, sign: false }); + data.append(FP16x16 { mag: 26283, sign: false }); + data.append(FP16x16 { mag: 109207, sign: false }); + data.append(FP16x16 { mag: 109207, sign: false }); + data.append(FP16x16 { mag: 13215, sign: false }); + data.append(FP16x16 { mag: 59562, sign: false }); + data.append(FP16x16 { mag: 59562, sign: false }); + data.append(FP16x16 { mag: 162194, sign: false }); + data.append(FP16x16 { mag: 162194, sign: false }); + data.append(FP16x16 { mag: 61452, sign: false }); + data.append(FP16x16 { mag: 109207, sign: false }); + data.append(FP16x16 { mag: 109207, sign: false }); + data.append(FP16x16 { mag: 47813, sign: false }); + data.append(FP16x16 { mag: 163638, sign: false }); + data.append(FP16x16 { mag: 163638, sign: false }); + data.append(FP16x16 { mag: 162194, sign: false }); + data.append(FP16x16 { mag: 162194, sign: false }); + data.append(FP16x16 { mag: 61452, sign: false }); + data.append(FP16x16 { mag: 50927, sign: false }); + data.append(FP16x16 { mag: 50927, sign: false }); + data.append(FP16x16 { mag: 47813, sign: false }); + data.append(FP16x16 { mag: 163638, sign: false }); + data.append(FP16x16 { mag: 163638, sign: false }); + data.append(FP16x16 { mag: 90475, sign: false }); + data.append(FP16x16 { mag: 90475, sign: false }); + data.append(FP16x16 { mag: 23172, sign: false }); + data.append(FP16x16 { mag: 23172, sign: false }); + data.append(FP16x16 { mag: 16348, sign: false }); + data.append(FP16x16 { mag: 16348, sign: false }); + data.append(FP16x16 { mag: 68181, sign: false }); + data.append(FP16x16 { mag: 192465, sign: false }); + data.append(FP16x16 { mag: 192465, sign: false }); + data.append(FP16x16 { mag: 22889, sign: false }); + data.append(FP16x16 { mag: 105533, sign: false }); + data.append(FP16x16 { mag: 105533, sign: false }); + data.append(FP16x16 { mag: 3531, sign: false }); + data.append(FP16x16 { mag: 2708, sign: false }); + data.append(FP16x16 { mag: 66040, sign: false }); + data.append(FP16x16 { mag: 66040, sign: false }); + data.append(FP16x16 { mag: 37523, sign: false }); + data.append(FP16x16 { mag: 37523, sign: false }); + data.append(FP16x16 { mag: 88843, sign: false }); + data.append(FP16x16 { mag: 88843, sign: false }); + data.append(FP16x16 { mag: 39485, sign: false }); + data.append(FP16x16 { mag: 16925, sign: false }); + data.append(FP16x16 { mag: 143362, sign: false }); + data.append(FP16x16 { mag: 77348, sign: false }); + data.append(FP16x16 { mag: 134754, sign: false }); + data.append(FP16x16 { mag: 134754, sign: false }); + data.append(FP16x16 { mag: 65160, sign: false }); + data.append(FP16x16 { mag: 39485, sign: false }); + data.append(FP16x16 { mag: 39485, sign: false }); + data.append(FP16x16 { mag: 16925, sign: false }); + data.append(FP16x16 { mag: 143362, sign: false }); + data.append(FP16x16 { mag: 77348, sign: false }); + data.append(FP16x16 { mag: 134754, sign: false }); + data.append(FP16x16 { mag: 134754, sign: false }); + data.append(FP16x16 { mag: 105854, sign: false }); + data.append(FP16x16 { mag: 151153, sign: false }); + data.append(FP16x16 { mag: 151153, sign: false }); + data.append(FP16x16 { mag: 30204, sign: false }); + data.append(FP16x16 { mag: 13085, sign: false }); + data.append(FP16x16 { mag: 53240, sign: false }); + data.append(FP16x16 { mag: 123500, sign: false }); + data.append(FP16x16 { mag: 123500, sign: false }); + data.append(FP16x16 { mag: 113647, sign: false }); + data.append(FP16x16 { mag: 151153, sign: false }); + data.append(FP16x16 { mag: 151153, sign: false }); + data.append(FP16x16 { mag: 30204, sign: false }); + data.append(FP16x16 { mag: 159746, sign: false }); + data.append(FP16x16 { mag: 159746, sign: false }); + data.append(FP16x16 { mag: 23362, sign: false }); + data.append(FP16x16 { mag: 94698, sign: false }); + data.append(FP16x16 { mag: 113647, sign: false }); + data.append(FP16x16 { mag: 113647, sign: false }); + data.append(FP16x16 { mag: 72099, sign: false }); + data.append(FP16x16 { mag: 35937, sign: false }); + data.append(FP16x16 { mag: 159746, sign: false }); + data.append(FP16x16 { mag: 159746, sign: false }); + data.append(FP16x16 { mag: 23362, sign: false }); + data.append(FP16x16 { mag: 23362, sign: false }); + data.append(FP16x16 { mag: 11488, sign: true }); + data.append(FP16x16 { mag: 17484, sign: false }); + data.append(FP16x16 { mag: 35937, sign: false }); + data.append(FP16x16 { mag: 35937, sign: false }); + data.append(FP16x16 { mag: 16572, sign: true }); + data.append(FP16x16 { mag: 47800, sign: true }); + data.append(FP16x16 { mag: 14711, sign: true }); + data.append(FP16x16 { mag: 14711, sign: true }); + data.append(FP16x16 { mag: 33675, sign: true }); + data.append(FP16x16 { mag: 8412, sign: true }); + data.append(FP16x16 { mag: 8412, sign: true }); + data.append(FP16x16 { mag: 59966, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_3d_dilations.cairo b/tests/nodes/maxpool_3d_dilations.cairo new file mode 100644 index 000000000..f7aafbfc7 --- /dev/null +++ b/tests/nodes/maxpool_3d_dilations.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_3d_dilations() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::Some(array![2, 2, 2].span()), + array![2, 2, 2].span(), + Option::None, + Option::None, + Option::Some(array![1, 1, 1].span()), + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_3d_dilations/input_0.cairo b/tests/nodes/maxpool_3d_dilations/input_0.cairo new file mode 100644 index 000000000..a094e9312 --- /dev/null +++ b/tests/nodes/maxpool_3d_dilations/input_0.cairo @@ -0,0 +1,80 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(4); + shape.append(4); + shape.append(4); + + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + 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 }); + data.append(FP16x16 { mag: 655360, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 851968, sign: false }); + data.append(FP16x16 { mag: 917504, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_3d_dilations/output_0.cairo b/tests/nodes/maxpool_3d_dilations/output_0.cairo new file mode 100644 index 000000000..c755b5052 --- /dev/null +++ b/tests/nodes/maxpool_3d_dilations/output_0.cairo @@ -0,0 +1,24 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(2); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + data.append(FP16x16 { mag: 720896, sign: false }); + data.append(FP16x16 { mag: 786432, sign: false }); + data.append(FP16x16 { mag: 983040, sign: false }); + data.append(FP16x16 { mag: 1048576, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_4d_dilations.cairo b/tests/nodes/maxpool_4d_dilations.cairo new file mode 100644 index 000000000..0ad3072ab --- /dev/null +++ b/tests/nodes/maxpool_4d_dilations.cairo @@ -0,0 +1,30 @@ +mod input_0; +mod output_0; + + +use orion::operators::nn::NNTrait; +use orion::numbers::FixedTrait; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::nn::FP16x16NN; + +#[test] +#[available_gas(2000000000)] +fn test_maxpool_4d_dilations() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let (y_0, _) = NNTrait::max_pool( + @input_0, + Option::None, + Option::None, + Option::Some(array![2, 2, 2, 2].span()), + array![2, 2, 2, 2].span(), + Option::None, + Option::None, + Option::None, + 1 + ); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/maxpool_4d_dilations/input_0.cairo b/tests/nodes/maxpool_4d_dilations/input_0.cairo new file mode 100644 index 000000000..3716a1363 --- /dev/null +++ b/tests/nodes/maxpool_4d_dilations/input_0.cairo @@ -0,0 +1,785 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(4); + shape.append(4); + shape.append(4); + shape.append(4); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 57326, sign: true }); + data.append(FP16x16 { mag: 21730, sign: true }); + data.append(FP16x16 { mag: 42582, sign: false }); + data.append(FP16x16 { mag: 39862, sign: false }); + data.append(FP16x16 { mag: 33772, sign: false }); + data.append(FP16x16 { mag: 85813, sign: false }); + data.append(FP16x16 { mag: 53862, sign: false }); + data.append(FP16x16 { mag: 172442, sign: false }); + data.append(FP16x16 { mag: 49149, sign: false }); + data.append(FP16x16 { mag: 22905, sign: true }); + data.append(FP16x16 { mag: 33282, sign: true }); + data.append(FP16x16 { mag: 16327, sign: true }); + data.append(FP16x16 { mag: 107261, sign: true }); + data.append(FP16x16 { mag: 46347, sign: true }); + data.append(FP16x16 { mag: 6995, sign: true }); + data.append(FP16x16 { mag: 24181, sign: false }); + data.append(FP16x16 { mag: 104507, sign: false }); + data.append(FP16x16 { mag: 30885, sign: false }); + data.append(FP16x16 { mag: 171591, sign: false }); + data.append(FP16x16 { mag: 11569, sign: true }); + data.append(FP16x16 { mag: 8237, sign: false }); + data.append(FP16x16 { mag: 36041, sign: false }); + data.append(FP16x16 { mag: 70645, sign: true }); + data.append(FP16x16 { mag: 193719, sign: false }); + data.append(FP16x16 { mag: 69351, sign: true }); + data.append(FP16x16 { mag: 28229, sign: false }); + data.append(FP16x16 { mag: 52027, sign: true }); + data.append(FP16x16 { mag: 13767, sign: true }); + data.append(FP16x16 { mag: 11650, sign: false }); + data.append(FP16x16 { mag: 10048, sign: true }); + data.append(FP16x16 { mag: 101266, sign: false }); + data.append(FP16x16 { mag: 61859, sign: true }); + data.append(FP16x16 { mag: 21139, sign: true }); + data.append(FP16x16 { mag: 77012, sign: false }); + data.append(FP16x16 { mag: 10808, sign: false }); + data.append(FP16x16 { mag: 38568, sign: true }); + data.append(FP16x16 { mag: 117829, sign: false }); + data.append(FP16x16 { mag: 65427, sign: true }); + data.append(FP16x16 { mag: 32883, sign: false }); + data.append(FP16x16 { mag: 88408, sign: true }); + data.append(FP16x16 { mag: 30189, sign: true }); + data.append(FP16x16 { mag: 134656, sign: true }); + data.append(FP16x16 { mag: 63780, sign: true }); + data.append(FP16x16 { mag: 26968, sign: false }); + data.append(FP16x16 { mag: 17303, sign: false }); + data.append(FP16x16 { mag: 58933, sign: true }); + data.append(FP16x16 { mag: 22852, sign: false }); + data.append(FP16x16 { mag: 46707, sign: true }); + data.append(FP16x16 { mag: 44311, sign: true }); + data.append(FP16x16 { mag: 67011, sign: true }); + data.append(FP16x16 { mag: 17975, sign: true }); + data.append(FP16x16 { mag: 19940, sign: true }); + data.append(FP16x16 { mag: 26874, sign: false }); + data.append(FP16x16 { mag: 77148, sign: true }); + data.append(FP16x16 { mag: 1260, sign: false }); + data.append(FP16x16 { mag: 39435, sign: true }); + data.append(FP16x16 { mag: 57637, sign: true }); + data.append(FP16x16 { mag: 70584, sign: true }); + data.append(FP16x16 { mag: 90946, sign: false }); + data.append(FP16x16 { mag: 120636, sign: true }); + data.append(FP16x16 { mag: 44169, sign: false }); + data.append(FP16x16 { mag: 20822, sign: true }); + data.append(FP16x16 { mag: 9217, sign: true }); + data.append(FP16x16 { mag: 56642, sign: true }); + data.append(FP16x16 { mag: 32615, sign: false }); + data.append(FP16x16 { mag: 56213, sign: false }); + data.append(FP16x16 { mag: 20292, sign: true }); + data.append(FP16x16 { mag: 67358, sign: true }); + data.append(FP16x16 { mag: 94393, sign: true }); + data.append(FP16x16 { mag: 39525, sign: true }); + data.append(FP16x16 { mag: 117822, sign: false }); + data.append(FP16x16 { mag: 63929, sign: false }); + data.append(FP16x16 { mag: 2412, sign: true }); + data.append(FP16x16 { mag: 35782, sign: true }); + data.append(FP16x16 { mag: 42346, sign: true }); + data.append(FP16x16 { mag: 57619, sign: true }); + data.append(FP16x16 { mag: 76605, sign: false }); + data.append(FP16x16 { mag: 55629, sign: true }); + data.append(FP16x16 { mag: 26508, sign: true }); + data.append(FP16x16 { mag: 31496, sign: true }); + data.append(FP16x16 { mag: 29971, sign: false }); + data.append(FP16x16 { mag: 1317, sign: true }); + data.append(FP16x16 { mag: 86516, sign: true }); + data.append(FP16x16 { mag: 48878, sign: true }); + data.append(FP16x16 { mag: 91296, sign: true }); + data.append(FP16x16 { mag: 80537, sign: false }); + data.append(FP16x16 { mag: 143126, sign: true }); + data.append(FP16x16 { mag: 35843, sign: false }); + data.append(FP16x16 { mag: 794, sign: true }); + data.append(FP16x16 { mag: 4465, sign: true }); + data.append(FP16x16 { mag: 44772, sign: false }); + data.append(FP16x16 { mag: 114134, sign: false }); + data.append(FP16x16 { mag: 34012, sign: true }); + data.append(FP16x16 { mag: 81192, sign: false }); + data.append(FP16x16 { mag: 68824, sign: false }); + data.append(FP16x16 { mag: 35529, sign: true }); + data.append(FP16x16 { mag: 48725, sign: true }); + data.append(FP16x16 { mag: 48274, sign: true }); + data.append(FP16x16 { mag: 97602, sign: false }); + data.append(FP16x16 { mag: 18341, sign: true }); + data.append(FP16x16 { mag: 31756, sign: false }); + data.append(FP16x16 { mag: 79390, sign: false }); + data.append(FP16x16 { mag: 72973, sign: false }); + data.append(FP16x16 { mag: 1249, sign: true }); + data.append(FP16x16 { mag: 6551, sign: false }); + data.append(FP16x16 { mag: 41473, sign: false }); + data.append(FP16x16 { mag: 6590, sign: true }); + data.append(FP16x16 { mag: 31148, sign: true }); + data.append(FP16x16 { mag: 54083, sign: false }); + data.append(FP16x16 { mag: 11225, sign: false }); + data.append(FP16x16 { mag: 27239, sign: false }); + data.append(FP16x16 { mag: 26555, sign: false }); + data.append(FP16x16 { mag: 17820, sign: true }); + data.append(FP16x16 { mag: 122, sign: true }); + data.append(FP16x16 { mag: 67786, sign: false }); + data.append(FP16x16 { mag: 71452, sign: true }); + data.append(FP16x16 { mag: 84467, sign: true }); + data.append(FP16x16 { mag: 38713, sign: true }); + data.append(FP16x16 { mag: 84330, sign: false }); + data.append(FP16x16 { mag: 37483, sign: false }); + data.append(FP16x16 { mag: 93738, sign: false }); + data.append(FP16x16 { mag: 5609, sign: false }); + data.append(FP16x16 { mag: 32866, sign: true }); + data.append(FP16x16 { mag: 112546, sign: true }); + data.append(FP16x16 { mag: 109193, sign: true }); + data.append(FP16x16 { mag: 4021, sign: false }); + data.append(FP16x16 { mag: 57369, sign: false }); + data.append(FP16x16 { mag: 26880, sign: true }); + data.append(FP16x16 { mag: 28676, sign: false }); + data.append(FP16x16 { mag: 21352, sign: false }); + data.append(FP16x16 { mag: 19899, sign: false }); + data.append(FP16x16 { mag: 25250, sign: false }); + data.append(FP16x16 { mag: 17564, sign: false }); + data.append(FP16x16 { mag: 73191, sign: true }); + data.append(FP16x16 { mag: 15444, sign: false }); + data.append(FP16x16 { mag: 3166, sign: false }); + data.append(FP16x16 { mag: 58258, sign: true }); + data.append(FP16x16 { mag: 20879, sign: true }); + data.append(FP16x16 { mag: 39604, sign: false }); + data.append(FP16x16 { mag: 81651, sign: true }); + data.append(FP16x16 { mag: 91451, sign: false }); + data.append(FP16x16 { mag: 58072, sign: false }); + data.append(FP16x16 { mag: 131524, sign: false }); + data.append(FP16x16 { mag: 32727, sign: false }); + data.append(FP16x16 { mag: 165314, sign: false }); + data.append(FP16x16 { mag: 4141, sign: true }); + data.append(FP16x16 { mag: 14668, sign: false }); + data.append(FP16x16 { mag: 124609, sign: false }); + data.append(FP16x16 { mag: 33880, sign: false }); + data.append(FP16x16 { mag: 180279, sign: false }); + data.append(FP16x16 { mag: 23806, sign: true }); + data.append(FP16x16 { mag: 33978, sign: false }); + data.append(FP16x16 { mag: 102278, sign: false }); + data.append(FP16x16 { mag: 91608, sign: true }); + data.append(FP16x16 { mag: 81589, sign: false }); + data.append(FP16x16 { mag: 55843, sign: true }); + data.append(FP16x16 { mag: 70024, sign: true }); + data.append(FP16x16 { mag: 30011, sign: true }); + data.append(FP16x16 { mag: 18673, sign: false }); + data.append(FP16x16 { mag: 17510, sign: true }); + data.append(FP16x16 { mag: 94591, sign: true }); + data.append(FP16x16 { mag: 83587, sign: true }); + data.append(FP16x16 { mag: 80327, sign: true }); + data.append(FP16x16 { mag: 58704, sign: true }); + data.append(FP16x16 { mag: 12647, sign: true }); + data.append(FP16x16 { mag: 119066, sign: false }); + data.append(FP16x16 { mag: 24450, sign: true }); + data.append(FP16x16 { mag: 73352, sign: true }); + data.append(FP16x16 { mag: 37058, sign: false }); + data.append(FP16x16 { mag: 21557, sign: false }); + data.append(FP16x16 { mag: 20631, sign: true }); + data.append(FP16x16 { mag: 93230, sign: false }); + data.append(FP16x16 { mag: 49743, sign: true }); + data.append(FP16x16 { mag: 33398, sign: false }); + data.append(FP16x16 { mag: 30495, sign: false }); + data.append(FP16x16 { mag: 69280, sign: true }); + data.append(FP16x16 { mag: 11759, sign: true }); + data.append(FP16x16 { mag: 87054, sign: false }); + data.append(FP16x16 { mag: 66395, sign: false }); + data.append(FP16x16 { mag: 20819, sign: false }); + data.append(FP16x16 { mag: 107561, sign: false }); + data.append(FP16x16 { mag: 24839, sign: false }); + data.append(FP16x16 { mag: 101166, sign: true }); + data.append(FP16x16 { mag: 4674, sign: true }); + data.append(FP16x16 { mag: 15294, sign: false }); + data.append(FP16x16 { mag: 14943, sign: false }); + data.append(FP16x16 { mag: 7494, sign: false }); + data.append(FP16x16 { mag: 83926, sign: false }); + data.append(FP16x16 { mag: 127871, sign: true }); + data.append(FP16x16 { mag: 87804, sign: false }); + data.append(FP16x16 { mag: 79905, sign: false }); + data.append(FP16x16 { mag: 82486, sign: false }); + data.append(FP16x16 { mag: 23065, sign: true }); + data.append(FP16x16 { mag: 14517, sign: false }); + data.append(FP16x16 { mag: 146450, sign: true }); + data.append(FP16x16 { mag: 78522, sign: true }); + data.append(FP16x16 { mag: 50973, sign: false }); + data.append(FP16x16 { mag: 59565, sign: false }); + data.append(FP16x16 { mag: 75649, sign: false }); + data.append(FP16x16 { mag: 37180, sign: false }); + data.append(FP16x16 { mag: 10656, sign: true }); + data.append(FP16x16 { mag: 20803, sign: false }); + data.append(FP16x16 { mag: 19563, sign: true }); + data.append(FP16x16 { mag: 27034, sign: false }); + data.append(FP16x16 { mag: 14241, sign: true }); + data.append(FP16x16 { mag: 27342, sign: false }); + data.append(FP16x16 { mag: 15449, sign: false }); + data.append(FP16x16 { mag: 54417, sign: false }); + data.append(FP16x16 { mag: 28698, sign: false }); + data.append(FP16x16 { mag: 10291, sign: false }); + data.append(FP16x16 { mag: 78630, sign: true }); + data.append(FP16x16 { mag: 54471, sign: false }); + data.append(FP16x16 { mag: 63909, sign: false }); + data.append(FP16x16 { mag: 155625, sign: true }); + data.append(FP16x16 { mag: 46452, sign: true }); + data.append(FP16x16 { mag: 85081, sign: true }); + data.append(FP16x16 { mag: 149800, sign: true }); + data.append(FP16x16 { mag: 83814, sign: true }); + data.append(FP16x16 { mag: 13220, sign: true }); + data.append(FP16x16 { mag: 105580, sign: true }); + data.append(FP16x16 { mag: 45775, sign: false }); + data.append(FP16x16 { mag: 81027, sign: false }); + data.append(FP16x16 { mag: 45746, sign: false }); + data.append(FP16x16 { mag: 20454, sign: true }); + data.append(FP16x16 { mag: 54633, sign: false }); + data.append(FP16x16 { mag: 58097, sign: false }); + data.append(FP16x16 { mag: 51893, sign: true }); + data.append(FP16x16 { mag: 537, sign: false }); + data.append(FP16x16 { mag: 47389, sign: false }); + data.append(FP16x16 { mag: 7725, sign: false }); + data.append(FP16x16 { mag: 64559, sign: false }); + data.append(FP16x16 { mag: 120617, sign: true }); + data.append(FP16x16 { mag: 13069, sign: true }); + data.append(FP16x16 { mag: 13012, sign: false }); + data.append(FP16x16 { mag: 64929, sign: false }); + data.append(FP16x16 { mag: 13880, sign: true }); + data.append(FP16x16 { mag: 29696, sign: false }); + data.append(FP16x16 { mag: 23484, sign: true }); + data.append(FP16x16 { mag: 78538, sign: true }); + data.append(FP16x16 { mag: 108676, sign: true }); + data.append(FP16x16 { mag: 30019, sign: false }); + data.append(FP16x16 { mag: 17931, sign: true }); + data.append(FP16x16 { mag: 27085, sign: false }); + data.append(FP16x16 { mag: 24850, sign: false }); + data.append(FP16x16 { mag: 12153, sign: false }); + data.append(FP16x16 { mag: 11538, sign: true }); + data.append(FP16x16 { mag: 69066, sign: true }); + data.append(FP16x16 { mag: 12725, sign: false }); + data.append(FP16x16 { mag: 37986, sign: false }); + data.append(FP16x16 { mag: 53389, sign: true }); + data.append(FP16x16 { mag: 114518, sign: true }); + data.append(FP16x16 { mag: 74200, sign: true }); + data.append(FP16x16 { mag: 54403, sign: true }); + data.append(FP16x16 { mag: 31396, sign: false }); + data.append(FP16x16 { mag: 40194, sign: false }); + data.append(FP16x16 { mag: 9643, sign: false }); + data.append(FP16x16 { mag: 29485, sign: true }); + data.append(FP16x16 { mag: 12679, sign: false }); + data.append(FP16x16 { mag: 15850, sign: true }); + data.append(FP16x16 { mag: 39421, sign: true }); + data.append(FP16x16 { mag: 88854, sign: false }); + data.append(FP16x16 { mag: 1130, sign: false }); + data.append(FP16x16 { mag: 16366, sign: true }); + data.append(FP16x16 { mag: 33087, sign: true }); + data.append(FP16x16 { mag: 28745, sign: true }); + data.append(FP16x16 { mag: 3488, sign: false }); + data.append(FP16x16 { mag: 59008, sign: true }); + data.append(FP16x16 { mag: 4364, sign: false }); + data.append(FP16x16 { mag: 42280, sign: true }); + data.append(FP16x16 { mag: 100073, sign: true }); + data.append(FP16x16 { mag: 4958, sign: true }); + data.append(FP16x16 { mag: 37532, sign: true }); + data.append(FP16x16 { mag: 30930, sign: false }); + data.append(FP16x16 { mag: 29580, sign: true }); + data.append(FP16x16 { mag: 930, sign: true }); + data.append(FP16x16 { mag: 4486, sign: true }); + data.append(FP16x16 { mag: 82232, sign: true }); + data.append(FP16x16 { mag: 47028, sign: true }); + data.append(FP16x16 { mag: 8929, sign: false }); + data.append(FP16x16 { mag: 70968, sign: false }); + data.append(FP16x16 { mag: 38614, sign: true }); + data.append(FP16x16 { mag: 57990, sign: true }); + data.append(FP16x16 { mag: 51540, sign: true }); + data.append(FP16x16 { mag: 15430, sign: true }); + data.append(FP16x16 { mag: 60366, sign: true }); + data.append(FP16x16 { mag: 26896, sign: false }); + data.append(FP16x16 { mag: 55881, sign: false }); + data.append(FP16x16 { mag: 37730, sign: false }); + data.append(FP16x16 { mag: 1519, sign: true }); + data.append(FP16x16 { mag: 41072, sign: true }); + data.append(FP16x16 { mag: 70626, sign: true }); + data.append(FP16x16 { mag: 80140, sign: false }); + data.append(FP16x16 { mag: 48666, sign: true }); + data.append(FP16x16 { mag: 63754, sign: true }); + data.append(FP16x16 { mag: 64494, sign: true }); + data.append(FP16x16 { mag: 75672, sign: false }); + data.append(FP16x16 { mag: 39394, sign: true }); + data.append(FP16x16 { mag: 39881, sign: false }); + data.append(FP16x16 { mag: 23064, sign: true }); + data.append(FP16x16 { mag: 14821, sign: false }); + data.append(FP16x16 { mag: 27344, sign: true }); + data.append(FP16x16 { mag: 14728, sign: false }); + data.append(FP16x16 { mag: 59112, sign: true }); + data.append(FP16x16 { mag: 93610, sign: false }); + data.append(FP16x16 { mag: 80804, sign: true }); + data.append(FP16x16 { mag: 31174, sign: false }); + data.append(FP16x16 { mag: 31687, sign: true }); + data.append(FP16x16 { mag: 92103, sign: false }); + data.append(FP16x16 { mag: 84394, sign: false }); + data.append(FP16x16 { mag: 85999, sign: true }); + data.append(FP16x16 { mag: 93667, sign: true }); + data.append(FP16x16 { mag: 124722, sign: false }); + data.append(FP16x16 { mag: 29792, sign: true }); + data.append(FP16x16 { mag: 1719, sign: true }); + data.append(FP16x16 { mag: 10265, sign: false }); + data.append(FP16x16 { mag: 98163, sign: false }); + data.append(FP16x16 { mag: 44203, sign: false }); + data.append(FP16x16 { mag: 25672, sign: true }); + data.append(FP16x16 { mag: 92499, sign: false }); + data.append(FP16x16 { mag: 71127, sign: false }); + data.append(FP16x16 { mag: 107167, sign: true }); + data.append(FP16x16 { mag: 58795, sign: false }); + data.append(FP16x16 { mag: 33710, sign: false }); + data.append(FP16x16 { mag: 59629, sign: true }); + data.append(FP16x16 { mag: 12862, sign: false }); + data.append(FP16x16 { mag: 77599, sign: true }); + data.append(FP16x16 { mag: 2634, sign: false }); + data.append(FP16x16 { mag: 30404, sign: true }); + data.append(FP16x16 { mag: 34664, sign: false }); + data.append(FP16x16 { mag: 123236, sign: false }); + data.append(FP16x16 { mag: 9552, sign: false }); + data.append(FP16x16 { mag: 1400, sign: true }); + data.append(FP16x16 { mag: 57199, sign: true }); + data.append(FP16x16 { mag: 56490, sign: false }); + data.append(FP16x16 { mag: 15372, sign: false }); + data.append(FP16x16 { mag: 39428, sign: true }); + data.append(FP16x16 { mag: 22812, sign: true }); + data.append(FP16x16 { mag: 131564, sign: false }); + data.append(FP16x16 { mag: 119458, sign: true }); + data.append(FP16x16 { mag: 45170, sign: true }); + data.append(FP16x16 { mag: 59265, sign: false }); + data.append(FP16x16 { mag: 45955, sign: true }); + data.append(FP16x16 { mag: 54566, sign: true }); + data.append(FP16x16 { mag: 38354, sign: false }); + data.append(FP16x16 { mag: 138628, sign: true }); + data.append(FP16x16 { mag: 54243, sign: true }); + data.append(FP16x16 { mag: 47436, sign: false }); + data.append(FP16x16 { mag: 58880, sign: true }); + data.append(FP16x16 { mag: 48381, sign: false }); + data.append(FP16x16 { mag: 33455, sign: false }); + data.append(FP16x16 { mag: 38625, sign: false }); + data.append(FP16x16 { mag: 107342, sign: true }); + data.append(FP16x16 { mag: 46976, sign: false }); + data.append(FP16x16 { mag: 84206, sign: true }); + data.append(FP16x16 { mag: 29355, sign: false }); + data.append(FP16x16 { mag: 63176, sign: false }); + data.append(FP16x16 { mag: 34330, sign: true }); + data.append(FP16x16 { mag: 65025, sign: false }); + data.append(FP16x16 { mag: 25751, sign: true }); + data.append(FP16x16 { mag: 17809, sign: false }); + data.append(FP16x16 { mag: 22890, sign: false }); + data.append(FP16x16 { mag: 57565, sign: true }); + data.append(FP16x16 { mag: 16208, sign: false }); + data.append(FP16x16 { mag: 134370, sign: true }); + data.append(FP16x16 { mag: 58594, sign: false }); + data.append(FP16x16 { mag: 11743, sign: true }); + data.append(FP16x16 { mag: 44532, sign: true }); + data.append(FP16x16 { mag: 117474, sign: true }); + data.append(FP16x16 { mag: 44774, sign: false }); + data.append(FP16x16 { mag: 120360, sign: false }); + data.append(FP16x16 { mag: 28815, sign: false }); + data.append(FP16x16 { mag: 9241, sign: true }); + data.append(FP16x16 { mag: 25010, sign: false }); + data.append(FP16x16 { mag: 26794, sign: true }); + data.append(FP16x16 { mag: 36699, sign: true }); + data.append(FP16x16 { mag: 1476, sign: true }); + data.append(FP16x16 { mag: 39151, sign: true }); + data.append(FP16x16 { mag: 55160, sign: false }); + data.append(FP16x16 { mag: 51197, sign: false }); + data.append(FP16x16 { mag: 41092, sign: false }); + data.append(FP16x16 { mag: 63983, sign: true }); + data.append(FP16x16 { mag: 48252, sign: false }); + data.append(FP16x16 { mag: 51458, sign: false }); + data.append(FP16x16 { mag: 28035, sign: true }); + data.append(FP16x16 { mag: 93421, sign: false }); + data.append(FP16x16 { mag: 46679, sign: true }); + data.append(FP16x16 { mag: 81159, sign: false }); + data.append(FP16x16 { mag: 22207, sign: false }); + data.append(FP16x16 { mag: 24918, sign: false }); + data.append(FP16x16 { mag: 50366, sign: true }); + data.append(FP16x16 { mag: 38928, sign: true }); + data.append(FP16x16 { mag: 2326, sign: true }); + data.append(FP16x16 { mag: 41029, sign: true }); + data.append(FP16x16 { mag: 48971, sign: false }); + data.append(FP16x16 { mag: 102185, sign: true }); + data.append(FP16x16 { mag: 45825, sign: true }); + data.append(FP16x16 { mag: 6231, sign: true }); + data.append(FP16x16 { mag: 5992, sign: true }); + data.append(FP16x16 { mag: 60752, sign: false }); + data.append(FP16x16 { mag: 44154, sign: false }); + data.append(FP16x16 { mag: 58826, sign: true }); + data.append(FP16x16 { mag: 70712, sign: true }); + data.append(FP16x16 { mag: 16729, sign: true }); + data.append(FP16x16 { mag: 122822, sign: true }); + data.append(FP16x16 { mag: 5531, sign: true }); + data.append(FP16x16 { mag: 125650, sign: false }); + data.append(FP16x16 { mag: 116267, sign: false }); + data.append(FP16x16 { mag: 36107, sign: false }); + data.append(FP16x16 { mag: 28157, sign: true }); + data.append(FP16x16 { mag: 39470, sign: false }); + data.append(FP16x16 { mag: 47628, sign: false }); + data.append(FP16x16 { mag: 22813, sign: false }); + data.append(FP16x16 { mag: 48005, sign: false }); + data.append(FP16x16 { mag: 2843, sign: false }); + data.append(FP16x16 { mag: 65208, sign: false }); + data.append(FP16x16 { mag: 42277, sign: false }); + data.append(FP16x16 { mag: 18706, sign: true }); + data.append(FP16x16 { mag: 30606, sign: true }); + data.append(FP16x16 { mag: 27669, sign: true }); + data.append(FP16x16 { mag: 38065, sign: false }); + data.append(FP16x16 { mag: 11748, sign: false }); + data.append(FP16x16 { mag: 37583, sign: true }); + data.append(FP16x16 { mag: 37178, sign: false }); + data.append(FP16x16 { mag: 45096, sign: false }); + data.append(FP16x16 { mag: 120975, sign: true }); + data.append(FP16x16 { mag: 42732, sign: false }); + data.append(FP16x16 { mag: 87833, sign: false }); + data.append(FP16x16 { mag: 25683, sign: true }); + data.append(FP16x16 { mag: 57618, sign: true }); + data.append(FP16x16 { mag: 23177, sign: false }); + data.append(FP16x16 { mag: 5134, sign: true }); + data.append(FP16x16 { mag: 7581, sign: true }); + data.append(FP16x16 { mag: 75724, sign: true }); + data.append(FP16x16 { mag: 64458, sign: true }); + data.append(FP16x16 { mag: 141791, sign: true }); + data.append(FP16x16 { mag: 42154, sign: true }); + data.append(FP16x16 { mag: 12865, sign: false }); + data.append(FP16x16 { mag: 17234, sign: true }); + data.append(FP16x16 { mag: 126303, sign: true }); + data.append(FP16x16 { mag: 93382, sign: true }); + data.append(FP16x16 { mag: 57375, sign: false }); + data.append(FP16x16 { mag: 75818, sign: false }); + data.append(FP16x16 { mag: 5159, sign: false }); + data.append(FP16x16 { mag: 35848, sign: false }); + data.append(FP16x16 { mag: 4853, sign: false }); + data.append(FP16x16 { mag: 57301, sign: true }); + data.append(FP16x16 { mag: 8128, sign: true }); + data.append(FP16x16 { mag: 83696, sign: true }); + data.append(FP16x16 { mag: 136686, sign: false }); + data.append(FP16x16 { mag: 112560, sign: true }); + data.append(FP16x16 { mag: 66658, sign: false }); + data.append(FP16x16 { mag: 28200, sign: true }); + data.append(FP16x16 { mag: 40036, sign: false }); + data.append(FP16x16 { mag: 31169, sign: true }); + data.append(FP16x16 { mag: 174668, sign: false }); + data.append(FP16x16 { mag: 29868, sign: false }); + data.append(FP16x16 { mag: 10093, sign: true }); + data.append(FP16x16 { mag: 74999, sign: false }); + data.append(FP16x16 { mag: 78237, sign: true }); + data.append(FP16x16 { mag: 127676, sign: true }); + data.append(FP16x16 { mag: 22293, sign: true }); + data.append(FP16x16 { mag: 1758, sign: false }); + data.append(FP16x16 { mag: 56726, sign: false }); + data.append(FP16x16 { mag: 133433, sign: true }); + data.append(FP16x16 { mag: 9554, sign: true }); + data.append(FP16x16 { mag: 74809, sign: true }); + data.append(FP16x16 { mag: 17296, sign: true }); + data.append(FP16x16 { mag: 33106, sign: false }); + data.append(FP16x16 { mag: 57029, sign: true }); + data.append(FP16x16 { mag: 67553, sign: false }); + data.append(FP16x16 { mag: 65247, sign: false }); + data.append(FP16x16 { mag: 33811, sign: false }); + data.append(FP16x16 { mag: 37820, sign: false }); + data.append(FP16x16 { mag: 8349, sign: false }); + data.append(FP16x16 { mag: 82711, sign: true }); + data.append(FP16x16 { mag: 6355, sign: true }); + data.append(FP16x16 { mag: 51759, sign: true }); + data.append(FP16x16 { mag: 90465, sign: false }); + data.append(FP16x16 { mag: 63747, sign: false }); + data.append(FP16x16 { mag: 51827, sign: false }); + data.append(FP16x16 { mag: 2074, sign: true }); + data.append(FP16x16 { mag: 7986, sign: true }); + data.append(FP16x16 { mag: 127840, sign: true }); + data.append(FP16x16 { mag: 9095, sign: true }); + data.append(FP16x16 { mag: 39166, sign: true }); + data.append(FP16x16 { mag: 4900, sign: false }); + data.append(FP16x16 { mag: 85400, sign: false }); + data.append(FP16x16 { mag: 20179, sign: true }); + data.append(FP16x16 { mag: 102313, sign: false }); + data.append(FP16x16 { mag: 30560, sign: true }); + data.append(FP16x16 { mag: 23998, sign: true }); + data.append(FP16x16 { mag: 18546, sign: false }); + data.append(FP16x16 { mag: 50624, sign: false }); + data.append(FP16x16 { mag: 28484, sign: false }); + data.append(FP16x16 { mag: 106711, sign: false }); + data.append(FP16x16 { mag: 145610, sign: true }); + data.append(FP16x16 { mag: 45143, sign: true }); + data.append(FP16x16 { mag: 49679, sign: true }); + data.append(FP16x16 { mag: 43769, sign: true }); + data.append(FP16x16 { mag: 70976, sign: false }); + data.append(FP16x16 { mag: 97200, sign: false }); + data.append(FP16x16 { mag: 51404, sign: true }); + data.append(FP16x16 { mag: 5413, sign: false }); + data.append(FP16x16 { mag: 16219, sign: true }); + data.append(FP16x16 { mag: 68769, sign: false }); + data.append(FP16x16 { mag: 14687, sign: true }); + data.append(FP16x16 { mag: 24476, sign: false }); + data.append(FP16x16 { mag: 50297, sign: true }); + data.append(FP16x16 { mag: 21305, sign: true }); + data.append(FP16x16 { mag: 63960, sign: false }); + data.append(FP16x16 { mag: 60809, sign: false }); + data.append(FP16x16 { mag: 28337, sign: true }); + data.append(FP16x16 { mag: 14402, sign: true }); + data.append(FP16x16 { mag: 118874, sign: true }); + data.append(FP16x16 { mag: 76081, sign: false }); + data.append(FP16x16 { mag: 72871, sign: false }); + data.append(FP16x16 { mag: 25750, sign: true }); + data.append(FP16x16 { mag: 9294, sign: true }); + data.append(FP16x16 { mag: 113450, sign: false }); + data.append(FP16x16 { mag: 11901, sign: false }); + data.append(FP16x16 { mag: 130175, sign: true }); + data.append(FP16x16 { mag: 78751, sign: false }); + data.append(FP16x16 { mag: 37596, sign: false }); + data.append(FP16x16 { mag: 15550, sign: true }); + data.append(FP16x16 { mag: 88187, sign: false }); + data.append(FP16x16 { mag: 39156, sign: false }); + data.append(FP16x16 { mag: 105270, sign: false }); + data.append(FP16x16 { mag: 17054, sign: false }); + data.append(FP16x16 { mag: 58134, sign: false }); + data.append(FP16x16 { mag: 81353, sign: false }); + data.append(FP16x16 { mag: 54805, sign: true }); + data.append(FP16x16 { mag: 17538, sign: false }); + data.append(FP16x16 { mag: 78980, sign: false }); + data.append(FP16x16 { mag: 198738, sign: false }); + data.append(FP16x16 { mag: 19403, sign: true }); + data.append(FP16x16 { mag: 34265, sign: false }); + data.append(FP16x16 { mag: 11007, sign: false }); + data.append(FP16x16 { mag: 28307, sign: false }); + data.append(FP16x16 { mag: 119920, sign: true }); + data.append(FP16x16 { mag: 26409, sign: false }); + data.append(FP16x16 { mag: 23076, sign: false }); + data.append(FP16x16 { mag: 34211, sign: false }); + data.append(FP16x16 { mag: 361, sign: true }); + data.append(FP16x16 { mag: 73745, sign: true }); + data.append(FP16x16 { mag: 7717, sign: true }); + data.append(FP16x16 { mag: 12468, sign: false }); + data.append(FP16x16 { mag: 25976, sign: false }); + data.append(FP16x16 { mag: 107153, sign: true }); + data.append(FP16x16 { mag: 68883, sign: false }); + data.append(FP16x16 { mag: 13933, sign: true }); + data.append(FP16x16 { mag: 79850, sign: false }); + data.append(FP16x16 { mag: 77337, sign: true }); + data.append(FP16x16 { mag: 27318, sign: true }); + data.append(FP16x16 { mag: 103120, sign: false }); + data.append(FP16x16 { mag: 12693, sign: true }); + data.append(FP16x16 { mag: 63090, sign: false }); + data.append(FP16x16 { mag: 45507, sign: false }); + data.append(FP16x16 { mag: 52553, sign: true }); + data.append(FP16x16 { mag: 70902, sign: false }); + data.append(FP16x16 { mag: 111650, sign: false }); + data.append(FP16x16 { mag: 23223, sign: false }); + data.append(FP16x16 { mag: 6272, sign: true }); + data.append(FP16x16 { mag: 7037, sign: false }); + data.append(FP16x16 { mag: 74819, sign: true }); + data.append(FP16x16 { mag: 68730, sign: false }); + data.append(FP16x16 { mag: 90482, sign: false }); + data.append(FP16x16 { mag: 16560, sign: true }); + data.append(FP16x16 { mag: 117248, sign: false }); + data.append(FP16x16 { mag: 80344, sign: false }); + data.append(FP16x16 { mag: 73022, sign: true }); + data.append(FP16x16 { mag: 33531, sign: false }); + data.append(FP16x16 { mag: 180033, sign: false }); + data.append(FP16x16 { mag: 46630, sign: true }); + data.append(FP16x16 { mag: 119335, sign: true }); + data.append(FP16x16 { mag: 20067, sign: true }); + data.append(FP16x16 { mag: 36506, sign: false }); + data.append(FP16x16 { mag: 24262, sign: true }); + data.append(FP16x16 { mag: 715, sign: true }); + data.append(FP16x16 { mag: 101984, sign: false }); + data.append(FP16x16 { mag: 22353, sign: false }); + data.append(FP16x16 { mag: 50986, sign: false }); + data.append(FP16x16 { mag: 67113, sign: false }); + data.append(FP16x16 { mag: 55868, sign: false }); + data.append(FP16x16 { mag: 100422, sign: true }); + data.append(FP16x16 { mag: 53193, sign: false }); + data.append(FP16x16 { mag: 51699, sign: false }); + data.append(FP16x16 { mag: 47894, sign: true }); + data.append(FP16x16 { mag: 8082, sign: true }); + data.append(FP16x16 { mag: 32263, sign: true }); + data.append(FP16x16 { mag: 27212, sign: true }); + data.append(FP16x16 { mag: 7226, sign: true }); + data.append(FP16x16 { mag: 30746, sign: false }); + data.append(FP16x16 { mag: 12011, sign: false }); + data.append(FP16x16 { mag: 13020, sign: false }); + data.append(FP16x16 { mag: 7085, sign: false }); + data.append(FP16x16 { mag: 18086, sign: true }); + data.append(FP16x16 { mag: 30286, sign: false }); + data.append(FP16x16 { mag: 20925, sign: true }); + data.append(FP16x16 { mag: 87195, sign: true }); + data.append(FP16x16 { mag: 42790, sign: false }); + data.append(FP16x16 { mag: 8451, sign: false }); + data.append(FP16x16 { mag: 91114, sign: true }); + data.append(FP16x16 { mag: 32647, sign: true }); + data.append(FP16x16 { mag: 64992, sign: true }); + data.append(FP16x16 { mag: 74108, sign: false }); + data.append(FP16x16 { mag: 15790, sign: true }); + data.append(FP16x16 { mag: 80038, sign: true }); + data.append(FP16x16 { mag: 34209, sign: true }); + data.append(FP16x16 { mag: 7672, sign: false }); + data.append(FP16x16 { mag: 27429, sign: false }); + data.append(FP16x16 { mag: 57827, sign: false }); + data.append(FP16x16 { mag: 2154, sign: true }); + data.append(FP16x16 { mag: 44183, sign: false }); + data.append(FP16x16 { mag: 60736, sign: false }); + data.append(FP16x16 { mag: 62972, sign: false }); + data.append(FP16x16 { mag: 10, sign: true }); + data.append(FP16x16 { mag: 30567, sign: true }); + data.append(FP16x16 { mag: 58169, sign: false }); + data.append(FP16x16 { mag: 113162, sign: true }); + data.append(FP16x16 { mag: 47093, sign: true }); + data.append(FP16x16 { mag: 11549, sign: false }); + data.append(FP16x16 { mag: 103543, sign: true }); + data.append(FP16x16 { mag: 19276, sign: true }); + data.append(FP16x16 { mag: 43892, sign: false }); + data.append(FP16x16 { mag: 28123, sign: false }); + data.append(FP16x16 { mag: 94676, sign: false }); + data.append(FP16x16 { mag: 85149, sign: true }); + data.append(FP16x16 { mag: 26867, sign: true }); + data.append(FP16x16 { mag: 112263, sign: true }); + data.append(FP16x16 { mag: 24373, sign: true }); + data.append(FP16x16 { mag: 34755, sign: false }); + data.append(FP16x16 { mag: 9384, sign: false }); + data.append(FP16x16 { mag: 49546, sign: true }); + data.append(FP16x16 { mag: 79711, sign: false }); + data.append(FP16x16 { mag: 27758, sign: true }); + data.append(FP16x16 { mag: 8378, sign: true }); + data.append(FP16x16 { mag: 40580, sign: false }); + data.append(FP16x16 { mag: 112339, sign: true }); + data.append(FP16x16 { mag: 10388, sign: true }); + data.append(FP16x16 { mag: 37733, sign: false }); + data.append(FP16x16 { mag: 105370, sign: false }); + data.append(FP16x16 { mag: 38217, sign: true }); + data.append(FP16x16 { mag: 52641, sign: true }); + data.append(FP16x16 { mag: 33139, sign: true }); + data.append(FP16x16 { mag: 8245, sign: true }); + data.append(FP16x16 { mag: 2784, sign: false }); + data.append(FP16x16 { mag: 113567, sign: false }); + data.append(FP16x16 { mag: 104763, sign: false }); + data.append(FP16x16 { mag: 4564, sign: false }); + data.append(FP16x16 { mag: 15700, sign: false }); + data.append(FP16x16 { mag: 43737, sign: false }); + data.append(FP16x16 { mag: 34793, sign: false }); + data.append(FP16x16 { mag: 57257, sign: true }); + data.append(FP16x16 { mag: 132151, sign: true }); + data.append(FP16x16 { mag: 45201, sign: true }); + data.append(FP16x16 { mag: 98318, sign: false }); + data.append(FP16x16 { mag: 106443, sign: true }); + data.append(FP16x16 { mag: 34909, sign: true }); + data.append(FP16x16 { mag: 32335, sign: true }); + data.append(FP16x16 { mag: 40192, sign: true }); + data.append(FP16x16 { mag: 22349, sign: false }); + data.append(FP16x16 { mag: 49484, sign: true }); + data.append(FP16x16 { mag: 10589, sign: true }); + data.append(FP16x16 { mag: 7125, sign: true }); + data.append(FP16x16 { mag: 104325, sign: true }); + data.append(FP16x16 { mag: 41763, sign: false }); + data.append(FP16x16 { mag: 38116, sign: false }); + data.append(FP16x16 { mag: 84951, sign: true }); + data.append(FP16x16 { mag: 52756, sign: false }); + data.append(FP16x16 { mag: 60485, sign: false }); + data.append(FP16x16 { mag: 90227, sign: false }); + data.append(FP16x16 { mag: 12616, sign: false }); + data.append(FP16x16 { mag: 94036, sign: false }); + data.append(FP16x16 { mag: 60362, sign: true }); + data.append(FP16x16 { mag: 6930, sign: false }); + data.append(FP16x16 { mag: 42650, sign: true }); + data.append(FP16x16 { mag: 57435, sign: false }); + data.append(FP16x16 { mag: 8276, sign: true }); + data.append(FP16x16 { mag: 36396, sign: true }); + data.append(FP16x16 { mag: 157288, sign: false }); + data.append(FP16x16 { mag: 3531, sign: true }); + data.append(FP16x16 { mag: 21519, sign: true }); + data.append(FP16x16 { mag: 1592, sign: true }); + data.append(FP16x16 { mag: 41150, sign: true }); + data.append(FP16x16 { mag: 73234, sign: false }); + data.append(FP16x16 { mag: 66372, sign: false }); + data.append(FP16x16 { mag: 58146, sign: false }); + data.append(FP16x16 { mag: 44555, sign: false }); + data.append(FP16x16 { mag: 35804, sign: true }); + data.append(FP16x16 { mag: 2150, sign: false }); + data.append(FP16x16 { mag: 55208, sign: false }); + data.append(FP16x16 { mag: 41980, sign: true }); + data.append(FP16x16 { mag: 66322, sign: true }); + data.append(FP16x16 { mag: 26460, sign: false }); + data.append(FP16x16 { mag: 77380, sign: false }); + data.append(FP16x16 { mag: 125207, sign: true }); + data.append(FP16x16 { mag: 30086, sign: true }); + data.append(FP16x16 { mag: 15517, sign: false }); + data.append(FP16x16 { mag: 118773, sign: false }); + data.append(FP16x16 { mag: 99314, sign: true }); + data.append(FP16x16 { mag: 11067, sign: true }); + data.append(FP16x16 { mag: 57066, sign: false }); + data.append(FP16x16 { mag: 5311, sign: false }); + data.append(FP16x16 { mag: 69970, sign: false }); + data.append(FP16x16 { mag: 93820, sign: false }); + data.append(FP16x16 { mag: 1079, sign: true }); + data.append(FP16x16 { mag: 9773, sign: false }); + data.append(FP16x16 { mag: 21840, sign: false }); + data.append(FP16x16 { mag: 2160, sign: true }); + data.append(FP16x16 { mag: 3271, sign: true }); + data.append(FP16x16 { mag: 99495, sign: true }); + data.append(FP16x16 { mag: 77215, sign: true }); + data.append(FP16x16 { mag: 85212, sign: false }); + data.append(FP16x16 { mag: 61944, sign: false }); + data.append(FP16x16 { mag: 2082, sign: false }); + data.append(FP16x16 { mag: 107676, sign: true }); + data.append(FP16x16 { mag: 40702, sign: false }); + data.append(FP16x16 { mag: 24223, sign: false }); + data.append(FP16x16 { mag: 82086, sign: true }); + data.append(FP16x16 { mag: 1236, sign: false }); + data.append(FP16x16 { mag: 65294, sign: false }); + data.append(FP16x16 { mag: 18639, sign: true }); + data.append(FP16x16 { mag: 81036, sign: false }); + data.append(FP16x16 { mag: 1472, sign: false }); + data.append(FP16x16 { mag: 26208, sign: true }); + data.append(FP16x16 { mag: 7969, sign: false }); + data.append(FP16x16 { mag: 94838, sign: false }); + data.append(FP16x16 { mag: 48398, sign: true }); + data.append(FP16x16 { mag: 127750, sign: false }); + data.append(FP16x16 { mag: 14694, sign: false }); + data.append(FP16x16 { mag: 98356, sign: false }); + data.append(FP16x16 { mag: 16239, sign: false }); + data.append(FP16x16 { mag: 20463, sign: false }); + data.append(FP16x16 { mag: 125215, sign: false }); + data.append(FP16x16 { mag: 8089, sign: true }); + data.append(FP16x16 { mag: 114956, sign: true }); + data.append(FP16x16 { mag: 56287, sign: false }); + data.append(FP16x16 { mag: 11168, sign: true }); + data.append(FP16x16 { mag: 85072, sign: true }); + data.append(FP16x16 { mag: 53241, sign: true }); + data.append(FP16x16 { mag: 47712, sign: false }); + data.append(FP16x16 { mag: 27321, sign: false }); + data.append(FP16x16 { mag: 2647, sign: false }); + data.append(FP16x16 { mag: 64711, sign: true }); + data.append(FP16x16 { mag: 8104, sign: true }); + data.append(FP16x16 { mag: 5213, sign: false }); + data.append(FP16x16 { mag: 87049, sign: true }); + data.append(FP16x16 { mag: 41663, sign: false }); + data.append(FP16x16 { mag: 26688, sign: true }); + data.append(FP16x16 { mag: 2385, sign: true }); + data.append(FP16x16 { mag: 8540, sign: true }); + data.append(FP16x16 { mag: 7311, sign: true }); + data.append(FP16x16 { mag: 2750, sign: true }); + data.append(FP16x16 { mag: 125527, sign: true }); + data.append(FP16x16 { mag: 51192, sign: false }); + data.append(FP16x16 { mag: 102959, sign: false }); + data.append(FP16x16 { mag: 94206, sign: false }); + data.append(FP16x16 { mag: 22114, sign: false }); + data.append(FP16x16 { mag: 56998, sign: false }); + data.append(FP16x16 { mag: 118819, sign: true }); + data.append(FP16x16 { mag: 87037, sign: false }); + data.append(FP16x16 { mag: 45884, sign: false }); + data.append(FP16x16 { mag: 108580, sign: false }); + data.append(FP16x16 { mag: 175625, sign: true }); + data.append(FP16x16 { mag: 42256, sign: false }); + data.append(FP16x16 { mag: 39523, sign: false }); + data.append(FP16x16 { mag: 29431, sign: false }); + data.append(FP16x16 { mag: 17315, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/maxpool_4d_dilations/output_0.cairo b/tests/nodes/maxpool_4d_dilations/output_0.cairo new file mode 100644 index 000000000..9c0185e89 --- /dev/null +++ b/tests/nodes/maxpool_4d_dilations/output_0.cairo @@ -0,0 +1,65 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + shape.append(2); + shape.append(2); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 49149, sign: false }); + data.append(FP16x16 { mag: 93230, sign: false }); + data.append(FP16x16 { mag: 131524, sign: false }); + data.append(FP16x16 { mag: 172442, sign: false }); + data.append(FP16x16 { mag: 171591, sign: false }); + data.append(FP16x16 { mag: 124609, sign: false }); + data.append(FP16x16 { mag: 107561, sign: false }); + data.append(FP16x16 { mag: 193719, sign: false }); + data.append(FP16x16 { mag: 97602, sign: false }); + data.append(FP16x16 { mag: 58097, sign: false }); + data.append(FP16x16 { mag: 117822, sign: false }); + data.append(FP16x16 { mag: 79390, sign: false }); + data.append(FP16x16 { mag: 93738, sign: false }); + data.append(FP16x16 { mag: 114134, sign: false }); + data.append(FP16x16 { mag: 84330, sign: false }); + data.append(FP16x16 { mag: 81192, sign: false }); + data.append(FP16x16 { mag: 93421, sign: false }); + data.append(FP16x16 { mag: 80140, sign: false }); + data.append(FP16x16 { mag: 88854, sign: false }); + data.append(FP16x16 { mag: 93610, sign: false }); + data.append(FP16x16 { mag: 57375, sign: false }); + data.append(FP16x16 { mag: 98163, sign: false }); + data.append(FP16x16 { mag: 116267, sign: false }); + data.append(FP16x16 { mag: 125650, sign: false }); + data.append(FP16x16 { mag: 136686, sign: false }); + data.append(FP16x16 { mag: 123236, sign: false }); + data.append(FP16x16 { mag: 174668, sign: false }); + data.append(FP16x16 { mag: 65025, sign: false }); + data.append(FP16x16 { mag: 68769, sign: false }); + data.append(FP16x16 { mag: 131564, sign: false }); + data.append(FP16x16 { mag: 97200, sign: false }); + data.append(FP16x16 { mag: 90465, sign: false }); + data.append(FP16x16 { mag: 105370, sign: false }); + data.append(FP16x16 { mag: 103120, sign: false }); + data.append(FP16x16 { mag: 113567, sign: false }); + data.append(FP16x16 { mag: 157288, sign: false }); + data.append(FP16x16 { mag: 118773, sign: false }); + data.append(FP16x16 { mag: 180033, sign: false }); + data.append(FP16x16 { mag: 90227, sign: false }); + data.append(FP16x16 { mag: 198738, sign: false }); + data.append(FP16x16 { mag: 101984, sign: false }); + data.append(FP16x16 { mag: 61944, sign: false }); + data.append(FP16x16 { mag: 62972, sign: false }); + data.append(FP16x16 { mag: 67113, sign: false }); + data.append(FP16x16 { mag: 127750, sign: false }); + data.append(FP16x16 { mag: 102959, sign: false }); + data.append(FP16x16 { mag: 94838, sign: false }); + data.append(FP16x16 { mag: 125215, sign: false }); + TensorTrait::new(shape.span(), data.span()) +}