




# 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   (  lowercase_      )       ->	List[Any]:

    """simple docstring"""


    return (torch.arange(state.num_processes      ) + 1.0 + (state.num_processes * state.process_index)).to(state.device      )




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	Dict:

    """simple docstring"""


    __UpperCamelCase  		=						create_tensor(lowercase_      )
    __UpperCamelCase  		=						gather(lowercase_      )
    assert gathered_tensor.tolist() == list(range(1					,     state.num_processes**2 + 1      )      )




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	Any:

    """simple docstring"""


    __UpperCamelCase  		=						[state.process_index]
    __UpperCamelCase  		=						gather_object(lowercase_      )
    assert len(lowercase_      ) == state.num_processes, F"{gathered_obj}, {len(lowercase_      )} != {state.num_processes}"
    assert gathered_obj == list(range(state.num_processes      )      ), F"{gathered_obj} != {list(range(state.num_processes      )      )}"




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	Dict:

    """simple docstring"""


    __UpperCamelCase  		=						create_tensor(lowercase_      )
    __UpperCamelCase  		=						broadcast(lowercase_      )
    assert broadcasted_tensor.shape == torch.Size([state.num_processes]      )
    assert broadcasted_tensor.tolist() == list(range(1					,     state.num_processes + 1      )      )




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	List[str]:

    """simple docstring"""


    if state.is_main_process:
        __UpperCamelCase  		=						torch.arange(state.num_processes + 1      ).to(state.device      )
    else:
        __UpperCamelCase  		=						torch.arange(state.num_processes      ).to(state.device      )
    __UpperCamelCase  		=						pad_across_processes(lowercase_      )
    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   (  lowercase_      )       ->	Tuple:

    """simple docstring"""


    if state.num_processes != 2:
        return
    __UpperCamelCase  		=						create_tensor(lowercase_      )
    __UpperCamelCase  		=						reduce(lowercase_					,     '''sum'''      )
    __UpperCamelCase  		=						torch.tensor([4.0, 6]      ).to(state.device      )
    assert torch.allclose(lowercase_					,     lowercase_      ), F"{reduced_tensor} != {truth_tensor}"




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	Tuple:

    """simple docstring"""


    if state.num_processes != 2:
        return
    __UpperCamelCase  		=						create_tensor(lowercase_      )
    __UpperCamelCase  		=						reduce(lowercase_					,     '''mean'''      )
    __UpperCamelCase  		=						torch.tensor([2.0, 3]      ).to(state.device      )
    assert torch.allclose(lowercase_					,     lowercase_      ), F"{reduced_tensor} != {truth_tensor}"




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	Any:

    """simple docstring"""


    main()




def 					__SCREAMING_SNAKE_CASE   (  )       ->	Union[str, Any]:

    """simple docstring"""


    __UpperCamelCase  		=						PartialState()
    state.print(F"State: {state}"      )
    state.print('''testing gather'''      )
    test_gather(lowercase_      )
    state.print('''testing gather_object'''      )
    test_gather_object(lowercase_      )
    state.print('''testing broadcast'''      )
    test_broadcast(lowercase_      )
    state.print('''testing pad_across_processes'''      )
    test_pad_across_processes(lowercase_      )
    state.print('''testing reduce_sum'''      )
    test_reduce_sum(lowercase_      )
    state.print('''testing reduce_mean'''      )
    test_reduce_mean(lowercase_      )


if __name__ == "__main__":
 main()











def 					__SCREAMING_SNAKE_CASE   (  lowercase_					,     lowercase_ = False      )       ->	str:

    """simple docstring"""


    if not isinstance(lowercase_					,     lowercase_      ):
        __UpperCamelCase  		=						F"Expected string as input, found {type(lowercase_      )}"
        raise ValueError(lowercase_      )
    if not isinstance(lowercase_					,     lowercase_      ):
        __UpperCamelCase  		=						F"Expected boolean as use_pascal parameter, found {type(lowercase_      )}"
        raise ValueError(lowercase_      )

    __UpperCamelCase  		=						input_str.split('''_'''      )

    __UpperCamelCase  		=						0 if use_pascal else 1

    __UpperCamelCase  		=						words[start_index:]

    __UpperCamelCase  		=						[word[0].upper() + word[1:] for word in words_to_capitalize]

    __UpperCamelCase  		=						'''''' if use_pascal else words[0]

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


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

 testmod()






