Eltwise
General
Forward
The eltwise primitive applies an operation to every element of the tensor (the variable names follow the standard Naming Conventions):

For notational convenience, in the formulas below we will denote individual element of
,
,
, and
tensors via s, d, ds, and dd respectively.




The following operations are supported:
Operation | oneDNN algorithm kind | Forward formula | Backward formula (from src) | Backward formula (from dst) |
---|---|---|---|---|



Difference Between Forward Training and Forward Inference
There is no difference between the dnnl_forward_training and dnnl_forward_inference propagation kinds.
Backward
The backward propagation computes
based on
and
tensors. However, some operations support a computation using
memory produced during the forward propagation. Refer to the table above for a list of operations supporting destination as input memory and the corresponding formulas.




Exceptions
The eltwise primitive with algorithm round does not support backward propagation.
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 |
---|---|
![]() | DNNL_ARG_SRC |
![]() | DNNL_ARG_DST |
![]() | DNNL_ARG_DIFF_SRC |
![]() | DNNL_ARG_DIFF_DST |
![]() | DNNL_ARG_ATTR_MULTIPLE_POST_OP(binary_post_op_position) | DNNL_ARG_SRC_1 |
Implementation Details
General Notes
- All eltwise primitives have a common initialization function (e.g., dnnl::eltwise_forward::desc::desc()) which takes both parameters
, and
. These parameters are ignored if they are unused.
- The memory format and data type for
and
are assumed to be the same, and in the API are typically referred as
data(e.g., seedata_descin dnnl::eltwise_forward::desc::desc()). The same holds forand
. The corresponding memory descriptors are referred to as
diff_data_desc. - Both forward and backward propagation support in-place operations, meaning that
can be used as input and output for forward propagation, and
can be used as input and output for backward propagation. In case of an in-place operation, the original data will be overwritten. Note, however, that some algorithms for backward propagation require original
, hence the corresponding forward propagation should not be performed in-place for those algorithms. Algorithms that use
for backward propagation can be safely done in-place.
- For some operations it might be beneficial to compute backward propagation based on
, rather than on
, for improved performance.
- For logsigmoid original formula
was replaced by
for numerical stability.
For operations supporting destination memory as input,
can be used instead of
when backward propagation is computed. This enables several performance optimizations (see the tips below).


Data Type Support
The eltwise primitive supports the following combinations of data types:
Propagation | Source / Destination | Intermediate data type |
---|---|---|
forward / backward | f32, bf16 | f32 |
forward | f16 | f16 |
forward | s32 / s8 / u8 | f32 |
There might be hardware and/or implementation specific restrictions. Check Implementation Limitations section below.
Here the intermediate data type means that the values coming in are first converted to the intermediate data type, then the operation is applied, and finally the result is converted to the output data type.
Data Representation
The eltwise primitive works with arbitrary data tensors. There is no special meaning associated with any logical dimensions.
Post-Ops and Attributes
Performance Tips
- For backward propagation, use the same memory format for
,
, and
(the format of the
and
are always the same because of the API). Different formats are functionally supported but lead to highly suboptimal performance.
- Use in-place operations whenever possible (see caveats in General Notes).
- As mentioned above for all operations supporting destination memory as input, one can use the
tensor instead of
. This enables the following potential optimizations for training:
- Such operations can be safely done in-place.
- Moreover, such operations can be fused as a post-op with the previous operation if that operation does not require its
to compute the backward propagation (e.g., if the convolution operation satisfies these conditions).
Example
This C++ API example demonstrates how to create and execute an Element-wise primitive in forward training propagation mode.