test_jit_setitem.py 4.6 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np

import paddle


class TestSetItemBase(unittest.TestCase):
    def setUp(self) -> None:
        pass

    def init_data(self):
        paddle.seed(2023)
        x = paddle.randn([4, 8, 16, 32])
        x.stop_gradient = False
        return x

    def init_func(self):
        def foo(x):
            y = x + 1
            y[:, 2] = x[:, 2] + 99
            return y

        return foo

    def test_case(self):
        func = self.init_func()
        dy_res = self.run_dygrah(func)
        st_res = self.run_to_static(func)

        for dy_out, st_out in zip(dy_res, st_res):
            np.testing.assert_allclose(dy_out.numpy(), st_out.numpy())

    def run_dygrah(self, func):
        x = self.init_data()
        y = func(x)
        x_grad = paddle.grad(y, x)[0]
        return y, x_grad

    def run_to_static(self, func):
        func = paddle.jit.to_static(func)
        return self.run_dygrah(func)


class TestCase1(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[2] = x[2] + 99  # (2, )
            return y

        return foo


class TestCase2(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[:] = x[:] + 99  # slice(None,None,None)
            return y

        return foo


class TestCase3(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[1::2] = x[1::2] + 99  # slice(1,None,2)
            return y

        return foo


class TestCase4(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[1, 2] = x[1, 2] + 99  # (1, 2)
            return y

        return foo


class TestCase5(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[[1, 2], [2, 3]] = x[[1, 2], [2, 3]] + 99  # ([1,2],[2,3])
            return y

        return foo


class TestCase6(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[1, :, 3] = x[1, :, 3] + 99  # slice(None,None,None),3)
            return y

        return foo


class TestCase7(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[1, ..., 2] = x[1, ..., 2] + 99  # (1, ..., 2)
            return y

        return foo


class TestCase8(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            index = paddle.to_tensor([1, 2], dtype="int64")
            y[index] = x[index] + 99  # Tensor([1,2])
            return y

        return foo


class TestCase9(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            one = paddle.to_tensor(1, dtype="int64")
            two = paddle.to_tensor(2, dtype="int64")
            y[one, :, :, 2] = x[1, :, :, two] + 100  # Tensor(1), Tensor(2)
            return y

        return foo


class TestCase10(TestSetItemBase):
    def init_func(self):
        def foo(x):
            y = x + 1
            y[..., 4:6] = y[..., 4:6] * 10000
            return y

        return foo


class TestCase11(TestSetItemBase):
    # Test gradient of value tensor
    def init_func(self):
        def foo(x, value):
            y = x + 1
            y[2, 4] = value
            return y

        return foo

    def run_dygrah(self, func):
        x = self.init_data()
        value = paddle.ones((16, 32))
        value.stop_gradient = False
        y = func(x, value)
        x_grad, value_grad = paddle.grad(y, [x, value])
        return y, x_grad, value_grad


181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
class TestCase12(TestSetItemBase):
    # Test combind-indexing
    def init_func(self):
        def foo(x, value):
            y = x + 1
            y[[0, 1], 1, :2] = value
            return y

        return foo

    def run_dygrah(self, func):
        x = self.init_data()
        value = paddle.ones((32,))
        value.stop_gradient = False
        y = func(x, value)
        x_grad, value_grad = paddle.grad(y, [x, value])
        return y, x_grad, value_grad


200 201
if __name__ == '__main__':
    unittest.main()