Skip to content

Commit b19eb59

Browse files
committed
rfc: dropout primitive attribute
1 parent 0cb412b commit b19eb59

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

rfcs/20230818-Dropout/README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Introducing Dropout primitive attribute
2+
3+
## Introduction
4+
5+
In many DNN and GNN models, [Dropout](https://en.wikipedia.org/wiki/Convolutional_neural_network#Dropout) is used to improve training results. In some cases, this layer can take a significant amount of time. To enhance the performance of training, we want to optimize it and, as a result, fuse it with the previous primitive.
6+
7+
This idea was [proposed](https://github.com/oneapi-src/oneDNN/pull/760) some time ago.
8+
Between post-op and attribute implementation, the primitive attribute was chosen to support complex primitives, like, RNN, where post-op semantics are not well defined.
9+
10+
## Proposal
11+
12+
Additional function to set dropout attibute in C API:
13+
14+
```c
15+
/// Returns the parameters of a drop out attribute.
16+
///
17+
/// @param attr Primitive attributes.
18+
/// @param mask_desc Output memory descriptor of a drop out mask.
19+
/// @returns #dnnl_success on success and a status describing the error
20+
/// otherwise.
21+
dnnl_status_t DNNL_API dnnl_primitive_attr_get_dropout(
22+
const_dnnl_primitive_attr_t attr, const_dnnl_memory_desc_t *mask_desc);
23+
24+
/// Set up drop-out primitive attribute.
25+
///
26+
/// @param attr Primitive attributes.
27+
/// @param mask_desc Output memory descriptor of a drop out mask.
28+
/// @returns #dnnl_success on success and a status describing the error
29+
/// otherwise.
30+
dnnl_status_t DNNL_API dnnl_primitive_attr_set_dropout(
31+
dnnl_primitive_attr_t attr, const_dnnl_memory_desc_t mask_desc);
32+
```
33+
34+
for C++ API:
35+
```c++
36+
/// Returns the parameters of a drop out attribute.
37+
///
38+
/// @param mask_desc Output memory descriptor of a drop out mask.
39+
void get_dropout(memory::desc &mask_desc) const {
40+
const_dnnl_memory_desc_t cdesc;
41+
uint8_t enabled_u8;
42+
error::wrap_c_api(
43+
dnnl_primitive_attr_get_dropout(get(), &enabled_u8, &cdesc),
44+
"could not get parameters of a dropout attribute");
45+
dnnl_memory_desc_t cloned_md = nullptr;
46+
error::wrap_c_api(dnnl_memory_desc_clone(&cloned_md, cdesc),
47+
"could not clone a memory descriptor");
48+
mask_desc = memory::desc(cloned_md);
49+
enabled = enabled_u8;
50+
}
51+
52+
/// Set up drop-out.
53+
///
54+
/// @param mask_desc Output memory descriptor of a drop out mask.
55+
void set_dropout(const memory::desc &mask_desc) {
56+
error::wrap_c_api(dnnl_primitive_attr_set_dropout(get(), enabled, mask_desc.get()),
57+
"could not set dropout primitive attribute");
58+
}
59+
```
60+
and runtime dropout arguments: output mask, which can be used in backward pass,
61+
dropout probability and seed.
62+
63+
```c
64+
/// Arguments for drop out output mask.
65+
#define DNNL_ARG_ATTR_DROPOUT_MASK 16385
66+
67+
/// Arguments for drop out probability param.
68+
#define DNNL_ARG_ATTR_DROPOUT_PROBABILITY 16386
69+
70+
/// Arguments for drop out seed.
71+
#define DNNL_ARG_ATTR_DROPOUT_SEED 16387
72+
```
73+
In most frameworks, the dropout operation is enabled only for the forward training pass, while for the backward pass, the binary multiplication operation can be used. For forward inference, nothing should be done to the tensor.
74+

0 commit comments

Comments
 (0)