未验证 提交 fb4a6ecf 编写于 作者: H Hulek 提交者: GitHub

Fused elementwises kernels and ops (#51427)

* Fused elementwises kernels and ops

* change fuse pass name

* adjust .pbtxt files

* adjust quantization attributes

* add missing arguments and fix others, review fixed

* simplify fused kernel registration

* fix elementwise unit tests

* reuse one fused elementwise op

* adjust proto

* Add supported datatypes

* Change 'Scale' to 'scale' in tests, change some tests to onednn

* Revert breaking changes

* Fix unit tests

* Delete obsolete test cases

* Delete commented out code

* Fix codestyle

* delete temporary condition

* fix conflicts and delete duplicate fusing

* Fix code after merge

* Move tests to new directory

* fix tests volatility

* Rename test_elementwise_add_onednn_op.py to test_elementwise_add_mkldnn_op.py

* Update CMakeLists.txt add mkldnn op test

---------
Co-authored-by: NSilv3S <slawomir.siwek@intel.com>
上级 26da689d
......@@ -180,7 +180,7 @@ if(WITH_MKLDNN)
pass_library(softplus_activation_onednn_fuse_pass inference DIR mkldnn)
pass_library(shuffle_channel_mkldnn_detect_pass inference DIR mkldnn)
pass_library(fc_act_mkldnn_fuse_pass inference DIR mkldnn)
pass_library(elt_act_mkldnn_fuse_pass inference DIR mkldnn)
pass_library(elementwise_act_onednn_fuse_pass inference DIR mkldnn)
pass_library(matmul_elementwise_add_mkldnn_fuse_pass inference DIR mkldnn)
pass_library(matmul_activation_mkldnn_fuse_pass inference DIR mkldnn)
pass_library(layer_norm_onednn_optimization_pass inference DIR mkldnn)
......
......@@ -2260,7 +2260,8 @@ PDNode *patterns::OpRequant::operator()() {
auto any_op = pattern->NewNode(any_op_repr())
->assert_is_op()
->assert_more([&](Node *node) {
return node->Op()->HasAttr("Scale_out") ? true : false;
return (node->Op()->HasAttr("Scale_out") ||
node->Op()->HasAttr("scale_out"));
});
auto requant_in = pattern->NewNode(requant_in_repr())
->assert_is_op_input("requantize", "Input");
......@@ -2288,7 +2289,10 @@ PDNode *patterns::RequantOp::operator()() {
->assert_more([&](Node *node) {
return (node->Op()->HasAttr("Scale_in") ||
node->Op()->HasAttr("Scale_x") ||
node->Op()->HasAttr("Scale_y"));
node->Op()->HasAttr("Scale_y") ||
node->Op()->HasAttr("scale_in") ||
node->Op()->HasAttr("scale_x") ||
node->Op()->HasAttr("scale_y"));
});
requant_op->LinksFrom({requant_in}).LinksTo({requant_out});
......
......@@ -1045,14 +1045,14 @@ void CPUQuantizePass::QuantizeElementwise(
"X",
input_x_scale,
is_x_unsigned,
"Scale_x");
"scale_x");
QuantizeInput(g,
elementwise_op,
elementwise_y,
"Y",
input_y_scale,
is_y_unsigned,
"Scale_y");
"scale_y");
bool is_output_unsigned{false};
auto output_scale =
......@@ -1064,7 +1064,7 @@ void CPUQuantizePass::QuantizeElementwise(
"Out",
output_scale,
is_output_unsigned,
"Scale_out");
"scale_out");
++quantize_elementwise_count;
};
......@@ -1314,9 +1314,9 @@ void CPUQuantizePass::ApplyImpl(ir::Graph* graph) const {
QuantizeImmutable(graph, "nearest_interp", "X");
QuantizeImmutable(graph, "nearest_interp_v2", "X");
QuantizeImmutable(graph, "split", "X");
QuantizeElementwise(graph, "elementwise_add");
QuantizeElementwise(graph, "elementwise_mul");
QuantizeElementwise(graph, "elementwise_sub");
QuantizeElementwise(graph, "fused_elementwise_add");
QuantizeElementwise(graph, "fused_elementwise_mul");
QuantizeElementwise(graph, "fused_elementwise_sub");
QuantizeFusionGru(graph);
QuantizeMultiGru(graph);
QuantizeFusionLSTM(graph);
......
......@@ -97,14 +97,15 @@ void SetOp(ProgramDesc* prog,
op->SetAttr("Scale_x", 1.0f);
op->SetAttr("Scale_y", 1.0f);
op->SetAttr("Scale_out", 1.0f);
} else if (type == "elementwise_add" || type == "elementwise_mul" ||
type == "elementwise_sub") {
} else if (type == "fused_elementwise_add" ||
type == "fused_elementwise_sub" ||
type == "fused_elementwise_mul") {
op->SetInput("X", {inputs[0]});
if (inputs.size() > 1) op->SetInput("Y", {inputs[1]});
op->SetOutput("Out", {outputs[0]});
op->SetAttr("Scale_x", 1.0f);
op->SetAttr("Scale_y", 1.0f);
op->SetAttr("Scale_out", 1.0f);
op->SetAttr("scale_x", 1.0f);
op->SetAttr("scale_y", 1.0f);
op->SetAttr("scale_out", 1.0f);
} else if (type == "fusion_gru") {
op->SetInput("X", {inputs[0]});
op->SetInput("Bias", {inputs[1]});
......@@ -178,16 +179,19 @@ void CheckScales(const OpDesc* op, float scale, float shift) {
scale);
scale_names.push_back("Scale_in");
scale_names.push_back("Scale_out");
} else if (type == "fused_matmul" || type == "elementwise_add" ||
type == "elementwise_mul" || type == "elementwise_sub") {
} else if (type == "fused_matmul") {
scale_names.push_back("Scale_x");
scale_names.push_back("Scale_y");
scale_names.push_back("Scale_out");
if (type == "fused_matmul") {
auto const& names = op->InputNames();
if (std::find(names.begin(), names.end(), "ResidualData") != names.end())
scale_names.push_back("Scale_in_eltwise");
}
auto const& names = op->InputNames();
if (std::find(names.begin(), names.end(), "ResidualData") != names.end())
scale_names.push_back("Scale_in_eltwise");
} else if (type == "fused_elementwise_add" ||
type == "fused_elementwise_sub" ||
type == "fused_elementwise_mul") {
scale_names.push_back("scale_x");
scale_names.push_back("scale_y");
scale_names.push_back("scale_out");
} else if (type == "fusion_gru" || type == "fusion_lstm") {
EXPECT_EQ(op->GetAttrIfExists<float>("Shift_data"), shift);
EXPECT_EQ(op->GetAttrIfExists<std::vector<float>>("Scale_weights")[0],
......@@ -710,9 +714,9 @@ void TestElementwiseUnsignedAndSignedInput(
}
const std::vector<std::vector<std::string>> elementwises = {
{"elementwise_add", "ElementwiseAdd"},
{"elementwise_mul", "ElementwiseMul"},
{"elementwise_sub", "ElementwiseSub"}};
{"fused_elementwise_add", "FusedElementwiseAdd"},
{"fused_elementwise_mul", "FusedElementwiseMul"},
{"fused_elementwise_sub", "FusedElementwiseSub"}};
class TestElementwises
: public testing::TestWithParam<std::vector<std::string>> {};
......
......@@ -32,6 +32,9 @@ void CPUQuantizePlacementPass::ApplyImpl(ir::Graph* graph) const {
"fused_conv2d",
"fused_conv3d",
"fused_matmul",
"fused_elementwise_add",
"fused_elementwise_mul",
"fused_elementwise_sub",
"elementwise_add",
"elementwise_mul",
"elementwise_sub",
......@@ -87,12 +90,7 @@ void CPUQuantizePlacementPass::ApplyImpl(ir::Graph* graph) const {
return;
}
// Remove this condition when all fused_elementwise ops are merged
if (!(op->Op()->Type() == "elementwise_add" ||
op->Op()->Type() == "elementwise_sub" ||
op->Op()->Type() == "elementwise_mul")) {
ConvertToFusedOp(op->Op());
}
ConvertToFusedOp(op->Op());
op->Op()->SetAttr("mkldnn_data_type", std::string("int8"));
};
gpd(graph, handler);
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/fluid/framework/ir/mkldnn/elt_act_mkldnn_fuse_pass.h"
#include "paddle/fluid/framework/ir/mkldnn/elementwise_act_onednn_fuse_pass.h"
#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/framework/ir/mkldnn/activation_onednn_fuse_pass.h"
......@@ -62,6 +62,7 @@ void ElementwiseActivationOneDNNPass::FuseElementwiseAct(
GET_IR_NODE_FROM_SUBGRAPH(
activation_out, activation_out, elementwise_act_pattern);
ConvertToFusedOp(elementwise->Op());
SetActivationAttrs(elementwise->Op(), activation->Op(), act_type);
elementwise->Op()->SetOutput("Out", {activation_out->Name()});
......@@ -84,9 +85,9 @@ void ElementwiseActivationOneDNNPass::FuseElementwiseAct(
} // namespace framework
} // namespace paddle
REGISTER_PASS(elt_act_mkldnn_fuse_pass,
REGISTER_PASS(elementwise_act_onednn_fuse_pass,
paddle::framework::ir::ElementwiseActivationOneDNNPass);
REGISTER_PASS_CAPABILITY(elt_act_mkldnn_fuse_pass)
REGISTER_PASS_CAPABILITY(elementwise_act_onednn_fuse_pass)
.AddCombination(
paddle::framework::compatible::OpVersionComparatorCombination()
.LE("elementwise_add", 1)
......
......@@ -148,6 +148,8 @@ inline void ConvertToFusedOp(OpDesc* op) {
const std::map<std::string, std::string> fused_ops = {
{"conv2d", "fused_conv2d"},
{"depthwise_conv2d", "fused_conv2d"},
{"elementwise_add", "fused_elementwise_add"},
{"elementwise_sub", "fused_elementwise_sub"},
{"elementwise_mul", "fused_elementwise_mul"},
{"elementwise_div", "fused_elementwise_div"},
{"matmul", "fused_matmul"},
......
......@@ -31,6 +31,8 @@ void FuseOperatorScaleOneDNNPass::ApplyImpl(Graph *graph) const {
"fused_matmul",
"matmul",
"matmul_v2",
"fused_elementwise_add",
"fused_elementwise_sub",
"fused_elementwise_mul",
"fused_elementwise_div",
"elementwise_add",
......@@ -119,6 +121,8 @@ REGISTER_PASS_CAPABILITY(operator_scale_onednn_fuse_pass)
.EQ("fused_matmul", 0)
.LE("matmul", 1)
.EQ("matmul_v2", 0)
.EQ("fused_elementwise_add", 0)
.EQ("fused_elementwise_sub", 0)
.EQ("fused_elementwise_mul", 0)
.EQ("fused_elementwise_div", 0)
.LE("elementwise_add", 1)
......
......@@ -16,7 +16,6 @@
#include "paddle/fluid/framework/ir/mkldnn/mkldnn_pass_util.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/backends/onednn/onednn_reuse.h"
#include "paddle/utils/string/pretty_log.h"
namespace paddle {
......
......@@ -368,7 +368,7 @@ void CpuPassStrategy::EnableMKLDNN() {
"batch_norm_act_fuse_pass", //
"softplus_activation_onednn_fuse_pass", //
"shuffle_channel_mkldnn_detect_pass", //
"elt_act_mkldnn_fuse_pass", //
"elementwise_act_onednn_fuse_pass", //
"layer_norm_onednn_optimization_pass", //
"operator_scale_onednn_fuse_pass", //
"operator_unsqueeze2_onednn_fuse_pass", //
......
......@@ -22,14 +22,6 @@ extra {
attrs {
name: "Out0_threshold"
type: FLOAT
}
attrs {
name: "x_data_format"
type: STRING
}
attrs {
name: "y_data_format"
type: STRING
}
attrs {
name: "Scale_x"
......
......@@ -16,11 +16,7 @@ def {
}
extra {
attrs {
name: "x_data_format"
type: STRING
}
attrs {
name: "y_data_format"
name: "act"
type: STRING
}
attrs {
......@@ -35,8 +31,4 @@ extra {
name: "Scale_out"
type: FLOAT
}
attrs {
name: "act"
type: STRING
}
}
......@@ -13,16 +13,6 @@ def {
name: "axis"
type: INT
}
}
extra {
attrs {
name: "x_data_format"
type: STRING
}
attrs {
name: "y_data_format"
type: STRING
}
attrs {
name: "Scale_x"
type: FLOAT
......
......@@ -16,11 +16,7 @@ def {
}
extra {
attrs {
name: "x_data_format"
type: STRING
}
attrs {
name: "y_data_format"
name: "act"
type: STRING
}
attrs {
......@@ -35,8 +31,4 @@ extra {
name: "Scale_out"
type: FLOAT
}
attrs {
name: "act"
type: STRING
}
}
......@@ -16,11 +16,7 @@ def {
}
extra {
attrs {
name: "x_data_format"
type: STRING
}
attrs {
name: "y_data_format"
name: "act"
type: STRING
}
attrs {
......@@ -35,8 +31,4 @@ extra {
name: "Scale_out"
type: FLOAT
}
attrs {
name: "act"
type: STRING
}
}
type: "fused_elementwise_add"
def {
inputs {
name: "X"
}
inputs {
name: "Y"
}
outputs {
name: "Out"
}
attrs {
name: "axis"
type: INT
}
}
extra {
attrs {
name: "fuse_activation"
type: STRING
}
attrs {
name: "fuse_alpha"
type: FLOAT
}
attrs {
name: "fuse_beta"
type: FLOAT
}
attrs {
name: "fused_output_scale"
type: FLOAT
}
attrs {
name: "fused_unsqueeze2_axes"
type: INTS
}
attrs {
name: "scale_x"
type: FLOAT
}
attrs {
name: "scale_y"
type: FLOAT
}
attrs {
name: "scale_out"
type: FLOAT
}
}
type: "fused_elementwise_sub"
def {
inputs {
name: "X"
}
inputs {
name: "Y"
}
outputs {
name: "Out"
}
attrs {
name: "axis"
type: INT
}
}
extra {
attrs {
name: "fuse_activation"
type: STRING
}
attrs {
name: "fuse_alpha"
type: FLOAT
}
attrs {
name: "fuse_beta"
type: FLOAT
}
attrs {
name: "fused_output_scale"
type: FLOAT
}
attrs {
name: "fused_unsqueeze2_axes"
type: INTS
}
attrs {
name: "scale_x"
type: FLOAT
}
attrs {
name: "scale_y"
type: FLOAT
}
attrs {
name: "scale_out"
type: FLOAT
}
}
......@@ -170,9 +170,9 @@ REGISTER_OPERATOR(elementwise_sub_grad_grad,
REGISTER_OP_VERSION(elementwise_sub)
.AddCheckpoint(
R"ROC(Register elementwise_sub for adding the attribute of Scale_y)ROC",
R"ROC(Register elementwise_sub for adding the attribute of scale_y)ROC",
paddle::framework::compatible::OpVersionDesc().NewAttr(
"Scale_y",
"scale_y",
"In order to support the function of scaling the input Y when "
"using the operator of elementwise_sub.",
1.0f));
......@@ -62,6 +62,21 @@ class FusedElementwiseOpMaker : public framework::OpProtoAndCheckerMaker {
} // namespace paddle
namespace ops = paddle::operators;
REGISTER_OPERATOR(
fused_elementwise_add,
ops::ElementwiseOp,
ops::FusedElementwiseOpMaker,
ops::ElementwiseOpInferVarType,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
fused_elementwise_sub,
ops::ElementwiseOp,
ops::FusedElementwiseOpMaker,
ops::ElementwiseOpInferVarType,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
fused_elementwise_mul,
......
......@@ -92,7 +92,7 @@
- op : add (elementwise_add)
backward : add_grad (elementwise_add_grad)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : add_n (sum)
......@@ -661,7 +661,7 @@
outputs :
out: Out
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : dot
......@@ -733,7 +733,7 @@
- op : elementwise_pow
backward : elementwise_pow_grad
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : elu
......@@ -922,19 +922,19 @@
- op : floor_divide (elementwise_floordiv)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : fmax (elementwise_fmax)
backward : fmax_grad (elementwise_fmax_grad)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : fmin (elementwise_fmin)
backward : fmin_grad (elementwise_fmin_grad)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : fold
......@@ -1036,7 +1036,7 @@
- op : grad_add
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : greater_equal
......@@ -1114,7 +1114,7 @@
- op : heaviside (elementwise_heaviside)
backward : heaviside_grad (elementwise_heaviside_grad)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : histogram
......@@ -1522,13 +1522,13 @@
- op : maximum (elementwise_max)
backward : maximum_grad (elementwise_max_grad)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : maximum (elementwise_min)
backward : maximum_grad (elementwise_min_grad)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : maxout
......@@ -1642,7 +1642,7 @@
outputs :
out : Out
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : mv
......@@ -1897,7 +1897,7 @@
- op : remainder (elementwise_mod)
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : renorm
......@@ -2260,7 +2260,7 @@
outputs :
out : Out
extra :
attrs : [bool use_mkldnn = false, str x_data_format = "", str y_data_format = "", str mkldnn_data_type = "float32",
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
- op : sum (reduce_sum)
......
......@@ -151,12 +151,32 @@ void FusedElementwiseKernel(const OneDNNContext& dev_ctx,
out); \
}
DEFINE_ONEDNN_ELEMENTWISE_KERNEL(FusedAdd, dnnl::algorithm::binary_add)
DEFINE_ONEDNN_ELEMENTWISE_KERNEL(FusedSubtract, dnnl::algorithm::binary_sub)
DEFINE_ONEDNN_ELEMENTWISE_KERNEL(FusedMultiply, dnnl::algorithm::binary_mul)
DEFINE_ONEDNN_ELEMENTWISE_KERNEL(FusedDivide, dnnl::algorithm::binary_div)
} // namespace fusion
} // namespace phi
PD_REGISTER_KERNEL(fused_elementwise_add,
OneDNN,
ONEDNN,
phi::fusion::FusedAddKernel,
float,
phi::dtype::bfloat16,
int8_t,
uint8_t) {}
PD_REGISTER_KERNEL(fused_elementwise_sub,
OneDNN,
ONEDNN,
phi::fusion::FusedSubtractKernel,
float,
phi::dtype::bfloat16,
int8_t,
uint8_t) {}
PD_REGISTER_KERNEL(fused_elementwise_mul,
OneDNN,
ONEDNN,
......
......@@ -30,26 +30,6 @@ void ElementwiseKernel(const OneDNNContext& dev_ctx,
DenseTensor* out) {
const auto& onednn_engine = dev_ctx.GetEngine();
float scale_x = dev_ctx.HasDnnAttr("Scale_x")
? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("Scale_x"))
: 1.0f;
float scale_y = dev_ctx.HasDnnAttr("Scale_y")
? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("Scale_y"))
: 1.0f;
float scale_out =
dev_ctx.HasDnnAttr("Scale_out")
? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("Scale_out"))
: 1.0f;
dnnl::post_ops post_operations;
funcs::AppendActivation(dev_ctx, post_operations);
if (dev_ctx.HasDnnAttr("fused_output_scale")) {
float scale_alpha =
PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("fused_output_scale"));
post_operations.append_eltwise(
1.0, dnnl::algorithm::eltwise_linear, scale_alpha, 0.0f);
}
auto* non_const_x = &x;
auto* non_const_y = &y;
......@@ -60,11 +40,10 @@ void ElementwiseKernel(const OneDNNContext& dev_ctx,
non_const_x,
non_const_y,
out,
scale_x,
scale_y,
scale_out,
true,
post_operations);
1.0f,
1.0f,
1.0f,
true);
// oneDNN's binary is optimized for broadcasting y into x, so in other case
// we have to swap tensors to achieve optimal performance
......
......@@ -16,6 +16,38 @@
namespace phi {
KernelSignature FusedElementwiseAddOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature("fused_elementwise_add",
{"X", "Y"},
{"axis",
"fuse_activation",
"fuse_alpha",
"fuse_beta",
"fused_output_scale",
"fused_unsqueeze2_axes",
"scale_x",
"scale_y",
"scale_out"},
{"Out"});
}
KernelSignature FusedElementwiseSubOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature("fused_elementwise_sub",
{"X", "Y"},
{"axis",
"fuse_activation",
"fuse_alpha",
"fuse_beta",
"fused_output_scale",
"fused_unsqueeze2_axes",
"scale_x",
"scale_y",
"scale_out"},
{"Out"});
}
KernelSignature FusedElementwiseMulOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature("fused_elementwise_mul",
......@@ -50,6 +82,10 @@ KernelSignature FusedElementwiseDivOpArgumentMapping(
} // namespace phi
PD_REGISTER_ARG_MAPPING_FN(fused_elementwise_add,
phi::FusedElementwiseAddOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(fused_elementwise_sub,
phi::FusedElementwiseSubOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(fused_elementwise_mul,
phi::FusedElementwiseMulOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(fused_elementwise_div,
......
......@@ -132,69 +132,21 @@ p_norm
def elementwise_add_orig2prim(op, x, y):
if x.shape != y.shape:
y = broadcast(y, shape=x.shape)
if op.attr('Scale_x') - 1.0 > 1e-5:
scale_x = fill_const(
shape=x.shape, dtype=x.dtype, value=op.attr('Scale_x')
)
x = mul(x, scale_x)
if op.attr('Scale_y') - 1.0 > 1e-5:
scale_y = fill_const(
shape=y.shape, dtype=y.dtype, value=op.attr('Scale_y')
)
y = mul(y, scale_y)
z = add(x, y)
if op.attr('Scale_out') - 1.0 > 1e-5:
scale_out = fill_const(
shape=z.shape, dtype=z.dtype, value=op.attr('Scale_out')
)
z = mul(z, scale_out)
return z
return add(x, y)
@REGISTER_ORIG2PRIM('elementwise_sub')
def elementwise_sub_orig2prim(op, x, y):
if x.shape != y.shape:
y = broadcast(y, shape=x.shape)
if op.attr('Scale_x') - 1.0 > 1e-5:
scale_x = fill_const(
shape=x.shape, dtype=x.dtype, value=op.attr('Scale_x')
)
x = mul(x, scale_x)
if op.attr('Scale_y') - 1.0 > 1e-5:
scale_y = fill_const(
shape=y.shape, dtype=y.dtype, value=op.attr('Scale_y')
)
y = mul(y, scale_y)
z = sub(x, y)
if op.attr('Scale_out') - 1.0 > 1e-5:
scale_out = fill_const(
shape=z.shape, dtype=z.dtype, value=op.attr('Scale_out')
)
z = mul(z, scale_out)
return z
return sub(x, y)
@REGISTER_ORIG2PRIM('elementwise_mul')
def elementwise_mul_orig2prim(op, x, y):
if x.shape != y.shape:
y = broadcast(y, shape=x.shape)
if op.attr('Scale_x') - 1.0 > 1e-5:
scale_x = fill_const(
shape=x.shape, dtype=x.dtype, value=op.attr('Scale_x')
)
x = mul(x, scale_x)
if op.attr('Scale_y') - 1.0 > 1e-5:
scale_y = fill_const(
shape=y.shape, dtype=y.dtype, value=op.attr('Scale_y')
)
y = mul(y, scale_y)
z = mul(x, y)
if op.attr('Scale_out') - 1.0 > 1e-5:
scale_out = fill_const(
shape=z.shape, dtype=z.dtype, value=op.attr('Scale_out')
)
z = mul(z, scale_out)
return z
return mul(x, y)
@REGISTER_ORIG2PRIM('elementwise_div')
......
......@@ -233,21 +233,13 @@ void GetElementwiseAddOpGrad(const std::vector<T> &dout_vec,
dout_ptr, dout_vec.data(), size_z * sizeof(T), cudaMemcpyHostToDevice);
int axis = -1;
bool use_mkldnn = false, use_quantizer = false;
bool use_mkldnn = false;
std::string mkldnn_data_type = "float32";
std::string x_data_format = "", y_data_format = "";
float Scale_x = 1.0, Scale_y = 1.0, Scale_out = 1.0;
framework::AttributeMap attrs;
attrs.insert({"axis", axis});
attrs.insert({"use_mkldnn", use_mkldnn});
attrs.insert({"x_data_format", x_data_format});
attrs.insert({"y_data_format", y_data_format});
attrs.insert({"use_quantizer", use_quantizer});
attrs.insert({"mkldnn_data_type", mkldnn_data_type});
attrs.insert({"Scale_x", Scale_x});
attrs.insert({"Scale_y", Scale_y});
attrs.insert({"Scale_out", Scale_out});
auto op = framework::OpRegistry::CreateOp(
"elementwise_add_grad",
......
......@@ -23,10 +23,10 @@ from paddle import fluid
from paddle.fluid.core import PassVersionChecker
class ElementwiseActivationMkldnnFusePassTest(InferencePassTest):
class ElementwiseActivationOneDNNFusePassTest(InferencePassTest):
act_alpha = None
act_beta = None
pass_name = 'elt_act_mkldnn_fuse_pass'
pass_name = 'elementwise_act_onednn_fuse_pass'
def setUp(self):
self.set_params()
......@@ -65,24 +65,24 @@ class ElementwiseActivationMkldnnFusePassTest(InferencePassTest):
self.assertTrue(PassVersionChecker.IsCompatible(self.pass_name))
class ElementwiseActivationMkldnnFusePassTest_Add_Relu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Relu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = F.relu
class ElementwiseActivationMkldnnFusePassTest_Add_Tanh(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Tanh(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.tanh
class ElementwiseActivationMkldnnFusePassTest_Add_LeakyRelu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_LeakyRelu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
......@@ -90,40 +90,40 @@ class ElementwiseActivationMkldnnFusePassTest_Add_LeakyRelu(
self.act = paddle.nn.functional.leaky_relu
class ElementwiseActivationMkldnnFusePassTest_Add_Swish(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Swish(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.nn.functional.swish
class ElementwiseActivationMkldnnFusePassTest_Add_HardSwish(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_HardSwish(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.nn.functional.hardswish
class ElementwiseActivationMkldnnFusePassTest_Add_SQRT(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_SQRT(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.sqrt
class ElementwiseActivationMkldnnFusePassTest_Add_ABS(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_ABS(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.abs
class ElementwiseActivationMkldnnFusePassTest_Add_Clip(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Clip(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
......@@ -132,16 +132,16 @@ class ElementwiseActivationMkldnnFusePassTest_Add_Clip(
self.act_beta = 10.0
class ElementwiseActivationMkldnnFusePassTest_Add_Gelu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Gelu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.nn.functional.gelu
class ElementwiseActivationMkldnnFusePassTest_Add_Gelu_Tanh(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Gelu_Tanh(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
......@@ -149,40 +149,40 @@ class ElementwiseActivationMkldnnFusePassTest_Add_Gelu_Tanh(
self.act_alpha = True
class ElementwiseActivationMkldnnFusePassTest_Add_Relu6(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Relu6(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.nn.functional.relu6
class ElementwiseActivationMkldnnFusePassTest_Add_Sigmoid(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Add_Sigmoid(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
self.act = paddle.nn.functional.sigmoid
class ElementwiseActivationMkldnnFusePassTest_Sub_Relu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Relu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = F.relu
class ElementwiseActivationMkldnnFusePassTest_Sub_Tanh(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Tanh(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = paddle.tanh
class ElementwiseActivationMkldnnFusePassTest_Sub_LeakyRelu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_LeakyRelu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
......@@ -190,32 +190,32 @@ class ElementwiseActivationMkldnnFusePassTest_Sub_LeakyRelu(
self.act = paddle.nn.functional.leaky_relu
class ElementwiseActivationMkldnnFusePassTest_Sub_Swish(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Swish(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = paddle.nn.functional.swish
class ElementwiseActivationMkldnnFusePassTest_Sub_HardSwish(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_HardSwish(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = paddle.nn.functional.hardswish
class ElementwiseActivationMkldnnFusePassTest_Sub_ABS(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_ABS(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = paddle.abs
class ElementwiseActivationMkldnnFusePassTest_Sub_Clip(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Clip(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
......@@ -224,16 +224,16 @@ class ElementwiseActivationMkldnnFusePassTest_Sub_Clip(
self.act_beta = 10.0
class ElementwiseActivationMkldnnFusePassTest_Sub_Gelu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Gelu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = paddle.nn.functional.gelu
class ElementwiseActivationMkldnnFusePassTest_Sub_Gelu_Tanh(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Gelu_Tanh(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
......@@ -241,40 +241,40 @@ class ElementwiseActivationMkldnnFusePassTest_Sub_Gelu_Tanh(
self.act_alpha = True
class ElementwiseActivationMkldnnFusePassTest_Sub_Relu6(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Relu6(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = paddle.nn.functional.relu6
class ElementwiseActivationMkldnnFusePassTest_Sub_Sigmoid(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Sub_Sigmoid(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
self.act = paddle.nn.functional.sigmoid
class ElementwiseActivationMkldnnFusePassTest_Mul_Relu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Relu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = F.relu
class ElementwiseActivationMkldnnFusePassTest_Mul_Tanh(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Tanh(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = paddle.tanh
class ElementwiseActivationMkldnnFusePassTest_Mul_LeakyRelu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_LeakyRelu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
......@@ -282,40 +282,40 @@ class ElementwiseActivationMkldnnFusePassTest_Mul_LeakyRelu(
self.act = paddle.nn.functional.leaky_relu
class ElementwiseActivationMkldnnFusePassTest_Mul_Swish(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Swish(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = paddle.nn.functional.swish
class ElementwiseActivationMkldnnFusePassTest_Mul_HardSwish(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_HardSwish(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = paddle.nn.functional.hardswish
class ElementwiseActivationMkldnnFusePassTest_Mul_SQRT(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_SQRT(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = paddle.sqrt
class ElementwiseActivationMkldnnFusePassTest_Mul_ABS(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_ABS(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = paddle.abs
class ElementwiseActivationMkldnnFusePassTest_Mul_Clip(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Clip(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
......@@ -324,16 +324,16 @@ class ElementwiseActivationMkldnnFusePassTest_Mul_Clip(
self.act_beta = 10.0
class ElementwiseActivationMkldnnFusePassTest_Mul_Gelu(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Gelu(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = paddle.nn.functional.gelu
class ElementwiseActivationMkldnnFusePassTest_Mul_Gelu_Tanh(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Gelu_Tanh(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
......@@ -341,16 +341,16 @@ class ElementwiseActivationMkldnnFusePassTest_Mul_Gelu_Tanh(
self.act_alpha = True
class ElementwiseActivationMkldnnFusePassTest_Mul_Relu6(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Relu6(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
self.act = paddle.nn.functional.relu6
class ElementwiseActivationMkldnnFusePassTest_Mul_Sigmoid(
ElementwiseActivationMkldnnFusePassTest
class ElementwiseActivationOneDNNFusePassTest_Mul_Sigmoid(
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
......@@ -358,7 +358,7 @@ class ElementwiseActivationMkldnnFusePassTest_Mul_Sigmoid(
class ElementwiseScaleOneDNNFusePassTest_Add(
ElementwiseActivationMkldnnFusePassTest
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.add
......@@ -367,7 +367,7 @@ class ElementwiseScaleOneDNNFusePassTest_Add(
class ElementwiseScaleOneDNNFusePassTest_Sub(
ElementwiseActivationMkldnnFusePassTest
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.subtract
......@@ -376,7 +376,7 @@ class ElementwiseScaleOneDNNFusePassTest_Sub(
class ElementwiseScaleOneDNNFusePassTest_Mul(
ElementwiseActivationMkldnnFusePassTest
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.multiply
......@@ -385,7 +385,7 @@ class ElementwiseScaleOneDNNFusePassTest_Mul(
class ElementwiseScaleOneDNNFusePassTest_Div(
ElementwiseActivationMkldnnFusePassTest
ElementwiseActivationOneDNNFusePassTest
):
def set_params(self):
self.operand = paddle.divide
......
......@@ -118,17 +118,17 @@ class TestElementwiseAddActivationOneDNNFusePass(PassAutoScanTest):
config = self.create_inference_config(
use_mkldnn=True,
passes=[
'elt_act_mkldnn_fuse_pass',
'elementwise_act_onednn_fuse_pass',
'operator_scale_onednn_fuse_pass',
],
)
yield config, ['elementwise_add'], (1e-5, 1e-5)
yield config, ['fused_elementwise_add'], (1e-5, 1e-5)
def test(self):
self.run_and_statis(
quant=False,
passes=[
'elt_act_mkldnn_fuse_pass',
'elementwise_act_onednn_fuse_pass',
'operator_scale_onednn_fuse_pass',
],
)
......
......@@ -763,9 +763,9 @@ class TrtConvertVitToMultiHeadMatmulTest(TrtLayerAutoScanTest):
},
"op_outputs": {"Out": ["elementwise_add1_output"]},
"op_attrs": {
"Scale_out": 1.0,
"Scale_x": 1.0,
"Scale_y": 1.0,
"scale_out": 1.0,
"scale_x": 1.0,
"scale_y": 1.0,
"axis": 2,
"Out": 1.0,
},
......
......@@ -24,6 +24,8 @@ endif()
set_tests_properties(test_concat_mkldnn_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_conv3d_mkldnn_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_elementwise_mul_onednn_op PROPERTIES TIMEOUT 60)
set_tests_properties(test_elementwise_add_mkldnn_op PROPERTIES TIMEOUT 60)
if(WITH_MKLDNN AND NOT WIN32)
set_tests_properties(test_onnx_format_quantization_mobilenetv1
PROPERTIES TIMEOUT 300)
......
......@@ -23,7 +23,7 @@ from paddle.fluid.tests.unittests.test_elementwise_add_op import (
)
class TestMKLDNNElementwiseAddOp(TestElementwiseAddOp):
class TestOneDNNElementwiseAddOp(TestElementwiseAddOp):
def init_kernel_type(self):
self.use_mkldnn = True
......@@ -31,21 +31,21 @@ class TestMKLDNNElementwiseAddOp(TestElementwiseAddOp):
self.dtype = np.float32
class TestMKLDNNElementwiseAddOp2(TestMKLDNNElementwiseAddOp):
class TestOneDNNElementwiseAddOp2(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.random((100,)).astype(self.dtype)
self.y = np.random.random((100,)).astype(self.dtype)
self.out = np.add(self.x, self.y)
class TestMKLDNNElementwiseAddOp3(TestMKLDNNElementwiseAddOp):
class TestOneDNNElementwiseAddOp3(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.y = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.out = np.add(self.x, self.y)
class TestMKLDNNElementwiseAddOp4(TestMKLDNNElementwiseAddOp):
class TestOneDNNElementwiseAddOp4(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 32]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [4, 32]).astype(self.dtype)
......@@ -59,21 +59,21 @@ class TestMKLDNNElementwiseAddOp4(TestMKLDNNElementwiseAddOp):
pass
class TestMKLDNNElementwiseAddOp5(TestMKLDNNElementwiseAddOp):
class TestOneDNNElementwiseAddOp5(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [100]).astype(self.dtype)
self.out = np.add(self.x, self.y)
class TestMKLDNNElementwiseAddOpBroadcastXintoY(TestMKLDNNElementwiseAddOp):
class TestOneDNNElementwiseAddOpBroadcastXintoY(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 50, 1]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [2, 50, 160]).astype(self.dtype)
self.out = np.add(self.x, self.y)
class TestMKLDNNElementwiseAddOp_broadcast_3(TestMKLDNNElementwiseAddOp):
class TestOneDNNElementwiseAddOp_broadcast_3(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.rand(2, 10, 12, 3).astype(self.dtype)
self.y = np.random.rand(10, 12).astype(self.dtype)
......@@ -83,7 +83,7 @@ class TestMKLDNNElementwiseAddOp_broadcast_3(TestMKLDNNElementwiseAddOp):
self.axis = 1
class TestElementwiseAddOp_xsize_lessthan_ysize_add(TestMKLDNNElementwiseAddOp):
class TestElementwiseAddOp_xsize_lessthan_ysize_add(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.rand(10, 12).astype(self.dtype)
self.y = np.random.rand(2, 2, 10, 12).astype(self.dtype)
......@@ -103,21 +103,21 @@ class TestElementwiseAddOp_xsize_lessthan_ysize_add(TestMKLDNNElementwiseAddOp):
pass
class TestMKLDNNElementwiseAddOpZeroDim(TestMKLDNNElementwiseAddOp):
class TestOneDNNlementwiseAddOpZeroDim(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.random.random((100,)).astype(self.dtype)
self.y = np.array(3.0).astype(self.dtype)
self.out = np.add(self.x, self.y)
class TestMKLDNNElementwiseAddOpZeroDim2(TestMKLDNNElementwiseAddOp):
class TestOneDNNlementwiseAddOpZeroDim2(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.array(3.0).astype(self.dtype)
self.y = np.random.random((100,)).astype(self.dtype)
self.out = np.add(self.x, self.y)
class TestMKLDNNElementwiseAddOpZeroDim3(TestMKLDNNElementwiseAddOp):
class TestOneDNNlementwiseAddOpZeroDim3(TestOneDNNElementwiseAddOp):
def init_input_output(self):
self.x = np.array(3.0).astype(self.dtype)
self.y = np.array(3.0).astype(self.dtype)
......@@ -144,9 +144,9 @@ class TestInt8(TestElementwiseAddOp):
self.out = np.add(self.x, self.y)
def init_scales(self):
self.attrs['Scale_x'] = 1.0
self.attrs['Scale_y'] = 1.0
self.attrs['Scale_out'] = 1.0
self.attrs['scale_x'] = 1.0
self.attrs['scale_y'] = 1.0
self.attrs['scale_out'] = 1.0
def test_check_output(self):
# TODO(wangzhongpu): support mkldnn op in dygraph mode
......@@ -163,48 +163,6 @@ class TestInt8(TestElementwiseAddOp):
pass
class TestInt8Scales(TestInt8):
def quantize(self, tensor, dt="int8"):
max_int = 127.0 if dt == "int8" else 255.0
scale = max_int / np.abs(np.amax(tensor))
quantized = np.round(scale * tensor).astype(dt)
return scale, quantized
def init_input_output(self):
self.x_f = np.random.random((100,)).astype("float")
self.y_f = np.random.random((100,)).astype("float")
self.out_f = np.add(self.x_f, self.y_f)
self.scale_x, self.x = self.quantize(self.x_f)
self.scale_y, self.y = self.quantize(self.y_f)
self.scale_o, self.out = self.quantize(self.out_f)
def init_scales(self):
self.attrs['Scale_x'] = self.scale_x
self.attrs['Scale_y'] = self.scale_y
self.attrs['Scale_out'] = self.scale_o
def test_check_output(self):
# TODO(wangzhongpu): support mkldnn op in dygraph mode
self.init_scales()
int_atol = 1 # different quantization techniques
self.check_output(check_dygraph=(not self.use_mkldnn), atol=int_atol)
class TestUint8Scales(TestInt8Scales):
def init_input_output(self):
self.x_f = np.random.random((100,)).astype("float")
self.y_f = np.random.random((100,)).astype("float")
self.out_f = np.add(self.x_f, self.y_f)
self.scale_x, self.x = self.quantize(self.x_f, "uint8")
self.scale_y, self.y = self.quantize(self.y_f, "uint8")
self.scale_o, self.out = self.quantize(self.out_f, "uint8")
def init_dtype(self):
self.dtype = np.uint8
if __name__ == '__main__':
enable_static()
unittest.main()
......@@ -23,7 +23,7 @@ from paddle.fluid.tests.unittests.test_elementwise_mul_op import (
)
class TestMKLDNNElementwiseMulOp(ElementwiseMulOp):
class TestOneDNNElementwiseMulOp(ElementwiseMulOp):
def init_kernel_type(self):
self.use_mkldnn = True
......@@ -31,21 +31,21 @@ class TestMKLDNNElementwiseMulOp(ElementwiseMulOp):
self.dtype = np.float32
class TestMKLDNNElementwiseMulOp2(TestMKLDNNElementwiseMulOp):
class TestOneDNNElementwiseMulOp2(TestOneDNNElementwiseMulOp):
def init_input_output(self):
self.x = np.random.random((100,)).astype(self.dtype)
self.y = np.random.random((100,)).astype(self.dtype)
self.out = np.multiply(self.x, self.y)
class TestMKLDNNElementwiseMulOp3(TestMKLDNNElementwiseMulOp):
class TestOneDNNElementwiseMulOp3(TestOneDNNElementwiseMulOp):
def init_input_output(self):
self.x = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.y = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.out = np.multiply(self.x, self.y)
class TestMKLDNNElementwiseMulOp4(TestMKLDNNElementwiseMulOp):
class TestOneDNNElementwiseMulOp4(TestOneDNNElementwiseMulOp):
def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 32]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [4, 32]).astype(self.dtype)
......@@ -59,7 +59,7 @@ class TestMKLDNNElementwiseMulOp4(TestMKLDNNElementwiseMulOp):
pass
class TestMKLDNNElementwiseMulOp5(TestMKLDNNElementwiseMulOp):
class TestOneDNNElementwiseMulOp5(TestOneDNNElementwiseMulOp):
def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [100]).astype(self.dtype)
......@@ -76,7 +76,7 @@ class TestMKLDNNElementwiseMulOp5(TestMKLDNNElementwiseMulOp):
pass
class TestMKLDNNElementwiseMulOpZeroDim(TestMKLDNNElementwiseMulOp):
class TestOneDNNElementwiseMulOpZeroDim(TestOneDNNElementwiseMulOp):
def init_input_output(self):
self.x = np.random.random((100,)).astype(self.dtype)
self.y = np.array(3.0).astype(self.dtype)
......@@ -92,7 +92,7 @@ class TestMKLDNNElementwiseMulOpZeroDim(TestMKLDNNElementwiseMulOp):
pass
class TestMKLDNNElementwiseMulOpZeroDim2(TestMKLDNNElementwiseMulOp):
class TestOneDNNElementwiseMulOpZeroDim2(TestOneDNNElementwiseMulOp):
def init_input_output(self):
self.x = np.array(3.0).astype(self.dtype)
self.y = np.random.random((100,)).astype(self.dtype)
......@@ -108,7 +108,7 @@ class TestMKLDNNElementwiseMulOpZeroDim2(TestMKLDNNElementwiseMulOp):
pass
class TestMKLDNNElementwiseMulOpZeroDim3(TestMKLDNNElementwiseMulOp):
class TestOneDNNElementwiseMulOpZeroDim3(TestOneDNNElementwiseMulOp):
def init_input_output(self):
self.x = np.array(3.0).astype(self.dtype)
self.y = np.array(3.0).astype(self.dtype)
......@@ -144,9 +144,9 @@ class TestInt8(ElementwiseMulOp):
self.out = np.multiply(self.x, self.y)
def init_scales(self):
self.attrs['Scale_x'] = 1.0
self.attrs['Scale_y'] = 1.0
self.attrs['Scale_out'] = 1.0
self.attrs['scale_x'] = 1.0
self.attrs['scale_y'] = 1.0
self.attrs['scale_out'] = 1.0
def test_check_output(self):
# TODO(wangzhongpu): support mkldnn op in dygraph mode
......@@ -163,48 +163,6 @@ class TestInt8(ElementwiseMulOp):
pass
class TestInt8Scales(TestInt8):
def quantize(self, tensor, dt="int8"):
max_int = 127.0 if dt == "int8" else 255.0
scale = max_int / np.abs(np.amax(tensor))
quantized = np.round(scale * tensor).astype(dt)
return scale, quantized
def init_input_output(self):
self.x_f = np.random.random((100,)).astype("float")
self.y_f = np.random.random((100,)).astype("float")
self.out_f = np.multiply(self.x_f, self.y_f)
self.scale_x, self.x = self.quantize(self.x_f)
self.scale_y, self.y = self.quantize(self.y_f)
self.scale_o, self.out = self.quantize(self.out_f)
def init_scales(self):
self.attrs['Scale_x'] = self.scale_x
self.attrs['Scale_y'] = self.scale_y
self.attrs['Scale_out'] = self.scale_o
def test_check_output(self):
# TODO(wangzhongpu): support mkldnn op in dygraph mode
self.init_scales()
int_atol = 1 # different quantization techniques
self.check_output(check_dygraph=(not self.use_mkldnn), atol=int_atol)
class TestUint8Scales(TestInt8Scales):
def init_input_output(self):
self.x_f = np.random.random((100,)).astype("float")
self.y_f = np.random.random((100,)).astype("float")
self.out_f = np.multiply(self.x_f, self.y_f)
self.scale_x, self.x = self.quantize(self.x_f, "uint8")
self.scale_y, self.y = self.quantize(self.y_f, "uint8")
self.scale_o, self.out = self.quantize(self.out_f, "uint8")
def init_dtype(self):
self.dtype = np.uint8
if __name__ == '__main__':
enable_static()
unittest.main()
......@@ -30,7 +30,7 @@ from paddle.fluid.tests.unittests.eager_op_test import (
not (isinstance(_current_expected_place(), core.CPUPlace)),
"GPU is not supported",
)
class TestMKLDNNElementwiseSubOp(OpTest):
class TestOneDNNElementwiseSubOp(OpTest):
def setUp(self):
self.op_type = "elementwise_sub"
self.init_dtype()
......@@ -71,49 +71,49 @@ class TestMKLDNNElementwiseSubOp(OpTest):
self.check_output()
class TestMKLDNNElementwiseSubOp2(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOp2(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.random((100,)).astype(self.dtype)
self.y = np.random.random((100,)).astype(self.dtype)
self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp3(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOp3(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.y = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp4(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOp4(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 32]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [4, 32]).astype(self.dtype)
self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp5(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOp5(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype)
self.y = np.random.uniform(1, 2, [100]).astype(self.dtype)
self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp6(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOp6(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.uniform(0.1, 2, [180, 1]).astype(self.dtype)
self.y = np.random.uniform(0.1, 1, [1, 256]).astype(self.dtype)
self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp7(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOp7(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.uniform(0.1, 2, [1, 180]).astype(self.dtype)
self.y = np.random.uniform(0.1, 1, [256, 1]).astype(self.dtype)
self.out = np.subtract(self.x, self.y)
class TestMKLDNNElementwiseSubOp_broadcast(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOp_broadcast(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.rand(2, 10, 12, 3).astype(self.dtype)
self.y = np.random.rand(10, 12).astype(self.dtype)
......@@ -123,7 +123,7 @@ class TestMKLDNNElementwiseSubOp_broadcast(TestMKLDNNElementwiseSubOp):
self.axis = 1
class TestElementwiseSubOp_xsize_lessthan_ysize_sub(TestMKLDNNElementwiseSubOp):
class TestElementwiseSubOp_xsize_lessthan_ysize_sub(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.rand(10, 12).astype(self.dtype)
self.y = np.random.rand(2, 2, 10, 12).astype(self.dtype)
......@@ -133,7 +133,7 @@ class TestElementwiseSubOp_xsize_lessthan_ysize_sub(TestMKLDNNElementwiseSubOp):
self.axis = 2
class TestMKLDNNElementwiseSubOpZeroDim(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOpZeroDim(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.random.random((100,)).astype(self.dtype)
self.y = np.array(3.0).astype(self.dtype)
......@@ -149,7 +149,7 @@ class TestMKLDNNElementwiseSubOpZeroDim(TestMKLDNNElementwiseSubOp):
pass
class TestMKLDNNElementwiseSubOpZeroDim2(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOpZeroDim2(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.array(3.0).astype(self.dtype)
self.y = np.random.random((100,)).astype(self.dtype)
......@@ -165,7 +165,7 @@ class TestMKLDNNElementwiseSubOpZeroDim2(TestMKLDNNElementwiseSubOp):
pass
class TestMKLDNNElementwiseSubOpZeroDim3(TestMKLDNNElementwiseSubOp):
class TestOneDNNElementwiseSubOpZeroDim3(TestOneDNNElementwiseSubOp):
def init_input_output(self):
self.x = np.array(3.0).astype(self.dtype)
self.y = np.array(3.0).astype(self.dtype)
......@@ -182,7 +182,7 @@ class TestMKLDNNElementwiseSubOpZeroDim3(TestMKLDNNElementwiseSubOp):
@OpTestTool.skip_if_not_cpu_bf16()
class TestBf16(TestMKLDNNElementwiseSubOp):
class TestBf16(TestOneDNNElementwiseSubOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.init_dtype()
......@@ -271,7 +271,7 @@ class TestBf16Broadcasting(TestBf16):
)
class TestInt8(TestMKLDNNElementwiseSubOp):
class TestInt8(TestOneDNNElementwiseSubOp):
def init_kernel_type(self):
self.use_mkldnn = True
self._cpu_only = True
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册