"""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 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	Union[str, Any]     )	->				int:
  return (torch.arange(state.num_processes     ) + 1.0 + (state.num_processes * state.process_index)).to(state.device     )





def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	List[str]     )	->				Union[str, Any]:
  _lowerCAmelCase						:	List[Any]        =					create_tensor(SCREAMING_SNAKE_CASE_     )
  _lowerCAmelCase						:	int        =					gather(SCREAMING_SNAKE_CASE_     )
  assert gathered_tensor.tolist() == list(range(1      ,state.num_processes**2 + 1     )     )





def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	Any     )	->				List[str]:
  _lowerCAmelCase						:	Dict        =					[state.process_index]
  _lowerCAmelCase						:	Optional[int]        =					gather_object(SCREAMING_SNAKE_CASE_     )
  assert len(SCREAMING_SNAKE_CASE_     ) == state.num_processes, f"{gathered_obj}, {len(SCREAMING_SNAKE_CASE_     )} != {state.num_processes}"
  assert gathered_obj == list(range(state.num_processes     )     ), f"{gathered_obj} != {list(range(state.num_processes     )     )}"





def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	Tuple     )	->				Optional[int]:
  _lowerCAmelCase						:	Optional[Any]        =					create_tensor(SCREAMING_SNAKE_CASE_     )
  _lowerCAmelCase						:	str        =					broadcast(SCREAMING_SNAKE_CASE_     )
  assert broadcasted_tensor.shape == torch.Size([state.num_processes]     )
  assert broadcasted_tensor.tolist() == list(range(1      ,state.num_processes + 1     )     )





def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	int     )	->				int:






  if state.is_main_process:
    _lowerCAmelCase						:	str        =					torch.arange(state.num_processes + 1     ).to(state.device     )
  else:
    _lowerCAmelCase						:	int        =					torch.arange(state.num_processes     ).to(state.device     )
  _lowerCAmelCase						:	Optional[int]        =					pad_across_processes(SCREAMING_SNAKE_CASE_     )
  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 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	Any     )	->				List[Any]:






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





def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	str     )	->				List[Any]:






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





def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	Optional[Any]     )	->				Optional[int]:
  main()





def 							SCREAMING_SNAKE_CASE					(      )	->				str:
  _lowerCAmelCase						:	List[Any]        =					PartialState()
  state.print(f"State: {state}"     )
  state.print("""testing gather"""     )
  test_gather(SCREAMING_SNAKE_CASE_     )
  state.print("""testing gather_object"""     )
  test_gather_object(SCREAMING_SNAKE_CASE_     )
  state.print("""testing broadcast"""     )
  test_broadcast(SCREAMING_SNAKE_CASE_     )
  state.print("""testing pad_across_processes"""     )
  test_pad_across_processes(SCREAMING_SNAKE_CASE_     )
  state.print("""testing reduce_sum"""     )
  test_reduce_sum(SCREAMING_SNAKE_CASE_     )
  state.print("""testing reduce_mean"""     )
  test_reduce_mean(SCREAMING_SNAKE_CASE_     )


if __name__ == "__main__":
     main()






"""simple docstring"""




def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	str      ,_lowerCamelCase						:	bool = False     )	->				str:

  if not isinstance(_lowerCamelCase      ,_lowerCamelCase     ):
    _lowerCAmelCase						:	Union[str, Any]        =					f"Expected string as input, found {type(_lowerCamelCase     )}"
    raise ValueError(_lowerCamelCase     )
  if not isinstance(_lowerCamelCase      ,_lowerCamelCase     ):
    _lowerCAmelCase						:	Union[str, Any]        =					f"Expected boolean as use_pascal parameter, found {type(_lowerCamelCase     )}"
    raise ValueError(_lowerCamelCase     )

  _lowerCAmelCase						:	str        =					input_str.split("""_"""     )

  _lowerCAmelCase						:	Optional[int]        =					0 if use_pascal else 1

  _lowerCAmelCase						:	List[Any]        =					words[start_index:]

  _lowerCAmelCase						:	Union[str, Any]        =					[word[0].upper() + word[1:] for word in words_to_capitalize]

  _lowerCAmelCase						:	List[Any]        =					"""""" if use_pascal else words[0]

  return "".join([initial_word, *capitalized_words]     )


if __name__ == "__main__":
     from doctest import testmod

     testmod()




