

'''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   ,      snake_case   ,      snake_case=9_9   ,      snake_case=1_3   ,      snake_case=1_6   ,      snake_case=7   ,      snake_case=True   ,      snake_case=True   ,      snake_case=True   ,      snake_case=False   ,      snake_case=True   ,      snake_case=2   ,      snake_case=3_2   ,      snake_case=4   ,      snake_case=4   ,      snake_case=3_0   ,      snake_case=0   ,      snake_case=1   ,      snake_case=2   ,      snake_case=None   ,      ):


							'''simple docstring'''

							UpperCAmelCase		:							List[Any]        =       parent
							UpperCAmelCase		:							Tuple        =       batch_size
							UpperCAmelCase		:							Tuple        =       decoder_seq_length
							# For common tests
							UpperCAmelCase		:							List[str]        =       self.decoder_seq_length
							UpperCAmelCase		:							Tuple        =       is_training
							UpperCAmelCase		:							int        =       use_attention_mask
							UpperCAmelCase		:							List[Any]        =       use_labels

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

							UpperCAmelCase		:							List[Any]        =       None
							UpperCAmelCase		:							int        =       decoder_seq_length
							UpperCAmelCase		:							Union[str, Any]        =       2
							UpperCAmelCase		:							List[Any]        =       1







		def 				A_	(			self      ):


							'''simple docstring'''

							UpperCAmelCase		:							Union[str, Any]        =       ids_tensor([self.batch_size, self.decoder_seq_length]   ,      self.vocab_size      )

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

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

							UpperCAmelCase		:							Dict        =       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   ,      snake_case   ,      snake_case   ,      snake_case   ,      snake_case   ,      ):


							'''simple docstring'''

							UpperCAmelCase		:							str        =       True
							UpperCAmelCase		:							Any        =       SpeechaTextaDecoder(config=snake_case      ).to(snake_case      ).eval()
							UpperCAmelCase		:							List[Any]        =       input_ids[:2]

							input_ids[input_ids == 0] += 1
							# first forward pass
							UpperCAmelCase		:							int        =       model(snake_case   ,      use_cache=snake_case      )
							UpperCAmelCase		:							str        =       model(snake_case      )
							UpperCAmelCase		:							str        =       model(snake_case   ,      use_cache=snake_case      )

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

							UpperCAmelCase		:							List[Any]        =       outputs["past_key_values"]

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

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

							print(snake_case      )

							UpperCAmelCase		:							Optional[int]        =       model(snake_case      )["last_hidden_state"]
							UpperCAmelCase		:							Union[str, Any]        =       model(snake_case   ,      past_key_values=snake_case      )["last_hidden_state"]

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

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







		def 				A_	(			self      ):


							'''simple docstring'''

							UpperCAmelCase		:							Tuple        =       self.prepare_config_and_inputs()
							(
							    (
							    UpperCAmelCase
							) ,			(
							    UpperCAmelCase
							) ,			(
							    UpperCAmelCase
							) ,			(
							    UpperCAmelCase
							) ,			
							)		:							str        =       config_and_inputs

							UpperCAmelCase		:							str        =       {
							    "input_ids": input_ids,
							    "attention_mask": attention_mask,
							}
							return config, inputs_dict







@require_torch
class   UpperCamelCase__   (       lowercase__							,							lowercase__							,							lowercase__							,							unittest.TestCase       ):


		"""simple docstring"""

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







		def 				A_	(			self   ,      ):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[Any]        =       SpeechaTextaStandaloneDecoderModelTester(self   ,      is_training=snake_case      )
							UpperCAmelCase		:							Optional[int]        =       ConfigTester(self   ,      config_class=snake_case      )







		def 				A_	(			self      ):


							'''simple docstring'''

							pass







		def 				A_	(			self      ):


							'''simple docstring'''

							pass







		def 				A_	(			self      ):


							'''simple docstring'''

							pass







		def 				A_	(			self      ):


							'''simple docstring'''

							self.config_tester.run_common_tests()







		def 				A_	(			self      ):


							'''simple docstring'''

							UpperCAmelCase		:							Tuple        =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*snake_case      )







		def 				A_	(			self      ):


							'''simple docstring'''

							return



'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a       :      List[Any]  	=			logging.get_logger(__name__)

a       :      Dict  	=			{
    "apple/mobilevitv2-1.0": "https://huggingface.co/apple/mobilevitv2-1.0/resolve/main/config.json",
}







class   UpperCamelCase__   (       lowercase__       ):


		"""simple docstring"""

		SCREAMING_SNAKE_CASE__  :     List[Any]       =							"mobilevitv2"







		def __init__(			self   ,      snake_case=3   ,      snake_case=2_5_6   ,      snake_case=2   ,      snake_case=2.0   ,      snake_case="swish"   ,      snake_case=3   ,      snake_case=3_2   ,      snake_case=0.1   ,      snake_case=0.02   ,      snake_case=1e-5   ,      snake_case=5_1_2   ,      snake_case=[6, 1_2, 1_8]   ,      snake_case=0.1   ,      snake_case=2_5_5   ,      snake_case=[2, 4, 3]   ,      snake_case=[1_2_8, 1_9_2, 2_5_6]   ,      snake_case=1.0   ,      snake_case=2   ,      snake_case=0.0   ,      snake_case=0.0   ,      **snake_case   ,      ):


							'''simple docstring'''

							super().__init__(**snake_case      )

							UpperCAmelCase		:							Tuple        =       num_channels
							UpperCAmelCase		:							Optional[int]        =       image_size
							UpperCAmelCase		:							Optional[int]        =       patch_size
							UpperCAmelCase		:							Optional[int]        =       expand_ratio
							UpperCAmelCase		:							List[Any]        =       hidden_act
							UpperCAmelCase		:							Optional[Any]        =       conv_kernel_size
							UpperCAmelCase		:							str        =       output_stride
							UpperCAmelCase		:							Optional[Any]        =       initializer_range
							UpperCAmelCase		:							Optional[Any]        =       layer_norm_eps
							UpperCAmelCase		:							List[str]        =       n_attn_blocks
							UpperCAmelCase		:							Any        =       base_attn_unit_dims
							UpperCAmelCase		:							str        =       width_multiplier
							UpperCAmelCase		:							Optional[int]        =       ffn_multiplier
							UpperCAmelCase		:							List[Any]        =       ffn_dropout
							UpperCAmelCase		:							List[str]        =       attn_dropout
							UpperCAmelCase		:							Optional[Any]        =       classifier_dropout_prob

							# decode head attributes for semantic segmentation
							UpperCAmelCase		:							Optional[Any]        =       aspp_out_channels
							UpperCAmelCase		:							Dict        =       atrous_rates
							UpperCAmelCase		:							int        =       aspp_dropout_prob
							UpperCAmelCase		:							List[str]        =       semantic_loss_ignore_index







class   UpperCamelCase__   (       lowercase__       ):


		"""simple docstring"""

		SCREAMING_SNAKE_CASE__  :     Optional[int]       =							version.parse("1.11"       )







		@property
		def 				A_	(			self      ):


							'''simple docstring'''

							return OrderedDict([("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"})]      )







		@property
		def 				A_	(			self      ):


							'''simple docstring'''

							if self.task == "image-classification":
												return OrderedDict([("logits", {0: "batch"})]      )
							else:
												return OrderedDict([("last_hidden_state", {0: "batch"}), ("pooler_output", {0: "batch"})]      )







		@property
		def 				A_	(			self      ):


							'''simple docstring'''

							return 1e-4
