-
Notifications
You must be signed in to change notification settings - Fork 85
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 #428 from hakymulla/reducesumsq
Reduce Sum Square Operator
- Loading branch information
Showing
65 changed files
with
1,483 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
Empty file.
38 changes: 38 additions & 0 deletions
38
docs/framework/operators/tensor/tensor.reduce_sum_square.md
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,38 @@ | ||
## tensor.reduce_sum_square | ||
|
||
```rust | ||
fn reduce_sum_square(self: @Tensor<T>, axis: usize, keepdims: bool) -> Tensor<T>; | ||
``` | ||
|
||
Computes the sum square 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_sum_square_example() -> Tensor<u32> { | ||
|
||
let mut shape = ArrayTrait::<usize>::new(); | ||
shape.append(2); | ||
shape.append(2); | ||
let mut data = ArrayTrait::new(); | ||
data.append(1); | ||
data.append(2); | ||
data.append(3); | ||
data.append(4); | ||
let tensor = TensorTrait::<u32>::new(shape.span(), data.span()); | ||
|
||
We can call `reduce_sum_square` function as follows. | ||
return tensor.reduce_sum_square(axis: 1, keepdims: true); | ||
} | ||
>>> [[5, 25]] | ||
``` |
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,266 @@ | ||
import numpy as np | ||
from nodegen.node import RunAll | ||
from ..helpers import make_node, make_test, to_fp, Tensor, Dtype, FixedImpl | ||
import numpy as np | ||
|
||
|
||
class Reduce_sum_square(RunAll): | ||
@staticmethod | ||
def reduce_sum_square_fp8x23(): | ||
def reduce_sum_square_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.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int64) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_fp8x23_export_do_not_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, false)", name) | ||
|
||
def reduce_sum_square_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.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_fp8x23_export_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, true)", name) | ||
|
||
def reduce_sum_square_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, dtype=np.float32), shape).astype(np.int64) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64) | ||
|
||
x = Tensor(Dtype.FP8x23, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_fp8x23_export_negative_axes_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(0, true)", name) | ||
|
||
|
||
reduce_sum_square_export_do_not_keepdims() | ||
reduce_sum_square_export_keepdims() | ||
reduce_sum_square_axis_0() | ||
|
||
@staticmethod | ||
def reduce_sum_square_fp16x16(): | ||
def reduce_sum_square_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.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int64) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP16x16, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_fp16x16_export_do_not_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, false)", name) | ||
|
||
def reduce_sum_square_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.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP16x16, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_fp16x16_export_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, true)", name) | ||
|
||
def reduce_sum_square_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.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64) | ||
|
||
x = Tensor(Dtype.FP16x16, x.shape, x.flatten()) | ||
y = Tensor(Dtype.FP16x16, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_fp16x16_export_negative_axes_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(0, true)", name) | ||
|
||
|
||
reduce_sum_square_export_do_not_keepdims() | ||
reduce_sum_square_export_keepdims() | ||
reduce_sum_square_axis_0() | ||
|
||
@staticmethod | ||
def reduce_sum_square_i8(): | ||
def reduce_sum_square_export_do_not_keepdims(): | ||
shape = [2, 2, 2] | ||
axes = np.array([2], dtype=np.int8) | ||
keepdims = False | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int8) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int8) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_i8_export_do_not_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, false)", name) | ||
|
||
def reduce_sum_square_export_keepdims(): | ||
shape = [2, 2, 2] | ||
axes = np.array([2], dtype=np.int8) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int8) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int8) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_i8_export_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, true)", name) | ||
|
||
def reduce_sum_square_axis_0(): | ||
shape = [2, 2, 2] | ||
axes = np.array([0], dtype=np.int8) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int8) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int8) | ||
|
||
x = Tensor(Dtype.I8, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I8, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_i8_export_negative_axes_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(0, true)", name) | ||
|
||
|
||
reduce_sum_square_export_do_not_keepdims() | ||
reduce_sum_square_export_keepdims() | ||
reduce_sum_square_axis_0() | ||
|
||
@staticmethod | ||
def reduce_sum_square_i32(): | ||
def reduce_sum_square_export_do_not_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.int32) | ||
keepdims = False | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int32) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int32) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_i32_export_do_not_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, false)", name) | ||
|
||
def reduce_sum_square_export_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.int32) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int32) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int32) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_i32_export_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, true)", name) | ||
|
||
def reduce_sum_square_axis_0(): | ||
shape = [3, 3, 3] | ||
axes = np.array([0], dtype=np.int32) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int32) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int32) | ||
|
||
x = Tensor(Dtype.I32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.I32, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_i32_export_negative_axes_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(0, true)", name) | ||
|
||
|
||
reduce_sum_square_export_do_not_keepdims() | ||
reduce_sum_square_export_keepdims() | ||
reduce_sum_square_axis_0() | ||
|
||
@staticmethod | ||
def reduce_sum_square_u32(): | ||
def reduce_sum_square_export_do_not_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.uint32) | ||
keepdims = False | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.uint32) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.uint32) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_u32_export_do_not_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, false)", name) | ||
|
||
def reduce_sum_square_export_keepdims(): | ||
shape = [3, 2, 2] | ||
axes = np.array([2], dtype=np.uint32) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.uint32) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.uint32) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_u32_export_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(2, true)", name) | ||
|
||
def reduce_sum_square_axis_0(): | ||
shape = [3, 3, 3] | ||
axes = np.array([0], dtype=np.uint32) | ||
keepdims = True | ||
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.uint32) | ||
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.uint32) | ||
|
||
x = Tensor(Dtype.U32, x.shape, x.flatten()) | ||
y = Tensor(Dtype.U32, y.shape, y.flatten()) | ||
|
||
name = "reduce_sum_square_u32_export_negative_axes_keepdims" | ||
make_node([x], [y], name) | ||
make_test( | ||
[x], y, "input_0.reduce_sum_square(0, true)", name) | ||
|
||
|
||
reduce_sum_square_export_do_not_keepdims() | ||
reduce_sum_square_export_keepdims() | ||
reduce_sum_square_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
Oops, something went wrong.