Skip to content

Commit

Permalink
modif from review
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Nov 23, 2023
1 parent ed317df commit 1a77dfc
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 61 deletions.
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
* [tensor.dequantize\_linear](framework/operators/tensor/tensor.dequantize\_linear.md)
* [tensor.qlinear\_add](framework/operators/tensor/tensor.qlinear\_add.md)
* [tensor.qlinear\_matmul](framework/operators/tensor/tensor.qlinear\_matmul.md)
* [tensor.qlinear\_leakyrelu](framework/operators/tensor/tensor.qlinear\_leakyrelu.md)
* [tensor.nonzero](framework/operators/tensor/tensor.nonzero.md)
* [tensor.squeeze](framework/operators/tensor/tensor.squeeze.md)
* [tensor.unsqueeze](framework/operators/tensor/tensor.unsqueeze.md)
Expand Down
1 change: 1 addition & 0 deletions docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ You can see below the list of current supported ONNX Operators:
| [DequantizeLinear](operators/tensor/tensor.quantize\_linear.md) | :white\_check\_mark: |
| [QlinearAdd](operators/tensor/tensor.qlinear\_add.md) | :white\_check\_mark: |
| [QLinearMatmul](operators/tensor/tensor.qlinear\_matmul.md) | :white\_check\_mark: |
| [QLinearLeakyRelu](operators/tensor/tensor.qlinear\_leakyrelu.md) | :white\_check\_mark: |
| [Nonzero](operators/tensor/tensor.nonzero.md) | :white\_check\_mark: |
| [Squeeze](operators/tensor/tensor.squeeze.md) | :white\_check\_mark: |
| [Unsqueeze](operators/tensor/tensor.unsqueeze.md) | :white\_check\_mark: |
Expand Down
1 change: 1 addition & 0 deletions docs/framework/operators/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ use orion::operators::tensor::TensorTrait;
| [`tensor.dequantize_linear`](tensor.dequantize\_linear.md) | Dequantizes an i8 Tensor using linear dequantization. |
| [`tensor.qlinear_add`](tensor.qlinear\_add.md) | Performs the sum of two quantized i8 Tensors. |
| [`tensor.qlinear_matmul`](tensor.qlinear\_matmul.md) | Performs the product of two quantized i8 Tensors. |
| [`tensor.qlinear_leakyrelu`](tensor.qlinear\_leakyrelu.md) | Applies the Leaky Relu operator to a quantized Tensor |
| [`tensor.gather`](tensor.gather.md) | Gather entries of the axis dimension of data. |
| [`tensor.nonzero`](tensor.nonzero.md) | Produces indices of the elements that are non-zero (in row-major order - by dimension). |
| [`tensor.squeeze`](tensor.squeeze.md) | Removes dimensions of size 1 from the shape of a tensor. |
Expand Down
74 changes: 74 additions & 0 deletions docs/framework/operators/tensor/tensor.qlinear_leakyrelu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# tensor.qlinear_leakyrelu

```rust
fn qlinear_leakyrelu(self: @Tensor<i8>, a_scale: @Tensor<T>, a_zero_point: @Tensor<T>, alpha: T) -> Tensor::<i8>;
```

Applies the Leaky Relu operator to a quantized Tensor

QLinar LeakyRelu takes as input a quantized Tensor, its scale and zero point and an scalar alpha, and produces one output data (a quantized Tensor)
where the function `f(x) = alpha * x for x < 0, f(x) = x for x >= 0`, is applied to the data tensor elementwise.
The quantization formula is y = saturate((x / y_scale) + y_zero_point).
Scale and zero point must have same shape and the same type. They must be either scalar (per tensor) or N-D tensor (per row for 'a' and per column for 'b').
Scalar refers to per tensor quantization whereas N-D refers to per row or per column quantization.

## Args

* `self`(`@Tensor<i8>`) - The first tensor to be multiplied (a).
* `a_scale`(`@Tensor<T>`) - Scale for input `a`.
* `a_zero_point`(`@Tensor<T>`) - Zero point for input `a`.
* `alpha`(`T`) - The factor multiplied to negative elements.

## Returns

A new `Tensor<i8>`, containing result of the Leaky Relu.

## Type Constraints

u32 tensor, not supported.
fp8x23wide tensor, not supported.
fp16x16wide tensor, not supported.
bool tensor, not supported.

## Example

```rust

use array::{ArrayTrait, SpanTrait};

use orion::operators::tensor::{TensorTrait, Tensor, I8Tensor, FP16x16Tensor};
use orion::numbers::{i8, FP16x16, FP16x16Impl, IntegerTrait, FixedTrait};


fn qlinear_leakyrelu_example() -> Tensor<i8> {
let a = TensorTrait::<
i8
>::new(
shape: array![2, 3].span(),
data: array![
IntegerTrait::<i8>::new(10_u8, true),
IntegerTrait::<i8>::new(10_u8, true),
IntegerTrait::<i8>::new(10_u8, true),
IntegerTrait::<i8>::new(10_u8, false),
IntegerTrait::<i8>::new(10_u8, false),
IntegerTrait::<i8>::new(10_u8, false)
]
.span(),
);

let a_scale = TensorTrait::<
FP16x16
>::new(shape: array![1].span(), data: array![FixedTrait::<FP16x16>::new(327680, false)].span(),);
let a_zero_point = TensorTrait::<
FP16x16
>::new(shape: array![1].span(), data: array![FixedTrait::<FP16x16>::new(131072, false)].span(),);

let alpha = FixedTrait::<FP16x16>::new(655360, false);

return = a
.qlinear_leakyrelu(
@a_scale, @a_zero_point, alpha
);
}

>>> [[-118, -118, -118], [10, 10, 10]]
8 changes: 3 additions & 5 deletions src/operators/tensor/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl TensorSerde<T, impl TSerde: Serde<T>, impl TDrop: Drop<T>> of Serde<Tensor<
/// dequantize_linear - Dequantizes an i8 Tensor using linear dequantization.
/// qlinear_add - Performs the sum of two quantized i8 Tensors.
/// qlinear_matmul - Performs the product of two quantized i8 Tensors.
/// qlinear_leakyrelu - Applies the Leaky Relu operator to a quantized Tensor
/// gather - Gather entries of the axis dimension of data.
/// nonzero - Produces indices of the elements that are non-zero (in row-major order - by dimension).
/// squeeze - Removes dimensions of size 1 from the shape of a tensor.
Expand Down Expand Up @@ -2768,7 +2769,7 @@ trait TensorTrait<T> {
/// fn qlinear_leakyrelu(self: @Tensor<i8>, a_scale: @Tensor<T>, a_zero_point: @Tensor<T>, alpha: T) -> Tensor::<i8>;
/// ```
///
/// Apply the Leaky Relu operator to a quantized Tensor
/// Applies the Leaky Relu operator to a quantized Tensor
///
/// QLinar LeakyRelu takes as input a quantized Tensor, its scale and zero point and an scalar alpha, and produces one output data (a quantized Tensor)
/// where the function `f(x) = alpha * x for x < 0, f(x) = x for x >= 0`, is applied to the data tensor elementwise.
Expand Down Expand Up @@ -2838,10 +2839,7 @@ trait TensorTrait<T> {
/// >>> [[-118, -118, -118], [10, 10, 10]]
///
fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<T>,
a_zero_point: @Tensor<T>,
alpha: T
self: @Tensor<i8>, a_scale: @Tensor<T>, a_zero_point: @Tensor<T>, alpha: T
) -> Tensor::<i8>;
/// # tensor.slice
///
Expand Down
5 changes: 1 addition & 4 deletions src/operators/tensor/implementations/tensor_bool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,7 @@ impl BoolTensor of TensorTrait<bool> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<bool>,
a_zero_point: @Tensor<bool>,
alpha: bool,
self: @Tensor<i8>, a_scale: @Tensor<bool>, a_zero_point: @Tensor<bool>, alpha: bool,
) -> Tensor::<i8> {
panic(array!['not supported!'])
}
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_fp16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,7 @@ impl FP16x16Tensor of TensorTrait<FP16x16> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<FP16x16>,
a_zero_point: @Tensor<FP16x16>,
alpha: FP16x16

self: @Tensor<i8>, a_scale: @Tensor<FP16x16>, a_zero_point: @Tensor<FP16x16>, alpha: FP16x16
) -> Tensor::<i8> {
quantization::qlinear_leakyrelu::qlinear_leakyrelu(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ impl FP16x16WTensor of TensorTrait<FP16x16W> {
a_scale: @Tensor<FP16x16W>,
a_zero_point: @Tensor<FP16x16W>,
alpha: FP16x16W

) -> Tensor::<i8> {
panic(array!['not supported!'])
}
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_fp32x32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ impl FP32x32Tensor of TensorTrait<FP32x32> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<FP32x32>,
a_zero_point: @Tensor<FP32x32>,
alpha: FP32x32

self: @Tensor<i8>, a_scale: @Tensor<FP32x32>, a_zero_point: @Tensor<FP32x32>, alpha: FP32x32
) -> Tensor::<i8> {
quantization::qlinear_leakyrelu::qlinear_leakyrelu(
self,
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_fp64x64.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ impl FP64x64Tensor of TensorTrait<FP64x64> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<FP64x64>,
a_zero_point: @Tensor<FP64x64>,
alpha: FP64x64

self: @Tensor<i8>, a_scale: @Tensor<FP64x64>, a_zero_point: @Tensor<FP64x64>, alpha: FP64x64
) -> Tensor::<i8> {
quantization::qlinear_leakyrelu::qlinear_leakyrelu(
self,
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_fp8x23.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,7 @@ impl FP8x23Tensor of TensorTrait<FP8x23> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<FP8x23>,
a_zero_point: @Tensor<FP8x23>,
alpha: FP8x23

self: @Tensor<i8>, a_scale: @Tensor<FP8x23>, a_zero_point: @Tensor<FP8x23>, alpha: FP8x23
) -> Tensor::<i8> {
quantization::qlinear_leakyrelu::qlinear_leakyrelu(
self,
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_fp8x23wide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,7 @@ impl FP8x23WTensor of TensorTrait<FP8x23W> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<FP8x23W>,
a_zero_point: @Tensor<FP8x23W>,
alpha: FP8x23W

self: @Tensor<i8>, a_scale: @Tensor<FP8x23W>, a_zero_point: @Tensor<FP8x23W>, alpha: FP8x23W
) -> Tensor::<i8> {
panic(array!['not supported!'])
}
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_i32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ impl I32Tensor of TensorTrait<i32> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<i32>,
a_zero_point: @Tensor<i32>,
alpha: i32

self: @Tensor<i8>, a_scale: @Tensor<i32>, a_zero_point: @Tensor<i32>, alpha: i32
) -> Tensor::<i8> {
quantization::qlinear_leakyrelu::qlinear_leakyrelu(
self,
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_i8.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,7 @@ impl I8Tensor of TensorTrait<i8> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<i8>,
a_zero_point: @Tensor<i8>,
alpha: i8

self: @Tensor<i8>, a_scale: @Tensor<i8>, a_zero_point: @Tensor<i8>, alpha: i8
) -> Tensor::<i8> {
quantization::qlinear_leakyrelu::qlinear_leakyrelu(
self,
Expand Down
6 changes: 1 addition & 5 deletions src/operators/tensor/implementations/tensor_u32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,7 @@ impl U32Tensor of TensorTrait<u32> {
}

fn qlinear_leakyrelu(
self: @Tensor<i8>,
a_scale: @Tensor<u32>,
a_zero_point: @Tensor<u32>,
alpha: u32

self: @Tensor<i8>, a_scale: @Tensor<u32>, a_zero_point: @Tensor<u32>, alpha: u32
) -> Tensor::<i8> {
panic(array!['not supported!'])
}
Expand Down
19 changes: 8 additions & 11 deletions src/operators/tensor/quantization/qlinear_leakyrelu.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,26 @@ fn qlinear_leakyrelu<
impl QCopy: Copy<Q>,
impl QDrop: Drop<Q>
>(
a: @Tensor<Q>,
a_scale: @Tensor<T>,
a_zero_point: @Tensor<T>,
alpha: T,
min: T,
max: T
a: @Tensor<Q>, a_scale: @Tensor<T>, a_zero_point: @Tensor<T>, alpha: T, min: T, max: T
) -> Tensor<Q> {
let mut dequantized_a = dequantize_linear(@(*a), a_scale, a_zero_point);

let mut result_data = ArrayTrait::<T>::new();
let mut i = 0;
loop {
match dequantized_a.data.pop_front() {
Option::Some(elem) => {
Option::Some(elem) => {
if *elem < NumberTrait::zero() {
result_data.append(*elem * alpha);
} else {
result_data.append(*elem);
}
},
Option::None(_) => { break;}
},
Option::None(_) => { break; }
};
};

return quantize_linear(@TensorTrait::new(dequantized_a.shape, result_data.span()), a_scale, a_zero_point, min, max);
return quantize_linear(
@TensorTrait::new(dequantized_a.shape, result_data.span()), a_scale, a_zero_point, min, max
);
}

0 comments on commit 1a77dfc

Please sign in to comment.