





# 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 A_	(		A__				)  ->		List[str]:
       return (torch.arange(state.num_processes				) + 1.0 + (state.num_processes * state.process_index)).to(state.device				)



def A_	(		A__				)  ->		Tuple:
       a__						:	Dict =				create_tensor(A__				)
       a__						:	Tuple =				gather(A__				)
       assert gathered_tensor.tolist() == list(range(1		,      state.num_processes**2 + 1				)				)



def A_	(		A__				)  ->		Any:
       a__						:	Tuple =				[state.process_index]
       a__						:	Union[str, Any] =				gather_object(A__				)
       assert len(A__				) == state.num_processes, F'{gathered_obj}, {len(A__				)} != {state.num_processes}'
       assert gathered_obj == list(range(state.num_processes				)				), F'{gathered_obj} != {list(range(state.num_processes				)				)}'



def A_	(		A__				)  ->		Dict:
       a__						:	List[Any] =				create_tensor(A__				)
       a__						:	Optional[int] =				broadcast(A__				)
       assert broadcasted_tensor.shape == torch.Size([state.num_processes]				)
       assert broadcasted_tensor.tolist() == list(range(1		,      state.num_processes + 1				)				)



def A_	(		A__				)  ->		List[str]:
       # We need to pad the tensor with one more element if we are the main process
       # to ensure that we can pad
       if state.is_main_process:
              a__						:	str =				torch.arange(state.num_processes + 1				).to(state.device				)
       else:
              a__						:	Optional[int] =				torch.arange(state.num_processes				).to(state.device				)
       a__						:	Optional[Any] =				pad_across_processes(A__				)
       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 A_	(		A__				)  ->		List[Any]:
       # For now runs on only two processes
       if state.num_processes != 2:
              return
       a__						:	Optional[Any] =				create_tensor(A__				)
       a__						:	str =				reduce(A__		,      'sum'				)
       a__						:	Any =				torch.tensor([4.0, 6]				).to(state.device				)
       assert torch.allclose(A__		,      A__				), F'{reduced_tensor} != {truth_tensor}'



def A_	(		A__				)  ->		Dict:
       # For now runs on only two processes
       if state.num_processes != 2:
              return
       a__						:	int =				create_tensor(A__				)
       a__						:	Union[str, Any] =				reduce(A__		,      'mean'				)
       a__						:	Any =				torch.tensor([2.0, 3]				).to(state.device				)
       assert torch.allclose(A__		,      A__				), F'{reduced_tensor} != {truth_tensor}'



def A_	(		A__				)  ->		List[Any]:
       # For xla_spawn (TPUs)
       main()



def A_	(		)  ->		int:
       a__						:	List[str] =				PartialState()
       state.print(F'State: {state}'				)
       state.print('testing gather'				)
       test_gather(A__				)
       state.print('testing gather_object'				)
       test_gather_object(A__				)
       state.print('testing broadcast'				)
       test_broadcast(A__				)
       state.print('testing pad_across_processes'				)
       test_pad_across_processes(A__				)
       state.print('testing reduce_sum'				)
       test_reduce_sum(A__				)
       state.print('testing reduce_mean'				)
       test_reduce_mean(A__				)


if __name__ == "__main__":
    main()









from math import loga



def A_	(		A__				)  ->		int:
       if a < 0:
              raise ValueError('Input value must be a positive integer'				)
       elif isinstance(A__		,      A__				):
              raise TypeError('Input value must be a \'int\' type'				)
       return 0 if (a == 0) else int(loga(a & -a				)				)


if __name__ == "__main__":
    import doctest

    doctest.testmod()


