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


_A      :				Optional[int] 				=    50358
_A      :				str 				=    50362







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


 '''simple docstring'''






 def a (				self       :     Union[str, Any]							)  ->   int:
  __lowerCAmelCase    							=						"""openai/whisper-small.en"""
  __lowerCAmelCase    							=						tempfile.mkdtemp()
 def a (				self       :     int				,	**SCREAMING_SNAKE_CASE__       :     List[str]							)  ->   Union[str, Any]:
  return WhisperTokenizer.from_pretrained(self.checkpoint				,	**SCREAMING_SNAKE_CASE__							)
 def a (				self       :     Tuple				,	**SCREAMING_SNAKE_CASE__       :     List[str]							)  ->   Dict:
  return WhisperFeatureExtractor.from_pretrained(self.checkpoint				,	**SCREAMING_SNAKE_CASE__							)
 def a (				self       :     int							)  ->   List[Any]:
  shutil.rmtree(self.tmpdirname							)
 def a (				self       :     Dict							)  ->   Dict:
  __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 a (				self       :     Tuple							)  ->   Optional[Any]:
  __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 a (				self       :     List[str]							)  ->   List[Any]:
  __lowerCAmelCase    							=						self.get_feature_extractor()
  __lowerCAmelCase    							=						self.get_tokenizer()

  __lowerCAmelCase    							=						WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__				,	feature_extractor=SCREAMING_SNAKE_CASE__							)

  __lowerCAmelCase    							=						floats_list((3, 10_00)							)

  __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 a (				self       :     Tuple							)  ->   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 a (				self       :     List[str]							)  ->   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 a (				self       :     Any							)  ->   List[str]:
  __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 a (				self       :     int							)  ->   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 a (				self       :     Union[str, Any]							)  ->   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()				,	[5_03_60, 17_70, 13, 22_64, 3_46, 3_53]							)
  self.assertEqual(SCREAMING_SNAKE_CASE__				,	"""<|startofprev|> Mr. Quilter"""							)
 def a (				self       :     List[Any]							)  ->   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()				,	[5_03_60, 2_20]							)
  self.assertEqual(SCREAMING_SNAKE_CASE__				,	"""<|startofprev|> """							)





 def a (				self       :     Any							)  ->   int:
  __lowerCAmelCase    							=						WhisperProcessor(tokenizer=self.get_tokenizer()				,	feature_extractor=self.get_feature_extractor()							)

  def _test_prompt_error_raised_helper(SCREAMING_SNAKE_CASE__       :     Union[str, Any]				,	SCREAMING_SNAKE_CASE__       :     List[str]							):
   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|>"""							)







'''simple docstring'''

import functools
from math import sqrt
from time import time







def 					UpperCamelCase_						( snake_case_	:	Dict						,		*snake_case_	:	Union[str, Any]						,		**snake_case_	:	List[str]				) ->					List[str]:






 '''simple docstring'''

 __lowerCAmelCase    							=						time()
 __lowerCAmelCase    							=						func(*snake_case_						,		**snake_case_				)
 __lowerCAmelCase    							=						time()
 if int(end - start				) > 0:
  print(f"""{func.__name__} runtime: {(end - start):0.4f} s"""				)
 else:
  print(f"""{func.__name__} runtime: {(end - start) * 10_00:0.4f} ms"""				)
 return output







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 if n == 0:
  return [0]
 __lowerCAmelCase    							=						[0, 1]
 for _ in range(n - 1				):
  fib.append(fib[-1] + fib[-2]				)
 return fib







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 def fib_recursive_term(snake_case_	:	int				) -> int:
  if i < 0:
   raise Exception("""n is negative"""				)
  if i < 2:
   return i
  return fib_recursive_term(i - 1				) + fib_recursive_term(i - 2				)

 if n < 0:
  raise Exception("""n is negative"""				)
 return [fib_recursive_term(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 @functools.cache
 def fib_recursive_term(snake_case_	:	int				) -> int:
  if i < 0:
   raise Exception("""n is negative"""				)
  if i < 2:
   return i
  return fib_recursive_term(i - 1				) + fib_recursive_term(i - 2				)

 if n < 0:
  raise Exception("""n is negative"""				)
 return [fib_recursive_term(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 # Cache must be outside recursuive function
 # other it will reset every time it calls itself.
 __lowerCAmelCase    							=						{0: 0, 1: 1, 2: 1}  # Prefilled cache

 def rec_fn_memoized(snake_case_	:	int				) -> int:
  if num in cache:
   return cache[num]

  __lowerCAmelCase    							=						rec_fn_memoized(num - 1				) + rec_fn_memoized(num - 2				)
  __lowerCAmelCase    							=						value
  return value

 return [rec_fn_memoized(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 if n >= 14_75:
  raise Exception("""n is too large"""				)
 __lowerCAmelCase    							=						sqrt(5				)
 __lowerCAmelCase    							=						(1 + sqrt_a) / 2
 return [round(phi**i / sqrt_a				) for i in range(n + 1				)]


if __name__ == "__main__":
 _A      :				Optional[Any] 				=    30
 time_func(fib_iterative, num)
 time_func(fib_recursive, num)  # Around 3s runtime
 time_func(fib_recursive_cached, num)  # Around 0ms runtime
 time_func(fib_memoization, num)
 time_func(fib_binet, num)






