-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Wang, Yi A <[email protected]>
- Loading branch information
Showing
2 changed files
with
85 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright 2022 The HuggingFace Team. 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. | ||
|
||
""" | ||
A set of basic tensor ops compatible with tpu, gpu, and multigpu | ||
""" | ||
|
||
|
||
import torch | ||
from accelerate.utils.operations import _gpu_broadcast, is_tensor_information, recursively_apply | ||
|
||
from ..state import GaudiPartialState | ||
from ..utils import GaudiDistributedType | ||
|
||
|
||
def initialize_tensors(data_structure): | ||
""" | ||
Recursively initializes tensors from a nested list/tuple/dictionary of [`~utils.TensorInformation`]. | ||
Returns: | ||
The same data structure as `data` with tensors instead of [`~utils.TensorInformation`]. | ||
""" | ||
|
||
def _initialize_tensor(tensor_info): | ||
return torch.zeros(*tensor_info.shape, dtype=tensor_info.dtype) | ||
|
||
return recursively_apply(_initialize_tensor, data_structure, test_type=is_tensor_information) | ||
|
||
|
||
def broadcast(tensor, from_process: int = 0): | ||
""" | ||
Recursively broadcast tensor in a nested list/tuple/dictionary of tensors to all devices. | ||
Args: | ||
tensor (nested list/tuple/dictionary of `torch.Tensor`): | ||
The data to gather. | ||
from_process (`int`, *optional*, defaults to 0): | ||
The process from which to send the data | ||
Returns: | ||
The same data structure as `tensor` with all tensors broadcasted to the proper device. | ||
""" | ||
if GaudiPartialState().distributed_type in [GaudiDistributedType.MULTI_HPU, GaudiDistributedType.DEEPSPEED]: | ||
return _gpu_broadcast(tensor, src=from_process) | ||
return tensor | ||
|
||
|
||
def broadcast_object_list(object_list, from_process: int = 0): | ||
""" | ||
Broadcast a list of picklable objects form one process to the others. | ||
Args: | ||
object_list (list of picklable objects): | ||
The list of objects to broadcast. This list will be modified inplace. | ||
from_process (`int`, *optional*, defaults to 0): | ||
The process from which to send the data. | ||
Returns: | ||
The same list containing the objects from process 0. | ||
""" | ||
if GaudiPartialState().distributed_type in [GaudiDistributedType.MULTI_HPU, GaudiDistributedType.DEEPSPEED]: | ||
torch.distributed.broadcast_object_list(object_list, src=from_process, device="hpu") | ||
return object_list |