예제 #1
0
class TestRtManager(unittest.TestCase):

    rt_manager = RtManager()
    xf_exec_graph_factory = RuntimeFactory()

    @classmethod
    def setUpClass(cls):
        cls.rt_manager.register_rt('test', TestRuntime, X_2_T)

    def test_register_rt(self):
        rt_manager = TestRtManager.rt_manager
        xf_exec_graph_factory = TestRtManager.xf_exec_graph_factory

        assert('test' in rt_manager.runtimes)
        assert('test' in xf_exec_graph_factory._runtimes)

    def test_register_op(self):
        rt_manager = TestRtManager.rt_manager
        xf_exec_graph_factory = TestRtManager.xf_exec_graph_factory

        rt_manager.register_op('test', 'TestOp2', get_test_op_2)
        assert('TestOp2' in X_2_T)
예제 #2
0
class TestQuantSimPass(unittest.TestCase):

    xgraph_factory = XGraphFactory()
    xf_exec_graph_factory = RuntimeFactory()

    @classmethod
    def setUpClass(cls):

        def xgraph_build_func(xgraph):
            raise NotImplementedError("")

        def xgraph_optimizer(xgraph):
            raise NotImplementedError("")

        def xgraph_quantizer(xgraph):
            raise NotImplementedError("")

        def xgraph_compiler(xgraph):
            raise NotImplementedError("")

        target_registry = TargetRegistry()
        target_registry.register_target('npu_test',
                                        xgraph_optimizer,
                                        xgraph_quantizer,
                                        xgraph_compiler,
                                        xgraph_build_func)

        @register_op_support_check('npu_test', 'Convolution')
        def conv_op_support(X, bXs, tXs):
            return True

    @classmethod
    def tearDownClass(cls):

        target_registry = TargetRegistry()
        target_registry.unregister_target('npu_test')

    def test_conv(self):
        W = np.reshape(
                np.array([[[1, 2], [3, 0]], [[1, 1], [0, 1]]],
                         dtype=np.float32),
                (2, 1, 2, 2))
        B = np.array([0., 0.], dtype=np.float32)

        net = [
            XLayer(
                name='in',
                type=['Input'],
                shapes=[1, 1, 4, 4],
                sizes=[16],
                bottoms=[],
                tops=['conv2d0'],
                layer=['in'],
                targets=[]
            ),
            XLayer(
                name='conv2d0',
                type=['Convolution'],
                shapes=[1, 2, 3, 3],
                sizes=[18],
                bottoms=['in'],
                tops=[],
                layer=['conv2d0'],
                data=ConvData(W, B),
                attrs={
                    'data_layout': 'NCHW',
                    'kernel_layout': 'OIHW',
                    'kernel_size': [2, 2],
                    'shape': [1, 2, 3, 3],
                    'padding': [[0, 0], [0, 0], [0, 0], [0, 0]],
                    'strides': [1, 1],
                    'dilation': [1, 1],
                    'groups': 1
                },
                targets=[]
            )
        ]
        xgraph = TestQuantSimPass.xgraph_factory.build_from_xlayer(
            net, name='test1'
        )

        quant_sim_pass = XGraphQuantSimPass(
            fdir=FILE_PATH,
            name=xgraph.get_name() + '_qsim'
        )
        qsim_xgraph = quant_sim_pass.execute(xgraph=xgraph,
                                             subgraphs_only=False)

        exec_graph = TestQuantSimPass.xf_exec_graph_factory.build_runtime(
            qsim_xgraph
        )

        inpts = {
            'in': np.reshape(
                np.array([
                    [10, 10, 0, 40],
                    [50, 10, 0, 80],
                    [30, 50, 10, 0],
                    [10, 90, 30, 40]],
                    dtype=np.float32
                ),
                (1, 1, 4, 4))
        }
        res = exec_graph.run(inpts)
        outpt = res[0]
        # for idx, layer, inpts, outpt, _ in exec_graph.run_stepwise(inpts):
        #     print(layer.name, outpt)

        expected_outpt = np.array([[[
            [182.28346,    36.45669,    80.20472],
            [160.40944,   160.40944,   189.5748],
            [160.40944,   342.6929,    102.078735]],

            [[29.165354,    7.2913384, 123.95275],
             [109.37008,   21.874016,   80.20472],
             [167.70079,    87.49606,    51.039368]]]],
            dtype=np.float32)

        np.testing.assert_array_almost_equal(outpt, expected_outpt, decimal=4)

    def test_conv_maxpool_subgraph(self):
        W = np.reshape(
            np.array([[[1, 2], [3, 0]], [[1, 1], [0, 1]]], dtype=np.float32),
            (2, 1, 2, 2))
        B = np.array([0., 0.], dtype=np.float32)

        net = [
            XLayer(
                name='in',
                type=['Input'],
                shapes=[1, 1, 4, 4],
                sizes=[16],
                bottoms=[],
                tops=['conv2d0'],
                layer=['in'],
                targets=[]
            ),
            XLayer(
                name='conv2d0',
                type=['Convolution'],
                shapes=[1, 2, 3, 3],
                sizes=[18],
                bottoms=['in'],
                tops=[],
                layer=['conv2d0'],
                data=ConvData(W, B),
                attrs={
                    'data_layout': 'NCHW',
                    'kernel_layout': 'OIHW',
                    'shape': [1, 2, 3, 3],
                    'padding': [[0, 0], [0, 0], [0, 0], [0, 0]],
                    'strides': [1, 1],
                    'dilation': [1, 1],
                    'groups': 1
                },
                targets=[]
            ),
            XLayer(
                name='max_pool2d0',
                type=['Pooling'],
                shapes=[1, 2, 2, 2],
                sizes=[8],
                bottoms=['conv2d0'],
                tops=[],
                layer=['max_pool2d0'],
                attrs={
                    'kernel_size': [2, 2],
                    'insize': [3, 3],
                    'outsize': [2, 2],
                    'data_layout': 'NCHW',
                    'padding': [[0, 0], [0, 0], [0, 0], [0, 0]],
                    'strides': [1, 1],
                    'pool_type': 'Max'
                },
                targets=[]
            )
        ]
        xgraph = TestQuantSimPass.xgraph_factory.build_from_xlayer(
            net, name='testtest'
        )
        p_xgraph = partition(xgraph, ['npu_test'])

        assert p_xgraph.get_layers()[0].target == 'cpu'
        assert p_xgraph.get_layers()[1].target == 'npu_test'
        assert p_xgraph.get_layers()[2].target == 'cpu'

        assert p_xgraph.get_layers()[0].subgraph is None
        assert p_xgraph.get_layers()[1].subgraph == 'xp0'
        assert p_xgraph.get_layers()[2].subgraph is None

        quant_sim_pass = XGraphQuantSimPass(
            fdir=FILE_PATH,
            name=xgraph.get_name() + '_qsim'
        )
        qsim_xgraph = quant_sim_pass.execute(xgraph=p_xgraph,
                                             subgraphs_only=True)

        exec_graph = TestQuantSimPass.xf_exec_graph_factory.build_runtime(
            qsim_xgraph
        )

        inpts = {
            'in': np.reshape(
                np.array([
                    [10, 10, 0, 40],
                    [50, 10, 0, 80],
                    [30, 50, 10, 0],
                    [10, 90, 30, 40]],
                    dtype=np.float32
                ),
                (1, 1, 4, 4))
        }
        res = exec_graph.run(inpts)
        outpt = res[0]

        expected_outpt = np.array([[
            [[182.28346, 189.5748],
             [342.6929, 342.6929]],
            [[109.37008, 123.95275],
             [167.70079, 87.49606]]]],
            dtype=np.float32)

        np.testing.assert_array_almost_equal(outpt, expected_outpt, decimal=4)
예제 #3
0
class TfGenerator(object):
    """
    Responsible for generating tensorflow model from xgraph data structure
    """

    runtime_factory = RuntimeFactory()
    xgraph_factory = XGraphFactory()
    xgraph_partitioner = XGraphPartitioner()

    @classmethod
    def generate(cls,
                 xgraph,
                 base_name,
                 subgraphs_only=False,
                 layout='NCHW',
                 batch_size=-1,
                 placeholder=False,
                 out_dir=os.getcwd()):
        # type: (XGraph, str, boolean, str, int) -> Dict[str, str]
        """
        Generate one or multiple tensorflow pb file from an xgraph and
        return dictionary of the base_name/partitions mapping to the pb files
        """
        # layout_transform_pass = XGraphLayoutTransformationPass(layout)
        # xgraph = layout_transform_pass.execute(xgraph, subgraphs_only=False)

        # Import tensorflow only when needed
        import tensorflow as tf

        executors = []
        if not subgraphs_only:
            executors.append((base_name, base_name,
                              TfGenerator.runtime_factory.build_runtime(
                                  xgraph,
                                  batch_size=batch_size,
                                  placeholder=placeholder)))
        else:
            for Xp in \
                    TfGenerator.xgraph_partitioner.get_subgraphs(xgraph):

                sub_xgraph = TfGenerator.xgraph_factory.build_from_xlayer(
                    Xp.subgraph_data)
                executors.append((base_name + '_' + Xp.name, Xp.name,
                                  TfGenerator.runtime_factory.build_runtime(
                                      sub_xgraph,
                                      batch_size=batch_size,
                                      placeholder=placeholder),
                                  sub_xgraph.get_output_names()))

        ret = {}
        for file_name, name, executor, output_names in executors:
            graph_def = executor.tf_graph.as_graph_def()
            # with executor.tf_step_graph.as_default():
            #    graph_def = tf.get_default_graph().as_graph_def()

            with tf.compat.v1.Session(graph=executor.tf_graph) as sess:
                sess.run(tf.compat.v1.global_variables_initializer())
                graph_def = tf.graph_util.convert_variables_to_constants(
                    sess, graph_def, output_names)

            file_path = os.path.join(out_dir, file_name + '.pb')

            with tf.gfile.GFile(file_path, "wb") as f:
                f.write(graph_def.SerializeToString())

            ret[name] = file_path

        return ret

    @classmethod
    def run(self, xgraph, pb_file, inputs):
        # type: (XGraph, str, dict[str, numpy.ndarray])
        """ Run frozen tensorflow graph for corresponding XGraph """

        # Import Tensorflow only when needed
        import tensorflow as tf

        input_graph_def = tf.GraphDef()
        with tf.gfile.GFile(frozen_graph, "rb") as f:
            input_graph_def.ParseFromString(f.read())

        tf.compat.v1.reset_default_graph()
        tf.import_graph_def(input_graph_def, name='')

        input_names = xgraph.get_input_names()
        output_names = xgraph.get_output_names()

        inputs_tf = {}
        for in_name in input_names:
            input_tensor = tf.get_default_graph().get_tensor_by_name(in_name +
                                                                     ':0')

            inputs_tf[input_tensor] = inputs[in_name]

        outputs = [
            tf.get_default_graph().get_tensor_by_name(out_name + ':0')
            for out_name in output_names
        ]

        with tf.compat.v1.Session() as sess:
            res = sess.run(outputs, feed_dict=inputs_tf)

        return res
예제 #4
0
class TestRuntimeFactory(unittest.TestCase):

    xgraph_factory = XGraphFactory()
    runtime_factory = RuntimeFactory()

    def test_runtime_factory_net_params(self):

        xlayers = [
            XLayer(name='in1',
                   type=['Input'],
                   bottoms=[],
                   tops=['conv1'],
                   targets=[]),
            XLayer(name='in2',
                   type=['Input'],
                   bottoms=[],
                   tops=['add1'],
                   targets=[]),
            XLayer(name='conv1',
                   type=['Convolution'],
                   bottoms=['in1'],
                   tops=['add1'],
                   data=ConvData(weights=np.array([[[[1, 2], [3, 4]]]],
                                                  dtype=np.float32),
                                 biases=np.array([0., 1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='add1',
                   type=['Eltwise'],
                   bottoms=['conv1', 'in2'],
                   tops=['conv2', 'pool1'],
                   targets=[]),
            XLayer(name='conv2',
                   type=['Convolution'],
                   bottoms=['add1'],
                   tops=['add2'],
                   data=ConvData(weights=np.array([[[[4, 5], [6, 7]]]],
                                                  dtype=np.float32),
                                 biases=np.array([0., -1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='pool1',
                   type=['Pooling'],
                   bottoms=['add1'],
                   tops=['add2'],
                   targets=[]),
            XLayer(name='add2',
                   type=['Eltwise'],
                   bottoms=['conv2', 'pool1'],
                   tops=[],
                   targets=[]),
            XLayer(name='pool2',
                   type=['Pooling'],
                   bottoms=['add1'],
                   tops=['pool3'],
                   targets=[]),
            XLayer(name='pool3',
                   type=['Pooling'],
                   bottoms=['pool2'],
                   tops=[],
                   targets=[])
        ]
        xgraph = TestRuntimeFactory.xgraph_factory.build_from_xlayer(xlayers)

        # 1.
        net, params = TestRuntimeFactory.runtime_factory\
            ._get_net_and_params(xgraph, ['add1'])

        assert len(net) == 4
        np.testing.assert_array_equal(
            params['conv1_kernel'],
            np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
        np.testing.assert_array_equal(params['conv1_biases'],
                                      np.array([0., 1.], dtype=np.float32))

        # 2.
        net, params = TestRuntimeFactory.runtime_factory\
            ._get_net_and_params(xgraph, ['conv1'])

        assert len(net) == 2
        assert net[0].name == 'in1'
        assert net[1].name == 'conv1'
        np.testing.assert_array_equal(
            params['conv1_kernel'],
            np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
        np.testing.assert_array_equal(params['conv1_biases'],
                                      np.array([0., 1.], dtype=np.float32))

        # 3.
        net, params = TestRuntimeFactory.runtime_factory\
            ._get_net_and_params(xgraph, ['pool1', 'pool2'])

        assert len(net) == 7
        np.testing.assert_array_equal(
            params['conv1_kernel'],
            np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
        np.testing.assert_array_equal(params['conv1_biases'],
                                      np.array([0., 1.], dtype=np.float32))