Skip to content

Commit 2617d8b

Browse files
authored
Merge pull request #9993 from jczaja/prv-softmax-mkldnn-fix
- Added Epsilon (preventing softmax output from being too small) for softmax MKLDNN op
2 parents ebbc28e + de8094f commit 2617d8b

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

paddle/fluid/operators/softmax_mkldnn_op.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> {
7373
softmax_dst_memory);
7474
std::vector<primitive> pipeline{softmax};
7575
stream(stream::kind::eager).submit(pipeline).wait();
76+
77+
const bool is_test = ctx.Attr<bool>("is_test");
78+
if (!is_test) {
79+
T threshold = exp(-64);
80+
for (size_t i = 0; i < dst_tz[0] * dst_tz[1]; ++i) {
81+
output_data[i] =
82+
output_data[i] < threshold ? threshold : output_data[i];
83+
}
84+
}
7685
}
7786
};
7887

paddle/fluid/operators/softmax_op.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class SoftmaxOpMaker : public framework::OpProtoAndCheckerMaker {
9797
AddAttr<bool>("use_mkldnn",
9898
"(bool, default false) Only used in mkldnn kernel")
9999
.SetDefault(false);
100+
AddAttr<bool>("is_test",
101+
"Disable epsilon adding to softmax results. Used by MKLDNN.")
102+
.SetDefault(false);
100103
AddComment(R"DOC(
101104
Softmax Operator.
102105

python/paddle/fluid/layers/nn.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def fc(input,
8888
bias_attr=None,
8989
use_mkldnn=False,
9090
act=None,
91+
is_test=False,
9192
name=None):
9293
"""
9394
**Fully Connected Layer**
@@ -134,6 +135,7 @@ def fc(input,
134135
bias_attr (ParamAttr|list of ParamAttr, default None): The parameter attribute for the bias
135136
of this layer. If it is set to None, no bias will be added to the output units.
136137
act (str, default None): Activation to be applied to the output of this layer.
138+
is_test(bool): A flag indicating whether execution is in test phase.
137139
use_mkldnn(bool): Use mkldnn kernel or not, it is valid only when the mkldnn
138140
library is installed. Default: False
139141
name (str, default None): The name of this layer.
@@ -177,8 +179,11 @@ def fc(input,
177179
inputs={"Input": input,
178180
"W": w},
179181
outputs={"Out": tmp},
180-
attrs={"use_mkldnn": use_mkldnn,
181-
"bias_attr": bias_attr})
182+
attrs={
183+
"use_mkldnn": use_mkldnn,
184+
"is_test": is_test,
185+
"bias_attr": bias_attr
186+
})
182187
return helper.append_activation(tmp)
183188
else:
184189
for input_var, param_attr in helper.iter_inputs_and_params():

0 commit comments

Comments
 (0)