def test_xgraph_add_remove(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) xgraph.remove(X_conv.name) 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)
def _test_add_remove(in_name: str, conv_name: str): 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]) 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 xgraph.remove(X_conv.name) 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
def test_xgraph_add_get(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 assert isinstance(xgraph.get('in1'), XLayer) assert xgraph.get('in1').bottoms == [] assert xgraph.get('in1').tops == [] 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 xgraph.get_layer_names() == ['in1', 'conv1'] assert xgraph.get_output_names() == ['conv1'] assert xgraph.get_input_names() == ['in1'] assert xgraph.get('in1').tops == ['conv1'] assert isinstance(xgraph.get('conv1'), XLayer) assert xgraph.get('conv1').bottoms == ['in1'] assert xgraph.get('conv1').tops == [] assert xgraph.get('conv1').type == ['Convolution'] np.testing.assert_array_equal( xgraph.get('conv1').data.weights, np.array([[[[1, 2], [3, 4]]]], dtype=np.float32) ) np.testing.assert_array_equal( xgraph.get('conv1').data.biases, np.array([0., 1.], dtype=np.float32) ) xgraph.get('conv1').data = ConvData( weights=xgraph.get('conv1').data.weights * 2, biases=xgraph.get('conv1').data.biases ) np.testing.assert_array_equal( xgraph.get('conv1').data.weights, np.array([[[[2, 4], [6, 8]]]], dtype=np.float32) ) xgraph.remove(X_conv.name) assert len(xgraph) == 1 assert 'in1' in xgraph assert len(xgraph.get_layer_names()) == 1 assert len(xgraph.get_output_names()) == 1 assert len(xgraph.get_input_names()) == 1
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'])
def _test_add_get(in_name: str, conv_name: str): expected_in_name = px.stringify(in_name) expected_conv_name = px.stringify(conv_name) in1 = px.ops.input(op_name=expected_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]) 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 assert isinstance(xgraph.get(in_name), XLayer) assert xgraph.get(in_name).bottoms == [] assert xgraph.get(in_name).tops == [] xgraph.add(X_conv) assert len(xgraph) == 2 assert xgraph.get_layer_names() == [ expected_in_name, expected_conv_name ] assert xgraph.get_output_names() == [expected_conv_name] assert xgraph.get_input_names() == [expected_in_name] assert xgraph.get(in_name).tops == [expected_conv_name] assert isinstance(xgraph.get(conv_name), XLayer) assert xgraph.get(conv_name).bottoms == [expected_in_name] assert xgraph.get(conv_name).tops == [] assert xgraph.get(conv_name).type == ["Convolution"] np.testing.assert_array_equal( xgraph.get(conv_name).data.weights, np.array([[[[1, 2], [3, 4]]]], dtype=np.float32), ) np.testing.assert_array_equal( xgraph.get(conv_name).data.biases, np.array([0.0], dtype=np.float32), ) xgraph.get(conv_name).data = ConvData( weights=xgraph.get(conv_name).data.weights * 2, biases=xgraph.get(conv_name).data.biases, ) np.testing.assert_array_equal( xgraph.get(conv_name).data.weights, np.array([[[[2, 4], [6, 8]]]], dtype=np.float32), ) xgraph.remove(X_conv.name) assert len(xgraph) == 1 assert in_name in xgraph assert len(xgraph.get_layer_names()) == 1 assert len(xgraph.get_output_names()) == 1 assert len(xgraph.get_input_names()) == 1
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"])