-
Notifications
You must be signed in to change notification settings - Fork 81
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 #599 from canacechan/BitShift
feat: bit shift
- Loading branch information
Showing
26 changed files
with
473 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# TensorTrait::bit_shift | ||
|
||
```rust | ||
fn bit_shift(tensor1: @Tensor<T>, tensor2: @Tensor<T>, direction: felt252) -> Tensor<T>; | ||
``` | ||
|
||
Bitwise shift operator performs element-wise operation. For each input element, if the attribute "direction" is "RIGHT", this operator moves its binary representation toward the right side so that the input value is effectively decreased. If the attribute "direction" is "LEFT", bits of binary representation moves toward the left side, which results the increase of its actual value. | ||
The input tensor1 is the tensor to be shifted and another input tensor2 specifies the amounts of shifting. | ||
|
||
## Args | ||
|
||
* `tensor1`(`@Tensor<T>`) - First operand, input to be shifted. | ||
* `tensor2`(`@Tensor<T>`) - Second operand, amounts of shift. | ||
* `direction`(@Tensor<T>) - Direction of moving bits. It can be either "RIGHT" (for right shift) or "LEFT" (for left shift). | ||
|
||
## Returns | ||
|
||
* Output tensor | ||
|
||
## Examples | ||
|
||
```rust | ||
|
||
use orion::operators::tensor::{U32Tensor, U32TensorAdd}; | ||
use core::array::{ArrayTrait, SpanTrait}; | ||
use orion::operators::tensor::{TensorTrait, Tensor}; | ||
use orion::utils::{assert_eq, assert_seq_eq}; | ||
use orion::operators::tensor::U32TensorPartialEq; | ||
|
||
|
||
fn example() -> Tensor<u32> { | ||
let mut shape = ArrayTrait::<usize>::new(); | ||
shape.append(3); | ||
shape.append(3); | ||
|
||
let mut data = ArrayTrait::new(); | ||
data.append(21); | ||
data.append(7); | ||
data.append(18); | ||
data.append(43); | ||
data.append(49); | ||
data.append(49); | ||
data.append(4); | ||
data.append(28); | ||
data.append(24); | ||
let input_0 = TensorTrait::new(shape.span(), data.span()); | ||
|
||
let mut shape1 = ArrayTrait::<usize>::new(); | ||
shape1.append(3); | ||
shape1.append(3); | ||
|
||
let mut data1 = ArrayTrait::new(); | ||
data1.append(4); | ||
data1.append(0); | ||
data1.append(0); | ||
data1.append(3); | ||
data1.append(1); | ||
data1.append(1); | ||
data1.append(1); | ||
data1.append(1); | ||
data1.append(0); | ||
let input_1 = TensorTrait::new(shape1.span(), data1.span()); | ||
|
||
|
||
return TensorTrait::bit_shift(@input_0, @input_1, 'LEFT'); | ||
} | ||
>>> [336 7 18 344 98 98 8 56 24] | ||
``` |
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,32 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl, Trait, get_data_statement | ||
|
||
class Bit_shift(RunAll): | ||
|
||
@staticmethod | ||
def left_u32(): | ||
x = np.random.randint(0, 60, (3, 3)).astype(np.uint32) | ||
y = np.random.randint(0, 6, (3, 3)).astype(np.uint32) | ||
z = x << y | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "bit_shift_left_u32" | ||
make_test([x, y], z, "TensorTrait::bit_shift(@input_0, @input_1, 'LEFT')", name) | ||
|
||
@staticmethod | ||
def right_u32(): | ||
x = np.random.randint(0, 60, (3, 3)).astype(np.uint32) | ||
y = np.random.randint(0, 6, (3, 3)).astype(np.uint32) | ||
z = x >> y | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
z = Tensor(Dtype.U32, z.shape, z.flatten()) | ||
|
||
name = "bit_shift_right_u32" | ||
make_test([x, y], z, "TensorTrait::bit_shift(@input_0, @input_1, 'RIGHT')", name) | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,4 +68,5 @@ mod hann_window; | |
mod hamming_window; | ||
mod blackman_window; | ||
mod scatter_nd; | ||
mod bit_shift; | ||
mod eye_like; |
Oops, something went wrong.