Ejemplo n.º 1
0
    def testMatmulExecution(self):
        data_a = np.random.randn(10, 20)
        data_b = np.random.randn(20)

        a = tensor(data_a, chunk_size=2)
        b = tensor(data_b, chunk_size=3)
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_a, data_b)
        np.testing.assert_allclose(res, expected)

        data_a = np.random.randn(10, 20)
        data_b = np.random.randn(10)

        a = tensor(data_a, chunk_size=2)
        b = tensor(data_b, chunk_size=3)
        c = matmul(b, a)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_b, data_a)
        np.testing.assert_allclose(res, expected)

        data_a = np.random.randn(15, 1, 20, 30)
        data_b = np.random.randn(1, 11, 30, 20)

        a = tensor(data_a, chunk_size=12)
        b = tensor(data_b, chunk_size=13)
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_a, data_b)
        np.testing.assert_allclose(res, expected, atol=.0001)

        a = arange(2 * 2 * 4, chunk_size=1).reshape((2, 2, 4))
        b = arange(2 * 2 * 4, chunk_size=1).reshape((2, 4, 2))
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(np.arange(2 * 2 * 4).reshape(2, 2, 4),
                             np.arange(2 * 2 * 4).reshape(2, 4, 2))
        np.testing.assert_allclose(res, expected, atol=.0001)

        data_a = sps.random(10, 20)
        data_b = sps.random(20, 5)

        a = tensor(data_a, chunk_size=2)
        b = tensor(data_b, chunk_size=3)
        c = matmul(a, b)

        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.matmul(data_a.toarray(), data_b.toarray())
        np.testing.assert_allclose(res.toarray(), expected)
Ejemplo n.º 2
0
    def testMatmul(self):
        t1 = tensor([[0, 1, 0], [1, 0, 0]], chunk_size=2).tosparse()
        t2 = t1.T

        t3 = matmul(t1, t2, out=empty((2, 2), dtype=t1.dtype, order='F'))
        self.assertEqual(t3.order.value, 'F')

        with self.assertRaises(TypeError):
            matmul(t1, t2, out=1)

        with self.assertRaises(TypeError):
            matmul(t1, t2, out=empty((2, 2), dtype='?'))

        with self.assertRaises(ValueError):
            matmul(t1, t2, out=empty((3, 2), dtype=t1.dtype))

        raw1 = np.asfortranarray(np.random.rand(3, 3))
        raw2 = np.asfortranarray(np.random.rand(3, 3))
        raw3 = np.random.rand(3, 3)

        self.assertEqual(matmul(tensor(raw1), tensor(raw2)).flags['C_CONTIGUOUS'],
                         np.matmul(raw1, raw2).flags['C_CONTIGUOUS'])
        self.assertEqual(matmul(tensor(raw1), tensor(raw2)).flags['F_CONTIGUOUS'],
                         np.matmul(raw1, raw2).flags['F_CONTIGUOUS'])

        self.assertEqual(matmul(tensor(raw1), tensor(raw2), order='A').flags['C_CONTIGUOUS'],
                         np.matmul(raw1, raw2, order='A').flags['C_CONTIGUOUS'])
        self.assertEqual(matmul(tensor(raw1), tensor(raw2), order='A').flags['F_CONTIGUOUS'],
                         np.matmul(raw1, raw2, order='A').flags['F_CONTIGUOUS'])

        self.assertEqual(matmul(tensor(raw1), tensor(raw3), order='A').flags['C_CONTIGUOUS'],
                         np.matmul(raw1, raw3, order='A').flags['C_CONTIGUOUS'])
        self.assertEqual(matmul(tensor(raw1), tensor(raw3), order='A').flags['F_CONTIGUOUS'],
                         np.matmul(raw1, raw3, order='A').flags['F_CONTIGUOUS'])
Ejemplo n.º 3
0
    def testMatmul(self):
        a_data = [[1, 0], [0, 1]]
        b_data = [[4, 1], [2, 2]]

        a = tensor(a_data, chunk_size=1)
        b = tensor(b_data, chunk_size=1)

        t = matmul(a, b)

        self.assertEqual(t.shape, (2, 2))
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))

        b_data = [1, 2]
        b = tensor(b_data, chunk_size=1)

        t = matmul(a, b)

        self.assertEqual(t.shape, (2, ))
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))

        t = matmul(b, a)

        self.assertEqual(t.shape, (2, ))
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))

        a_data = np.arange(2 * 2 * 4).reshape((2, 2, 4))
        b_data = np.arange(2 * 2 * 4).reshape((2, 4, 2))

        a = tensor(a_data, chunk_size=1)
        b = tensor(b_data, chunk_size=1)

        t = matmul(a, b)

        self.assertEqual(t.shape, (2, 2, 2))
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))

        t = matmul(tensor([2j, 3j], chunk_size=1),
                   tensor([2j, 3j], chunk_size=1))

        self.assertEqual(t.shape, ())
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))

        with self.assertRaises(ValueError):
            matmul([1, 2], 3)

        with self.assertRaises(ValueError):
            matmul(np.random.randn(2, 3, 4), np.random.randn(3, 4, 3))

        t = matmul(tensor(np.random.randn(2, 3, 4), chunk_size=2),
                   tensor(np.random.randn(3, 1, 4, 3), chunk_size=3))
        self.assertEqual(t.shape, (3, 2, 3, 3))

        v = ones((100, 100), chunk_size=10)
        tv = matmul(v, v)
        self.assertEqual(tv.shape, (100, 100))
        tv.tiles()
        self.assertEqual(tv.shape, tuple(sum(s) for s in tv.nsplits))
Ejemplo n.º 4
0
def test_matmul():
    t1 = tensor([[0, 1, 0], [1, 0, 0]], chunk_size=2).tosparse()
    t2 = t1.T

    t3 = matmul(t1, t2, out=empty((2, 2), dtype=t1.dtype, order='F'))
    assert t3.order.value == 'F'

    with pytest.raises(TypeError):
        matmul(t1, t2, out=1)

    with pytest.raises(TypeError):
        matmul(t1, t2, out=empty((2, 2), dtype='?'))

    with pytest.raises(ValueError):
        matmul(t1, t2, out=empty((3, 2), dtype=t1.dtype))

    raw1 = np.asfortranarray(np.random.rand(3, 3))
    raw2 = np.asfortranarray(np.random.rand(3, 3))
    raw3 = np.random.rand(3, 3)

    assert matmul(tensor(raw1), tensor(raw2)).flags['C_CONTIGUOUS'] == \
           np.matmul(raw1, raw2).flags['C_CONTIGUOUS']
    assert matmul(tensor(raw1), tensor(raw2)).flags['F_CONTIGUOUS'] == \
           np.matmul(raw1, raw2).flags['F_CONTIGUOUS']

    assert matmul(tensor(raw1), tensor(raw2), order='A').flags['C_CONTIGUOUS'] == \
           np.matmul(raw1, raw2, order='A').flags['C_CONTIGUOUS']
    assert matmul(tensor(raw1), tensor(raw2), order='A').flags['F_CONTIGUOUS'] == \
           np.matmul(raw1, raw2, order='A').flags['F_CONTIGUOUS']

    assert matmul(tensor(raw1), tensor(raw3), order='A').flags['C_CONTIGUOUS'] == \
           np.matmul(raw1, raw3, order='A').flags['C_CONTIGUOUS']
    assert matmul(tensor(raw1), tensor(raw3), order='A').flags['F_CONTIGUOUS'] == \
           np.matmul(raw1, raw3, order='A').flags['F_CONTIGUOUS']
Ejemplo n.º 5
0
def test_matmul_execution(setup):
    rs = np.random.RandomState(0)

    data_a = rs.randn(10, 20)
    data_b = rs.randn(20)

    a = tensor(data_a, chunk_size=5)
    b = tensor(data_b, chunk_size=6)
    c = matmul(a, b)

    res = c.execute().fetch()
    expected = np.matmul(data_a, data_b)
    np.testing.assert_allclose(res, expected)

    data_a = rs.randn(10, 20)
    data_b = rs.randn(10)

    a = tensor(data_a, chunk_size=5)
    b = tensor(data_b, chunk_size=6)
    c = matmul(b, a)

    res = c.execute().fetch()
    expected = np.matmul(data_b, data_a)
    np.testing.assert_allclose(res, expected)

    data_a = rs.randn(15, 1, 20, 30)
    data_b = rs.randn(1, 11, 30, 20)

    a = tensor(data_a, chunk_size=12)
    b = tensor(data_b, chunk_size=13)
    c = matmul(a, b)

    res = c.execute().fetch()
    expected = np.matmul(data_a, data_b)
    np.testing.assert_allclose(res, expected, atol=.0001)

    a = arange(2 * 2 * 4, chunk_size=1).reshape((2, 2, 4))
    b = arange(2 * 2 * 4, chunk_size=1).reshape((2, 4, 2))
    c = matmul(a, b)

    res = c.execute().fetch()
    expected = np.matmul(
        np.arange(2 * 2 * 4).reshape(2, 2, 4),
        np.arange(2 * 2 * 4).reshape(2, 4, 2))
    np.testing.assert_allclose(res, expected, atol=.0001)

    data_a = sps.random(10, 20)
    data_b = sps.random(20, 5)

    a = tensor(data_a, chunk_size=5)
    b = tensor(data_b, chunk_size=6)
    c = matmul(a, b)

    res = c.execute().fetch()
    expected = np.matmul(data_a.toarray(), data_b.toarray())
    np.testing.assert_allclose(res.toarray(), expected)

    # test order
    data_a = np.asfortranarray(rs.randn(10, 20))
    data_b = np.asfortranarray(rs.randn(20, 30))

    a = tensor(data_a, chunk_size=12)
    b = tensor(data_b, chunk_size=13)

    c = matmul(a, b)
    res = c.execute().fetch()
    expected = np.matmul(data_a, data_b)

    np.testing.assert_allclose(res, expected)
    assert res.flags['C_CONTIGUOUS'] == expected.flags['C_CONTIGUOUS']
    assert res.flags['F_CONTIGUOUS'] == expected.flags['F_CONTIGUOUS']

    c = matmul(a, b, order='A')
    res = c.execute().fetch()
    expected = np.matmul(data_a, data_b, order='A')

    np.testing.assert_allclose(res, expected)
    assert res.flags['C_CONTIGUOUS'] == expected.flags['C_CONTIGUOUS']
    assert res.flags['F_CONTIGUOUS'] == expected.flags['F_CONTIGUOUS']

    c = matmul(a, b, order='C')
    res = c.execute().fetch()
    expected = np.matmul(data_a, data_b, order='C')

    np.testing.assert_allclose(res, expected)
    assert res.flags['C_CONTIGUOUS'] == expected.flags['C_CONTIGUOUS']
    assert res.flags['F_CONTIGUOUS'] == expected.flags['F_CONTIGUOUS']
Ejemplo n.º 6
0
def test_matmul():
    a_data = [[1, 0], [0, 1]]
    b_data = [[4, 1], [2, 2]]

    a = tensor(a_data, chunk_size=1)
    b = tensor(b_data, chunk_size=1)

    t = matmul(a, b)

    assert t.shape == (2, 2)
    t = tile(t)
    assert t.shape == tuple(sum(s) for s in t.nsplits)

    b_data = [1, 2]
    b = tensor(b_data, chunk_size=1)

    t = matmul(a, b)

    assert t.shape == (2, )
    t = tile(t)
    assert t.shape == tuple(sum(s) for s in t.nsplits)

    t = matmul(b, a)

    assert t.shape == (2, )
    t = tile(t)
    assert t.shape == tuple(sum(s) for s in t.nsplits)

    a_data = np.arange(2 * 2 * 4).reshape((2, 2, 4))
    b_data = np.arange(2 * 2 * 4).reshape((2, 4, 2))

    a = tensor(a_data, chunk_size=1)
    b = tensor(b_data, chunk_size=1)

    t = matmul(a, b)

    assert t.shape == (2, 2, 2)
    t = tile(t)
    assert t.shape == tuple(sum(s) for s in t.nsplits)

    t = matmul(tensor([2j, 3j], chunk_size=1), tensor([2j, 3j], chunk_size=1))

    assert t.shape == ()
    t = tile(t)
    assert t.shape == tuple(sum(s) for s in t.nsplits)

    with pytest.raises(ValueError):
        matmul([1, 2], 3)

    with pytest.raises(ValueError):
        matmul(np.random.randn(2, 3, 4), np.random.randn(3, 4, 3))

    t = matmul(tensor(np.random.randn(2, 3, 4), chunk_size=2),
               tensor(np.random.randn(3, 1, 4, 3), chunk_size=3))
    assert t.shape == (3, 2, 3, 3)

    v = ones((100, 100), chunk_size=10)
    tv = matmul(v, v)
    assert tv.shape == (100, 100)
    tv = tile(tv)
    assert tv.shape == tuple(sum(s) for s in tv.nsplits)