





'''simple docstring'''






# Copyright 2023 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.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      return (torch.arange(state.num_processes    ) + 1.0 + (state.num_processes * state.process_index)).to(state.device    )
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      _lowerCAmelCase					=				create_tensor(lowerCAmelCase    )
      _lowerCAmelCase					=				gather(lowerCAmelCase    )
      assert gathered_tensor.tolist() == list(range(1				,      state.num_processes**2 + 1    )    )
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      _lowerCAmelCase					=				[state.process_index]
      _lowerCAmelCase					=				gather_object(lowerCAmelCase    )
      assert len(lowerCAmelCase    ) == state.num_processes, f"{gathered_obj}, {len(lowerCAmelCase    )} != {state.num_processes}"
      assert gathered_obj == list(range(state.num_processes    )    ), f"{gathered_obj} != {list(range(state.num_processes    )    )}"
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      _lowerCAmelCase					=				create_tensor(lowerCAmelCase    )
      _lowerCAmelCase					=				broadcast(lowerCAmelCase    )
      assert broadcasted_tensor.shape == torch.Size([state.num_processes]    )
      assert broadcasted_tensor.tolist() == list(range(1				,      state.num_processes + 1    )    )
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      if state.is_main_process:
            _lowerCAmelCase					=				torch.arange(state.num_processes + 1    ).to(state.device    )
      else:
            _lowerCAmelCase					=				torch.arange(state.num_processes    ).to(state.device    )
      _lowerCAmelCase					=				pad_across_processes(lowerCAmelCase    )
      assert padded_tensor.shape == torch.Size([state.num_processes + 1]    )
      if not state.is_main_process:
            assert padded_tensor.tolist() == list(range(0				,      state.num_processes    )    ) + [0]
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      if state.num_processes != 2:
            return
      _lowerCAmelCase					=				create_tensor(lowerCAmelCase    )
      _lowerCAmelCase					=				reduce(lowerCAmelCase				,      """sum"""    )
      _lowerCAmelCase					=				torch.tensor([4.0, 6]    ).to(state.device    )
      assert torch.allclose(lowerCAmelCase				,      lowerCAmelCase    ), f"{reduced_tensor} != {truth_tensor}"
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      if state.num_processes != 2:
            return
      _lowerCAmelCase					=				create_tensor(lowerCAmelCase    )
      _lowerCAmelCase					=				reduce(lowerCAmelCase				,      """mean"""    )
      _lowerCAmelCase					=				torch.tensor([2.0, 3]    ).to(state.device    )
      assert torch.allclose(lowerCAmelCase				,      lowerCAmelCase    ), f"{reduced_tensor} != {truth_tensor}"
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      main()
def   UpperCamelCase__ (						):





      """simple docstring"""


      _lowerCAmelCase					=				PartialState()
      state.print(f"State: {state}"    )
      state.print("""testing gather"""    )
      test_gather(lowerCAmelCase    )
      state.print("""testing gather_object"""    )
      test_gather_object(lowerCAmelCase    )
      state.print("""testing broadcast"""    )
      test_broadcast(lowerCAmelCase    )
      state.print("""testing pad_across_processes"""    )
      test_pad_across_processes(lowerCAmelCase    )
      state.print("""testing reduce_sum"""    )
      test_reduce_sum(lowerCAmelCase    )
      state.print("""testing reduce_mean"""    )
      test_reduce_mean(lowerCAmelCase    )


if __name__ == "__main__":
      main()













'''simple docstring'''






import os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
A__							:  Optional[int]    ='''.'''


if __name__ == "__main__":
      A__							:  int    =os.path.join(REPO_PATH, '''utils/documentation_tests.txt''')
      A__							:  Optional[int]    =[]
      A__							:  Tuple    =[]
      with open(doctest_file_path) as fp:
            for line in fp:
                  A__							:  Optional[Any]    =line.strip()
                  A__							:  Union[str, Any]    =os.path.join(REPO_PATH, line)
                  if not (os.path.isfile(path) or os.path.isdir(path)):
                        non_existent_paths.append(line)
                  all_paths.append(path)
      if len(non_existent_paths) > 0:
            A__							:  List[Any]    ='''\n'''.join(non_existent_paths)
            raise ValueError(F"""`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}""")
      if all_paths != sorted(all_paths):
            raise ValueError('''Files in `utils/documentation_tests.txt` are not in alphabetical order.''')






