Skip to content

Commit acdf7cb

Browse files
committed
- Added EPS for softmax MKLDNN op
- EPS added to softmax mkldnn primitive outcome is limited to training phase Fixes after review clang format fixes clang format fixes
1 parent a097d08 commit acdf7cb

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def fc(input,
8787
bias_attr=None,
8888
use_mkldnn=False,
8989
act=None,
90+
is_test=False,
9091
name=None):
9192
"""
9293
**Fully Connected Layer**
@@ -133,6 +134,7 @@ def fc(input,
133134
bias_attr (ParamAttr|list of ParamAttr, default None): The parameter attribute for the bias
134135
of this layer. If it is set to None, no bias will be added to the output units.
135136
act (str, default None): Activation to be applied to the output of this layer.
137+
is_test(bool): A flag indicating whether execution is in test phase.
136138
use_mkldnn(bool): Use mkldnn kernel or not, it is valid only when the mkldnn
137139
library is installed. Default: False
138140
name (str, default None): The name of this layer.
@@ -177,7 +179,9 @@ def fc(input,
177179
"W": w},
178180
outputs={"Out": tmp},
179181
attrs={"use_mkldnn": use_mkldnn,
180-
"bias_attr": bias_attr})
182+
"is_test": is_test,
183+
"bias_attr": bias_attr
184+
})
181185
return helper.append_activation(tmp)
182186
else:
183187
for input_var, param_attr in helper.iter_inputs_and_params():

0 commit comments

Comments
 (0)