
"""simple docstring"""


import shutil
import tempfile
import unittest

import pytest

from transformers import WhisperTokenizer, is_speech_available
from transformers.testing_utils import require_sentencepiece, require_torch, require_torchaudio

from .test_feature_extraction_whisper import floats_list


if is_speech_available():
						from transformers import WhisperFeatureExtractor, WhisperProcessor


SCREAMING_SNAKE_CASE__						       =				5_0358
SCREAMING_SNAKE_CASE__						       =				5_0362




@require_torch
@require_torchaudio
@require_sentencepiece
class    __lowerCamelCase     (   unittest.TestCase ):




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     '''openai/whisper-small.en'''
							lowerCamelCase_ 		=     tempfile.mkdtemp()




				def 					UpperCAmelCase__	(							self       ,	**UpperCAmelCase      ):
							return WhisperTokenizer.from_pretrained(self.checkpoint       ,	**lowerCAmelCase_      )




				def 					UpperCAmelCase__	(							self       ,	**UpperCAmelCase      ):
							return WhisperFeatureExtractor.from_pretrained(self.checkpoint       ,	**lowerCAmelCase_      )




				def 					UpperCAmelCase__	(							self      ):
							shutil.rmtree(self.tmpdirname      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.get_tokenizer()
							lowerCamelCase_ 		=     self.get_feature_extractor()

							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=lowerCAmelCase_       ,	feature_extractor=lowerCAmelCase_      )

							processor.save_pretrained(self.tmpdirname      )
							lowerCamelCase_ 		=     WhisperProcessor.from_pretrained(self.tmpdirname      )

							self.assertEqual(processor.tokenizer.get_vocab()       ,	tokenizer.get_vocab()      )
							self.assertIsInstance(processor.tokenizer       ,	lowerCAmelCase_      )

							self.assertEqual(processor.feature_extractor.to_json_string()       ,	feature_extractor.to_json_string()      )
							self.assertIsInstance(processor.feature_extractor       ,	lowerCAmelCase_      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=self.get_tokenizer()       ,	feature_extractor=self.get_feature_extractor()      )
							processor.save_pretrained(self.tmpdirname      )

							lowerCamelCase_ 		=     self.get_tokenizer(bos_token='''(BOS)'''       ,	eos_token='''(EOS)'''      )
							lowerCamelCase_ 		=     self.get_feature_extractor(do_normalize=lowerCAmelCase_       ,	padding_value=1.0      )

							lowerCamelCase_ 		=     WhisperProcessor.from_pretrained(
							    self.tmpdirname       ,	bos_token='''(BOS)'''       ,	eos_token='''(EOS)'''       ,	do_normalize=lowerCAmelCase_       ,	padding_value=1.0      )

							self.assertEqual(processor.tokenizer.get_vocab()       ,	tokenizer_add_kwargs.get_vocab()      )
							self.assertIsInstance(processor.tokenizer       ,	lowerCAmelCase_      )

							self.assertEqual(processor.feature_extractor.to_json_string()       ,	feature_extractor_add_kwargs.to_json_string()      )
							self.assertIsInstance(processor.feature_extractor       ,	lowerCAmelCase_      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.get_feature_extractor()
							lowerCamelCase_ 		=     self.get_tokenizer()

							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=lowerCAmelCase_       ,	feature_extractor=lowerCAmelCase_      )

							lowerCamelCase_ 		=     floats_list((3, 1000)      )

							lowerCamelCase_ 		=     feature_extractor(lowerCAmelCase_       ,	return_tensors='''np'''      )
							lowerCamelCase_ 		=     processor(lowerCAmelCase_       ,	return_tensors='''np'''      )

							for key in input_feat_extract.keys():
										self.assertAlmostEqual(input_feat_extract[key].sum()       ,	input_processor[key].sum()       ,	delta=1e-2      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.get_feature_extractor()
							lowerCamelCase_ 		=     self.get_tokenizer()

							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=lowerCAmelCase_       ,	feature_extractor=lowerCAmelCase_      )

							lowerCamelCase_ 		=     '''This is a test string'''

							lowerCamelCase_ 		=     processor(text=lowerCAmelCase_      )

							lowerCamelCase_ 		=     tokenizer(lowerCAmelCase_      )

							for key in encoded_tok.keys():
										self.assertListEqual(encoded_tok[key]       ,	encoded_processor[key]      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.get_feature_extractor()
							lowerCamelCase_ 		=     self.get_tokenizer()

							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=lowerCAmelCase_       ,	feature_extractor=lowerCAmelCase_      )

							lowerCamelCase_ 		=     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							lowerCamelCase_ 		=     processor.batch_decode(lowerCAmelCase_      )
							lowerCamelCase_ 		=     tokenizer.batch_decode(lowerCAmelCase_      )

							self.assertListEqual(lowerCAmelCase_       ,	lowerCAmelCase_      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.get_feature_extractor()
							lowerCamelCase_ 		=     self.get_tokenizer()

							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=lowerCAmelCase_       ,	feature_extractor=lowerCAmelCase_      )

							self.assertListEqual(
							    processor.model_input_names       ,	feature_extractor.model_input_names       ,	msg='''`processor` and `feature_extractor` model input names do not match'''       ,	)




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     self.get_feature_extractor()
							lowerCamelCase_ 		=     self.get_tokenizer()

							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=lowerCAmelCase_       ,	feature_extractor=lowerCAmelCase_      )
							lowerCamelCase_ 		=     processor.get_decoder_prompt_ids(task='''transcribe'''       ,	no_timestamps=lowerCAmelCase_      )

							self.assertIsInstance(lowerCAmelCase_       ,	lowerCAmelCase_      )
							for ids in forced_decoder_ids:
										self.assertIsInstance(lowerCAmelCase_       ,	(list, tuple)      )

							lowerCamelCase_ 		=     [TRANSCRIBE, NOTIMESTAMPS]
							self.assertListEqual([ids[-1] for ids in forced_decoder_ids]       ,	lowerCAmelCase_      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=self.get_tokenizer()       ,	feature_extractor=self.get_feature_extractor()      )
							lowerCamelCase_ 		=     processor.get_prompt_ids('''Mr. Quilter'''      )
							lowerCamelCase_ 		=     processor.tokenizer.decode(lowerCAmelCase_      )

							self.assertListEqual(prompt_ids.tolist()       ,	[5_0360, 1770, 13, 2264, 346, 353]      )
							self.assertEqual(lowerCAmelCase_       ,	'''<|startofprev|> Mr. Quilter'''      )




				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=self.get_tokenizer()       ,	feature_extractor=self.get_feature_extractor()      )
							lowerCamelCase_ 		=     processor.get_prompt_ids(''''''      )
							lowerCamelCase_ 		=     processor.tokenizer.decode(lowerCAmelCase_      )

							self.assertListEqual(prompt_ids.tolist()       ,	[5_0360, 220]      )
							self.assertEqual(lowerCAmelCase_       ,	'''<|startofprev|> '''      )





				def 					UpperCAmelCase__	(							self      ):
							lowerCamelCase_ 		=     WhisperProcessor(tokenizer=self.get_tokenizer()       ,	feature_extractor=self.get_feature_extractor()      )

							def _test_prompt_error_raised_helper(UpperCAmelCase       ,	UpperCAmelCase      ):
										with pytest.raises(lowerCAmelCase_      ) as excinfo:
													processor.get_prompt_ids(lowerCAmelCase_      )
										lowerCamelCase_ 		=     f"Encountered text in the prompt corresponding to disallowed special token: {special_token}."
										self.assertEqual(lowerCAmelCase_       ,	str(excinfo.value      )      )

							_test_prompt_error_raised_helper('''<|startofprev|> test'''       ,	'''<|startofprev|>'''      )
							_test_prompt_error_raised_helper('''test <|notimestamps|>'''       ,	'''<|notimestamps|>'''      )
							_test_prompt_error_raised_helper('''test <|zh|> test <|transcribe|>'''       ,	'''<|zh|>'''      )





"""simple docstring"""


from collections import OrderedDict
from typing import Mapping

from packaging import version

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


A_						       =				logging.get_logger(__name__)

A_						       =				{
    """MBZUAI/swiftformer-xs""": """https://huggingface.co/MBZUAI/swiftformer-xs/resolve/main/config.json""",
}




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			Any									=						'swiftformer'




				def __init__(							self       ,	UpperCAmelCase=3       ,	UpperCAmelCase=[3, 3, 6, 4]       ,	UpperCAmelCase=[48, 56, 112, 220]       ,	UpperCAmelCase=4       ,	UpperCAmelCase=[True, True, True, True]       ,	UpperCAmelCase="gelu"       ,	UpperCAmelCase=3       ,	UpperCAmelCase=2       ,	UpperCAmelCase=1       ,	UpperCAmelCase=0.0       ,	UpperCAmelCase=True       ,	UpperCAmelCase=1e-5       ,	UpperCAmelCase=1e-5       ,	**UpperCAmelCase       ,	):
							super().__init__(**UpperCAmelCase      )
							lowerCamelCase_ 		=     num_channels
							lowerCamelCase_ 		=     depths
							lowerCamelCase_ 		=     embed_dims
							lowerCamelCase_ 		=     mlp_ratio
							lowerCamelCase_ 		=     downsamples
							lowerCamelCase_ 		=     hidden_act
							lowerCamelCase_ 		=     down_patch_size
							lowerCamelCase_ 		=     down_stride
							lowerCamelCase_ 		=     down_pad
							lowerCamelCase_ 		=     drop_path_rate
							lowerCamelCase_ 		=     use_layer_scale
							lowerCamelCase_ 		=     layer_scale_init_value
							lowerCamelCase_ 		=     batch_norm_eps




class    __lowerCamelCase     (   lowerCAmelCase ):
				a__:			str									=						version.parse('1.11' )




				@property
				def 					UpperCAmelCase__	(							self      ):
							return OrderedDict(
							    [
							        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
							    ]      )





				@property
				def 					UpperCAmelCase__	(							self      ):
							return 1e-4



