-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 issue #34554 #34623
Merged
Merged
[oneDNN] Fix to issue #34554 #34623
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5f0422f
- Added softmax without caching
jczaja f6e981f
- Binary is no longer manually cached
jczaja b832284
- Activation onednn caching removed
jczaja 2b24a80
- Removed manual caching of activation
jczaja 98270c1
- modified UT
jczaja ed2b1a5
- fix
jczaja 53fa9b0
- fix
jczaja e888232
- fixes to building
jczaja 40b9025
- fix
jczaja b264260
- fix
jczaja b54fcfa
- fix to UT
jczaja 7f408f9
- Faulty UT workaround
jczaja 1148ce6
- approval workaround
jczaja a265881
- Fixes after review
jczaja 5cb2b4c
- compilation fixes
jczaja 82de4cb
- more lint fixes
jczaja 17f23f7
- more fixes after review
jczaja a701e18
- fixes after another round of review
jczaja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
cc_test(test_mkldnn_caching SRCS mkldnn/test_mkldnn_caching.cc DEPS op_registry elementwise_mul_op elementwise_add_op activation_op softmax_op softmax scope device_context enforce) | ||
cc_test(test_mkldnn_caching SRCS mkldnn/test_mkldnn_caching.cc DEPS op_registry elementwise_mul_op elementwise_add_op activation_op softmax_op conv_op im2col vol2col softmax scope device_context enforce) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,69 +32,54 @@ using platform::to_void_cast; | |
|
||
template <typename T> | ||
class SoftmaxMKLDNNHandler | ||
: public platform::MKLDNNHandlerT<T, mkldnn::softmax_forward, | ||
mkldnn::softmax_backward> { | ||
: public platform::MKLDNNHandlerNoCachingT<T, mkldnn::softmax_forward, | ||
mkldnn::softmax_backward> { | ||
public: | ||
SoftmaxMKLDNNHandler(const MKLDNNDeviceContext& dev_ctx, | ||
const mkldnn::engine mkldnn_engine, | ||
SoftmaxMKLDNNHandler(const mkldnn::engine mkldnn_engine, | ||
platform::Place cpu_place, const Tensor* input, | ||
Tensor* output, const int axis, | ||
const std::string uniq_name, bool is_inplaced) | ||
: platform::MKLDNNHandlerT<T, mkldnn::softmax_forward, | ||
mkldnn::softmax_backward>( | ||
dev_ctx, mkldnn_engine, cpu_place, | ||
// Softmax may be inplace then uniq_name is no longer unique | ||
is_inplaced ? platform::CreateKey( | ||
dev_ctx, framework::vectorize(input->dims()), | ||
axis, uniq_name) | ||
: platform::CreateKey( | ||
dev_ctx, framework::vectorize(input->dims()), | ||
uniq_name)) { | ||
if (!this->isCached()) { | ||
PADDLE_ENFORCE_EQ( | ||
input->dims(), output->dims(), | ||
platform::errors::InvalidArgument( | ||
"The shape of input and output tensor must be identical.")); | ||
|
||
auto softmax_tz = framework::vectorize(input->dims()); | ||
auto md = memory::desc(softmax_tz, platform::MKLDNNGetDataType<T>(), | ||
input->format()); | ||
|
||
this->AcquireForwardPrimitiveDescriptor(prop_kind::forward_scoring, md, | ||
axis); | ||
} | ||
Tensor* output, const int axis) | ||
: platform::MKLDNNHandlerNoCachingT<T, mkldnn::softmax_forward, | ||
mkldnn::softmax_backward>( | ||
mkldnn_engine, cpu_place) { | ||
PADDLE_ENFORCE_EQ( | ||
input->dims(), output->dims(), | ||
platform::errors::InvalidArgument( | ||
"The shape of input and output tensor must be identical.")); | ||
|
||
auto softmax_tz = framework::vectorize(input->dims()); | ||
auto md = memory::desc(softmax_tz, platform::MKLDNNGetDataType<T>(), | ||
input->format()); | ||
|
||
this->AcquireForwardPrimitiveDescriptor(prop_kind::forward_scoring, md, | ||
axis); | ||
} | ||
|
||
SoftmaxMKLDNNHandler(const framework::ExecutionContext& ctx, | ||
const MKLDNNDeviceContext& dev_ctx, | ||
const mkldnn::engine mkldnn_engine, | ||
platform::Place cpu_place, const Tensor* out, | ||
const Tensor* out_grad, Tensor* in_x_grad, | ||
const std::string& unique_name) | ||
: platform::MKLDNNHandlerT<T, mkldnn::softmax_forward, | ||
mkldnn::softmax_backward>( | ||
dev_ctx, dev_ctx.GetEngine(), cpu_place, | ||
platform::CreateKey(dev_ctx, framework::vectorize(out->dims()), | ||
unique_name)) { | ||
if (!this->isBwdCached()) { | ||
PADDLE_ENFORCE_EQ( | ||
out_grad->dims(), in_x_grad->dims(), | ||
platform::errors::InvalidArgument("The shape of softmax_grad's input " | ||
"and output must be identical.")); | ||
|
||
auto dims = out_grad->dims(); // input and output share the same shape | ||
const int axis = CanonicalAxis(ctx.Attr<int>("axis"), dims.size()); | ||
auto softmax_tz = framework::vectorize<int64_t>(dims); | ||
|
||
auto data_softmax_md = MKLDNNMemDesc( | ||
softmax_tz, platform::MKLDNNGetDataType<T>(), out->format()); | ||
auto diff_softmax_md = MKLDNNMemDesc( | ||
softmax_tz, platform::MKLDNNGetDataType<T>(), out_grad->format()); | ||
|
||
this->AcquireForwardPrimitiveDescriptor(prop_kind::forward_scoring, | ||
data_softmax_md, axis); | ||
this->AcquireBackwardPrimitiveDescriptor(diff_softmax_md, data_softmax_md, | ||
axis); | ||
} | ||
: platform::MKLDNNHandlerNoCachingT<T, mkldnn::softmax_forward, | ||
mkldnn::softmax_backward>( | ||
mkldnn_engine, cpu_place) { | ||
PADDLE_ENFORCE_EQ( | ||
out_grad->dims(), in_x_grad->dims(), | ||
platform::errors::InvalidArgument("The shape of softmax_grad's input " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we recommend that the expected value and actual value are also reported in the error message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
"and output must be identical.")); | ||
|
||
auto dims = out_grad->dims(); // input and output share the same shape | ||
const int axis = CanonicalAxis(ctx.Attr<int>("axis"), dims.size()); | ||
auto softmax_tz = framework::vectorize<int64_t>(dims); | ||
|
||
auto data_softmax_md = MKLDNNMemDesc( | ||
softmax_tz, platform::MKLDNNGetDataType<T>(), out->format()); | ||
auto diff_softmax_md = MKLDNNMemDesc( | ||
softmax_tz, platform::MKLDNNGetDataType<T>(), out_grad->format()); | ||
|
||
this->AcquireForwardPrimitiveDescriptor(prop_kind::forward_scoring, | ||
data_softmax_md, axis); | ||
this->AcquireBackwardPrimitiveDescriptor(diff_softmax_md, data_softmax_md, | ||
axis); | ||
} | ||
}; | ||
|
||
|
@@ -111,9 +96,8 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> { | |
|
||
const int axis = CanonicalAxis(ctx.Attr<int>("axis"), input->dims().size()); | ||
|
||
SoftmaxMKLDNNHandler<T> handler(dev_ctx, mkldnn_engine, ctx.GetPlace(), | ||
input, output, axis, ctx.OutputName("Out"), | ||
is_inplaced); | ||
SoftmaxMKLDNNHandler<T> handler(mkldnn_engine, ctx.GetPlace(), input, | ||
output, axis); | ||
|
||
auto softmax_src_memory_p = handler.AcquireSrcMemory(input); | ||
// For Inplace src and and dst are the same memory object | ||
|
@@ -149,11 +133,12 @@ class SoftmaxMKLDNNGradKernel : public paddle::framework::OpKernel<T> { | |
paddle::platform::errors::PreconditionNotMet( | ||
"Operator DNNL SoftmaxGrad must use CPUPlace")); | ||
auto& dev_ctx = ctx.template device_context<MKLDNNDeviceContext>(); | ||
const auto& mkldnn_engine = dev_ctx.GetEngine(); | ||
const Tensor* output = ctx.Input<Tensor>("Out"); | ||
auto* out_grad = ctx.template Input<Tensor>(framework::GradVarName("Out")); | ||
auto* in_x_grad = ctx.template Output<Tensor>(framework::GradVarName("X")); | ||
|
||
SoftmaxMKLDNNHandler<T> handler(ctx, dev_ctx, ctx.GetPlace(), output, | ||
SoftmaxMKLDNNHandler<T> handler(ctx, mkldnn_engine, ctx.GetPlace(), output, | ||
out_grad, in_x_grad, ctx.InputName("Out")); | ||
|
||
auto dst_memory_p = handler.AcquireDstMemory(output); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you clarify this comment, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. Comment was updated.