




# 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  UpperCAmelCase_							(				__UpperCAmelCase						:     Optional[Any]							) ->			Union[str, Any]:
      return (torch.arange(state.num_processes							) + 1.0 + (state.num_processes * state.process_index)).to(state.device							)

def  UpperCAmelCase_							(				__UpperCAmelCase						:     str							) ->			List[str]:
      SCREAMING_SNAKE_CASE_     						=      create_tensor(__UpperCAmelCase							)
      SCREAMING_SNAKE_CASE_     						=      gather(__UpperCAmelCase							)
      assert gathered_tensor.tolist() == list(range(1      ,      state.num_processes**2 + 1							)							)

def  UpperCAmelCase_							(				__UpperCAmelCase						:     Tuple							) ->			int:
      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  UpperCAmelCase_							(				__UpperCAmelCase						:     List[str]							) ->			int:
      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  UpperCAmelCase_							(				__UpperCAmelCase						:     Optional[Any]							) ->			Dict:
      # 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  UpperCAmelCase_							(				__UpperCAmelCase						:     Optional[Any]							) ->			Union[str, Any]:
      # 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  UpperCAmelCase_							(				__UpperCAmelCase						:     Tuple							) ->			List[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  UpperCAmelCase_							(				__UpperCAmelCase						:     Dict							) ->			str:
      # For xla_spawn (TPUs)
      main()

def  UpperCAmelCase_							(				) ->			Any:
      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()





def  UpperCAmelCase_							(				__UpperCAmelCase						:     str							) ->			str:
      SCREAMING_SNAKE_CASE_     						=      ''
      for ch in key:
            if ch == " " or ch not in key_no_dups and ch.isalpha():
                  key_no_dups += ch
      return key_no_dups

def  UpperCAmelCase_							(				__UpperCAmelCase						:     str							) ->			dict[str, str]:
      SCREAMING_SNAKE_CASE_     						=      [chr(i + 65							) for i in range(26							)]
      # Remove duplicate characters from key
      SCREAMING_SNAKE_CASE_     						=      remove_duplicates(key.upper()							)
      SCREAMING_SNAKE_CASE_     						=      len(__UpperCAmelCase							)
      # First fill cipher with key characters
      SCREAMING_SNAKE_CASE_     						=      {alphabet[i]: char for i, char in enumerate(__UpperCAmelCase							)}
      # Then map remaining characters in alphabet to
      # the alphabet from the beginning
      for i in range(len(__UpperCAmelCase							)      ,      26							):
            SCREAMING_SNAKE_CASE_     						=      alphabet[i - offset]
            # Ensure we are not mapping letters to letters previously mapped
            while char in key:
                  offset -= 1
                  SCREAMING_SNAKE_CASE_     						=      alphabet[i - offset]
            SCREAMING_SNAKE_CASE_     						=      char
      return cipher_alphabet

def  UpperCAmelCase_							(				__UpperCAmelCase						:     str      ,      __UpperCAmelCase						:     dict[str, str]							) ->			str:
      return "".join(cipher_map.get(__UpperCAmelCase      ,      __UpperCAmelCase							) for ch in message.upper()							)

def  UpperCAmelCase_							(				__UpperCAmelCase						:     str      ,      __UpperCAmelCase						:     dict[str, str]							) ->			str:
      SCREAMING_SNAKE_CASE_     						=      {v: k for k, v in cipher_map.items()}
      return "".join(rev_cipher_map.get(__UpperCAmelCase      ,      __UpperCAmelCase							) for ch in message.upper()							)

def  UpperCAmelCase_							(				) ->			None:
      SCREAMING_SNAKE_CASE_     						=      input('Enter message to encode or decode: '							).strip()
      SCREAMING_SNAKE_CASE_     						=      input('Enter keyword: '							).strip()
      SCREAMING_SNAKE_CASE_     						=      input('Encipher or decipher? E/D:'							).strip()[0].lower()
      try:
            SCREAMING_SNAKE_CASE_     						=      {'e': encipher, 'd': decipher}[option]
      except KeyError:
            raise KeyError('invalid input option'							)
      SCREAMING_SNAKE_CASE_     						=      create_cipher_map(__UpperCAmelCase							)
      print(func(__UpperCAmelCase      ,      __UpperCAmelCase							)							)


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   main()