def testWithGivenSession(session): from mars.tensor.core import mutable_tensor # simple dtype. mut1 = mutable_tensor("test", (4, 5), dtype='double', fill_value=123.456, chunk_size=3) mut1[1:4, 2] = 8 mut1[2:4] = np.arange(10).reshape(2, 5) arr1 = mut1.seal() expected = np.full((4, 5), fill_value=123.456, dtype='double') expected[1:4, 2] = 8 expected[2:4] = np.arange(10).reshape(2, 5) np.testing.assert_array_equal(session.fetch(arr1), expected) # structured dtype, but the `fill_value` cannot be tuple (consistent with np.full). dtype = np.dtype([('x', np.int32), ('y', np.double)]) mut2 = mutable_tensor("test", (4, 5), dtype=dtype, fill_value=123.456, chunk_size=3) mut2[1:4, 2] = (1, 2.) mut2[2:4] = np.arange(10).reshape(2, 5) arr2 = mut2.seal() expected = np.full((4, 5), fill_value=123.456, dtype=dtype) expected[1:4, 2] = (1, 2.) expected[2:4] = np.arange(10).reshape(2, 5) np.testing.assert_array_equal(session.fetch(arr2), expected)
def testWithGivenSession(session): from mars.tensor.core import mutable_tensor # simple dtype. mut1 = mutable_tensor("test", (4, ), dtype='<U16', chunk_size=3) mut1[0] = 'a' mut1[1] = 'bb' mut1[2] = 'cccc' mut1[3] = 'dddddddd' arr1 = mut1.seal() expected = np.empty((4, ), dtype='<U16') expected[0] = 'a' expected[1] = 'bb' expected[2] = 'cccc' expected[3] = 'dddddddd' np.testing.assert_array_equal(session.fetch(arr1), expected) # structured array that contains string dtype = np.dtype([('x', np.int32), ('y', '<U16')]) mut2 = mutable_tensor("test", (4, ), dtype=dtype, chunk_size=3) mut2[0] = (0, 'a') mut2[1] = (1, 'bb') mut2[2] = (2, 'cccc') mut2[3] = (3, 'dddddddd') arr2 = mut2.seal() expected = np.empty((4, ), dtype=dtype) expected[0] = (0, 'a') expected[1] = (1, 'bb') expected[2] = (2, 'cccc') expected[3] = (3, 'dddddddd') np.testing.assert_array_equal(session.fetch(arr2), expected)
def testWithGivenSession(session): from mars.tensor.core import mutable_tensor # simple dtype. mut1 = mutable_tensor("test1", (4, 5), dtype='int', chunk_size=3) mut2 = mutable_tensor("test2", (4, 5), dtype='int', chunk_size=3) mut1[:] = 111 mut2[:] = 222 arr1 = mut1.seal() arr2 = mut2.seal() expected1 = np.full((4, 5), 111, dtype='int') expected2 = np.full((4, 5), 222, dtype='int') np.testing.assert_array_equal(session.fetch(arr1), expected1) np.testing.assert_array_equal(session.fetch(arr2), expected2)
def testWithGivenSession(session): from mars.tensor.core import mutable_tensor # cannot get non-existing mutable tensor with self.assertRaises(ValueError): mutable_tensor("test") # should be create mut1 = mutable_tensor("test", (4, 5), dtype='int32', chunk_size=3) # should be get mut2 = mutable_tensor("test") # mut1 should equal to mut2, but are not the same object self.assertEqual(mut1.shape, mut2.shape) self.assertEqual(mut1.dtype, mut2.dtype) # LocalSession return the same MutableTensor instance when `get_mutable_tensor`. if isinstance(session._sess, LocalSession): self.assertTrue(mut1 is mut2) else: self.assertTrue(mut1 is not mut2) mut2[1:4, 2] = 8 mut2[2:4] = np.arange(10).reshape(2, 5) expected = np.zeros((4, 5), dtype='int32') expected[1:4, 2] = 8 expected[2:4] = np.arange(10).reshape(2, 5) # cannot be sealed twice # # Note that we operate on `mut2`, if we seal `mut1`, the result may not be correct. # # When we operate both on `mut1` and `mut2`, the result may not correct since the # two MutableTensor instances both main their own local buffers, but they cannot # be both sealed. arr = mut2.seal() with self.assertRaises(ValueError): mut1.seal() # check value np.testing.assert_array_equal(session.fetch(arr), expected)