From cb8de919bc3b0805e956bb15fd66a06e8bda6a0a Mon Sep 17 00:00:00 2001 From: chachaleo Date: Fri, 22 Dec 2023 16:25:01 +0100 Subject: [PATCH] feat.tensor-complex --- nodegen/helpers.py | 16 + nodegen/node/reduce_l2.py | 26 + src/numbers/complex_number/complex64.cairo | 7 +- src/operators/ml.cairo | 4 +- src/operators/tensor.cairo | 4 + src/operators/tensor/implementations.cairo | 1 + .../tensor/implementations/tensor_bool.cairo | 4 +- .../implementations/tensor_complex64.cairo | 615 ++++++++++++++++++ .../implementations/tensor_fp16x16.cairo | 6 +- .../implementations/tensor_fp16x16wide.cairo | 6 +- .../implementations/tensor_fp32x32.cairo | 6 +- .../implementations/tensor_fp64x64.cairo | 6 +- .../implementations/tensor_fp8x23.cairo | 6 +- .../implementations/tensor_fp8x23wide.cairo | 6 +- .../tensor/implementations/tensor_i32.cairo | 6 +- .../tensor/implementations/tensor_i8.cairo | 6 +- .../tensor/implementations/tensor_u32.cairo | 6 +- src/operators/tensor/math/acos.cairo | 9 +- src/operators/tensor/math/acosh.cairo | 8 +- src/operators/tensor/math/asin.cairo | 9 +- src/operators/tensor/math/asinh.cairo | 8 +- src/operators/tensor/math/atan.cairo | 8 +- src/operators/tensor/math/cos.cairo | 8 +- src/operators/tensor/math/cosh.cairo | 8 +- src/operators/tensor/math/exp.cairo | 8 +- src/operators/tensor/math/gather_nd.cairo | 45 +- src/operators/tensor/math/log.cairo | 8 +- src/operators/tensor/math/reduce_l2.cairo | 30 +- src/operators/tensor/math/sin.cairo | 8 +- src/operators/tensor/math/sinh.cairo | 8 +- src/operators/tensor/math/sqrt.cairo | 8 +- src/operators/tensor/math/tanh.cairo | 8 +- tests/lib.cairo | 1 + tests/nodes.cairo | 1 + .../gather_nd_fp16x16_3d_batch_dims1.cairo | 2 +- .../gather_nd_fp16x16_3d_batch_dims2.cairo | 2 +- .../nodes/gather_nd_fp16x16_3d_default.cairo | 2 +- .../gather_nd_fp8x23_3d_batch_dims1.cairo | 2 +- .../gather_nd_fp8x23_3d_batch_dims2.cairo | 2 +- tests/nodes/gather_nd_fp8x23_3d_default.cairo | 2 +- .../nodes/gather_nd_i32_3d_batch_dims1.cairo | 2 +- .../nodes/gather_nd_i32_3d_batch_dims2.cairo | 2 +- tests/nodes/gather_nd_i32_3d_default.cairo | 2 +- tests/nodes/gather_nd_i8_3d_batch_dims1.cairo | 2 +- tests/nodes/gather_nd_i8_3d_default.cairo | 2 +- tests/nodes/gather_nd_u32_batch_dims1.cairo | 2 +- tests/nodes/gather_nd_u32_batch_dims2.cairo | 2 +- tests/nodes/gather_nd_u32_default.cairo | 2 +- tests/nodes/reduce_l2_complex64_axis_0.cairo | 24 + .../reduce_l2_complex64_axis_0/input_0.cairo | 56 ++ .../reduce_l2_complex64_axis_0/output_0.cairo | 35 + 51 files changed, 939 insertions(+), 118 deletions(-) create mode 100644 src/operators/tensor/implementations/tensor_complex64.cairo create mode 100644 tests/nodes/reduce_l2_complex64_axis_0.cairo create mode 100644 tests/nodes/reduce_l2_complex64_axis_0/input_0.cairo create mode 100644 tests/nodes/reduce_l2_complex64_axis_0/output_0.cairo diff --git a/nodegen/helpers.py b/nodegen/helpers.py index cf876ccc0..07ae07ed0 100644 --- a/nodegen/helpers.py +++ b/nodegen/helpers.py @@ -10,6 +10,8 @@ class FixedImpl(Enum): FP8x23 = 'FP8x23' FP16x16 = 'FP16x16' + FP64x64 = 'FP64x64' + def to_fp(x: np.ndarray, fp_impl: FixedImpl): @@ -18,15 +20,19 @@ def to_fp(x: np.ndarray, fp_impl: FixedImpl): return (x * 2**23).astype(np.int64) case FixedImpl.FP16x16: return (x * 2**16).astype(np.int64) + case FixedImpl.FP64x64: + return (x * 2**64) class Dtype(Enum): FP8x23 = 'FP8x23' FP16x16 = 'FP16x16' + FP64x64 = 'FP64x64' I8 = 'i8' I32 = 'i32' U32 = 'u32' BOOL = 'bool' + COMPLEX64 = 'complex64' class Tensor: @@ -166,8 +172,15 @@ def get_data_statement(data: np.ndarray, dtype: Dtype) -> list[str]: return ["FP8x23 { "+f"mag: {abs(int(x))}, sign: {str(x < 0).lower()} "+"}" for x in data.flatten()] case Dtype.FP16x16: return ["FP16x16 { "+f"mag: {abs(int(x))}, sign: {str(x < 0).lower()} "+"}" for x in data.flatten()] + case Dtype.FP64x64: + return ["FP64x64 { "+f"mag: {abs(int(x))}, sign: {str(x < 0).lower()} "+"}" for x in data.flatten()] case Dtype.BOOL: return [str(x).lower() for x in data.flatten()] + case Dtype.COMPLEX64: + return ["complex64 { "+"real: FP64x64 { "+f"mag: {abs(int(np.real(x)))}, sign: {str(np.real(x) < 0).lower()} "+"} , img: FP64x64 { "+f"mag: {abs(int(np.imag(x)))}, sign: {str(np.imag(x) < 0).lower()} "+"} }" for x in data.flatten()] + + + def get_data_statement_for_sequences(data: Sequence, dtype: Dtype) -> list[list[str]]: @@ -227,6 +240,7 @@ def find_all_types(tensors: list[Tensor | Sequence]) -> list[Dtype]: Dtype.FP8x23: ["orion::operators::tensor::FP8x23Tensor",], Dtype.FP16x16: ["orion::operators::tensor::FP16x16Tensor",], Dtype.BOOL: ["orion::operators::tensor::BoolTensor",], + Dtype.COMPLEX64: ["orion::operators::tensor::Complex64Tensor",], } @@ -246,6 +260,7 @@ def find_all_types(tensors: list[Tensor | Sequence]) -> list[Dtype]: Dtype.FP8x23: ["orion::operators::tensor::FP8x23TensorPartialEq",], Dtype.FP16x16: ["orion::operators::tensor::FP16x16TensorPartialEq",], Dtype.BOOL: ["orion::operators::tensor::BoolTensorPartialEq",], + Dtype.COMPLEX64: ["orion::operators::tensor::Complex64TensorPartialEq",], } @@ -256,4 +271,5 @@ def find_all_types(tensors: list[Tensor | Sequence]) -> list[Dtype]: Dtype.FP8x23: ["orion::numbers::{FixedTrait, FP8x23}",], Dtype.FP16x16: ["orion::numbers::{FixedTrait, FP16x16}",], Dtype.BOOL: [], + Dtype.COMPLEX64: ["orion::numbers::{NumberTrait, complex64}",], } \ No newline at end of file diff --git a/nodegen/node/reduce_l2.py b/nodegen/node/reduce_l2.py index 081125f54..67c8cfa10 100644 --- a/nodegen/node/reduce_l2.py +++ b/nodegen/node/reduce_l2.py @@ -4,6 +4,7 @@ import numpy as np + class Reduce_l2(RunAll): @staticmethod def reduce_l2_fp8x23(): @@ -107,4 +108,29 @@ def reduce_l2_axis_0(): reduce_l2_export_do_not_keepdims() reduce_l2_export_keepdims() + reduce_l2_axis_0() + + @staticmethod + def reduce_l2_complex64(): + + + + def reduce_l2_axis_0(): + shape = [2, 3] + axes = np.array([0], dtype=np.int64) + keepdims = True + x = np.reshape(np.array([1.+2.j, 2.-1.j, 3.-3.j, 3.-2.j, 3.+5.j, 4.- 1.j]), shape) + y = np.sqrt(np.sum(a=np.square(abs(x)), axis=tuple(axes), keepdims=True)) + print(to_fp(x.flatten(), FixedImpl.FP64x64)) + + x = Tensor(Dtype.COMPLEX64, x.shape, to_fp( + x.flatten(), FixedImpl.FP64x64)) + + y = Tensor(Dtype.COMPLEX64, y.shape, to_fp( + y.flatten(), FixedImpl.FP64x64)) + + name = "reduce_l2_complex64_axis_0" + make_test( + [x], y, "input_0.reduce_l2(0, true)", name) + reduce_l2_axis_0() \ No newline at end of file diff --git a/src/numbers/complex_number/complex64.cairo b/src/numbers/complex_number/complex64.cairo index fe7b70581..c3b649c6d 100644 --- a/src/numbers/complex_number/complex64.cairo +++ b/src/numbers/complex_number/complex64.cairo @@ -73,7 +73,12 @@ impl Complex64Impl of ComplexTrait { let y = self.img; let two = FP64x64Impl::new(TWO, false); let real = (((x.pow(two) + y.pow(two)).sqrt() + x) / two).sqrt(); - let img = (((x.pow(two) + y.pow(two)).sqrt() - x) / two).sqrt(); + let img = if y == FP64x64Impl::ZERO() { + FP64x64Impl::ZERO() + } else { + (((x.pow(two) + y.pow(two)).sqrt() - x) / two).sqrt() + }; + let img = FP64x64Impl::new(img.mag, y.sign); complex64 { real, img } } diff --git a/src/operators/ml.cairo b/src/operators/ml.cairo index 4bfd10060..93a490bbe 100644 --- a/src/operators/ml.cairo +++ b/src/operators/ml.cairo @@ -11,4 +11,6 @@ use orion::operators::ml::tree_ensemble::tree_ensemble_classifier::{ use orion::operators::ml::tree_ensemble::tree_ensemble_regressor::{ TreeEnsembleRegressor, TreeEnsembleRegressorImpl, TreeEnsembleRegressorTrait, AGGREGATE_FUNCTION }; -use orion::operators::ml::linear::linear_regressor::{LinearRegressorTrait, LinearRegressorImpl, LinearRegressor}; +use orion::operators::ml::linear::linear_regressor::{ + LinearRegressorTrait, LinearRegressorImpl, LinearRegressor +}; diff --git a/src/operators/tensor.cairo b/src/operators/tensor.cairo index c217cb1f6..adf2076b5 100644 --- a/src/operators/tensor.cairo +++ b/src/operators/tensor.cairo @@ -39,3 +39,7 @@ use orion::operators::tensor::implementations::tensor_u32::{ use orion::operators::tensor::implementations::tensor_bool::{BoolTensor, BoolTensorPartialEq}; +use orion::operators::tensor::implementations::tensor_complex64::{ + Complex64Tensor, Complex64TensorAdd, Complex64TensorSub, Complex64TensorMul, Complex64TensorDiv, + Complex64TensorPartialEq, +}; diff --git a/src/operators/tensor/implementations.cairo b/src/operators/tensor/implementations.cairo index 61ee08dd3..f9a7b406f 100644 --- a/src/operators/tensor/implementations.cairo +++ b/src/operators/tensor/implementations.cairo @@ -8,3 +8,4 @@ mod tensor_fp64x64; mod tensor_fp32x32; mod tensor_fp16x16wide; mod tensor_fp8x23wide; +mod tensor_complex64; diff --git a/src/operators/tensor/implementations/tensor_bool.cairo b/src/operators/tensor/implementations/tensor_bool.cairo index d2afe3fc5..1f11e1f3f 100644 --- a/src/operators/tensor/implementations/tensor_bool.cairo +++ b/src/operators/tensor/implementations/tensor_bool.cairo @@ -472,7 +472,9 @@ impl BoolTensor of TensorTrait { panic(array!['not supported!']) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } } diff --git a/src/operators/tensor/implementations/tensor_complex64.cairo b/src/operators/tensor/implementations/tensor_complex64.cairo new file mode 100644 index 000000000..810f347ca --- /dev/null +++ b/src/operators/tensor/implementations/tensor_complex64.cairo @@ -0,0 +1,615 @@ +use core::array::ArrayTrait; +use core::array::SpanTrait; +use core::option::OptionTrait; +use core::traits::{TryInto, Into}; + +use orion::numbers::fixed_point::core::FixedTrait; +use orion::operators::tensor::core::{ + new_tensor, constant_of_shape, stride, Tensor, TensorTrait, ravel_index, unravel_index, reshape, + at_tensor, +}; +use orion::operators::tensor::{math, linalg, quantization, core as core_tensor, ml}; +use orion::numbers::{i8, i32, NumberTrait, FP64x64, FP64x64Impl}; +use orion::numbers::fixed_point::implementations::fp64x64::core::ONE; +use orion::operators::tensor::implementations::{ + tensor_i8::I8Tensor, tensor_u32::U32Tensor, tensor_bool::BoolTensor +}; +use orion::numbers::complex_number::complex_trait::ComplexTrait; +use orion::numbers::complex_number::complex64::{Complex64Impl, complex64}; + + +impl Complex64Tensor of TensorTrait { + fn new(shape: Span, data: Span) -> Tensor { + new_tensor(shape, data) + } + + fn constant_of_shape(shape: Span, value: complex64) -> Tensor { + constant_of_shape(shape, value) + } + + fn at(self: @Tensor, indices: Span) -> complex64 { + *at_tensor(self, indices) + } + + fn add(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::add(@lhs, @rhs) + } + + fn sub(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::sub(@lhs, @rhs) + } + + fn mul(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::mul(@lhs, @rhs) + } + + fn div(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::div(@lhs, @rhs) + } + + fn min_in_tensor(self: @Tensor) -> complex64 { + panic(array!['not supported!']) + } + + fn min(tensors: Span>) -> Tensor { + panic(array!['not supported!']) + } + + fn max_in_tensor(self: @Tensor) -> complex64 { + panic(array!['not supported!']) + } + + fn max(tensors: Span>) -> Tensor { + panic(array!['not supported!']) + } + + fn stride(self: @Tensor) -> Span { + stride(*self.shape) + } + + fn ravel_index(self: @Tensor, indices: Span) -> usize { + ravel_index(*self.shape, indices) + } + + fn unravel_index(self: @Tensor, index: usize) -> Span { + unravel_index(index, *self.shape) + } + + fn reshape(self: @Tensor, target_shape: Span) -> Tensor { + reshape(self, target_shape) + } + + fn reduce_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_sum::reduce_sum(self, axis, keepdims) + } + + fn reduce_prod(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_prod::reduce_prod(self, axis, keepdims) + } + + fn argmax( + self: @Tensor, + axis: usize, + keepdims: Option, + select_last_index: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn argmin( + self: @Tensor, + axis: usize, + keepdims: Option, + select_last_index: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn transpose(self: @Tensor, axes: Span) -> Tensor { + linalg::transpose::transpose(self, axes) + } + + fn matmul(self: @Tensor, other: @Tensor) -> Tensor { + linalg::matmul::matmul(self, other) + } + + fn exp(self: @Tensor) -> Tensor { + math::exp::exp(*self) + } + + fn log(self: @Tensor) -> Tensor { + math::log::log(*self) + } + + fn equal(self: @Tensor, other: @Tensor) -> Tensor { + math::equal::equal(self, other) + } + + fn greater(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn greater_equal(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn less(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn less_equal(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn abs(self: @Tensor) -> Tensor { + math::abs::abs(*self) + } + + fn neg(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn ceil(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn sin(self: @Tensor) -> Tensor { + math::sin::sin(*self) + } + + fn cos(self: @Tensor) -> Tensor { + math::cos::cos(*self) + } + + fn asin(self: @Tensor) -> Tensor { + math::asin::asin(*self) + } + + fn cumsum( + self: @Tensor, axis: usize, exclusive: Option, reverse: Option + ) -> Tensor { + math::cumsum::cumsum(self, axis, exclusive, reverse) + } + + fn flatten(self: @Tensor, axis: usize) -> Tensor { + math::flatten::flatten(self, axis) + } + + fn sinh(self: @Tensor) -> Tensor { + math::sinh::sinh(*self) + } + + fn tanh(self: @Tensor) -> Tensor { + math::tanh::tanh(*self) + } + + fn cosh(self: @Tensor) -> Tensor { + math::cosh::cosh(*self) + } + + fn acosh(self: @Tensor) -> Tensor { + math::acosh::acosh(*self) + } + + fn asinh(self: @Tensor) -> Tensor { + math::asinh::asinh(*self) + } + + fn atan(self: @Tensor) -> Tensor { + math::atan::atan(*self) + } + + fn xor(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn or(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn acos(self: @Tensor) -> Tensor { + math::acos::acos(*self) + } + + fn onehot( + self: @Tensor, depth: usize, axis: Option, values: Span + ) -> Tensor { + panic(array!['not supported!']) + } + + fn sqrt(self: @Tensor) -> Tensor { + math::sqrt::sqrt(*self) + } + + fn concat(tensors: Span>, axis: usize,) -> Tensor { + math::concat::concat(tensors, axis) + } + + fn quantize_linear( + self: @Tensor, y_scale: @Tensor, y_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + + fn dequantize_linear( + self: @Tensor, x_scale: @Tensor, x_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + + fn qlinear_add( + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + + fn qlinear_mul( + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + + fn qlinear_matmul( + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + b: @Tensor, + b_scale: @Tensor, + b_zero_point: @Tensor, + y_scale: @Tensor, + y_zero_point: @Tensor + ) -> Tensor:: { + panic(array!['not supported!']) + } + + fn qlinear_concat( + tensors: Span>, + scales: Span>, + zero_points: Span>, + y_scale: @Tensor, + y_zero_point: @Tensor, + axis: usize + ) -> Tensor:: { + panic(array!['not supported!']) + } + + fn qlinear_leakyrelu( + self: @Tensor, + a_scale: @Tensor, + a_zero_point: @Tensor, + alpha: complex64 + ) -> Tensor:: { + panic(array!['not supported!']) + } + + fn slice( + self: @Tensor, + starts: Span, + ends: Span, + axes: Option>, + steps: Option> + ) -> Tensor { + core_tensor::slice::(self, starts, ends, axes, steps) + } + + fn gather( + self: @Tensor, indices: Tensor, axis: Option + ) -> Tensor { + math::gather::gather(self, indices, axis) + } + + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { + math::gather_nd::gather_nd(self, indices, batch_dims) + } + + fn nonzero(self: @Tensor) -> Tensor { + core_tensor::nonzero(self) + } + + fn squeeze(self: @Tensor, axes: Option>) -> Tensor { + core_tensor::squeeze(self, axes) + } + + fn unsqueeze(self: @Tensor, axes: Span) -> Tensor { + core_tensor::unsqueeze(self, axes) + } + + fn sign(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn clip( + self: @Tensor, min: Option, max: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn and(self: @Tensor, other: @Tensor) -> Tensor { + math::and::and(self, other) + } + + fn identity(self: @Tensor) -> Tensor { + core_tensor::identity(self) + } + + fn where( + self: @Tensor, x: @Tensor, y: @Tensor + ) -> Tensor { + panic(array!['not supported!']) + } + + fn bitwise_and(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn bitwise_xor(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn bitwise_or(self: @Tensor, other: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn round(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn reduce_l1(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_l1::reduce_l1(self, axis, keepdims) + } + + fn array_feature_extractor( + self: @Tensor, indices: Tensor + ) -> Tensor { + ml::array_feature_extractor::array_feature_extractor(*self, indices) + } + + fn binarizer(self: @Tensor, threshold: Option) -> Tensor { + panic(array!['not supported!']) + } + + fn reduce_sum_square( + self: @Tensor, axis: usize, keepdims: bool + ) -> Tensor { + math::reduce_sum_square::reduce_sum_square(self, axis, keepdims) + } + + fn reduce_l2(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_l2::reduce_l2_complex(self, axis, keepdims) + } + + fn trilu(self: @Tensor, upper: bool, k: i64) -> Tensor { + linalg::trilu::trilu(self, upper, k) + } + + fn scatter( + self: @Tensor, + updates: Tensor, + indices: Tensor, + axis: Option, + reduction: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn not(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + + fn gather_elements( + self: @Tensor, indices: Tensor, axis: Option + ) -> Tensor { + math::gather_elements::gather_elements(self, indices, axis) + } + + fn sequence_length(self: Array>) -> Tensor { + math::sequence_length::sequence_length(self) + } + + fn shrink( + self: Tensor, bias: Option, lambd: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn sequence_at(sequence: Array>, position: Tensor) -> Tensor { + math::sequence_at::sequence_at(sequence, position) + } + + fn sequence_construct(tensors: Array>) -> Array> { + math::sequence_construct::sequence_construct(tensors) + } + + + fn sequence_empty() -> Array> { + math::sequence_empty::sequence_empty::() + } + + fn reduce_mean( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + math::reduce_mean::reduce_mean(self, axes, keepdims, noop_with_empty_axes) + } + + fn reduce_min( + self: @Tensor, + axes: Option>, + keepdims: Option, + noop_with_empty_axes: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn pow(self: @Tensor, other: @Tensor) -> Tensor { + math::pow::pow(self, other) + } + + fn sequence_erase( + sequence: Array>, position: Option> + ) -> Array> { + math::sequence_erase::sequence_erase(sequence, position) + } + + fn sequence_insert( + self: Array>, tensor: @Tensor, position: Option> + ) -> Array> { + math::sequence_insert::sequence_insert(self, tensor, position) + } + + fn is_inf( + self: @Tensor, detect_negative: Option, detect_positive: Option + ) -> Tensor { + panic(array!['not supported!']) + } + + fn is_nan(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn concat_from_sequence( + sequence: Array>, axis: i32, new_axis: Option + ) -> Tensor { + math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) + } + + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { + math::reduce_log_sum::reduce_log_sum(self, axis, keepdims) + } + + + fn erf(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } + + fn unique( + self: @Tensor, axis: Option, sorted: Option + ) -> (Tensor, Tensor, Tensor, Tensor) { + panic(array!['not supported!']) + } +} + +/// Implements addition for `Tensor` using the `Add` trait. +impl Complex64TensorAdd of Add> { + /// Adds two `Tensor` instances element-wise. + /// + /// # Arguments + /// * `lhs` - The first tensor. + /// * `rhs` - The second tensor. + /// + /// # Returns + /// * A `Tensor` instance representing the result of the element-wise addition. + fn add(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::add(@lhs, @rhs) + } +} + +/// Implements subtraction for `Tensor` using the `Sub` trait. +impl Complex64TensorSub of Sub> { + /// Subtracts two `Tensor` instances element-wise. + /// + /// # Arguments + /// * `lhs` - The first tensor. + /// * `rhs` - The second tensor. + /// + /// # Returns + /// * A `Tensor` instance representing the result of the element-wise subtraction. + fn sub(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::sub(@lhs, @rhs) + } +} + +/// Implements multiplication for `Tensor` using the `Mul` trait. +impl Complex64TensorMul of Mul> { + /// Multiplies two `Tensor` instances element-wise. + /// + /// # Arguments + /// * `lhs` - The first tensor. + /// * `rhs` - The second tensor. + /// + /// # Returns + /// * A `Tensor` instance representing the result of the element-wise multiplication. + fn mul(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::mul(@lhs, @rhs) + } +} + +/// Implements division for `Tensor` using the `Div` trait. +impl Complex64TensorDiv of Div> { + /// Divides two `Tensor` instances element-wise. + /// + /// # Arguments + /// * `lhs` - The first tensor. + /// * `rhs` - The second tensor. + /// + /// # Returns + /// * A `Tensor` instance representing the result of the element-wise division. + fn div(lhs: Tensor, rhs: Tensor) -> Tensor { + math::arithmetic::div(@lhs, @rhs) + } +} + +/// Implements partial equal for two `Tensor` using the `complex64` trait. +impl Complex64TensorPartialEq of PartialEq> { + fn eq(lhs: @Tensor, rhs: @Tensor) -> bool { + tensor_eq(*lhs, *rhs) + } + + fn ne(lhs: @Tensor, rhs: @Tensor) -> bool { + !tensor_eq(*lhs, *rhs) + } +} + + +// Internals + +fn eq(lhs: @complex64, rhs: @complex64) -> bool { + let eq = (*lhs.real == *rhs.real) && (*lhs.img == *rhs.img); + eq +} + +fn tensor_eq(mut lhs: Tensor, mut rhs: Tensor,) -> bool { + let mut is_eq = true; + + loop { + if lhs.shape.len() == 0 || !is_eq { + break; + } + + is_eq = lhs.shape.pop_front().unwrap() == rhs.shape.pop_front().unwrap(); + }; + + if !is_eq { + return false; + } + + loop { + if lhs.data.len() == 0 || !is_eq { + break; + } + + is_eq = eq(lhs.data.pop_front().unwrap(), rhs.data.pop_front().unwrap()); + }; + + return is_eq; +} + diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index ccaf5903d..ce489a45a 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -520,10 +520,12 @@ impl FP16x16Tensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_log_sum::reduce_log_sum(self, axis, keepdims) } diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index dc32202ed..1ea152edf 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -486,10 +486,12 @@ impl FP16x16WTensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_log_sum::reduce_log_sum(self, axis, keepdims) } diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 9100d6f82..5e9b5512a 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -521,10 +521,12 @@ impl FP32x32Tensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_log_sum::reduce_log_sum(self, axis, keepdims) } diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index ee6441058..2b5142789 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -522,10 +522,12 @@ impl FP64x64Tensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_log_sum::reduce_log_sum(self, axis, keepdims) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index 17a601f7b..18cd91af7 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -520,10 +520,12 @@ impl FP8x23Tensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_log_sum::reduce_log_sum(self, axis, keepdims) } diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index a7d19901b..a1f9bcbdf 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -473,10 +473,12 @@ impl FP8x23WTensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { math::reduce_log_sum::reduce_log_sum(self, axis, keepdims) } diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index a987b0633..c8753c5a9 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -517,10 +517,12 @@ impl I32Tensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { panic(array!['not supported!']) } diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index 8c1e2fd32..fa67a7950 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -515,10 +515,12 @@ impl I8Tensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { panic(array!['not supported!']) } diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index 5b2058401..9a90ef883 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -458,10 +458,12 @@ impl U32Tensor of TensorTrait { math::concat_from_sequence::concat_from_sequence(sequence, axis, new_axis) } - fn gather_nd(self: @Tensor, indices: Tensor, batch_dims: Option) -> Tensor { + fn gather_nd( + self: @Tensor, indices: Tensor, batch_dims: Option + ) -> Tensor { math::gather_nd::gather_nd(self, indices, batch_dims) } - + fn reduce_log_sum(self: @Tensor, axis: usize, keepdims: bool) -> Tensor { panic(array!['not supported!']) } diff --git a/src/operators/tensor/math/acos.cairo b/src/operators/tensor/math/acos.cairo index 1e6fbcfe8..c36260752 100644 --- a/src/operators/tensor/math/acos.cairo +++ b/src/operators/tensor/math/acos.cairo @@ -2,6 +2,7 @@ use core::array::ArrayTrait; use core::array::SpanTrait; use core::option::OptionTrait; +use orion::numbers::NumberTrait; use orion::numbers::fixed_point::core::FixedTrait; use orion::operators::tensor::core::{Tensor, TensorTrait}; @@ -9,10 +10,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn acos< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/acosh.cairo b/src/operators/tensor/math/acosh.cairo index 78649e620..f486d5609 100644 --- a/src/operators/tensor/math/acosh.cairo +++ b/src/operators/tensor/math/acosh.cairo @@ -11,10 +11,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn acosh< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/asin.cairo b/src/operators/tensor/math/asin.cairo index 7018edcc6..b33132797 100644 --- a/src/operators/tensor/math/asin.cairo +++ b/src/operators/tensor/math/asin.cairo @@ -2,6 +2,7 @@ use core::array::ArrayTrait; use core::array::SpanTrait; use core::option::OptionTrait; +use orion::numbers::NumberTrait; use orion::numbers::fixed_point::core::FixedTrait; use orion::operators::tensor::core::{Tensor, TensorTrait}; @@ -9,10 +10,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn asin< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/asinh.cairo b/src/operators/tensor/math/asinh.cairo index 28fcb467f..8d015554d 100644 --- a/src/operators/tensor/math/asinh.cairo +++ b/src/operators/tensor/math/asinh.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn asinh< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/atan.cairo b/src/operators/tensor/math/atan.cairo index 25e991700..b5f93eb1c 100644 --- a/src/operators/tensor/math/atan.cairo +++ b/src/operators/tensor/math/atan.cairo @@ -11,10 +11,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn atan< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/cos.cairo b/src/operators/tensor/math/cos.cairo index aad6ea925..5abeb327d 100644 --- a/src/operators/tensor/math/cos.cairo +++ b/src/operators/tensor/math/cos.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn cos< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/cosh.cairo b/src/operators/tensor/math/cosh.cairo index f3f15a284..08c434f5a 100644 --- a/src/operators/tensor/math/cosh.cairo +++ b/src/operators/tensor/math/cosh.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn cosh< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/exp.cairo b/src/operators/tensor/math/exp.cairo index a7e04dca1..889082d56 100644 --- a/src/operators/tensor/math/exp.cairo +++ b/src/operators/tensor/math/exp.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn exp< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/gather_nd.cairo b/src/operators/tensor/math/gather_nd.cairo index 120eff5be..737a4fe32 100644 --- a/src/operators/tensor/math/gather_nd.cairo +++ b/src/operators/tensor/math/gather_nd.cairo @@ -14,12 +14,7 @@ use orion::operators::tensor::U32TensorPartialEq; use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; /// Cf: TensorTrait::gather_nd docstring -fn gather_nd< - T, - impl TTensorTrait: TensorTrait, - impl TCopy: Copy, - impl TDrop: Drop, ->( +fn gather_nd, impl TCopy: Copy, impl TDrop: Drop,>( self: @Tensor, indices: Tensor, batch_dims: Option ) -> Tensor { let batch_dims = match batch_dims { @@ -29,19 +24,22 @@ fn gather_nd< let data_rank = (*self.shape).len(); let indices_rank = (indices.shape).len(); - assert((data_rank >= 1 ) & (indices_rank >= 1), 'rank must > 1'); - + assert((data_rank >= 1) & (indices_rank >= 1), 'rank must > 1'); + let mut data_shape = *self.shape; let mut indices_shape = indices.shape; let mut data_shape_clone = data_shape.clone(); let mut indices_shape_clone = indices_shape.clone(); let indices_shape_last = indices_shape_clone.pop_back().unwrap(); - assert((*indices_shape_last >= 1) & (*indices_shape_last <= data_rank-batch_dims), 'check indices'); + assert( + (*indices_shape_last >= 1) & (*indices_shape_last <= data_rank - batch_dims), + 'check indices' + ); let mut batch_dims_shape = ArrayTrait::new(); let mut output_shape = ArrayTrait::new(); - let mut index_data = ArrayTrait::new(); + let mut index_data = ArrayTrait::new(); let mut output_data = ArrayTrait::new(); let mut batch_dims_size = batch_dims; @@ -51,7 +49,7 @@ fn gather_nd< let mut ind = 0; loop { if (ind == batch_dims) { - break(); + break (); } match indices_shape_clone.pop_front() { Option::Some(val) => { @@ -65,17 +63,14 @@ fn gather_nd< loop { match indices_shape_clone.pop_front() { - Option::Some(val) => { - batch_dims_shape.append(*val); - }, + Option::Some(val) => { batch_dims_shape.append(*val); }, Option::None(_) => { break; } }; }; if (*indices_shape_last == data_rank - batch_dims) { output_shape = batch_dims_shape; - } - else { + } else { let mut ind = 0; let mut multiple = 1; output_shape = batch_dims_shape; @@ -136,16 +131,18 @@ fn gather_nd< match data_indices.pop_front() { Option::Some(val) => { let index = ind % *indices_shape_last; - let incr= total_data_len * (ind / breaker); + let incr = total_data_len * (ind / breaker); result += (*val * total_data_len / *multiple_data_len.at(index)); ind += 1; - if (index == *indices_shape_last-1) { - let mut data_ind:usize = result ; + if (index == *indices_shape_last - 1) { + let mut data_ind: usize = result; loop { - if data_ind == result + incrementer { break; } + if data_ind == result + incrementer { + break; + } index_data.append(data_ind + incr); - data_ind+=1; + data_ind += 1; }; result = 0; }; @@ -156,13 +153,11 @@ fn gather_nd< loop { match index_data.pop_front() { - Option::Some(val) => { - output_data.append(*self.data[val]); - }, + Option::Some(val) => { output_data.append(*self.data[val]); }, Option::None(_) => { break; } }; }; let mut output_tensor = TensorTrait::::new(output_shape.span(), output_data.span()); return output_tensor; -} \ No newline at end of file +} diff --git a/src/operators/tensor/math/log.cairo b/src/operators/tensor/math/log.cairo index 817ca135a..e55291fca 100644 --- a/src/operators/tensor/math/log.cairo +++ b/src/operators/tensor/math/log.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn log< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/reduce_l2.cairo b/src/operators/tensor/math/reduce_l2.cairo index b18fa704c..d03499fec 100644 --- a/src/operators/tensor/math/reduce_l2.cairo +++ b/src/operators/tensor/math/reduce_l2.cairo @@ -11,12 +11,11 @@ use orion::numbers::fixed_point::core::FixedTrait; fn square< T, MAG, - impl FTensorTrait: TensorTrait, - impl FFixed: FixedTrait, - impl FNumber: NumberTrait, + impl TTensorTrait: TensorTrait, + impl TNumber: NumberTrait, impl TMul: Mul, - impl FCopy: Copy, - impl FDrop: Drop, + impl TCopy: Copy, + impl TDrop: Drop, >( self: @Tensor ) -> Tensor { @@ -41,7 +40,6 @@ fn reduce_l2< T, MAG, impl TTensor: TensorTrait, - impl FFixed: FixedTrait, impl TNumber: NumberTrait, impl TMul: Mul, impl TCopy: Copy, @@ -53,3 +51,23 @@ fn reduce_l2< let tensor_square_sum = tensor_square.reduce_sum(axis: axis, keepdims: keepdims); return tensor_square_sum.sqrt(); } + +fn reduce_l2_complex< + T, + MAG, + impl TTensor: TensorTrait, + impl TNumber: NumberTrait, + impl TMul: Mul, + impl TCopy: Copy, + impl TDrop: Drop, + impl TPrint: PrintTrait +>( + 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); + + return tensor_square_sum.sqrt(); +} + diff --git a/src/operators/tensor/math/sin.cairo b/src/operators/tensor/math/sin.cairo index b1471d77e..cc810eab7 100644 --- a/src/operators/tensor/math/sin.cairo +++ b/src/operators/tensor/math/sin.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn sin< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/sinh.cairo b/src/operators/tensor/math/sinh.cairo index 5d3ef828b..7c3373288 100644 --- a/src/operators/tensor/math/sinh.cairo +++ b/src/operators/tensor/math/sinh.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn sinh< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/sqrt.cairo b/src/operators/tensor/math/sqrt.cairo index 84d23c150..f3111bed9 100644 --- a/src/operators/tensor/math/sqrt.cairo +++ b/src/operators/tensor/math/sqrt.cairo @@ -11,10 +11,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn sqrt< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/src/operators/tensor/math/tanh.cairo b/src/operators/tensor/math/tanh.cairo index b94fba485..f6f3eb6e1 100644 --- a/src/operators/tensor/math/tanh.cairo +++ b/src/operators/tensor/math/tanh.cairo @@ -12,10 +12,10 @@ use orion::operators::tensor::core::{Tensor, TensorTrait}; fn tanh< T, MAG, - impl FFixedTrait: FixedTrait, - impl FTensor: TensorTrait, - impl FCopy: Copy, - impl FDrop: Drop, + impl TNumberTrait: NumberTrait, + impl TTensor: TensorTrait, + impl TCopy: Copy, + impl TDrop: Drop, >( mut self: Tensor ) -> Tensor { diff --git a/tests/lib.cairo b/tests/lib.cairo index f5cecb77d..c408347ef 100644 --- a/tests/lib.cairo +++ b/tests/lib.cairo @@ -5,3 +5,4 @@ mod nodes; mod ml; mod operators; + diff --git a/tests/nodes.cairo b/tests/nodes.cairo index c7155e942..57312ec2f 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -850,3 +850,4 @@ mod gather_nd_i8_3d_batch_dims1; mod gather_nd_u32_default; mod gather_nd_u32_batch_dims1; mod gather_nd_u32_batch_dims2; +mod reduce_l2_complex64_axis_0; diff --git a/tests/nodes/gather_nd_fp16x16_3d_batch_dims1.cairo b/tests/nodes/gather_nd_fp16x16_3d_batch_dims1.cairo index d2c0b80dd..025cc8261 100644 --- a/tests/nodes/gather_nd_fp16x16_3d_batch_dims1.cairo +++ b/tests/nodes/gather_nd_fp16x16_3d_batch_dims1.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_fp16x16_3d_batch_dims1() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(1)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(1)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_fp16x16_3d_batch_dims2.cairo b/tests/nodes/gather_nd_fp16x16_3d_batch_dims2.cairo index 507847851..677a40f6a 100644 --- a/tests/nodes/gather_nd_fp16x16_3d_batch_dims2.cairo +++ b/tests/nodes/gather_nd_fp16x16_3d_batch_dims2.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_fp16x16_3d_batch_dims2() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(2)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(2)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_fp16x16_3d_default.cairo b/tests/nodes/gather_nd_fp16x16_3d_default.cairo index ae4609a66..b8339a0d2 100644 --- a/tests/nodes/gather_nd_fp16x16_3d_default.cairo +++ b/tests/nodes/gather_nd_fp16x16_3d_default.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_fp16x16_3d_default() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(0)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(0)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_fp8x23_3d_batch_dims1.cairo b/tests/nodes/gather_nd_fp8x23_3d_batch_dims1.cairo index b9a083796..65980d91f 100644 --- a/tests/nodes/gather_nd_fp8x23_3d_batch_dims1.cairo +++ b/tests/nodes/gather_nd_fp8x23_3d_batch_dims1.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_fp8x23_3d_batch_dims1() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(1)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(1)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_fp8x23_3d_batch_dims2.cairo b/tests/nodes/gather_nd_fp8x23_3d_batch_dims2.cairo index 5e42ca893..48c812baf 100644 --- a/tests/nodes/gather_nd_fp8x23_3d_batch_dims2.cairo +++ b/tests/nodes/gather_nd_fp8x23_3d_batch_dims2.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_fp8x23_3d_batch_dims2() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(2)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(2)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_fp8x23_3d_default.cairo b/tests/nodes/gather_nd_fp8x23_3d_default.cairo index 12b6408e0..342cd2b72 100644 --- a/tests/nodes/gather_nd_fp8x23_3d_default.cairo +++ b/tests/nodes/gather_nd_fp8x23_3d_default.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_fp8x23_3d_default() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(0)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(0)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_i32_3d_batch_dims1.cairo b/tests/nodes/gather_nd_i32_3d_batch_dims1.cairo index 243b0ca16..318ccd62e 100644 --- a/tests/nodes/gather_nd_i32_3d_batch_dims1.cairo +++ b/tests/nodes/gather_nd_i32_3d_batch_dims1.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_i32_3d_batch_dims1() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(1)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(1)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_i32_3d_batch_dims2.cairo b/tests/nodes/gather_nd_i32_3d_batch_dims2.cairo index d11370b94..177c8e40f 100644 --- a/tests/nodes/gather_nd_i32_3d_batch_dims2.cairo +++ b/tests/nodes/gather_nd_i32_3d_batch_dims2.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_i32_3d_batch_dims2() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(2)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(2)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_i32_3d_default.cairo b/tests/nodes/gather_nd_i32_3d_default.cairo index 35c054093..97212f737 100644 --- a/tests/nodes/gather_nd_i32_3d_default.cairo +++ b/tests/nodes/gather_nd_i32_3d_default.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_i32_3d_default() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(0)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(0)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_i8_3d_batch_dims1.cairo b/tests/nodes/gather_nd_i8_3d_batch_dims1.cairo index ae83a8c7d..f849c8677 100644 --- a/tests/nodes/gather_nd_i8_3d_batch_dims1.cairo +++ b/tests/nodes/gather_nd_i8_3d_batch_dims1.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_i8_3d_batch_dims1() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(1)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(1)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_i8_3d_default.cairo b/tests/nodes/gather_nd_i8_3d_default.cairo index 73e1d91b2..ff7ad9252 100644 --- a/tests/nodes/gather_nd_i8_3d_default.cairo +++ b/tests/nodes/gather_nd_i8_3d_default.cairo @@ -18,7 +18,7 @@ fn test_gather_nd_i8_3d_default() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(0)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(0)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_u32_batch_dims1.cairo b/tests/nodes/gather_nd_u32_batch_dims1.cairo index 0428ec1d5..860675f66 100644 --- a/tests/nodes/gather_nd_u32_batch_dims1.cairo +++ b/tests/nodes/gather_nd_u32_batch_dims1.cairo @@ -16,7 +16,7 @@ fn test_gather_nd_u32_batch_dims1() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(1)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(1)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_u32_batch_dims2.cairo b/tests/nodes/gather_nd_u32_batch_dims2.cairo index 39857ef1d..f0662be99 100644 --- a/tests/nodes/gather_nd_u32_batch_dims2.cairo +++ b/tests/nodes/gather_nd_u32_batch_dims2.cairo @@ -16,7 +16,7 @@ fn test_gather_nd_u32_batch_dims2() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(2)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(2)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/gather_nd_u32_default.cairo b/tests/nodes/gather_nd_u32_default.cairo index f55b49d5e..be6edd699 100644 --- a/tests/nodes/gather_nd_u32_default.cairo +++ b/tests/nodes/gather_nd_u32_default.cairo @@ -16,7 +16,7 @@ fn test_gather_nd_u32_default() { let input_1 = input_1::input_1(); let z_0 = output_0::output_0(); - let y_0 = input_0.gather_nd(indices:input_1, batch_dims:Option::Some(0)); + let y_0 = input_0.gather_nd(indices: input_1, batch_dims: Option::Some(0)); assert_eq(y_0, z_0); } diff --git a/tests/nodes/reduce_l2_complex64_axis_0.cairo b/tests/nodes/reduce_l2_complex64_axis_0.cairo new file mode 100644 index 000000000..bb1391263 --- /dev/null +++ b/tests/nodes/reduce_l2_complex64_axis_0.cairo @@ -0,0 +1,24 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::Complex64Tensor; +use core::array::{ArrayTrait, SpanTrait}; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::Complex64TensorPartialEq; + +use orion::numbers::complex_number::complex64::Complex64Print; +use orion::numbers::{NumberTrait, complex64}; + +#[test] +#[available_gas(2000000000)] +fn test_reduce_l2_complex64_axis_0() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.reduce_l2(0, true); + + assert_eq(y_0, z_0); +} + diff --git a/tests/nodes/reduce_l2_complex64_axis_0/input_0.cairo b/tests/nodes/reduce_l2_complex64_axis_0/input_0.cairo new file mode 100644 index 000000000..6d7281bfb --- /dev/null +++ b/tests/nodes/reduce_l2_complex64_axis_0/input_0.cairo @@ -0,0 +1,56 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::Complex64Tensor; +use orion::numbers::{NumberTrait, complex64}; +use orion::numbers::{FixedTrait, FP64x64}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(3); + + let mut data = ArrayTrait::new(); + data + .append( + complex64 { + real: FP64x64 { mag: 18446744073709551616, sign: false }, + img: FP64x64 { mag: 36893488147419103232, sign: false } + } + ); + data + .append( + complex64 { + real: FP64x64 { mag: 36893488147419103232, sign: false }, + img: FP64x64 { mag: 18446744073709551616, sign: true } + } + ); + data + .append( + complex64 { + real: FP64x64 { mag: 55340232221128654848, sign: false }, + img: FP64x64 { mag: 55340232221128654848, sign: true } + } + ); + data + .append( + complex64 { + real: FP64x64 { mag: 55340232221128654848, sign: false }, + img: FP64x64 { mag: 36893488147419103232, sign: true } + } + ); + data + .append( + complex64 { + real: FP64x64 { mag: 55340232221128654848, sign: false }, + img: FP64x64 { mag: 92233720368547758080, sign: false } + } + ); + data + .append( + complex64 { + real: FP64x64 { mag: 73786976294838206464, sign: false }, + img: FP64x64 { mag: 18446744073709551616, sign: true } + } + ); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/reduce_l2_complex64_axis_0/output_0.cairo b/tests/nodes/reduce_l2_complex64_axis_0/output_0.cairo new file mode 100644 index 000000000..ee6564432 --- /dev/null +++ b/tests/nodes/reduce_l2_complex64_axis_0/output_0.cairo @@ -0,0 +1,35 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::Complex64Tensor; +use orion::numbers::{NumberTrait, complex64}; +use orion::numbers::{FixedTrait, FP64x64}; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(3); + + let mut data = ArrayTrait::new(); + data + .append( + complex64 { + real: FP64x64 { mag: 78262906948318920704, sign: false }, + img: FP64x64 { mag: 0, sign: false } + } + ); + data + .append( + complex64 { + real: FP64x64 { mag: 115199879809953955840, sign: false }, + img: FP64x64 { mag: 0, sign: false } + } + ); + data + .append( + complex64 { + real: FP64x64 { mag: 109132409670155108352, sign: false }, + img: FP64x64 { mag: 0, sign: false } + } + ); + TensorTrait::new(shape.span(), data.span()) +}