Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Max #632

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .vscode/extensions.json

This file was deleted.

6 changes: 0 additions & 6 deletions .vscode/settings.json

This file was deleted.

Binary file not shown.
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
* [tensor.gather\_elements](framework/operators/tensor/tensor.gather\_elements.md)
* [tensor.gather\_nd](framework/operators/tensor/tensor.gather\_nd.md)
* [tensor.reduce\_min](framework/operators/tensor/tensor.reduce\_min.md)
* [tensor.reduce_max](framework/operators/tensor/tensor.reduce_max.md)
* [tensor.shrink](framework/operators/tensor/tensor.shrink.md)
* [tensor.reduce\_mean](framework/operators/tensor/tensor.reduce\_mean.md)
* [tensor.pow](framework/operators/tensor/tensor.pow.md)
Expand Down
245 changes: 123 additions & 122 deletions docs/framework/compatibility.md

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions docs/framework/operators/tensor/tensor.reduce_max.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## tensor.reduce_max

```rust
fn reduce_max(self: @Tensor<T>, axes: Option<Span<usize>>, keepdims: Option<bool>, noop_with_empty_axes: Option<bool>) -> Tensor<T>;
```

Computes the max of the input tensor's elements along the provided axes.

## Args

- `self`(`@Tensor<T>`) - The input tensor.
- `axes`(`Option<Span<usize>>`) - Optional input list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor if 'noop_with_empty_axes' is false, else act as an Identity op when 'noop_with_empty_axes' is true.
- `keepdims`(`Option<bool>`) - Keep the reduced dimension or not, default true means keep reduced dimension.
- `noop_with_empty_axes`(`Option<bool>`) - Defines behavior if 'axes' is empty. Default behavior with 'false' is to reduce all axes. When axes is empty and this attribute is set to true, input tensor will not be reduced,and the output tensor would be equivalent to input tensor.

## Panics

- Panics if axis is not in the range of the input tensor's dimensions.

## Returns

A new `Tensor<T>` instance with the specified axes reduced by maximum of its elements.

## Examples

```rust
use array::{ArrayTrait, SpanTrait};

use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor};

fn reduce_max_example() -> Tensor<u32> {
let tensor = TensorTrait::<u32>::new(
shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(),
);

// We can call `reduce_max` function as follows.
return tensor.reduce_max(axes: array![1].span(),
keepdims: Option::None(()),
noop_with_empty_axes: Option::None(()));
}
>>> [[2,3],[6,7]]
```
201 changes: 201 additions & 0 deletions nodegen/node/reduce_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import numpy as np
from nodegen.node import RunAll
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl

class Reduce_max(RunAll):
@staticmethod
def reduce_max_u32():
def reduce_max_1D():
x = np.array([0, 1, 2, 3]).astype(np.uint32)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.uint32)

x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())

name = "reduce_max_u32_1D"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def reduce_max_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.uint32)

x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())

name = "reduce_max_u32_2D_default"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=False).astype(np.uint32)

x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())

name = "reduce_max_u32_2D_keepdims"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::Some(false), Option::None(()))", name)

def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.maximum.reduce(x, axis=(1), keepdims=True).astype(np.uint32)

x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())

name = "reduce_max_u32_2D_axis_1"
make_test([x], y, "input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)

default()
keepdims()
axis_1()
reduce_max_1D()
reduce_max_2D()

@staticmethod
def reduce_max_i32():
def reduce_max_1D():
x = np.array([0, 1, 2, 3]).astype(np.int32)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.int32)

x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())

name = "reduce_max_i32_1D"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def reduce_max_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.int32)

x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())

name = "reduce_max_i32_2D_default"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=False).astype(np.int32)

x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())

name = "reduce_max_i32_2D_keepdims"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::Some(false), Option::None(()))", name)

def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.maximum.reduce(x, axis=(1), keepdims=True).astype(np.int32)

x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())

name = "reduce_max_i32_2D_axis_1"
make_test([x], y, "input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)

default()
keepdims()
axis_1()
reduce_max_1D()
reduce_max_2D()

@staticmethod
def reduce_max_i8():
def reduce_max_1D():
x = np.array([0, 1, 2, 3]).astype(np.int8)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.int8)

x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())

name = "reduce_max_i8_1D"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def reduce_max_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.int8)

x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())

name = "reduce_max_i8_2D_default"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=False).astype(np.int8)

x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())

name = "reduce_max_i8_2D_keepdims"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::Some(false), Option::None(()))", name)

def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.maximum.reduce(x, axis=(1), keepdims=True).astype(np.int8)

x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())

name = "reduce_max_i8_2D_axis_1"
make_test([x], y, "input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)

default()
keepdims()
axis_1()

reduce_max_1D()
reduce_max_2D()

@staticmethod
def reduce_max_fp16x16():
def reduce_max_1D():
x = np.array([0, 1, 2, 3]).astype(np.float16)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.float16)

x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())

name = "reduce_max_fp16x16_1D"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def reduce_max_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=True).astype(np.int64)

x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())

name = "reduce_max_fp16x16_2D_default"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::None(()), Option::None(()))", name)

def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.maximum.reduce(x, axis=None, keepdims=False).astype(np.int64)

x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())

name = "reduce_max_fp16x16_2D_keepdims"
make_test([x], y, "input_0.reduce_max(Option::None(()), Option::Some(false), Option::None(()))", name)

def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.maximum.reduce(x, axis=(1), keepdims=True).astype(np.int64)

x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())

name = "reduce_max_fp16x16_2D_axis_1"
make_test([x], y, "input_0.reduce_max(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)

default()
keepdims()
axis_1()
reduce_max_1D()
reduce_max_2D()
Loading
Loading