forked from gizatechxyz/orion
-
Notifications
You must be signed in to change notification settings - Fork 0
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 gizatechxyz#486 from hakymulla/reduce_log_sum
reduce log sum operator
- Loading branch information
Showing
38 changed files
with
703 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
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,45 @@ | ||
## tensor.reduce_log_sum | ||
|
||
```rust | ||
fn reduce_log_sum(self: @Tensor<T>, axis: usize, keepdims: bool) -> Tensor<T>; | ||
``` | ||
|
||
Computes the log sum of the input tensor's elements along the provided axes. | ||
## Args | ||
|
||
* `self`(`@Tensor<T>`) - The input tensor. | ||
* `axis`(`usize`) - The dimension to reduce. | ||
* `keepdims`(`bool`) - If true, retains reduced dimensions with length 1. | ||
|
||
## Panics | ||
|
||
* Panics if axis is not in the range of the input tensor's dimensions. | ||
|
||
## Returns | ||
|
||
A new `Tensor<T>` instance with the specified axis reduced by summing its elements. | ||
|
||
fn reduce_log_sum() -> Tensor<u32> { | ||
|
||
let mut sizes = ArrayTrait::new(); | ||
sizes.append(2); | ||
sizes.append(2); | ||
sizes.append(2); | ||
|
||
let mut data = ArrayTrait::new(); | ||
data.append(FixedTrait::new_unscaled(1, false)); | ||
data.append(FixedTrait::new_unscaled(2, false)); | ||
data.append(FixedTrait::new_unscaled(3, false)); | ||
data.append(FixedTrait::new_unscaled(4, false)); | ||
data.append(FixedTrait::new_unscaled(5, false)); | ||
data.append(FixedTrait::new_unscaled(6, false)); | ||
data.append(FixedTrait::new_unscaled(7, false)); | ||
data.append(FixedTrait::new_unscaled(8, false)); | ||
|
||
let tensor = TensorTrait::<FP16x16>::new(sizes.span(), data.span()); | ||
|
||
We can call `reduce_log_sum` function as follows. | ||
return tensor.reduce_log_sum(axis: 2, keepdims: false); | ||
} | ||
>>> [[0x11938, 0x1f203], [0x265d9, 0x2b540]] | ||
``` |
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,117 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl | ||
import numpy as np | ||
|
||
|
||
class Reduce_log_sum(RunAll): | ||
@staticmethod | ||
def reduce_log_sum_fp8x23(): | ||
def reduce_log_sum_export_do_not_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.int64) | ||
keepdims = False | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape) | ||
y = np.log(np.sum(x, axis=tuple(axes), keepdims=False)) | ||
|
||
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 = "reduce_log_sum_fp8x23_export_do_not_keepdims" | ||
make_test( | ||
[x], y, "input_0.reduce_log_sum(2, false)", name) | ||
|
||
def reduce_log_sum_export_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.int64) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64) | ||
y = np.log(np.sum(x, axis=tuple(axes), keepdims=True)) | ||
|
||
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 = "reduce_log_sum_fp8x23_export_keepdims" | ||
make_test( | ||
[x], y, "input_0.reduce_log_sum(2, true)", name) | ||
|
||
def reduce_log_sum_axis_0(): | ||
shape = [3, 3, 3] | ||
axes = np.array([0], dtype=np.int64) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1), shape) | ||
y = np.log(np.sum(x, axis=tuple(axes), keepdims=True)) | ||
|
||
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 = "reduce_log_sum_fp8x23_export_negative_axes_keepdims" | ||
make_test( | ||
[x], y, "input_0.reduce_log_sum(0, true)", name) | ||
|
||
|
||
reduce_log_sum_export_do_not_keepdims() | ||
reduce_log_sum_export_keepdims() | ||
reduce_log_sum_axis_0() | ||
|
||
@staticmethod | ||
def reduce_log_sum_fp16x16(): | ||
def reduce_log_sum_export_do_not_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.int64) | ||
keepdims = False | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64) | ||
y = np.log(np.sum(x, axis=tuple(axes), keepdims=False)) | ||
|
||
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 = "reduce_log_sum_fp16x16_export_do_not_keepdims" | ||
make_test( | ||
[x], y, "input_0.reduce_log_sum(2, false)", name) | ||
|
||
def reduce_log_sum_export_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.int64) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64) | ||
y = np.log(np.sum(x, axis=tuple(axes), keepdims=True)) | ||
|
||
|
||
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 = "reduce_log_sum_fp16x16_export_keepdims" | ||
make_test( | ||
[x], y, "input_0.reduce_log_sum(2, true)", name) | ||
|
||
def reduce_log_sum_axis_0(): | ||
shape = [2, 2, 2] | ||
axes = np.array([0], dtype=np.int64) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64) | ||
y = np.log(np.sum(x, axis=tuple(axes), keepdims=True)) | ||
|
||
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 = "reduce_log_sum_fp16x16_export_negative_axes_keepdims" | ||
make_test( | ||
[x], y, "input_0.reduce_log_sum(0, true)", name) | ||
|
||
|
||
reduce_log_sum_export_do_not_keepdims() | ||
reduce_log_sum_export_keepdims() | ||
reduce_log_sum_axis_0() |
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 |
---|---|---|
|
@@ -62,4 +62,5 @@ mod sequence_insert; | |
mod concat_from_sequence; | ||
mod is_nan; | ||
mod is_inf; | ||
mod reduce_log_sum; | ||
mod erf; |
Oops, something went wrong.