-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from dincerguner/clip-impl
implement clip
- Loading branch information
Showing
44 changed files
with
10,882 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# tensor.clip | ||
|
||
```rust | ||
fn clip(self: @Tensor<T>, min: T, max: T) -> Tensor<T>; | ||
``` | ||
|
||
Clip operator limits the given input within an interval. | ||
|
||
## Args | ||
|
||
* `self`(`@Tensor<T>`) - Input tensor whose elements to be clipped. | ||
* `min`(`Option<T>`) - Minimum value, under which element is replaced by min. | ||
* `max`(`Option<T>`) - Maximum value, above which element is replaced by max. | ||
|
||
## Returns | ||
|
||
Output `Tensor<T>` with clipped input elements. | ||
|
||
## Example | ||
|
||
```rust | ||
use array::{ArrayTrait, SpanTrait}; | ||
|
||
use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor}; | ||
|
||
fn clip_example() -> Tensor<u32> { | ||
let tensor = TensorTrait::<u32>::new( | ||
shape: array![2, 3].span(), | ||
data: array![[ 1, 2, 3],[4, 5, 6]].span(), | ||
); | ||
|
||
return tensor.clip( | ||
min: Option::None(()), | ||
max: Option::Some(3), | ||
); | ||
} | ||
>>> [[1. 2. 3.] | ||
[3. 3. 3.]] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl | ||
|
||
|
||
class Clip(RunAll): | ||
@staticmethod | ||
def clip_u32(): | ||
def clip_2D(): | ||
x = np.random.randint(0, 255, (2, 4)).astype(np.uint32) | ||
y = np.clip(x, np.uint32(10), np.uint32(20)) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
|
||
name = "clip_u32_2d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(u32 { mag: 10, sign: false }), Option::Some(u32 { mag: 20, sign: false }))", name) | ||
|
||
def clip_3D(): | ||
x = np.random.randint(0, 255, (20, 10, 5)).astype(np.uint32) | ||
y = np.clip(x, np.uint32(10), np.uint32(20)) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
|
||
name = "clip_u32_3d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(u32 { mag: 10, sign: false }), Option::Some(u32 { mag: 20, sign: false }))", name) | ||
|
||
clip_2D() | ||
clip_3D() | ||
|
||
@staticmethod | ||
def clip_i32(): | ||
def clip_2D(): | ||
x = np.random.randint(-127, 127, (2, 4)).astype(np.int32) | ||
y = np.clip(x, np.int32(-10), np.int32(20)) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
|
||
name = "clip_i32_2d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(i32 { mag: 10, sign: true }), Option::Some(i32 { mag: 20, sign: false }))", name) | ||
|
||
def clip_3D(): | ||
x = np.random.randint(-127, 127, (20, 10, 5)).astype(np.int32) | ||
y = np.clip(x, np.int32(-10), np.int32(20)) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
|
||
name = "clip_i32_3d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(i32 { mag: 10, sign: true }), Option::Some(i32 { mag: 20, sign: false }))", name) | ||
|
||
|
||
clip_2D() | ||
clip_3D() | ||
|
||
@staticmethod | ||
def clip_i8(): | ||
def clip_2D(): | ||
x = np.random.randint(-127, 127, (2, 4)).astype(np.int8) | ||
y = np.clip(x, np.int8(-10), np.int8(20)) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
|
||
name = "clip_i8_2d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(i8 { mag: 10, sign: true }), Option::Some(i8 { mag: 20, sign: false }))", name) | ||
|
||
def clip_3D(): | ||
x = np.random.randint(-127, 127, (20, 10, 5)).astype(np.int8) | ||
y = np.clip(x, np.int8(-10), np.int8(20)) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
|
||
name = "clip_i8_3d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(i8 { mag: 10, sign: true }), Option::Some(i8 { mag: 20, sign: false }))", name) | ||
|
||
clip_2D() | ||
clip_3D() | ||
|
||
@staticmethod | ||
def clip_fp8x23(): | ||
def clip_2D(): | ||
x = to_fp(np.random.randint(-127, 127, (2, 4) | ||
).astype(np.int64), FixedImpl.FP8x23) | ||
y = np.clip(x, to_fp(np.int64(-10), FixedImpl.FP8x23), to_fp(np.int64(20), FixedImpl.FP8x23)) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) | ||
|
||
name = "clip_fp8x23_2d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(FP8x23 { mag: 83886080, sign: true }), Option::Some(FP8x23 { mag: 167772160, sign: false }))", name) | ||
|
||
def clip_3D(): | ||
x = to_fp(np.random.randint(-127, 127, (20, 10, 5) | ||
).astype(np.int64), FixedImpl.FP8x23) | ||
y = np.clip(x, to_fp(np.int64(-10), FixedImpl.FP8x23), to_fp(np.int64(20), FixedImpl.FP8x23)) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) | ||
|
||
name = "clip_fp8x23_3d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(FP8x23 { mag: 83886080, sign: true }), Option::Some(FP8x23 { mag: 167772160, sign: false }))", name) | ||
|
||
clip_2D() | ||
clip_3D() | ||
|
||
@staticmethod | ||
def clip_fp16x16(): | ||
def clip_2D(): | ||
x = to_fp(np.random.randint(-127, 127, (2, 4) | ||
).astype(np.int64), FixedImpl.FP16x16) | ||
y = np.clip(x, to_fp(np.int64(-10), FixedImpl.FP16x16), to_fp(np.int64(20), FixedImpl.FP16x16)) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP16x16, y.shape, y.flatten()) | ||
|
||
name = "clip_fp16x16_2d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(FP16x16 { mag: 655360, sign: true }), Option::Some(FP16x16 { mag: 1310720, sign: false }))", name) | ||
|
||
def clip_3D(): | ||
x = to_fp(np.random.randint(-127, 127, (20, 10, 5) | ||
).astype(np.int64), FixedImpl.FP16x16) | ||
y = np.clip(x, to_fp(np.int64(-10), FixedImpl.FP16x16), to_fp(np.int64(20), FixedImpl.FP16x16)) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP16x16, y.shape, y.flatten()) | ||
|
||
name = "clip_fp16x16_3d" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.clip(Option::Some(FP16x16 { mag: 655360, sign: true }), Option::Some(FP16x16 { mag: 1310720, sign: false }))", name) | ||
|
||
clip_2D() | ||
clip_3D() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.