# Reduction#

The reduction primitive performs a reduction operation on one or multiple arbitrary dimensions, with respect to a specified algorithm. Variable names follow the standard Conventions.

$\dst(f) = \mathop{reduce\_op}\limits_{r \in R}\src(r),$

where $$reduce\_op$$ can be max, min, sum, mul, mean, Lp-norm and Lp-norm-power-p, $$f$$ is an index in an idle dimension and $$r$$ is an index in a reduction dimension $$R$$.

The reduction algorithms are specified as follow.

Mean:

$\dst(f) = \frac{\sum\limits_{r \in R}\src(r)} {\|R\|},$

where $$|R|$$ is the size of a reduction dimension.

Lp-norm:

$\dst(f) = \root p \of {\mathop{eps\_op}(\sum\limits_{r \in R}|src(r)|^p, eps)},$

where $$eps\_op$$ can be max and sum.

Lp-norm-power-p:

$\dst(f) = \mathop{eps\_op}(\sum\limits_{r}|src(r)|^p, eps),$

where $$eps\_op$$ can be max and sum.

Note

• The reduction primitive requires the source and destination tensors to have the same number of dimensions.

• Dimensions which are reduced are of size 1 in the destination tensor.

• The reduction primitive does not have a notion of forward or backward propagations.

## Execution Arguments#

When executed, the inputs and outputs should be mapped to an execution argument index as specified by the following table.

Primitive input/output

Execution argument index

$$\src$$

DNNL_ARG_SRC

$$\dst$$

DNNL_ARG_DST

## Operation Details#

The dst memory format can be either specified explicitly or by using the special format tag any (recommended), in which case the primitive will derive the most appropriate memory format based on the format of the source tensor.

## Data Types Support#

The reduction primitive supports the following combinations of data types:

Propagation

Source / Destination

forward / backward

## Data Representation#

The reduction primitive works with arbitrary data tensors. There is no special meaning associated with any of the dimensions of a tensor.

## Attributes and Post-ops#

Type

Operation

Description

Restrictions

Attribute

Scales

Sets scale(s) for the corresponding tensor(s)

Int8 computations only

Attribute

Zero points

Sets zero point(s) for the corresponding tensors

Int8 computations only

post-op

Eltwise

Applies an elementwise operation to the result

post-op

Binary

Applies a binary operation to the result

## API#

struct reduction : public dnnl::primitive#

Reduction.

Public Functions

reduction() = default#

Default constructor. Produces an empty object.

struct primitive_desc : public dnnl::primitive_desc#

Primitive descriptor for a reduction primitive.

Public Functions

primitive_desc() = default#

Default constructor. Produces an empty object.

primitive_desc(const engine &aengine, algorithm aalgorithm, const memory::desc &src_desc, const memory::desc &dst_desc, float p, float eps, const primitive_attr &attr = default_attr(), bool allow_empty = false)#

Constructs a primitive descriptor for a reduction primitive using algorithm specific parameters, source and destination memory descriptors.

Note

Destination memory descriptor may be initialized with dnnl::memory::format_tag::any value of format_tag.

Parameters:
• aengine – Engine to use.

• p – algorithm specific parameter.

• eps – algorithm specific parameter.

• src_desc – Source memory descriptor.

• dst_desc – Destination memory descriptor.

• attr – Primitive attributes to use. Attributes are optional and default to empty attributes.

• allow_empty – A flag signifying whether construction is allowed to fail without throwing an exception. In this case an empty object will be produced. This flag is optional and defaults to false.

memory::desc src_desc() const#

Returns a source memory descriptor.

Returns:

Source memory descriptor.

Returns:

A zero memory descriptor if the primitive does not have a source parameter.

memory::desc dst_desc() const#

Returns a destination memory descriptor.

Returns:

Destination memory descriptor.

Returns:

A zero memory descriptor if the primitive does not have a destination parameter.

float get_p() const#

Returns a reduction P parameter.

Returns:

A reduction P parameter.

Returns:

Zero if the primitive does not have a reduction P parameter.

float get_epsilon() const#

Returns an epsilon.

Returns:

An epsilon.

Returns:

Zero if the primitive does not have an epsilon parameter.

algorithm get_algorithm() const#

Returns an algorithm kind.

Returns:

An algorithm kind.

Returns:

dnnl::algorithm::undef if the primitive does not have an algorithm parameter.