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_0_3_5_8
_SCREAMING_SNAKE_CASE   		=			5_0_3_6_2
@require_torch
@require_torchaudio
@require_sentencepiece
class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   def 	lowerCamelCase_  (    self	:	List[Any]							):





    """simple docstring"""


    UpperCamelCase      =			"""openai/whisper-small.en"""
    UpperCamelCase      =			tempfile.mkdtemp()







   def 	lowerCamelCase_  (    self	:	List[Any]      ,      **lowerCamelCase_	:	List[Any]							):





    """simple docstring"""


    return WhisperTokenizer.from_pretrained(self.checkpoint      ,      **lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	Dict      ,      **lowerCamelCase_	:	List[Any]							):





    """simple docstring"""


    return WhisperFeatureExtractor.from_pretrained(self.checkpoint      ,      **lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	Union[str, Any]							):





    """simple docstring"""


    shutil.rmtree(self.tmpdirname							)







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase      =			self.get_tokenizer()
    UpperCamelCase      =			self.get_feature_extractor()

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

    processor.save_pretrained(self.tmpdirname							)
    UpperCamelCase      =			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 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    UpperCamelCase      =			WhisperProcessor(tokenizer=self.get_tokenizer()      ,      feature_extractor=self.get_feature_extractor()							)
    processor.save_pretrained(self.tmpdirname							)

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

    UpperCamelCase      =			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 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    UpperCamelCase      =			self.get_feature_extractor()
    UpperCamelCase      =			self.get_tokenizer()

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

    UpperCamelCase      =			floats_list((3, 1000)							)

    UpperCamelCase      =			feature_extractor(lowerCamelCase_      ,      return_tensors="""np"""							)
    UpperCamelCase      =			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 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    UpperCamelCase      =			self.get_feature_extractor()
    UpperCamelCase      =			self.get_tokenizer()

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

    UpperCamelCase      =			"""This is a test string"""

    UpperCamelCase      =			processor(text=lowerCamelCase_							)

    UpperCamelCase      =			tokenizer(lowerCamelCase_							)

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







   def 	lowerCamelCase_  (    self	:	Tuple							):





    """simple docstring"""


    UpperCamelCase      =			self.get_feature_extractor()
    UpperCamelCase      =			self.get_tokenizer()

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

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

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

    self.assertListEqual(lowerCamelCase_      ,      lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase      =			self.get_feature_extractor()
    UpperCamelCase      =			self.get_tokenizer()

    UpperCamelCase      =			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 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			self.get_feature_extractor()
    UpperCamelCase      =			self.get_tokenizer()

    UpperCamelCase      =			WhisperProcessor(tokenizer=lowerCamelCase_      ,      feature_extractor=lowerCamelCase_							)
    UpperCamelCase      =			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)							)

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







   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    UpperCamelCase      =			WhisperProcessor(tokenizer=self.get_tokenizer()      ,      feature_extractor=self.get_feature_extractor()							)
    UpperCamelCase      =			processor.get_prompt_ids("""Mr. Quilter"""							)
    UpperCamelCase      =			processor.tokenizer.decode(lowerCamelCase_							)

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







   def 	lowerCamelCase_  (    self	:	Optional[Any]							):





    """simple docstring"""


    UpperCamelCase      =			WhisperProcessor(tokenizer=self.get_tokenizer()      ,      feature_extractor=self.get_feature_extractor()							)
    UpperCamelCase      =			processor.get_prompt_ids(""""""							)
    UpperCamelCase      =			processor.tokenizer.decode(lowerCamelCase_							)

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







   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			WhisperProcessor(tokenizer=self.get_tokenizer()      ,      feature_extractor=self.get_feature_extractor()							)

    def _test_prompt_error_raised_helper(lowerCamelCase_	:	Tuple      ,      lowerCamelCase_	:	Optional[int]							):
     with pytest.raises(lowerCamelCase_							) as excinfo:
      processor.get_prompt_ids(lowerCamelCase_							)
     UpperCamelCase      =			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|>"""							)

import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
     import transformer_engine.pytorch as te





def  lowercase(  UpperCamelCase_       ,				UpperCamelCase_=True       ,				UpperCamelCase_=True       ,				UpperCamelCase_=True					)		->  int:







 '''simple docstring'''
 if not is_fpa_available():
  raise ImportError("""Using `convert_model` requires transformer_engine to be installed."""					)
 for name, module in model.named_children():
  if isinstance(UpperCamelCase_       ,				nn.Linear					) and to_transformer_engine and _convert_linear:
   # Return early if the linear layer weights are not multiples of 16
   if any(p % 16 != 0 for p in module.weight.shape					):
    return
   UpperCamelCase      =			module.bias is not None
   UpperCamelCase      =			te.Linear(
       module.in_features       ,				module.out_features       ,				bias=UpperCamelCase_       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   if has_bias:
    UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  elif isinstance(UpperCamelCase_       ,				nn.LayerNorm					) and to_transformer_engine and _convert_ln:
   UpperCamelCase      =			te.LayerNorm(module.normalized_shape[0]       ,				eps=module.eps       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  elif isinstance(UpperCamelCase_       ,				te.Linear					) and not to_transformer_engine and _convert_linear:
   UpperCamelCase      =			module.bias is not None
   UpperCamelCase      =			nn.Linear(
       module.in_features       ,				module.out_features       ,				bias=UpperCamelCase_       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   if has_bias:
    UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  elif isinstance(UpperCamelCase_       ,				te.LayerNorm					) and not to_transformer_engine and _convert_ln:
   UpperCamelCase      =			nn.LayerNorm(module.normalized_shape[0]       ,				eps=module.eps       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  else:
   convert_model(
       UpperCamelCase_       ,				to_transformer_engine=UpperCamelCase_       ,				_convert_linear=_convert_linear       ,				_convert_ln=_convert_ln       ,				)





def  lowercase(  UpperCamelCase_					)		->  Any:







 '''simple docstring'''
 if not is_fpa_available():
  raise ImportError("""Using `has_transformer_engine_layers` requires transformer_engine to be installed."""					)
 for m in model.modules():
  if isinstance(UpperCamelCase_       ,				(te.LayerNorm, te.Linear)					):
   return True
 return False
