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__    :       Optional[Any]						=			5_0_3_5_8
lowercase__    :       Tuple						=			5_0_3_6_2




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



	"""simple docstring"""





	def     SCREAMING_SNAKE_CASE_       (							self       )		->Tuple:
								lowerCAmelCase				=	'''openai/whisper-small.en'''
								lowerCAmelCase				=	tempfile.mkdtemp()


	def     SCREAMING_SNAKE_CASE_       (							self	,    **__SCREAMING_SNAKE_CASE       )		->Optional[int]:
								return WhisperTokenizer.from_pretrained(self.checkpoint	,    **__SCREAMING_SNAKE_CASE       )


	def     SCREAMING_SNAKE_CASE_       (							self	,    **__SCREAMING_SNAKE_CASE       )		->Optional[int]:
								return WhisperFeatureExtractor.from_pretrained(self.checkpoint	,    **__SCREAMING_SNAKE_CASE       )


	def     SCREAMING_SNAKE_CASE_       (							self       )		->Union[str, Any]:
								shutil.rmtree(self.tmpdirname       )


	def     SCREAMING_SNAKE_CASE_       (							self       )		->str:
								lowerCAmelCase				=	self.get_tokenizer()
								lowerCAmelCase				=	self.get_feature_extractor()

								lowerCAmelCase				=	WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE	,    feature_extractor=__SCREAMING_SNAKE_CASE       )

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

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

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


	def     SCREAMING_SNAKE_CASE_       (							self       )		->Tuple:
								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=__SCREAMING_SNAKE_CASE	,    padding_value=1.0       )

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

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

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


	def     SCREAMING_SNAKE_CASE_       (							self       )		->Optional[int]:
								lowerCAmelCase				=	self.get_feature_extractor()
								lowerCAmelCase				=	self.get_tokenizer()

								lowerCAmelCase				=	WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE	,    feature_extractor=__SCREAMING_SNAKE_CASE       )

								lowerCAmelCase				=	floats_list((3, 1000)       )

								lowerCAmelCase				=	feature_extractor(__SCREAMING_SNAKE_CASE	,    return_tensors='''np'''       )
								lowerCAmelCase				=	processor(__SCREAMING_SNAKE_CASE	,    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     SCREAMING_SNAKE_CASE_       (							self       )		->List[Any]:
								lowerCAmelCase				=	self.get_feature_extractor()
								lowerCAmelCase				=	self.get_tokenizer()

								lowerCAmelCase				=	WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE	,    feature_extractor=__SCREAMING_SNAKE_CASE       )

								lowerCAmelCase				=	'''This is a test string'''

								lowerCAmelCase				=	processor(text=__SCREAMING_SNAKE_CASE       )

								lowerCAmelCase				=	tokenizer(__SCREAMING_SNAKE_CASE       )

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


	def     SCREAMING_SNAKE_CASE_       (							self       )		->List[Any]:
								lowerCAmelCase				=	self.get_feature_extractor()
								lowerCAmelCase				=	self.get_tokenizer()

								lowerCAmelCase				=	WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE	,    feature_extractor=__SCREAMING_SNAKE_CASE       )

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

								lowerCAmelCase				=	processor.batch_decode(__SCREAMING_SNAKE_CASE       )
								lowerCAmelCase				=	tokenizer.batch_decode(__SCREAMING_SNAKE_CASE       )

								self.assertListEqual(__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )


	def     SCREAMING_SNAKE_CASE_       (							self       )		->Optional[Any]:
								lowerCAmelCase				=	self.get_feature_extractor()
								lowerCAmelCase				=	self.get_tokenizer()

								lowerCAmelCase				=	WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE	,    feature_extractor=__SCREAMING_SNAKE_CASE       )

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


	def     SCREAMING_SNAKE_CASE_       (							self       )		->List[str]:
								lowerCAmelCase				=	self.get_feature_extractor()
								lowerCAmelCase				=	self.get_tokenizer()

								lowerCAmelCase				=	WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE	,    feature_extractor=__SCREAMING_SNAKE_CASE       )
								lowerCAmelCase				=	processor.get_decoder_prompt_ids(task='''transcribe'''	,    no_timestamps=__SCREAMING_SNAKE_CASE       )

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

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


	def     SCREAMING_SNAKE_CASE_       (							self       )		->List[str]:
								lowerCAmelCase				=	WhisperProcessor(tokenizer=self.get_tokenizer()	,    feature_extractor=self.get_feature_extractor()       )
								lowerCAmelCase				=	processor.get_prompt_ids('''Mr. Quilter'''       )
								lowerCAmelCase				=	processor.tokenizer.decode(__SCREAMING_SNAKE_CASE       )

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


	def     SCREAMING_SNAKE_CASE_       (							self       )		->List[Any]:
								lowerCAmelCase				=	WhisperProcessor(tokenizer=self.get_tokenizer()	,    feature_extractor=self.get_feature_extractor()       )
								lowerCAmelCase				=	processor.get_prompt_ids(''''''       )
								lowerCAmelCase				=	processor.tokenizer.decode(__SCREAMING_SNAKE_CASE       )

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




	def     SCREAMING_SNAKE_CASE_       (							self       )		->Dict:
								lowerCAmelCase				=	WhisperProcessor(tokenizer=self.get_tokenizer()	,    feature_extractor=self.get_feature_extractor()       )

								def _test_prompt_error_raised_helper(__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       ):
															with pytest.raises(__SCREAMING_SNAKE_CASE       ) as excinfo:
																						processor.get_prompt_ids(__SCREAMING_SNAKE_CASE       )
															lowerCAmelCase				=	F"Encountered text in the prompt corresponding to disallowed special token: {special_token}."
															self.assertEqual(__SCREAMING_SNAKE_CASE	,    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|>'''       )




from typing import Callable, Optional, Union

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


lowercase__    :       Optional[Any]						=			logging.get_logger(__name__)

lowercase__    :       Union[str, Any]						=			{
    '''microsoft/prophetnet-large-uncased''': (
        '''https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json'''
    ),
}




class      lowercase_						(						UpperCamelCase_		):



	"""simple docstring"""





	UpperCAmelCase_						:      Optional[int]     =       """prophetnet"""
	UpperCAmelCase_						:      Optional[int]     =       ["""past_key_values"""]
	UpperCAmelCase_						:      Any     =       {
	    """num_attention_heads""": """num_encoder_attention_heads""",
	}


	def __init__(							self	,    __SCREAMING_SNAKE_CASE = 0.1	,    __SCREAMING_SNAKE_CASE = "gelu"	,    __SCREAMING_SNAKE_CASE = 30522	,    __SCREAMING_SNAKE_CASE = 1024	,    __SCREAMING_SNAKE_CASE = 4096	,    __SCREAMING_SNAKE_CASE = 12	,    __SCREAMING_SNAKE_CASE = 16	,    __SCREAMING_SNAKE_CASE = 4096	,    __SCREAMING_SNAKE_CASE = 12	,    __SCREAMING_SNAKE_CASE = 16	,    __SCREAMING_SNAKE_CASE = 0.1	,    __SCREAMING_SNAKE_CASE = 0.1	,    __SCREAMING_SNAKE_CASE = 512	,    __SCREAMING_SNAKE_CASE = 0.0_2	,    __SCREAMING_SNAKE_CASE = True	,    __SCREAMING_SNAKE_CASE = True	,    __SCREAMING_SNAKE_CASE = 0	,    __SCREAMING_SNAKE_CASE = 2	,    __SCREAMING_SNAKE_CASE = 32	,    __SCREAMING_SNAKE_CASE = 128	,    __SCREAMING_SNAKE_CASE = False	,    __SCREAMING_SNAKE_CASE = 0.0	,    __SCREAMING_SNAKE_CASE = True	,    __SCREAMING_SNAKE_CASE = 0	,    __SCREAMING_SNAKE_CASE = 1	,    __SCREAMING_SNAKE_CASE = 2	,    **__SCREAMING_SNAKE_CASE	,    )		->int:
								lowerCAmelCase				=	vocab_size
								lowerCAmelCase				=	hidden_size
								lowerCAmelCase				=	encoder_ffn_dim
								lowerCAmelCase				=	num_encoder_layers
								lowerCAmelCase				=	num_encoder_attention_heads
								lowerCAmelCase				=	decoder_ffn_dim
								lowerCAmelCase				=	num_decoder_layers
								lowerCAmelCase				=	num_decoder_attention_heads
								lowerCAmelCase				=	max_position_embeddings
								lowerCAmelCase				=	init_std  # Normal(0, this parameter)
								lowerCAmelCase				=	activation_function

								# parameters for prophetnet
								lowerCAmelCase				=	ngram
								lowerCAmelCase				=	num_buckets
								lowerCAmelCase				=	relative_max_distance
								lowerCAmelCase				=	disable_ngram_loss
								lowerCAmelCase				=	eps

								# 3 Types of Dropout
								lowerCAmelCase				=	attention_dropout
								lowerCAmelCase				=	activation_dropout
								lowerCAmelCase				=	dropout

								lowerCAmelCase				=	use_cache

								super().__init__(
								    pad_token_id=__SCREAMING_SNAKE_CASE	,    bos_token_id=__SCREAMING_SNAKE_CASE	,    eos_token_id=__SCREAMING_SNAKE_CASE	,    is_encoder_decoder=__SCREAMING_SNAKE_CASE	,    add_cross_attention=__SCREAMING_SNAKE_CASE	,    decoder_start_token_id=__SCREAMING_SNAKE_CASE	,    **__SCREAMING_SNAKE_CASE	,    )


	@property
	def     SCREAMING_SNAKE_CASE_       (							self       )		->int:
								return self.num_encoder_layers + self.num_decoder_layers




	@num_hidden_layers.setter
	def     SCREAMING_SNAKE_CASE_       (							self	,    __SCREAMING_SNAKE_CASE       )		->Union[str, Any]:
								raise NotImplementedError(
								    '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and'''
								    ''' `num_decoder_layers`.'''       )



