




'''simple docstring'''


import logging
import os

from .state import PartialState

class      A       (						logging.LoggerAdapter       ):







							@staticmethod
							def   __lowerCAmelCase     (    lowerCAmelCase_			:    Union[str, Any]   )     ->				Dict:


														"""simple docstring"""


														_a      	=							PartialState()
														return not main_process_only or (main_process_only and state.is_main_process)







							def   __lowerCAmelCase     (    self			:    Any				,     lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    Tuple				,     *lowerCAmelCase_			:    Dict				,     **lowerCAmelCase_			:    Tuple   )     ->				Any:


														"""simple docstring"""


														if PartialState._shared_state == {}:
																					raise RuntimeError(
																					    '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.'''   )
														_a      	=							kwargs.pop('''main_process_only'''				,     lowerCAmelCase_   )
														_a      	=							kwargs.pop('''in_order'''				,     lowerCAmelCase_   )

														if self.isEnabledFor(lowerCAmelCase_   ):
																					if self._should_log(lowerCAmelCase_   ):
																												_a	,				_a      	=							self.process(lowerCAmelCase_				,     lowerCAmelCase_   )
																												self.logger.log(lowerCAmelCase_				,     lowerCAmelCase_				,     *lowerCAmelCase_				,     **lowerCAmelCase_   )

																					elif in_order:
																												_a      	=							PartialState()
																												for i in range(state.num_processes   ):
																																			if i == state.process_index:
																																										_a	,				_a      	=							self.process(lowerCAmelCase_				,     lowerCAmelCase_   )
																																										self.logger.log(lowerCAmelCase_				,     lowerCAmelCase_				,     *lowerCAmelCase_				,     **lowerCAmelCase_   )
																																			state.wait_for_everyone()



def 						snake_case_       (UpperCamelCase			:    str					,		UpperCamelCase			:    str = None ):





							'''simple docstring'''


							if log_level is None:
														_a      	=							os.environ.get('''ACCELERATE_LOG_LEVEL'''					,		UpperCamelCase )
							_a      	=							logging.getLogger(UpperCamelCase )
							if log_level is not None:
														logger.setLevel(log_level.upper() )
														logger.root.setLevel(log_level.upper() )
							return MultiProcessAdapter(UpperCamelCase					,		{} )



from math import ceil






def   lowerCamelCase__							(							__lowerCAmelCase    :    Dict       ,				__lowerCAmelCase    :    int							):




			"""simple docstring"""


			lowerCAmelCase_         =      list(range(0       ,				__lowerCAmelCase							)							)

			lowerCAmelCase_         =      [item for sublist in list(device_map.values()							) for item in sublist]

			# Duplicate check
			lowerCAmelCase_         =      []
			for i in device_map_blocks:
						if device_map_blocks.count(__lowerCAmelCase							) > 1 and i not in duplicate_blocks:
									duplicate_blocks.append(__lowerCAmelCase							)
    # Missing blocks
			lowerCAmelCase_         =      [i for i in blocks if i not in device_map_blocks]
			lowerCAmelCase_         =      [i for i in device_map_blocks if i not in blocks]

			if len(__lowerCAmelCase							) != 0:
						raise ValueError(
						    "Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device."
						    " These attention blocks were specified more than once: " + str(__lowerCAmelCase							)							)
			if len(__lowerCAmelCase							) != 0:
						raise ValueError(
						    "There are attention blocks for this model that are not specified in the device_map. Add these attention "
						    "blocks to a device on the device_map: " + str(__lowerCAmelCase							)							)
			if len(__lowerCAmelCase							) != 0:
						raise ValueError(
						    "The device_map contains more attention blocks than this model has. Remove these from the device_map:"
						    + str(__lowerCAmelCase							)							)






def   lowerCamelCase__							(							__lowerCAmelCase    :    List[str]       ,				__lowerCAmelCase    :    Tuple							):




			"""simple docstring"""


			lowerCAmelCase_         =      list(range(__lowerCAmelCase							)							)
			lowerCAmelCase_         =      int(ceil(n_layers / len(__lowerCAmelCase							)							)							)
			lowerCAmelCase_         =      [layers[i : i + n_blocks] for i in range(0       ,				__lowerCAmelCase       ,				__lowerCAmelCase							)]

			return dict(zip(__lowerCAmelCase       ,				__lowerCAmelCase							)							)



