



'''simple docstring'''

from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
							from ...feature_extraction_utils import FeatureExtractionMixin
							from ...tokenization_utils_base import PreTrainedTokenizerBase
							from ...utils import TensorType

__lowerCAmelCase									=   logging.get_logger(__name__)

__lowerCAmelCase									=   {
    '''openai/whisper-base''': '''https://huggingface.co/openai/whisper-base/resolve/main/config.json''',
}

# fmt: off
__lowerCAmelCase									=   [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1_058, 1_220, 1_267, 1_279, 1_303, 1_343, 1_377,
    1_391, 1_635, 1_782, 1_875, 2_162, 2_361, 2_488, 3_467, 4_008, 4_211,
    4_600, 4_808, 5_299, 5_855, 6_329, 7_203, 9_609, 9_959, 10_563, 10_786,
    11_420, 11_709, 11_907, 13_163, 13_697, 13_700, 14_808, 15_306, 16_410, 16_791,
    17_992, 19_203, 19_510, 20_724, 22_305, 22_935, 27_007, 30_109, 30_420, 33_409,
    34_949, 40_283, 40_493, 40_549, 47_282, 49_146, 50_257, 50_359, 50_360, 50_361
]
__lowerCAmelCase									=   [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1_350, 1_853, 1_982, 2_460, 2_627,
    3_246, 3_253, 3_268, 3_536, 3_846, 3_961, 4_183, 4_667, 6_585, 6_647,
    7_273, 9_061, 9_383, 10_428, 10_929, 11_938, 12_033, 12_331, 12_562, 13_793,
    14_157, 14_635, 15_265, 15_618, 16_553, 16_604, 18_362, 18_956, 20_075, 21_675,
    22_520, 26_130, 26_161, 26_435, 28_279, 29_464, 31_650, 32_302, 32_470, 36_865,
    42_863, 47_425, 49_870, 50_254, 50_258, 50_360, 50_361, 50_362
]

class 	__magic_name__						(    _UpperCamelCase   ):
							lowerCAmelCase       :    Union[str, Any]   				=					'whisper'
							lowerCAmelCase       :    Optional[Any]   				=					['past_key_values']
							lowerCAmelCase       :    str   				=					{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







							def __init__(							self					:       Optional[Any]	,_UpperCAmelCase					:       Dict=51865	,_UpperCAmelCase					:       List[str]=80	,_UpperCAmelCase					:       List[str]=6	,_UpperCAmelCase					:       str=4	,_UpperCAmelCase					:       Optional[int]=6	,_UpperCAmelCase					:       Optional[int]=4	,_UpperCAmelCase					:       str=1536	,_UpperCAmelCase					:       Optional[int]=1536	,_UpperCAmelCase					:       str=0.0	,_UpperCAmelCase					:       str=0.0	,_UpperCAmelCase					:       Optional[Any]=50257	,_UpperCAmelCase					:       Union[str, Any]=True	,_UpperCAmelCase					:       Dict=True	,_UpperCAmelCase					:       Union[str, Any]="gelu"	,_UpperCAmelCase					:       Dict=256	,_UpperCAmelCase					:       List[str]=0.0	,_UpperCAmelCase					:       Union[str, Any]=0.0	,_UpperCAmelCase					:       List[Any]=0.0	,_UpperCAmelCase					:       Union[str, Any]=0.02	,_UpperCAmelCase					:       Any=False	,_UpperCAmelCase					:       Any=1500	,_UpperCAmelCase					:       Dict=448	,_UpperCAmelCase					:       List[Any]=50256	,_UpperCAmelCase					:       List[str]=50256	,_UpperCAmelCase					:       Optional[int]=50256	,_UpperCAmelCase					:       Optional[Any]=None	,_UpperCAmelCase					:       int=[220, 50256]	,_UpperCAmelCase					:       List[Any]=False	,_UpperCAmelCase					:       int=256	,_UpperCAmelCase					:       Optional[Any]=False	,_UpperCAmelCase					:       List[str]=0.05	,_UpperCAmelCase					:       Any=10	,_UpperCAmelCase					:       Union[str, Any]=2	,_UpperCAmelCase					:       Optional[int]=0.0	,_UpperCAmelCase					:       Optional[int]=10	,_UpperCAmelCase					:       Optional[Any]=0	,_UpperCAmelCase					:       Any=7	,**_UpperCAmelCase					:       Optional[int]	,):
									_a     :      Tuple        =							vocab_size
									_a     :      List[str]        =							num_mel_bins
									_a     :      Any        =							d_model
									_a     :      Dict        =							encoder_layers
									_a     :      Tuple        =							encoder_attention_heads
									_a     :      Optional[Any]        =							decoder_layers
									_a     :      int        =							decoder_attention_heads
									_a     :      Optional[Any]        =							decoder_ffn_dim
									_a     :      Tuple        =							encoder_ffn_dim
									_a     :      Optional[int]        =							dropout
									_a     :      Tuple        =							attention_dropout
									_a     :      Union[str, Any]        =							activation_dropout
									_a     :      List[str]        =							activation_function
									_a     :      Optional[Any]        =							init_std
									_a     :      str        =							encoder_layerdrop
									_a     :      int        =							decoder_layerdrop
									_a     :      Dict        =							use_cache
									_a     :      List[str]        =							encoder_layers
									_a     :      Optional[Any]        =							scale_embedding  # scale factor will be sqrt(d_model) if True
									_a     :      int        =							max_source_positions
									_a     :      Union[str, Any]        =							max_target_positions

									# Audio Classification-specific parameters. Feel free to ignore for other classes.
									_a     :      str        =							classifier_proj_size
									_a     :      str        =							use_weighted_layer_sum

									# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
									_a     :      Optional[int]        =							apply_spec_augment
									_a     :      int        =							mask_time_prob
									_a     :      str        =							mask_time_length
									_a     :      Optional[int]        =							mask_time_min_masks
									_a     :      str        =							mask_feature_prob
									_a     :      int        =							mask_feature_length
									_a     :      Dict        =							mask_feature_min_masks

									_a     :      Tuple        =							median_filter_width

									super().__init__(
									    pad_token_id=_UpperCAmelCase	,bos_token_id=_UpperCAmelCase	,eos_token_id=_UpperCAmelCase	,is_encoder_decoder=_UpperCAmelCase	,decoder_start_token_id=_UpperCAmelCase	,suppress_tokens=_UpperCAmelCase	,begin_suppress_tokens=_UpperCAmelCase	,**_UpperCAmelCase	,)

class 	__magic_name__						(    _UpperCamelCase   ):







							@property
							def 							__lowercase			(							self					:       Optional[int]		):
									_a     :      Tuple        =							OrderedDict(
									    [
									        ('input_features', {0: 'batch', 1: 'feature_size', 2: 'encoder_sequence'}),
									    ]		)
									if self.use_past:
											_a     :      Dict        =							{0: 'batch'}
									else:
											_a     :      int        =							{0: 'batch', 1: 'decoder_sequence'}

									if self.use_past:
											self.fill_with_past_key_values_(_UpperCAmelCase	,direction='inputs'		)

									return common_inputs







							def 							__lowercase			(							self					:       List[str]	,_UpperCAmelCase					:       Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]	,_UpperCAmelCase					:       int = -1	,_UpperCAmelCase					:       int = -1	,_UpperCAmelCase					:       bool = False	,_UpperCAmelCase					:       Optional["TensorType"] = None	,_UpperCAmelCase					:       int = 22050	,_UpperCAmelCase					:       float = 5.0	,_UpperCAmelCase					:       int = 220	,):
									_a     :      List[Any]        =							OrderedDict()
									_a     :      int        =							OnnxConfig.generate_dummy_inputs(
									    self	,preprocessor=preprocessor.feature_extractor	,batch_size=_UpperCAmelCase	,framework=_UpperCAmelCase	,sampling_rate=_UpperCAmelCase	,time_duration=_UpperCAmelCase	,frequency=_UpperCAmelCase	,)
									_a     :      List[Any]        =							encoder_inputs['input_features'].shape[2]
									_a     :      Optional[Any]        =							encoder_sequence_length // 2 if self.use_past else seq_length

									_a     :      Optional[int]        =							super().generate_dummy_inputs(
									    preprocessor.tokenizer	,_UpperCAmelCase	,_UpperCAmelCase	,_UpperCAmelCase	,_UpperCAmelCase		)

									_a     :      Any        =							encoder_inputs.pop('input_features'		)
									_a     :      List[Any]        =							decoder_inputs.pop('decoder_input_ids'		)

									if "past_key_values" in decoder_inputs:
											_a     :      Optional[int]        =							decoder_inputs.pop('past_key_values'		)

									return dummy_inputs







							@property
							def 							__lowercase			(							self					:       Dict		):
									return 1E-3









'''simple docstring'''

import fire
from torch.utils.data import DataLoader
from tqdm import tqdm

from transformers import AutoTokenizer
from utils import SeqaSeqDataset, pickle_save



def      __lowerCamelCase     (      lowerCAmelCase_						,			lowerCAmelCase_						,			lowerCAmelCase_=1024						,			lowerCAmelCase_=1024						,			lowerCAmelCase_=False						,			**lowerCAmelCase_  )			->      List[Any]:
		_a     :      str        =							AutoTokenizer.from_pretrained(lowerCAmelCase_  )
		_a     :      List[Any]        =							SeqaSeqDataset(lowerCAmelCase_						,			lowerCAmelCase_						,			lowerCAmelCase_						,			lowerCAmelCase_						,			type_path='train'						,			**lowerCAmelCase_  )
		_a     :      List[str]        =							tok.pad_token_id

		def get_lens(lowerCAmelCase_  ):
				_a     :      Dict        =							tqdm(
				    DataLoader(lowerCAmelCase_						,			batch_size=512						,			num_workers=8						,			shuffle=lowerCAmelCase_						,			collate_fn=ds.collate_fn  )						,			desc=str(ds.len_file  )						,			)
				_a     :      Union[str, Any]        =							[]
				for batch in dl:
						_a     :      Optional[Any]        =							batch['input_ids'].ne(lowerCAmelCase_  ).sum(1  ).tolist()
						_a     :      Optional[Any]        =							batch['labels'].ne(lowerCAmelCase_  ).sum(1  ).tolist()
						if consider_target:
								for src, tgt in zip(lowerCAmelCase_						,			lowerCAmelCase_  ):
										max_lens.append(max(lowerCAmelCase_						,			lowerCAmelCase_  )  )
						else:
								max_lens.extend(lowerCAmelCase_  )
				return max_lens

		_a     :      str        =							get_lens(lowerCAmelCase_  )
		_a     :      Optional[int]        =							SeqaSeqDataset(lowerCAmelCase_						,			lowerCAmelCase_						,			lowerCAmelCase_						,			lowerCAmelCase_						,			type_path='val'						,			**lowerCAmelCase_  )
		_a     :      Dict        =							get_lens(lowerCAmelCase_  )
		pickle_save(lowerCAmelCase_						,			train_ds.len_file  )
		pickle_save(lowerCAmelCase_						,			val_ds.len_file  )


if __name__ == "__main__":
							fire.Fire(save_len_file)




