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


_snake_case					: int     			=					50_358
_snake_case					: Any     			=					50_362


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




		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			"openai/whisper-small.en"
								_SCREAMING_SNAKE_CASE					=			tempfile.mkdtemp()




		def      lowercase							(					self       ,    **UpperCamelCase   ):
								return WhisperTokenizer.from_pretrained(self.checkpoint       ,    **UpperCamelCase   )




		def      lowercase							(					self       ,    **UpperCamelCase   ):
								return WhisperFeatureExtractor.from_pretrained(self.checkpoint       ,    **UpperCamelCase   )




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




		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			self.get_tokenizer()
								_SCREAMING_SNAKE_CASE					=			self.get_feature_extractor()

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

								processor.save_pretrained(self.tmpdirname   )
								_SCREAMING_SNAKE_CASE					=			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      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			WhisperProcessor(tokenizer=self.get_tokenizer()       ,    feature_extractor=self.get_feature_extractor()   )
								processor.save_pretrained(self.tmpdirname   )

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

								_SCREAMING_SNAKE_CASE					=			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      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			self.get_feature_extractor()
								_SCREAMING_SNAKE_CASE					=			self.get_tokenizer()

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

								_SCREAMING_SNAKE_CASE					=			floats_list((3, 1_000)   )

								_SCREAMING_SNAKE_CASE					=			feature_extractor(UpperCamelCase       ,    return_tensors="np"   )
								_SCREAMING_SNAKE_CASE					=			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      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			self.get_feature_extractor()
								_SCREAMING_SNAKE_CASE					=			self.get_tokenizer()

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

								_SCREAMING_SNAKE_CASE					=			"This is a test string"

								_SCREAMING_SNAKE_CASE					=			processor(text=UpperCamelCase   )

								_SCREAMING_SNAKE_CASE					=			tokenizer(UpperCamelCase   )

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




		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			self.get_feature_extractor()
								_SCREAMING_SNAKE_CASE					=			self.get_tokenizer()

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

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

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

								self.assertListEqual(UpperCamelCase       ,    UpperCamelCase   )




		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			self.get_feature_extractor()
								_SCREAMING_SNAKE_CASE					=			self.get_tokenizer()

								_SCREAMING_SNAKE_CASE					=			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      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			self.get_feature_extractor()
								_SCREAMING_SNAKE_CASE					=			self.get_tokenizer()

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

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




		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			WhisperProcessor(tokenizer=self.get_tokenizer()       ,    feature_extractor=self.get_feature_extractor()   )
								_SCREAMING_SNAKE_CASE					=			processor.get_prompt_ids("Mr. Quilter"   )
								_SCREAMING_SNAKE_CASE					=			processor.tokenizer.decode(UpperCamelCase   )

								self.assertListEqual(prompt_ids.tolist()       ,    [50_360, 1_770, 13, 2_264, 346, 353]   )
								self.assertEqual(UpperCamelCase       ,    "<|startofprev|> Mr. Quilter"   )




		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			WhisperProcessor(tokenizer=self.get_tokenizer()       ,    feature_extractor=self.get_feature_extractor()   )
								_SCREAMING_SNAKE_CASE					=			processor.get_prompt_ids(""   )
								_SCREAMING_SNAKE_CASE					=			processor.tokenizer.decode(UpperCamelCase   )

								self.assertListEqual(prompt_ids.tolist()       ,    [50_360, 220]   )
								self.assertEqual(UpperCamelCase       ,    "<|startofprev|> "   )




		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			WhisperProcessor(tokenizer=self.get_tokenizer()       ,    feature_extractor=self.get_feature_extractor()   )

								def _test_prompt_error_raised_helper(UpperCamelCase       ,    UpperCamelCase   ):
														with pytest.raises(UpperCamelCase   ) as excinfo:
																				processor.get_prompt_ids(UpperCamelCase   )
														_SCREAMING_SNAKE_CASE					=			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'''







import logging

# standard libraries
import sys

import numpy as np
import pytest  # type: ignore

# Custom/local libraries
from matrix import matrix_operation as matop

_snake_case					: List[str]     			=					[[12, 10], [3, 9]]
_snake_case					: Any     			=					[[3, 4], [7, 4]]
_snake_case					: List[str]     			=					[[3, 0, 2], [2, 0, -2], [0, 1, 1]]
_snake_case					: Any     			=					[[3, 0, -2], [2, 0, 2], [0, 1, 1]]
_snake_case					: Any     			=					[[3, 0, 2], [2, 0, -2], [0, 1, 1], [2, 0, -2]]
_snake_case					: Optional[Any]     			=					[1]
_snake_case					: str     			=					[2]

_snake_case					: Optional[int]     			=					logging.getLogger()
_snake_case					: Optional[Any]     			=					logging.DEBUG
_snake_case					: Tuple     			=					logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)



@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ("mat1", "mat2")						,      [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]	)
def       _a		(						_SCREAMING_SNAKE_CASE		: List[Any]						,      _SCREAMING_SNAKE_CASE		: Tuple	):
						if (np.array(_SCREAMING_SNAKE_CASE	)).shape < (2, 2) or (np.array(_SCREAMING_SNAKE_CASE	)).shape < (2, 2):
												with pytest.raises(_SCREAMING_SNAKE_CASE	):
																		logger.info(F'\n\t{test_addition.__name__} returned integer'	)
																		matop.add(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)
						elif (np.array(_SCREAMING_SNAKE_CASE	)).shape == (np.array(_SCREAMING_SNAKE_CASE	)).shape:
												logger.info(F'\n\t{test_addition.__name__} with same matrix dims'	)
												_SCREAMING_SNAKE_CASE					=			(np.array(_SCREAMING_SNAKE_CASE	) + np.array(_SCREAMING_SNAKE_CASE	)).tolist()
												_SCREAMING_SNAKE_CASE					=			matop.add(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)
												assert theo == act
						else:
												with pytest.raises(_SCREAMING_SNAKE_CASE	):
																		logger.info(F'\n\t{test_addition.__name__} with different matrix dims'	)
																		matop.add(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)



@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ("mat1", "mat2")						,      [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]	)
def       _a		(						_SCREAMING_SNAKE_CASE		: Optional[Any]						,      _SCREAMING_SNAKE_CASE		: List[str]	):
						if (np.array(_SCREAMING_SNAKE_CASE	)).shape < (2, 2) or (np.array(_SCREAMING_SNAKE_CASE	)).shape < (2, 2):
												with pytest.raises(_SCREAMING_SNAKE_CASE	):
																		logger.info(F'\n\t{test_subtraction.__name__} returned integer'	)
																		matop.subtract(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)
						elif (np.array(_SCREAMING_SNAKE_CASE	)).shape == (np.array(_SCREAMING_SNAKE_CASE	)).shape:
												logger.info(F'\n\t{test_subtraction.__name__} with same matrix dims'	)
												_SCREAMING_SNAKE_CASE					=			(np.array(_SCREAMING_SNAKE_CASE	) - np.array(_SCREAMING_SNAKE_CASE	)).tolist()
												_SCREAMING_SNAKE_CASE					=			matop.subtract(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)
												assert theo == act
						else:
												with pytest.raises(_SCREAMING_SNAKE_CASE	):
																		logger.info(F'\n\t{test_subtraction.__name__} with different matrix dims'	)
																		assert matop.subtract(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)



@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ("mat1", "mat2")						,      [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]	)
def       _a		(						_SCREAMING_SNAKE_CASE		: Any						,      _SCREAMING_SNAKE_CASE		: List[str]	):
						if (np.array(_SCREAMING_SNAKE_CASE	)).shape < (2, 2) or (np.array(_SCREAMING_SNAKE_CASE	)).shape < (2, 2):
												logger.info(F'\n\t{test_multiplication.__name__} returned integer'	)
												with pytest.raises(_SCREAMING_SNAKE_CASE	):
																		matop.add(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)
						elif (np.array(_SCREAMING_SNAKE_CASE	)).shape == (np.array(_SCREAMING_SNAKE_CASE	)).shape:
												logger.info(F'\n\t{test_multiplication.__name__} meets dim requirements'	)
												_SCREAMING_SNAKE_CASE					=			(np.matmul(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)).tolist()
												_SCREAMING_SNAKE_CASE					=			matop.multiply(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)
												assert theo == act
						else:
												with pytest.raises(_SCREAMING_SNAKE_CASE	):
																		logger.info(
																		    F'\n\t{test_multiplication.__name__} does not meet dim requirements'	)
																		assert matop.subtract(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE	)



@pytest.mark.mat_ops
def       _a		(						):
						_SCREAMING_SNAKE_CASE					=			(3.5 * np.array(_SCREAMING_SNAKE_CASE	)).tolist()
						_SCREAMING_SNAKE_CASE					=			matop.scalar_multiply(_SCREAMING_SNAKE_CASE						,      3.5	)
						assert theo == act



@pytest.mark.mat_ops
def       _a		(						):
						_SCREAMING_SNAKE_CASE					=			(np.identity(5	)).tolist()
						_SCREAMING_SNAKE_CASE					=			matop.identity(5	)
						assert theo == act




@pytest.mark.mat_ops
@pytest.mark.parametrize("mat"						,      [mat_a, mat_b, mat_c, mat_d, mat_e, mat_f]	)
def       _a		(						_SCREAMING_SNAKE_CASE		: Tuple	):
						if (np.array(_SCREAMING_SNAKE_CASE	)).shape < (2, 2):
												with pytest.raises(_SCREAMING_SNAKE_CASE	):
																		logger.info(F'\n\t{test_transpose.__name__} returned integer'	)
																		matop.transpose(_SCREAMING_SNAKE_CASE	)
						else:
												_SCREAMING_SNAKE_CASE					=			(np.transpose(_SCREAMING_SNAKE_CASE	)).tolist()
												_SCREAMING_SNAKE_CASE					=			matop.transpose(_SCREAMING_SNAKE_CASE						,      return_map=_SCREAMING_SNAKE_CASE	)
												assert theo == act