from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a  :     Any       =  logging.get_logger(__name__)

__a  :     Tuple       =  {
    """facebook/s2t-small-librispeech-asr""": (
        """https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json"""
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text
}




class    _UpperCamelCase      (						_UpperCAmelCase ):


			"""simple docstring"""






			__a  : Union[str, Any]			 =						'''speech_to_text'''
			__a  : Optional[Any]			 =						['''past_key_values''']
			__a  : Union[str, Any]			 =						{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}





			def __init__(   self      ,   lowerCAmelCase__=1_00_00      ,   lowerCAmelCase__=12      ,   lowerCAmelCase__=20_48      ,   lowerCAmelCase__=4      ,   lowerCAmelCase__=6      ,   lowerCAmelCase__=20_48      ,   lowerCAmelCase__=4      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__="relu"      ,   lowerCAmelCase__=2_56      ,   lowerCAmelCase__=0.1      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=0.02      ,   lowerCAmelCase__=2      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=1      ,   lowerCAmelCase__=0      ,   lowerCAmelCase__=2      ,   lowerCAmelCase__=60_00      ,   lowerCAmelCase__=10_24      ,   lowerCAmelCase__=2      ,   lowerCAmelCase__=(5, 5)      ,   lowerCAmelCase__=10_24      ,   lowerCAmelCase__=80      ,   lowerCAmelCase__=1      ,   **lowerCAmelCase__      ,   )     ->					int:

							'''simple docstring'''


							__lowercase 			=							vocab_size
							__lowercase 			=							d_model
							__lowercase 			=							encoder_ffn_dim
							__lowercase 			=							encoder_layers
							__lowercase 			=							encoder_attention_heads
							__lowercase 			=							decoder_ffn_dim
							__lowercase 			=							decoder_layers
							__lowercase 			=							decoder_attention_heads
							__lowercase 			=							dropout
							__lowercase 			=							attention_dropout
							__lowercase 			=							activation_dropout
							__lowercase 			=							activation_function
							__lowercase 			=							init_std
							__lowercase 			=							encoder_layerdrop
							__lowercase 			=							decoder_layerdrop
							__lowercase 			=							use_cache
							__lowercase 			=							encoder_layers
							__lowercase 			=							scale_embedding  # scale factor will be sqrt(d_model) if True
							__lowercase 			=							max_source_positions
							__lowercase 			=							max_target_positions
							__lowercase 			=							num_conv_layers
							__lowercase 			=							list(lowerCAmelCase__						)
							__lowercase 			=							conv_channels
							__lowercase 			=							input_feat_per_channel
							__lowercase 			=							input_channels

							if len(self.conv_kernel_sizes						) != self.num_conv_layers:
											raise ValueError(
											    '''Configuration for convolutional module is incorrect. '''
											    '''It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` '''
											    F"but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes						)}`, "
											    F"`config.num_conv_layers = {self.num_conv_layers}`."						)

							super().__init__(
							    pad_token_id=lowerCAmelCase__      ,   bos_token_id=lowerCAmelCase__      ,   eos_token_id=lowerCAmelCase__      ,   is_encoder_decoder=lowerCAmelCase__      ,   decoder_start_token_id=lowerCAmelCase__      ,   **lowerCAmelCase__      ,   )
import gc
import threading
import time

import psutil
import torch




class    _UpperCamelCase      :


			"""simple docstring"""






			def __init__(   self						)     ->					str:

							'''simple docstring'''


							__lowercase 			=							psutil.Process()
							__lowercase 			=							False





			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					str:

							'''simple docstring'''


							__lowercase 			=							-1

							while True:
											__lowercase 			=							max(self.process.memory_info().rss      ,   self.cpu_memory_peak						)

											# can't sleep or will not catch the peak right (this comment is here on purpose)
											if not self.peak_monitoring:
															break





			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					int:

							'''simple docstring'''


							__lowercase 			=							True
							__lowercase 			=							threading.Thread(target=self.peak_monitor						)
							__lowercase 			=							True
							self.thread.start()





			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					str:

							'''simple docstring'''


							__lowercase 			=							False
							self.thread.join()
							return self.cpu_memory_peak


__a  :     List[str]       =  PeakCPUMemory()





def 				UpperCAmelCase						(    ):







				"""simple docstring"""



				__lowercase 			=							{'''time''': time.time()}

				gc.collect()
				torch.cuda.empty_cache()

				# CPU mem
				__lowercase 			=							psutil.Process().memory_info().rss
				cpu_peak_tracker.start()

				# GPU mem
				for i in range(torch.cuda.device_count()							):
								__lowercase 			=							torch.cuda.memory_allocated(lowercase							)
				torch.cuda.reset_peak_memory_stats()

				return measures





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				__lowercase 			=							{'''time''': time.time() - start_measures['''time''']}

				gc.collect()
				torch.cuda.empty_cache()

				# CPU mem
				__lowercase 			=							(psutil.Process().memory_info().rss - start_measures['''cpu''']) / 2**20
				__lowercase 			=							(cpu_peak_tracker.stop() - start_measures['''cpu''']) / 2**20

				# GPU mem
				for i in range(torch.cuda.device_count()							):
								__lowercase 			=							(torch.cuda.memory_allocated(lowercase							) - start_measures[str(lowercase							)]) / 2**20
								__lowercase 			=							(torch.cuda.max_memory_allocated(lowercase							) - start_measures[str(lowercase							)]) / 2**20

				return measures





def 				UpperCAmelCase						(    lowercase							,			lowercase							):







				"""simple docstring"""



				print(F"{description}:"							)
				print(F"- Time: {measures['time']:.2f}s"							)
				for i in range(torch.cuda.device_count()							):
								print(F"- GPU {i} allocated: {measures[str(lowercase							)]:.2f}MiB"							)
								__lowercase 			=							measures[F"{i}-peak"]
								print(F"- GPU {i} peak: {peak:.2f}MiB"							)
				print(F"- CPU RAM allocated: {measures['cpu']:.2f}MiB"							)
				print(F"- CPU RAM peak: {measures['cpu-peak']:.2f}MiB"							)