test_transform.py 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2022 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 paddle
18
from paddle.incubate.autograd.primx import Transform, orig2prim, prim2orig
19 20 21 22 23 24 25 26 27

paddle.enable_static()


class TestAutoGradTransformForAdd(unittest.TestCase):
    def setUp(self):
        self.main_program = paddle.static.Program()
        self.startup_program = paddle.static.Program()

28 29 30
        with paddle.static.program_guard(
            self.main_program, self.startup_program
        ):
31 32 33 34 35 36 37
            self.init_data()

    def init_data(self):
        # { input_index: input_shape }
        self.xs_shape_map = {0: (20, 40), 1: (20, 40)}
        # { output_index: output_shape }
        self.ys_shape_map = {0: (20, 40)}
38 39 40
        X0 = paddle.static.data(
            name='X0', shape=self.xs_shape_map[0], dtype='float32'
        )
41
        X0.stop_gradient = False
42 43 44
        X1 = paddle.static.data(
            name='X1', shape=self.xs_shape_map[1], dtype='float32'
        )
45 46 47 48
        X1.stop_gradient = False

        A = paddle.tanh(X0)
        B = paddle.tanh(X1)
J
Jiabin Yang 已提交
49 50
        C = paddle.rsqrt(B)
        Y = paddle.add(A, C)
51 52

        self.orig_xs = [X0, X1]
53 54 55
        self.orig_ys = [
            Y,
        ]
56

J
Jiabin Yang 已提交
57 58
        self.orig_ops = ['tanh', 'tanh', 'elementwise_add', 'rsqrt']
        self.orig2prim_ops = ['tanh_p', 'tanh_p', 'add_p', 'rsqrt_p']
59 60 61 62 63 64 65 66 67 68 69 70 71 72
        self.linearize_ops = self.orig2prim_ops + [
            # call fill_const() in linearize() function
            'fill_constant_p',
            'fill_constant_p',
            # linearized op
            'mul_p',
            'sub_p',
            'fill_constant_p',
            'mul_p',
            'mul_p',
            'sub_p',
            'fill_constant_p',
            'mul_p',
            'add_p',
J
Jiabin Yang 已提交
73 74 75 76
            'fill_constant_p',
            'div_p',
            'div_p',
            'mul_p',
77 78 79 80 81 82 83 84 85 86 87 88 89
        ]
        self.transpose_ops = self.orig2prim_ops + [
            # call fill_const() in transpose() function
            'fill_constant_p',
            # linearized op after remove path
            'fill_constant_p',
            'fill_constant_p',
            'mul_p',
            'sub_p',
            'fill_constant_p',
            'mul_p',
            'sub_p',
            'fill_constant_p',
J
Jiabin Yang 已提交
90 91 92 93
            'mul_p',
            'div_p',
            'div_p',
            'fill_constant_p',
94 95
            # transposed op
            'mul_p',
96
            'mul_p',
97
        ]
98
        self.prim2orig_ops_with_blacklist = [
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            'tanh',
            'tanh',
            'add_p',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'sub_p',
            'fill_constant',
            'elementwise_mul',
            'sub_p',
            'fill_constant',
            'elementwise_mul',
            'elementwise_mul',
            'rsqrt',
            'fill_constant',
            'elementwise_div',
            'elementwise_div',
            'elementwise_mul',
118
        ]
119
        self.prim2orig_ops = [
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            'tanh',
            'tanh',
            'elementwise_add',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'elementwise_sub',
            'fill_constant',
            'elementwise_mul',
            'elementwise_sub',
            'fill_constant',
            'elementwise_mul',
            'elementwise_mul',
            'rsqrt',
            'fill_constant',
            'elementwise_div',
            'elementwise_div',
            'elementwise_mul',
139 140 141 142
        ]

    def test_run(self):
        # Must using with program_guard(), otherwise prim ops will append other block
143 144 145
        with paddle.static.program_guard(
            self.main_program, self.startup_program
        ):
146 147 148 149 150 151 152 153 154 155 156 157 158
            ad = Transform(self.main_program.block(0))
            orig_ops = [op.type for op in self.main_program.block(0).ops]
            self.assertEqual(sorted(orig_ops), sorted(self.orig_ops))

            # Test orig2prim
            orig2prim(block=self.main_program.block(0))
            orig2prim_ops = [op.type for op in self.main_program.block(0).ops]
            self.assertEqual(sorted(orig2prim_ops), sorted(self.orig2prim_ops))

            # Test linearize
            xs_dot, ys_dot = ad.linearize(self.orig_xs, self.orig_ys)
            linearize_ops = [op.type for op in self.main_program.block(0).ops]
            self.assertEqual(sorted(linearize_ops), sorted(self.linearize_ops))
159
            flatten_xs_dot = paddle.utils.flatten(xs_dot)
160 161
            for k, v in self.xs_shape_map.items():
                self.assertEqual(flatten_xs_dot[k].shape, v)
162
            flatten_ys_dot = paddle.utils.flatten(ys_dot)
163 164 165 166 167 168 169
            for k, v in self.ys_shape_map.items():
                self.assertEqual(flatten_ys_dot[k].shape, v)

            # Test transpose
            ys_bar, xs_bar = ad.transpose(ys_dot, xs_dot, retain_fwd=False)
            transpose_ops = [op.type for op in self.main_program.block(0).ops]
            self.assertEqual(sorted(transpose_ops), sorted(self.transpose_ops))
170
            flatten_xs_bar = paddle.utils.flatten(xs_bar)
171 172 173 174
            for k, v in self.xs_shape_map.items():
                # There may be None in the result of transpose like gather op
                if flatten_xs_bar[k] is not None:
                    self.assertEqual(flatten_xs_bar[k].shape, v)
175
            flatten_ys_bar = paddle.utils.flatten(ys_bar)
176 177 178
            for k, v in self.ys_shape_map.items():
                self.assertEqual(flatten_ys_bar[k].shape, v)

179
            # Test prim2orig with blacklist
180 181 182
            prim2orig(
                block=self.main_program.block(0), blacklist=['add_p', 'sub_p']
            )
183
            prim2orig_ops = [op.type for op in self.main_program.block(0).ops]
184 185 186
            self.assertEqual(
                sorted(prim2orig_ops), sorted(self.prim2orig_ops_with_blacklist)
            )
187

188 189 190 191 192 193 194 195 196 197 198 199
            # Test prim2orig
            prim2orig(block=self.main_program.block(0))
            prim2orig_ops = [op.type for op in self.main_program.block(0).ops]
            self.assertEqual(sorted(prim2orig_ops), sorted(self.prim2orig_ops))


class TestAutoGradTransformForMatmul(TestAutoGradTransformForAdd):
    def init_data(self):
        # { input_index: input_shape }
        self.xs_shape_map = {0: (100, 2), 1: (5, 2)}
        # { output_index: output_shape }
        self.ys_shape_map = {0: (100, 5)}
200 201 202
        X0 = paddle.static.data(
            'X0', shape=self.xs_shape_map[0], dtype='float32'
        )
203
        X0.stop_gradient = False
204 205 206
        X1 = paddle.static.data(
            'X1', shape=self.xs_shape_map[1], dtype='float32'
        )
207 208 209 210 211 212 213
        X1.stop_gradient = False

        A = paddle.reshape(X1, [2, 5])
        B = paddle.scale(A, scale=2.0, bias=2.0)
        Y = paddle.matmul(X0, B)

        self.orig_xs = [X0, X1]
214 215 216
        self.orig_ys = [
            Y,
        ]
217 218 219

        self.orig_ops = ['reshape2', 'scale', 'matmul_v2']
        self.orig2prim_ops = [
220 221 222 223 224 225 226
            'reshape_p',
            'fill_constant_p',
            'fill_constant_p',
            'fill_constant_p',
            'mul_p',
            'add_p',
            'matmul_p',
227 228 229 230 231 232 233 234 235 236 237 238 239
        ]
        self.linearize_ops = self.orig2prim_ops + [
            # call fill_const() in linearize() function
            'fill_constant_p',
            'fill_constant_p',
            # linearized op
            'reshape_p',
            'mul_p',
            # 'mul_p', # JVP rules handle `None` input, some op will not be appended
            # 'add_p',
            # 'add_p',
            'matmul_p',
            'matmul_p',
240
            'add_p',
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        ]
        self.transpose_ops = self.orig2prim_ops + [
            # call fill_const() in transpose() function
            'fill_constant_p',
            # linearized op after remove path
            'fill_constant_p',
            'fill_constant_p',
            'mul_p',
            # transposed op
            'transpose_p',
            'matmul_p',
            'transpose_p',
            'matmul_p',
            # 'mul_p',
            'reshape_p',
        ]

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        self.prim2orig_ops_with_blacklist = [
            'reshape2',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'add_p',
            'matmul_v2',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'transpose2',
            'matmul_v2',
            'transpose2',
            'matmul_v2',
            # 'elementwise_mul',
            'reshape2',
        ]

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
        self.prim2orig_ops = [
            'reshape2',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'elementwise_add',
            'matmul_v2',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'transpose2',
            'matmul_v2',
            'transpose2',
            'matmul_v2',
            # 'elementwise_mul',
            'reshape2',
        ]


class TestAutoGradTransformForIndexSelect(TestAutoGradTransformForAdd):
    def init_data(self):
        # { input_index: input_shape }
302
        self.xs_shape_map = {0: (7, 8, 9), 1: (8, 1), 2: (7, 8, 9), 3: (3,)}
303 304 305
        # { output_index: output_shape }
        self.ys_shape_map = {0: (3, 16, 9)}

306 307 308
        X0 = paddle.static.data(
            'X0', shape=self.xs_shape_map[0], dtype='float32'
        )
309
        X0.stop_gradient = False
310 311 312
        X1 = paddle.static.data(
            'X1', shape=self.xs_shape_map[1], dtype='float32'
        )
313
        X1.stop_gradient = False
314 315 316
        X2 = paddle.static.data(
            'X2', shape=self.xs_shape_map[2], dtype='float32'
        )
317 318 319 320 321 322 323 324 325 326 327
        X2.stop_gradient = False
        X3 = paddle.static.data('X3', shape=self.xs_shape_map[3], dtype='int32')
        X3.stop_gradient = False

        A = paddle.add(X0, X1)  # (7, 8, 9)
        B = paddle.norm(x=A, p=2)  # (1, )
        C = paddle.subtract(X2, B)  # (7, 8, 9)
        D = paddle.concat(x=(A, C), axis=1)  # (7, 16, 9)
        Y = paddle.index_select(D, X3, axis=0)  # (3, 16, 9)

        self.orig_xs = [X0, X1, X2, X3]
328 329 330
        self.orig_ys = [
            Y,
        ]
331
        self.orig_ops = [
332 333 334 335 336
            'elementwise_add',
            'p_norm',
            'elementwise_sub',
            'concat',
            'index_select',
337 338
        ]
        self.orig2prim_ops = [
339 340 341 342 343 344 345 346 347 348
            'broadcast_p',
            'add_p',
            'reshape_p',
            'mul_p',
            'reduce_sum_p',
            'sqrt_p',
            'broadcast_p',
            'sub_p',
            'concat_p',
            'gather_p',
349 350 351 352 353 354 355 356 357 358 359 360 361 362
        ]
        self.linearize_ops = self.orig2prim_ops + [
            # call fill_const() in linearize() function
            'fill_constant_p',
            'fill_constant_p',
            'fill_constant_p',
            'fill_constant_p',
            # linearized op
            'broadcast_p',
            'add_p',
            'reshape_p',
            'mul_p',
            'mul_p',
            'add_p',
363
            'reduce_sum_p',
364 365 366 367 368 369
            'fill_constant_p',  # 'sqrt_p', Will not append sqrt_p op when apply JVP for sqrt_p
            'mul_p',
            'div_p',
            'broadcast_p',
            'sub_p',
            'concat_p',
370
            'gather_p',
371 372 373 374 375 376 377 378 379 380 381 382
        ]
        self.transpose_ops = self.orig2prim_ops + [
            # call fill_const() in transpose() function
            'fill_constant_p',
            # linearized op after remove path
            'fill_constant_p',
            'fill_constant_p',
            'fill_constant_p',
            'fill_constant_p',
            'fill_constant_p',
            'mul_p',
            # transposed op
383
            'reduce_sum_p',
384 385 386 387 388 389 390
            'reshape_p',
            'reshape_p',
            'mul_p',
            'mul_p',
            'reshape_p',
            'broadcast_p',
            'div_p',
391
            'reduce_sum_p',
392 393 394 395 396 397 398 399 400 401
            'reshape_p',
            'fill_constant_p',
            'sub_p',
            'split_p',
            'fill_constant_p',
            'scatter_add_p',
            'add_p',  # The output of the op is used by multiple subsequent ops
            'add_p',
        ]

402
        self.prim2orig_ops_with_blacklist = [
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
            'expand_v2',
            'add_p',
            'reshape2',
            'elementwise_mul',
            'reduce_sum',
            'sqrt',
            'expand_v2',
            'sub_p',
            'concat',
            'gather',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'reduce_sum',
            'reshape2',
            'reshape2',
            'elementwise_mul',
            'elementwise_mul',
            'reshape2',
            'expand_v2',
            'elementwise_div',
            'reduce_sum',
            'reshape2',
            'fill_constant',
            'sub_p',
            'split',
            'fill_constant',
            'fill_any_like',
            'add_p',
            'scatter',
            'elementwise_add',
            'add_p',
439 440
        ]

441
        self.prim2orig_ops = [
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
            'expand_v2',
            'elementwise_add',
            'reshape2',
            'elementwise_mul',
            'reduce_sum',
            'sqrt',
            'expand_v2',
            'elementwise_sub',
            'concat',
            'gather',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'elementwise_mul',
            'reduce_sum',
            'reshape2',
            'reshape2',
            'elementwise_mul',
            'elementwise_mul',
            'reshape2',
            'expand_v2',
            'elementwise_div',
            'reduce_sum',
            'reshape2',
            'fill_constant',
            'elementwise_sub',
            'split',
            'fill_constant',
            'fill_any_like',
            'elementwise_add',
            'scatter',
            'elementwise_add',
            'elementwise_add',
478 479 480 481 482
        ]


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