





# 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						(   UpperCAmelCase					)    ->    str:







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



def 	_a						(   UpperCAmelCase					)    ->    str:







   """simple docstring"""
   lowerCamelCase__       :						Any        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						List[str]        =							gather(UpperCAmelCase					)
   assert gathered_tensor.tolist() == list(range(1      ,  state.num_processes**2 + 1					)					)



def 	_a						(   UpperCAmelCase					)    ->    Optional[Any]:







   """simple docstring"""
   lowerCamelCase__       :						Dict        =							[state.process_index]
   lowerCamelCase__       :						Union[str, Any]        =							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 	_a						(   UpperCAmelCase					)    ->    int:







   """simple docstring"""
   lowerCamelCase__       :						Optional[Any]        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						int        =							broadcast(UpperCAmelCase					)
   assert broadcasted_tensor.shape == torch.Size([state.num_processes]					)
   assert broadcasted_tensor.tolist() == list(range(1      ,  state.num_processes + 1					)					)



def 	_a						(   UpperCAmelCase					)    ->    Optional[Any]:







   """simple docstring"""
   # 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:
      lowerCamelCase__       :						Union[str, Any]        =							torch.arange(state.num_processes + 1					).to(state.device					)
   else:
      lowerCamelCase__       :						List[Any]        =							torch.arange(state.num_processes					).to(state.device					)
   lowerCamelCase__       :						str        =							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 	_a						(   UpperCAmelCase					)    ->    Optional[Any]:







   """simple docstring"""
   # For now runs on only two processes
   if state.num_processes != 2:
      return
   lowerCamelCase__       :						Dict        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						Any        =							reduce(UpperCAmelCase      ,  '''sum'''					)
   lowerCamelCase__       :						List[str]        =							torch.tensor([4.0, 6]					).to(state.device					)
   assert torch.allclose(UpperCAmelCase      ,  UpperCAmelCase					), f"{reduced_tensor} != {truth_tensor}"



def 	_a						(   UpperCAmelCase					)    ->    Optional[Any]:







   """simple docstring"""
   # For now runs on only two processes
   if state.num_processes != 2:
      return
   lowerCamelCase__       :						Any        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						int        =							reduce(UpperCAmelCase      ,  '''mean'''					)
   lowerCamelCase__       :						Optional[Any]        =							torch.tensor([2.0, 3]					).to(state.device					)
   assert torch.allclose(UpperCAmelCase      ,  UpperCAmelCase					), f"{reduced_tensor} != {truth_tensor}"



def 	_a						(   UpperCAmelCase					)    ->    List[str]:







   """simple docstring"""
   main()



def 	_a						(   )    ->    int:







   """simple docstring"""
   lowerCamelCase__       :						List[str]        =							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()










def 	_a						(   UpperCAmelCase					)    ->    bool:







   """simple docstring"""
   return str(UpperCAmelCase					) == str(UpperCAmelCase					)[::-1]



def 	_a						(   UpperCAmelCase					)    ->    int:







   """simple docstring"""
   return int(UpperCAmelCase					) + int(str(UpperCAmelCase					)[::-1]					)



def 	_a						(   UpperCAmelCase = 10000					)    ->    int:







   """simple docstring"""
   lowerCamelCase__       :						Tuple        =							[]
   for num in range(1      ,  UpperCAmelCase					):
      lowerCamelCase__       :						List[str]        =							0
      lowerCamelCase__       :						Union[str, Any]        =							num
      while iterations < 50:
         lowerCamelCase__       :						Dict        =							sum_reverse(UpperCAmelCase					)
         iterations += 1
         if is_palindrome(UpperCAmelCase					):
            break
      else:
         lychrel_nums.append(UpperCAmelCase					)
   return len(UpperCAmelCase					)


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



