2019-05-24 13:20:41 +08:00
|
|
|
# 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 jin 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.
|
|
|
|
|
import os
|
2023-03-02 18:57:07 +08:00
|
|
|
|
2019-05-28 21:31:28 +08:00
|
|
|
from ..framework import Parameter
|
2022-06-05 10:58:58 +08:00
|
|
|
|
2019-05-24 13:20:41 +08:00
|
|
|
__parallel_ctx__clz__ = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _is_data_parallel_mode():
|
|
|
|
|
global __parallel_ctx__clz__
|
2022-10-23 20:01:27 +08:00
|
|
|
return (
|
|
|
|
|
__parallel_ctx__clz__ is not None
|
|
|
|
|
and int(os.getenv("PADDLE_TRAINERS_NUM", "1")) > 1
|
|
|
|
|
)
|
2019-05-24 13:20:41 +08:00
|
|
|
|
|
|
|
|
|
2020-08-28 14:46:28 +08:00
|
|
|
def _is_parallel_ctx_initialized():
|
|
|
|
|
global __parallel_ctx__clz__
|
|
|
|
|
return __parallel_ctx__clz__ is not None
|
|
|
|
|
|
|
|
|
|
|
2021-12-06 09:01:15 +08:00
|
|
|
def _set_parallel_ctx(ccl_parallel_context):
|
2019-05-24 13:20:41 +08:00
|
|
|
global __parallel_ctx__clz__
|
2025-08-21 02:00:58 +08:00
|
|
|
assert __parallel_ctx__clz__ is None, (
|
|
|
|
|
"ParallelContext can only be initialized once."
|
|
|
|
|
)
|
2021-12-06 09:01:15 +08:00
|
|
|
__parallel_ctx__clz__ = ccl_parallel_context
|
2019-05-24 13:20:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _init_parallel_ctx():
|
|
|
|
|
global __parallel_ctx__clz__
|
2025-08-21 02:00:58 +08:00
|
|
|
assert __parallel_ctx__clz__ is not None, (
|
|
|
|
|
"ParallelContext should be initialized."
|
|
|
|
|
)
|
2019-05-24 13:20:41 +08:00
|
|
|
__parallel_ctx__clz__.init()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _broadcast_parameters(parameters):
|
2023-03-02 18:57:07 +08:00
|
|
|
from ..distributed import broadcast
|
|
|
|
|
|
2019-05-24 13:20:41 +08:00
|
|
|
for param in parameters:
|
2020-12-31 13:13:44 +08:00
|
|
|
# In model parallel, some parameters are split into multiple devices,
|
|
|
|
|
# so we could not broadcast these parameters.
|
2022-10-23 20:01:27 +08:00
|
|
|
if param.is_distributed:
|
|
|
|
|
continue
|
2019-05-28 21:31:28 +08:00
|
|
|
if isinstance(param, Parameter) and param.trainable:
|
2023-03-02 18:57:07 +08:00
|
|
|
broadcast(param, 0, sync_op=True)
|