Elementwise¶
The elementwise primitive applies an operation to every element of the tensor. Variable names follow the standard Conventions.
for \(\overline{x} = (x_0, \ldots, x_n)\).
Forward¶
The following forward operations are supported. Here \(s\) and \(d\) denote \(\src\) and \(\dst\), tensor values respectively.
Elementwise algorithm |
Forward formula |
---|---|
\(d = \begin{cases} s & \text{if}\ s > 0 \\ -s & \text{if}\ s \leq 0 \end{cases}\) |
|
\(d = \begin{cases} \alpha & \text{if}\ s > \alpha \geq 0 \\ s & \text{if}\ 0 < s \leq \alpha \\ 0 & \text{if}\ s \leq 0 \end{cases}\) |
|
\(d = \begin{cases} \beta & \text{if}\ s > \beta \geq \alpha \\ s & \text{if}\ \alpha < s \leq \beta \\ \alpha & \text{if}\ s \leq \alpha \end{cases}\) |
|
\(d = \begin{cases} s & \text{if}\ s > 0 \\ \alpha (e^s - 1) & \text{if}\ s \leq 0 \end{cases}\) |
|
\(d = e^s\) |
|
\(d = 0.5 s (1 + erf[\frac{s}{\sqrt{2}}])\) |
|
\(d = 0.5 s (1 + tanh[\sqrt{\frac{2}{\pi}} (s + 0.044715 s^3)])\) |
|
\(d = \alpha s + \beta\) |
|
\(d = \log_{e}{s}\) |
|
\(d = \frac{1}{1+e^{-s}}\) |
|
\(d = \alpha s^{\beta}\) |
|
\(d = \begin{cases} s & \text{if}\ s > 0 \\ \alpha s & \text{if}\ s \leq 0 \end{cases}\) |
|
\(d = round(s)\) |
|
\(d = \log_{e}(1+e^s)\) |
|
\(d = \sqrt{s}\) |
|
\(d = s^2\) |
|
\(d = \frac{s}{1+e^{-\alpha s}}\) |
|
\(d = \tanh{s}\) |
Backward¶
The backward propagation computes \(\diffsrc(\overline{s})\), based on \(\diffdst(\overline{s})\) and \(\src(\overline{s})\). However, some operations support a computation using \(\dst(\overline{s})\) memory produced during forward propagation. Refer to the table above for a list of operations supporting destination as input memory and the corresponding formulas.
The following backward operations are supported. Here \(s\), \(d\), \(ds\) and \(dd\) denote \(\src\), \(\dst\), \(\diffsrc\), and a \(\diffdst\) tensor values respectively.
Elementwise algorithm |
Backward formula |
---|---|
\(ds = \begin{cases} dd & \text{if}\ s > 0 \\ -dd & \text{if}\ s < 0 \\ 0 & \text{if}\ s = 0 \end{cases}\) |
|
\(ds = \begin{cases} dd & \text{if}\ 0 < s \leq \alpha, \\ 0 & \text{otherwise}\ \end{cases}\) |
|
\(ds = \begin{cases} dd & \text{if}\ \alpha < s \leq \beta \\ 0 & \text{otherwise}\ \end{cases}\) |
|
\(ds = \begin{cases} dd & \text{if}\ s > 0 \\ dd \cdot \alpha e^s & \text{if}\ s \leq 0 \end{cases}\) |
|
\(ds = \begin{cases} dd & \text{if}\ d > 0 \\ dd \cdot (d + \alpha) & \text{if}\ d \leq 0 \end{cases}\) only if \(\alpha \geq 0\) |
|
\(ds = dd \cdot e^s\) |
|
\(ds = dd \cdot d\) |
|
\(ds = dd \cdot \left(0.5 + 0.5 \, \mathrm{erf}\left({\frac{s}{\sqrt{2}}}\right) + \frac{s}{\sqrt{2\pi}}e^{-0.5s^{2}}\right)\) |
|
\(\begin{array}{rl} ds = & dd \\ & \cdot 0.5 (1 + \tanh[\sqrt{\frac{2}{\pi}} (s + 0.044715 s^3)]) \\ & \cdot (1 + \sqrt{\frac{2}{\pi}} (s + 0.134145 s^3) \\ & \cdot (1 - \tanh[\sqrt{\frac{2}{\pi}} (s + 0.044715 s^3)]) ) \end{array}\) |
|
\(ds = \alpha \cdot dd\) |
|
\(ds = \frac{dd}{s}\) |
|
\(ds = \frac{dd}{1+e^{-s}} \cdot (1 - \frac{1}{1+e^{-s}})\) |
|
\(ds = dd \cdot d \cdot (1 - d)\) |
|
\(ds = dd \cdot \alpha \beta s^{\beta - 1}\) |
|
\(ds = \begin{cases} dd & \text{if}\ s > 0 \\ \alpha \cdot dd & \text{if}\ s \leq 0 \end{cases}\) |
|
\(ds = \begin{cases} dd & \text{if}\ d > 0 \\ \alpha \cdot dd & \text{if}\ d \leq 0 \end{cases}\) only if \(alpha \geq 0\) |
|
\(ds = \frac{dd}{1 + e^{-s}}\) |
|
\(ds = \frac{dd}{2\sqrt{s}}\) |
|
\(ds = \frac{dd}{2d}\) |
|
\(ds = dd \cdot 2 s\) |
|
\(ds = \frac{dd}{1 + e^{-\alpha s}}(1 + \alpha s (1 - \frac{1}{1 + e^{-\alpha s}}))\) |
|
\(ds = dd \cdot (1 - \tanh^2{s})\) |
|
\(ds = dd \cdot (1 - d^2)\) |
Difference Between Forward Training and Forward Inference¶
There is no difference between the #dnnl_forward_training and #dnnl_forward_inference propagation kinds.
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\) |
|
\(\dst\) |
|
\(\diffsrc\) |
|
\(\diffdst\) |
Operation Details¶
The
dnnl::eltwise_forward::desc::desc()
anddnnl::eltwise_backward::desc::desc()
constructors take both parameters \(\alpha\), and \(\beta\). These parameters are ignored if they are unused by the algorithm.The memory format and data type for \(\src\) and \(\dst\) are assumed to be the same, and in the API are typically denoted as
data
(for examplednnl::eltwise_forward::desc::desc()
has adata_desc
argument). The same holds for \(\diffsrc\) and \(\diffdst\). The corresponding memory descriptors are denoted asdiff_data_desc
.Both forward and backward propagation support in-place operations, meaning that \(\src\) can be used as input and output for forward propagation, and \(\diffdst\) 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 \(\src\), hence the corresponding forward propagation should not be performed in-place for those algorithms. Algorithms that use \(\dst\) for backward propagation can be safely done in-place.
For some operations it might be beneficial to compute backward propagation based on \(\dst(\overline{s})\), rather than on \(\src(\overline{s})\), for improved performance.
Note
For operations supporting destination memory as input, \(\dst\) can be used instead of \(\src\) when backward propagation is computed. This enables several performance optimizations (see the tips below).
Data Type Support¶
The eltwise primitive should support the following combinations of data types.
Note
Here we abbreviate data types names for readability. For example, dnnl::memory::data_type::f32
is
abbreviated to f32
.
Propagation |
Source / Destination |
Intermediate data type |
---|---|---|
forward / backward |
||
forward |
||
forward |
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¶
The eltwise primitive does not have to support any post-ops or attributes.
API¶
-
struct dnnl::eltwise_forward : public dnnl::primitive¶
Elementwise unary operation forward propagation primitive.
Public Functions
-
eltwise_forward()¶
Default constructor. Produces an empty object.
-
eltwise_forward(const primitive_desc &pd)¶
Constructs an eltwise forward propagation primitive.
- Parameters
pd – Primitive descriptor for an eltwise forward propagation primitive.
-
struct desc¶
Descriptor for an elementwise forward propagation primitive.
Public Functions
-
desc(prop_kind aprop_kind, algorithm aalgorithm, const memory::desc &data_desc, float alpha = 0, float beta = 0)¶
Constructs a descriptor for an elementwise forward propagation primitive.
- Parameters
aprop_kind – Propagation kind. Possible values are dnnl::prop_kind::forward_training, and dnnl::prop_kind::forward_inference.
aalgorithm – Elementwise algorithm kind.
data_desc – Source and destination memory descriptors.
alpha – The alpha parameter for the elementwise operation. Specific meaning depends on the algorithm.
beta – The beta parameter for the elementwise operation. Specific meaning depends on the algorithm.
-
desc(prop_kind aprop_kind, algorithm aalgorithm, const memory::desc &data_desc, float alpha = 0, float beta = 0)¶
-
struct primitive_desc : public dnnl::primitive_desc¶
Primitive descriptor for an elementwise forward propagation primitive.
Public Functions
-
primitive_desc()¶
Default constructor. Produces an empty object.
-
primitive_desc(const desc &adesc, const engine &aengine, bool allow_empty = false)¶
Constructs a primitive descriptor for an elementwise forward propagation primitive.
- Parameters
adesc – Descriptor for an elementwise forward propagation primitive.
aengine – Engine to use.
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.
-
primitive_desc(const desc &adesc, const primitive_attr &attr, const engine &aengine, bool allow_empty = false)¶
Constructs a primitive descriptor for an elementwise forward propagation primitive.
- Parameters
adesc – Descriptor for an elementwise forward propagation primitive.
aengine – Engine to use.
attr – Primitive attributes to use.
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.
-
primitive_desc()¶
-
eltwise_forward()¶
-
struct dnnl::eltwise_backward : public dnnl::primitive¶
Elementwise unary operation backward propagation primitive.
Public Functions
-
eltwise_backward()¶
Default constructor. Produces an empty object.
-
eltwise_backward(const primitive_desc &pd)¶
Constructs an eltwise backward propagation primitive.
- Parameters
pd – Primitive descriptor for an eltwise backward propagation primitive.
-
struct desc¶
Descriptor for an elementwise backward propagation primitive.
Public Functions
-
desc(algorithm aalgorithm, const memory::desc &diff_data_desc, const memory::desc &data_desc, float alpha = 0, float beta = 0)¶
Constructs a descriptor for an elementwise backward propagation primitive.
- Parameters
aalgorithm – Elementwise algorithm kind.
diff_data_desc – Diff source and destination memory descriptors.
data_desc – Source memory descriptor.
alpha – The alpha parameter for the elementwise operation. Specific meaning depends on the algorithm.
beta – The beta parameter for the elementwise operation. Specific meaning depends on the algorithm.
-
desc(algorithm aalgorithm, const memory::desc &diff_data_desc, const memory::desc &data_desc, float alpha = 0, float beta = 0)¶
-
struct primitive_desc : public dnnl::primitive_desc¶
Primitive descriptor for eltwise backward propagation.
Public Functions
-
primitive_desc()¶
Default constructor. Produces an empty object.
-
primitive_desc(const desc &adesc, const engine &aengine, const eltwise_forward::primitive_desc &hint_fwd_pd, bool allow_empty = false)¶
Constructs a primitive descriptor for an elementwise backward propagation primitive.
- Parameters
adesc – Descriptor for an elementwise backward propagation primitive.
aengine – Engine to use.
hint_fwd_pd – Primitive descriptor for an elementwise forward propagation primitive. It is used as a hint for deciding which memory format to use.
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.
-
primitive_desc(const desc &adesc, const primitive_attr &attr, const engine &aengine, const eltwise_forward::primitive_desc &hint_fwd_pd, bool allow_empty = false)¶
Constructs a primitive descriptor for an elementwise backward propagation primitive.
- Parameters
adesc – Descriptor for an elementwise backward propagation primitive.
attr – Primitive attributes to use.
aengine – Engine to use.
hint_fwd_pd – Primitive descriptor for an elementwise forward propagation primitive. It is used as a hint for deciding which memory format to use.
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.
-
primitive_desc()¶
-
eltwise_backward()¶