
"""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 					UpperCamelCase     :

				"""simple docstring"""



				def __init__(    self		,     lowercase__		,     lowercase__=99		,     lowercase__=13		,     lowercase__=16		,     lowercase__=7		,     lowercase__=True		,     lowercase__=True		,     lowercase__=True		,     lowercase__=False		,     lowercase__=True		,     lowercase__=2		,     lowercase__=32		,     lowercase__=4		,     lowercase__=4		,     lowercase__=30		,     lowercase__=0		,     lowercase__=1		,     lowercase__=2		,     lowercase__=None		,     )   ->   Any:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     parent
									SCREAMING_SNAKE_CASE           =     batch_size
									SCREAMING_SNAKE_CASE           =     decoder_seq_length
									# For common tests
									SCREAMING_SNAKE_CASE           =     self.decoder_seq_length
									SCREAMING_SNAKE_CASE           =     is_training
									SCREAMING_SNAKE_CASE           =     use_attention_mask
									SCREAMING_SNAKE_CASE           =     use_labels

									SCREAMING_SNAKE_CASE           =     vocab_size
									SCREAMING_SNAKE_CASE           =     d_model
									SCREAMING_SNAKE_CASE           =     d_model
									SCREAMING_SNAKE_CASE           =     decoder_layers
									SCREAMING_SNAKE_CASE           =     decoder_layers
									SCREAMING_SNAKE_CASE           =     decoder_ffn_dim
									SCREAMING_SNAKE_CASE           =     decoder_attention_heads
									SCREAMING_SNAKE_CASE           =     decoder_attention_heads
									SCREAMING_SNAKE_CASE           =     eos_token_id
									SCREAMING_SNAKE_CASE           =     bos_token_id
									SCREAMING_SNAKE_CASE           =     pad_token_id
									SCREAMING_SNAKE_CASE           =     decoder_start_token_id
									SCREAMING_SNAKE_CASE           =     use_cache
									SCREAMING_SNAKE_CASE           =     max_position_embeddings

									SCREAMING_SNAKE_CASE           =     None
									SCREAMING_SNAKE_CASE           =     decoder_seq_length
									SCREAMING_SNAKE_CASE           =     2
									SCREAMING_SNAKE_CASE           =     1


				def 							A (    self      )   ->   List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     ids_tensor([self.batch_size, self.decoder_seq_length]		,     self.vocab_size      )

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

									SCREAMING_SNAKE_CASE           =     None
									if self.use_labels:
														SCREAMING_SNAKE_CASE           =     ids_tensor([self.batch_size, self.decoder_seq_length]		,     self.vocab_size      )

									SCREAMING_SNAKE_CASE           =     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		,     lowercase__		,     lowercase__		,     lowercase__		,     lowercase__		,     )   ->   List[str]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     True
									SCREAMING_SNAKE_CASE           =     SpeechaTextaDecoder(config=lowercase__      ).to(lowercase__      ).eval()
									SCREAMING_SNAKE_CASE           =     input_ids[:2]

									input_ids[input_ids == 0] += 1
									# first forward pass
									SCREAMING_SNAKE_CASE           =     model(lowercase__		,     use_cache=lowercase__      )
									SCREAMING_SNAKE_CASE           =     model(lowercase__      )
									SCREAMING_SNAKE_CASE           =     model(lowercase__		,     use_cache=lowercase__      )

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

									SCREAMING_SNAKE_CASE           =     outputs['past_key_values']

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

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

									print(lowercase__      )

									SCREAMING_SNAKE_CASE           =     model(lowercase__      )['last_hidden_state']
									SCREAMING_SNAKE_CASE           =     model(lowercase__		,     past_key_values=lowercase__      )['last_hidden_state']

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

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




				def 							A (    self      )   ->   List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.prepare_config_and_inputs()
									(
									    (
									    SCREAMING_SNAKE_CASE
									)     ,   (
									    SCREAMING_SNAKE_CASE
									)     ,   (
									    SCREAMING_SNAKE_CASE
									)     ,   (
									    SCREAMING_SNAKE_CASE
									)     ,   
									)           =     config_and_inputs

									SCREAMING_SNAKE_CASE           =     {
									    'input_ids': input_ids,
									    'attention_mask': attention_mask,
									}
									return config, inputs_dict






@require_torch
class 					UpperCamelCase     (	__magic_name__  ,				__magic_name__  ,				__magic_name__  ,				unittest.TestCase				):

				"""simple docstring"""



				UpperCAmelCase_    :				Optional[Any]						  =    (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
				UpperCAmelCase_    :				List[str]						  =    (SpeechaTextaForCausalLM,) if is_torch_available() else ()
				UpperCAmelCase_    :				List[Any]						  =    {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
				UpperCAmelCase_    :				int						  =    True
				UpperCAmelCase_    :				Dict						  =    False


				def 							A (    self		,     )   ->   List[str]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     SpeechaTextaStandaloneDecoderModelTester(self		,     is_training=lowercase__      )
									SCREAMING_SNAKE_CASE           =     ConfigTester(self		,     config_class=lowercase__      )


				def 							A (    self      )   ->   List[Any]:
									"""simple docstring"""







									pass


				def 							A (    self      )   ->   Any:
									"""simple docstring"""







									pass


				def 							A (    self      )   ->   Any:
									"""simple docstring"""







									pass


				def 							A (    self      )   ->   str:
									"""simple docstring"""







									self.config_tester.run_common_tests()


				def 							A (    self      )   ->   List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_decoder_model_past(*lowercase__      )




				def 							A (    self      )   ->   Tuple:
									"""simple docstring"""







									return


"""simple docstring"""




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available


if is_torch_available():
	from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
	snake_case			       =		False

if is_vision_available():
	from PIL import Image

	from transformers import (
	    AutoProcessor,
	    PixaStructImageProcessor,
	    PixaStructProcessor,
	    PreTrainedTokenizerFast,
	    TaTokenizer,
	)




@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11  ,				reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`."  ,				)
@require_vision
@require_torch
class 					UpperCamelCase     (	unittest.TestCase				):

				"""simple docstring"""



				def 							A (    self      )   ->   Any:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     tempfile.mkdtemp()

									SCREAMING_SNAKE_CASE           =     PixaStructImageProcessor()
									SCREAMING_SNAKE_CASE           =     TaTokenizer.from_pretrained('t5-small'      )

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(lowercase__		,     lowercase__      )

									processor.save_pretrained(self.tmpdirname      )


				def 							A (    self		,     **lowercase__      )   ->   Dict:
									"""simple docstring"""







									return AutoProcessor.from_pretrained(self.tmpdirname		,     **lowercase__      ).tokenizer


				def 							A (    self		,     **lowercase__      )   ->   Union[str, Any]:
									"""simple docstring"""







									return AutoProcessor.from_pretrained(self.tmpdirname		,     **lowercase__      ).image_processor


				def 							A (    self      )   ->   Optional[int]:
									"""simple docstring"""







									shutil.rmtree(self.tmpdirname      )


				def 							A (    self      )   ->   int:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     [np.random.randint(255		,     size=(3, 30, 400)		,     dtype=np.uinta      )]

									SCREAMING_SNAKE_CASE           =     [Image.fromarray(np.moveaxis(lowercase__		,     0		,     -1      )      ) for x in image_inputs]

									return image_inputs


				def 							A (    self      )   ->   int:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(tokenizer=self.get_tokenizer()		,     image_processor=self.get_image_processor()      )
									processor.save_pretrained(self.tmpdirname      )

									SCREAMING_SNAKE_CASE           =     self.get_tokenizer(bos_token='(BOS)'		,     eos_token='(EOS)'      )
									SCREAMING_SNAKE_CASE           =     self.get_image_processor(do_normalize=lowercase__		,     padding_value=1.0      )

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor.from_pretrained(
									    self.tmpdirname		,     bos_token='(BOS)'		,     eos_token='(EOS)'		,     do_normalize=lowercase__		,     padding_value=1.0      )

									self.assertEqual(processor.tokenizer.get_vocab()		,     tokenizer_add_kwargs.get_vocab()      )
									self.assertIsInstance(processor.tokenizer		,     lowercase__      )

									self.assertEqual(processor.image_processor.to_json_string()		,     image_processor_add_kwargs.to_json_string()      )
									self.assertIsInstance(processor.image_processor		,     lowercase__      )


				def 							A (    self      )   ->   int:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.get_image_processor()
									SCREAMING_SNAKE_CASE           =     self.get_tokenizer()

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(tokenizer=lowercase__		,     image_processor=lowercase__      )

									SCREAMING_SNAKE_CASE           =     self.prepare_image_inputs()

									SCREAMING_SNAKE_CASE           =     image_processor(lowercase__		,     return_tensors='np'      )
									SCREAMING_SNAKE_CASE           =     processor(images=lowercase__		,     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      )   ->   List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.get_image_processor()
									SCREAMING_SNAKE_CASE           =     self.get_tokenizer()

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(tokenizer=lowercase__		,     image_processor=lowercase__      )

									SCREAMING_SNAKE_CASE           =     'lower newer'

									SCREAMING_SNAKE_CASE           =     processor(text=lowercase__      )

									SCREAMING_SNAKE_CASE           =     tokenizer(lowercase__		,     return_token_type_ids=lowercase__		,     add_special_tokens=lowercase__      )

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


				def 							A (    self      )   ->   List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.get_image_processor()
									SCREAMING_SNAKE_CASE           =     self.get_tokenizer()

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(tokenizer=lowercase__		,     image_processor=lowercase__      )

									SCREAMING_SNAKE_CASE           =     'lower newer'
									SCREAMING_SNAKE_CASE           =     self.prepare_image_inputs()

									SCREAMING_SNAKE_CASE           =     processor(text=lowercase__		,     images=lowercase__      )

									self.assertListEqual(
									    list(inputs.keys()      )		,     ['flattened_patches', 'attention_mask', 'decoder_attention_mask', 'decoder_input_ids']      )

									# test if it raises when no input is passed
									with pytest.raises(lowercase__      ):
														processor()


				def 							A (    self      )   ->   Union[str, Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.get_image_processor()
									SCREAMING_SNAKE_CASE           =     self.get_tokenizer()

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(tokenizer=lowercase__		,     image_processor=lowercase__      )

									SCREAMING_SNAKE_CASE           =     'lower newer'
									SCREAMING_SNAKE_CASE           =     self.prepare_image_inputs()

									SCREAMING_SNAKE_CASE           =     processor(text=lowercase__		,     images=lowercase__      )

									SCREAMING_SNAKE_CASE           =     [512, 1024, 2048, 4096]
									SCREAMING_SNAKE_CASE           =     [770, 770, 770, 770]
									# with text
									for i, max_patch in enumerate(lowercase__      ):
														SCREAMING_SNAKE_CASE           =     processor(text=lowercase__		,     images=lowercase__		,     max_patches=lowercase__      )
														self.assertEqual(inputs['flattened_patches'][0].shape[0]		,     lowercase__      )
														self.assertEqual(inputs['flattened_patches'][0].shape[1]		,     expected_hidden_size[i]      )

									# without text input
									for i, max_patch in enumerate(lowercase__      ):
														SCREAMING_SNAKE_CASE           =     processor(images=lowercase__		,     max_patches=lowercase__      )
														self.assertEqual(inputs['flattened_patches'][0].shape[0]		,     lowercase__      )
														self.assertEqual(inputs['flattened_patches'][0].shape[1]		,     expected_hidden_size[i]      )


				def 							A (    self      )   ->   Tuple:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.get_image_processor()
									SCREAMING_SNAKE_CASE           =     self.get_tokenizer()

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(tokenizer=lowercase__		,     image_processor=lowercase__      )

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

									SCREAMING_SNAKE_CASE           =     processor.batch_decode(lowercase__      )
									SCREAMING_SNAKE_CASE           =     tokenizer.batch_decode(lowercase__      )

									self.assertListEqual(lowercase__		,     lowercase__      )




				def 							A (    self      )   ->   Optional[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =     self.get_image_processor()
									SCREAMING_SNAKE_CASE           =     self.get_tokenizer()

									SCREAMING_SNAKE_CASE           =     PixaStructProcessor(tokenizer=lowercase__		,     image_processor=lowercase__      )

									SCREAMING_SNAKE_CASE           =     'lower newer'
									SCREAMING_SNAKE_CASE           =     self.prepare_image_inputs()

									SCREAMING_SNAKE_CASE           =     processor(text=lowercase__		,     images=lowercase__      )

									# For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
									self.assertListEqual(
									    list(inputs.keys()      )		,     ['flattened_patches', 'attention_mask', 'decoder_attention_mask', 'decoder_input_ids']      )

									SCREAMING_SNAKE_CASE           =     processor(text=lowercase__      )

									# For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
									self.assertListEqual(list(inputs.keys()      )		,     ['input_ids', 'attention_mask']      )
