Skip to content

asymmetric quant and bmm+mul fusion. #40

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
Feb 4, 2021
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
17 changes: 16 additions & 1 deletion tensorflow/core/common_runtime/mkl_layout_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
csinfo_.fused_conv2d = "_FusedConv2D";
csinfo_.fused_depthwise_conv2d = "_FusedDepthwiseConv2dNative";
csinfo_.fused_matmul = "_FusedMatMul";
csinfo_.fused_batch_matmul = "_FusedBatchMatMul";
csinfo_.fused_batch_matmul_v2 = "_FusedBatchMatMulV2";
csinfo_.identity = "Identity";
csinfo_.leakyrelu = "LeakyRelu";
csinfo_.leakyrelu_grad = "LeakyReluGrad";
Expand All @@ -300,6 +302,8 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
csinfo_.mkl_fused_conv2d = "_MklFusedConv2D";
csinfo_.mkl_fused_depthwise_conv2d = "_MklFusedDepthwiseConv2dNative";
csinfo_.mkl_fused_matmul = "_MklFusedMatMul";
csinfo_.mkl_fused_batch_matmul = "_MklFusedBatchMatMul";
csinfo_.mkl_fused_batch_matmul_v2 = "_MklFusedBatchMatMulV2";
csinfo_.mkl_native_conv2d_with_bias = "_MklNativeConv2DWithBias";
csinfo_.mkl_native_fused_batch_norm_ex = "_MklNativeFusedBatchNormEx";
csinfo_.mkl_native_fused_conv2d = "_MklNativeFusedConv2D";
Expand Down Expand Up @@ -510,7 +514,12 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
: csinfo_.mkl_fused_matmul,
CopyAttrsAllCheckConstFilter, FusedMatMulRewrite,
GetRewriteCause()});

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_.identity, mkl_op_registry::GetMklOpName(csinfo_.identity),
CopyAttrsAll, RewriteIfAtleastOneMklInput, GetRewriteCause()});
Expand Down Expand Up @@ -956,6 +965,8 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
string fused_conv2d;
string fused_depthwise_conv2d;
string fused_matmul;
string fused_batch_matmul;
string fused_batch_matmul_v2;
string identity;
string leakyrelu;
string leakyrelu_grad;
Expand All @@ -978,6 +989,8 @@ class MklLayoutRewritePass : public GraphOptimizationPass {
string mkl_fused_conv2d;
string mkl_fused_depthwise_conv2d;
string mkl_fused_matmul;
string mkl_fused_batch_matmul;
string mkl_fused_batch_matmul_v2;
string mkl_native_conv2d_with_bias;
string mkl_native_fused_batch_norm_ex;
string mkl_native_fused_conv2d;
Expand Down Expand Up @@ -3796,6 +3809,8 @@ MklLayoutRewritePass::CheckForNodeRewrite(const Node* n) const {
n->type_string() != csinfo_.fused_conv2d &&
n->type_string() != csinfo_.fused_depthwise_conv2d &&
n->type_string() != csinfo_.fused_matmul &&
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
50 changes: 50 additions & 0 deletions tensorflow/core/common_runtime/mkl_layout_pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,56 @@ REGISTER_TEST_ALL_TYPES(NodeRewrite_FusedMatMul_Negative);
REGISTER_TEST_ALL_TYPES(NodeRewrite_FusedMatMul_LeakyRelu_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

// Test set: _FusedBatchMatMulV2 -> MklFusedBatchMatMulV2 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: '_FusedBatchMatMulV2'" \
" 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(_MklFusedBatchMatMulV2);Z(Zeta)" \
"|A->D;B->D:1;C->D:2;C->Z:1;D->Z"); \
}
REGISTER_TEST_ALL_TYPES(NodeRewrite_FusedBatchMatMulV2_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
92 changes: 92 additions & 0 deletions tensorflow/core/grappler/optimizers/mkl_remapper_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,98 @@ TEST_F(MklRemapperTest, FuseMatMulWithBiasAddAndAdd) {
EXPECT_EQ(1, tensors.size());
test::ExpectClose(tensors_expected[0], tensors[0], 0, 1e-6);
}

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 = adjx ? ops::Placeholder::Shape({b, k, m})
: ops::Placeholder::Shape({b, m, k});
auto weight_shape = adjy ? ops::Placeholder::Shape({b, n, k})
: ops::Placeholder::Shape({b, k, n});

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 = adjx ? GenerateRandomTensor<DT_FLOAT>({b, k, m})
: GenerateRandomTensor<DT_FLOAT>({b, m, k});
auto weight_t = adjy ? GenerateRandomTensor<DT_FLOAT>({b, n, k})
: GenerateRandomTensor<DT_FLOAT>({b, k, n});

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