diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c1fc2cfd6..2cbaddbe3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -9,5 +9,5 @@ jobs: - uses: actions/checkout@v3 - uses: software-mansion/setup-scarb@v1 with: - scarb-version: "2.5.3" + scarb-version: "2.6.4" - run: scarb test --workspace && scarb fmt --workspace \ No newline at end of file diff --git a/.tool-versions b/.tool-versions index ebe254233..e1290964b 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -scarb 2.5.3 +scarb 2.6.4 diff --git a/Scarb.toml b/Scarb.toml index f05fa6649..27ead9f84 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,7 +1,7 @@ [package] name = "orion" -version = "0.2.4" -cairo-version = "2.5.3" +version = "0.2.5" +cairo-version = "2.6.3" edition = "2023_10" description = "ONNX Runtime in Cairo for verifiable ML inference using STARK" homepage = "https://github.com/gizatechxyz/orion" diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index fe2995096..51c1d6d06 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -55,7 +55,8 @@ use orion::operators::tensor::TensorTrait; | [`tensor.min_in_tensor`](tensor.min\_in\_tensor.md) | Returns the minimum value in the tensor. | | [`tensor.min`](tensor.min.md) | Returns the minimum value in the tensor. | | [`tensor.max`](tensor.max.md) | Returns the maximum value in the tensor. | -| [`tensor.reduce_sum`](tensor.reduce\_sum.md) | Reduces a tensor by summing its elements along a specified axis. | +| [`tensor.reduce_sum`](tensor.reduce\_sum.md) | Computes the sum of the input tensor's elements along the provided axes. | +| [`tensor.reduce_sum_single_axis`](tensor.reduce\_sum\_single\_axis.md) | Reduces a tensor by summing its elements along a specified axis. | | [`tensor.reduce_prod`](tensor.reduce\_prod.md) | Reduces a tensor to its products along specified axis. | | [`tensor.argmax`](tensor.argmax.md) | Returns the index of the maximum value along the specified axis. | | [`tensor.argmin`](tensor.argmin.md) | Returns the index of the minimum value along the specified axis. | diff --git a/docs/framework/operators/tensor/tensor.reduce_sum.md b/docs/framework/operators/tensor/tensor.reduce_sum.md index 3aa77d2ce..52b49d137 100644 --- a/docs/framework/operators/tensor/tensor.reduce_sum.md +++ b/docs/framework/operators/tensor/tensor.reduce_sum.md @@ -4,13 +4,14 @@ fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor; ``` -Reduces a tensor by summing its elements along a specified axis. +Computes the sum of the input tensor's elements along the provided axes ## Args * `self`(`@Tensor`) - The input tensor. -* `axis`(`usize`) - The dimension to reduce. -* `keepdims`(`bool`) - If true, retains reduced dimensions with length 1. +* `axes`(`Option>`) - Optional input list of integers, along which to reduce. +* `keepdims`(`Option`) - If true, retains reduced dimensions with length 1. +* `noop_with_empty_axes`(`Option`) - Defines behavior if 'axes' is empty. Default behavior with 'false' is to reduce all axes. When axes is empty and this attribute is set to true, input tensor will not be reduced,and the output tensor would be equivalent to input tensor. ## Panics @@ -29,11 +30,11 @@ use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; fn reduce_sum_example() -> Tensor { let tensor = TensorTrait::::new( - shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), + shape: array![3, 2, 2].span(), data: array![1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12].span(), ); // We can call `reduce_sum` function as follows. - return tensor.reduce_sum(axis: 0, keepdims: false); + return tensor.reduce_sum(Option::Some(array![1].span()), Option::Some(false), Option::None); } ->>> [[4,6],[8,10]] +>>> [[4, 6] [12, 14] [20, 22]] ``` diff --git a/docs/framework/operators/tensor/tensor.reduce_sum_single_axis.md b/docs/framework/operators/tensor/tensor.reduce_sum_single_axis.md new file mode 100644 index 000000000..3eab60dfa --- /dev/null +++ b/docs/framework/operators/tensor/tensor.reduce_sum_single_axis.md @@ -0,0 +1,39 @@ +## tensor.reduce_sum_single_axis + +```rust + fn reduce_sum_single_axis(self: @Tensor, axis: usize, keepdims: bool) -> Tensor; +``` + +Reduces a tensor by summing its elements along a specified axis. + +## Args + +* `self`(`@Tensor`) - The input tensor. +* `axis`(`usize`) - The dimension to reduce. +* `keepdims`(`bool`) - If true, retains reduced dimensions with length 1. + +## Panics + +* Panics if axis is not in the range of the input tensor's dimensions. + +## Returns + +A new `Tensor` instance with the specified axis reduced by summing its elements. + +## Examples + +```rust +use core::array::{ArrayTrait, SpanTrait}; + +use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + +fn reduce_sum_single_axis_example() -> Tensor { + let tensor = TensorTrait::::new( + shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), + ); + + // We can call `reduce_sum_single_axis` function as follows. + return tensor.reduce_sum_single_axis(axis: 0, keepdims: false); +} +>>> [[4,6],[8,10]] +``` diff --git a/nodegen/file_manager.py b/nodegen/file_manager.py index 203b6b333..babe26399 100644 --- a/nodegen/file_manager.py +++ b/nodegen/file_manager.py @@ -91,25 +91,36 @@ def base_template( This method generates a list of strings that form the template of a Cairo test function, including module imports, function definition, and assertions. """ - return [ + template = [ *[f"mod input_{i};" for i in range(arg_cnt)], *[f"mod output_{i};" for i in range(out_cnt)], - *[""], - *[""], + "", + "", *[f"use {ref};" for ref in refs], - *[""], - *["#[test]"], - *["#[available_gas(2000000000)]"], - *[f"fn test_{name}()" + " {"], + "", + "#[test]", + "#[available_gas(2000000000)]", + f"fn test_{name}()" + " {", *[f" let input_{i} = input_{i}::input_{i}();" for i in range(arg_cnt)], *[f" let z_{i} = output_{i}::output_{i}();" for i in range(out_cnt)], - *[""], - *[f" let ({', '.join(f'y_{i}' for i in range(out_cnt))}) = {func_sig};"], - *[""], - *[f" assert_eq(y_{i}, z_{i});" for i in range(out_cnt)], - *["}"], + "" ] + # Handling conditional function signature based on the number of outputs + if out_cnt > 1: + template.append(f" let ({', '.join(f'y_{i}' for i in range(out_cnt))}) = {func_sig};") + else: + template.append(f" let y_0 = {func_sig};") + + # Continue appending to the template + template.extend([ + "", + *[f" assert_eq(y_{i}, z_{i});" for i in range(out_cnt)], + "}" + ]) + + return template + @classmethod def sequence_template(cls, name: str, arg_cnt: int, refs: list[str], func_sig: str) -> list[str]: """ diff --git a/nodegen/node/reduce_sum.py b/nodegen/node/reduce_sum.py index 111724001..53b2ed824 100644 --- a/nodegen/node/reduce_sum.py +++ b/nodegen/node/reduce_sum.py @@ -4,285 +4,63 @@ class Reduce_sum(RunAll): - @staticmethod - def reduce_sum_u32(): - def reduce_sum_1D(): - x = np.array([0, 1, 2,]).astype(np.uint32) - y = np.array([3]).astype(np.uint32) - - x = Tensor(Dtype.U32, x.shape, x.flatten()) - y = Tensor(Dtype.U32, y.shape, y.flatten()) - - name = "reduce_sum_u32_1D" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def reduce_sum_2D(): - def default(): - x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2) - y = np.array([2, 4]).astype(np.uint32) - - x = Tensor(Dtype.U32, x.shape, x.flatten()) - y = Tensor(Dtype.U32, y.shape, y.flatten()) - - name = "reduce_sum_u32_2D_default" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def keepdims(): - x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2) - y = np.array([2, 4]).astype(np.uint32).reshape(1, 2) - - x = Tensor(Dtype.U32, x.shape, x.flatten()) - y = Tensor(Dtype.U32, y.shape, y.flatten()) - - name = "reduce_sum_u32_2D_keepdims" - make_test( - [x], y, "input_0.reduce_sum(0, true)", name) - - def axis_1(): - x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2) - y = np.array([1, 5]).astype(np.uint32) - - x = Tensor(Dtype.U32, x.shape, x.flatten()) - y = Tensor(Dtype.U32, y.shape, y.flatten()) - - name = "reduce_sum_u32_2D_axis_1" - make_test( - [x], y, "input_0.reduce_sum(1, false)", name) - - default() - keepdims() - axis_1() - reduce_sum_1D() - reduce_sum_2D() @staticmethod - def reduce_sum_i32(): - def reduce_sum_1D(): - x = np.array([0, 1, 2,]).astype(np.int32) - y = np.array([3]).astype(np.int32) + def reduce_sum_no_keep_dims(): + axes = np.array([1], dtype=np.uint32) + keepdims = 0 - x = Tensor(Dtype.I32, x.shape, x.flatten()) - y = Tensor(Dtype.I32, y.shape, y.flatten()) + x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [ + [9, 10], [11, 12]]]).astype(np.uint32) + y = np.sum(x, axis=tuple(axes.tolist()), keepdims=keepdims == 1) - name = "reduce_sum_i32_1D" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) - def reduce_sum_2D(): - def default(): - x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2) - y = np.array([2, 4]).astype(np.int32) - - x = Tensor(Dtype.I32, x.shape, x.flatten()) - y = Tensor(Dtype.I32, y.shape, y.flatten()) - - name = "reduce_sum_i32_2D_default" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def keepdims(): - x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2) - y = np.array([2, 4]).astype(np.int32).reshape(1, 2) - - x = Tensor(Dtype.I32, x.shape, x.flatten()) - y = Tensor(Dtype.I32, y.shape, y.flatten()) - - name = "reduce_sum_i32_2D_keepdims" - make_test( - [x], y, "input_0.reduce_sum(0, true)", name) - - def axis_1(): - x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2) - y = np.array([1, 5]).astype(np.int32) - - x = Tensor(Dtype.I32, x.shape, x.flatten()) - y = Tensor(Dtype.I32, y.shape, y.flatten()) - - name = "reduce_sum_i32_2D_axis_1" - make_test( - [x], y, "input_0.reduce_sum(1, false)", name) - - default() - keepdims() - axis_1() - reduce_sum_1D() - reduce_sum_2D() + name = "reduce_sum_no_keep_dims" + make_test( + [x], y, "input_0.reduce_sum(Option::Some(array![1].span()), Option::Some(false), Option::None)", name) @staticmethod - def reduce_sum_i8(): - def reduce_sum_1D(): - x = np.array([0, 1, 2,]).astype(np.int8) - y = np.array([3]).astype(np.int8) - - x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) - y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) - - name = "reduce_sum_i8_1D" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def reduce_sum_2D(): - def default(): - x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2) - y = np.array([2, 4]).astype(np.int8) - - x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) - y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) + def reduce_sum_keep_dims(): + axes = np.array([1], dtype=np.uint32) + keepdims = 1 - name = "reduce_sum_i8_2D_default" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) + x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [ + [9, 10], [11, 12]]]).astype(np.uint32) + y = np.sum(x, axis=tuple(axes.tolist()), keepdims=keepdims == 1) - def keepdims(): - x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2) - y = np.array([2, 4]).astype(np.int8).reshape(1, 2) + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) - x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) - y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) - - name = "reduce_sum_i8_2D_keepdims" - make_test( - [x], y, "input_0.reduce_sum(0, true)", name) - - def axis_1(): - x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2) - y = np.array([1, 5]).astype(np.int8) - - x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) - y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) - - name = "reduce_sum_i8_2D_axis_1" - make_test( - [x], y, "input_0.reduce_sum(1, false)", name) - - default() - keepdims() - axis_1() - reduce_sum_1D() - reduce_sum_2D() + name = "reduce_sum_keep_dims" + make_test( + [x], y, "input_0.reduce_sum(Option::Some(array![1].span()), Option::Some(true), Option::None)", name) @staticmethod - def reduce_sum_fp8x23(): - def reduce_sum_1D(): - x = np.array([0, 1, 2,]).astype(np.int64) - y = np.array([3]).astype(np.int64) - - x = Tensor(Dtype.FP8x23, x.shape, to_fp( - x.flatten(), FixedImpl.FP8x23)) - y = Tensor(Dtype.FP8x23, y.shape, to_fp( - y.flatten(), FixedImpl.FP8x23)) - - name = "reduce_sum_fp8x23_1D" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def reduce_sum_2D(): - def default(): - x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) - y = np.array([2, 4]).astype(np.int64) - - x = Tensor(Dtype.FP8x23, x.shape, to_fp( - x.flatten(), FixedImpl.FP8x23)) - y = Tensor(Dtype.FP8x23, y.shape, to_fp( - y.flatten(), FixedImpl.FP8x23)) - - name = "reduce_sum_fp8x23_2D_default" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def keepdims(): - x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) - y = np.array([2, 4]).astype(np.int64).reshape(1, 2) - - x = Tensor(Dtype.FP8x23, x.shape, to_fp( - x.flatten(), FixedImpl.FP8x23)) - y = Tensor(Dtype.FP8x23, y.shape, to_fp( - y.flatten(), FixedImpl.FP8x23)) - - name = "reduce_sum_fp8x23_2D_keepdims" - make_test( - [x], y, "input_0.reduce_sum(0, true)", name) + def reduce_sum_default_axes_keepdims(): + keepdims = 1 - def axis_1(): - x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) - y = np.array([1, 5]).astype(np.int64) + x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [ + [9, 10], [11, 12]]]).astype(np.uint32) + y = np.sum(x, axis=None, keepdims=keepdims == 1) - x = Tensor(Dtype.FP8x23, x.shape, to_fp( - x.flatten(), FixedImpl.FP8x23)) - y = Tensor(Dtype.FP8x23, y.shape, to_fp( - y.flatten(), FixedImpl.FP8x23)) + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) - name = "reduce_sum_fp8x23_2D_axis_1" - make_test( - [x], y, "input_0.reduce_sum(1, false)", name) - - default() - keepdims() - axis_1() - - reduce_sum_1D() - reduce_sum_2D() + name = "reduce_sum_default_axes_keepdims" + make_test( + [x], y, "input_0.reduce_sum(Option::Some(array![].span()), Option::Some(true), Option::None)", name) @staticmethod - def reduce_sum_fp16x16(): - def reduce_sum_1D(): - x = np.array([0, 1, 2,]).astype(np.int64) - y = np.array([3]).astype(np.int64) - - 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 = "reduce_sum_fp16x16_1D" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def reduce_sum_2D(): - def default(): - x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) - y = np.array([2, 4]).astype(np.int64) - - 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 = "reduce_sum_fp16x16_2D_default" - make_test( - [x], y, "input_0.reduce_sum(0, false)", name) - - def keepdims(): - x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) - y = np.array([2, 4]).astype(np.int64).reshape(1, 2) - - 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 = "reduce_sum_fp16x16_2D_keepdims" - make_test( - [x], y, "input_0.reduce_sum(0, true)", name) - - def axis_1(): - x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) - y = np.array([1, 5]).astype(np.int64) - - 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 = "reduce_sum_fp16x16_2D_axis_1" - make_test( - [x], y, "input_0.reduce_sum(1, false)", name) + def reduce_sum_empty_axes_input_noop(): + x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [ + [9, 10], [11, 12]]]).astype(np.uint32) + y = np.array(x) - default() - keepdims() - axis_1() + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) - reduce_sum_1D() - reduce_sum_2D() + name = "reduce_sum_empty_axes_input_noop" + make_test( + [x], y, "input_0.reduce_sum(Option::None, Option::Some(true), Option::Some(true))", name) diff --git a/nodegen/node/reduce_sum_single_axis.py b/nodegen/node/reduce_sum_single_axis.py new file mode 100644 index 000000000..91197c45d --- /dev/null +++ b/nodegen/node/reduce_sum_single_axis.py @@ -0,0 +1,288 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl + + +class Reduce_sum_single_axis(RunAll): + @staticmethod + def reduce_sum_single_axis_u32(): + def reduce_sum_single_axis_1D(): + x = np.array([0, 1, 2,]).astype(np.uint32) + y = np.array([3]).astype(np.uint32) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_u32_1D" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def reduce_sum_single_axis_2D(): + def default(): + x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2) + y = np.array([2, 4]).astype(np.uint32) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_u32_2D_default" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def keepdims(): + x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2) + y = np.array([2, 4]).astype(np.uint32).reshape(1, 2) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_u32_2D_keepdims" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, true)", name) + + def axis_1(): + x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2) + y = np.array([1, 5]).astype(np.uint32) + + x = Tensor(Dtype.U32, x.shape, x.flatten()) + y = Tensor(Dtype.U32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_u32_2D_axis_1" + make_test( + [x], y, "input_0.reduce_sum_single_axis(1, false)", name) + + default() + keepdims() + axis_1() + reduce_sum_single_axis_1D() + reduce_sum_single_axis_2D() + + @staticmethod + def reduce_sum_single_axis_i32(): + def reduce_sum_single_axis_1D(): + x = np.array([0, 1, 2,]).astype(np.int32) + y = np.array([3]).astype(np.int32) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i32_1D" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def reduce_sum_single_axis_2D(): + def default(): + x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2) + y = np.array([2, 4]).astype(np.int32) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i32_2D_default" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def keepdims(): + x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2) + y = np.array([2, 4]).astype(np.int32).reshape(1, 2) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i32_2D_keepdims" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, true)", name) + + def axis_1(): + x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2) + y = np.array([1, 5]).astype(np.int32) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i32_2D_axis_1" + make_test( + [x], y, "input_0.reduce_sum_single_axis(1, false)", name) + + default() + keepdims() + axis_1() + reduce_sum_single_axis_1D() + reduce_sum_single_axis_2D() + + @staticmethod + def reduce_sum_single_axis_i8(): + def reduce_sum_single_axis_1D(): + x = np.array([0, 1, 2,]).astype(np.int8) + y = np.array([3]).astype(np.int8) + + x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i8_1D" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def reduce_sum_single_axis_2D(): + def default(): + x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2) + y = np.array([2, 4]).astype(np.int8) + + x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i8_2D_default" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def keepdims(): + x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2) + y = np.array([2, 4]).astype(np.int8).reshape(1, 2) + + x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i8_2D_keepdims" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, true)", name) + + def axis_1(): + x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2) + y = np.array([1, 5]).astype(np.int8) + + x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) + y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) + + name = "reduce_sum_single_axis_i8_2D_axis_1" + make_test( + [x], y, "input_0.reduce_sum_single_axis(1, false)", name) + + default() + keepdims() + axis_1() + reduce_sum_single_axis_1D() + reduce_sum_single_axis_2D() + + @staticmethod + def reduce_sum_single_axis_fp8x23(): + def reduce_sum_single_axis_1D(): + x = np.array([0, 1, 2,]).astype(np.int64) + y = np.array([3]).astype(np.int64) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + + name = "reduce_sum_single_axis_fp8x23_1D" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def reduce_sum_single_axis_2D(): + def default(): + x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) + y = np.array([2, 4]).astype(np.int64) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + + name = "reduce_sum_single_axis_fp8x23_2D_default" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def keepdims(): + x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) + y = np.array([2, 4]).astype(np.int64).reshape(1, 2) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + + name = "reduce_sum_single_axis_fp8x23_2D_keepdims" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, true)", name) + + def axis_1(): + x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) + y = np.array([1, 5]).astype(np.int64) + + x = Tensor(Dtype.FP8x23, x.shape, to_fp( + x.flatten(), FixedImpl.FP8x23)) + y = Tensor(Dtype.FP8x23, y.shape, to_fp( + y.flatten(), FixedImpl.FP8x23)) + + name = "reduce_sum_single_axis_fp8x23_2D_axis_1" + make_test( + [x], y, "input_0.reduce_sum_single_axis(1, false)", name) + + default() + keepdims() + axis_1() + + reduce_sum_single_axis_1D() + reduce_sum_single_axis_2D() + + @staticmethod + def reduce_sum_single_axis_fp16x16(): + def reduce_sum_single_axis_1D(): + x = np.array([0, 1, 2,]).astype(np.int64) + y = np.array([3]).astype(np.int64) + + 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 = "reduce_sum_single_axis_fp16x16_1D" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def reduce_sum_single_axis_2D(): + def default(): + x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) + y = np.array([2, 4]).astype(np.int64) + + 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 = "reduce_sum_single_axis_fp16x16_2D_default" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, false)", name) + + def keepdims(): + x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) + y = np.array([2, 4]).astype(np.int64).reshape(1, 2) + + 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 = "reduce_sum_single_axis_fp16x16_2D_keepdims" + make_test( + [x], y, "input_0.reduce_sum_single_axis(0, true)", name) + + def axis_1(): + x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) + y = np.array([1, 5]).astype(np.int64) + + 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 = "reduce_sum_single_axis_fp16x16_2D_axis_1" + make_test( + [x], y, "input_0.reduce_sum_single_axis(1, false)", name) + + default() + keepdims() + axis_1() + + reduce_sum_single_axis_1D() + reduce_sum_single_axis_2D() diff --git a/src/operators/nn/functional/logsoftmax.cairo b/src/operators/nn/functional/logsoftmax.cairo index fdf89c43d..33df374f0 100644 --- a/src/operators/nn/functional/logsoftmax.cairo +++ b/src/operators/nn/functional/logsoftmax.cairo @@ -10,7 +10,7 @@ fn logsoftmax< z: @Tensor, axis: usize ) -> Tensor { let exp_tensor = z.exp(); - let sum = exp_tensor.reduce_sum(axis, true); + let sum = exp_tensor.reduce_sum_single_axis(axis, true); let softmax = exp_tensor / sum; let logsoftmax = softmax.log(); @@ -38,7 +38,7 @@ fn logsoftmaxWide< z: @Tensor, axis: usize ) -> Tensor { let exp_tensor: Tensor = exp_upcast(*z); - let sum = exp_tensor.reduce_sum(axis, true); + let sum = exp_tensor.reduce_sum_single_axis(axis, true); let softmax = div_downcast(@exp_tensor, @sum); softmax.log() diff --git a/src/operators/nn/functional/softmax.cairo b/src/operators/nn/functional/softmax.cairo index 10602bde7..e8be7953d 100644 --- a/src/operators/nn/functional/softmax.cairo +++ b/src/operators/nn/functional/softmax.cairo @@ -13,7 +13,7 @@ fn softmax< z: @Tensor, axis: usize ) -> Tensor { let exp_tensor = z.exp(); - let sum = exp_tensor.reduce_sum(axis, true); + let sum = exp_tensor.reduce_sum_single_axis(axis, true); exp_tensor / sum } @@ -39,7 +39,7 @@ fn softmaxWide< z: @Tensor, axis: usize ) -> Tensor { let exp_tensor: Tensor = exp_upcast(*z); - let sum = exp_tensor.reduce_sum(axis, true); + let sum = exp_tensor.reduce_sum_single_axis(axis, true); div_downcast(@exp_tensor, @sum) } diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 0d21a4de3..c2b9668b1 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -53,7 +53,8 @@ impl TensorSerde, impl TDrop: Drop> of Serde { /// fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor; /// ``` /// + /// Computes the sum of the input tensor's elements along the provided axes + /// + /// ## Args + /// + /// * `self`(`@Tensor`) - The input tensor. + /// * `axes`(`Option>`) - Optional input list of integers, along which to reduce. + /// * `keepdims`(`Option`) - If true, retains reduced dimensions with length 1. + /// * `noop_with_empty_axes`(`Option`) - Defines behavior if 'axes' is empty. Default behavior with 'false' is to reduce all axes. When axes is empty and this attribute is set to true, input tensor will not be reduced,and the output tensor would be equivalent to input tensor. + /// + /// ## Panics + /// + /// * Panics if axis is not in the range of the input tensor's dimensions. + /// + /// ## Returns + /// + /// A new `Tensor` instance with the specified axis reduced by summing its elements. + /// + /// ## Examples + /// + /// ```rust + /// use core::array::{ArrayTrait, SpanTrait}; + /// + /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; + /// + /// fn reduce_sum_example() -> Tensor { + /// let tensor = TensorTrait::::new( + /// shape: array![3, 2, 2].span(), data: array![1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12].span(), + /// ); + /// + /// // We can call `reduce_sum` function as follows. + /// return tensor.reduce_sum(Option::Some(array![1].span()), Option::Some(false), Option::None); + /// } + /// >>> [[4, 6] [12, 14] [20, 22]] + /// ``` + /// + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor; + /// ## tensor.reduce_sum_single_axis + /// + /// ```rust + /// fn reduce_sum_single_axis(self: @Tensor, axis: usize, keepdims: bool) -> Tensor; + /// ``` + /// /// Reduces a tensor by summing its elements along a specified axis. /// /// ## Args @@ -664,18 +712,18 @@ trait TensorTrait { /// /// use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; /// - /// fn reduce_sum_example() -> Tensor { + /// fn reduce_sum_single_axis_example() -> Tensor { /// let tensor = TensorTrait::::new( /// shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(), /// ); /// - /// // We can call `reduce_sum` function as follows. - /// return tensor.reduce_sum(axis: 0, keepdims: false); + /// // We can call `reduce_sum_single_axis` function as follows. + /// return tensor.reduce_sum_single_axis(axis: 0, keepdims: false); /// } /// >>> [[4,6],[8,10]] /// ``` /// - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor; + fn reduce_sum_single_axis(self: @Tensor, axis: usize, keepdims: bool) -> Tensor; /// # tensor.argmax /// /// ```rust diff --git a/src/operators/tensor/implementations/tensor_bool.cairo b/src/operators/tensor/implementations/tensor_bool.cairo index 612a397cc..53b5e3060 100644 --- a/src/operators/tensor/implementations/tensor_bool.cairo +++ b/src/operators/tensor/implementations/tensor_bool.cairo @@ -64,7 +64,16 @@ impl BoolTensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn reduce_sum_single_axis(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { panic(array!['not supported!']) } @@ -570,17 +579,19 @@ impl BoolTryIntobool of TryInto { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_complex64.cairo b/src/operators/tensor/implementations/tensor_complex64.cairo index c9c31ae23..0cba48505 100644 --- a/src/operators/tensor/implementations/tensor_complex64.cairo +++ b/src/operators/tensor/implementations/tensor_complex64.cairo @@ -73,10 +73,23 @@ impl Complex64Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) } + + fn reduce_sum_single_axis( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) + } + + fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_prod::reduce_prod(self, axis, keepdims) } @@ -668,17 +681,19 @@ fn eq(lhs: @complex64, rhs: @complex64) -> bool { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index a37ed0442..405f665e1 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -75,8 +75,19 @@ impl FP16x16Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + fn reduce_sum_single_axis( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { @@ -760,17 +771,19 @@ fn relative_eq(lhs: @FP16x16, rhs: @FP16x16) -> bool { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 2003b28ff..01057cbf2 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -79,8 +79,19 @@ impl FP16x16WTensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + fn reduce_sum_single_axis( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { @@ -719,17 +730,19 @@ fn relative_eq(lhs: @FP16x16W, rhs: @FP16x16W) -> bool { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 4870226a1..cfdb06536 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -72,8 +72,20 @@ impl FP32x32Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + + fn reduce_sum_single_axis( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { @@ -766,17 +778,19 @@ fn relative_eq(lhs: @FP32x32, rhs: @FP32x32) -> bool { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index 3a7214d18..a33e549b5 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -72,8 +72,19 @@ impl FP64x64Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + fn reduce_sum_single_axis( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { @@ -766,17 +777,19 @@ fn relative_eq(lhs: @FP64x64, rhs: @FP64x64) -> bool { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.shape.len() != 0 && is_eq { - is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index b4a26d749..d1fcc5477 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -72,10 +72,23 @@ impl FP8x23Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + + fn reduce_sum_single_axis( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } + fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_prod::reduce_prod(self, axis, keepdims) } @@ -777,17 +790,19 @@ fn relative_eq(lhs: @FP8x23, rhs: @FP8x23) -> bool { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index 06a297b69..c061dff21 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -75,10 +75,23 @@ impl FP8x23WTensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + + fn reduce_sum_single_axis( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } + fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_prod::reduce_prod(self, axis, keepdims) } @@ -720,17 +733,19 @@ fn relative_eq(lhs: @FP8x23W, rhs: @FP8x23W) -> bool { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = relative_eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 296876516..e04b4e8b5 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -72,10 +72,18 @@ impl I32Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) } + fn reduce_sum_single_axis(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) + } fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_prod::reduce_prod(self, axis, keepdims) @@ -711,17 +719,19 @@ impl I32TensorPartialOrd of PartialOrd> { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index 42d807c68..e9f076d9d 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -70,8 +70,17 @@ impl I8Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + fn reduce_sum_single_axis(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { @@ -702,17 +711,19 @@ impl I8TensorPartialOrd of PartialOrd> { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() == 0 && !is_eq { - is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); - }; + while lhs.data.len() == 0 + && !is_eq { + is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); + }; is_eq } diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index efb681a86..b8264345a 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -69,8 +69,17 @@ impl U32Tensor of TensorTrait { reshape(self, target_shape) } - fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { - math::reduce_sum::reduce_sum(self, axis, keepdims) + fn reduce_sum( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_sum::reduce_sum(self, axes, keepdims, noop_with_empty_axes) + } + + fn reduce_sum_single_axis(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_sum_single_axis::reduce_sum_single_axis(self, axis, keepdims) } fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { @@ -656,17 +665,19 @@ impl U32TensorPartialOrd of PartialOrd> { fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { let mut is_eq = true; - while lhs.shape.len() != 0 && is_eq { - is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); - }; + while lhs.shape.len() != 0 + && is_eq { + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; if !is_eq { return false; } - while lhs.data.len() != 0 && is_eq { - is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); - }; + while lhs.data.len() != 0 + && is_eq { + is_eq = lhs.data.pop_front().unwrap() == rhs.data.pop_front().unwrap(); + }; is_eq } diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index b73f6d102..fb4813ae1 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -3,6 +3,7 @@ mod min; mod max_in_tensor; mod max; mod reduce_sum; +mod reduce_sum_single_axis; mod reduce_prod; mod argmax; mod argmin; diff --git a/src/operators/tensor/math/layer_normalization.cairo b/src/operators/tensor/math/layer_normalization.cairo index e61e826f5..f185b6bb5 100644 --- a/src/operators/tensor/math/layer_normalization.cairo +++ b/src/operators/tensor/math/layer_normalization.cairo @@ -4,6 +4,7 @@ use orion::operators::tensor::{ TensorTrait, Tensor, I8Tensor, I32Tensor, U32Tensor, FP16x16Tensor, BoolTensor }; use orion::operators::vec::{VecTrait, NullableVec, NullableVecImpl}; +use orion::operators::tensor::math::reduce_sum_single_axis::reduce_sum_single_axis; /// Cf: TensorTrait::layer_normalization docstring fn layer_normalization< @@ -12,6 +13,7 @@ fn layer_normalization< +TensorTrait, +NumberTrait, +PartialEq, + +AddEq, +Copy, +Drop, +Div>, @@ -90,13 +92,13 @@ fn layer_normalization< one_tensor.append(NumberTrait::one()); let x_mat = self.reshape(shape_matrix.span()); - let x_mean = x_mat.reduce_sum(1, true) + let x_mean = reduce_sum_single_axis(@x_mat, 1, true) / TensorTrait::new(shape_one.span(), col_number_tensor.span()); let x_diff = x_mat - x_mean; let x_squared_diff = x_diff * x_diff; - let variance = x_squared_diff.reduce_sum(1, true) + let variance = reduce_sum_single_axis(@x_squared_diff, 1, true) / TensorTrait::new(shape_one.span(), col_number_tensor.span()); let variance_eps = variance + TensorTrait::new(shape_one.span(), epsilon_tensor.span()); diff --git a/src/operators/tensor/math/reduce_l1.cairo b/src/operators/tensor/math/reduce_l1.cairo index ba2be9215..8322af094 100644 --- a/src/operators/tensor/math/reduce_l1.cairo +++ b/src/operators/tensor/math/reduce_l1.cairo @@ -1,6 +1,7 @@ use orion::numbers::NumberTrait; use orion::numbers::fixed_point::core::FixedTrait; use orion::operators::tensor::core::{Tensor, TensorTrait, ravel_index, unravel_index}; +use orion::operators::tensor::math::reduce_sum_single_axis::reduce_sum_single_axis; /// Cf: TensorTrait::reduce_sum docstring fn reduce_l1< @@ -16,5 +17,5 @@ fn reduce_l1< ) -> Tensor { let data_abs = self.abs(); - data_abs.reduce_sum(axis: axis, keepdims: keepdims) + reduce_sum_single_axis(@data_abs, axis: axis, keepdims: keepdims) } diff --git a/src/operators/tensor/math/reduce_l2.cairo b/src/operators/tensor/math/reduce_l2.cairo index 96f4b7245..cf5279df2 100644 --- a/src/operators/tensor/math/reduce_l2.cairo +++ b/src/operators/tensor/math/reduce_l2.cairo @@ -3,6 +3,7 @@ use core::debug::PrintTrait; use orion::numbers::NumberTrait; use orion::numbers::fixed_point::core::FixedTrait; use orion::operators::tensor::core::{Tensor, TensorTrait, ravel_index, unravel_index}; +use orion::operators::tensor::math::reduce_sum_single_axis::reduce_sum_single_axis; fn square< T, @@ -40,13 +41,14 @@ fn reduce_l2< impl TTensor: TensorTrait, impl TNumber: NumberTrait, impl TMul: Mul, + impl TAddEq: AddEq, impl TCopy: Copy, impl TDrop: Drop, >( self: @Tensor, axis: usize, keepdims: bool ) -> Tensor { let tensor_square = square(self); - let tensor_square_sum = tensor_square.reduce_sum(axis: axis, keepdims: keepdims); + let tensor_square_sum = reduce_sum_single_axis(@tensor_square, axis: axis, keepdims: keepdims); tensor_square_sum.sqrt() } @@ -57,6 +59,7 @@ fn reduce_l2_complex< impl TTensor: TensorTrait, impl TNumber: NumberTrait, impl TMul: Mul, + impl TAddEq: AddEq, impl TCopy: Copy, impl TDrop: Drop, impl TPrint: PrintTrait @@ -64,7 +67,9 @@ fn reduce_l2_complex< self: @Tensor, axis: usize, keepdims: bool ) -> Tensor { let mut tensor_square = square(@self.abs()); - let mut tensor_square_sum = tensor_square.reduce_sum(axis: axis, keepdims: keepdims); + let mut tensor_square_sum = reduce_sum_single_axis( + @tensor_square, axis: axis, keepdims: keepdims + ); tensor_square_sum.sqrt() } diff --git a/src/operators/tensor/math/reduce_log_sum.cairo b/src/operators/tensor/math/reduce_log_sum.cairo index 60a5225cb..8911b1e04 100644 --- a/src/operators/tensor/math/reduce_log_sum.cairo +++ b/src/operators/tensor/math/reduce_log_sum.cairo @@ -1,6 +1,7 @@ use orion::numbers::NumberTrait; use orion::numbers::fixed_point::core::FixedTrait; use orion::operators::tensor::core::{Tensor, TensorTrait, ravel_index, unravel_index}; +use orion::operators::tensor::math::reduce_sum_single_axis::reduce_sum_single_axis; /// Cf: TensorTrait::reduce_sum_square docstring fn reduce_log_sum< @@ -15,7 +16,7 @@ fn reduce_log_sum< >( self: @Tensor, axis: usize, keepdims: bool ) -> Tensor { - let tensor_square_sum = self.reduce_sum(axis: axis, keepdims: keepdims); + let tensor_square_sum = reduce_sum_single_axis(self, axis: axis, keepdims: keepdims); let tensor_square_sum_log = tensor_square_sum.log(); tensor_square_sum_log diff --git a/src/operators/tensor/math/reduce_sum.cairo b/src/operators/tensor/math/reduce_sum.cairo index 078345f4a..66d5aea5b 100644 --- a/src/operators/tensor/math/reduce_sum.cairo +++ b/src/operators/tensor/math/reduce_sum.cairo @@ -1,6 +1,12 @@ +use alexandria_sorting::bubble_sort; +use alexandria_data_structures::array_ext::{SpanTraitExt}; + +use orion::numbers::fixed_point::core::FixedTrait; use orion::numbers::NumberTrait; use orion::operators::tensor::core::{Tensor, TensorTrait, ravel_index, unravel_index}; -use orion::operators::tensor::helpers::{reduce_output_shape, len_from_shape, combine_indices}; +use orion::operators::tensor::helpers::{ + reduce_output_shape, len_from_shape, combine_indices, get_all_axes +}; /// Cf: TensorTrait::reduce_sum docstring fn reduce_sum< @@ -8,48 +14,98 @@ fn reduce_sum< MAG, impl TTensor: TensorTrait, impl TNumber: NumberTrait, - impl TAddEq: AddEq, impl TCopy: Copy, impl TDrop: Drop >( - self: @Tensor, axis: usize, keepdims: bool + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option ) -> Tensor { - let mut output_data: Array = array![]; - - if (*self.shape).len() == 1 { - assert(axis == 0, 'axis out of dimensions'); - let current_sum = accumulate_sum::(*self.data, *self.shape, *self.shape, axis); - output_data.append(current_sum); + let noop_with_empty_axes = match noop_with_empty_axes { + Option::Some(noop_with_empty_axes) => noop_with_empty_axes, + Option::None => false, + }; + let axes = match axes { + Option::Some(axes) => { + if (axes.len() == 0) { + get_all_axes(*self.shape) + } else { + assert(axes.len() == axes.unique().len(), 'duplicated axis.'); + let mut axes_arr: Array = array![]; + let mut copy_axes = axes; + loop { + match copy_axes.pop_front() { + Option::Some(axis) => { axes_arr.append(*axis); }, + Option::None => { break; } + }; + }; + let sorted_axes = bubble_sort::bubble_sort_elements(axes_arr, true).span(); + sorted_axes + } + }, + Option::None => { + if noop_with_empty_axes { + return *self; + } + get_all_axes(*self.shape) + }, + }; + let keepdims = match keepdims { + Option::Some(keepdims) => keepdims, + Option::None => true, + }; - let mut output_shape: Array = array![]; - output_shape.append(1); + let mut axis_c = 0; + let mut copy_axes = axes; + let mut shape = *self.shape; + let mut data = *self.data; + loop { + match copy_axes.pop_front() { + Option::Some(axis) => { + if (shape.len() == 1) { + let current_sum = accumulate_sum::(data, shape, shape, 0); + shape = array![].span(); + data = array![current_sum].span(); + break (); + } + let mut temp_data = array![]; + let mut temp_shape = reduce_output_shape(shape, *axis - axis_c, false); + let data_len = len_from_shape(temp_shape); + let mut index: usize = 0; + while index != data_len { + let indices = unravel_index(index, temp_shape); + let current_sum = accumulate_sum::(data, shape, indices, *axis - axis_c); - return TensorTrait::new(output_shape.span(), output_data.span()); - } else { - assert(axis <= (*self.shape).len(), 'axis out of dimensions'); - let output_shape = reduce_output_shape(*self.shape, axis, false); - let output_data_len = len_from_shape(output_shape); - let mut index: usize = 0; - while index != output_data_len { - let output_indices = unravel_index(index, output_shape); - let current_sum = accumulate_sum::(*self.data, *self.shape, output_indices, axis); + temp_data.append(current_sum); - output_data.append(current_sum); + index += 1; + }; - index += 1; + shape = temp_shape; + data = temp_data.span(); + axis_c += 1; + }, + Option::None => { break; } }; + }; - if keepdims { - let output_shape = reduce_output_shape(*self.shape, axis, true); + let mut axes_copy = axes; + if keepdims { + shape = *self.shape; + loop { + match axes_copy.pop_front() { + Option::Some(axis) => { shape = reduce_output_shape(shape, *axis, true); }, + Option::None => { break; } + }; + }; - TensorTrait::::new(output_shape, output_data.span()) - } else { - TensorTrait::::new(output_shape, output_data.span()) - } + TensorTrait::::new(shape, data) + } else { + TensorTrait::::new(shape, data) } } - /// Helper function that accumulates the sum of elements along a specific axis. /// /// # Arguments @@ -62,42 +118,35 @@ fn reduce_sum< /// * Panics if gas limit is exceeded during execution. /// /// # Returns -/// * An i32 value representing the accumulated sum along the specified axis. +/// * A value representing the accumulated sum along the specified axis. fn accumulate_sum< - T, - MAG, - impl TNumber: NumberTrait, - impl TAddEq: AddEq, - impl TCopy: Copy, - impl TDrop: Drop + T, MAG, impl TNumber: NumberTrait, + impl TCopy: Copy, impl TDrop: Drop >( mut input_data: Span, input_shape: Span, output_indices: Span, axis: usize ) -> T { let axis_len = *(input_shape)[axis]; - let mut acc: T = NumberTrait::zero(); + let mut sum: T = NumberTrait::zero(); - let mut axis_index: usize = 0; + let mut axis_index = 0; if (input_shape).len() > 1 { - loop { - if axis_index == axis_len { - break (); - } - + while axis_index != axis_len { let input_indices = combine_indices(output_indices, axis_index, axis); let input_index = ravel_index(input_shape, input_indices); let ele = *(input_data)[input_index]; - acc += ele; + sum = NumberTrait::add(sum, ele); + axis_index += 1; }; } else { loop { match input_data.pop_front() { - Option::Some(item) => { acc += *item; }, + Option::Some(item) => sum = NumberTrait::add(sum, *item), Option::None => { break; } }; }; } - return acc; + sum } diff --git a/src/operators/tensor/math/reduce_sum_single_axis.cairo b/src/operators/tensor/math/reduce_sum_single_axis.cairo new file mode 100644 index 000000000..75ceb8bf8 --- /dev/null +++ b/src/operators/tensor/math/reduce_sum_single_axis.cairo @@ -0,0 +1,106 @@ +use core::array::SpanTrait; +use core::option::OptionTrait; +use orion::numbers::NumberTrait; +use orion::operators::tensor::core::{Tensor, TensorTrait, ravel_index, unravel_index}; +use orion::operators::tensor::helpers::{reduce_output_shape, len_from_shape, combine_indices}; + + +fn reduce_sum_single_axis< + T, + MAG, + impl TTensor: TensorTrait, + impl TNumber: NumberTrait, + impl TAddEq: AddEq, + impl TCopy: Copy, + impl TDrop: Drop +>( + self: @Tensor, axis: usize, keepdims: bool +) -> Tensor { + let mut output_data: Array = array![]; + + if (*self.shape).len() == 1 { + assert(axis == 0, 'axis out of dimensions'); + let current_sum = accumulate_sum::(*self.data, *self.shape, *self.shape, axis); + output_data.append(current_sum); + + let mut output_shape: Array = array![]; + output_shape.append(1); + + return TensorTrait::new(output_shape.span(), output_data.span()); + } else { + assert(axis <= (*self.shape).len(), 'axis out of dimensions'); + let output_shape = reduce_output_shape(*self.shape, axis, false); + let output_data_len = len_from_shape(output_shape); + let mut index: usize = 0; + while index != output_data_len { + let output_indices = unravel_index(index, output_shape); + let current_sum = accumulate_sum::(*self.data, *self.shape, output_indices, axis); + + output_data.append(current_sum); + + index += 1; + }; + + if keepdims { + let output_shape = reduce_output_shape(*self.shape, axis, true); + + TensorTrait::::new(output_shape, output_data.span()) + } else { + TensorTrait::::new(output_shape, output_data.span()) + } + } +} + + +/// Helper function that accumulates the sum of elements along a specific axis. +/// +/// # Arguments +/// * `input_data` - The input's data. +/// * `input_shape` - The input's shape. +/// * `output_indices` - A span of output indices. +/// * `axis` - The axis along which to accumulate the sum. +/// +/// # Panics +/// * Panics if gas limit is exceeded during execution. +/// +/// # Returns +/// * An i32 value representing the accumulated sum along the specified axis. +fn accumulate_sum< + T, + MAG, + impl TNumber: NumberTrait, + impl TAddEq: AddEq, + impl TCopy: Copy, + impl TDrop: Drop +>( + mut input_data: Span, input_shape: Span, output_indices: Span, axis: usize +) -> T { + let axis_len = *(input_shape)[axis]; + let mut acc: T = NumberTrait::zero(); + + let mut axis_index: usize = 0; + + if (input_shape).len() > 1 { + loop { + if axis_index == axis_len { + break (); + } + + let input_indices = combine_indices(output_indices, axis_index, axis); + let input_index = ravel_index(input_shape, input_indices); + let ele = *(input_data)[input_index]; + acc += ele; + axis_index += 1; + }; + } else { + loop { + match input_data.pop_front() { + Option::Some(item) => { acc += *item; }, + Option::None => { break; } + }; + }; + } + + return acc; +} + diff --git a/src/operators/tensor/math/reduce_sum_square.cairo b/src/operators/tensor/math/reduce_sum_square.cairo index b8ad7df99..3b9cd5e2b 100644 --- a/src/operators/tensor/math/reduce_sum_square.cairo +++ b/src/operators/tensor/math/reduce_sum_square.cairo @@ -1,6 +1,7 @@ use orion::numbers::NumberTrait; use orion::operators::tensor::core::{Tensor, TensorTrait, ravel_index, unravel_index}; use orion::numbers::fixed_point::core::FixedTrait; +use orion::operators::tensor::math::reduce_sum_single_axis::reduce_sum_single_axis; fn square< T, @@ -45,7 +46,7 @@ fn reduce_sum_square< self: @Tensor, axis: usize, keepdims: bool ) -> Tensor { let tensor_square = square(self); - let tensor_square_sum = tensor_square.reduce_sum(axis: axis, keepdims: keepdims); + let tensor_square_sum = reduce_sum_single_axis(@tensor_square, axis: axis, keepdims: keepdims); tensor_square_sum } diff --git a/tests/nodes.cairo b/tests/nodes.cairo index 29bebb762..ff888973b 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -278,26 +278,6 @@ mod or_i8; mod or_i8_broadcast; mod or_u32; mod or_u32_broadcast; -mod reduce_sum_fp16x16_1D; -mod reduce_sum_fp16x16_2D_default; -mod reduce_sum_fp16x16_2D_keepdims; -mod reduce_sum_fp16x16_2D_axis_1; -mod reduce_sum_fp8x23_1D; -mod reduce_sum_fp8x23_2D_default; -mod reduce_sum_fp8x23_2D_keepdims; -mod reduce_sum_fp8x23_2D_axis_1; -mod reduce_sum_i32_1D; -mod reduce_sum_i32_2D_default; -mod reduce_sum_i32_2D_keepdims; -mod reduce_sum_i32_2D_axis_1; -mod reduce_sum_i8_1D; -mod reduce_sum_i8_2D_default; -mod reduce_sum_i8_2D_keepdims; -mod reduce_sum_i8_2D_axis_1; -mod reduce_sum_u32_1D; -mod reduce_sum_u32_2D_default; -mod reduce_sum_u32_2D_keepdims; -mod reduce_sum_u32_2D_axis_1; mod relu_fp16x16; mod relu_fp8x23; mod relu_i32; @@ -1047,3 +1027,27 @@ mod label_encoder_fp8x23_default; mod label_encoder_i8_default; mod label_encoder_i32_default; mod label_encoder_u32_default; +mod reduce_sum_single_axis_fp16x16_1D; +mod reduce_sum_single_axis_fp16x16_2D_default; +mod reduce_sum_single_axis_fp16x16_2D_keepdims; +mod reduce_sum_single_axis_fp16x16_2D_axis_1; +mod reduce_sum_single_axis_fp8x23_1D; +mod reduce_sum_single_axis_fp8x23_2D_default; +mod reduce_sum_single_axis_fp8x23_2D_keepdims; +mod reduce_sum_single_axis_fp8x23_2D_axis_1; +mod reduce_sum_single_axis_i32_1D; +mod reduce_sum_single_axis_i32_2D_default; +mod reduce_sum_single_axis_i32_2D_keepdims; +mod reduce_sum_single_axis_i32_2D_axis_1; +mod reduce_sum_single_axis_i8_1D; +mod reduce_sum_single_axis_i8_2D_default; +mod reduce_sum_single_axis_i8_2D_keepdims; +mod reduce_sum_single_axis_i8_2D_axis_1; +mod reduce_sum_single_axis_u32_1D; +mod reduce_sum_single_axis_u32_2D_default; +mod reduce_sum_single_axis_u32_2D_keepdims; +mod reduce_sum_single_axis_u32_2D_axis_1; +mod reduce_sum_keep_dims; +mod reduce_sum_no_keep_dims; +mod reduce_sum_default_axes_keepdims; +mod reduce_sum_empty_axes_input_noop; diff --git a/tests/nodes/reduce_sum_default_axes_keepdims.cairo b/tests/nodes/reduce_sum_default_axes_keepdims.cairo new file mode 100644 index 000000000..483b37d20 --- /dev/null +++ b/tests/nodes/reduce_sum_default_axes_keepdims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32TensorPartialEq; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_default_axes_keepdims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum(Option::Some(array![].span()), Option::Some(true), Option::None); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_default_axes_keepdims/input_0.cairo b/tests/nodes/reduce_sum_default_axes_keepdims/input_0.cairo new file mode 100644 index 000000000..2de5818c3 --- /dev/null +++ b/tests/nodes/reduce_sum_default_axes_keepdims/input_0.cairo @@ -0,0 +1,26 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + data.append(10); + data.append(11); + data.append(12); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_default_axes_keepdims/output_0.cairo b/tests/nodes/reduce_sum_default_axes_keepdims/output_0.cairo new file mode 100644 index 000000000..6cc93d6f7 --- /dev/null +++ b/tests/nodes/reduce_sum_default_axes_keepdims/output_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(78); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_empty_axes_input_noop.cairo b/tests/nodes/reduce_sum_empty_axes_input_noop.cairo new file mode 100644 index 000000000..973479855 --- /dev/null +++ b/tests/nodes/reduce_sum_empty_axes_input_noop.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32TensorPartialEq; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_empty_axes_input_noop() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum(Option::None, Option::Some(true), Option::Some(true)); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_empty_axes_input_noop/input_0.cairo b/tests/nodes/reduce_sum_empty_axes_input_noop/input_0.cairo new file mode 100644 index 000000000..2de5818c3 --- /dev/null +++ b/tests/nodes/reduce_sum_empty_axes_input_noop/input_0.cairo @@ -0,0 +1,26 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + data.append(10); + data.append(11); + data.append(12); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_empty_axes_input_noop/output_0.cairo b/tests/nodes/reduce_sum_empty_axes_input_noop/output_0.cairo new file mode 100644 index 000000000..d679605a0 --- /dev/null +++ b/tests/nodes/reduce_sum_empty_axes_input_noop/output_0.cairo @@ -0,0 +1,26 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + data.append(10); + data.append(11); + data.append(12); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_keep_dims.cairo b/tests/nodes/reduce_sum_keep_dims.cairo new file mode 100644 index 000000000..661d3711f --- /dev/null +++ b/tests/nodes/reduce_sum_keep_dims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32TensorPartialEq; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_keep_dims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum(Option::Some(array![1].span()), Option::Some(true), Option::None); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_keep_dims/input_0.cairo b/tests/nodes/reduce_sum_keep_dims/input_0.cairo new file mode 100644 index 000000000..2de5818c3 --- /dev/null +++ b/tests/nodes/reduce_sum_keep_dims/input_0.cairo @@ -0,0 +1,26 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + data.append(10); + data.append(11); + data.append(12); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_keep_dims/output_0.cairo b/tests/nodes/reduce_sum_keep_dims/output_0.cairo new file mode 100644 index 000000000..5326997d6 --- /dev/null +++ b/tests/nodes/reduce_sum_keep_dims/output_0.cairo @@ -0,0 +1,20 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(6); + data.append(12); + data.append(14); + data.append(20); + data.append(22); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_no_keep_dims.cairo b/tests/nodes/reduce_sum_no_keep_dims.cairo new file mode 100644 index 000000000..a83405f01 --- /dev/null +++ b/tests/nodes/reduce_sum_no_keep_dims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::U32TensorPartialEq; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_no_keep_dims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum(Option::Some(array![1].span()), Option::Some(false), Option::None); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_no_keep_dims/input_0.cairo b/tests/nodes/reduce_sum_no_keep_dims/input_0.cairo new file mode 100644 index 000000000..2de5818c3 --- /dev/null +++ b/tests/nodes/reduce_sum_no_keep_dims/input_0.cairo @@ -0,0 +1,26 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + data.append(10); + data.append(11); + data.append(12); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_no_keep_dims/output_0.cairo b/tests/nodes/reduce_sum_no_keep_dims/output_0.cairo new file mode 100644 index 000000000..72c71a185 --- /dev/null +++ b/tests/nodes/reduce_sum_no_keep_dims/output_0.cairo @@ -0,0 +1,19 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(6); + data.append(12); + data.append(14); + data.append(20); + data.append(22); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_1D.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_1D.cairo new file mode 100644 index 000000000..b91251f21 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_1D.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp16x16_1D() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_1D/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_1D/input_0.cairo new file mode 100644 index 000000000..33e05815f --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_1D/input_0.cairo @@ -0,0 +1,15 @@ +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(3); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_1D/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_1D/output_0.cairo new file mode 100644 index 000000000..db089f852 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_1D/output_0.cairo @@ -0,0 +1,13 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1.cairo new file mode 100644 index 000000000..ab1792bf1 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp16x16_2D_axis_1() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(1, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1/input_0.cairo new file mode 100644 index 000000000..6a8b7cb09 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1/output_0.cairo new file mode 100644 index 000000000..11bc960a2 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_axis_1/output_0.cairo @@ -0,0 +1,14 @@ +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(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 327680, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default.cairo new file mode 100644 index 000000000..0faf8ca88 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp16x16_2D_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default/input_0.cairo new file mode 100644 index 000000000..6a8b7cb09 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default/output_0.cairo new file mode 100644 index 000000000..d3b81daf0 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_default/output_0.cairo @@ -0,0 +1,14 @@ +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(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 262144, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims.cairo new file mode 100644 index 000000000..4a587072e --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP16x16TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp16x16_2D_keepdims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, true); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims/input_0.cairo new file mode 100644 index 000000000..6a8b7cb09 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims/input_0.cairo @@ -0,0 +1,17 @@ +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(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 196608, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims/output_0.cairo new file mode 100644 index 000000000..cadc8f1f9 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp16x16_2D_keepdims/output_0.cairo @@ -0,0 +1,15 @@ +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(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 131072, sign: false }); + data.append(FP16x16 { mag: 262144, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_1D.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_1D.cairo new file mode 100644 index 000000000..9640ee345 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_1D.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp8x23_1D() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_1D/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_1D/input_0.cairo new file mode 100644 index 000000000..0b5fd5c6a --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_1D/input_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_1D/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_1D/output_0.cairo new file mode 100644 index 000000000..713f0d28a --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_1D/output_0.cairo @@ -0,0 +1,13 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 25165824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1.cairo new file mode 100644 index 000000000..84c6bf093 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp8x23_2D_axis_1() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(1, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1/input_0.cairo new file mode 100644 index 000000000..88c1db446 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1/output_0.cairo new file mode 100644 index 000000000..4b3e55bbe --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_axis_1/output_0.cairo @@ -0,0 +1,14 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 41943040, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default.cairo new file mode 100644 index 000000000..5b9eb0fb1 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp8x23_2D_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default/input_0.cairo new file mode 100644 index 000000000..88c1db446 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default/output_0.cairo new file mode 100644 index 000000000..12e4ce95f --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_default/output_0.cairo @@ -0,0 +1,14 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 33554432, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims.cairo new file mode 100644 index 000000000..0c4bede22 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_fp8x23_2D_keepdims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, true); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims/input_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims/input_0.cairo new file mode 100644 index 000000000..88c1db446 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 25165824, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims/output_0.cairo b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims/output_0.cairo new file mode 100644 index 000000000..3435050a2 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_fp8x23_2D_keepdims/output_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 16777216, sign: false }); + data.append(FP8x23 { mag: 33554432, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_1D.cairo b/tests/nodes/reduce_sum_single_axis_i32_1D.cairo new file mode 100644 index 000000000..7619c0ee4 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_1D.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::I32TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i32_1D() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_1D/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_1D/input_0.cairo new file mode 100644 index 000000000..a16657995 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_1D/input_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_1D/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_1D/output_0.cairo new file mode 100644 index 000000000..d1b50c386 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_1D/output_0.cairo @@ -0,0 +1,13 @@ +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); + + let mut data = ArrayTrait::new(); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1.cairo new file mode 100644 index 000000000..9cd61ef22 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::I32TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i32_2D_axis_1() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(1, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1/input_0.cairo new file mode 100644 index 000000000..2a164a41a --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1/output_0.cairo new file mode 100644 index 000000000..b9cf7c45d --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_axis_1/output_0.cairo @@ -0,0 +1,14 @@ +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(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_default.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_default.cairo new file mode 100644 index 000000000..cb28aac41 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_default.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::I32TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i32_2D_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_default/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_default/input_0.cairo new file mode 100644 index 000000000..2a164a41a --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_default/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_default/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_default/output_0.cairo new file mode 100644 index 000000000..f1b1716d2 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_default/output_0.cairo @@ -0,0 +1,14 @@ +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(2); + + let mut data = ArrayTrait::new(); + data.append(2); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims.cairo new file mode 100644 index 000000000..390f3742c --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::I32TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i32_2D_keepdims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, true); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims/input_0.cairo new file mode 100644 index 000000000..2a164a41a --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims/output_0.cairo new file mode 100644 index 000000000..cbcfa21d7 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i32_2D_keepdims/output_0.cairo @@ -0,0 +1,15 @@ +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(2); + + let mut data = ArrayTrait::new(); + data.append(2); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_1D.cairo b/tests/nodes/reduce_sum_single_axis_i8_1D.cairo new file mode 100644 index 000000000..7a3059a32 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_1D.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i8_1D() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_1D/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_1D/input_0.cairo new file mode 100644 index 000000000..61b70cda3 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_1D/input_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 1, sign: false }); + data.append(FP8x23 { mag: 2, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_1D/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_1D/output_0.cairo new file mode 100644 index 000000000..3a2fc6193 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_1D/output_0.cairo @@ -0,0 +1,13 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 3, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1.cairo new file mode 100644 index 000000000..381c69ab2 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i8_2D_axis_1() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(1, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1/input_0.cairo new file mode 100644 index 000000000..53f6405e1 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 1, sign: false }); + data.append(FP8x23 { mag: 2, sign: false }); + data.append(FP8x23 { mag: 3, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1/output_0.cairo new file mode 100644 index 000000000..26f2c2987 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_axis_1/output_0.cairo @@ -0,0 +1,14 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 1, sign: false }); + data.append(FP8x23 { mag: 5, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_default.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_default.cairo new file mode 100644 index 000000000..09d98cd79 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_default.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i8_2D_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_default/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_default/input_0.cairo new file mode 100644 index 000000000..53f6405e1 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_default/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 1, sign: false }); + data.append(FP8x23 { mag: 2, sign: false }); + data.append(FP8x23 { mag: 3, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_default/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_default/output_0.cairo new file mode 100644 index 000000000..de769f8c0 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_default/output_0.cairo @@ -0,0 +1,14 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 2, sign: false }); + data.append(FP8x23 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims.cairo new file mode 100644 index 000000000..2af82ece3 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP8x23TensorPartialEq; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_i8_2D_keepdims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, true); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims/input_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims/input_0.cairo new file mode 100644 index 000000000..53f6405e1 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 1, sign: false }); + data.append(FP8x23 { mag: 2, sign: false }); + data.append(FP8x23 { mag: 3, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims/output_0.cairo b/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims/output_0.cairo new file mode 100644 index 000000000..b45c5c277 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_i8_2D_keepdims/output_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use orion::numbers::{FixedTrait, FP8x23}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 2, sign: false }); + data.append(FP8x23 { mag: 4, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_1D.cairo b/tests/nodes/reduce_sum_single_axis_u32_1D.cairo new file mode 100644 index 000000000..e266d2e04 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_1D.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::U32TensorPartialEq; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_u32_1D() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_1D/input_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_1D/input_0.cairo new file mode 100644 index 000000000..a350d75c1 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_1D/input_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_1D/output_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_1D/output_0.cairo new file mode 100644 index 000000000..bd3b7eea9 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_1D/output_0.cairo @@ -0,0 +1,13 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1.cairo new file mode 100644 index 000000000..fcd06fb15 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::U32TensorPartialEq; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_u32_2D_axis_1() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(1, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1/input_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1/input_0.cairo new file mode 100644 index 000000000..530126fb5 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1/output_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1/output_0.cairo new file mode 100644 index 000000000..1bee2ed67 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_axis_1/output_0.cairo @@ -0,0 +1,14 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(5); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_default.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_default.cairo new file mode 100644 index 000000000..312619beb --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_default.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::U32TensorPartialEq; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_u32_2D_default() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, false); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_default/input_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_default/input_0.cairo new file mode 100644 index 000000000..530126fb5 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_default/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_default/output_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_default/output_0.cairo new file mode 100644 index 000000000..5aeddf000 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_default/output_0.cairo @@ -0,0 +1,14 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(2); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims.cairo new file mode 100644 index 000000000..124af02d5 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::U32TensorPartialEq; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_sum_single_axis_u32_2D_keepdims() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_sum_single_axis(0, true); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims/input_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims/input_0.cairo new file mode 100644 index 000000000..530126fb5 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(0); + data.append(1); + data.append(2); + data.append(3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims/output_0.cairo b/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims/output_0.cairo new file mode 100644 index 000000000..5b761b563 --- /dev/null +++ b/tests/nodes/reduce_sum_single_axis_u32_2D_keepdims/output_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{U32Tensor, U32TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(2); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +}