





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






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






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				Tuple     ):
		SCREAMING_SNAKE_CASE__								=   [state.process_index]
		SCREAMING_SNAKE_CASE__								=   gather_object(UpperCamelCase__     )
		assert len(UpperCamelCase__     ) == state.num_processes, f'''{gathered_obj}, {len(UpperCamelCase__     )} != {state.num_processes}'''
		assert gathered_obj == list(range(state.num_processes     )     ), f'''{gathered_obj} != {list(range(state.num_processes     )     )}'''






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






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				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:
				SCREAMING_SNAKE_CASE__								=   torch.arange(state.num_processes + 1     ).to(state.device     )
		else:
				SCREAMING_SNAKE_CASE__								=   torch.arange(state.num_processes     ).to(state.device     )
		SCREAMING_SNAKE_CASE__								=   pad_across_processes(UpperCamelCase__     )
		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__				(							UpperCamelCase__:				Optional[int]     ):
		# For now runs on only two processes
		if state.num_processes != 2:
				return
		SCREAMING_SNAKE_CASE__								=   create_tensor(UpperCamelCase__     )
		SCREAMING_SNAKE_CASE__								=   reduce(UpperCamelCase__       , """sum"""     )
		SCREAMING_SNAKE_CASE__								=   torch.tensor([4.0, 6]     ).to(state.device     )
		assert torch.allclose(UpperCamelCase__       , UpperCamelCase__     ), f'''{reduced_tensor} != {truth_tensor}'''






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				str     ):
		# For now runs on only two processes
		if state.num_processes != 2:
				return
		SCREAMING_SNAKE_CASE__								=   create_tensor(UpperCamelCase__     )
		SCREAMING_SNAKE_CASE__								=   reduce(UpperCamelCase__       , """mean"""     )
		SCREAMING_SNAKE_CASE__								=   torch.tensor([2.0, 3]     ).to(state.device     )
		assert torch.allclose(UpperCamelCase__       , UpperCamelCase__     ), f'''{reduced_tensor} != {truth_tensor}'''






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				Optional[int]     ):
		# For xla_spawn (TPUs)
		main()






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


if __name__ == "__main__":
	main()


"""simple docstring"""
from __future__ import annotations

from functools import lru_cache
from math import ceil

_A							=  1_00

_A							=  set(range(3, NUM_PRIMES, 2))
primes.add(2)
_A							=  42

for prime in range(3, ceil(NUM_PRIMES**0.5), 2):
    if prime not in primes:
        continue
    primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime)))




@lru_cache(maxsize=1_00    )
def 					a__						(						lowerCAmelCase    )	-> set[int]:
      if number_to_partition < 0:
            return set()
      elif number_to_partition == 0:
            return {1}

      UpperCAmelCase__			:							set[int]							=    set()
      UpperCAmelCase__			:							int
      UpperCAmelCase__			:							int

      for prime in primes:
            if prime > number_to_partition:
                  continue
            for sub in partition(number_to_partition - prime    ):
                  ret.add(sub * prime    )

      return ret




def 					a__						(						lowerCAmelCase = 50_00    )	-> int | None:
      for number_to_partition in range(1			,	lowerCAmelCase    ):
            if len(partition(lowerCAmelCase    )    ) > number_unique_partitions:
                  return number_to_partition
      return None


if __name__ == "__main__":
    print(f'''{solution() = }''')

