Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rfc: dropout primitive attribute #1708

Open
wants to merge 1 commit into
base: rfcs
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions rfcs/20230818-Dropout/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Introducing Dropout primitive attribute

## Introduction

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.

This idea was [proposed](https://github.com/oneapi-src/oneDNN/pull/760) some time ago.
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.

## Proposal

Additional function to set dropout attibute in C API:

```c
/// Returns the parameters of a drop out attribute.
///
/// @param attr Primitive attributes.
/// @param mask_desc Output memory descriptor of a drop out mask.
/// @returns #dnnl_success on success and a status describing the error
/// otherwise.
dnnl_status_t DNNL_API dnnl_primitive_attr_get_dropout(
dzarukin marked this conversation as resolved.
Show resolved Hide resolved
const_dnnl_primitive_attr_t attr, const_dnnl_memory_desc_t *mask_desc);

/// Set up drop-out primitive attribute.
///
/// @param attr Primitive attributes.
/// @param mask_desc Output memory descriptor of a drop out mask.
/// @returns #dnnl_success on success and a status describing the error
/// otherwise.
dnnl_status_t DNNL_API dnnl_primitive_attr_set_dropout(
dnnl_primitive_attr_t attr, const_dnnl_memory_desc_t mask_desc);
```

for C++ API:
```c++
/// Returns the parameters of a drop out attribute.
///
/// @param mask_desc Output memory descriptor of a drop out mask.
void get_dropout(memory::desc &mask_desc) const {
const_dnnl_memory_desc_t cdesc;
error::wrap_c_api(
dnnl_primitive_attr_get_dropout(get(), &cdesc),
"could not get parameters of a dropout attribute");
dnnl_memory_desc_t cloned_md = nullptr;
error::wrap_c_api(dnnl_memory_desc_clone(&cloned_md, cdesc),
"could not clone a memory descriptor");
mask_desc = memory::desc(cloned_md);
enabled = enabled_u8;
}

/// Set up drop-out.
///
/// @param mask_desc Output memory descriptor of a drop out mask.
void set_dropout(const memory::desc &mask_desc) {
error::wrap_c_api(dnnl_primitive_attr_set_dropout(get(), mask_desc.get()),
"could not set dropout primitive attribute");
}
```
and runtime dropout arguments: output mask, which can be used in backward pass,
dropout probability and seed.

```c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(random spot): We might need to introduce a standalone Dropout primitive to support frrameworks like ONNX that registers operations supported by the backend. In the current proposal Dropout functionality will be limited to a few patterns, but the rest patterns will not be able to implement Dropout functionality using oneDNN. Please double check with the frameworks if the solution works for them as well.
+@georgen117

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the question here is "is there a benefit to support dropout primitive in oneDNN"?
For the case of ONNX, isn't it ok if the oneDNN provider provides the dropout operation without using oneDNN, but uses oneDNN when dropout fusion occurs?

/// Arguments for drop out output mask.
#define DNNL_ARG_ATTR_DROPOUT_MASK 16385

/// Arguments for drop out probability param.
#define DNNL_ARG_ATTR_DROPOUT_PROBABILITY 16386

/// Arguments for drop out seed.
#define DNNL_ARG_ATTR_DROPOUT_SEED 16387
```
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.