Example #1
0
    def testOnes(self):
        tensor = ones((10, 10, 8), chunk_size=(3, 3, 5))
        tensor = tensor.tiles()
        self.assertEqual(tensor.shape, (10, 10, 8))
        self.assertEqual(len(tensor.chunks), 32)

        tensor = ones((10, 3), chunk_size=(4, 2))
        tensor = tensor.tiles()
        self.assertEqual(tensor.shape, (10, 3))

        chunk = tensor.cix[1, 1]
        self.assertEqual(tensor.get_chunk_slices(chunk.index),
                         (slice(4, 8), slice(2, 3)))

        tensor = ones((10, 5), chunk_size=(2, 3), gpu=True)
        tensor = tensor.tiles()

        self.assertTrue(tensor.op.gpu)
        self.assertTrue(tensor.chunks[0].op.gpu)

        tensor1 = ones((10, 10, 8), chunk_size=(3, 3, 5))
        tensor1 = tensor1.tiles()

        tensor2 = ones((10, 10, 8), chunk_size=(3, 3, 5))
        tensor2 = tensor2.tiles()

        self.assertEqual(tensor1.chunks[0].op.key, tensor2.chunks[0].op.key)
        self.assertEqual(tensor1.chunks[0].key, tensor2.chunks[0].key)
        self.assertNotEqual(tensor1.chunks[0].op.key, tensor1.chunks[1].op.key)
        self.assertNotEqual(tensor1.chunks[0].key, tensor1.chunks[1].key)

        tensor = ones((2, 3, 4))
        self.assertEqual(len(list(tensor)), 2)

        tensor2 = ones((2, 3, 4), chunk_size=1)
        # tensor's op key must be equal to tensor2
        self.assertEqual(tensor.op.key, tensor2.op.key)
        self.assertNotEqual(tensor.key, tensor2.key)

        tensor3 = ones((2, 3, 3))
        self.assertNotEqual(tensor.op.key, tensor3.op.key)
        self.assertNotEqual(tensor.key, tensor3.key)

        # test create chunk op of ones manually
        chunk_op1 = TensorOnes(dtype=tensor.dtype)
        chunk1 = chunk_op1.new_chunk(None, shape=(3, 3), index=(0, 0))
        chunk_op2 = TensorOnes(dtype=tensor.dtype)
        chunk2 = chunk_op2.new_chunk(None, shape=(3, 4), index=(0, 1))
        self.assertNotEqual(chunk1.op.key, chunk2.op.key)
        self.assertNotEqual(chunk1.key, chunk2.key)

        tensor = ones((100, 100), chunk_size=50)
        tensor = tensor.tiles()
        self.assertEqual(len({c.op.key for c in tensor.chunks}), 1)
        self.assertEqual(len({c.key for c in tensor.chunks}), 1)
Example #2
0
def test_ones():
    tensor = ones((10, 10, 8), chunk_size=(3, 3, 5))
    tensor = tile(tensor)
    assert tensor.shape == (10, 10, 8)
    assert len(tensor.chunks) == 32

    tensor = ones((10, 3), chunk_size=(4, 2))
    tensor = tile(tensor)
    assert tensor.shape == (10, 3)

    chunk = tensor.cix[1, 1]
    assert tensor.get_chunk_slices(chunk.index) == (slice(4, 8), slice(2, 3))

    tensor = ones((10, 5), chunk_size=(2, 3), gpu=True)
    tensor = tile(tensor)

    assert tensor.op.gpu is True
    assert tensor.chunks[0].op.gpu is True

    tensor1 = ones((10, 10, 8), chunk_size=(3, 3, 5))
    tensor1 = tile(tensor1)

    tensor2 = ones((10, 10, 8), chunk_size=(3, 3, 5))
    tensor2 = tile(tensor2)

    assert tensor1.chunks[0].op.key == tensor2.chunks[0].op.key
    assert tensor1.chunks[0].key == tensor2.chunks[0].key
    assert tensor1.chunks[0].op.key != tensor1.chunks[1].op.key
    assert tensor1.chunks[0].key != tensor1.chunks[1].key

    tensor = ones((2, 3, 4))
    assert len(list(tensor)) == 2

    tensor2 = ones((2, 3, 4), chunk_size=1)
    # tensor's op key must be equal to tensor2
    assert tensor.op.key == tensor2.op.key
    assert tensor.key != tensor2.key

    tensor3 = ones((2, 3, 3))
    assert tensor.op.key != tensor3.op.key
    assert tensor.key != tensor3.key

    # test create chunk op of ones manually
    chunk_op1 = TensorOnes(dtype=tensor.dtype)
    chunk1 = chunk_op1.new_chunk(None, shape=(3, 3), index=(0, 0))
    chunk_op2 = TensorOnes(dtype=tensor.dtype)
    chunk2 = chunk_op2.new_chunk(None, shape=(3, 4), index=(0, 1))
    assert chunk1.op.key != chunk2.op.key
    assert chunk1.key != chunk2.key

    tensor = ones((100, 100), chunk_size=50)
    tensor = tile(tensor)
    assert len({c.op.key for c in tensor.chunks}) == 1
    assert len({c.key for c in tensor.chunks}) == 1