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_r222592299
########## 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() Review comment: Yes, it's done by line 119. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
