Exemple #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)
Exemple #2
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))
        self.assertEqual(calc_shape(t), t.shape)
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

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

        t = matmul(a, b)

        self.assertEqual(t.shape, (2, ))
        self.assertEqual(calc_shape(t), t.shape)
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

        t = matmul(b, a)

        self.assertEqual(t.shape, (2, ))
        self.assertEqual(calc_shape(t), t.shape)
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

        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))
        self.assertEqual(calc_shape(t), t.shape)
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

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

        self.assertEqual(t.shape, ())
        self.assertEqual(calc_shape(t), t.shape)
        t.tiles()
        self.assertEqual(t.shape, tuple(sum(s) for s in t.nsplits))
        self.assertEqual(calc_shape(t.chunks[0]), t.chunks[0].shape)

        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))
        self.assertEqual(calc_shape(tv), tv.shape)
        tv.tiles()
        self.assertEqual(tv.shape, tuple(sum(s) for s in tv.nsplits))
        self.assertEqual(calc_shape(tv.chunks[0]), tv.chunks[0].shape)