Example #1
0
def test_backend_dtype_exception():
    backend = pytorch_backend.PyTorchBackend(dtype=torch.float32)
    tensor = np.random.rand(2, 2, 2)
    with pytest.raises(TypeError):
        _ = backend.convert_to_tensor(tensor)
Example #2
0
def test_norm():
    backend = pytorch_backend.PyTorchBackend()
    a = backend.convert_to_tensor(np.ones((2, 2)))
    assert backend.norm(a) == 2
Example #3
0
def test_zeros(dtype):
    backend = pytorch_backend.PyTorchBackend(dtype=dtype)
    a = backend.zeros((4, 4))
    np.testing.assert_allclose(torch.zeros((4, 4), dtype=dtype), a)
def test_shape_tuple():
    backend = pytorch_backend.PyTorchBackend()
    a = backend.convert_to_tensor(np.ones([2, 3, 4]))
    actual = backend.shape_tuple(a)
    assert actual == (2, 3, 4)
def test_sqrt():
    backend = pytorch_backend.PyTorchBackend()
    a = backend.convert_to_tensor(np.array([4.0, 9.0]))
    actual = backend.sqrt(a)
    expected = np.array([2, 3])
    np.testing.assert_allclose(expected, actual)
def test_index_update(dtype):
    backend = pytorch_backend.PyTorchBackend()
    tensor = backend.randn((4, 2, 3), dtype=dtype, seed=10)
    out = backend.index_update(tensor, tensor > 0.1, 0.0)
    tensor[tensor > 0.1] = 0.0
    np.testing.assert_allclose(out, tensor)
def test_eigs_not_implemented():
    backend = pytorch_backend.PyTorchBackend()
    with pytest.raises(NotImplementedError):
        backend.eigs(np.ones((2, 2)))
Example #8
0
def test_diagflat(dtype, k):
    backend = pytorch_backend.PyTorchBackend()
    array = backend.randn((16, ), dtype=dtype, seed=10)
    actual = backend.diagflat(array, k=k)
    expected = torch.diag_embed(array, offset=k)
    np.testing.assert_allclose(expected, actual)
Example #9
0
def test_trace_raises():
    shape = tuple([1] * 30)
    backend = pytorch_backend.PyTorchBackend()
    array = backend.randn(shape, seed=10)
    with pytest.raises(ValueError):
        _ = backend.trace(array)
Example #10
0
def test_broadcast_right_multiplication():
    backend = pytorch_backend.PyTorchBackend()
    tensor1 = backend.randn((2, 4, 3), dtype=torch.float64, seed=10)
    tensor2 = backend.randn((3, ), dtype=torch.float64, seed=10)
    out = backend.broadcast_right_multiplication(tensor1, tensor2)
    np.testing.assert_allclose(out, tensor1 * tensor2)
Example #11
0
def test_sparse_shape():
    dtype = torch.float64
    backend = pytorch_backend.PyTorchBackend()
    tensor = backend.randn((2, 3, 4), dtype=dtype, seed=10)
    np.testing.assert_allclose(backend.sparse_shape(tensor), tensor.shape)
Example #12
0
def test_gmres_not_implemented():
    backend = pytorch_backend.PyTorchBackend()
    dummy = backend.zeros(2)
    with pytest.raises(NotImplementedError):
        backend.gmres(lambda x: x, dummy)
def test_gmres_not_implemented():
    backend = pytorch_backend.PyTorchBackend()
    with pytest.raises(NotImplementedError):
        backend.gmres(lambda x: x, np.ones((2)))
Example #14
0
def test_multiply(a, b, expected):
    backend = pytorch_backend.PyTorchBackend()
    tensor1 = backend.convert_to_tensor(a)
    tensor2 = backend.convert_to_tensor(b)

    np.testing.assert_allclose(backend.multiply(tensor1, tensor2), expected)
def test_reshape():
    backend = pytorch_backend.PyTorchBackend()
    a = backend.convert_to_tensor(np.ones((2, 3, 4)))
    actual = backend.shape_tuple(backend.reshape(a, (6, 4, 1)))
    assert actual == (6, 4, 1)
Example #16
0
def test_slice_raises_error():
    backend = pytorch_backend.PyTorchBackend()
    a = backend.convert_to_tensor(
        np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]))
    with pytest.raises(ValueError):
        backend.slice(a, (1, 1), (2, 2, 2))
def test_random_uniform_behavior():
    backend = pytorch_backend.PyTorchBackend()
    a = backend.random_uniform((4, 4), seed=10)
    torch.manual_seed(10)
    b = torch.empty((4, 4), dtype=torch.float64).uniform_()
    torch.allclose(a, b)
def test_ones(dtype):
    backend = pytorch_backend.PyTorchBackend()
    a = backend.ones((4, 4), dtype=dtype)
    np.testing.assert_allclose(torch.ones((4, 4), dtype=dtype), a)
def test_matrix_inv_raises(dtype):
    backend = pytorch_backend.PyTorchBackend()
    matrix = backend.randn((4, 4, 4), dtype=dtype, seed=10)
    with pytest.raises(ValueError):
        backend.inv(matrix)
def test_eye_dtype(dtype):
    backend = pytorch_backend.PyTorchBackend()
    a = backend.eye(N=4, M=4, dtype=dtype)
    assert a.dtype == dtype
def test_eigsh_lanczos_raises_error_for_incompatible_shapes():
    backend = pytorch_backend.PyTorchBackend()
    A = backend.randn((4, 4), dtype=torch.float64)
    init = backend.randn((3, ), dtype=torch.float64)
    with pytest.raises(ValueError):
        backend.eigsh_lanczos(A, initial_state=init)
def test_zeros_dtype(dtype):
    backend = pytorch_backend.PyTorchBackend()
    a = backend.zeros((4, 4), dtype=dtype)
    assert a.dtype == dtype
def test_shape_prod():
    backend = pytorch_backend.PyTorchBackend()
    a = backend.convert_to_tensor(2 * np.ones([1, 2, 3, 4]))
    actual = np.array(backend.shape_prod(a))
    assert actual == 2**24
def test_random_uniform_dtype(dtype):
    backend = pytorch_backend.PyTorchBackend()
    a = backend.random_uniform((4, 4), dtype=dtype)
    assert a.dtype == dtype
def test_diag():
    backend = pytorch_backend.PyTorchBackend()
    b = backend.convert_to_tensor(np.array([1.0, 2.0, 3.0]))
    actual = backend.diag(b)
    expected = np.array([[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]])
    np.testing.assert_allclose(expected, actual)
def test_randn_seed(dtype):
    backend = pytorch_backend.PyTorchBackend()
    a = backend.randn((4, 4), seed=10, dtype=dtype)
    b = backend.randn((4, 4), seed=10, dtype=dtype)
    np.testing.assert_allclose(a, b)
Example #27
0
def test_eye(dtype):
    backend = pytorch_backend.PyTorchBackend(dtype=dtype)
    a = backend.eye(N=4, M=5)
    np.testing.assert_allclose(torch.eye(n=4, m=5, dtype=dtype), a)
def test_random_uniform_seed(dtype):
    backend = pytorch_backend.PyTorchBackend()
    a = backend.random_uniform((4, 4), seed=10, dtype=dtype)
    b = backend.random_uniform((4, 4), seed=10, dtype=dtype)
    torch.allclose(a, b)
Example #29
0
def test_randn(dtype):
    backend = pytorch_backend.PyTorchBackend(dtype=dtype)
    a = backend.randn((4, 4))
    assert a.shape == (4, 4)
Example #30
0
def test_randn_dtype_2(dtype):
    backend = pytorch_backend.PyTorchBackend(dtype=dtype)
    a = backend.randn((4, 4))
    assert a.dtype == dtype