# Reduce¶

Computes a reduction across dimensions of an input tensor into an output tensor.

## Attributes¶

operation Reduce operation can be one of:

• SUM Sums the elements.

• PROD Multiplies the elements.

• MAX Retrieves the maximum element.

• MIN Retrieves the minimum element.

• AVG Computes the average of the elements.

axes The axes to reduce, represented as a bitmask. For example, when $$axes=6$$, dims 1 and 2 are reduced.

keep_dims Controls whether to preserve the original rank by keeping the reduced dimensions (with a dimension of 1), or to reduce the tensor’s rank.

## Inputs¶

input: tensor of type T.

## Outputs¶

output: tensor of type T.

## Data Types¶

T: int8, int32, float16, float32

## Shape Information¶

input is a tensor with a shape of $$[a_0,...,a_n], n \geq 1$$

when keep_dims is set to True, output is a tensor with a shape of $$[b_0,...,b_n]$$ where: $$b_i = \begin{cases}a_i, & i \notin \text{axes} \\1, & i \in \text{axes} \\\end{cases}$$

when keep_dims is set to False, output is a tensor with a shape of $$[b_0,...,b_m]$$ where: $$b_j = a_{j-r(j)}$$ $$j \in m, m=n-r(n), \text{r(x) is the number of bits in axes that are set and their index is less then or equal to x}$$

## Examples¶

Reduce
in1 = network.add_input("input1", dtype=trt.float32, shape=(1, 2, 2, 3))
layer = network.add_reduce(in1, op=trt.ReduceOperation.MAX, axes=4, keep_dims=True)
network.mark_output(layer.get_output(0))

inputs[in1.name] = np.array(
[
[
[[-3.0, -2.0, -1.0], [0.0, 1.0, 2.0]],
[[3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
]
]
)

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array(
[
[
[[0.0, 1.0, 2.0]],
[[6.0, 7.0, 8.0]],
]
]
)

in1 = network.add_input("input1", dtype=trt.float32, shape=(1, 2, 2, 3))
layer = network.add_reduce(in1, op=trt.ReduceOperation.PROD, axes=6, keep_dims=False)
network.mark_output(layer.get_output(0))

inputs[in1.name] = np.array(
[
[
[[-3.0, -2.0, -1.0], [0.0, 1.0, 2.0]],
[[3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
]
]
)

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array([[0.0, -56.0, -80.0]])