test_jit_setitem.py 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15

16 17 18 19 20
import unittest

import numpy as np

import paddle
21
import paddle.nn.functional as F
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43


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()
C
co63oc 已提交
44
        dy_res = self.run_dygraph(func)
45 46 47 48 49
        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())

C
co63oc 已提交
50
    def run_dygraph(self, func):
51 52 53 54 55 56 57
        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)
C
co63oc 已提交
58
        return self.run_dygraph(func)
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


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

C
co63oc 已提交
174
    def run_dygraph(self, func):
175 176 177 178 179 180 181 182
        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


183
class TestCase12(TestSetItemBase):
184
    # Test gradient of value tensor
185
    def init_func(self):
186 187 188 189 190 191 192
        def foo():
            res = paddle.zeros([4, 3, 2])
            b = paddle.zeros([4, 3, 2])
            v = paddle.to_tensor(1.0)
            for i in range(paddle.shape(b)[0]):
                res[i] = v
            return res
193 194 195

        return foo

C
co63oc 已提交
196
    def run_dygraph(self, func):
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        y = func()
        return (y,)


class TestCase13(TestSetItemBase):
    # Test gradient of value tensor
    def init_func(self):
        def foo():
            res = paddle.zeros([4, 3, 2])
            v = paddle.to_tensor(1.0)
            for i in range(4):
                res[i] = v
            return res

        return foo

    def run_dygraph(self, func):
        y = func()
        return (y,)


class TestCase14(TestSetItemBase):
    # Test gradient of value tensor
    def init_func(self):
        def foo():
            data = np.arange(8).reshape((2, 4)).astype('float32')
            x = paddle.to_tensor(data)
            x[:, 1:] = x[:, :-1].clone()
            x[:, 0] = 1
            res = x.flatten()
            return res

        return foo

    def run_dygraph(self, func):
        y = func()
        return (y,)
234 235


236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
class TestCase15(TestSetItemBase):
    # Test gradient of value tensor
    def init_func(self):
        def foo(x, H, W):
            B, _, _, C = x.shape
            pad_list = paddle.zeros([4], dtype="int32")
            pad_list[3] = H // 2
            pad_list[1] = W // 2

            # 问题在这里,进去F.pad以后,pad_list是初始变量而非赋值后的变量
            # 在修改前,赋值前后的变量是同一个,没有问题
            # 修改后,期望接收赋值后的变量,接收赋值前变量结果是不对的
            x = F.pad(x, pad_list, data_format="NHWC")
            return x

        return foo

    def run_dygraph(self, func):
        # 注释这句看结果diff
        x = paddle.ones((1, 6, 6, 3))
        H = paddle.full([1], 6, dtype='int32')
        W = paddle.full([1], 6, dtype='int32')
        y = func(x, H, W)
        return (y,)


262 263
if __name__ == '__main__':
    unittest.main()