def testSparseDotExecution(self): a_data = sps.random(5, 9, density=.1) b_data = sps.random(9, 10, density=.2) a = tensor(a_data, chunk_size=2) b = tensor(b_data, chunk_size=3) c = dot(a, b) res = self.executor.execute_tensor(c, concat=True)[0] self.assertTrue(issparse(res)) np.testing.assert_allclose(res.toarray(), a_data.dot(b_data).toarray()) c2 = dot(a, b, sparse=False) res = self.executor.execute_tensor(c2, concat=True)[0] self.assertFalse(issparse(res)) np.testing.assert_allclose(res, a_data.dot(b_data).toarray()) c3 = tensordot(a, b.T, (-1, -1), sparse=False) res = self.executor.execute_tensor(c3, concat=True)[0] self.assertFalse(issparse(res)) np.testing.assert_allclose(res, a_data.dot(b_data).toarray()) c = inner(a, b.T) res = self.executor.execute_tensor(c, concat=True)[0] self.assertTrue(issparse(res)) np.testing.assert_allclose(res.toarray(), a_data.dot(b_data).toarray()) c = inner(a, b.T, sparse=False) res = self.executor.execute_tensor(c, concat=True)[0] self.assertFalse(issparse(res)) np.testing.assert_allclose(res, a_data.dot(b_data).toarray())
def assertArrayEqual(self, a, b): if issparse(a): a = a.toarray() else: a = np.asarray(a) if issparse(b): b = b.toarray() else: b = np.asarray(b) return self.assertTrue(self._nan_equal(a, b))
def assertArrayEqual(self, a, b, almost=False): if issparse(a): a = a.toarray() else: a = np.asarray(a) if issparse(b): b = b.toarray() else: b = np.asarray(b) if not almost: np.testing.assert_array_equal(a, b) else: np.testing.assert_almost_equal(a, b)
def assertArrayEqual(self, a, b): if issparse(a): a = a.toarray() else: a = np.asarray(a) if issparse(b): b = b.toarray() else: b = np.asarray(b) try: return np.testing.assert_equal(a, b) except AssertionError: return False
def func(*args): new_args = [] for arg in args: if issparse(arg): new_args.append(arg.toarray()) else: new_args.append(arg) return op(*new_args)
def testSparseRandintExecution(self): size_executor = ExecutorForTest(sync_provider_type=ExecutorForTest.SyncProviderType.MOCK) arr = tensor.random.randint(1, 2, size=(30, 50), density=.1, chunk_size=10, dtype='f4') size_res = size_executor.execute_tensor(arr, mock=True) self.assertAlmostEqual(arr.nbytes * 0.1, sum(tp[0] for tp in size_res)) res = self.executor.execute_tensor(arr, concat=True)[0] self.assertTrue(issparse(res)) self.assertEqual(res.shape, (30, 50)) self.assertTrue(np.all(res.data >= 1)) self.assertTrue(np.all(res.data < 2)) self.assertAlmostEqual((res >= 1).toarray().sum(), 30 * 50 * .1, delta=20)
def test_sparse_randint_execution(setup): # size_executor = ExecutorForTest(sync_provider_type=ExecutorForTest.SyncProviderType.MOCK) arr = tensor.random.randint(1, 2, size=(30, 50), density=.1, chunk_size=20, dtype='f4') # size_res = size_executor.execute_tensor(arr, mock=True) # assert pytest.approx(arr.nbytes * 0.1) == sum(tp[0] for tp in size_res) res = arr.execute().fetch() assert issparse(res) is True assert res.shape == (30, 50) np.testing.assert_array_less(res.data, 2) np.testing.assert_array_less(0, res.data) assert pytest.approx((res >= 1).toarray().sum(), 30 * 50 * .1, abs=20)
def testSparseRandintExecution(self): arr = tensor.random.randint(1, 2, size=(30, 50), density=.1, chunk_size=10, dtype='f4') res = self.executor.execute_tensor(arr, concat=True)[0] self.assertTrue(issparse(res)) self.assertEqual(res.shape, (30, 50)) self.assertTrue(np.all(res.data >= 1)) self.assertTrue(np.all(res.data < 2)) self.assertAlmostEqual((res >= 1).toarray().sum(), 30 * 50 * .1, delta=20)