From 17ff46b6a3f4e9e2c2df65d1cb842aded667e274 Mon Sep 17 00:00:00 2001 From: 0YuanZhang0 <953963890@qq.com> Date: Mon, 20 Apr 2020 19:37:44 +0800 Subject: [PATCH] Cherry-pick Manual seed op into paddle.framework (#23537) add manual_seed op into paddle.framework.random --- python/paddle/__init__.py | 3 +- python/paddle/fluid/framework.py | 32 ++++++++++++- .../fluid/tests/unittests/test_manual_seed.py | 45 +++++++++++++++++++ python/paddle/framework/__init__.py | 3 ++ python/paddle/framework/random.py | 26 ++++++++++- python/setup.py.in | 1 + 6 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 python/paddle/fluid/tests/unittests/test_manual_seed.py diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 81ecb448ecf..2dd755ecafd 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -36,6 +36,7 @@ batch = batch.batch import paddle.sysconfig import paddle.tensor import paddle.nn +import paddle.framework import paddle.imperative # TODO: define alias in tensor and framework directory @@ -198,7 +199,7 @@ from .tensor.search import nonzero #DEFINE_ALIAS from .tensor.search import sort #DEFINE_ALIAS # from .framework.framework import set_default_dtype #DEFINE_ALIAS # from .framework.framework import get_default_dtype #DEFINE_ALIAS -# from .framework.random import manual_seed #DEFINE_ALIAS +from .framework.random import manual_seed #DEFINE_ALIAS # from .framework import append_backward #DEFINE_ALIAS # from .framework import gradients #DEFINE_ALIAS # from .framework import Executor #DEFINE_ALIAS diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 50f26c45df0..f5e26711d26 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -66,6 +66,8 @@ _dygraph_tracer_ = None _dygraph_current_expected_place_ = None _current_device = None +global_prog_seed = 0 + def require_version(min_version, max_version=None): """ @@ -3653,7 +3655,8 @@ class Program(object): self.desc = core.ProgramDesc() self.blocks = [Block(self, 0)] self.current_block_idx = 0 - self._seed = 0 + global global_prog_seed + self._seed = global_prog_seed self._current_role = core.op_proto_and_checker_maker.OpRole.Forward self.__op_role_var = [] @@ -3692,6 +3695,33 @@ class Program(object): # appending gradients times self._appending_grad_times = 0 + def global_seed(self, seed=0): + """ + Set global seed for Program + + Returns: + None. + + Examples: + .. code-block:: python + + import paddle.fluid as fluid + + prog = fluid.default_main_program() + print(prog.random_seed) + ## 0 + ## the default random seed is 0 + + prog.global_seed(102) + prog1 = fluid.default_main_program() + print(prog1.random_seed) + ## 102 + ## the random seed is 102 + """ + global global_prog_seed + global_prog_seed = seed + self._seed = global_prog_seed + @property def _op_role(self): """ diff --git a/python/paddle/fluid/tests/unittests/test_manual_seed.py b/python/paddle/fluid/tests/unittests/test_manual_seed.py new file mode 100644 index 00000000000..747026622e4 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_manual_seed.py @@ -0,0 +1,45 @@ +# 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. + +from __future__ import print_function +import unittest + +import paddle.fluid as fluid +from paddle.framework import manual_seed +from paddle.fluid.framework import Program, default_main_program, default_startup_program + + +class TestManualSeed(unittest.TestCase): + def test_manual_seed(self): + local_program = Program() + local_main_prog = default_main_program() + local_start_prog = default_startup_program() + + self.assertEqual(0, local_program.random_seed) + self.assertEqual(0, local_main_prog.random_seed) + self.assertEqual(0, local_start_prog.random_seed) + + manual_seed(102) + global_program1 = Program() + global_program2 = Program() + global_main_prog = default_main_program() + global_start_prog = default_startup_program() + self.assertEqual(102, global_program1.random_seed) + self.assertEqual(102, global_program2.random_seed) + self.assertEqual(102, global_main_prog.random_seed) + self.assertEqual(102, global_start_prog.random_seed) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/framework/__init__.py b/python/paddle/framework/__init__.py index 0932fe46c59..873b22f4f4d 100644 --- a/python/paddle/framework/__init__.py +++ b/python/paddle/framework/__init__.py @@ -38,3 +38,6 @@ # 'WeightNormParamAttr', # 'Model', # 'Sequential'] + +from . import random +from .random import manual_seed diff --git a/python/paddle/framework/random.py b/python/paddle/framework/random.py index 7b3be421e47..c0e7b3e21dd 100644 --- a/python/paddle/framework/random.py +++ b/python/paddle/framework/random.py @@ -13,4 +13,28 @@ # limitations under the License. # TODO: define random api -# __all__ = ['manual_seed'] +import paddle.fluid as fluid + +__all__ = ['manual_seed'] + + +def manual_seed(seed): + """ + Set global manual seed for program + + Args: + manual_seed(int): random seed for program + + Returns: + None. + + Examples: + .. code-block:: python + + from paddle.framework import manual_seed + manual_seed(102) + """ + fluid.default_main_program().random_seed = seed + fluid.default_startup_program().random_seed = seed + program = fluid.Program() + program.global_seed(seed) diff --git a/python/setup.py.in b/python/setup.py.in index 76776ab50a1..36851adde19 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -141,6 +141,7 @@ packages=['paddle', 'paddle.distributed', 'paddle.fluid', 'paddle.tensor', + 'paddle.framework', 'paddle.fluid.dygraph', 'paddle.tensor', 'paddle.fluid.dygraph.dygraph_to_static', -- GitLab