test_indexing.py 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
import numpy as np

import megengine.functional as F
import megengine.jit as jit
import megengine.tensor as tensor
from megengine.autodiff.grad_manager import GradManager


def test_subtensor():
    def tester(ishape, index, dtype=None):
        dtype = dtype or np.float32
        inp = tensor(np.random.randn(*ishape), dtype=dtype)
        oshape = inp[index].shape
        dout = tensor(np.random.randn(*oshape), dtype=dtype)

        gm = GradManager()

        @jit.trace(without_host=True, capture_as_const=True, use_xla=True)
        def func(inp, dout):
            gm.attach([inp])
            with gm:
                out = inp[index]
                gm.backward(out, dout)
            return out, inp.grad

        mge_rsts = func(inp, dout)
        xla_rsts = func(inp, dout)

        for mge_rst, xla_rst in zip(mge_rsts, xla_rsts):
            np.testing.assert_allclose(mge_rst.numpy(), xla_rst.numpy(), atol=1e-5)

    tester(
        (16, 32, 64, 128), (10, slice(3, 13, 1), slice(-12, -3, 2), slice(None, 13, 3),)
    )
    tester(
        (16, 32, 64, 128),
        (slice(3, None, 1), slice(5, None, 3), slice(None, 13, 1), slice(None, 18, 4),),
    )
    tester(
        (16, 32, 64, 128),
        (slice(None, None, 1), None, slice(None, None, 5), slice(-12, -3, 1),),
    )
    tester(
        (16, 32, 1, 128),
        (slice(-12, -3, 2), slice(-13, None, 1), 0, slice(-12, None, 3),),
    )
    tester(
        (16, 32, 64, 128),
        (slice(None, -4, 1), 18, slice(None, -3, 4), slice(None, -3, 1),),
    )
    tester((16, 32, 64, 128), 10)
    tester((16, 32, 64, 128), None)
    tester((16, 32, 64, 128), (slice(3, None, 1), None, slice(-12, -3, 2),))


def test_setsubtensor():
    def tester(x_shape, y_shape, indices, dtype=None):
        dtype = dtype or np.float32
        x = tensor(np.random.randn(*x_shape), dtype=dtype)
        y = tensor(np.random.randn(*y_shape), dtype=dtype)

        @jit.trace(without_host=True, use_xla=True)
        def func(x, y):
            x.__setitem__(indices, y)
            return x

        mge_rsts = func(x, y)
        xla_rsts = func(x, y)

        for mge_rst, xla_rst in zip(mge_rsts, xla_rsts):
            np.testing.assert_allclose(mge_rst.numpy(), xla_rst.numpy(), atol=1e-5)

    tester((32, 16, 8), (16, 8), (11,))
    tester((32, 16, 8), (16, 8), (11,))
    tester((32, 16, 8), (1, 8), (11,))
    tester((32, 16, 8), (8,), (11,))
    tester((32, 16, 8), (1,), (11,))
    tester((32, 16, 8), (14, 16, 8), (slice(2, 16, 1),))
    tester((32, 16, 8), (7, 16, 8), (slice(2, 16, 2),))
    tester((32, 16, 8), (16, 8), (slice(2, 16, 1),))
    tester((32, 16, 8), (16, 8), (slice(2, 16, 2),))
    tester((32, 16, 8), (1, 8), (slice(2, 16, 1),))
    tester((32, 16, 8), (1, 8), (slice(2, 16, 2),))
    tester((32, 16, 8), (8,), (slice(2, 16, 1),))
    tester((32, 16, 8), (8,), (slice(2, 16, 2),))
    tester((32, 16, 8), (1,), (slice(2, 16, 1),))
    tester((32, 16, 8), (1,), (slice(2, 16, 2),))
    tester((32, 16, 8), (8, 10, 8), (slice(4, 26, 3), slice(2, 12, 1),))
    tester((32, 16, 8), (1, 10, 8), (slice(4, 26, 3), slice(2, 12, 1),))
    tester((32, 16, 8), (10, 8), (slice(4, 26, 3), slice(2, 12, 1),))
    tester((32, 16, 8), (1, 8), (slice(4, 26, 3), slice(2, 12, 1),))
    tester((32, 16, 8), (8,), (slice(4, 26, 3), slice(2, 12, 1),))
    tester((32, 16, 8), (1,), (slice(4, 26, 3), slice(2, 12, 1),))
    tester((32, 16, 8), (10, 8), (10, slice(2, 12, 1),))
    tester((32, 16, 8), (1, 8), (10, slice(2, 12, 1),))
    tester((32, 16, 8), (8,), (10, slice(2, 12, 1),))
    tester((32, 16, 8), (1,), (10, slice(2, 12, 1),))
    tester((32, 16, 8), (1, 10, 16, 8), (None, slice(2, 12, 1),))
    tester((32, 16, 8), (1, 1, 16, 8), (None, slice(2, 12, 1),))
    tester((32, 16, 8), (10, 16, 8), (None, slice(2, 12, 1),))
    tester((32, 16, 8), (1, 16, 8), (None, slice(2, 12, 1),))
    tester((32, 16, 8), (8,), (None, slice(2, 12, 1),))


def test_indexing_one_hot():
    def tester(ishape, axis, keepdims, dtype=None):
        dtype = dtype or np.float32
        x = tensor(np.random.randn(*ishape), dtype=dtype)
        nr_class = ishape[axis]
        idx_shape = list(ishape)
        del idx_shape[axis]
        index = tensor(np.random.randint(0, nr_class, idx_shape), dtype="int32")
        oshape = F.nn.indexing_one_hot(x, index, axis, keepdims=keepdims).shape
        dy = tensor(np.random.randn(*oshape), dtype=dtype)

        gm = GradManager()

        # only capture_as_const is True, this function can be trace successfully
        @jit.trace(without_host=True, capture_as_const=True, use_xla=True)
        def func(x, index, dy):
            gm.attach([x])
            with gm:
                y = F.nn.indexing_one_hot(x, index, axis, keepdims=keepdims)
                gm.backward(y, dy)
            return y, x.grad

        mge_rsts = func(x, index, dy)
        xla_rsts = func(x, index, dy)

        for mge_rst, xla_rst in zip(mge_rsts, xla_rsts):
            np.testing.assert_allclose(mge_rst.numpy(), xla_rst.numpy(), atol=1e-5)

    tester((4, 8, 16), 0, True)
    tester((4, 8, 16), 0, False)
    tester((4, 8, 16), 1, True)
    tester((4, 8, 16), 1, False)
    tester((4, 8, 16), -1, True)
    tester((4, 8, 16), -1, False)
    tester((4, 1, 16), -2, True)
    tester((4, 1, 16), -2, False)


if __name__ == "__main__":
    test_subtensor()
    test_setsubtensor()
    test_indexing_one_hot()