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

[Intel MKL] Enable FP32 FusedMatMul for MKL-DNN. #31782

Merged

Conversation

Zantares
Copy link
Contributor

Enable FP32 FusedMatMul(MatMul with BiasAdd) for MKL-DNN backend according to below rule:

  1. No transpose attribute with first input
  2. FusedMatMul op only has 1 post op;

Also added several cases for testing, moved common code from QuantizedMatMul to an head file.

Modified:

  • tensorflow/core/graph/mkl_layout_pass.cc
  • tensorflow/core/graph/mkl_layout_pass_test.cc
  • tensorflow/core/grappler/optimizers/remapper.cc
  • tensorflow/core/kernels/BUILD
  • tensorflow/core/kernels/mkl_fused_ops_test.cc
  • tensorflow/core/kernels/mkl_qmatmul_op.cc
  • tensorflow/core/ops/mkl_nn_ops.cc
  • tensorflow/core/util/mkl_util.h

Added:

  • tensorflow/core/kernels/mkl_matmul_op_fused.cc
  • tensorflow/core/kernels/mkl_matmul_ops_common.h

Signed-off-by: Lu Teng teng.lu@intel.com

@tensorflow-bot tensorflow-bot bot added the size:XL CL Change Size:Extra Large label Aug 20, 2019
@gbaned gbaned self-assigned this Aug 20, 2019
@gbaned gbaned added the comp:mkl MKL related issues label Aug 20, 2019
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation Aug 20, 2019
@gbaned gbaned requested a review from penpornk August 20, 2019 05:31
@mellanox-github
Copy link

Can one of the admins verify this patch?

@gbaned
Copy link
Contributor

gbaned commented Aug 27, 2019

@Zantares Could you please resolve the conflicts? Thanks!

@gbaned gbaned added the stat:awaiting response Status - Awaiting response from author label Aug 27, 2019
@Zantares
Copy link
Contributor Author

@Zantares Could you please resolve the conflicts? Thanks!

Solved, thanks!

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Aug 31, 2019
@gbaned gbaned added the awaiting review Pull request awaiting review label Sep 3, 2019
Copy link
Member

@penpornk penpornk left a comment

Choose a reason for hiding this comment

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

I'm so sorry for the delay! I have mostly minor comments.

tensorflow/core/graph/mkl_layout_pass.cc Outdated Show resolved Hide resolved
tensorflow/core/graph/mkl_layout_pass.cc Outdated Show resolved Hide resolved

// Do not rewrite with transpose attribute because reorder has performance
// impact.
TF_CHECK_OK(GetNodeAttr(n->def(), "transpose_a", &trans_a));
Copy link
Member

Choose a reason for hiding this comment

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

What about transposing B? Are we assuming B is small and would be cheap to reorder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Input B(weight) can be easily pass to MKL-DNN with different memory desc(io/oi) to represent the transpose attribute. It doesn't need reorder here.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Thank you for the explanation!

tensorflow/core/kernels/mkl_matmul_op_fused.cc Outdated Show resolved Hide resolved
tensorflow/core/kernels/mkl_matmul_op_fused.cc Outdated Show resolved Hide resolved
tensorflow/core/kernels/mkl_matmul_ops_common.h Outdated Show resolved Hide resolved
tensorflow/core/kernels/mkl_matmul_ops_common.h Outdated Show resolved Hide resolved
tensorflow/core/kernels/mkl_matmul_ops_common.h Outdated Show resolved Hide resolved
tensorflow/core/ops/mkl_nn_ops.cc Outdated Show resolved Hide resolved
PR Queue automation moved this from Assigned Reviewer to Reviewer Requested Changes Sep 4, 2019
@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Sep 4, 2019
@Zantares
Copy link
Contributor Author

Zantares commented Sep 5, 2019

@penpornk thanks for your review, I'll spend 1~2 days to refine the code and ping you once I finish the work:)

penpornk
penpornk previously approved these changes Sep 10, 2019

// Do not rewrite with transpose attribute because reorder has performance
// impact.
TF_CHECK_OK(GetNodeAttr(n->def(), "transpose_a", &trans_a));
Copy link
Member

Choose a reason for hiding this comment

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

Got it. Thank you for the explanation!

PR Queue automation moved this from Reviewer Requested Changes to Approved by Reviewer Sep 10, 2019
@tensorflow-bot tensorflow-bot bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Sep 10, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Sep 10, 2019
@gbaned
Copy link
Contributor

gbaned commented Sep 11, 2019

@Zantares Could you please address Ubuntu Sanity errors? Thanks!

@Zantares
Copy link
Contributor Author

Zantares commented Sep 11, 2019

@Zantares Could you please address Ubuntu Sanity errors? Thanks!

I've located the failure. It caused by comma missing in BUILD file.

Hi @penpornk , I'm sorry that I didn't ping you after refining the code, because I found I can handle the fusion of MatMul, BiasAdd and Relu via reusing code here: https://github.com/tensorflow/tensorflow/pull/31782/files#diff-4e6ad12b109cad067443f126cc8f2145R187.
It's not a really fusion and just execute Relu after MatMulWithBias in same MKL stream, however it may bring some benefit via cache locality. Do you have some suggestion if I should add this feature into this PR? I mean whether I should split it to another PR because you've already approved this PR now.

PR Queue automation moved this from Approved by Reviewer to Reviewer Requested Changes Sep 11, 2019
Copy link
Member

@penpornk penpornk left a comment

Choose a reason for hiding this comment

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

If the changes are minor, please feel free to add them to this PR. (I actually found a typo but didn't want to request changes earlier. I might as well request it now.) Otherwise, please open a new PR. :)

const int channel = weight_tf_shape.dim_size(1 - dim_pair[1]);

OP_REQUIRES(ctx, k == weight_tf_shape.dim_size(dim_pair[1]),
errors::InvalidArgument("Matrix size are incompatible: In[0]: ",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
errors::InvalidArgument("Matrix size are incompatible: In[0]: ",
errors::InvalidArgument("Matrix sizes are incompatible: In[0]: ",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted the change here because there's a case checking the error verbose like this: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/eager/function_test.py#L270

@penpornk
Copy link
Member

And no worries about pinging! I saw that you added a commit that addressed all the comments so I assumed the PR is ready for review. Sorry for not waiting until you say so!

@gbaned gbaned added awaiting review Pull request awaiting review and removed ready to pull PR ready for merge process labels Sep 13, 2019
@penpornk penpornk added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Sep 19, 2019
@Zantares
Copy link
Contributor Author

Hi @penpornk , we decide to separate Relu fusion from this PR after internal discussion. Besides, I'v tested Tensorflow UT locally and fixed some errors, please take a look at the new commit, thanks!

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Sep 23, 2019
Copy link
Member

@penpornk penpornk left a comment

Choose a reason for hiding this comment

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

Got it. Thanks again for your PR!

PR Queue automation moved this from Reviewer Requested Changes to Approved by Reviewer Sep 23, 2019
@tensorflow-bot tensorflow-bot bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Sep 23, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Sep 23, 2019
@gbaned gbaned added ready to pull PR ready for merge process and removed ready to pull PR ready for merge process labels Sep 24, 2019
tensorflow-copybara pushed a commit that referenced this pull request Sep 24, 2019
@tensorflow-copybara tensorflow-copybara merged commit bc0b234 into tensorflow:master Sep 24, 2019
PR Queue automation moved this from Approved by Reviewer to Merged Sep 24, 2019
@Zantares Zantares deleted the tenglu/fuse_fp32_matmul branch September 25, 2019 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes comp:mkl MKL related issues ready to pull PR ready for merge process size:XL CL Change Size:Extra Large
Projects
PR Queue
  
Merged
Development

Successfully merging this pull request may close these issues.

None yet

8 participants