From 5e0227dc38145add2f411f5796c661e8295e64db Mon Sep 17 00:00:00 2001 From: guofei <52460041+gfwm2013@users.noreply.github.com> Date: Tue, 22 Sep 2020 23:20:26 +0800 Subject: [PATCH] [cherry-pick 1.8] Fix the unittests to surpport python3.8 (#27451) test=release/1.8 --- .../fluid/incubate/fleet/base/role_maker.py | 17 ++++++++++++----- .../fluid/tests/unittests/test_device_guard.py | 18 ++++++++++++++++-- .../unittests/test_framework_debug_str.py | 2 +- .../unittests/test_save_model_without_var.py | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/python/paddle/fluid/incubate/fleet/base/role_maker.py b/python/paddle/fluid/incubate/fleet/base/role_maker.py index b9cd73d1584..9f3c7a8fd33 100644 --- a/python/paddle/fluid/incubate/fleet/base/role_maker.py +++ b/python/paddle/fluid/incubate/fleet/base/role_maker.py @@ -14,9 +14,10 @@ """Defination of Role Makers.""" from __future__ import print_function -from multiprocessing import Process, Manager +import multiprocessing import paddle.fluid as fluid import os +import sys import time __all__ = [ @@ -602,7 +603,7 @@ class GeneralRoleMaker(RoleMakerBase): if ip_port != "": self._http_ip_port = ip_port.split(":") # it's for communication between processes - self._manager = Manager() + self._manager = multiprocessing.Manager() # global dict to store status self._http_server_d = self._manager.dict() # set running status of http server @@ -636,9 +637,15 @@ class GeneralRoleMaker(RoleMakerBase): "all": len(worker_endpoints) + len(eplist) } # child process for http server - self._http_server = Process( - target=self.__start_kv_server, - args=(self._http_server_d, size_d)) + if sys.version_info >= (3, 8) and sys.platform == 'darwin': + self._http_server = multiprocessing.get_context( + 'fork').Process( + target=self.__start_kv_server, + args=(self._http_server_d, size_d)) + else: + self._http_server = multiprocessing.Process( + target=self.__start_kv_server, + args=(self._http_server_d, size_d)) self._http_server.daemon = True # set running status to True self._http_server_d["running"] = True diff --git a/python/paddle/fluid/tests/unittests/test_device_guard.py b/python/paddle/fluid/tests/unittests/test_device_guard.py index 7b05f6b0f24..681501d8d9d 100644 --- a/python/paddle/fluid/tests/unittests/test_device_guard.py +++ b/python/paddle/fluid/tests/unittests/test_device_guard.py @@ -33,6 +33,14 @@ def execute(main_program, startup_program): exe.run(main_program) +def get_vaild_warning_num(warning, w): + num = 0 + for i in range(len(w)): + if warning in str(w[i].message): + num += 1 + return num + + class TestDeviceGuard(unittest.TestCase): def test_device_guard(self): main_program = fluid.Program() @@ -108,7 +116,10 @@ class TestDeviceGuard(unittest.TestCase): i = fluid.layers.increment(x=i, value=1, in_place=True) fluid.layers.less_than(x=i, y=loop_len, cond=cond) - assert len(w) == 1 + warning = "The Op(while) is not support to set device." + warning_num = get_vaild_warning_num(warning, w) + assert warning_num == 1 + all_ops = main_program.global_block().ops device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName() for op in all_ops: @@ -138,7 +149,10 @@ class TestDeviceGuard(unittest.TestCase): shape=[1], value=4.0, dtype='float32') result = fluid.layers.less_than(x=x, y=y, force_cpu=False) - assert len(w) == 2 + warning = "\'device_guard\' has higher priority when they are used at the same time." + warning_num = get_vaild_warning_num(warning, w) + assert warning_num == 2 + all_ops = main_program.global_block().ops device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName() for op in all_ops: diff --git a/python/paddle/fluid/tests/unittests/test_framework_debug_str.py b/python/paddle/fluid/tests/unittests/test_framework_debug_str.py index 72f43e56ccb..6511b56b5e8 100644 --- a/python/paddle/fluid/tests/unittests/test_framework_debug_str.py +++ b/python/paddle/fluid/tests/unittests/test_framework_debug_str.py @@ -22,7 +22,7 @@ class TestDebugStringFramework(unittest.TestCase): def test_debug_str(self): p = Program() p.current_block().create_var(name='t', shape=[0, 1]) - self.assertRaises(ValueError, callableObj=p.__str__) + self.assertRaises(ValueError, p.to_string, True) if __name__ == '__main__': diff --git a/python/paddle/fluid/tests/unittests/test_save_model_without_var.py b/python/paddle/fluid/tests/unittests/test_save_model_without_var.py index b74a6e10917..4c63dced83b 100644 --- a/python/paddle/fluid/tests/unittests/test_save_model_without_var.py +++ b/python/paddle/fluid/tests/unittests/test_save_model_without_var.py @@ -50,7 +50,7 @@ class TestSaveModelWithoutVar(unittest.TestCase): params_filename='params') expected_warn = "no variable in your model, please ensure there are any variables in your model to save" self.assertTrue(len(w) > 0) - self.assertTrue(expected_warn == str(w[0].message)) + self.assertTrue(expected_warn == str(w[-1].message)) if __name__ == '__main__': -- GitLab