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

[oneDNN] Fix to #33282 , added support of X input broadcasting to oneDNN elementwise ops #33549

Merged
merged 4 commits into from
Jun 24, 2021

Conversation

jczaja
Copy link
Contributor

@jczaja jczaja commented Jun 14, 2021

PR types

Bug fixes

PR changes

OPs

Describe

This is fix to #33282 . It does implement broadcasting for elementwise X input as previosuly only input Y was having broadcasting support. Inplace support for oneDNN elementwise kernels was disabled.

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@jczaja jczaja added the Intel label Jun 14, 2021
@lidanqing-intel
Copy link
Contributor

@jczaja Hi next time could you please name the PR with more specific description? For example this PR could be extend broadcast elementwise_add op with X(1 dims) Y(2 dims).
It will be easier for release note

@lidanqing-intel lidanqing-intel self-requested a review June 15, 2021 07:45
@jczaja jczaja requested review from jakpiase and arlesniak June 15, 2021 08:22
@jczaja jczaja changed the title [oneDNN] Fix to #33282 [oneDNN] Fix to #33282 , added support of X input broadcasting to oneDNN elementwise_add/elementwise_mul Jun 15, 2021
// For Inplace src and and dst are the same memory object
const auto dst_memory =
is_inplaced ? src_x_memory : handler.AcquireDstMemory(z);
const auto dst_memory = handler.AcquireDstMemory(z);

Copy link
Contributor

Choose a reason for hiding this comment

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

So we remove the inplace support for elementwise_add and elementwise_mul ? But you plan to remove it permanently or you will still add inplace later ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inplace working with oneDNN caching is very hard , and there is little perf gain and some performance improvement. Among all of this elementwise ops for inplace are most difficult so I removed inplace support for elementwise oneDNN kernels.

@@ -674,15 +666,21 @@ class BinaryMKLDNNHandler : public platform::MKLDNNHandlerT<T, dnnl::binary> {
const auto dst_tz =
(z == nullptr) ? src_x_tz : framework::vectorize(z->dims());
Copy link
Contributor

@lidanqing-intel lidanqing-intel Jun 15, 2021

Choose a reason for hiding this comment

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

if z is null, we point set dst_tz to src_x_tz, but what if src_x_tz is one dimension ? And why z could be nullptr, in which case z could be nullptr ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good observation.

  1. z is null in situation when we want Binary primitive to write into allocation managed by oneDNN e.g. we do not want output be send to Tensor. This is only used in elementwise_mul_grad:77 as this operation consists of Binary + Reorder . Binary output is a temporary data that is an input to Reorder . Output of reorder goes into output tensor.

  2. Currently there in elementwise_mul_grad (src_x_tz) is always gradient of of Out buffer so I know it is never to be broadcasted. But I agree that code would be better if it works for z == nullptr and x being bradcasted. So I improve that. thanks

src_x_tz, platform::MKLDNNGetDataType<T>(), x->format());
auto src1_md = dnnl::memory::desc(
src_y_tz, platform::MKLDNNGetDataType<T>(), y->format());
if (rankdiff > 0) {
auto rankdiff = x->dims().size() - y->dims().size();
if (rankdiff > 0) { // Second input is lesser than first
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry but why it is implemented this way.
I thought broadcast is copy values line by line until Y is same dimension as X dimension.
If X is 5*12, and Y is 5, rankdiff is 1?
or it is not like that ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If X is [5,12] and Y is [5] then Y dims for oneDNN need to be [5,1] . And then oneDNN having inputs [5,12] and [5,1] will recognize that second input need broadcasting to have applied. So code you are referencing is just to change shape from [5] to [5,1]

@jczaja jczaja closed this Jun 15, 2021
@jczaja jczaja reopened this Jun 15, 2021
paddle-bot-old bot referenced this pull request Jun 15, 2021
Copy link
Contributor

@lidanqing-intel lidanqing-intel left a comment

Choose a reason for hiding this comment

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

LGTM

@jczaja
Copy link
Contributor Author

jczaja commented Jun 15, 2021

@juncaipeng PR-CI-APPROVAL is complaining on wrong PADDLE_ENFORCE_EQ macro I touched in this PR and I do not understand what is wrong with it. couple you please advice?

@juncaipeng
Copy link
Contributor

@juncaipeng PR-CI-APPROVAL is complaining on wrong PADDLE_ENFORCE_EQ macro I touched in this PR and I do not understand what is wrong with it. couple you please advice?

The error infomation is Possible errors include 1. the error message is empty / 2. the error message is too short / 3. the error type is not specified.

The specification in https://github.com/PaddlePaddle/Paddle/wiki/Paddle-Error-Message-Writing-Specification-(English-Verison)

Maybe you can add more error messages ~

src_x_tz, platform::MKLDNNGetDataType<T>(), x->format());
auto src1_md = dnnl::memory::desc(
src_y_tz, platform::MKLDNNGetDataType<T>(), y->format());
if (rankdiff > 0) {
if (rankdiff > 0) { // Second input is lesser than first
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (rankdiff > 0) { // Second input is lesser than first
if (rankdiff > 0) { // Second input is smaller than first

@@ -644,14 +644,6 @@ class BinaryMKLDNNHandler : public platform::MKLDNNHandlerT<T, dnnl::binary> {
platform::CreateKey(
dev_ctx, framework::vectorize(x->dims()), uniq_name,
(algo == dnnl::algorithm::binary_mul ? "M" : ""))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why aren't you just passing algo to CreateKey function? It can convert dnnl:algorithm datatype to a string

@jczaja jczaja changed the title [oneDNN] Fix to #33282 , added support of X input broadcasting to oneDNN elementwise_add/elementwise_mul [oneDNN] Fix to #33282 , added support of X input broadcasting to oneDNN elementwise ops Jun 16, 2021
@lidanqing-intel
Copy link
Contributor

@jczaja Please cherry-pick this PR into release/2.1 after this PR pass all CIs. Thanks.

@lidanqing-intel
Copy link
Contributor

lidanqing-intel commented Jun 18, 2021

@jczaja I saw you remove the whole test of inplace tests. Since 22 is tag day and Baidu expect today to merge this PR. Is it possible to keep minor change and make it pass CIs. If CI still fail not because of this PR, let me know.

@jczaja
Copy link
Contributor Author

jczaja commented Jun 18, 2021

@juncaipeng I would like You to advice me on failing test_pass_builder . Reason of its failure is that
"/tmp/test_viz_pass" file cannot be created , as this file is to be created/opened via std::ofstream , and in this
situation tmp directory does not exist so ofstream fials due to missing tmp directory. When this tmp directory is to be created?

Error message:

2021-06-18 19:51:35 Error Message Summary:
2021-06-18 19:51:35 ----------------------
2021-06-18 19:51:35 UnavailableError: Can not open file /workspace/Paddle/build/python/paddle/fluid/tests/unittests/tmp/test_viz_pass for printing the graph.
2021-06-18 19:51:35 [Hint: Expected fout->good() == true, but received fout->good():0 != true:1.] (at /workspace/Paddle/paddle/fluid/framework/ir/graph_viz_pass.cc:44)

Copy link
Contributor

@arlesniak arlesniak left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@lidanqing-intel lidanqing-intel left a comment

Choose a reason for hiding this comment

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

LGTM

@jakpiase
Copy link
Contributor

LGTM

@jczaja jczaja merged commit 049dd85 into PaddlePaddle:develop Jun 24, 2021
@jczaja jczaja deleted the prv-33282-fix branch June 24, 2021 07:42
jczaja added a commit to jczaja/Paddle that referenced this pull request Jun 29, 2021
…sting to oneDNN elementwise ops (PaddlePaddle#33549)

* - fix to PaddlePaddle#33282

* - Increased threshold for elementwise_mul_bf16 grad

* -disabled faulty UT

* - fix to approval
Superjomn pushed a commit that referenced this pull request Jul 9, 2021
…DNN elementwise ops (#33549) (#33845)

* - fix to #33282

* - Increased threshold for elementwise_mul_bf16 grad

* -disabled faulty UT

* - fix to approval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants