






# 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    __lowerCAmelCase   (    __snake_case							):
     return (torch.arange(state.num_processes							) + 1.0 + (state.num_processes * state.process_index)).to(state.device							)





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





def    __lowerCAmelCase   (    __snake_case							):
     __lowerCAmelCase             =       [state.process_index]
     __lowerCAmelCase             =       gather_object(__snake_case							)
     assert len(__snake_case							) == state.num_processes, F"""{gathered_obj}, {len(__snake_case							)} != {state.num_processes}"""
     assert gathered_obj == list(range(state.num_processes							)							), F"""{gathered_obj} != {list(range(state.num_processes							)							)}"""





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





def    __lowerCAmelCase   (    __snake_case							):




     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(__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    __lowerCAmelCase   (    __snake_case							):




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





def    __lowerCAmelCase   (    __snake_case							):




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





def    __lowerCAmelCase   (    __snake_case							):
     main()





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


if __name__ == "__main__":
 main()










from timeit import timeit


def     lowercase	(    a ):



		'''simple docstring'''




		if number < 0:
				raise ValueError("the value of input must not be negative" )
		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						0
		while number:
				number &= number - 1
				result += 1
		return result


def     lowercase	(    a ):



		'''simple docstring'''




		if number < 0:
				raise ValueError("the value of input must not be negative" )
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						0
		while number:
				if number % 2 == 1:
						result += 1
				number >>= 1
		return result


def     lowercase	(    ):



		'''simple docstring'''




		def do_benchmark(a ) -> None:
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						"import __main__ as z"
				print(F"Benchmark when {number = }:" )
				print(F"{get_set_bits_count_using_modulo_operator(a ) = }" )
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						timeit("z.get_set_bits_count_using_modulo_operator(25)"     , setup=a )
				print(F"timeit() runs in {timing} seconds" )
				print(F"{get_set_bits_count_using_brian_kernighans_algorithm(a ) = }" )
				SCREAMING_SNAKE_CASE_				:str				     =						timeit(
				    "z.get_set_bits_count_using_brian_kernighans_algorithm(25)"     , setup=a     , )
				print(F"timeit() runs in {timing} seconds" )

		for number in (25, 37, 58, 0):
				do_benchmark(a )
				print()


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		benchmark()






