
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    _UpperCamelCase:

						def __init__(					self		:			Optional[int]	,						_lowerCamelCase		:			Tuple	,						_lowerCamelCase		:			Union[str, Any]=99	,						_lowerCamelCase		:			List[str]=13	,						_lowerCamelCase		:			Dict=16	,						_lowerCamelCase		:			Optional[Any]=7	,						_lowerCamelCase		:			str=True	,						_lowerCamelCase		:			Dict=True	,						_lowerCamelCase		:			Optional[Any]=True	,						_lowerCamelCase		:			Union[str, Any]=False	,						_lowerCamelCase		:			List[Any]=True	,						_lowerCamelCase		:			Optional[Any]=2	,						_lowerCamelCase		:			Dict=32	,						_lowerCamelCase		:			Optional[int]=4	,						_lowerCamelCase		:			str=4	,						_lowerCamelCase		:			Union[str, Any]=30	,						_lowerCamelCase		:			str=0	,						_lowerCamelCase		:			List[str]=1	,						_lowerCamelCase		:			Optional[Any]=2	,						_lowerCamelCase		:			Tuple=None	,						):
													_UpperCAmelCase  :			Union[str, Any]	       =		parent
													_UpperCAmelCase  :			Dict	       =		batch_size
													_UpperCAmelCase  :			int	       =		decoder_seq_length
													# For common tests
													_UpperCAmelCase  :			Any	       =		self.decoder_seq_length
													_UpperCAmelCase  :			List[Any]	       =		is_training
													_UpperCAmelCase  :			Optional[Any]	       =		use_attention_mask
													_UpperCAmelCase  :			str	       =		use_labels

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

													_UpperCAmelCase  :			Tuple	       =		None
													_UpperCAmelCase  :			Optional[Any]	       =		decoder_seq_length
													_UpperCAmelCase  :			int	       =		2
													_UpperCAmelCase  :			str	       =		1

						def 						a__ (					self		:			int			):
													_UpperCAmelCase  :			Optional[int]	       =		ids_tensor([self.batch_size, self.decoder_seq_length]	,						self.vocab_size			)

													_UpperCAmelCase  :			Any	       =		None
													if self.use_attention_mask:
																				_UpperCAmelCase  :			Any	       =		ids_tensor([self.batch_size, self.decoder_seq_length]	,						vocab_size=2			)

													_UpperCAmelCase  :			List[Any]	       =		None
													if self.use_labels:
																				_UpperCAmelCase  :			int	       =		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		:			List[str]	,						_lowerCamelCase		:			int	,						_lowerCamelCase		:			Tuple	,						_lowerCamelCase		:			Optional[int]	,						_lowerCamelCase		:			List[Any]	,						):
													_UpperCAmelCase  :			Tuple	       =		True
													_UpperCAmelCase  :			Optional[Any]	       =		SpeechaTextaDecoder(config=_lowerCamelCase			).to(_lowerCamelCase			).eval()
													_UpperCAmelCase  :			Dict	       =		input_ids[:2]

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

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

													_UpperCAmelCase  :			Dict	       =		outputs["past_key_values"]

													# create hypothetical next token and extent to next_input_ids
													_UpperCAmelCase  :			Optional[int]	       =		ids_tensor((2, 1)	,						config.vocab_size - 1			) + 1

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

													print(_lowerCamelCase			)

													_UpperCAmelCase  :			Union[str, Any]	       =		model(_lowerCamelCase			)["last_hidden_state"]
													_UpperCAmelCase  :			str	       =		model(_lowerCamelCase	,						past_key_values=_lowerCamelCase			)["last_hidden_state"]

													# select random slice
													_UpperCAmelCase  :			Optional[int]	       =		ids_tensor((1,)	,						output_from_past.shape[-1]			).item()
													_UpperCAmelCase  :			List[Any]	       =		output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
													_UpperCAmelCase  :			Dict	       =		output_from_past[:, 0, random_slice_idx].detach()

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






						def 						a__ (					self		:			Optional[Any]			):
													_UpperCAmelCase  :			Optional[Any]	       =		self.prepare_config_and_inputs()
													(
													    (
													    _UpperCAmelCase
													)       ,(
													    _UpperCAmelCase
													)       ,(
													    _UpperCAmelCase
													)       ,(
													    _UpperCAmelCase
													)       ,
													)  :			int	       =		config_and_inputs

													_UpperCAmelCase  :			Dict	       =		{
													    "input_ids": input_ids,
													    "attention_mask": attention_mask,
													}
													return config, inputs_dict







@require_torch
class    _UpperCamelCase(   SCREAMING_SNAKE_CASE		,						SCREAMING_SNAKE_CASE		,						SCREAMING_SNAKE_CASE		,						unittest.TestCase				):
						__A:						Dict				   =      (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
						__A:						Any				   =      (SpeechaTextaForCausalLM,) if is_torch_available() else ()
						__A:						Union[str, Any]				   =      {"""text-generation""": SpeechaTextaForCausalLM} if is_torch_available() else {}
						__A:						Tuple				   =      True
						__A:						Dict				   =      False

						def 						a__ (					self		:			Tuple	,						):
													_UpperCAmelCase  :			Dict	       =		SpeechaTextaStandaloneDecoderModelTester(self	,						is_training=_lowerCamelCase			)
													_UpperCAmelCase  :			Tuple	       =		ConfigTester(self	,						config_class=_lowerCamelCase			)

						def 						a__ (					self		:			Union[str, Any]			):
													pass

						def 						a__ (					self		:			Optional[Any]			):
													pass

						def 						a__ (					self		:			int			):
													pass

						def 						a__ (					self		:			Optional[Any]			):
													self.config_tester.run_common_tests()

						def 						a__ (					self		:			int			):
													_UpperCAmelCase  :			Any	       =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_decoder_model_past(*_lowerCamelCase			)






						def 						a__ (					self		:			Optional[Any]			):
													return






import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
				from PIL import Image
class    _UpperCamelCase(   unittest.TestCase		,						SCREAMING_SNAKE_CASE				):

						def 						a__ (					self		:			Optional[int]			):
													_UpperCAmelCase  :			Any	       =		load_tool("image-captioning"			)
													self.tool.setup()
													_UpperCAmelCase  :			Optional[Any]	       =		load_tool("image-captioning"	,						remote=_lowerCamelCase			)

						def 						a__ (					self		:			int			):
													_UpperCAmelCase  :			Optional[int]	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			Optional[int]	       =		self.tool(_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)

						def 						a__ (					self		:			str			):
													_UpperCAmelCase  :			List[str]	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			Optional[Any]	       =		self.remote_tool(_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)

						def 						a__ (					self		:			Union[str, Any]			):
													_UpperCAmelCase  :			Any	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			Union[str, Any]	       =		self.tool(image=_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)






						def 						a__ (					self		:			Optional[int]			):
													_UpperCAmelCase  :			str	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			str	       =		self.remote_tool(image=_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)




