





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






       '''simple docstring'''


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

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

            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							None
            _SCREAMING_SNAKE_CASE				:		int  			=							decoder_seq_length
            _SCREAMING_SNAKE_CASE				:		Dict  			=							2
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							1




       def 			UpperCamelCase_	(					self							)	->   Any:
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							ids_tensor([self.batch_size, self.decoder_seq_length]			,  self.vocab_size							)

            _SCREAMING_SNAKE_CASE				:		List[str]  			=							None
            if self.use_attention_mask:
                 _SCREAMING_SNAKE_CASE				:		int  			=							ids_tensor([self.batch_size, self.decoder_seq_length]			,  vocab_size=2							)

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

            _SCREAMING_SNAKE_CASE				:		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 			UpperCamelCase_	(					self			,  __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase			,  )	->   Union[str, Any]:
            _SCREAMING_SNAKE_CASE				:		Tuple  			=							True
            _SCREAMING_SNAKE_CASE				:		Dict  			=							SpeechaTextaDecoder(config=__lowerCamelCase							).to(__lowerCamelCase							).eval()
            _SCREAMING_SNAKE_CASE				:		int  			=							input_ids[:2]

            input_ids[input_ids == 0] += 1
            # first forward pass
            _SCREAMING_SNAKE_CASE				:		int  			=							model(__lowerCamelCase			,  use_cache=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							model(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							model(__lowerCamelCase			,  use_cache=__lowerCamelCase							)

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

            _SCREAMING_SNAKE_CASE				:		int  			=							outputs["past_key_values"]

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

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

            print(__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE				:		Dict  			=							model(__lowerCamelCase							)["last_hidden_state"]
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							model(__lowerCamelCase			,  past_key_values=__lowerCamelCase							)["last_hidden_state"]

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

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







       def 			UpperCamelCase_	(					self							)	->   Optional[Any]:
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							self.prepare_config_and_inputs()
            (
                (
                _SCREAMING_SNAKE_CASE
            )			,	(
                _SCREAMING_SNAKE_CASE
            )			,	(
                _SCREAMING_SNAKE_CASE
            )			,	(
                _SCREAMING_SNAKE_CASE
            )			,	
            )				:		List[str]  			=							config_and_inputs

            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							{
                "input_ids": input_ids,
                "attention_mask": attention_mask,
            }
            return config, inputs_dict





@require_torch
class 	lowerCAmelCase__(       __lowercase     ,   __lowercase     ,   __lowercase     ,   unittest.TestCase			):






       '''simple docstring'''


       __snake_case       	=			(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
       __snake_case       	=			(SpeechaTextaForCausalLM,) if is_torch_available() else ()
       __snake_case       	=			{'text-generation': SpeechaTextaForCausalLM} if is_torch_available() else {}
       __snake_case       	=			True
       __snake_case       	=			False




       def 			UpperCamelCase_	(					self			,  )	->   int:
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							SpeechaTextaStandaloneDecoderModelTester(self			,  is_training=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE				:		int  			=							ConfigTester(self			,  config_class=__lowerCamelCase							)




       def 			UpperCamelCase_	(					self							)	->   Any:
            pass




       def 			UpperCamelCase_	(					self							)	->   List[str]:
            pass




       def 			UpperCamelCase_	(					self							)	->   Optional[int]:
            pass




       def 			UpperCamelCase_	(					self							)	->   Any:
            self.config_tester.run_common_tests()




       def 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_decoder_model_past(*__lowerCamelCase							)







       def 			UpperCamelCase_	(					self							)	->   List[str]:
            return






import unittest

import numpy as np

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import CLIPImageProcessor





class 	lowerCAmelCase__(       unittest.TestCase			):






       '''simple docstring'''


       def __init__(					self			,  __lowerCamelCase			,  __lowerCamelCase=7			,  __lowerCamelCase=3			,  __lowerCamelCase=1_8			,  __lowerCamelCase=3_0			,  __lowerCamelCase=4_0_0			,  __lowerCamelCase=True			,  __lowerCamelCase=None			,  __lowerCamelCase=True			,  __lowerCamelCase=None			,  __lowerCamelCase=True			,  __lowerCamelCase=[0.4814_5466, 0.457_8275, 0.4082_1073]			,  __lowerCamelCase=[0.2686_2954, 0.2613_0258, 0.2757_7711]			,  __lowerCamelCase=True			,  )	->   List[Any]:
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							size if size is not None else {"shortest_edge": 2_0}
            _SCREAMING_SNAKE_CASE				:		int  			=							crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
            _SCREAMING_SNAKE_CASE				:		str  			=							parent
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							batch_size
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							num_channels
            _SCREAMING_SNAKE_CASE				:		Any  			=							image_size
            _SCREAMING_SNAKE_CASE				:		Any  			=							min_resolution
            _SCREAMING_SNAKE_CASE				:		int  			=							max_resolution
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							do_resize
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							size
            _SCREAMING_SNAKE_CASE				:		int  			=							do_center_crop
            _SCREAMING_SNAKE_CASE				:		Dict  			=							crop_size
            _SCREAMING_SNAKE_CASE				:		int  			=							do_normalize
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							image_mean
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_std
            _SCREAMING_SNAKE_CASE				:		Tuple  			=							do_convert_rgb




       def 			UpperCamelCase_	(					self							)	->   List[str]:
            return {
                "do_resize": self.do_resize,
                "size": self.size,
                "do_center_crop": self.do_center_crop,
                "crop_size": self.crop_size,
                "do_normalize": self.do_normalize,
                "image_mean": self.image_mean,
                "image_std": self.image_std,
                "do_convert_rgb": self.do_convert_rgb,
            }







       def 			UpperCamelCase_	(					self			,  __lowerCamelCase=False			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)	->   Tuple:
            assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

            if equal_resolution:
                 _SCREAMING_SNAKE_CASE				:		Tuple  			=							[]
                 for i in range(self.batch_size							):
                      image_inputs.append(
                          np.random.randint(
                              2_5_5			,  size=(self.num_channels, self.max_resolution, self.max_resolution)			,  dtype=np.uinta							)							)
            else:
                 _SCREAMING_SNAKE_CASE				:		Dict  			=							[]
                 for i in range(self.batch_size							):
                      _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE				:		Tuple  			=							np.random.choice(np.arange(self.min_resolution			,  self.max_resolution							)			,  2							)
                      image_inputs.append(np.random.randint(2_5_5			,  size=(self.num_channels, width, height)			,  dtype=np.uinta							)							)

            if not numpify and not torchify:
                 # PIL expects the channel dimension as last dimension
                 _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							[Image.fromarray(np.moveaxis(__lowerCamelCase			,  0			,  -1							)							) for x in image_inputs]

            if torchify:
                 _SCREAMING_SNAKE_CASE				:		List[Any]  			=							[torch.from_numpy(__lowerCamelCase							) for x in image_inputs]

            return image_inputs





@require_torch
@require_vision
class 	lowerCAmelCase__(       __lowercase     ,   unittest.TestCase			):






       '''simple docstring'''


       __snake_case       	=			CLIPImageProcessor if is_vision_available() else None




       def 			UpperCamelCase_	(					self							)	->   Dict:
            _SCREAMING_SNAKE_CASE				:		str  			=							CLIPImageProcessingTester(self							)




       @property
       def 			UpperCamelCase_	(					self							)	->   int:
            return self.image_processor_tester.prepare_image_processor_dict()




       def 			UpperCamelCase_	(					self							)	->   int:
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							self.image_processing_class(**self.image_processor_dict							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_resize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "size"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_normalize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_mean"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_std"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_convert_rgb"							)							)




       def 			UpperCamelCase_	(					self							)	->   Optional[Any]:
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							self.image_processing_class.from_dict(self.image_processor_dict							)
            self.assertEqual(image_processor.size			,  {"shortest_edge": 2_0}							)
            self.assertEqual(image_processor.crop_size			,  {"height": 1_8, "width": 1_8}							)

            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							self.image_processing_class.from_dict(self.image_processor_dict			,  size=4_2			,  crop_size=8_4							)
            self.assertEqual(image_processor.size			,  {"shortest_edge": 4_2}							)
            self.assertEqual(image_processor.crop_size			,  {"height": 8_4, "width": 8_4}							)




       def 			UpperCamelCase_	(					self							)	->   Tuple:
            pass




       def 			UpperCamelCase_	(					self							)	->   str:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		str  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random PIL images
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  Image.Image							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE				:		int  			=							image_processing(image_inputs[0]			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )

            # Test batched
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_processing(__lowerCamelCase			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )




       def 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random numpy tensors
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase			,  numpify=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  np.ndarray							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE				:		Dict  			=							image_processing(image_inputs[0]			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )

            # Test batched
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							image_processing(__lowerCamelCase			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )







       def 			UpperCamelCase_	(					self							)	->   Any:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		str  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random PyTorch tensors
            _SCREAMING_SNAKE_CASE				:		int  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase			,  torchify=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  torch.Tensor							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_processing(image_inputs[0]			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )

            # Test batched
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_processing(__lowerCamelCase			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )





@require_torch
@require_vision
class 	lowerCAmelCase__(       __lowercase     ,   unittest.TestCase			):






       '''simple docstring'''


       __snake_case       	=			CLIPImageProcessor if is_vision_available() else None




       def 			UpperCamelCase_	(					self							)	->   int:
            _SCREAMING_SNAKE_CASE				:		Any  			=							CLIPImageProcessingTester(self			,  num_channels=4							)
            _SCREAMING_SNAKE_CASE				:		Any  			=							3




       @property
       def 			UpperCamelCase_	(					self							)	->   Dict:
            return self.image_processor_tester.prepare_image_processor_dict()




       def 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            _SCREAMING_SNAKE_CASE				:		str  			=							self.image_processing_class(**self.image_processor_dict							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_resize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "size"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_normalize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_mean"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "image_std"							)							)
            self.assertTrue(hasattr(__lowerCamelCase			,  "do_convert_rgb"							)							)




       def 			UpperCamelCase_	(					self							)	->   List[str]:
            pass







       def 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE				:		Dict  			=							self.image_processing_class(**self.image_processor_dict							)
            # create random PIL images
            _SCREAMING_SNAKE_CASE				:		Any  			=							self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase			,  Image.Image							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE				:		Dict  			=							image_processing(image_inputs[0]			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    1,
                    self.expected_encoded_image_num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )

            # Test batched
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							image_processing(__lowerCamelCase			,  return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape			,  (
                    self.image_processor_tester.batch_size,
                    self.expected_encoded_image_num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )			,  )