test_dist_transpiler.py 49.4 KB
Newer Older
Y
Yancey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2018 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
import functools
16 17
import gc
import math
18
import unittest
19

20
import numpy as np
T
tangwei12 已提交
21

22 23
gc.set_debug(gc.DEBUG_COLLECTABLE)

24
import paddle
25
from paddle import fluid
26

Y
Yancey 已提交
27

W
Wu Yi 已提交
28
class TranspilerTest(unittest.TestCase):
Y
Yancey 已提交
29
    def setUp(self):
W
Wu Yi 已提交
30 31 32 33 34 35 36 37 38 39 40
        self.trainer_id = 0
        self.trainers = 2
        self.pservers = 2
        # NOTE: we do not actually bind this port
        self.pserver_eps = "127.0.0.1:6174,127.0.0.1:6175"
        self.pserver1_ep = "127.0.0.1:6174"
        self.pserver2_ep = "127.0.0.1:6175"
        self.sync_mode = True
        self.transpiler = None

    def net_conf(self):
G
GGBond8488 已提交
41
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
42 43
        y_predict = paddle.static.nn.fc(
            x,
44
            size=1000,
C
Charles-hit 已提交
45
            weight_attr=fluid.ParamAttr(name='fc_w'),
46 47
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
48
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
49
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
50
        avg_cost = paddle.mean(cost)
W
Wu Yi 已提交
51 52 53 54 55
        sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.1)
        sgd_optimizer.minimize(avg_cost)

    def get_main_program(self):
        main = fluid.Program()
56
        main.random_seed = 1
W
Wu Yi 已提交
57 58 59 60 61
        with fluid.program_guard(main):
            self.net_conf()
        self.origin_prog = main.clone()
        return main

1
123malin 已提交
62
    def get_trainer(self, config=None, sync_mode=True):
G
gongweibao 已提交
63 64
        src = fluid.default_startup_program().clone()

1
123malin 已提交
65
        t = self._transpiler_instance(config, sync_mode=True)
G
gongweibao 已提交
66

W
Wu Yi 已提交
67
        trainer_main = t.get_trainer_program(wait_port=False)
G
gongweibao 已提交
68 69
        trainer_startup = fluid.default_startup_program()

70 71
        assert src.num_blocks == 1
        assert trainer_startup.num_blocks == src.num_blocks
G
gongweibao 已提交
72 73

        return trainer_main, trainer_startup
W
Wu Yi 已提交
74

Q
qiaolongfei 已提交
75 76
    def get_pserver(self, ep, config=None, sync_mode=True):
        t = self._transpiler_instance(config, sync_mode)
W
Wu Yi 已提交
77 78 79 80
        pserver = t.get_pserver_program(ep)
        startup = t.get_startup_program(ep, pserver)
        return pserver, startup

Q
qiaolongfei 已提交
81
    def _transpiler_instance(self, config=None, sync_mode=True):
W
Wu Yi 已提交
82 83
        if not self.transpiler:
            main = self.get_main_program()
84 85 86 87 88
            self.transpiler = (
                paddle.distributed.transpiler.DistributeTranspiler(
                    config=config
                )
            )
89 90 91 92 93 94 95
            self.transpiler.transpile(
                self.trainer_id,
                program=main,
                pservers=self.pserver_eps,
                trainers=self.trainers,
                sync_mode=sync_mode,
            )
G
gongweibao 已提交
96

W
Wu Yi 已提交
97
        return self.transpiler
Y
Yancey 已提交
98

Q
qiaolongfei 已提交
99 100
    def transpiler_test_impl(self):
        pass
W
Wu Yi 已提交
101

Y
Yancey 已提交
102
    def test_transpiler(self):
Q
qiaolongfei 已提交
103 104
        main = fluid.Program()
        startup = fluid.Program()
T
tangwei12 已提交
105 106 107
        with fluid.unique_name.guard():
            with fluid.program_guard(main, startup):
                self.transpiler_test_impl()
108 109 110 111 112 113
        # NOTE: run gc.collect to eliminate pybind side objects to
        # prevent random double-deallocate when inherited in python.
        del self.transpiler
        del main
        del startup
        gc.collect()
Q
qiaolongfei 已提交
114 115 116 117


class TestBasicModel(TranspilerTest):
    def transpiler_test_impl(self):
W
Wu Yi 已提交
118 119 120
        pserver, startup = self.get_pserver(self.pserver1_ep)
        pserver2, startup2 = self.get_pserver(self.pserver2_ep)

G
gongweibao 已提交
121 122
        trainer, trainer_startup = self.get_trainer()

T
tianshuo78520a 已提交
123
        # split var blocks should be in startup program
G
gongweibao 已提交
124 125 126 127 128 129 130 131
        self.assertTrue("fc_w.block0" in trainer_startup.global_block().vars)
        self.assertTrue("fc_w.block1" in trainer_startup.global_block().vars)
        self.assertTrue("fc_w" in trainer_startup.global_block().vars)
        self.assertTrue("fc_b" in trainer_startup.global_block().vars)
        self.assertTrue("fc_w@GRAD" not in trainer_startup.global_block().vars)
        self.assertTrue("fc_b@GRAD" not in trainer_startup.global_block().vars)

        src = [op.type for op in trainer_startup.global_block().ops]
132 133 134 135 136 137 138 139 140
        dst = [
            'fill_constant',
            'fill_constant',
            'uniform_random',
            'recv',
            'recv',
            'fetch_barrier',
            'concat',
        ]
G
gongweibao 已提交
141 142

        self.assertEqual(src, dst)
W
Wu Yi 已提交
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
        self.assertEqual(
            [op.type for op in trainer.global_block().ops],
            [
                'mul',
                'elementwise_add',
                'elementwise_sub',
                'square',
                'mean',
                'fill_constant',
                'mean_grad',
                'square_grad',
                'elementwise_sub_grad',
                'elementwise_add_grad',
                'send',
                'mul_grad',
                'split_byref',
                'send',
                'send_barrier',
                'recv',
                'recv',
                'fetch_barrier',
                'concat',
            ],
        )
Y
Yancey 已提交
168 169 170

        self.assertEqual(len(pserver.blocks), 3)
        # block0: listen_and_serv
171 172 173
        self.assertEqual(
            [op.type for op in pserver.blocks[0].ops], ["listen_and_serv"]
        )
W
Wu Yi 已提交
174
        # block1~2: optimize pass
175 176 177
        self.assertEqual(
            [op.type for op in pserver.blocks[1].ops], ["sum", "scale", "sgd"]
        )
Y
Yancey 已提交
178
        # confirm startup program
179 180 181 182
        self.assertEqual(
            [op.type for op in startup.global_block().ops],
            ["fill_constant", "fill_constant", "uniform_random"],
        )
Y
Yancey1989 已提交
183
        # the variable #fc_w will be split into two blocks
Y
Yancey 已提交
184 185
        fc_w_var = startup.global_block().var("fc_w.block1")
        self.assertEqual(fc_w_var.shape, (500, 1000))
W
Wu Yi 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        # all parameters should be optimized on pserver

        pserver_params = []
        for prog in [pserver, pserver2]:
            for blk in prog.blocks:
                for op in blk.ops:
                    if "Param" in op.input_names:
                        param_name = op.input("Param")[0]
                        is_block_idx = param_name.find(".block")
                        if is_block_idx != -1:
                            origin_param_name = param_name[:is_block_idx]
                        else:
                            origin_param_name = param_name
                        pserver_params.append(origin_param_name)
        trainer_params = []
        for op in self.origin_prog.global_block().ops:
            if "Param" in op.input_names:
                trainer_params.append(op.input("Param")[0])
        self.assertEqual(set(pserver_params), set(trainer_params))


G
gongweibao 已提交
207
class TestBasicModelWithLargeBlockSize(TranspilerTest):
Q
qiaolongfei 已提交
208
    def transpiler_test_impl(self):
209
        config = paddle.distributed.transpiler.DistributeTranspilerConfig()
G
gongweibao 已提交
210 211 212 213 214
        config.min_block_size = 1048576

        pserver, startup = self.get_pserver(self.pserver1_ep, config)
        pserver2, startup2 = self.get_pserver(self.pserver2_ep, config)

G
gongweibao 已提交
215
        trainer, _ = self.get_trainer(config)
G
gongweibao 已提交
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        self.assertEqual(
            [op.type for op in trainer.global_block().ops],
            [
                'mul',
                'elementwise_add',
                'elementwise_sub',
                'square',
                'mean',
                'fill_constant',
                'mean_grad',
                'square_grad',
                'elementwise_sub_grad',
                'elementwise_add_grad',
                'send',
                'mul_grad',
                'send',
                'send_barrier',
                'recv',
                'recv',
                'fetch_barrier',
            ],
        )
G
gongweibao 已提交
239 240 241

        self.assertEqual(len(pserver.blocks), 2)
        # block0: listen_and_serv
242 243 244
        self.assertEqual(
            [op.type for op in pserver.blocks[0].ops], ["listen_and_serv"]
        )
G
gongweibao 已提交
245
        # block1~2: optimize pass
246 247 248
        self.assertEqual(
            [op.type for op in pserver.blocks[1].ops], ["sum", "scale", "sgd"]
        )
G
gongweibao 已提交
249
        # confirm startup program
250 251 252 253
        self.assertEqual(
            [op.type for op in startup.global_block().ops],
            ["fill_constant", "fill_constant"],
        )
G
gongweibao 已提交
254 255
        # the variable #fc_w will be split into two blocks
        fc_w_var = startup2.global_block().var("fc_w")
256
        self.assertEqual(fc_w_var.shape, (1000, 1000))
G
gongweibao 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        # all parameters should be optimized on pserver

        pserver_params = []
        for prog in [pserver, pserver2]:
            for blk in prog.blocks:
                for op in blk.ops:
                    if "Param" in op.input_names:
                        param_name = op.input("Param")[0]
                        is_block_idx = param_name.find(".block")
                        if is_block_idx != -1:
                            origin_param_name = param_name[:is_block_idx]
                        else:
                            origin_param_name = param_name
                        pserver_params.append(origin_param_name)
        trainer_params = []
        for op in self.origin_prog.global_block().ops:
            if "Param" in op.input_names:
                trainer_params.append(op.input("Param")[0])
        self.assertEqual(set(pserver_params), set(trainer_params))


W
Wu Yi 已提交
278 279
class TestNoSliceVar(TranspilerTest):
    def setUp(self):
280
        super().setUp()
W
Wu Yi 已提交
281

Q
qiaolongfei 已提交
282
    def transpiler_test_impl(self):
283
        config = paddle.distributed.transpiler.DistributeTranspilerConfig()
G
gongweibao 已提交
284 285 286 287
        config.slice_var_up = False

        _, startup = self.get_pserver(self.pserver1_ep, config)
        _, startup2 = self.get_pserver(self.pserver2_ep, config)
W
Wu Yi 已提交
288

289
        if "fc_w" in startup.global_block().vars:
W
Wu Yi 已提交
290
            fc_w_var = startup.global_block().vars["fc_w"]
291
        elif "fc_w" in startup2.global_block().vars:
W
Wu Yi 已提交
292 293 294
            fc_w_var = startup2.global_block().vars["fc_w"]

        self.assertEqual(fc_w_var.shape, (1000, 1000))
Y
Yancey 已提交
295 296


W
Wu Yi 已提交
297 298
class TestLRDecay(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
299
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
300 301
        y_predict = paddle.static.nn.fc(
            x,
302
            size=1000,
C
Charles-hit 已提交
303
            weight_attr=fluid.ParamAttr(name='fc_w'),
304 305
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
306
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
307
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
308
        avg_cost = paddle.mean(cost)
W
Wu Yi 已提交
309
        sgd_optimizer = fluid.optimizer.SGD(
310 311 312 313 314 315 316
            learning_rate=fluid.layers.exponential_decay(
                learning_rate=1.0,
                decay_steps=2100,
                decay_rate=0.1,
                staircase=True,
            )
        )
W
Wu Yi 已提交
317 318
        sgd_optimizer.minimize(avg_cost)

Q
qiaolongfei 已提交
319
    def transpiler_test_impl(self):
W
Wu Yi 已提交
320
        pserver, startup = self.get_pserver(self.pserver1_ep)
G
gongweibao 已提交
321
        trainer, _ = self.get_trainer()
W
Wu Yi 已提交
322 323 324

        self.assertEqual(len(pserver.blocks), 4)
        lr_decay_ops = [op.type for op in pserver.blocks[1].ops]
325 326 327 328 329 330 331 332 333 334 335 336 337 338
        self.assertEqual(
            lr_decay_ops,
            [
                "increment",
                "cast",
                "fill_constant",
                "elementwise_div",
                "floor",
                "fill_constant",
                "elementwise_pow",
                "fill_constant",
                "elementwise_mul",
            ],
        )
W
Wu Yi 已提交
339 340


T
tangwei12 已提交
341 342 343 344
class TestFakeInit(TranspilerTest):
    def net_conf(self):
        dict_size, embedding_size, neg_num = 10000, 8, 5

G
GGBond8488 已提交
345 346
        input_word = paddle.static.data(
            name="input_word", shape=[-1, 1], dtype='int64', lod_level=1
347
        )
G
GGBond8488 已提交
348 349
        true_word = paddle.static.data(
            name='true_label', shape=[-1, 1], dtype='int64', lod_level=1
350
        )
G
GGBond8488 已提交
351 352
        neg_word = paddle.static.data(
            name="neg_label", shape=[-1, 1], dtype='int64', lod_level=1
353
        )
T
tangwei12 已提交
354 355 356 357 358 359 360
        inputs = [input_word, true_word, neg_word]

        init_width = 0.5 / embedding_size
        input_emb = fluid.layers.embedding(
            input=inputs[0],
            is_sparse=True,
            size=[dict_size, embedding_size],
361 362
            param_attr=fluid.ParamAttr(
                name='emb',
363 364 365
                initializer=paddle.nn.initializer.Uniform(
                    -init_width, init_width
                ),
366 367
            ),
        )
T
tangwei12 已提交
368 369 370 371 372 373

        true_emb_w = fluid.layers.embedding(
            input=inputs[1],
            is_sparse=True,
            size=[dict_size, embedding_size],
            param_attr=fluid.ParamAttr(
374 375
                name='emb_w',
                initializer=paddle.nn.initializer.Constant(value=0.0),
376 377
            ),
        )
T
tangwei12 已提交
378 379 380 381 382 383

        true_emb_b = fluid.layers.embedding(
            input=inputs[1],
            is_sparse=True,
            size=[dict_size, 1],
            param_attr=fluid.ParamAttr(
384 385
                name='emb_b',
                initializer=paddle.nn.initializer.Constant(value=0.0),
386 387
            ),
        )
T
tangwei12 已提交
388

389
        neg_word_reshape = paddle.reshape(inputs[2], shape=[-1, 1])
T
tangwei12 已提交
390 391
        neg_word_reshape.stop_gradient = True

392 393 394 395 396 397
        neg_emb_w = fluid.layers.embedding(
            input=neg_word_reshape,
            is_sparse=True,
            size=[dict_size, embedding_size],
            param_attr=fluid.ParamAttr(name='emb_w', learning_rate=1.0),
        )
T
tangwei12 已提交
398

399
        neg_emb_w_re = paddle.reshape(
400 401
            neg_emb_w, shape=[-1, neg_num, embedding_size]
        )
T
tangwei12 已提交
402

403 404 405 406 407 408
        neg_emb_b = fluid.layers.embedding(
            input=neg_word_reshape,
            is_sparse=True,
            size=[dict_size, 1],
            param_attr=fluid.ParamAttr(name='emb_b', learning_rate=1.0),
        )
T
tangwei12 已提交
409

410
        neg_emb_b_vec = paddle.reshape(neg_emb_b, shape=[-1, neg_num])
T
tangwei12 已提交
411

412
        true_logits = paddle.add(
413
            paddle.sum(
414
                paddle.multiply(input_emb, true_emb_w),
415 416 417 418 419 420
                dim=1,
                keep_dim=True,
            ),
            true_emb_b,
        )

421
        input_emb_re = paddle.reshape(input_emb, shape=[-1, 1, embedding_size])
422

K
kangguangli 已提交
423
        neg_matmul = paddle.matmul(input_emb_re, neg_emb_w_re, transpose_y=True)
424
        neg_matmul_re = paddle.reshape(neg_matmul, shape=[-1, neg_num])
425
        neg_logits = paddle.add(neg_matmul_re, neg_emb_b_vec)
T
tangwei12 已提交
426
        # nce loss
427 428 429
        label_ones = fluid.layers.fill_constant_batch_size_like(
            true_logits, shape=[-1, 1], value=1.0, dtype='float32'
        )
T
tangwei12 已提交
430
        label_zeros = fluid.layers.fill_constant_batch_size_like(
431 432
            true_logits, shape=[-1, neg_num], value=0.0, dtype='float32'
        )
T
tangwei12 已提交
433

434
        true_xent = paddle.nn.functional.binary_cross_entropy_with_logits(
435 436
            true_logits, label_ones
        )
437
        neg_xent = paddle.nn.functional.binary_cross_entropy_with_logits(
438 439
            neg_logits, label_zeros
        )
440
        cost = paddle.add(
441 442
            paddle.sum(true_xent, axis=1),
            paddle.sum(neg_xent, axis=1),
443
        )
444
        avg_cost = paddle.mean(cost)
T
tangwei12 已提交
445 446

        sgd_optimizer = fluid.optimizer.SGD(
447 448 449 450 451 452 453
            learning_rate=fluid.layers.exponential_decay(
                learning_rate=1.0,
                decay_steps=2100,
                decay_rate=0.1,
                staircase=True,
            )
        )
T
tangwei12 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466
        sgd_optimizer.minimize(avg_cost)

    def transpiler_test_impl(self):
        trainer, startup = self.get_trainer()

        fake_init_ops = []
        for op in startup.global_block().ops:
            if op.type == "fake_init":
                fake_init_ops.append(op)

        self.assertEqual(len(fake_init_ops), 3)


467 468
class TestDecayedAdagrad(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
469
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
470 471
        y_predict = paddle.static.nn.fc(
            x,
472
            size=1000,
C
Charles-hit 已提交
473
            weight_attr=fluid.ParamAttr(name='fc_w'),
474 475
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
476
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
477
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
478
        avg_cost = paddle.mean(cost)
479 480 481 482 483 484 485 486
        opt = fluid.optimizer.DecayedAdagrad(learning_rate=0.1)
        opt.minimize(avg_cost)

    def transpiler_test_impl(self):
        pserver, startup = self.get_pserver(self.pserver1_ep)
        trainer, _ = self.get_trainer()


487 488
class TestFtrl(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
489
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
490 491
        y_predict = paddle.static.nn.fc(
            x,
492
            size=1000,
C
Charles-hit 已提交
493
            weight_attr=fluid.ParamAttr(name='fc_w'),
494 495
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
496
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
497
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
498
        avg_cost = paddle.mean(cost)
499 500 501 502 503 504 505 506
        opt = fluid.optimizer.Ftrl(learning_rate=0.1)
        opt.minimize(avg_cost)

    def transpiler_test_impl(self):
        pserver, startup = self.get_pserver(self.pserver1_ep)
        trainer, _ = self.get_trainer()


W
Wu Yi 已提交
507 508
class TestLRDecayConditional(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
509
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
510 511
        y_predict = paddle.static.nn.fc(
            x,
512
            size=1000,
C
Charles-hit 已提交
513
            weight_attr=fluid.ParamAttr(name='fc_w'),
514 515
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
516
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
517
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
518
        avg_cost = paddle.mean(cost)
W
Wu Yi 已提交
519
        sgd_optimizer = fluid.optimizer.SGD(
520 521 522 523
            learning_rate=fluid.layers.piecewise_decay(
                [10000, 20000], [1.0, 0.5, 1.0]
            )
        )
W
Wu Yi 已提交
524 525
        sgd_optimizer.minimize(avg_cost)

Q
qiaolongfei 已提交
526
    def transpiler_test_impl(self):
W
Wu Yi 已提交
527
        pserver, startup = self.get_pserver(self.pserver1_ep)
G
gongweibao 已提交
528
        trainer, _ = self.get_trainer()
W
Wu Yi 已提交
529 530 531 532

        serv_op = pserver.blocks[0].ops[0]
        sub_blocks = []
        optimize_blocks = []
G
gongweibao 已提交
533
        for b in serv_op.all_attrs()["optimize_blocks"]:
W
Wu Yi 已提交
534 535 536 537 538 539 540
            optimize_blocks.append(b.idx)
        for b in pserver.blocks:
            if b.idx not in optimize_blocks:
                sub_blocks.append(b.idx)

        self.assertEqual(len(pserver.blocks), 7)
        lr_decay_ops = [op.type for op in pserver.blocks[1].ops]
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        self.assertEqual(
            lr_decay_ops,
            [
                "increment",
                "cast",
                "fill_constant",
                "fill_constant",
                "less_than",
                "logical_not",
                "conditional_block",
                "fill_constant",
                "fill_constant",
                "less_than",
                "logical_not",
                "logical_and",
                "logical_and",
                "conditional_block",
                "fill_constant",
                "conditional_block",
            ],
        )
W
Wu Yi 已提交
562 563 564 565 566 567 568 569 570 571
        # test the condition blocks
        for b in sub_blocks:
            if b == 0:
                continue
            block = pserver.blocks[b]
            self.assertEqual([op.type for op in block.ops], ["assign"])


class TestL2Decay(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
572
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
573 574
        y_predict = paddle.static.nn.fc(
            x,
W
Wu Yi 已提交
575
            size=1000,
C
Charles-hit 已提交
576
            weight_attr=fluid.ParamAttr(
577
                name='fc_w', regularizer=paddle.regularizer.L2Decay()
578 579 580
            ),
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
581
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
582
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
583
        avg_cost = paddle.mean(cost)
W
Wu Yi 已提交
584
        sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.1)
585 586 587 588

        def filter(param):
            return param.name == "fc_w"

589
        clip = paddle.nn.ClipGradByValue(0.1, need_clip=filter)
590
        sgd_optimizer.minimize(avg_cost, grad_clip=clip)
W
Wu Yi 已提交
591

Q
qiaolongfei 已提交
592
    def transpiler_test_impl(self):
W
Wu Yi 已提交
593
        pserver, startup = self.get_pserver(self.pserver1_ep)
G
gongweibao 已提交
594
        trainer, _ = self.get_trainer()
W
Wu Yi 已提交
595 596

        self.assertEqual(len(pserver.blocks), 3)
597 598 599 600 601 602 603 604
        self.assertEqual(
            [op.type for op in pserver.blocks[1].ops],
            ["sum", "scale", "clip", "sgd"],
        )
        self.assertEqual(
            [op.type for op in pserver.blocks[2].ops],
            ["sum", "scale", "clip", "scale", "sum", "sgd"],
        )
W
Wu Yi 已提交
605 606
        # TODO(typhoonzero): test clipping and L2Decay ops are removed from trainer

Y
Yancey 已提交
607

T
typhoonzero 已提交
608 609
class TestL2DecayWithPiecewise(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
610
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
611 612
        y_predict = paddle.static.nn.fc(
            x,
613
            size=1000,
C
Charles-hit 已提交
614
            weight_attr=fluid.ParamAttr(name='fc_w'),
615 616
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
617
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
618
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
619
        avg_cost = paddle.mean(cost)
T
typhoonzero 已提交
620 621 622 623
        base_lr = 1.0
        bd = [1, 10, 20, 30]
        lr = [base_lr * (0.1**i) for i in range(len(bd) + 1)]
        sgd_optimizer = fluid.optimizer.Momentum(
624 625 626
            learning_rate=fluid.layers.piecewise_decay(
                boundaries=bd, values=lr
            ),
T
typhoonzero 已提交
627
            momentum=0.9,
628
            regularization=paddle.regularizer.L2Decay(1e-4),
629
        )
T
typhoonzero 已提交
630 631
        sgd_optimizer.minimize(avg_cost)

Q
qiaolongfei 已提交
632
    def transpiler_test_impl(self):
T
typhoonzero 已提交
633
        pserver, startup = self.get_pserver(self.pserver1_ep)
G
gongweibao 已提交
634
        trainer, _ = self.get_trainer()
T
typhoonzero 已提交
635 636

        self.assertEqual(len(pserver.blocks), 9)
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
        self.assertEqual(
            [op.type for op in pserver.blocks[1].ops],
            [
                "increment",
                "cast",
                "fill_constant",
                "fill_constant",
                "less_than",
                "logical_not",
                "conditional_block",
                "fill_constant",
                "fill_constant",
                "less_than",
                "logical_not",
                "logical_and",
                "logical_and",
                "conditional_block",
                "fill_constant",
                "fill_constant",
                "less_than",
                "logical_not",
                "logical_and",
                "logical_and",
                "conditional_block",
                "fill_constant",
                "fill_constant",
                "less_than",
                "logical_not",
                "logical_and",
                "logical_and",
                "conditional_block",
                "fill_constant",
                "conditional_block",
            ],
        )
        self.assertEqual(
            [op.type for op in pserver.blocks[7].ops],
            ["sum", "scale", "scale", "sum", "momentum"],
        )
        self.assertEqual(
            [op.type for op in pserver.blocks[8].ops],
            ["sum", "scale", "scale", "sum", "momentum"],
        )
Y
Yancey 已提交
680 681


Q
Qiao Longfei 已提交
682 683
class TestEmptyPserverOptimizeBlocks(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
684
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
Q
Qiao Longfei 已提交
685
        # only one parameter
C
Charles-hit 已提交
686 687
        y_predict = paddle.static.nn.fc(
            x,
688
            size=1000,
C
Charles-hit 已提交
689
            weight_attr=fluid.ParamAttr(name='fc_w'),
690 691
            bias_attr=False,
        )
G
GGBond8488 已提交
692
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
693
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
694
        avg_cost = paddle.mean(cost)
Q
Qiao Longfei 已提交
695 696 697 698
        sgd_optimizer = fluid.optimizer.SGD(learning_rate=1.0)
        sgd_optimizer.minimize(avg_cost)

    def transpiler_test_impl(self):
699
        config = paddle.distributed.transpiler.DistributeTranspilerConfig()
Q
Qiao Longfei 已提交
700 701 702 703 704 705 706 707
        config.slice_var_up = False

        pserver, startup = self.get_pserver(ep=self.pserver2_ep, config=config)

        self.assertEqual(len(pserver.blocks), 2)
        self.assertEqual(len(pserver.blocks[1].ops), 0)


708
class TestDistLookupTableBase(TranspilerTest):
Q
Qiao Longfei 已提交
709
    def network_with_table(self, is_sparse, is_distributed):
T
tangwei12 已提交
710 711
        self.table_size = 1000
        self.emb_size = 64
T
tangwei12 已提交
712
        self.lookup_table_name = 'shared_w'
T
tangwei12 已提交
713

Q
Qiao Longfei 已提交
714
        def emb_pool(ids, table_name, is_distributed):
715 716 717 718 719 720 721 722
            emb = fluid.layers.embedding(
                input=ids,
                size=[self.table_size, self.emb_size],
                dtype='float32',
                param_attr=table_name,
                is_sparse=is_sparse,
                is_distributed=is_distributed,
            )
723 724 725
            pool = paddle.static.nn.sequence_lod.sequence_pool(
                input=emb, pool_type='average'
            )
726 727
            return pool

G
GGBond8488 已提交
728 729
        title_ids = paddle.static.data(
            name='title_ids', shape=[-1, 1], dtype='int64', lod_level=1
730
        )
G
GGBond8488 已提交
731 732
        brand_ids = paddle.static.data(
            name='brand_ids', shape=[-1, 1], dtype='int64', lod_level=1
733
        )
G
GGBond8488 已提交
734 735
        profile_ids = paddle.static.data(
            name='brand_ids', shape=[-1, 1], dtype='int64', lod_level=1
736
        )
Q
Qiao Longfei 已提交
737 738 739
        title_emb = emb_pool(title_ids, self.lookup_table_name, is_distributed)
        brand_emb = emb_pool(brand_ids, self.lookup_table_name, is_distributed)
        profile_emb = emb_pool(profile_ids, "profile_emb", False)
740
        fc0 = paddle.concat([title_emb, brand_emb, profile_emb], axis=1)
C
Charles-hit 已提交
741 742
        predict = paddle.static.nn.fc(
            x=fc0,
743
            size=2,
C
Charles-hit 已提交
744
            weight_attr=fluid.ParamAttr(name='fc_w'),
745 746
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
747

G
GGBond8488 已提交
748
        label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
749 750 751
        cost = paddle.nn.functional.cross_entropy(
            input=predict, label=label, reduction='none', use_softmax=False
        )
752
        avg_cost = paddle.mean(cost)
753 754 755 756
        optimizer = fluid.optimizer.Adam(learning_rate=0.003)
        optimizer.minimize(avg_cost)


Q
qiaolongfei 已提交
757 758 759 760 761 762 763
class TestLocalLookupTable(TestDistLookupTableBase):
    def net_conf(self):
        self.network_with_table(is_sparse=True, is_distributed=False)

    def transpiler_test_impl(self):
        pserver1, startup1 = self.get_pserver(self.pserver1_ep)

764
        self.assertEqual(len(pserver1.blocks), 4)
Q
qiaolongfei 已提交
765 766
        # 0 listen_and_serv
        # 1 optimize for fc_w or fc_b adam
767 768 769 770
        self.assertEqual(
            [op.type for op in pserver1.blocks[1].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
Q
qiaolongfei 已提交
771 772
        # 2 optimize for table adam
        # NOTE: if param is not selected rows, the grad will scaled to grad / trainer_num
773 774 775 776
        self.assertEqual(
            [op.type for op in pserver1.blocks[2].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
Q
qiaolongfei 已提交
777

778 779
        # 3 optimize for table 2 adam
        # NOTE: if param is not selected rows, the grad will scaled to grad / trainer_num
780 781 782 783
        self.assertEqual(
            [op.type for op in pserver1.blocks[3].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
784

G
gongweibao 已提交
785
        trainer, _ = self.get_trainer()
Q
qiaolongfei 已提交
786 787
        self.assertEqual(len(trainer.blocks), 1)
        ops = [
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
            'lookup_table',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'concat',
            'mul',
            'elementwise_add',
            'cross_entropy2',
            'mean',
            'fill_constant',
            'mean_grad',
            'cross_entropy_grad2',
            'elementwise_add_grad',
            'send',
            'mul_grad',
            'send',
            'concat_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'split_selected_rows',
            'send',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sum',
            'split_selected_rows',
            'send',
            'send_barrier',
            'recv',
            'recv',
            'fetch_barrier',
Q
qiaolongfei 已提交
822 823 824 825
        ]
        self.assertEqual([op.type for op in trainer.blocks[0].ops], ops)


826 827 828 829 830 831 832
class TestDistLookupTable(TestDistLookupTableBase):
    def net_conf(self):
        self.network_with_table(is_sparse=True, is_distributed=True)

    def transpiler_test_impl(self):
        pserver1, startup1 = self.get_pserver(self.pserver1_ep)

833
        self.assertEqual(len(pserver1.blocks), 6)
834 835
        # 0 listen_and_serv
        # 1 optimize for fc_w or fc_b adam
836 837 838 839
        self.assertEqual(
            [op.type for op in pserver1.blocks[1].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
840
        # 4 prefetch -> lookup_sparse_table_read for data0
841 842 843 844
        self.assertEqual(
            [op.type for op in pserver1.blocks[2].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
Q
Qiao Longfei 已提交
845
        # 2 optimize for table sgd
846 847 848
        self.assertEqual(
            [op.type for op in pserver1.blocks[3].ops], ["sum", "sgd"]
        )
849
        # 3 prefetch -> lookup_sparse_table_read for data0
850 851 852 853
        self.assertEqual(
            [op.type for op in pserver1.blocks[4].ops],
            ["lookup_sparse_table_read"],
        )
Q
Qiao Longfei 已提交
854 855 856 857 858 859
        # 5 save table
        self.assertEqual([op.type for op in pserver1.blocks[5].ops], ["save"])

        trainer, trainer_startup = self.get_trainer()
        self.assertEqual(len(trainer.blocks), 1)
        ops = [
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
            'split_ids',
            'prefetch',
            'merge_ids',
            'sequence_pool',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'concat',
            'mul',
            'elementwise_add',
            'cross_entropy2',
            'mean',
            'fill_constant',
            'mean_grad',
            'cross_entropy_grad2',
            'elementwise_add_grad',
            'send',
            'mul_grad',
            'send',
            'concat_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'split_selected_rows',
            'send',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sum',
            'split_ids',
            'send',
            'send_barrier',
            'recv',
            'recv',
            'fetch_barrier',
Q
Qiao Longfei 已提交
895 896 897
        ]
        self.assertEqual([op.type for op in trainer.blocks[0].ops], ops)
        startup_ops = [
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'uniform_random',
            'uniform_random',
            'recv',
            'recv',
            'recv',
            'fetch_barrier',
            'concat',
            'fake_init',
Q
Qiao Longfei 已提交
920
        ]
921 922 923
        self.assertEqual(
            [op.type for op in trainer_startup.blocks[0].ops], startup_ops
        )
Q
Qiao Longfei 已提交
924 925


Q
qiaolongfei 已提交
926 927 928 929 930
class TestAsyncLocalLookupTable(TestDistLookupTableBase):
    def net_conf(self):
        self.network_with_table(is_sparse=True, is_distributed=False)

    def transpiler_test_impl(self):
931
        config = paddle.distributed.transpiler.DistributeTranspilerConfig()
Q
qiaolongfei 已提交
932
        pserver1, startup1 = self.get_pserver(self.pserver1_ep, config, False)
Q
qiaolongfei 已提交
933

934
        self.assertEqual(len(pserver1.blocks), 4)
Q
qiaolongfei 已提交
935 936
        # 0 listen_and_serv
        # 1 optimize for fc_w or fc_b adam
937 938 939 940
        self.assertEqual(
            [op.type for op in pserver1.blocks[1].ops],
            ["adam", "scale", "scale"],
        )
Q
qiaolongfei 已提交
941 942
        # 2 optimize for table adam
        # NOTE: if param is not selected rows, the grad will scaled to grad / trainer_num
943 944 945 946
        self.assertEqual(
            [op.type for op in pserver1.blocks[2].ops],
            ["adam", "scale", "scale"],
        )
947 948
        # 3 optimize for table adam
        # NOTE: if param is not selected rows, the grad will scaled to grad / trainer_num
949 950 951 952
        self.assertEqual(
            [op.type for op in pserver1.blocks[3].ops],
            ["adam", "scale", "scale"],
        )
Q
qiaolongfei 已提交
953

G
gongweibao 已提交
954
        trainer, _ = self.get_trainer(config)
Q
qiaolongfei 已提交
955 956
        self.assertEqual(len(trainer.blocks), 1)
        ops = [
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
            'lookup_table',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'concat',
            'mul',
            'elementwise_add',
            'cross_entropy2',
            'mean',
            'fill_constant',
            'mean_grad',
            'cross_entropy_grad2',
            'elementwise_add_grad',
            'send',
            'mul_grad',
            'send',
            'concat_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'split_selected_rows',
            'send',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sum',
            'split_selected_rows',
            'send',
            'recv',
            'recv',
Q
qiaolongfei 已提交
989 990 991 992
        ]
        self.assertEqual([op.type for op in trainer.blocks[0].ops], ops)


Q
qiaolongfei 已提交
993 994 995 996 997
class TestAsyncDistLookupTable(TestDistLookupTableBase):
    def net_conf(self):
        self.network_with_table(is_sparse=True, is_distributed=True)

    def transpiler_test_impl(self):
998
        config = paddle.distributed.transpiler.DistributeTranspilerConfig()
Q
qiaolongfei 已提交
999

Q
qiaolongfei 已提交
1000
        pserver1, startup1 = self.get_pserver(self.pserver1_ep, config, False)
Q
qiaolongfei 已提交
1001

1002
        self.assertEqual(len(pserver1.blocks), 6)
Q
qiaolongfei 已提交
1003 1004
        # 0 listen_and_serv
        # 1 optimize for fc_w or fc_b adam
1005 1006 1007 1008
        self.assertEqual(
            [op.type for op in pserver1.blocks[1].ops],
            ["adam", "scale", "scale"],
        )
1009
        # 2 optimize for table adam
1010 1011 1012 1013
        self.assertEqual(
            [op.type for op in pserver1.blocks[2].ops],
            ["adam", "scale", "scale"],
        )
1014 1015
        # 3 optimize for table sgd
        self.assertEqual([op.type for op in pserver1.blocks[3].ops], ["sgd"])
1016
        # 4 prefetch -> lookup_sparse_table_read for data0
1017 1018 1019 1020
        self.assertEqual(
            [op.type for op in pserver1.blocks[4].ops],
            ["lookup_sparse_table_read"],
        )
1021 1022
        # 5 save table
        self.assertEqual([op.type for op in pserver1.blocks[5].ops], ["save"])
Q
qiaolongfei 已提交
1023

Q
Qiao Longfei 已提交
1024
        trainer, trainer_startup = self.get_trainer(config)
Q
qiaolongfei 已提交
1025 1026
        self.assertEqual(len(trainer.blocks), 1)
        ops = [
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
            'split_ids',
            'prefetch',
            'merge_ids',
            'sequence_pool',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'concat',
            'mul',
            'elementwise_add',
            'cross_entropy2',
            'mean',
            'fill_constant',
            'mean_grad',
            'cross_entropy_grad2',
            'elementwise_add_grad',
            'send',
            'mul_grad',
            'send',
            'concat_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'split_selected_rows',
            'send',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sum',
            'split_ids',
            'send',
            'recv',
            'recv',
Q
Qiao Longfei 已提交
1060
        ]
Q
qiaolongfei 已提交
1061
        self.assertEqual([op.type for op in trainer.blocks[0].ops], ops)
Q
Qiao Longfei 已提交
1062
        startup_ops = [
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'fill_constant',
            'uniform_random',
            'uniform_random',
            'recv',
            'recv',
            'recv',
            'fetch_barrier',
            'concat',
            'fake_init',
Q
Qiao Longfei 已提交
1085
        ]
1086 1087 1088
        self.assertEqual(
            [op.type for op in trainer_startup.blocks[0].ops], startup_ops
        )
Q
qiaolongfei 已提交
1089 1090


T
tangwei12 已提交
1091
class TestDistLookupTableSliceSize(TestDistLookupTableBase):
T
tangwei12 已提交
1092 1093 1094 1095
    def net_conf(self):
        self.network_with_table(is_sparse=True, is_distributed=True)

    def transpiler_test_impl(self):
1096
        config = paddle.distributed.transpiler.DistributeTranspilerConfig()
T
tangwei12 已提交
1097
        pserver1, _ = self.get_pserver(self.pserver1_ep, config)
T
tangwei12 已提交
1098 1099 1100

        self.assertTrue(self.transpiler.has_distributed_lookup_table)
        lookup_table_var = pserver1.global_block().vars[
1101 1102
            self.transpiler.table_name
        ]
T
tangwei12 已提交
1103 1104 1105
        row_size = lookup_table_var.shape[0]
        calc_row_size = int(math.ceil(self.table_size / self.pservers))
        self.assertEqual(row_size, calc_row_size)
T
tangwei12 已提交
1106 1107


T
tangwei12 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116
class TestDistArgsInProgram(TestDistLookupTableBase):
    def net_conf(self):
        self.network_with_table(is_sparse=True, is_distributed=True)

    def transpiler_test_impl(self):
        trainer, _ = self.get_trainer()

        self.assertTrue(trainer._is_distributed)
        self.assertTrue(trainer._is_chief)
1117 1118 1119 1120 1121 1122
        self.assertEqual(
            trainer._distributed_lookup_table, self.lookup_table_name
        )
        self.assertEqual(
            trainer._endpoints, [self.pserver1_ep, self.pserver2_ep]
        )
T
tangwei12 已提交
1123 1124


W
Wu Yi 已提交
1125 1126
class TestRMSPropOptimizer(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
1127
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
1128 1129
        y_predict = paddle.static.nn.fc(
            x,
1130
            size=1000,
C
Charles-hit 已提交
1131
            weight_attr=fluid.ParamAttr(name='fc_w'),
1132 1133
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
1134
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
1135
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
1136
        avg_cost = paddle.mean(cost)
W
Wu Yi 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145
        optimizer = fluid.optimizer.RMSProp(learning_rate=0.1)
        optimizer.minimize(avg_cost)

    def transpiler_test_impl(self):
        pserver, startup = self.get_pserver(self.pserver1_ep)
        pserver2, startup2 = self.get_pserver(self.pserver2_ep)

        self.assertEqual(len(pserver.blocks), 3)
        # block1~2: optimize pass
1146 1147 1148 1149
        self.assertEqual(
            [op.type for op in pserver.blocks[1].ops],
            ["sum", "scale", "rmsprop"],
        )
W
Wu Yi 已提交
1150 1151 1152 1153 1154 1155 1156
        # the variable #fc_w will be split into two blocks
        fc_w_var = startup.global_block().var("fc_w.block1")
        self.assertEqual(fc_w_var.shape, (500, 1000))
        moment_var = startup.global_block().var("momentum_1")
        self.assertEqual(moment_var.shape, (500, 1000))


T
tangwei12 已提交
1157 1158
class TestLoadSliceVar(TranspilerTest):
    def net_conf(self):
G
GGBond8488 已提交
1159
        x = paddle.static.data(name='x', shape=[-1, 1000], dtype='float32')
C
Charles-hit 已提交
1160 1161
        y_predict = paddle.static.nn.fc(
            x,
1162
            size=1000,
C
Charles-hit 已提交
1163
            weight_attr=fluid.ParamAttr(name='fc_w'),
1164 1165
            bias_attr=fluid.ParamAttr(name='fc_b'),
        )
G
GGBond8488 已提交
1166
        y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32')
1167
        cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y)
1168
        avg_cost = paddle.mean(cost)
T
tangwei12 已提交
1169 1170 1171 1172 1173 1174 1175
        optimizer = fluid.optimizer.RMSProp(learning_rate=0.1)
        optimizer.minimize(avg_cost)

    def transpiler_test_impl(self):
        pserver, _ = self.get_pserver(self.pserver1_ep)
        pserver2, _ = self.get_pserver(self.pserver2_ep)

1176
        vars_ps1 = pserver._parameters_on_pservers.get_distributed_vars_by_ep(
1177 1178
            self.pserver1_ep
        )
1179
        vars_ps2 = pserver._parameters_on_pservers.get_distributed_vars_by_ep(
1180 1181
            self.pserver2_ep
        )
1182 1183 1184 1185

        self.assertTrue(vars_ps1)
        self.assertTrue(vars_ps2)

1186
        for idx in range(len(vars_ps1)):
1187 1188 1189 1190 1191 1192
            total_numel = 0
            ps1_numel, ps2_numel = 0, 0

            ps1_var = vars_ps1[idx]

            if not ps1_var.is_slice:
1193 1194 1195 1196 1197 1198
                total_numel = functools.reduce(
                    lambda x, y: x * y, vars_ps1[idx].origin.shape
                )
                ps1_numel = functools.reduce(
                    lambda x, y: x * y, vars_ps1[idx].slice.shape
                )
1199 1200 1201 1202 1203 1204 1205
            else:
                ps2_var = None
                for var in vars_ps2:
                    if var.origin.name == ps1_var.origin.name:
                        ps2_var = var
                        break

1206 1207 1208 1209 1210 1211 1212 1213 1214
                total_numel = functools.reduce(
                    lambda x, y: x * y, ps1_var.origin.shape
                )
                ps1_numel = functools.reduce(
                    lambda x, y: x * y, ps1_var.slice.shape
                )
                ps2_numel = functools.reduce(
                    lambda x, y: x * y, ps2_var.slice.shape
                )
1215 1216

            self.assertEqual(total_numel, ps1_numel + ps2_numel)
T
tangwei12 已提交
1217 1218


W
Wu Yi 已提交
1219 1220
class TestNCCL2Transpile(TranspilerTest):
    def test_nccl2_transpile(self):
T
tangwei12 已提交
1221
        if fluid.core.is_compiled_with_cuda():  # test nccl2 only with cuda
J
JiabinYang 已提交
1222 1223 1224 1225 1226
            main = fluid.Program()
            startup = fluid.Program()
            with fluid.program_guard(main, startup):
                self.net_conf()

1227
            config = paddle.distributed.transpiler.DistributeTranspilerConfig()
J
JiabinYang 已提交
1228
            config.mode = "nccl2"
W
Wu Yi 已提交
1229
            config.wait_port = False
1230 1231 1232
            t = paddle.distributed.transpiler.DistributeTranspiler(
                config=config
            )
1233 1234 1235 1236 1237 1238
            t.transpile(
                0,
                trainers="127.0.0.1:6174,127.0.0.1:6175",
                current_endpoint="127.0.0.1:6174",
                startup_program=startup,
            )
J
JiabinYang 已提交
1239 1240 1241
            print([op.type for op in startup.global_block().ops])
            self.assertEqual(startup.global_block().ops[-1].type, "gen_nccl_id")
            self.assertIsNotNone(startup.global_block().vars.get("NCCLID"))
1242
            gc.collect()
J
JiabinYang 已提交
1243 1244
        else:
            pass
W
Wu Yi 已提交
1245 1246


Q
Qiao Longfei 已提交
1247 1248 1249
# test for remote prefetch
class TestRemoteLookupTable(TestDistLookupTableBase):
    def net_conf(self):
1250
        import os
1251

1252
        os.environ['PADDLE_ENABLE_REMOTE_PREFETCH'] = "1"
Q
Qiao Longfei 已提交
1253
        self.network_with_table(is_sparse=True, is_distributed=False)
Q
Qiao Longfei 已提交
1254 1255 1256 1257 1258 1259 1260

    def transpiler_test_impl(self):
        pserver1, startup1 = self.get_pserver(self.pserver1_ep)

        self.assertEqual(len(pserver1.blocks), 4)
        # 0 listen_and_serv
        # 1 optimize for fc_w or fc_b adam
1261 1262 1263 1264
        self.assertEqual(
            [op.type for op in pserver1.blocks[1].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
Q
Qiao Longfei 已提交
1265 1266
        # 2 optimize for table adam
        # NOTE: if param is not selected rows, the grad will scaled to grad / trainer_num
1267 1268 1269 1270
        self.assertEqual(
            [op.type for op in pserver1.blocks[2].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
Q
Qiao Longfei 已提交
1271 1272 1273

        # 3 optimize for table 2 adam
        # NOTE: if param is not selected rows, the grad will scaled to grad / trainer_num
1274 1275 1276 1277
        self.assertEqual(
            [op.type for op in pserver1.blocks[3].ops],
            ["sum", "scale", "adam", "scale", "scale"],
        )
Q
Qiao Longfei 已提交
1278 1279 1280 1281

        trainer, _ = self.get_trainer()
        self.assertEqual(len(trainer.blocks), 1)
        ops = [
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
            'lookup_table',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'lookup_table',
            'sequence_pool',
            'concat',
            'mul',
            'elementwise_add',
            'cross_entropy2',
            'mean',
            'fill_constant',
            'mean_grad',
            'cross_entropy_grad2',
            'elementwise_add_grad',
            'send',
            'mul_grad',
            'send',
            'concat_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'split_selected_rows',
            'send',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sequence_pool_grad',
            'lookup_table_grad',
            'sum',
            'split_selected_rows',
            'send',
            'send_barrier',
            'recv',
            'recv',
            'fetch_barrier',
Q
Qiao Longfei 已提交
1316 1317 1318 1319
        ]
        self.assertEqual([op.type for op in trainer.blocks[0].ops], ops)


1320 1321 1322 1323 1324 1325 1326
# test for remote prefetch
class TestRemoteNce(TestDistLookupTableBase):
    def network_with_table(self, is_sparse, is_distributed):
        num_total_classes = 20
        sampler = "uniform"
        nid_freq_arr = np.random.dirichlet(np.ones(20) * 1000).astype('float32')

G
GGBond8488 已提交
1327 1328 1329 1330
        input = paddle.static.data(
            name="input", shape=[-1, 10], dtype="float32"
        )
        label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64")
1331

1332 1333 1334 1335 1336 1337 1338
        w_param = (
            fluid.default_main_program()
            .global_block()
            .create_parameter(
                shape=[num_total_classes, 10],
                dtype='float32',
                name='nce_w',
1339
                initializer=paddle.nn.initializer.Constant(),
1340 1341 1342 1343 1344 1345 1346 1347 1348
            )
        )
        b_param = (
            fluid.default_main_program()
            .global_block()
            .create_parameter(
                shape=[num_total_classes, 1],
                dtype='float32',
                name='nce_b',
1349
                initializer=paddle.nn.initializer.Constant(),
1350 1351 1352
            )
        )

1353
        cost = paddle.static.nn.nce(
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
            input=input,
            label=label,
            num_total_classes=num_total_classes,
            sampler=sampler,
            custom_dist=nid_freq_arr.tolist(),
            sample_weight=None,
            param_attr='nce_w',
            bias_attr='nce_b',
            seed=1,
            num_neg_samples=5,
            is_sparse=is_sparse,
        )
1366
        avg_cost = paddle.mean(cost)
1367 1368 1369 1370 1371 1372
        # optimizer
        optimizer = fluid.optimizer.Adam(learning_rate=0.003)
        optimizer.minimize(avg_cost)

    def net_conf(self):
        import os
1373

1374 1375 1376 1377 1378
        os.environ['PADDLE_ENABLE_REMOTE_PREFETCH'] = "1"
        self.network_with_table(is_sparse=True, is_distributed=False)

    def transpiler_test_impl(self):
        trainer, _ = self.get_trainer()
T
tangwei12 已提交
1379

1380 1381
        out_vars = ["nce_w"]
        in_vars = ["nce_b"]
T
tangwei12 已提交
1382 1383 1384

        recv_var_names = []

1385 1386
        for op in trainer.blocks[0].ops:
            if op.type == "recv":
T
tangwei12 已提交
1387 1388 1389 1390 1391 1392 1393
                for var in op.output("Out"):
                    recv_var_names.append(var)

        for out_var in out_vars:
            self.assertFalse(out_var in recv_var_names)
        for in_var in in_vars:
            self.assertTrue(in_var in recv_var_names)
1394 1395


J
JiabinYang 已提交
1396 1397 1398
# test for remote prefetch
class TestRemoteHsigmoid(TestDistLookupTableBase):
    def network_with_table(self, is_sparse, is_distributed):
1399
        num_total_classes = 3
J
JiabinYang 已提交
1400

G
GGBond8488 已提交
1401 1402 1403 1404
        input = paddle.static.data(name="input", shape=[-1, 1], dtype="float32")
        label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64")
        path_table = paddle.static.data(
            name='path_table', shape=[-1, 3], dtype='int64'
1405
        )
G
GGBond8488 已提交
1406 1407
        path_code = paddle.static.data(
            name='path_code', shape=[-1, 3], dtype='int64'
1408 1409 1410 1411 1412 1413 1414 1415
        )
        w_param = (
            fluid.default_main_program()
            .global_block()
            .create_parameter(
                shape=[num_total_classes, 10],
                dtype='float32',
                name='hs_w',
1416
                initializer=paddle.nn.initializer.Constant(),
1417 1418 1419 1420 1421 1422 1423 1424 1425
            )
        )
        b_param = (
            fluid.default_main_program()
            .global_block()
            .create_parameter(
                shape=[3, 1],
                dtype='float32',
                name='hs_b',
1426
                initializer=paddle.nn.initializer.Constant(),
1427 1428
            )
        )
J
JiabinYang 已提交
1429

1430
        emb = fluid.layers.embedding(
J
JiabinYang 已提交
1431
            input=input,
1432 1433
            is_sparse=is_sparse,
            size=[3, 3],
1434
            param_attr=fluid.ParamAttr(
1435
                initializer=paddle.nn.initializer.Normal(
1436 1437 1438 1439 1440
                    scale=1 / math.sqrt(num_total_classes)
                )
            ),
        )

1441 1442 1443 1444 1445 1446 1447 1448
        loss = paddle.nn.HSigmoidLoss(
            feature_size=emb.shape[1],
            num_classes=num_total_classes,
            is_custom=True,
            is_sparse=is_sparse,
        )

        cost = loss(
1449 1450 1451 1452 1453
            input=emb,
            label=label,
            path_table=path_table,
            path_code=path_code,
        )
1454

1455
        avg_cost = paddle.mean(cost)
J
JiabinYang 已提交
1456 1457 1458 1459 1460 1461
        # optimizer
        optimizer = fluid.optimizer.SGD(learning_rate=0.003)
        optimizer.minimize(avg_cost)

    def net_conf(self):
        import os
1462

J
JiabinYang 已提交
1463 1464 1465 1466 1467
        os.environ['PADDLE_ENABLE_REMOTE_PREFETCH'] = "1"
        self.network_with_table(is_sparse=True, is_distributed=False)

    def transpiler_test_impl(self):
        trainer, _ = self.get_trainer()
1468
        params_to_check = []
J
JiabinYang 已提交
1469
        for op in trainer.blocks[0].ops:
1470 1471 1472 1473 1474
            if op.type == "hierarchical_sigmoid":
                params_to_check = [op.input("W")[0], op.input("Bias")[0]]
                for name in ["epmap", "table_names", "epmap"]:
                    assert op.has_attr(name)
                    if name == "epmap":
1475
                        assert op.attr(name)[0] == '127.0.0.1:6174'
1476
                    elif name == "table_names":
1477
                        assert op.attr(name)[0] == 'hierarchical_sigmoid_0.w_0'
1478 1479 1480 1481 1482
                    else:
                        assert op.attr(name) == 3
            elif op.type == "lookup_table":
                params_to_check.append(op.input("W")[0])
            else:
J
JiabinYang 已提交
1483
                pass
1484 1485 1486 1487
        op_count = 0
        for op in trainer.blocks[0].ops:
            if op.type == "recv":
                assert len(op.output("Out")) == 1
1488
                assert op.output("Out")[0] == 'hierarchical_sigmoid_0.b_0'
1489 1490
                op_count += 1
        assert op_count == 1
J
JiabinYang 已提交
1491 1492


Y
Yancey 已提交
1493 1494
if __name__ == "__main__":
    unittest.main()