Esempio n. 1
0
    def test_xgraph_device_tagging(self):

        xgraph = XGraph()
        xgraph.add(XLayer(
            name='in1',
            type=['Input'],
            bottoms=[],
            tops=[],
            targets=[]
        ))

        xgraph.add(XLayer(
            name='in2',
            type=['Input'],
            bottoms=[],
            tops=[],
            targets=[]
        ))

        xgraph.add(XLayer(
            name='conv1',
            type=['Convolution'],
            bottoms=['in1'],
            tops=[],
            data=ConvData(
                weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                biases=np.array([0., 1.], dtype=np.float32)
            ),
            targets=[]
        ))

        xgraph.add(XLayer(
            name='add1',
            type=['Eltwise'],
            bottoms=['conv1', 'in2'],
            tops=[],
            targets=[]
        ))

        xgraph.insert(XLayer(
            name='conv2',
            type=['Convolution'],
            bottoms=['in2'],
            tops=['add1'],
            data=ConvData(
                weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                biases=np.array([0., 1.], dtype=np.float32)
            ),
            targets=[]
        ))

        xgraph.add(XLayer(
            name='pool1',
            type=['Pooling'],
            bottoms=['add1'],
            tops=[],
            targets=[]
        ))
        xgraph = partition(xgraph, ['cpu'])
        assert len(xgraph) == 6
        xlayers = xgraph.get_layers()
        assert xgraph.get_layer_names() == \
            ['in1', 'conv1', 'in2', 'conv2', 'add1', 'pool1']
        assert set(xlayers[0].targets) == set(['cpu', 'qsim'])
        assert set(xlayers[1].targets) == set(['cpu', 'qsim', 'test'])
        assert set(xlayers[2].targets) == set(['cpu', 'qsim'])
        assert set(xlayers[3].targets) == set(['cpu', 'qsim', 'test'])
        assert set(xlayers[4].targets) == set(['cpu', 'qsim'])
        assert set(xlayers[5].targets) == set(['cpu', 'qsim', 'test'])

        xgraph.remove('conv1')
        assert len(xgraph) == 5
        xlayers = xgraph.get_layers()

        assert xgraph.get_layer_names() == \
            ['in1', 'in2', 'conv2', 'add1', 'pool1']

        assert xlayers[3].type[0] == 'Eltwise'
        assert xlayers[3].bottoms == ['in1', 'conv2']

        assert set(xlayers[0].targets) == set(['cpu', 'qsim'])
        assert set(xlayers[1].targets) == set(['cpu', 'qsim'])
        assert set(xlayers[2].targets) == set(['cpu', 'qsim', 'test'])
        assert set(xlayers[3].targets) == set(['cpu', 'qsim'])
        assert set(xlayers[4].targets) == set(['cpu', 'qsim', 'test'])
Esempio n. 2
0
    def test_xgraph_device_tagging(self):

        xgraph = XGraph()
        xgraph.add(
            XLayer(name="in1", type=["Input"], bottoms=[], tops=[],
                   targets=[]))

        xgraph.add(
            XLayer(name="in2", type=["Input"], bottoms=[], tops=[],
                   targets=[]))

        xgraph.add(
            XLayer(
                name="conv1",
                type=["Convolution"],
                bottoms=["in1"],
                tops=[],
                data=ConvData(
                    weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                    biases=np.array([0.0, 1.0], dtype=np.float32),
                ),
                targets=[],
            ))

        xgraph.add(
            XLayer(
                name="add1",
                type=["Eltwise"],
                bottoms=["conv1", "in2"],
                tops=[],
                targets=[],
            ))

        xgraph.insert(
            XLayer(
                name="conv2",
                type=["Convolution"],
                bottoms=["in2"],
                tops=["add1"],
                data=ConvData(
                    weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                    biases=np.array([0.0, 1.0], dtype=np.float32),
                ),
                targets=[],
            ))

        xgraph.add(
            XLayer(name="pool1",
                   type=["Pooling"],
                   bottoms=["add1"],
                   tops=[],
                   targets=[]))
        xgraph = partition(xgraph, ["cpu"])
        assert len(xgraph) == 6
        xlayers = xgraph.get_layers()
        assert xgraph.get_layer_names() == [
            "in1",
            "conv1",
            "in2",
            "conv2",
            "add1",
            "pool1",
        ]
        assert set(xlayers[0].targets) == set(["cpu", "qsim"])
        assert set(xlayers[1].targets) == set(["cpu", "qsim", "test"])
        assert set(xlayers[2].targets) == set(["cpu", "qsim"])
        assert set(xlayers[3].targets) == set(["cpu", "qsim", "test"])
        assert set(xlayers[4].targets) == set(["cpu", "qsim"])
        assert set(xlayers[5].targets) == set(["cpu", "qsim", "test"])

        xgraph.remove("conv1")
        assert len(xgraph) == 5
        xlayers = xgraph.get_layers()

        assert xgraph.get_layer_names() == [
            "in1", "in2", "conv2", "add1", "pool1"
        ]

        assert xlayers[3].type[0] == "Eltwise"
        assert xlayers[3].bottoms == ["in1", "conv2"]

        assert set(xlayers[0].targets) == set(["cpu", "qsim"])
        assert set(xlayers[1].targets) == set(["cpu", "qsim"])
        assert set(xlayers[2].targets) == set(["cpu", "qsim", "test"])
        assert set(xlayers[3].targets) == set(["cpu", "qsim"])
        assert set(xlayers[4].targets) == set(["cpu", "qsim", "test"])
Esempio n. 3
0
    def test_xgraph_insert(self):

        xgraph = XGraph()
        xgraph.add(XLayer(
            name='in1',
            type=['Input'],
            bottoms=[],
            tops=[],
            targets=[]
        ))

        assert(len(xgraph) == 1)
        assert(len(xgraph.get_layer_names()) == 1)
        assert(len(xgraph.get_output_names()) == 1)
        assert(len(xgraph.get_input_names()) == 1)

        X_conv = XLayer(
            name='conv1',
            type=['Convolution'],
            bottoms=['in1'],
            tops=[],
            data=ConvData(
                weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                biases=np.array([0., 1.], dtype=np.float32)
            ),
            targets=[]
        )
        xgraph.add(X_conv)

        assert len(xgraph) == 2
        assert len(xgraph.get_layer_names()) == 2
        assert len(xgraph.get_output_names()) == 1
        assert len(xgraph.get_input_names()) == 1

        X_pool = XLayer(
            name='pool1',
            type=['Pooling'],
            bottoms=['in1'],
            tops=['conv1'],
            targets=[]
        )
        xgraph.insert(X_pool)

        assert len(xgraph) == 3
        assert len(xgraph.get_layer_names()) == 3
        assert len(xgraph.get_output_names()) == 1
        assert len(xgraph.get_input_names()) == 1

        xlayers = xgraph.get_layers()
        assert xlayers[0].name == 'in1'
        assert xlayers[0].bottoms == []
        assert xlayers[0].tops == ['pool1']
        assert xlayers[1].name == 'pool1'
        assert xlayers[1].bottoms == ['in1']
        assert xlayers[1].tops == ['conv1']
        assert xlayers[2].name == 'conv1'
        assert xlayers[2].bottoms == ['pool1']
        assert xlayers[2].tops == []

        X_in2 = XLayer(
            name='in2',
            type=['Input'],
            bottoms=[],
            tops=[],
            targets=[]
        )
        xgraph.add(X_in2)

        X_add = XLayer(
            name='add1',
            type=['Eltwise'],
            bottoms=['conv1', 'in2'],
            tops=[],
            targets=[]
        )
        xgraph.add(X_add)

        X_conv2 = XLayer(
            name='conv2',
            type=['Convolution'],
            bottoms=['in2'],
            tops=['add1'],
            data=ConvData(
                weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                biases=np.array([0., 1.], dtype=np.float32)
            ),
            targets=[]
        )
        xgraph.insert(X_conv2)

        assert(len(xgraph) == 6)
        assert(len(xgraph.get_layer_names()) == 6)
        assert(len(xgraph.get_output_names()) == 1)
        assert(len(xgraph.get_input_names()) == 2)

        xlayers = xgraph.get_layers()
        assert xlayers[0].name == 'in1'
        assert xlayers[0].bottoms == []
        assert xlayers[0].tops == ['pool1']
        assert xlayers[1].name == 'pool1'
        assert xlayers[1].bottoms == ['in1']
        assert xlayers[1].tops == ['conv1']
        assert xlayers[2].name == 'conv1'
        assert xlayers[2].bottoms == ['pool1']
        assert xlayers[2].tops == ['add1']
        assert xlayers[3].name == 'in2'
        assert xlayers[3].bottoms == []
        assert xlayers[3].tops == ['conv2']
        assert xlayers[4].name == 'conv2'
        assert xlayers[4].bottoms == ['in2']
        assert xlayers[4].tops == ['add1']
        assert xlayers[5].name == 'add1'
        assert xlayers[5].bottoms == ['conv1', 'conv2']
        assert xlayers[5].tops == []
Esempio n. 4
0
        def _test_xgraph_insert(
            in_name: str,
            in2_name: str,
            conv_name: str,
            pool_name: str,
            add_name: str,
            conv2_name: str,
        ):

            expected_in_name = px.stringify(in_name)
            expected_in2_name = px.stringify(in2_name)
            expected_conv_name = px.stringify(conv_name)
            expected_pool_name = px.stringify(pool_name)
            expected_add_name = px.stringify(add_name)
            expected_conv2_name = px.stringify(conv2_name)

            in1 = px.ops.input(op_name=in_name, shape=[1, 2, 4, 4])
            W = px.ops.constant(
                "W", np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
            X_conv = px.ops.conv2d(op_name=conv_name,
                                   input_layer=in1,
                                   weights_layer=W,
                                   kernel_size=[2, 2])
            # X_pool = px.ops.pool2d(op_name=pool_name, input_layer=X_conv, )

            xgraph = XGraph()
            xgraph.add(in1)

            assert len(xgraph) == 1
            assert len(xgraph.get_layer_names()) == 1
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            xgraph.add(X_conv)

            assert len(xgraph) == 2
            assert len(xgraph.get_layer_names()) == 2
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            X_pool = XLayer(
                name=pool_name,
                type=["Pooling"],
                bottoms=[in_name],
                tops=[conv_name],
                targets=[],
            )
            xgraph.insert(X_pool)

            assert len(xgraph) == 3
            assert len(xgraph.get_layer_names()) == 3
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            xlayers = xgraph.get_layers()
            assert xlayers[0].name == expected_in_name
            assert xlayers[0].bottoms == []
            assert xlayers[0].tops == [expected_pool_name]
            assert xlayers[1].name == expected_pool_name
            assert xlayers[1].bottoms == [expected_in_name]
            assert xlayers[1].tops == [expected_conv_name]
            assert xlayers[2].name == expected_conv_name
            assert xlayers[2].bottoms == [expected_pool_name]
            assert xlayers[2].tops == []

            X_in2 = px.ops.input(op_name=in2_name, shape=[1, 2, 4, 4])
            xgraph.add(X_in2)

            X_add = XLayer(
                name=add_name,
                type=["Eltwise"],
                bottoms=[conv_name, in2_name],
                tops=[],
                targets=[],
            )
            xgraph.add(X_add)

            X_conv2 = XLayer(
                name=conv2_name,
                type=["Convolution"],
                bottoms=[in2_name],
                tops=[add_name],
                data=ConvData(
                    weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                    biases=np.array([0.0, 1.0], dtype=np.float32),
                ),
                targets=[],
            )
            xgraph.insert(X_conv2)

            assert len(xgraph) == 6
            assert len(xgraph.get_layer_names()) == 6
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 2

            xlayers = xgraph.get_layers()

            assert xlayers[0].name == expected_in_name
            assert xlayers[0].bottoms == []
            assert xlayers[0].tops == [expected_pool_name]
            assert xlayers[1].name == expected_pool_name
            assert xlayers[1].bottoms == [expected_in_name]
            assert xlayers[1].tops == [expected_conv_name]
            assert xlayers[2].name == expected_conv_name
            assert xlayers[2].bottoms == [expected_pool_name]
            assert xlayers[2].tops == [expected_add_name]
            assert xlayers[3].name == expected_in2_name
            assert xlayers[3].bottoms == []
            assert xlayers[3].tops == [expected_conv2_name]
            assert xlayers[4].name == expected_conv2_name
            assert xlayers[4].bottoms == [expected_in2_name]
            assert xlayers[4].tops == [expected_add_name]
            assert xlayers[5].name == expected_add_name
            assert xlayers[5].bottoms == [
                expected_conv_name, expected_conv2_name
            ]
            assert xlayers[5].tops == []