Exemple #1
0
def test_utils_astensor1d(is_varnode):
    if is_varnode:
        network = Network()
    else:
        network = None

    reference = make_tensor(0, network)

    # literal
    x = [1, 2, 3]
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert isinstance(xx, type(reference))
        np.testing.assert_equal(xx.numpy(), x)

    # numpy array
    x = np.asarray([1, 2, 3], dtype="int32")
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert isinstance(xx, type(reference))
        np.testing.assert_equal(xx.numpy(), x.astype(dtype) if dtype else x)

    # tensor
    x = make_tensor([1, 2, 3], network)
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert isinstance(xx, type(reference))
        np.testing.assert_equal(xx.numpy(), x.numpy())

    # mixed
    x = [1, make_tensor(2, network), 3]
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert isinstance(xx, type(reference))
        np.testing.assert_equal(xx.numpy(), [1, 2, 3])
Exemple #2
0
def test_utils_astensor1d():
    reference = tensor(0)

    # literal
    x = [1, 2, 3]
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert type(xx) is tensor
        np.testing.assert_equal(xx.numpy(), x)

    # numpy array
    x = np.asarray([1, 2, 3], dtype="int32")
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert type(xx) is tensor
        np.testing.assert_equal(xx.numpy(), x.astype(dtype) if dtype else x)

    # tensor
    x = tensor([1, 2, 3], dtype="int32")
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert type(xx) is tensor
        np.testing.assert_equal(xx.numpy(), x.numpy())

    # mixed
    x = [1, tensor(2), 3]
    for dtype in [None, "float32"]:
        xx = astensor1d(x, reference, dtype=dtype)
        assert type(xx) is tensor
        np.testing.assert_equal(xx.numpy(), [1, 2, 3])
Exemple #3
0
 def forward(self, input):
     A = input.shape[0]
     shape = astensor1d((A, A),
                        self.data,
                        dtype="int32",
                        device=input.device)
     x = F.reshape(self.data, shape)
     o = input + x
     return o
Exemple #4
0
def test_reshape_shape_inference(is_varnode):
    if is_varnode:
        network = Network()
        saved_symbolic_shape = set_symbolic_shape(False)
    else:
        network = None

    x_shape_known = make_tensor([1, 2, 3, 4], network)
    x_shape_unknown = F.broadcast_to(make_tensor([1.0], network),
                                     shape=make_tensor([1, 1, 1, 1],
                                                       network).sum())
    tshp_unknown = astensor1d(
        (make_tensor([2], network), make_tensor([2], network)), x_shape_known)
    tshp_known = astensor1d((2, 2), x_shape_known)
    tshp_known_unspec = astensor1d((2, -1), x_shape_known)

    def check_shape(output, target):
        source = output.shape
        if isinstance(source, tensor):
            source = source.numpy()
        np.testing.assert_equal(source, target.shape)

    def func(x, target_shape):
        return x.reshape(target_shape)

    cases = [
        {
            "input": [x_shape_known, tshp_unknown],
            "output": [
                np.zeros((2, 2)),
            ]
        },
        {
            "input": [x_shape_unknown, tshp_unknown],
            "output": [
                np.zeros((2, 2)),
            ]
        },
        {
            "input": [x_shape_known, tshp_known],
            "output": [
                np.zeros((2, 2)),
            ]
        },
        {
            "input": [x_shape_known, tshp_known_unspec],
            "output": [
                np.zeros((2, 2)),
            ]
        },
        {
            "input": [x_shape_unknown, tshp_known],
            "output": [
                np.zeros((2, 2)),
            ]
        },
        {
            "input": [x_shape_unknown, tshp_known_unspec],
            "output": [
                np.zeros((2, 2)),
            ]
        },
    ]
    opr_test(cases,
             func,
             compare_fn=check_shape,
             test_trace=True,
             network=network)
    if is_varnode:
        set_symbolic_shape(saved_symbolic_shape)