
'''simple docstring'''

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


def 		_UpperCAmelCase (     _lowerCamelCase  :				Dict						)			->  Optional[int]:
  _lowerCAmelCase				:     Optional[int]       =    create_tensor(lowercase__						)
  _lowerCAmelCase				:     Tuple       =    gather(lowercase__						)
  assert gathered_tensor.tolist() == list(range(1     ,					state.num_processes**2 + 1						)						)


def 		_UpperCAmelCase (     _lowerCamelCase  :				Optional[int]						)			->  Optional[Any]:
  _lowerCAmelCase				:     List[Any]       =    [state.process_index]
  _lowerCAmelCase				:     str       =    gather_object(lowercase__						)
  assert len(lowercase__						) == state.num_processes, f'{gathered_obj}, {len(lowercase__						)} != {state.num_processes}'
  assert gathered_obj == list(range(state.num_processes						)						), f'{gathered_obj} != {list(range(state.num_processes						)						)}'


def 		_UpperCAmelCase (     _lowerCamelCase  :				str						)			->  Tuple:
  _lowerCAmelCase				:     Tuple       =    create_tensor(lowercase__						)
  _lowerCAmelCase				:     List[Any]       =    broadcast(lowercase__						)
  assert broadcasted_tensor.shape == torch.Size([state.num_processes]						)
  assert broadcasted_tensor.tolist() == list(range(1     ,					state.num_processes + 1						)						)


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


def 		_UpperCAmelCase (     _lowerCamelCase  :				int						)			->  List[str]:
  # For now runs on only two processes
  if state.num_processes != 2:
    return
  _lowerCAmelCase				:     str       =    create_tensor(lowercase__						)
  _lowerCAmelCase				:     Dict       =    reduce(lowercase__     ,					"""mean"""						)
  _lowerCAmelCase				:     List[str]       =    torch.tensor([2.0, 3]						).to(state.device						)
  assert torch.allclose(lowercase__     ,					lowercase__						), f'{reduced_tensor} != {truth_tensor}'


def 		_UpperCAmelCase (     _lowerCamelCase  :				List[Any]						)			->  Tuple:
  # For xla_spawn (TPUs)
  main()


def 		_UpperCAmelCase (     )			->  Any:
  _lowerCAmelCase				:     Union[str, Any]       =    PartialState()
  state.print(f'State: {state}'						)
  state.print("""testing gather"""						)
  test_gather(lowercase__						)
  state.print("""testing gather_object"""						)
  test_gather_object(lowercase__						)
  state.print("""testing broadcast"""						)
  test_broadcast(lowercase__						)
  state.print("""testing pad_across_processes"""						)
  test_pad_across_processes(lowercase__						)
  state.print("""testing reduce_sum"""						)
  test_reduce_sum(lowercase__						)
  state.print("""testing reduce_mean"""						)
  test_reduce_mean(lowercase__						)


if __name__ == "__main__":
     main()





from collections import deque

from .hash_table import HashTable







class   __lowercase      (_UpperCAmelCase ):
 def __init__(							self      ,							*A_      ,							**A_					)					->int:



       '''simple docstring'''





       super().__init__(*A_      ,							**A_					)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					deque([]					) if self.values[key] is None else self.values[key]
       self.values[key].appendleft(A_					)
       __lowerCAmelCase					:						int					=					self.values[key]
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       return (
           sum(self.charge_factor - len(A_					) for slot in self.values					)
           / self.size_table
           * self.charge_factor
       )





 def 	UpperCamelCase__  (							self      ,							A_      ,							A_=None					)					->str:



       '''simple docstring'''





       if not (
           len(self.values[key]					) == self.charge_factor and self.values.count(A_					) == 0
       ):
             return key
       return super()._collision_resolution(A_      ,							A_					)


