未验证 提交 2617d8bc 编写于 作者: T Tao Luo 提交者: GitHub

Merge pull request #9993 from jczaja/prv-softmax-mkldnn-fix

- Added Epsilon (preventing softmax output from being too small) for softmax MKLDNN op
...@@ -73,6 +73,15 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> { ...@@ -73,6 +73,15 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> {
softmax_dst_memory); softmax_dst_memory);
std::vector<primitive> pipeline{softmax}; std::vector<primitive> pipeline{softmax};
stream(stream::kind::eager).submit(pipeline).wait(); stream(stream::kind::eager).submit(pipeline).wait();
const bool is_test = ctx.Attr<bool>("is_test");
if (!is_test) {
T threshold = exp(-64);
for (size_t i = 0; i < dst_tz[0] * dst_tz[1]; ++i) {
output_data[i] =
output_data[i] < threshold ? threshold : output_data[i];
}
}
} }
}; };
......
...@@ -97,6 +97,9 @@ class SoftmaxOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -97,6 +97,9 @@ class SoftmaxOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr<bool>("use_mkldnn", AddAttr<bool>("use_mkldnn",
"(bool, default false) Only used in mkldnn kernel") "(bool, default false) Only used in mkldnn kernel")
.SetDefault(false); .SetDefault(false);
AddAttr<bool>("is_test",
"Disable epsilon adding to softmax results. Used by MKLDNN.")
.SetDefault(false);
AddComment(R"DOC( AddComment(R"DOC(
Softmax Operator. Softmax Operator.
......
...@@ -88,6 +88,7 @@ def fc(input, ...@@ -88,6 +88,7 @@ def fc(input,
bias_attr=None, bias_attr=None,
use_mkldnn=False, use_mkldnn=False,
act=None, act=None,
is_test=False,
name=None): name=None):
""" """
**Fully Connected Layer** **Fully Connected Layer**
...@@ -134,6 +135,7 @@ def fc(input, ...@@ -134,6 +135,7 @@ def fc(input,
bias_attr (ParamAttr|list of ParamAttr, default None): The parameter attribute for the bias bias_attr (ParamAttr|list of ParamAttr, default None): The parameter attribute for the bias
of this layer. If it is set to None, no bias will be added to the output units. of this layer. If it is set to None, no bias will be added to the output units.
act (str, default None): Activation to be applied to the output of this layer. act (str, default None): Activation to be applied to the output of this layer.
is_test(bool): A flag indicating whether execution is in test phase.
use_mkldnn(bool): Use mkldnn kernel or not, it is valid only when the mkldnn use_mkldnn(bool): Use mkldnn kernel or not, it is valid only when the mkldnn
library is installed. Default: False library is installed. Default: False
name (str, default None): The name of this layer. name (str, default None): The name of this layer.
...@@ -177,8 +179,11 @@ def fc(input, ...@@ -177,8 +179,11 @@ def fc(input,
inputs={"Input": input, inputs={"Input": input,
"W": w}, "W": w},
outputs={"Out": tmp}, outputs={"Out": tmp},
attrs={"use_mkldnn": use_mkldnn, attrs={
"bias_attr": bias_attr}) "use_mkldnn": use_mkldnn,
"is_test": is_test,
"bias_attr": bias_attr
})
return helper.append_activation(tmp) return helper.append_activation(tmp)
else: else:
for input_var, param_attr in helper.iter_inputs_and_params(): for input_var, param_attr in helper.iter_inputs_and_params():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册