From 070c6ee74756f671f1deeb060bc5f2bef8f72479 Mon Sep 17 00:00:00 2001 From: Canance Chan Date: Fri, 15 Mar 2024 01:41:21 +0000 Subject: [PATCH] Feat: det --- docs/framework/operators/tensor/README.md | 1 + docs/framework/operators/tensor/tensor.det.md | 56 ++++++++++ nodegen/node/det.py | 65 +++++++++++ src/operators/tensor/core.cairo | 58 ++++++++++ .../tensor/implementations/tensor_bool.cairo | 4 + .../implementations/tensor_complex64.cairo | 4 + .../implementations/tensor_fp16x16.cairo | 4 + .../implementations/tensor_fp16x16wide.cairo | 4 + .../implementations/tensor_fp32x32.cairo | 4 + .../implementations/tensor_fp64x64.cairo | 4 + .../implementations/tensor_fp8x23.cairo | 4 + .../implementations/tensor_fp8x23wide.cairo | 4 + .../tensor/implementations/tensor_i32.cairo | 4 + .../tensor/implementations/tensor_i8.cairo | 4 + .../tensor/implementations/tensor_u32.cairo | 4 + src/operators/tensor/math.cairo | 1 + src/operators/tensor/math/det.cairo | 103 ++++++++++++++++++ tests/nodes.cairo | 4 + tests/nodes/det_fp16x16.cairo | 20 ++++ tests/nodes/det_fp16x16/input_0.cairo | 18 +++ tests/nodes/det_fp16x16/output_0.cairo | 13 +++ tests/nodes/det_fp8x23.cairo | 20 ++++ tests/nodes/det_fp8x23/input_0.cairo | 46 ++++++++ tests/nodes/det_fp8x23/output_0.cairo | 14 +++ tests/nodes/det_i32.cairo | 20 ++++ tests/nodes/det_i32/input_0.cairo | 32 ++++++ tests/nodes/det_i32/output_0.cairo | 14 +++ tests/nodes/det_i8.cairo | 20 ++++ tests/nodes/det_i8/input_0.cairo | 17 +++ tests/nodes/det_i8/output_0.cairo | 15 +++ 30 files changed, 581 insertions(+) create mode 100644 docs/framework/operators/tensor/tensor.det.md create mode 100644 nodegen/node/det.py create mode 100644 src/operators/tensor/math/det.cairo create mode 100644 tests/nodes/det_fp16x16.cairo create mode 100644 tests/nodes/det_fp16x16/input_0.cairo create mode 100644 tests/nodes/det_fp16x16/output_0.cairo create mode 100644 tests/nodes/det_fp8x23.cairo create mode 100644 tests/nodes/det_fp8x23/input_0.cairo create mode 100644 tests/nodes/det_fp8x23/output_0.cairo create mode 100644 tests/nodes/det_i32.cairo create mode 100644 tests/nodes/det_i32/input_0.cairo create mode 100644 tests/nodes/det_i32/output_0.cairo create mode 100644 tests/nodes/det_i8.cairo create mode 100644 tests/nodes/det_i8/input_0.cairo create mode 100644 tests/nodes/det_i8/output_0.cairo diff --git a/docs/framework/operators/tensor/README.md b/docs/framework/operators/tensor/README.md index fe2995096..72c8bcb3e 100644 --- a/docs/framework/operators/tensor/README.md +++ b/docs/framework/operators/tensor/README.md @@ -132,6 +132,7 @@ use orion::operators::tensor::TensorTrait; | [`tensor.dynamic_quantize_linear`](tensor.dynamic\_quantize\_linear.md) | Computes the Scale, Zero Point and FP32->8Bit conversion of FP32 Input data. | | [`tensor.scatter_nd`](tensor.scatter\_nd.md) | The output of the operation is produced by creating a copy of the input data, and then updating its value to values specified by updates at specific index positions specified by indices. Its output shape is the same as the shape of data | | [`tensor.label_encoder`](tensor.label\_encoder.md) | Maps each element in the input tensor to another value. | +| [`tensor.det`](tensor.det.md) | Det calculates determinant of a square matrix or batches of square matrices. | ## Arithmetic Operations diff --git a/docs/framework/operators/tensor/tensor.det.md b/docs/framework/operators/tensor/tensor.det.md new file mode 100644 index 000000000..55a2b515a --- /dev/null +++ b/docs/framework/operators/tensor/tensor.det.md @@ -0,0 +1,56 @@ +# TensorTrait::det + +```rust + fn det(tensor: @Tensor) -> T; +``` + +Det calculates determinant of a square matrix or batches of square matrices. Det takes one input tensor of shape [*, M, M], where * is zero or more batch dimensions, and the inner-most 2 dimensions form square matrices. The output is a tensor of shape [*], containing the determinants of all input submatrices. + +## Args + +* `tensor`(`@Tensor`) - The input tensor of shape [*, M, M]. + +## Returns + +* The output is a tensor of shape [*] + +## Examples + +```rust +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::I32TensorPartialEq; + +fn example() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(2); + shape.append(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + data.append(2); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + let input_0 = TensorTrait::new(shape.span(), data.span()); + + return input_0.det(); +} +>>> [0, -3] +``` \ No newline at end of file diff --git a/nodegen/node/det.py b/nodegen/node/det.py new file mode 100644 index 000000000..0563aa137 --- /dev/null +++ b/nodegen/node/det.py @@ -0,0 +1,65 @@ +import numpy as np +from nodegen.node import RunAll +from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl, Trait, get_data_statement + + +class Det(RunAll): + + @staticmethod + # We test here with fp8x23 implementation. + def fp8x23(): + x = np.random.randint(-2, 2, (2, 4, 4)).astype(np.float64) + y = np.linalg.det(x) + + 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 = "det_fp8x23" + make_test([x], y, f"input_0.det()", name) + + @staticmethod + # We test here with fp16x16 implementation. + def fp16x16(): + x = np.random.randint(-3, 3, (1, 2, 2)).astype(np.float64) + y = np.linalg.det(x) + + 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 = "det_fp16x16" + make_test([x], y, f"input_0.det()", name) + + @staticmethod + # We test here with i8 implementation. + def i8(): + x = np.random.randint(0, 6, (3, 1, 1)).astype(np.int8) + y = np.linalg.det(x) + + x = Tensor(Dtype.I8, x.shape, x.flatten()) + y = Tensor(Dtype.I8, y.shape, y.flatten()) + + name = "det_i8" + make_test([x], y, f"input_0.det()", name) + + @staticmethod + # We test here with i32 implementation. + def i32(): + x = np.array([[[1, 2, 3], + [4, 5, 6], + [7, 8, 9]], + [[2, 2, 3], + [4, 5, 6], + [7, 8, 9]] + ]) + y = np.linalg.det(x) + + x = Tensor(Dtype.I32, x.shape, x.flatten()) + y = Tensor(Dtype.I32, y.shape, y.flatten()) + + name = "det_i32" + make_test([x], y, f"input_0.det()", name) + \ No newline at end of file diff --git a/src/operators/tensor/core.cairo b/src/operators/tensor/core.cairo index 0d21a4de3..7b4c361bb 100644 --- a/src/operators/tensor/core.cairo +++ b/src/operators/tensor/core.cairo @@ -131,6 +131,7 @@ impl TensorSerde, impl TDrop: Drop> of Serde8Bit conversion of FP32 Input data. /// scatter_nd - The output of the operation is produced by creating a copy of the input data, and then updating its value to values specified by updates at specific index positions specified by indices. Its output shape is the same as the shape of data /// label_encoder - Maps each element in the input tensor to another value. +/// det - Det calculates determinant of a square matrix or batches of square matrices. trait TensorTrait { /// # tensor.new /// @@ -5850,6 +5851,63 @@ trait TensorTrait { values: Option>, values_tensor: Option> ) -> Tensor; + /// # TensorTrait::det + /// + /// ```rust + /// fn det(tensor: @Tensor) -> T; + /// ``` + /// + /// Det calculates determinant of a square matrix or batches of square matrices. Det takes one input tensor of shape [*, M, M], where * is zero or more batch dimensions, and the inner-most 2 dimensions form square matrices. The output is a tensor of shape [*], containing the determinants of all input submatrices. + /// + /// ## Args + /// + /// * `tensor`(`@Tensor`) - The input tensor of shape [*, M, M]. + /// + /// ## Returns + /// + /// * The output is a tensor of shape [*] + /// + /// ## Examples + /// + /// ```rust + /// use orion::operators::tensor::{I32Tensor, I32TensorAdd}; + /// use core::array::{ArrayTrait, SpanTrait}; + /// use orion::operators::tensor::{TensorTrait, Tensor}; + /// use orion::utils::{assert_eq, assert_seq_eq}; + /// use orion::operators::tensor::I32TensorPartialEq; + /// + /// fn example() -> Tensor { + /// let mut shape = ArrayTrait::::new(); + /// shape.append(2); + /// shape.append(3); + /// shape.append(3); + /// + /// let mut data = ArrayTrait::new(); + /// data.append(1); + /// data.append(2); + /// data.append(3); + /// data.append(4); + /// data.append(5); + /// data.append(6); + /// data.append(7); + /// data.append(8); + /// data.append(9); + /// data.append(2); + /// data.append(2); + /// data.append(3); + /// data.append(4); + /// data.append(5); + /// data.append(6); + /// data.append(7); + /// data.append(8); + /// data.append(9); + /// let input_0 = TensorTrait::new(shape.span(), data.span()); + /// + /// return input_0.det(); + /// } + /// >>> [0, -3] + /// ``` + fn det(self: @Tensor) -> Tensor; } /// Cf: TensorTrait::new docstring diff --git a/src/operators/tensor/implementations/tensor_bool.cairo b/src/operators/tensor/implementations/tensor_bool.cairo index 612a397cc..f7e673a29 100644 --- a/src/operators/tensor/implementations/tensor_bool.cairo +++ b/src/operators/tensor/implementations/tensor_bool.cairo @@ -547,6 +547,10 @@ impl BoolTensor of TensorTrait { ) -> Tensor { panic(array!['not supported!']) } + + fn det(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } } /// Implements partial equal for two `Tensor` using the `PartialEq` trait. diff --git a/src/operators/tensor/implementations/tensor_complex64.cairo b/src/operators/tensor/implementations/tensor_complex64.cairo index c9c31ae23..da49681b3 100644 --- a/src/operators/tensor/implementations/tensor_complex64.cairo +++ b/src/operators/tensor/implementations/tensor_complex64.cairo @@ -585,6 +585,10 @@ impl Complex64Tensor of TensorTrait { ) -> Tensor { panic(array!['not supported!']) } + + fn det(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp16x16.cairo b/src/operators/tensor/implementations/tensor_fp16x16.cairo index a37ed0442..c66890034 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16.cairo @@ -639,6 +639,10 @@ impl FP16x16Tensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo index 2003b28ff..022c8c2a9 100644 --- a/src/operators/tensor/implementations/tensor_fp16x16wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp16x16wide.cairo @@ -599,6 +599,10 @@ impl FP16x16WTensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp32x32.cairo b/src/operators/tensor/implementations/tensor_fp32x32.cairo index 4870226a1..a99071b1a 100644 --- a/src/operators/tensor/implementations/tensor_fp32x32.cairo +++ b/src/operators/tensor/implementations/tensor_fp32x32.cairo @@ -635,6 +635,10 @@ impl FP32x32Tensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp64x64.cairo b/src/operators/tensor/implementations/tensor_fp64x64.cairo index 3a7214d18..d921427e6 100644 --- a/src/operators/tensor/implementations/tensor_fp64x64.cairo +++ b/src/operators/tensor/implementations/tensor_fp64x64.cairo @@ -635,6 +635,10 @@ impl FP64x64Tensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp8x23.cairo b/src/operators/tensor/implementations/tensor_fp8x23.cairo index b4a26d749..ca203871e 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23.cairo @@ -633,6 +633,10 @@ impl FP8x23Tensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo index 06a297b69..a19068c56 100644 --- a/src/operators/tensor/implementations/tensor_fp8x23wide.cairo +++ b/src/operators/tensor/implementations/tensor_fp8x23wide.cairo @@ -576,6 +576,10 @@ impl FP8x23WTensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_i32.cairo b/src/operators/tensor/implementations/tensor_i32.cairo index 296876516..8e781787f 100644 --- a/src/operators/tensor/implementations/tensor_i32.cairo +++ b/src/operators/tensor/implementations/tensor_i32.cairo @@ -599,6 +599,10 @@ impl I32Tensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_i8.cairo b/src/operators/tensor/implementations/tensor_i8.cairo index 42d807c68..bb1feb9ca 100644 --- a/src/operators/tensor/implementations/tensor_i8.cairo +++ b/src/operators/tensor/implementations/tensor_i8.cairo @@ -602,6 +602,10 @@ impl I8Tensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + math::det::det(*self) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/implementations/tensor_u32.cairo b/src/operators/tensor/implementations/tensor_u32.cairo index efb681a86..db1feb795 100644 --- a/src/operators/tensor/implementations/tensor_u32.cairo +++ b/src/operators/tensor/implementations/tensor_u32.cairo @@ -546,6 +546,10 @@ impl U32Tensor of TensorTrait { self, default_list, default_tensor, keys, keys_tensor, values, values_tensor ) } + + fn det(self: @Tensor) -> Tensor { + panic(array!['not supported!']) + } } /// Implements addition for `Tensor` using the `Add` trait. diff --git a/src/operators/tensor/math.cairo b/src/operators/tensor/math.cairo index b73f6d102..c19389508 100644 --- a/src/operators/tensor/math.cairo +++ b/src/operators/tensor/math.cairo @@ -68,3 +68,4 @@ mod hann_window; mod hamming_window; mod blackman_window; mod scatter_nd; +mod det; diff --git a/src/operators/tensor/math/det.cairo b/src/operators/tensor/math/det.cairo new file mode 100644 index 000000000..7ef0f0164 --- /dev/null +++ b/src/operators/tensor/math/det.cairo @@ -0,0 +1,103 @@ +use orion::numbers::fixed_point::core::FixedTrait; +use orion::numbers::NumberTrait; +use orion::operators::tensor::core::{Tensor, TensorTrait}; + +fn det< + T, + MAG, + impl TTensor: TensorTrait, + impl TNumber: NumberTrait, + impl TAdd: Add, + impl TSub: Sub, + impl TMul: Mul, + impl TDiv: Div, + impl TTensorAdd: Add>, + impl TPartialOrd: PartialOrd, + impl TAddEq: AddEq, + impl TCopy: Copy, + impl TDrop: Drop, +>( + self: Tensor + ) -> Tensor { + assert((self.shape).len() == 3 && *(self.shape).at(1) == *(self.shape).at(2), 'Unexpected shape.'); + + let n = *(self.shape).at(0); + + let mut arr: Array = array![]; + let mut i: usize = 0; + + let mut tensor_array: Array> = array![]; + while i < n { + let s = self.slice(array![i].span(), array![i + 1].span(), Option::Some(array![0].span()), Option::Some(array![1].span())); + tensor_array.append(TensorTrait::::new(array![*(s.shape).at(1), *(s.shape).at(2)].span(), s.data)); + i += 1; + }; + i = 0; + let len = tensor_array.len(); + while i < len { + let s = tensor_array.at(i); + let m = determinant(*s); + arr.append(m); + i += 1; + }; + TensorTrait::::new(array![n].span(), arr.span()) +} + +fn determinant< + T, + MAG, + impl TTensor: TensorTrait, + impl TNumber: NumberTrait, + impl TAdd: Add, + impl TSub: Sub, + impl TMul: Mul, + impl TDiv: Div, + impl TTensorAdd: Add>, + impl TPartialOrd: PartialOrd, + impl TAddEq: AddEq, + impl TCopy: Copy, + impl TDrop: Drop, +>( + tensor: Tensor + ) -> T { + let shape_len = *(tensor.shape).at(0); + if shape_len == 2 { + return *(tensor.data).at(0) * *(tensor.data).at(3) - *(tensor.data).at(1) * *(tensor.data).at(2); + }; + let zero: T = NumberTrait::zero(); + let one: T = NumberTrait::one(); + let n_one: T = zero - one; + let mut d = zero; + let mut j: usize = 0; + let n = *(tensor.shape).at(0); + let mut n_j = n_one; + + while j < n { + n_j = n_j * n_one; + let cofactor: T = n_j * (*(tensor.data).at(j)) * determinant(minor(tensor, 0, j)); + d = d + cofactor; + j += 1; + }; + return d; + } + +fn minor< + T, + MAG, + impl TTensor: TensorTrait, + impl TNumber: NumberTrait, + impl TAdd: Add, + impl TSub: Sub, + impl TMul: Mul, + impl TDiv: Div, + impl TTensorAdd: Add>, + impl TPartialOrd: PartialOrd, + impl TAddEq: AddEq, + impl TCopy: Copy, + impl TDrop: Drop, +>( + tensor: Tensor, i: usize, j: usize + ) -> Tensor { + let upper_part = TensorTrait::concat(array![tensor.slice(array![0].span(), array![i].span(), Option::Some(array![0].span()), Option::Some(array![1].span())), tensor.slice(array![i + 1].span(), array![*(tensor.shape).at(0)].span(), Option::Some(array![0].span()), Option::Some(array![1].span()))].span(), 0); + TensorTrait::concat(array![upper_part.slice(array![0].span(), array![j].span(), Option::Some(array![1].span()), Option::Some(array![1].span())), upper_part.slice(array![j + 1].span(), array![*(tensor.shape).at(1)].span(), Option::Some(array![1].span()), Option::Some(array![1].span()))].span(), 1) +} diff --git a/tests/nodes.cairo b/tests/nodes.cairo index 29bebb762..3299e9d11 100644 --- a/tests/nodes.cairo +++ b/tests/nodes.cairo @@ -1047,3 +1047,7 @@ mod label_encoder_fp8x23_default; mod label_encoder_i8_default; mod label_encoder_i32_default; mod label_encoder_u32_default; +mod det_fp8x23; +mod det_i32; +mod det_fp16x16; +mod det_i8; diff --git a/tests/nodes/det_fp16x16.cairo b/tests/nodes/det_fp16x16.cairo new file mode 100644 index 000000000..39d98de4a --- /dev/null +++ b/tests/nodes/det_fp16x16.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::FP16x16TensorPartialEq; + +#[test] +#[available_gas(2000000000)] +fn test_det_fp16x16() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.det(); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/det_fp16x16/input_0.cairo b/tests/nodes/det_fp16x16/input_0.cairo new file mode 100644 index 000000000..9f3b4ce9a --- /dev/null +++ b/tests/nodes/det_fp16x16/input_0.cairo @@ -0,0 +1,18 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP16x16Tensor, FP16x16TensorAdd}; +use orion::numbers::{FixedTrait, FP16x16}; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(1); + shape.append(2); + shape.append(2); + + let mut data = ArrayTrait::new(); + data.append(FP16x16 { mag: 196608, sign: true }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 0, sign: false }); + data.append(FP16x16 { mag: 65536, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/det_fp16x16/output_0.cairo b/tests/nodes/det_fp16x16/output_0.cairo new file mode 100644 index 000000000..92bc2042c --- /dev/null +++ b/tests/nodes/det_fp16x16/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: true }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/det_fp8x23.cairo b/tests/nodes/det_fp8x23.cairo new file mode 100644 index 000000000..8f71de18d --- /dev/null +++ b/tests/nodes/det_fp8x23.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::utils::{assert_eq, assert_seq_eq}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{FP8x23Tensor, FP8x23TensorAdd}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::FP8x23TensorPartialEq; + +#[test] +#[available_gas(2000000000)] +fn test_det_fp8x23() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.det(); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/det_fp8x23/input_0.cairo b/tests/nodes/det_fp8x23/input_0.cairo new file mode 100644 index 000000000..1444b12c0 --- /dev/null +++ b/tests/nodes/det_fp8x23/input_0.cairo @@ -0,0 +1,46 @@ +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(4); + shape.append(4); + + let mut data = ArrayTrait::new(); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 0, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: false }); + data.append(FP8x23 { mag: 8388608, sign: true }); + data.append(FP8x23 { mag: 16777216, sign: true }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/det_fp8x23/output_0.cairo b/tests/nodes/det_fp8x23/output_0.cairo new file mode 100644 index 000000000..29bc0d86b --- /dev/null +++ b/tests/nodes/det_fp8x23/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: 25165823, sign: true }); + data.append(FP8x23 { mag: 50331648, sign: false }); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/det_i32.cairo b/tests/nodes/det_i32.cairo new file mode 100644 index 000000000..b5824d7fc --- /dev/null +++ b/tests/nodes/det_i32.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I32Tensor, I32TensorAdd}; +use orion::operators::tensor::I32TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; +use core::array::{ArrayTrait, SpanTrait}; + +#[test] +#[available_gas(2000000000)] +fn test_det_i32() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.det(); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/det_i32/input_0.cairo b/tests/nodes/det_i32/input_0.cairo new file mode 100644 index 000000000..fafbae7a3 --- /dev/null +++ b/tests/nodes/det_i32/input_0.cairo @@ -0,0 +1,32 @@ +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(3); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(1); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + data.append(2); + data.append(2); + data.append(3); + data.append(4); + data.append(5); + data.append(6); + data.append(7); + data.append(8); + data.append(9); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/det_i32/output_0.cairo b/tests/nodes/det_i32/output_0.cairo new file mode 100644 index 000000000..efeeeb5ee --- /dev/null +++ b/tests/nodes/det_i32/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(0); + data.append(-3); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/det_i8.cairo b/tests/nodes/det_i8.cairo new file mode 100644 index 000000000..f9b580171 --- /dev/null +++ b/tests/nodes/det_i8.cairo @@ -0,0 +1,20 @@ +mod input_0; +mod output_0; + + +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I8Tensor, I8TensorAdd}; +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::I8TensorPartialEq; +use orion::utils::{assert_eq, assert_seq_eq}; + +#[test] +#[available_gas(2000000000)] +fn test_det_i8() { + let input_0 = input_0::input_0(); + let z_0 = output_0::output_0(); + + let y_0 = input_0.det(); + + assert_eq(y_0, z_0); +} diff --git a/tests/nodes/det_i8/input_0.cairo b/tests/nodes/det_i8/input_0.cairo new file mode 100644 index 000000000..d0b8f5310 --- /dev/null +++ b/tests/nodes/det_i8/input_0.cairo @@ -0,0 +1,17 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I8Tensor, I8TensorAdd}; +use orion::numbers::NumberTrait; + +fn input_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + shape.append(1); + shape.append(1); + + let mut data = ArrayTrait::new(); + data.append(5); + data.append(3); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +} diff --git a/tests/nodes/det_i8/output_0.cairo b/tests/nodes/det_i8/output_0.cairo new file mode 100644 index 000000000..cfef71e1b --- /dev/null +++ b/tests/nodes/det_i8/output_0.cairo @@ -0,0 +1,15 @@ +use core::array::{ArrayTrait, SpanTrait}; +use orion::operators::tensor::{TensorTrait, Tensor}; +use orion::operators::tensor::{I8Tensor, I8TensorAdd}; +use orion::numbers::NumberTrait; + +fn output_0() -> Tensor { + let mut shape = ArrayTrait::::new(); + shape.append(3); + + let mut data = ArrayTrait::new(); + data.append(4); + data.append(3); + data.append(4); + TensorTrait::new(shape.span(), data.span()) +}