
# 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__					(							snake_case							):





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


def 							a__					(							snake_case							):





  """simple docstring"""
  __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		create_tensor(snake_case							)
  __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		gather(snake_case							)
  assert gathered_tensor.tolist() == list(range(1       ,			state.num_processes**2 + 1							)							)


def 							a__					(							snake_case							):





  """simple docstring"""
  __SCREAMING_SNAKE_CASE      :	str					=		[state.process_index]
  __SCREAMING_SNAKE_CASE      :	Optional[int]					=		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 							a__					(							snake_case							):





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


def 							a__					(							snake_case							):





  """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:
    __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		torch.arange(state.num_processes + 1							).to(state.device							)
  else:
    __SCREAMING_SNAKE_CASE      :	List[str]					=		torch.arange(state.num_processes							).to(state.device							)
  __SCREAMING_SNAKE_CASE      :	List[Any]					=		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 							a__					(							snake_case							):





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


def 							a__					(							snake_case							):





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


def 							a__					(							snake_case							):





  """simple docstring"""
  # For xla_spawn (TPUs)
  main()


def 							a__					(							):





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





"""simple docstring"""







from random import randint, random

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							int     ,		lowerCamelCase_:							int     ,		lowerCamelCase_:							int     ,		lowerCamelCase_:							bool = False     ,		lowerCamelCase_:							bool = False     ,		lowerCamelCase_:							int = 5     ,		):
						"""simple docstring"""






						snake_case   :			str						    = [[-1] * number_of_cells]  # Create a highway without any car
						snake_case   :			str						    = 0
						snake_case   :			Any						    = max(lowerCamelCase_     ,		0 )
						while i < number_of_cells:
												snake_case   :			Optional[int]						    = (
												    randint(0     ,		lowerCamelCase_ ) if random_speed else initial_speed
												)  # Place the cars
												i += (
												    randint(1     ,		max_speed * 2 ) if random_frequency else frequency
												)  # Arbitrary number, may need tuning
						return highway

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							list     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






						snake_case   :			Any						    = 0
						snake_case   :			int						    = highway_now[car_index + 1 :]
						for cell in range(len(lowerCamelCase_ ) ):  # May need a better name for this
												if cells[cell] != -1:  # If the cell is not empty then
																		return distance  # we have the distance we wanted
												distance += 1
						# Here if the car is near the end of the highway
						return distance + get_distance(lowerCamelCase_     ,		-1 )

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							list     ,		lowerCamelCase_:							float     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






						snake_case   :			List[str]						    = len(lowerCamelCase_ )
						# Beforce calculations, the highway is empty
						snake_case   :			Dict						    = [-1] * number_of_cells

						for car_index in range(lowerCamelCase_ ):
												if highway_now[car_index] != -1:
																		# Add 1 to the current speed of the car and cap the speed
																		snake_case   :			Tuple						    = min(highway_now[car_index] + 1     ,		lowerCamelCase_ )
																		# Number of empty cell before the next car
																		snake_case   :			int						    = get_distance(lowerCamelCase_     ,		lowerCamelCase_ ) - 1
																		# We can't have the car causing an accident
																		snake_case   :			str						    = min(next_highway[car_index]     ,		lowerCamelCase_ )
																		if random() < probability:
																								# Randomly, a driver will slow down
																								snake_case   :			Optional[Any]						    = max(next_highway[car_index] - 1     ,		0 )
						return next_highway

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							list     ,		lowerCamelCase_:							int     ,		lowerCamelCase_:							float     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






						snake_case   :			Optional[Any]						    = len(highway[0] )

						for i in range(lowerCamelCase_ ):
												snake_case   :			Union[str, Any]						    = update(highway[i]     ,		lowerCamelCase_     ,		lowerCamelCase_ )
												snake_case   :			Optional[int]						    = [-1] * number_of_cells

												for car_index in range(lowerCamelCase_ ):
																		snake_case   :			List[Any]						    = next_speeds_calculated[car_index]
																		if speed != -1:
																								# Change the position based on the speed (with % to create the loop)
																								snake_case   :			Optional[Any]						    = (car_index + speed) % number_of_cells
																								# Commit the change of position
																								snake_case   :			Tuple						    = speed
												highway.append(lowerCamelCase_ )

						return highway


if __name__ == "__main__":
			import doctest

			doctest.testmod()



