Skip to content

Revert "Fuse BatchMatMul with Mul" #26

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

Merged
merged 1 commit into from
Apr 30, 2020
Merged
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
16 changes: 0 additions & 16 deletions tensorflow/core/graph/mkl_layout_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
csinfo_.fused_depthwise_conv2d = "_FusedDepthwiseConv2dNative";
csinfo_.fused_matmul = "_FusedMatMul";
csinfo_.fused_matmul_grad = "_FusedMatMulGrad";
csinfo_.fused_batch_matmul = "_FusedBatchMatMul";
csinfo_.fused_batch_matmul_v2 = "_FusedBatchMatMulV2";
csinfo_.gelu = "Gelu";
csinfo_.gelu_grad = "GeluGrad";
csinfo_.identity = "Identity";
Expand Down Expand Up @@ -306,8 +304,6 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
csinfo_.mkl_fused_depthwise_conv2d = "_MklFusedDepthwiseConv2dNative";
csinfo_.mkl_fused_matmul = "_MklFusedMatMul";
csinfo_.mkl_fused_matmul_grad = "_MklFusedMatMulGrad";
csinfo_.mkl_fused_batch_matmul = "_MklFusedBatchMatMul";
csinfo_.mkl_fused_batch_matmul_v2 = "_MklFusedBatchMatMulV2";
csinfo_.mkl_pad_with_conv2d = "_MklPadWithConv2D";
csinfo_.mkl_pad_with_fused_conv2d = "_MklPadWithFusedConv2D";
csinfo_.pad = "Pad";
Expand Down Expand Up @@ -506,12 +502,6 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});

rinfo_.push_back({csinfo_.fused_batch_matmul,
csinfo_.mkl_fused_batch_matmul, CopyAttrsAll,
AlwaysRewrite, kRewriteForOpNameChange});
rinfo_.push_back({csinfo_.fused_batch_matmul_v2,
csinfo_.mkl_fused_batch_matmul_v2, CopyAttrsAll,
AlwaysRewrite, kRewriteForOpNameChange});
rinfo_.push_back({csinfo_.gelu, mkl_op_registry::GetMklOpName(csinfo_.gelu),
CopyAttrsAll, GeluRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.gelu_grad,
Expand Down Expand Up @@ -964,8 +954,6 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
string fused_depthwise_conv2d;
string fused_matmul;
string fused_matmul_grad;
string fused_batch_matmul;
string fused_batch_matmul_v2;
string gelu;
string gelu_grad;
string identity;
Expand All @@ -991,8 +979,6 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
string mkl_fused_depthwise_conv2d;
string mkl_fused_matmul;
string mkl_fused_matmul_grad;
string mkl_fused_batch_matmul;
string mkl_fused_batch_matmul_v2;
string mkl_pad_with_conv2d;
string mkl_pad_with_fused_conv2d;
string mul;
Expand Down Expand Up @@ -3828,8 +3814,6 @@ MklLayoutRewritePass::CheckForNodeRewrite(const Node* n) const {
n->type_string() != csinfo_.fused_depthwise_conv2d &&
n->type_string() != csinfo_.fused_matmul &&
n->type_string() != csinfo_.fused_matmul_grad &&
n->type_string() != csinfo_.fused_batch_matmul &&
n->type_string() != csinfo_.fused_batch_matmul_v2 &&
!mkl_op_registry::IsMklOp(mkl_op_registry::GetMklOpName(n->type_string()),
T)) {
return nullptr;
Expand Down
25 changes: 0 additions & 25 deletions tensorflow/core/graph/mkl_layout_pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2033,31 +2033,6 @@ REGISTER_TEST_ALL_TYPES(NodeRewrite_FusedMatMul_Negative);
REGISTER_TEST_ALL_TYPES(NodeRewrite_FusedMatMulGrad_Positive);
#undef REGISTER_TEST

// Test set: _FusedBatchMatMul -> MklFusedBatchMatMul rewrite tests
#define REGISTER_TEST(NAME, T, INPUT) \
TEST_F(MklLayoutPassTest, NAME##_##T) { \
InitGraph( \
"node { name: 'A' op: '" #INPUT "'}" \
"node { name: 'B' op: '" #INPUT "'}" \
"node { name: 'C' op: '" #INPUT "'}" \
"node { name: 'D' op: '_FusedBatchMatMul'" \
" attr { key: 'T' value { type:" #T "} }" \
" attr { key: 'adj_x' value { b: false } }" \
" attr { key: 'adj_y' value { b: false } }" \
" attr { key: 'num_args' value { i: 1 } }" \
" attr { key: 'fused_ops' value { list: {s: 'Scale'} } }" \
" input: ['A', 'B', 'C']}" \
"node { name: 'Z' op: 'Zeta'" \
" attr {key: 'T' value { type: " #T " } }" \
" input: ['D', 'C']}"); \
EXPECT_EQ(DoMklLayoutOptimizationPass(), \
"A(" #INPUT ");B(" #INPUT ");C(" #INPUT ");" \
"D(_MklFusedBatchMatMul);Z(Zeta)" \
"|A->D;B->D:1;C->D:2;C->Z:1;D->Z"); \
}
REGISTER_TEST_ALL_TYPES(NodeRewrite_FusedBatchMatMul_Positive)
#undef REGISTER_TEST

// Merge test for PadWithFusedConv2D Op with BiasAdd fusion
// padding is VALID type
// A = input(image), B = input(paddings), C = Pad(A, B) = input of conv2D,
Expand Down
93 changes: 0 additions & 93 deletions tensorflow/core/grappler/optimizers/mkl_remapper_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -765,99 +765,6 @@ TEST_F(MklRemapperTest, FuseBatchNormWithAddAndRelu) {
EXPECT_EQ(found, 3);
}
}

class MklFuseBatchMatMulWithMul : public MklRemapperTest {
public:
void VerifyFused(bool adjx, bool adjy) {
using ::tensorflow::ops::Placeholder;
int b = 2;
int m = 2;
int k = 3;
int n = 4;

tensorflow::Scope s = tensorflow::Scope::NewRootScope();

auto input_shape = ops::Placeholder::Shape({b, m, k});
if (adjx) input_shape = ops::Placeholder::Shape({b, k, m});
auto weight_shape = ops::Placeholder::Shape({b, k, n});
if (adjy) weight_shape = ops::Placeholder::Shape({b, n, k});

auto input = Placeholder(s.WithOpName("input"), DT_FLOAT, input_shape);
auto weight = Placeholder(s.WithOpName("weight"), DT_FLOAT, weight_shape);

auto batchmatmul =
ops::BatchMatMulV2(s.WithOpName("batchmatmul"), input, weight,
ops::BatchMatMulV2::Attrs().AdjX(adjx).AdjY(adjy));
auto scale = ops::Const(s.WithOpName("scale"), {10.0f});
auto mul = ops::Multiply(s.WithOpName("mul"), batchmatmul, scale);

auto fetch_mul = ops::Identity(s.WithOpName("fetch_mul"), mul);

auto input_t = GenerateRandomTensor<DT_FLOAT>({b, m, k});
if (adjx) input_t = GenerateRandomTensor<DT_FLOAT>({b, k, m});
auto weight_t = GenerateRandomTensor<DT_FLOAT>({b, k, n});
if (adjy) weight_t = GenerateRandomTensor<DT_FLOAT>({b, n, k});

GrapplerItem item;
item.fetch = {"fetch_mul"};
item.feed = {{"input", input_t}, {"weight", weight_t}};
TF_CHECK_OK(s.ToGraphDef(&item.graph));

// Place all nodes on CPU.
for (int i = 0; i < item.graph.node_size(); ++i) {
item.graph.mutable_node(i)->set_device("/device:CPU:0");
}

Remapper optimizer(RewriterConfig::ON);
GraphDef output;
TF_CHECK_OK(optimizer.Optimize(nullptr, item, &output));

int found = 0;
for (const NodeDef& node : output.node()) {
if (node.name() == "mul") {
EXPECT_EQ("_FusedBatchMatMulV2", node.op());
EXPECT_EQ("input", node.input(0));
EXPECT_EQ("weight", node.input(1));
EXPECT_EQ("scale", node.input(2));

const auto fused_ops = node.attr().at("fused_ops").list().s();
EXPECT_EQ(1, fused_ops.size());
EXPECT_EQ("Mul", fused_ops[0]);
found++;
}
}
EXPECT_EQ(1, found);

auto tensors_expected = EvaluateNodes(item.graph, item.fetch, item.feed);
auto tensors = EvaluateNodes(output, item.fetch, item.feed);
test::ExpectTensorNear<float>(tensors_expected[0], tensors[0], 1e-6);
}
};

TEST_F(MklFuseBatchMatMulWithMul, a0b0) {
bool adjx = false;
bool adjy = false;
this->VerifyFused(adjx, adjy);
}

TEST_F(MklFuseBatchMatMulWithMul, a1b0) {
bool adjx = true;
bool adjy = false;
this->VerifyFused(adjx, adjy);
}

TEST_F(MklFuseBatchMatMulWithMul, a0b1) {
bool adjx = false;
bool adjy = true;
this->VerifyFused(adjx, adjy);
}

TEST_F(MklFuseBatchMatMulWithMul, a1b1) {
bool adjx = true;
bool adjy = true;
this->VerifyFused(adjx, adjy);
}

#endif // ENABLE_MKLDNN_V1

} // namespace grappler
Expand Down
Loading