test_boxps.py 3.5 KB
Newer Older
H
hutuxian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2019 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
import unittest

H
hutuxian 已提交
17
import paddle
H
hutuxian 已提交
18 19 20
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.layers.nn import _pull_box_sparse
H
hutuxian 已提交
21 22 23 24
from paddle.fluid.transpiler import collective


class TestTranspile(unittest.TestCase):
25
    """TestCases for BoxPS Preload"""
H
hutuxian 已提交
26 27 28 29 30 31 32 33 34 35 36 37

    def get_transpile(self, mode, trainers="127.0.0.1:6174"):
        config = fluid.DistributeTranspilerConfig()
        config.mode = 'collective'
        config.collective_mode = mode
        t = fluid.DistributeTranspiler(config=config)
        return t

    def test_transpile(self):
        main_program = fluid.Program()
        startup_program = fluid.Program()
        t = self.get_transpile("single_process_multi_thread")
38 39 40 41 42 43
        t.transpile(
            trainer_id=0,
            startup_program=startup_program,
            trainers="127.0.0.1:6174",
            program=main_program,
        )
H
hutuxian 已提交
44 45
        t = self.get_transpile("grad_allreduce")
        try:
46 47 48 49 50 51
            t.transpile(
                trainer_id=0,
                startup_program=startup_program,
                trainers="127.0.0.1:6174",
                program=main_program,
            )
H
hutuxian 已提交
52 53 54 55 56 57
        except ValueError as e:
            print(e)

    def test_single_trainers(self):
        transpiler = collective.GradAllReduce(0)
        try:
58 59 60 61 62 63 64 65
            transpiler.transpile(
                startup_program=fluid.Program(),
                main_program=fluid.Program(),
                rank=1,
                endpoints="127.0.0.1:6174",
                current_endpoint="127.0.0.1:6174",
                wait_port="6174",
            )
H
hutuxian 已提交
66 67 68 69
        except ValueError as e:
            print(e)
        transpiler = collective.LocalSGD(0)
        try:
70 71 72 73 74 75 76 77
            transpiler.transpile(
                startup_program=fluid.Program(),
                main_program=fluid.Program(),
                rank=1,
                endpoints="127.0.0.1:6174",
                current_endpoint="127.0.0.1:6174",
                wait_port="6174",
            )
H
hutuxian 已提交
78 79
        except ValueError as e:
            print(e)
H
hutuxian 已提交
80 81


H
hutuxian 已提交
82
class TestRunCmd(unittest.TestCase):
83
    """TestCases for run_cmd"""
H
hutuxian 已提交
84 85 86 87 88 89 90 91

    def test_run_cmd(self):
        ret1 = int(core.run_cmd("ls; echo $?").strip().split('\n')[-1])
        ret2 = int(core.run_cmd("ls; echo $?", -1, -1).strip().split('\n')[-1])
        self.assertTrue(ret1 == 0)
        self.assertTrue(ret2 == 0)


H
hutuxian 已提交
92
class TestPullBoxSparseOP(unittest.TestCase):
93
    """TestCases for _pull_box_sparse op"""
H
hutuxian 已提交
94 95 96 97 98

    def test_pull_box_sparse_op(self):
        paddle.enable_static()
        program = fluid.Program()
        with fluid.program_guard(program):
G
GGBond8488 已提交
99 100
            x = paddle.static.data(
                name='x', shape=[-1, 1], dtype='int64', lod_level=0
101
            )
G
GGBond8488 已提交
102 103
            y = paddle.static.data(
                name='y', shape=[-1, 1], dtype='int64', lod_level=0
104
            )
H
hutuxian 已提交
105 106 107
            emb_x, emb_y = _pull_box_sparse([x, y], size=1)


H
hutuxian 已提交
108 109
if __name__ == '__main__':
    unittest.main()