Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelDkhn committed Mar 4, 2024
1 parent 72c2565 commit fb6f4a0
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 66 deletions.
1 change: 0 additions & 1 deletion src/operators/ml/normalizer/normalizer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ fn reduce_max_2D_axis_1<
let max = max(SpanTrait::slice(X.data, i * C, C));
new_data.append(max);
i += 1;

};
return new_data.span();
}
Expand Down
10 changes: 7 additions & 3 deletions src/operators/tensor/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5841,9 +5841,13 @@ trait TensorTrait<T> {
/// >>> [7, 9, 999, 7, 999, 999],
/// ```
///
fn label_encoder(self: @Tensor<T>, default_list: Option<Span<T>>,
default_tensor: Option<Tensor<T>>, keys: Option<Span<T>>,
keys_tensor: Option<Tensor<T>>, values: Option<Span<T>>,
fn label_encoder(
self: @Tensor<T>,
default_list: Option<Span<T>>,
default_tensor: Option<Tensor<T>>,
keys: Option<Span<T>>,
keys_tensor: Option<Tensor<T>>,
values: Option<Span<T>>,
values_tensor: Option<Tensor<T>>
) -> Tensor<T>;
}
Expand Down
12 changes: 10 additions & 2 deletions src/operators/tensor/implementations/tensor_bool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,16 @@ impl BoolTensor of TensorTrait<bool> {
panic(array!['not supported!'])
}

fn label_encoder(self: @Tensor<bool>, default_list: Option<Span<bool>>, default_tensor: Option<Tensor<bool>>, keys: Option<Span<bool>>, keys_tensor: Option<Tensor<bool>>, values: Option<Span<bool>>, values_tensor: Option<Tensor<bool>>) -> Tensor<bool> {
panic(array!['not supported!'])
fn label_encoder(
self: @Tensor<bool>,
default_list: Option<Span<bool>>,
default_tensor: Option<Tensor<bool>>,
keys: Option<Span<bool>>,
keys_tensor: Option<Tensor<bool>>,
values: Option<Span<bool>>,
values_tensor: Option<Tensor<bool>>
) -> Tensor<bool> {
panic(array!['not supported!'])
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/operators/tensor/implementations/tensor_complex64.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,16 @@ impl Complex64Tensor of TensorTrait<complex64> {
panic(array!['not supported!'])
}

fn label_encoder(self: @Tensor<complex64>, default_list: Option<Span<complex64>>, default_tensor: Option<Tensor<complex64>>, keys: Option<Span<complex64>>, keys_tensor: Option<Tensor<complex64>>, values: Option<Span<complex64>>, values_tensor: Option<Tensor<complex64>>) -> Tensor<complex64> {
panic(array!['not supported!'])
fn label_encoder(
self: @Tensor<complex64>,
default_list: Option<Span<complex64>>,
default_tensor: Option<Tensor<complex64>>,
keys: Option<Span<complex64>>,
keys_tensor: Option<Tensor<complex64>>,
values: Option<Span<complex64>>,
values_tensor: Option<Tensor<complex64>>
) -> Tensor<complex64> {
panic(array!['not supported!'])
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_fp16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,18 @@ impl FP16x16Tensor of TensorTrait<FP16x16> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<FP16x16>, default_list: Option<Span<FP16x16>>, default_tensor: Option<Tensor<FP16x16>>, keys: Option<Span<FP16x16>>, keys_tensor: Option<Tensor<FP16x16>>, values: Option<Span<FP16x16>>, values_tensor: Option<Tensor<FP16x16>>) -> Tensor<FP16x16> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<FP16x16>,
default_list: Option<Span<FP16x16>>,
default_tensor: Option<Tensor<FP16x16>>,
keys: Option<Span<FP16x16>>,
keys_tensor: Option<Tensor<FP16x16>>,
values: Option<Span<FP16x16>>,
values_tensor: Option<Tensor<FP16x16>>
) -> Tensor<FP16x16> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_fp16x16wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,18 @@ impl FP16x16WTensor of TensorTrait<FP16x16W> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<FP16x16W>, default_list: Option<Span<FP16x16W>>, default_tensor: Option<Tensor<FP16x16W>>, keys: Option<Span<FP16x16W>>, keys_tensor: Option<Tensor<FP16x16W>>, values: Option<Span<FP16x16W>>, values_tensor: Option<Tensor<FP16x16W>>) -> Tensor<FP16x16W> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<FP16x16W>,
default_list: Option<Span<FP16x16W>>,
default_tensor: Option<Tensor<FP16x16W>>,
keys: Option<Span<FP16x16W>>,
keys_tensor: Option<Tensor<FP16x16W>>,
values: Option<Span<FP16x16W>>,
values_tensor: Option<Tensor<FP16x16W>>
) -> Tensor<FP16x16W> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_fp32x32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,18 @@ impl FP32x32Tensor of TensorTrait<FP32x32> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<FP32x32>, default_list: Option<Span<FP32x32>>, default_tensor: Option<Tensor<FP32x32>>, keys: Option<Span<FP32x32>>, keys_tensor: Option<Tensor<FP32x32>>, values: Option<Span<FP32x32>>, values_tensor: Option<Tensor<FP32x32>>) -> Tensor<FP32x32> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<FP32x32>,
default_list: Option<Span<FP32x32>>,
default_tensor: Option<Tensor<FP32x32>>,
keys: Option<Span<FP32x32>>,
keys_tensor: Option<Tensor<FP32x32>>,
values: Option<Span<FP32x32>>,
values_tensor: Option<Tensor<FP32x32>>
) -> Tensor<FP32x32> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_fp64x64.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,18 @@ impl FP64x64Tensor of TensorTrait<FP64x64> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<FP64x64>, default_list: Option<Span<FP64x64>>, default_tensor: Option<Tensor<FP64x64>>, keys: Option<Span<FP64x64>>, keys_tensor: Option<Tensor<FP64x64>>, values: Option<Span<FP64x64>>, values_tensor: Option<Tensor<FP64x64>>) -> Tensor<FP64x64> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<FP64x64>,
default_list: Option<Span<FP64x64>>,
default_tensor: Option<Tensor<FP64x64>>,
keys: Option<Span<FP64x64>>,
keys_tensor: Option<Tensor<FP64x64>>,
values: Option<Span<FP64x64>>,
values_tensor: Option<Tensor<FP64x64>>
) -> Tensor<FP64x64> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_fp8x23.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,18 @@ impl FP8x23Tensor of TensorTrait<FP8x23> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<FP8x23>, default_list: Option<Span<FP8x23>>, default_tensor: Option<Tensor<FP8x23>>, keys: Option<Span<FP8x23>>, keys_tensor: Option<Tensor<FP8x23>>, values: Option<Span<FP8x23>>, values_tensor: Option<Tensor<FP8x23>>) -> Tensor<FP8x23> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<FP8x23>,
default_list: Option<Span<FP8x23>>,
default_tensor: Option<Tensor<FP8x23>>,
keys: Option<Span<FP8x23>>,
keys_tensor: Option<Tensor<FP8x23>>,
values: Option<Span<FP8x23>>,
values_tensor: Option<Tensor<FP8x23>>
) -> Tensor<FP8x23> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_fp8x23wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,18 @@ impl FP8x23WTensor of TensorTrait<FP8x23W> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<FP8x23W>, default_list: Option<Span<FP8x23W>>, default_tensor: Option<Tensor<FP8x23W>>, keys: Option<Span<FP8x23W>>, keys_tensor: Option<Tensor<FP8x23W>>, values: Option<Span<FP8x23W>>, values_tensor: Option<Tensor<FP8x23W>>) -> Tensor<FP8x23W> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<FP8x23W>,
default_list: Option<Span<FP8x23W>>,
default_tensor: Option<Tensor<FP8x23W>>,
keys: Option<Span<FP8x23W>>,
keys_tensor: Option<Tensor<FP8x23W>>,
values: Option<Span<FP8x23W>>,
values_tensor: Option<Tensor<FP8x23W>>
) -> Tensor<FP8x23W> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_i32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,18 @@ impl I32Tensor of TensorTrait<i32> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<i32>, default_list: Option<Span<i32>>, default_tensor: Option<Tensor<i32>>, keys: Option<Span<i32>>, keys_tensor: Option<Tensor<i32>>, values: Option<Span<i32>>, values_tensor: Option<Tensor<i32>>) -> Tensor<i32> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<i32>,
default_list: Option<Span<i32>>,
default_tensor: Option<Tensor<i32>>,
keys: Option<Span<i32>>,
keys_tensor: Option<Tensor<i32>>,
values: Option<Span<i32>>,
values_tensor: Option<Tensor<i32>>
) -> Tensor<i32> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_i8.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,18 @@ impl I8Tensor of TensorTrait<i8> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<i8>, default_list: Option<Span<i8>>, default_tensor: Option<Tensor<i8>>, keys: Option<Span<i8>>, keys_tensor: Option<Tensor<i8>>, values: Option<Span<i8>>, values_tensor: Option<Tensor<i8>>) -> Tensor<i8> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<i8>,
default_list: Option<Span<i8>>,
default_tensor: Option<Tensor<i8>>,
keys: Option<Span<i8>>,
keys_tensor: Option<Tensor<i8>>,
values: Option<Span<i8>>,
values_tensor: Option<Tensor<i8>>
) -> Tensor<i8> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/operators/tensor/implementations/tensor_u32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,18 @@ impl U32Tensor of TensorTrait<u32> {
math::scatter_nd::scatter_nd(self, updates, indices, reduction)
}

fn label_encoder(self: @Tensor<u32>, default_list: Option<Span<u32>>, default_tensor: Option<Tensor<u32>>, keys: Option<Span<u32>>, keys_tensor: Option<Tensor<u32>>, values: Option<Span<u32>>, values_tensor: Option<Tensor<u32>>) -> Tensor<u32> {
ml::label_encoder::label_encoder(self, default_list, default_tensor, keys, keys_tensor, values, values_tensor)
fn label_encoder(
self: @Tensor<u32>,
default_list: Option<Span<u32>>,
default_tensor: Option<Tensor<u32>>,
keys: Option<Span<u32>>,
keys_tensor: Option<Tensor<u32>>,
values: Option<Span<u32>>,
values_tensor: Option<Tensor<u32>>
) -> Tensor<u32> {
ml::label_encoder::label_encoder(
self, default_list, default_tensor, keys, keys_tensor, values, values_tensor
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/tensor/ml.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod array_feature_extractor;
mod label_encoder;
mod label_encoder;
45 changes: 21 additions & 24 deletions src/operators/tensor/ml/label_encoder.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ use core::traits::Into;
use core::traits::TryInto;
/// Cf: TensorTrait::label_encoder docstring
fn label_encoder<
T,
+Drop<T>,
+Copy<T>,
+AddEq<T>,
+TensorTrait<T>,
+PartialOrd<T>,
+Into<T, felt252>,
T, +Drop<T>, +Copy<T>, +AddEq<T>, +TensorTrait<T>, +PartialOrd<T>, +Into<T, felt252>,
>(
// self: @Tensor<T>, default: T, keys: Array<T>, values: Array<T>
self: @Tensor<T>, default_list: Option<Span<T>>, default_tensor: Option<Tensor<T>>, keys: Option<Span<T>>, keys_tensor: Option<Tensor<T>>, values: Option<Span<T>>, values_tensor: Option<Tensor<T>>,

) -> Tensor<T>
{
self: @Tensor<T>,
default_list: Option<Span<T>>,
default_tensor: Option<Tensor<T>>,
keys: Option<Span<T>>,
keys_tensor: Option<Tensor<T>>,
values: Option<Span<T>>,
values_tensor: Option<Tensor<T>>,
) -> Tensor<T> {
let mut default = match default_list {
Option::Some(value) => value,
Option::None => {
Option::None => {
match default_tensor {
Option::Some(value) => value.data,
Option::None => { core::panic_with_felt252('None') },
Expand All @@ -37,31 +35,30 @@ fn label_encoder<
};

let default = match default.pop_front() {
Option::Some(value) => *value,
Option::None => { core::panic_with_felt252('None') }
Option::Some(value) => *value,
Option::None => { core::panic_with_felt252('None') }
};

let mut keys = match keys {
Option::Some(value) => { value },
Option::None => {
Option::None => {
match keys_tensor {
Option::Some(value) => { value.data },
Option::None => { core::panic_with_felt252('None') },
Option::None => { core::panic_with_felt252('None') },
}
}
};

let mut values = match values {
let mut values = match values {
Option::Some(value) => { value },
Option::None => {
Option::None => {
match values_tensor {
Option::Some(value) => { value.data },
Option::None => { core::panic_with_felt252('None') },
Option::None => { core::panic_with_felt252('None') },
}
}
};


assert(keys.len() == values.len(), 'keys must be eq to values');
let mut key_value_dict: Felt252Dict<Nullable<T>> = Default::default();
let mut output_data = ArrayTrait::<T>::new();
Expand All @@ -74,7 +71,7 @@ fn label_encoder<
let value = match values.pop_front() {
Option::Some(value) => value,
Option::None => { break; }
};
};

key_value_dict.insert((*key).into(), nullable_from_box(BoxTrait::new(*value)));
};
Expand All @@ -87,8 +84,8 @@ fn label_encoder<
let res = key_value_dict.get(value.into());

let mut span = match match_nullable(res) {
FromNullableResult::Null => default,
FromNullableResult::NotNull(res) => res.unbox(),
FromNullableResult::Null => default,
FromNullableResult::NotNull(res) => res.unbox(),
};
output_data.append(span);
},
Expand All @@ -98,4 +95,4 @@ fn label_encoder<

let mut output_tensor = TensorTrait::<T>::new(*self.shape, output_data.span());
return output_tensor;
}
}
12 changes: 9 additions & 3 deletions tests/nodes/label_encoder_fp16x16_3d_default.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ fn test_label_encoder_fp16x16_3d_default() {
let input_3 = input_3::input_3();
let z_0 = output_0::output_0();

let y_0 = input_0.label_encoder(default_list:Option::None, default_tensor: Option::Some(input_1),
keys:Option::None, keys_tensor: Option::Some(input_2),
values: Option::None, values_tensor: Option::Some(input_3));
let y_0 = input_0
.label_encoder(
default_list: Option::None,
default_tensor: Option::Some(input_1),
keys: Option::None,
keys_tensor: Option::Some(input_2),
values: Option::None,
values_tensor: Option::Some(input_3)
);

assert_eq(y_0, z_0);
}
12 changes: 9 additions & 3 deletions tests/nodes/label_encoder_fp8x23_default.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ fn test_label_encoder_fp8x23_default() {
let input_3 = input_3::input_3();
let z_0 = output_0::output_0();

let y_0 = input_0.label_encoder(default_list:Option::None, default_tensor: Option::Some(input_1),
keys:Option::None, keys_tensor: Option::Some(input_2),
values: Option::None, values_tensor: Option::Some(input_3));
let y_0 = input_0
.label_encoder(
default_list: Option::None,
default_tensor: Option::Some(input_1),
keys: Option::None,
keys_tensor: Option::Some(input_2),
values: Option::None,
values_tensor: Option::Some(input_3)
);

assert_eq(y_0, z_0);
}
Loading

0 comments on commit fb6f4a0

Please sign in to comment.