env.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import random
  19. import numpy as np
  20. import paddle
  21. from paddle.distributed import fleet
  22. __all__ = ['init_parallel_env', 'set_random_seed', 'init_fleet_env']
  23. def init_fleet_env():
  24. fleet.init(is_collective=True)
  25. def init_parallel_env():
  26. env = os.environ
  27. dist = 'PADDLE_TRAINER_ID' in env and 'PADDLE_TRAINERS_NUM' in env
  28. if dist:
  29. trainer_id = int(env['PADDLE_TRAINER_ID'])
  30. local_seed = (99 + trainer_id)
  31. random.seed(local_seed)
  32. np.random.seed(local_seed)
  33. paddle.distributed.init_parallel_env()
  34. def set_random_seed(seed):
  35. random.seed(seed)
  36. np.random.seed(seed)