
'''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


__lowercase	=    5_03_58
__lowercase	=    5_03_62

@require_torch
@require_torchaudio
@require_sentencepiece
class 							_snake_case     (  unittest.TestCase ):
	"""simple docstring"""





	def 	__A      (						self				: Optional[int]      ):
			lowerCAmelCase_   :       List[Any]    							='''openai/whisper-small.en'''
			lowerCAmelCase_   :       str    							=tempfile.mkdtemp()




	def 	__A      (						self				: Any					,						**UpperCamelCase_				: Optional[Any]      ):
			return WhisperTokenizer.from_pretrained(self.checkpoint					,						**UpperCamelCase_      )




	def 	__A      (						self				: Dict					,						**UpperCamelCase_				: int      ):
			return WhisperFeatureExtractor.from_pretrained(self.checkpoint					,						**UpperCamelCase_      )




	def 	__A      (						self				: List[Any]      ):
			shutil.rmtree(self.tmpdirname      )




	def 	__A      (						self				: List[Any]      ):
			lowerCAmelCase_   :       Tuple    							=self.get_tokenizer()
			lowerCAmelCase_   :       str    							=self.get_feature_extractor()

			lowerCAmelCase_   :       List[Any]    							=WhisperProcessor(tokenizer=UpperCamelCase_					,						feature_extractor=UpperCamelCase_      )

			processor.save_pretrained(self.tmpdirname      )
			lowerCAmelCase_   :       Optional[Any]    							=WhisperProcessor.from_pretrained(self.tmpdirname      )

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

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




	def 	__A      (						self				: Any      ):
			lowerCAmelCase_   :       List[str]    							=WhisperProcessor(tokenizer=self.get_tokenizer()					,						feature_extractor=self.get_feature_extractor()      )
			processor.save_pretrained(self.tmpdirname      )

			lowerCAmelCase_   :       Tuple    							=self.get_tokenizer(bos_token='''(BOS)'''					,						eos_token='''(EOS)'''      )
			lowerCAmelCase_   :       str    							=self.get_feature_extractor(do_normalize=UpperCamelCase_					,						padding_value=1.0      )

			lowerCAmelCase_   :       List[Any]    							=WhisperProcessor.from_pretrained(
			    self.tmpdirname					,						bos_token='''(BOS)'''					,						eos_token='''(EOS)'''					,						do_normalize=UpperCamelCase_					,						padding_value=1.0      )

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

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




	def 	__A      (						self				: str      ):
			lowerCAmelCase_   :       int    							=self.get_feature_extractor()
			lowerCAmelCase_   :       int    							=self.get_tokenizer()

			lowerCAmelCase_   :       Any    							=WhisperProcessor(tokenizer=UpperCamelCase_					,						feature_extractor=UpperCamelCase_      )

			lowerCAmelCase_   :       Optional[int]    							=floats_list((3, 1000)      )

			lowerCAmelCase_   :       List[Any]    							=feature_extractor(UpperCamelCase_					,						return_tensors='''np'''      )
			lowerCAmelCase_   :       Optional[int]    							=processor(UpperCamelCase_					,						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 	__A      (						self				: Any      ):
			lowerCAmelCase_   :       Optional[Any]    							=self.get_feature_extractor()
			lowerCAmelCase_   :       Any    							=self.get_tokenizer()

			lowerCAmelCase_   :       Dict    							=WhisperProcessor(tokenizer=UpperCamelCase_					,						feature_extractor=UpperCamelCase_      )

			lowerCAmelCase_   :       Optional[int]    							='''This is a test string'''

			lowerCAmelCase_   :       int    							=processor(text=UpperCamelCase_      )

			lowerCAmelCase_   :       Union[str, Any]    							=tokenizer(UpperCamelCase_      )

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




	def 	__A      (						self				: Optional[Any]      ):
			lowerCAmelCase_   :       Optional[int]    							=self.get_feature_extractor()
			lowerCAmelCase_   :       List[str]    							=self.get_tokenizer()

			lowerCAmelCase_   :       List[str]    							=WhisperProcessor(tokenizer=UpperCamelCase_					,						feature_extractor=UpperCamelCase_      )

			lowerCAmelCase_   :       int    							=[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

			lowerCAmelCase_   :       Union[str, Any]    							=processor.batch_decode(UpperCamelCase_      )
			lowerCAmelCase_   :       str    							=tokenizer.batch_decode(UpperCamelCase_      )

			self.assertListEqual(UpperCamelCase_					,						UpperCamelCase_      )




	def 	__A      (						self				: Union[str, Any]      ):
			lowerCAmelCase_   :       Union[str, Any]    							=self.get_feature_extractor()
			lowerCAmelCase_   :       Any    							=self.get_tokenizer()

			lowerCAmelCase_   :       Optional[int]    							=WhisperProcessor(tokenizer=UpperCamelCase_					,						feature_extractor=UpperCamelCase_      )

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




	def 	__A      (						self				: List[Any]      ):
			lowerCAmelCase_   :       Any    							=self.get_feature_extractor()
			lowerCAmelCase_   :       Optional[int]    							=self.get_tokenizer()

			lowerCAmelCase_   :       Dict    							=WhisperProcessor(tokenizer=UpperCamelCase_					,						feature_extractor=UpperCamelCase_      )
			lowerCAmelCase_   :       Union[str, Any]    							=processor.get_decoder_prompt_ids(task='''transcribe'''					,						no_timestamps=UpperCamelCase_      )

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

			lowerCAmelCase_   :       Union[str, Any]    							=[TRANSCRIBE, NOTIMESTAMPS]
			self.assertListEqual([ids[-1] for ids in forced_decoder_ids]					,						UpperCamelCase_      )




	def 	__A      (						self				: str      ):
			lowerCAmelCase_   :       List[str]    							=WhisperProcessor(tokenizer=self.get_tokenizer()					,						feature_extractor=self.get_feature_extractor()      )
			lowerCAmelCase_   :       Optional[Any]    							=processor.get_prompt_ids('''Mr. Quilter'''      )
			lowerCAmelCase_   :       List[Any]    							=processor.tokenizer.decode(UpperCamelCase_      )

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




	def 	__A      (						self				: List[str]      ):
			lowerCAmelCase_   :       Any    							=WhisperProcessor(tokenizer=self.get_tokenizer()					,						feature_extractor=self.get_feature_extractor()      )
			lowerCAmelCase_   :       Tuple    							=processor.get_prompt_ids(''''''      )
			lowerCAmelCase_   :       List[str]    							=processor.tokenizer.decode(UpperCamelCase_      )

			self.assertListEqual(prompt_ids.tolist()					,						[50360, 220]      )
			self.assertEqual(UpperCamelCase_					,						'''<|startofprev|> '''      )





	def 	__A      (						self				: str      ):
			lowerCAmelCase_   :       Dict    							=WhisperProcessor(tokenizer=self.get_tokenizer()					,						feature_extractor=self.get_feature_extractor()      )

			def _test_prompt_error_raised_helper(UpperCamelCase_				: List[str]					,						UpperCamelCase_				: Optional[Any]      ):
					with pytest.raises(UpperCamelCase_      ) as excinfo:
							processor.get_prompt_ids(UpperCamelCase_      )
					lowerCAmelCase_   :       Tuple    							=F'Encountered text in the prompt corresponding to disallowed special token: {special_token}.'
					self.assertEqual(UpperCamelCase_					,						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 ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowercase	=    logging.get_logger(__name__)

__lowercase	=    {
    '''google/pegasus-x-base''': '''https://huggingface.co/google/pegasus-x-base/resolve/main/config.json''',
    '''google/pegasus-x-large''': '''https://huggingface.co/google/pegasus-x-large/resolve/main/config.json''',
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}

class 							_snake_case     (  lowerCAmelCase_ ):
	"""simple docstring"""





	_UpperCamelCase							:  Dict								=		'''pegasus_x'''
	_UpperCamelCase							:  Optional[int]								=		['''past_key_values''']
	_UpperCamelCase							:  str								=		{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}




	def __init__(						self				: Any					,						UpperCamelCase_				: Tuple=96103					,						UpperCamelCase_				: List[str]=16384					,						UpperCamelCase_				: Any=16					,						UpperCamelCase_				: Dict=4096					,						UpperCamelCase_				: str=16					,						UpperCamelCase_				: Union[str, Any]=16					,						UpperCamelCase_				: str=4096					,						UpperCamelCase_				: Any=16					,						UpperCamelCase_				: str=0.0					,						UpperCamelCase_				: Dict=0.0					,						UpperCamelCase_				: Union[str, Any]=True					,						UpperCamelCase_				: Union[str, Any]=True					,						UpperCamelCase_				: List[str]="gelu"					,						UpperCamelCase_				: Dict=1024					,						UpperCamelCase_				: str=0.1					,						UpperCamelCase_				: str=0.0					,						UpperCamelCase_				: Optional[Any]=0.0					,						UpperCamelCase_				: str=0.0_2					,						UpperCamelCase_				: Optional[Any]=0					,						UpperCamelCase_				: Tuple=True					,						UpperCamelCase_				: Tuple=0					,						UpperCamelCase_				: Optional[int]=1					,						UpperCamelCase_				: List[Any]=1					,						UpperCamelCase_				: List[Any]=32					,						UpperCamelCase_				: Optional[int]=512					,						UpperCamelCase_				: Any=True					,						**UpperCamelCase_				: Dict					,						):
			lowerCAmelCase_   :       List[Any]    							=vocab_size
			lowerCAmelCase_   :       Any    							=max_position_embeddings
			lowerCAmelCase_   :       int    							=d_model
			lowerCAmelCase_   :       List[Any]    							=encoder_ffn_dim
			lowerCAmelCase_   :       Union[str, Any]    							=encoder_layers
			lowerCAmelCase_   :       Tuple    							=encoder_attention_heads
			lowerCAmelCase_   :       List[Any]    							=decoder_ffn_dim
			lowerCAmelCase_   :       List[Any]    							=decoder_layers
			lowerCAmelCase_   :       List[str]    							=decoder_attention_heads
			lowerCAmelCase_   :       str    							=dropout
			lowerCAmelCase_   :       Any    							=attention_dropout
			lowerCAmelCase_   :       Any    							=activation_dropout
			lowerCAmelCase_   :       Optional[int]    							=activation_function
			lowerCAmelCase_   :       List[Any]    							=init_std
			lowerCAmelCase_   :       List[str]    							=encoder_layerdrop
			lowerCAmelCase_   :       Optional[int]    							=decoder_layerdrop
			lowerCAmelCase_   :       int    							=use_cache
			lowerCAmelCase_   :       Union[str, Any]    							=encoder_layers
			lowerCAmelCase_   :       Tuple    							=scale_embedding  # scale factor will be sqrt(d_model) if True

			lowerCAmelCase_   :       Optional[int]    							=num_global_tokens
			lowerCAmelCase_   :       List[str]    							=block_size
			lowerCAmelCase_   :       Optional[int]    							=stagger_local_blocks

			super().__init__(
			    pad_token_id=UpperCamelCase_					,						eos_token_id=UpperCamelCase_					,						is_encoder_decoder=UpperCamelCase_					,						decoder_start_token_id=UpperCamelCase_					,						forced_eos_token_id=UpperCamelCase_					,						**UpperCamelCase_					,						)




	@property
	def 	__A      (						self				: List[str]      ):
			return self.encoder_attention_heads





	@property
	def 	__A      (						self				: int      ):
			return self.d_model






