Skip to content

3d fusion relu #7

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

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tensorflow/compiler/tf2xla/kernels/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package(

load("//tensorflow:tensorflow.bzl", "tf_copts")
load("//tensorflow:tensorflow.bzl", "tf_kernel_library")
load(
"//third_party/mkl:build_defs.bzl",
"if_mkl",
)

tf_kernel_library(
name = "xla_ops",
Expand Down Expand Up @@ -153,8 +157,14 @@ tf_kernel_library(
"//tensorflow/core/kernels:sparse_to_dense_op",
"//tensorflow/core/kernels:stack_ops",
"//tensorflow/core/kernels:training_ops",
"//tensorflow/core/kernels:transpose_op",
],
] + if_mkl(
[
"//tensorflow/core/kernels:mkl_transpose_op",
],
[
"//tensorflow/core/kernels:transpose_op",
],
),
)

tf_kernel_library(
Expand Down
433 changes: 402 additions & 31 deletions tensorflow/core/graph/mkl_layout_pass.cc

Large diffs are not rendered by default.

116 changes: 115 additions & 1 deletion tensorflow/core/graph/mkl_layout_pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,28 @@ TEST_F(MklLayoutPassTest, NodeMerge_Conv2DWithBias_Negative_AttrMismatch) {
"DMT/_1->C:3");
}

// data_format attribute value mismatch. Merge should not be done
// in such case.
TEST_F(MklLayoutPassTest, NodeMerge_Conv2DWithBias_Negative_NoConv2D) {
InitGraph(
"node { name: 'A' op: 'Input'}"
"node { name: 'B' op: 'Input'}"
"node { name: 'C' op: 'MatMul'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" attr { key: 'transpose_a' value { b: false } }"
" attr { key: 'transpose_b' value { b: false } }"
" input: ['A', 'B']}"
"node { name: 'D' op: 'Input'}"
"node { name: 'E' op: 'BiasAdd'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" attr { key: 'data_format' value { s: 'NHCW' } }"
" input: ['C', 'D'] }");
EXPECT_EQ(DoMklLayoutOptimizationPass(),
"A(Input);B(Input);C(MatMul);D(Input);"
"E(BiasAdd)|A->C;"
"B->C:1;C->E;D->E:1");
}

// Test set 2: BiasAddGrad + Conv2DBackpropFilter fusion tests

TEST_F(MklLayoutPassTest, NodeMerge_Conv2DBackpropFilterFusion_Positive) {
Expand Down Expand Up @@ -2174,6 +2196,97 @@ TEST_F(MklLayoutPassTest, NodeMerge_Conv2DBackpropFilterFusion_Positive) {
"DMT/_0->D:3;DMT/_1->D:4;DMT/_2->D:5");
}

// Test set 3: Conv2DWithBias + Relu -> Conv2DWithBiasAndRelu merge tests
// C=Conv2D(A,B); E=BiasAdd(C,D); F=Relu(E); Z=Zeta(F,Y)
TEST_F(MklLayoutPassTest, NodeMerge_Conv2DWithBiasAndRelu_Positive) {
CHECK_EQ(kTensorOrdering, MklTfTensorOrdering::TENSORS_CONTIGUOUS);
InitGraph(
"node { name: 'A' op: 'Input'}"
"node { name: 'B' op: 'Input'}"
"node { name: 'C' op: 'Conv2D'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" attr { key: 'data_format' value { s: 'NCHW' } }"
" attr { key: 'use_cudnn_on_gpu' value { b: false } }"
" attr { key: 'strides' value { list: {i: 1, i:1, i:1, i:1} } }"
" attr { key: 'padding' value { s: 'SAME' } }"
" attr { key: 'dilations' value { list: {i: 1, i:1, i:1, i:1} } }"
" input: ['A', 'B']}"
"node { name: 'D' op: 'Input'}"
"node { name: 'E' op: 'BiasAdd'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" attr { key: 'data_format' value { s: 'NCHW' } }"
" input: ['C', 'D'] }"
"node { name: 'F' op: 'Relu'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" input: ['E'] }"
"node { name: 'Y' op: 'Input'}"
"node { name: 'Z' op: 'Zeta'"
" attr {key: 'T' value { type: DT_FLOAT } }"
" input: ['F', 'Y']}");
EXPECT_EQ(DoMklLayoutOptimizationPass(),
"A(Input);B(Input);D(Input);DMT/_0(Const);DMT/_1(Const);"
"DMT/_2(Const);F(_MklConv2DWithBiasAndRelu);Y(Input);Z(Zeta)|A->F;"
"A:control->DMT/_0:control;A:control->DMT/_1:control;"
"A:control->DMT/_2:control;B->F:1;D->F:2;DMT/_0->F:3;DMT/_1->F:4;"
"DMT/_2->F:5;F->Z;Y->Z:1");
}

// Test set 5:
// C=Conv2D(A,B); D=Relu(C); Z=Zeta(D,Y)
TEST_F(MklLayoutPassTest, NodeMerge_Conv2DWithRelu_Positive) {
CHECK_EQ(kTensorOrdering, MklTfTensorOrdering::TENSORS_CONTIGUOUS);
InitGraph(
"node { name: 'A' op: 'Input'}"
"node { name: 'B' op: 'Input'}"
"node { name: 'C' op: 'Conv2D'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" attr { key: 'data_format' value { s: 'NCHW' } }"
" attr { key: 'use_cudnn_on_gpu' value { b: false } }"
" attr { key: 'strides' value { list: {i: 1, i:1, i:1, i:1} } }"
" attr { key: 'padding' value { s: 'SAME' } }"
" attr { key: 'dilations' value { list: {i: 1, i:2, i:2, i:1} } }"
" input: ['A', 'B']}"
"node { name: 'D' op: 'Relu'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" input: ['C'] }"
"node { name: 'Y' op: 'Input'}"
"node { name: 'Z' op: 'Zeta'"
" attr {key: 'T' value { type: DT_FLOAT } }"
" input: ['D', 'Y']}");
EXPECT_EQ(DoMklLayoutOptimizationPass(),
"A(Input);B(Input);D(_MklConv2DWithRelu);DMT/_0(Const);"
"DMT/_1(Const);Y(Input);Z(Zeta)|A->D;A:control->DMT/_0:control;"
"A:control->DMT/_1:control;B->D:1;D->Z;DMT/_0->D:2;DMT/_1->D:3;"
"Y->Z:1");
}

// Conv2D+ReLU fusion negative test - Conv2D feeds multiple nodes.
TEST_F(MklLayoutPassTest, NodeMerge_Conv2DWithRelu_Negative1) {
CHECK_EQ(kTensorOrdering, MklTfTensorOrdering::TENSORS_CONTIGUOUS);
InitGraph(
"node { name: 'A' op: 'Input'}"
"node { name: 'B' op: 'Input'}"
"node { name: 'C' op: 'Conv2D'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" attr { key: 'data_format' value { s: 'NCHW' } }"
" attr { key: 'use_cudnn_on_gpu' value { b: false } }"
" attr { key: 'strides' value { list: {i: 1, i:1, i:1, i:1} } }"
" attr { key: 'padding' value { s: 'SAME' } }"
" attr { key: 'dilations' value { list: {i: 1, i:2, i:2, i:1} } }"
" input: ['A', 'B']}"
"node { name: 'D' op: 'Relu'"
" attr { key: 'T' value { type: DT_FLOAT } }"
" input: ['C'] }"
"node { name: 'Z' op: 'Zeta'"
" attr {key: 'T' value { type: DT_FLOAT } }"
" input: ['C', 'D']}");
EXPECT_EQ(DoMklLayoutOptimizationPass(),
"A(Input);B(Input);C(_MklConv2D);D(_MklRelu);DMT/_0(Const);"
"DMT/_1(Const);Z(Zeta)|A->C;A:control->DMT/_0:control;"
"A:control->DMT/_1:control;B->C:1;C->D;C->Z;C:2->D:1;D->Z:1;"
"DMT/_0->C:2;DMT/_1->C:3");
}

// BiasAddGrad fusion in the presence of BackpropFilter. But nodes do not match
// criteria for rewrite. So rewrite should not happen. 3rd input of
// Conv2DBackpropFilter is different than input to BiasAddGrad.
Expand Down Expand Up @@ -2357,7 +2470,7 @@ TEST_F(MklLayoutPassTest, NodeRewrite_Conv2D_Positive1) {
" attr { key: 'use_cudnn_on_gpu' value { b: false } }"
" attr { key: 'strides' value { list: {i: 1, i:1, i:1, i:1} } }"
" attr { key: 'padding' value { s: 'SAME' } }"
" attr { key: 'dilations' value { list: {i: 1, i:1, i:1, i:1} } }"
" attr { key: 'dilations' value { list: {i: 1, i:2, i:2, i:1} } }"
" input: ['A', 'B']}"
"node { name: 'D' op: 'Conv2D'"
" attr { key: 'T' value { type: DT_FLOAT } }"
Expand Down Expand Up @@ -3493,6 +3606,7 @@ TEST_F(MklLayoutPassTest, NodeMerge_Conv2DWithBias_DeviceTest) {
" attr { key: 'use_cudnn_on_gpu' value { b: false } }"
" attr { key: 'strides' value { list: {i: 1, i:1, i:1, i:1} } }"
" attr { key: 'padding' value { s: 'SAME' } }"
" attr { key: 'dilations' value { list: {i: 1, i:1, i:1, i:1} } }"
" input: ['A', 'B', 'M', 'N']}"
"node { name: 'D' op: 'Input'}"
"node { name: 'E' op: 'BiasAdd'"
Expand Down
42 changes: 29 additions & 13 deletions tensorflow/core/kernels/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,14 @@ cc_library(
":split_v_op",
":strided_slice_op",
":tile_ops",
":transpose_op",
] + if_mkl(
[
":mkl_transpose_op",
],
[
":transpose_op",
],
) + [
":unique_op",
":unpack_op",
":unravel_index_op",
Expand Down Expand Up @@ -886,18 +893,27 @@ tf_kernel_library(
deps = ARRAY_DEPS,
)

tf_kernel_library(
name = "transpose_op",
srcs = [
"transpose_op.cc",
] + if_mkl([
"mkl_transpose_op.cc",
]),
hdrs = ["transpose_op.h"],
deps = ARRAY_DEPS + if_mkl([
"//third_party/mkl:intel_binary_blob",
"@mkl_dnn",
]),
if_mkl(
[tf_mkl_kernel_library(
name = "mkl_transpose_op",
srcs = [
"transpose_op.cc",
"mkl_transpose_op.cc",
],
hdrs = ["transpose_op.h"],
deps = ARRAY_DEPS + if_mkl([
"//third_party/mkl:intel_binary_blob",
"@mkl_dnn",
]),
)],
[tf_kernel_library(
name = "transpose_op",
srcs = [
"transpose_op.cc",
],
hdrs = ["transpose_op.h"],
deps = ARRAY_DEPS,
)],
)

tf_kernel_library(
Expand Down
Loading