ZhennanQin commented on a change in pull request #12530: Implement mkldnn 
convolution fusion and quantization.
URL: https://github.com/apache/incubator-mxnet/pull/12530#discussion_r222604179
 
 

 ##########
 File path: tests/python/mkl/test_subgraph.py
 ##########
 @@ -0,0 +1,472 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import sys
+import os
+import mxnet as mx
+import numpy as np
+import unittest
+import ctypes
+from mxnet.io import NDArrayIter
+from mxnet.module import Module
+from mxnet.symbol import Symbol
+from importlib import import_module
+from numpy.testing import assert_allclose
+from mxnet.base import SymbolHandle, check_call, _LIB, mx_uint, c_str
+from mxnet.test_utils import DummyIter
+curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
+sys.path.append(os.path.join(curr_path, '../unittest/'))
+from common import with_seed
+
+DATA_SHAPE=[(4, 4, 10, 10), (32, 3, 24, 24), (64, 8, 64, 64)]
+DATA_LABEL=[(4, 10), (32, 10), (64, 10)]
+MIN_VALUE=-1.0
+MAX_VALUE=1.0
+
+def check_qsym_calibrated(qsym):
+  assert ''.join(qsym.attr_dict().keys()).find('quantized_sg_mkldnn_conv') != 
-1
+  for k, v in qsym.attr_dict().items():
+    if k.find('quantized_sg_mkldnn_conv') != -1:
+      assert 'min_calib_range' in v
+      assert 'max_calib_range' in v
+    if k.find('_quantize') != -1:
+      assert v['out_type'] == 'uint8'
+
+def check_qsym_forward(qsym, qarg_params, qaux_params, batch, data_shape, 
label_shape):
+  mod = mx.mod.Module(symbol=qsym, context=mx.current_context())
+  mod.bind(for_training=False,
+           data_shapes=[('data', data_shape)],
+           label_shapes=[('softmax_label', label_shape)])
+  mod.set_params(qarg_params, qaux_params)
+  mod.forward(batch, is_train=False)
+  for output in mod.get_outputs():
+    output.wait_to_read()
+  return output
+
+def check_quantize(sym, arg_params, aux_params, data_shape, label_shape, 
batch, sym_output):
+  excluded_sym_names = []
+  if mx.current_context() == mx.cpu():
+    excluded_sym_names += ['fc']
+  calib_data = mx.nd.random.uniform(shape=data_shape)
+  calib_data = NDArrayIter(data=calib_data)
+  calib_data = DummyIter(calib_data)
+  calib_layer = lambda name: name.endswith('_output')
+  qsym, qarg_params, qaux_params = mx.contrib.quant.quantize_model(sym=sym,
+                                                                   
arg_params=arg_params,
+                                                                   
aux_params=aux_params,
+                                                                   
ctx=mx.current_context(),
+                                                                   
excluded_sym_names=excluded_sym_names,
+                                                                   
quantized_dtype='uint8',
+                                                                   
calib_mode='naive',
+                                                                   
calib_data=calib_data,
+                                                                   
calib_layer=calib_layer,
+                                                                   
calib_quantize_op=True,
+                                                                   
num_calib_examples=20)
+  qsym = qsym.get_backend_symbol("MKLDNN_POST_QUANTIZE")
+  check_qsym_calibrated(qsym)
+  qsym_output = check_qsym_forward(qsym, qarg_params, qaux_params, batch, 
data_shape, label_shape)
+
+  diff = mx.nd.abs(sym_output - qsym_output.astype(sym_output.dtype))
+  cond = mx.nd.lesser(2, diff).sum().asscalar()
+  assert cond == 0
+
+@with_seed()
+def check_fusion(sym, data_shape, label_shape, attrs_op):
+  dev = mx.cpu()
+  mod = Module(symbol=sym)
+  mod.bind(data_shapes=[('data', data_shape)], label_shapes=[('softmax_label', 
label_shape)])
+  mod.init_params(mx.init.Normal(0.5))
+  arg_params, aux_params = mod.get_params()
+
+  data = [mx.random.uniform(MIN_VALUE, MAX_VALUE, shape=shape, ctx=dev) for _, 
shape in mod.data_shapes]
+  batch = mx.io.DataBatch(data, [])
+
+  mod.forward(batch, is_train=False)
+  for output in mod.get_outputs():
+      output.wait_to_read()
+
+  sym_sg = sym.get_backend_symbol("MKLDNN")
+  mod_sg = Module(symbol=sym)
+  mod_sg.bind(data_shapes=[('data', data_shape)], 
label_shapes=[('softmax_label', label_shape)])
+  mod_sg.set_params(arg_params, aux_params)
+
+  mod_sg.forward(batch, is_train=False)
+  for output_sg in mod_sg.get_outputs():
+      output_sg.wait_to_read()
+
+  assert ''.join(sym_sg.get_internals().list_outputs()).find('sg_mkldnn_conv') 
!= -1
+
+  for k, v in sym_sg.attr_dict().items():
+    if k.find('sg_mkldnn_conv') != -1:
+      for attr_op in attrs_op:
+        assert v[attr_op] == 'true'
+
+  # Check the result accuracy based on fp32 fusion
+  assert_allclose(output[0].asnumpy(), output_sg[0].asnumpy(), rtol = 0)
+  # fp32 to uint8
+  check_quantize(sym_sg, arg_params, aux_params, data_shape, label_shape, 
batch, output_sg)
+
+def check_neg_fusion(syms, attrs_name=None, excluded_attrs=None, 
date_shape=(4,4,10,10)):
+  for sym, attrs, excluded_attr in zip(syms, attrs_name, excluded_attrs):
+    sym_sg = sym.get_backend_symbol("MKLDNN")
+    exe_sg = sym_sg.simple_bind(mx.cpu(), data=date_shape, grad_req='null')
+
+    attrs_dict = sym_sg.attr_dict()
+    for k, v in attrs_dict.items():
+      if k.find('sg_mkldnn_conv') != -1:
+        for attr in attrs:
+          assert v[attr] == 'true'
+        for exc_attr in excluded_attr:
+          assert exc_attr not in v.keys()
+
+def head_symbol():
+  data = mx.symbol.Variable('data', dtype='float32')
+  weight = mx.symbol.Variable('weight', dtype='float32')
+  bn = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=0.9, 
name='bn')
+  return bn, weight
+
+def tail_symbol(sym):
+  fc = mx.sym.FullyConnected(data=sym, num_hidden=10, flatten=True, name='fc')
+  sym = mx.sym.SoftmaxOutput(data=fc, name='softmax')
+  return sym
+
+# single conv fuision case
+def single_conv(no_bias):
+  conv_attr = ['']
+  data, weight = head_symbol()
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64,
+                               kernel=(3, 3), stride=(1, 1), no_bias=no_bias)
+  sym = tail_symbol(conv)
+  return sym, conv_attr
+
+# conv + bn fusion case
+def conv_bn(no_bias):
+  conv_bn_attr = ['with_bn']
+  data, weight = head_symbol()
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64,
+                               kernel=(3, 3), stride=(1, 1), no_bias=no_bias)
+  bn1 = mx.symbol.BatchNorm(data=conv, name="bn1")
+  sym = tail_symbol(bn1)
+  return sym, conv_bn_attr
+
+# conv + relu fusion case
+def conv_relu(no_bias):
+  conv_relu_attr = ['with_relu']
+  data, weight = head_symbol()
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64,
+                               kernel=(3, 3), stride=(1, 1), no_bias=no_bias)
+  relu = mx.symbol.Activation(data=conv, name='relu', act_type="relu")
+  sym = tail_symbol(relu)
+  return sym, conv_relu_attr
+
+# conv + add fusion case
+def conv_add(no_bias):
+  conv_add_attr = ['with_sum']
+  data, weight = head_symbol()
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64,
+                               kernel=(3, 3), stride=(1, 1), no_bias=no_bias)
+  conv1 = mx.symbol.Convolution(data=data, weight=weight, name='conv1', 
num_filter=64,
+                                kernel=(3, 3), stride=(1, 1))
+  sum = conv + conv1
+  sym = tail_symbol(sum)
+  return sym, conv_add_attr
+
+# conv + bn + relu fusion case
+def conv_bn_relu(no_bias):
+  conv_bn_relu_attr = ['with_bn', 'with_relu']
+  data, weight = head_symbol()
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64,
+                               kernel=(3, 3), stride=(1, 1), no_bias=no_bias)
+  bn1 = mx.symbol.BatchNorm(data=conv, name="bn1")
+  relu = mx.symbol.Activation(data=bn1, name='relu', act_type="relu")
+  sym = tail_symbol(relu)
+  return sym, conv_bn_relu_attr
+
+# conv + bn + add + relu fusion case
+def conv_bn_sum_relu(no_bias):
+  conv_bn_add_relu_attr = ['with_sum', 'with_postsum_relu', 'with_bn']
+  data, weight = head_symbol()
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64,
+                               kernel=(3, 3), stride=(1, 1), no_bias=no_bias)
+  bn1 = mx.symbol.BatchNorm(data=conv, name="bn1")
+  conv1 = mx.symbol.Convolution(data=data, weight=weight, name='conv1', 
num_filter=64,
+                                kernel=(3, 3), stride=(1, 1))
+  sum1 = bn1 + conv1
+  relu = mx.symbol.Activation(data=sum1, name='relu', act_type="relu")
+  sym = tail_symbol(relu)
+  return sym, conv_bn_add_relu_attr
+
+def tail_neg_symbol(sym1, sym2):
+  fc1 = mx.sym.FullyConnected(data=sym1, num_hidden=10, flatten=True, 
name='fc1')
+  fc2 = mx.sym.FullyConnected(data=sym2, num_hidden=10, flatten=True, 
name='fc2')
+  concat = mx.sym.Concat(*[fc1, fc2], name="concat")
+  sym = mx.sym.SoftmaxOutput(data=concat, name='softmax')
+  return sym
+
+# conv + bn can't be fusion case
+# eg.1
+# conv --------- > bn
+#  |
+#  |
+#  -------------> [custom op]
+def neg_conv_bn():
+  syms = []
+  attrs = []
+  excluded_attrs = []
+  data, weight = head_symbol()
+
+  # eg.1 ([custom op] = pool)
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  bn1 = mx.symbol.BatchNorm(data=conv, name="bn1")
+  pool = mx.sym.Pooling(data=conv, kernel=(4, 4), pool_type='avg', name='pool')
+  sym = tail_neg_symbol(bn1, pool)
+
+  syms.append(sym)
+  attrs.append([])
+  excluded_attrs.append([])
+  return syms, attrs, excluded_attrs
+
+# conv + relu can't be fusion case
+# eg.1
+# conv -----------> relu
+#  |
+#  |
+#  ---------------> [custom op]
+def neg_conv_relu():
+  syms = []
+  attrs = []
+  excluded_attrs = []
+  data, weight = head_symbol()
+
+  # eg.1 ([custom op] = pool)
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  relu = mx.symbol.Activation(data=conv, name='relu', act_type="relu")
+  pool = mx.sym.Pooling(data=conv, kernel=(4, 4), pool_type='avg', name='pool')
+  sym = tail_neg_symbol(relu, pool)
+
+  syms.append(sym)
+  attrs.append([])
+  excluded_attrs.append([])
+  return syms, attrs, excluded_attrs
+
+# conv + add can't be fusion case
+# eg.1
+#  ---------------> [custom op]
+#  |
+#  |
+# conv -----------> add
+#                   |
+#                   |
+# added ------------>
+def neg_conv_add():
+  syms = []
+  attrs = []
+  excluded_attrs = []
+  val = mx.symbol.Variable('addval')
+  data, weight = head_symbol()
+
+  # eg.1 ([custom op] = pool, [added op] = val)
+  conv = mx.symbol.Convolution(data=data, weight=weight, name='conv', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  sum1 = conv + val
+  pool = mx.sym.Pooling(data=conv, kernel=(4, 4), pool_type='avg', name='pool')
+  sym = tail_neg_symbol(sum1, pool)
+
+  syms.append(sym)
+  attrs.append([])
+  excluded_attrs.append('with_sum')
+  return syms, attrs, excluded_attrs
+
+# conv + bn + relu can't be fusion case
+# eg.1
+#   --------------> [custom op]
+#   |
+# conv -----------> bn -----------> relu
+#
+# eg.2
+#                   --------------> [custom op]
+#                   |
+# conv -----------> bn -----------> relu
+def neg_conv_bn_relu():
+  syms = []
+  attrs = []
+  excluded_attrs = []
+  data, weight = head_symbol()
+
+  # eg.1 ([custom op] = pool11)
+  conv11 = mx.symbol.Convolution(data=data, weight=weight, name='conv11', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  bn11 = mx.symbol.BatchNorm(data=conv11, name="bn11")
+  relu11 = mx.symbol.Activation(data=bn11, name='relu11', act_type="relu")
+  pool11 = mx.sym.Pooling(data=conv11, kernel=(4, 4), pool_type='avg', 
name='pool11')
+  sym1 = tail_neg_symbol(relu11, pool11)
+
+  syms.append(sym1)
+  attrs.append([])
+  excluded_attrs.append([])
+
+  # eg.2 ([custom op] = pool)
+  conv21 = mx.symbol.Convolution(data=data, weight=weight, name='conv21', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  bn21 = mx.symbol.BatchNorm(data=conv21, name="bn21")
+  relu21 = mx.symbol.Activation(data=bn21, name='relu21', act_type="relu")
+  pool21 = mx.sym.Pooling(data=bn21, kernel=(4, 4), pool_type='avg', 
name='pool21')
+  sym2 = tail_neg_symbol(relu21, pool21)
+
+  syms.append(sym2)
+  attrs.append(['with_bn'])
+  excluded_attrs.append(['with_relu'])
+  return syms, attrs, excluded_attrs
+
+# conv + bn + add + relu can't be fusion case
+# eg.1
+#   --------------> [custom op]
+#   |
+# conv -----------> bn -----------> add -----------> relu
+#
+# eg.2
+#                    -------------> [custom op]
+#                    |
+# conv -----------> bn -----------> add -----------> relu
+#
+# eg.3
+#                                    --------------> [custom op]
+#                                    |
+# conv -----------> bn -----------> add -----------> relu
+def neg_conv_bn_add_relu():
+  syms = []
+  attrs = []
+  excluded_attrs = []
+  addVal = mx.symbol.Variable('addval')
+  data, weight = head_symbol()
+
+  # eg.1
+  conv11 = mx.symbol.Convolution(data=data, weight=weight, name='conv11', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  bn11 = mx.symbol.BatchNorm(data=conv11, name="bn11")
+  sum11 = bn11 + addVal
+  relu11 = mx.symbol.Activation(data=sum11, name='relu11', act_type="relu")
+  pool11 = mx.sym.Pooling(data=conv11, kernel=(4, 4), pool_type='avg', 
name='pool11')
+  sym1 = tail_neg_symbol(relu11, pool11)
+
+  syms.append(sym1)
+  attrs.append([])
+  excluded_attrs.append(['with_sum', 'with_postsum_relu', 'with_bn'])
+
+  # eg.2
+  conv21 = mx.symbol.Convolution(data=data, weight=weight, name='conv21', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  bn21 = mx.symbol.BatchNorm(data=conv21, name="bn21")
+  sum21 = bn21 + addVal
+  relu21 = mx.symbol.Activation(data=sum21, name='relu21', act_type="relu")
+  pool21 = mx.sym.Pooling(data=bn21, kernel=(4, 4), pool_type='avg', 
name='pool21')
+  sym2 = tail_neg_symbol(relu21, pool21)
+
+  syms.append(sym2)
+  attrs.append(['with_bn'])
+  excluded_attrs.append(['with_sum', 'with_postsum_relu'])
+
+  # eg.3
+  conv31 = mx.symbol.Convolution(data=data, weight=weight, name='conv31', 
num_filter=64, kernel=(3, 3), stride=(1, 1))
+  bn31 = mx.symbol.BatchNorm(data=conv31, name="bn31")
+  sum31 = bn31 + addVal
+  relu31 = mx.symbol.Activation(data=sum31, name='relu31', act_type="relu")
+  pool31 = mx.sym.Pooling(data=sum31, kernel=(4, 4), pool_type='avg', 
name='pool31')
+  sym3 = tail_neg_symbol(relu31, pool31)
+
+  syms.append(sym3)
+  attrs.append(['with_bn', 'with_sum'])
+  excluded_attrs.append(['with_postsum_relu'])
+  return syms, attrs, excluded_attrs
+
+@with_seed()
+def test_pos_single_conv():
+  for data_shape, label_shape in zip(DATA_SHAPE, DATA_LABEL):
+    net, attrs = single_conv(False)
+    check_fusion(net, data_shape, label_shape, attrs)
+    net, attrs = single_conv(True)
+    check_fusion(net, data_shape, label_shape, attrs)
+
+@with_seed()
+def test_pos_conv_relu():
+  for data_shape, label_shape in zip(DATA_SHAPE, DATA_LABEL):
+    net, attrs = conv_relu(False)
+    check_fusion(net, data_shape, label_shape, attrs)
+    net, attrs = conv_relu(True)
+    check_fusion(net, data_shape, label_shape, attrs)
+
+@with_seed()
+def test_pos_conv_bn():
+  for data_shape, label_shape in zip(DATA_SHAPE, DATA_LABEL):
+    net, attrs = conv_bn(False)
+    check_fusion(net, data_shape, label_shape, attrs)
+    net, attrs = conv_bn(True)
+    check_fusion(net, data_shape, label_shape, attrs)
+
+@with_seed()
+def test_pos_conv_add():
+  for data_shape, label_shape in zip(DATA_SHAPE, DATA_LABEL):
+    net, attrs = conv_add(False)
+    check_fusion(net, data_shape, label_shape, attrs)
+    net, attrs = conv_add(True)
+    check_fusion(net, data_shape, label_shape, attrs)
+
+@with_seed()
+def test_pos_conv_bn_relu():
+  for data_shape, label_shape in zip(DATA_SHAPE, DATA_LABEL):
+    net, attrs = conv_bn_relu(False)
+    check_fusion(net, data_shape, label_shape, attrs)
+    net, attrs = conv_bn_relu(True)
+    check_fusion(net, data_shape, label_shape, attrs)
+
+@with_seed()
+def test_pos_conv_bn_sum_relu():
+  for data_shape, label_shape in zip(DATA_SHAPE, DATA_LABEL):
+    net, attrs = conv_bn_sum_relu(False)
+    check_fusion(net, data_shape, label_shape, attrs)
+    net, attrs = conv_bn_sum_relu(True)
+    check_fusion(net, data_shape, label_shape, attrs)
+
+@with_seed()
+def test_neg_conv_bn():
+  for data_shape in DATA_SHAPE:
+    syms, attrs, excluded_attrs = neg_conv_bn()
+    check_neg_fusion(syms, attrs, excluded_attrs, data_shape)
+
+@with_seed()
+def test_neg_conv_relu():
+  for data_shape in DATA_SHAPE:
+    syms, attrs, excluded_attrs = neg_conv_relu()
+    check_neg_fusion(syms, attrs, excluded_attrs, data_shape)
+
+@with_seed()
+def test_neg_conv_add():
+  for data_shape in DATA_SHAPE:
+    syms, attrs, excluded_attrs = neg_conv_add()
+    check_neg_fusion(syms, attrs, excluded_attrs, data_shape)
+
+@with_seed()
+def test_neg_conv_bn_relu():
+  for data_shape in DATA_SHAPE:
+    syms, attrs, excluded_attrs = neg_conv_bn_relu()
+    check_neg_fusion(syms, attrs, excluded_attrs, data_shape)
+
+@with_seed()
+def test_neg_conv_bn_add_relu():
+  for data_shape in DATA_SHAPE:
+    syms, attrs, excluded_attrs = neg_conv_bn_add_relu()
+    check_neg_fusion(syms, attrs, excluded_attrs, data_shape)
+
+
+if __name__ == "__main__":
+  import nose
+  nose.runmodule()
 
 Review comment:
   Why make lint can't report this?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to