




'''simple docstring'''






import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
				    SpeechaTextaDecoder,
				    SpeechaTextaForCausalLM,
				)



@require_torch
class  A		:




				'''simple docstring'''





				def __init__(self			,  _UpperCAmelCase			,  _UpperCAmelCase=9_9			,  _UpperCAmelCase=1_3			,  _UpperCAmelCase=1_6			,  _UpperCAmelCase=7			,  _UpperCAmelCase=True			,  _UpperCAmelCase=True			,  _UpperCAmelCase=True			,  _UpperCAmelCase=False			,  _UpperCAmelCase=True			,  _UpperCAmelCase=2			,  _UpperCAmelCase=3_2			,  _UpperCAmelCase=4			,  _UpperCAmelCase=4			,  _UpperCAmelCase=3_0			,  _UpperCAmelCase=0			,  _UpperCAmelCase=1			,  _UpperCAmelCase=2			,  _UpperCAmelCase=None			,  )     ->	Tuple:
							__UpperCamelCase				:      int									=       parent
							__UpperCamelCase				:      str									=       batch_size
							__UpperCamelCase				:      Union[str, Any]									=       decoder_seq_length
							# For common tests
							__UpperCamelCase				:      List[str]									=       self.decoder_seq_length
							__UpperCamelCase				:      int									=       is_training
							__UpperCamelCase				:      Optional[int]									=       use_attention_mask
							__UpperCamelCase				:      Optional[Any]									=       use_labels

							__UpperCamelCase				:      List[Any]									=       vocab_size
							__UpperCamelCase				:      Dict									=       d_model
							__UpperCamelCase				:      List[Any]									=       d_model
							__UpperCamelCase				:      List[str]									=       decoder_layers
							__UpperCamelCase				:      Optional[Any]									=       decoder_layers
							__UpperCamelCase				:      Optional[int]									=       decoder_ffn_dim
							__UpperCamelCase				:      Optional[Any]									=       decoder_attention_heads
							__UpperCamelCase				:      Optional[int]									=       decoder_attention_heads
							__UpperCamelCase				:      str									=       eos_token_id
							__UpperCamelCase				:      str									=       bos_token_id
							__UpperCamelCase				:      str									=       pad_token_id
							__UpperCamelCase				:      List[str]									=       decoder_start_token_id
							__UpperCamelCase				:      Optional[Any]									=       use_cache
							__UpperCamelCase				:      Dict									=       max_position_embeddings

							__UpperCamelCase				:      List[Any]									=       None
							__UpperCamelCase				:      Tuple									=       decoder_seq_length
							__UpperCamelCase				:      List[Any]									=       2
							__UpperCamelCase				:      Tuple									=       1
				def 	a_	(self      )     ->	int:
							__UpperCamelCase				:      Union[str, Any]									=       ids_tensor([self.batch_size, self.decoder_seq_length]			,  self.vocab_size      )

							__UpperCamelCase				:      Union[str, Any]									=       None
							if self.use_attention_mask:
										__UpperCamelCase				:      Tuple									=       ids_tensor([self.batch_size, self.decoder_seq_length]			,  vocab_size=2      )

							__UpperCamelCase				:      Optional[Any]									=       None
							if self.use_labels:
										__UpperCamelCase				:      List[Any]									=       ids_tensor([self.batch_size, self.decoder_seq_length]			,  self.vocab_size      )

							__UpperCamelCase				:      Optional[int]									=       SpeechaTextaConfig(
							    vocab_size=self.vocab_size			,  d_model=self.d_model			,  decoder_layers=self.decoder_layers			,  decoder_ffn_dim=self.decoder_ffn_dim			,  decoder_attention_heads=self.decoder_attention_heads			,  eos_token_id=self.eos_token_id			,  bos_token_id=self.bos_token_id			,  use_cache=self.use_cache			,  pad_token_id=self.pad_token_id			,  decoder_start_token_id=self.decoder_start_token_id			,  max_position_embeddings=self.max_position_embeddings			,  )

							return (
							    config,
							    input_ids,
							    attention_mask,
							    lm_labels,
							)
				def 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	List[Any]:
							__UpperCamelCase				:      Dict									=       True
							__UpperCamelCase				:      List[Any]									=       SpeechaTextaDecoder(config=_UpperCAmelCase      ).to(_UpperCAmelCase      ).eval()
							__UpperCamelCase				:      Union[str, Any]									=       input_ids[:2]

							input_ids[input_ids == 0] += 1
							# first forward pass
							__UpperCamelCase				:      Tuple									=       model(_UpperCAmelCase			,  use_cache=_UpperCAmelCase      )
							__UpperCamelCase				:      Dict									=       model(_UpperCAmelCase      )
							__UpperCamelCase				:      Union[str, Any]									=       model(_UpperCAmelCase			,  use_cache=_UpperCAmelCase      )

							self.parent.assertTrue(len(_UpperCAmelCase      ) == len(_UpperCAmelCase      )      )
							self.parent.assertTrue(len(_UpperCAmelCase      ) == len(_UpperCAmelCase      ) + 1      )

							__UpperCamelCase				:      str									=       outputs["past_key_values"]

							# create hypothetical next token and extent to next_input_ids
							__UpperCamelCase				:      Any									=       ids_tensor((2, 1)			,  config.vocab_size - 1      ) + 1

							# append to next input_ids and
							__UpperCamelCase				:      str									=       torch.cat([input_ids, next_tokens]			,  dim=-1      )

							print(_UpperCAmelCase      )

							__UpperCamelCase				:      int									=       model(_UpperCAmelCase      )["last_hidden_state"]
							__UpperCamelCase				:      List[Any]									=       model(_UpperCAmelCase			,  past_key_values=_UpperCAmelCase      )["last_hidden_state"]

							# select random slice
							__UpperCamelCase				:      str									=       ids_tensor((1,)			,  output_from_past.shape[-1]      ).item()
							__UpperCamelCase				:      List[str]									=       output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
							__UpperCamelCase				:      List[str]									=       output_from_past[:, 0, random_slice_idx].detach()

							# test that outputs are equal for slice
							assert torch.allclose(_UpperCAmelCase			,  _UpperCAmelCase			,  atol=1E-3      )





				def 	a_	(self      )     ->	Any:
							__UpperCamelCase				:      int									=       self.prepare_config_and_inputs()
							(
							    __UpperCamelCase 
							)				:      List[str]									=       config_and_inputs

							__UpperCamelCase				:      List[Any]									=       {
							    "input_ids": input_ids,
							    "attention_mask": attention_mask,
							}
							return config, inputs_dict







@require_torch
class  A		(			SCREAMING_SNAKE_CASE__      ,							SCREAMING_SNAKE_CASE__      ,							SCREAMING_SNAKE_CASE__      ,							unittest.TestCase		):




				'''simple docstring'''





				A				     =    (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
				A				     =    (SpeechaTextaForCausalLM,) if is_torch_available() else ()
				A				     =    {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
				A				     =    True
				A				     =    False
				def 	a_	(self			,  )     ->	Dict:
							__UpperCamelCase				:      Tuple									=       SpeechaTextaStandaloneDecoderModelTester(self			,  is_training=_UpperCAmelCase      )
							__UpperCamelCase				:      Dict									=       ConfigTester(self			,  config_class=_UpperCAmelCase      )
				def 	a_	(self      )     ->	Tuple:
							pass
				def 	a_	(self      )     ->	Dict:
							pass
				def 	a_	(self      )     ->	Optional[Any]:
							pass
				def 	a_	(self      )     ->	str:
							self.config_tester.run_common_tests()
				def 	a_	(self      )     ->	Dict:
							__UpperCamelCase				:      Optional[Any]									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*_UpperCAmelCase      )





				def 	a_	(self      )     ->	Optional[Any]:
							return









'''simple docstring'''






import math
import os

import cva
import numpy as np

_lowerCAmelCase				     = 255.0

def       __lowerCAmelCase (   snake_case__ ,				snake_case__						):
			__UpperCamelCase				:      Dict									=       np.mean((original - contrast) ** 2						)
			if mse == 0:
						return 100

			return 20 * math.logaa(PIXEL_MAX / math.sqrt(snake_case__						)						)

def       __lowerCAmelCase (   ):
			__UpperCamelCase				:      Tuple									=       os.path.dirname(os.path.realpath(snake_case__						)						)
			# Loading images (original image and compressed image)
			__UpperCamelCase				:      Tuple									=       cva.imread(os.path.join(snake_case__ ,				"image_data/original_image.png"						)						)
			__UpperCamelCase				:      List[Any]									=       cva.imread(os.path.join(snake_case__ ,				"image_data/compressed_image.png"						) ,				1						)

			__UpperCamelCase				:      Dict									=       cva.imread(os.path.join(snake_case__ ,				"image_data/PSNR-example-base.png"						)						)
			__UpperCamelCase				:      Optional[Any]									=       cva.imread(
			    os.path.join(snake_case__ ,				"image_data/PSNR-example-comp-10.jpg"						) ,				1						)

			# Value expected: 29.73dB
			print("-- First Test --"						)
			print(F"PSNR value is {peak_signal_to_noise_ratio(snake_case__ ,				snake_case__						)} dB"						)

			# # Value expected: 31.53dB (Wikipedia Example)
			print("\n-- Second Test --"						)
			print(F"PSNR value is {peak_signal_to_noise_ratio(snake_case__ ,				snake_case__						)} dB"						)


if __name__ == "__main__":
				main()



