






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





    def __init__(							self	:					Union[str, Any]				,		A	:					Dict				,		A	:					List[str]=9_9				,		A	:					Optional[int]=1_3				,		A	:					int=1_6				,		A	:					str=7				,		A	:					Union[str, Any]=True				,		A	:					Optional[int]=True				,		A	:					str=True				,		A	:					List[str]=False				,		A	:					List[Any]=True				,		A	:					List[Any]=2				,		A	:					Optional[Any]=3_2				,		A	:					int=4				,		A	:					List[Any]=4				,		A	:					List[str]=3_0				,		A	:					int=0				,		A	:					List[str]=1				,		A	:					int=2				,		A	:					Dict=None				,		):


      '''simple docstring'''




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

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

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





    def 				lowerCamelCase__      (							self	:					Tuple			):


      '''simple docstring'''




      a     :			Any				=   ids_tensor([self.batch_size, self.decoder_seq_length]				,		self.vocab_size			)

      a     :			Tuple				=   None
      if self.use_attention_mask:
        a     :			Optional[int]				=   ids_tensor([self.batch_size, self.decoder_seq_length]				,		vocab_size=2			)

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

      a     :			Tuple				=   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 				lowerCamelCase__      (							self	:					List[str]				,		A	:					Union[str, Any]				,		A	:					Union[str, Any]				,		A	:					List[str]				,		A	:					List[str]				,		):


      '''simple docstring'''




      a     :			Dict				=   True
      a     :			Any				=   SpeechaTextaDecoder(config=__UpperCAmelCase			).to(__UpperCAmelCase			).eval()
      a     :			Any				=   input_ids[:2]

      input_ids[input_ids == 0] += 1
      # first forward pass
      a     :			Dict				=   model(__UpperCAmelCase				,		use_cache=__UpperCAmelCase			)
      a     :			List[Any]				=   model(__UpperCAmelCase			)
      a     :			int				=   model(__UpperCAmelCase				,		use_cache=__UpperCAmelCase			)

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

      a     :			Optional[Any]				=   outputs['past_key_values']

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

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

      print(__UpperCAmelCase			)

      a     :			Dict				=   model(__UpperCAmelCase			)['last_hidden_state']
      a     :			int				=   model(__UpperCAmelCase				,		past_key_values=__UpperCAmelCase			)['last_hidden_state']

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

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





    def 				lowerCamelCase__      (							self	:					Union[str, Any]			):


      '''simple docstring'''




      a     :			str				=   self.prepare_config_and_inputs()
      (
          (
          a   
      ),     (
          a   
      ),     (
          a   
      ),     (
          a   
      ),        
      )     :			Any				=   config_and_inputs

      a     :			Optional[Any]				=   {
          'input_ids': input_ids,
          'attention_mask': attention_mask,
      }
      return config, inputs_dict






@require_torch
class 						snake_case  (  snake_case__   , snake_case__   , snake_case__   , unittest.TestCase       ):
    __magic_name__         =     (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
    __magic_name__         =     (SpeechaTextaForCausalLM,) if is_torch_available() else ()
    __magic_name__         =     {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
    __magic_name__         =     True
    __magic_name__         =     False





    def 				lowerCamelCase__      (							self	:					str				,		):


      '''simple docstring'''




      a     :			Union[str, Any]				=   SpeechaTextaStandaloneDecoderModelTester(self				,		is_training=__UpperCAmelCase			)
      a     :			Tuple				=   ConfigTester(self				,		config_class=__UpperCAmelCase			)





    def 				lowerCamelCase__      (							self	:					Tuple			):


      '''simple docstring'''




      pass





    def 				lowerCamelCase__      (							self	:					Union[str, Any]			):


      '''simple docstring'''




      pass





    def 				lowerCamelCase__      (							self	:					Optional[Any]			):


      '''simple docstring'''




      pass





    def 				lowerCamelCase__      (							self	:					int			):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    def 				lowerCamelCase__      (							self	:					Optional[int]			):


      '''simple docstring'''




      a     :			str				=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past(*__UpperCAmelCase			)





    def 				lowerCamelCase__      (							self	:					Dict			):


      '''simple docstring'''




      return









from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PretrainedConfig, PreTrainedTokenizer
from ...onnx import OnnxConfig, PatchingSpec
from ...utils import TensorType, is_torch_available, logging


__A				    =						logging.get_logger(__name__)

__A				    =						{
    '''microsoft/layoutlm-base-uncased''': (
        '''https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/config.json'''
    ),
    '''microsoft/layoutlm-large-uncased''': (
        '''https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/config.json'''
    ),
}






class 		lowercase						( snake_case__):






	"""simple docstring"""


	a__      :					Dict		   =							"layoutlm"




	def __init__(	self	:						List[Any]						,  __UpperCAmelCase	:						int=30_522						,  __UpperCAmelCase	:						List[Any]=768						,  __UpperCAmelCase	:						Optional[int]=12						,  __UpperCAmelCase	:						List[str]=12						,  __UpperCAmelCase	:						int=3_072						,  __UpperCAmelCase	:						str="gelu"						,  __UpperCAmelCase	:						str=0.1						,  __UpperCAmelCase	:						Any=0.1						,  __UpperCAmelCase	:						Union[str, Any]=512						,  __UpperCAmelCase	:						Any=2						,  __UpperCAmelCase	:						Any=0.02						,  __UpperCAmelCase	:						Dict=1E-12						,  __UpperCAmelCase	:						Optional[int]=0						,  __UpperCAmelCase	:						List[str]="absolute"						,  __UpperCAmelCase	:						List[Any]=True						,  __UpperCAmelCase	:						Optional[int]=1_024						,  **__UpperCAmelCase	:						int						,  )			-> Union[str, Any]:
							super().__init__(pad_token_id=__UpperCAmelCase						,  **__UpperCAmelCase     )
							UpperCAmelCase_=   vocab_size
							UpperCAmelCase_=   hidden_size
							UpperCAmelCase_=   num_hidden_layers
							UpperCAmelCase_=   num_attention_heads
							UpperCAmelCase_=   hidden_act
							UpperCAmelCase_=   intermediate_size
							UpperCAmelCase_=   hidden_dropout_prob
							UpperCAmelCase_=   attention_probs_dropout_prob
							UpperCAmelCase_=   max_position_embeddings
							UpperCAmelCase_=   type_vocab_size
							UpperCAmelCase_=   initializer_range
							UpperCAmelCase_=   layer_norm_eps
							UpperCAmelCase_=   position_embedding_type
							UpperCAmelCase_=   use_cache
							UpperCAmelCase_=   max_ad_position_embeddings






class 		lowercase						( snake_case__):






	"""simple docstring"""


	def __init__(	self	:						List[str]						,  __UpperCAmelCase	:						PretrainedConfig						,  __UpperCAmelCase	:						str = "default"						,  __UpperCAmelCase	:						List[PatchingSpec] = None						,  )			-> str:
							super().__init__(__UpperCAmelCase						,  task=__UpperCAmelCase						,  patching_specs=__UpperCAmelCase     )
							UpperCAmelCase_=   config.max_ad_position_embeddings - 1




	@property
	def   _SCREAMING_SNAKE_CASE		(	self	:						Optional[Any]     )			-> Mapping[str, Mapping[int, str]]:
							return OrderedDict(
							    [
							        ("""input_ids""", {0: """batch""", 1: """sequence"""}),
							        ("""bbox""", {0: """batch""", 1: """sequence"""}),
							        ("""attention_mask""", {0: """batch""", 1: """sequence"""}),
							        ("""token_type_ids""", {0: """batch""", 1: """sequence"""}),
							    ]     )




	def   _SCREAMING_SNAKE_CASE		(	self	:						Any						,  __UpperCAmelCase	:						PreTrainedTokenizer						,  __UpperCAmelCase	:						int = -1						,  __UpperCAmelCase	:						int = -1						,  __UpperCAmelCase	:						bool = False						,  __UpperCAmelCase	:						Optional[TensorType] = None						,  )			-> Mapping[str, Any]:
							UpperCAmelCase_=   super().generate_dummy_inputs(
							    __UpperCAmelCase						,  batch_size=__UpperCAmelCase						,  seq_length=__UpperCAmelCase						,  is_pair=__UpperCAmelCase						,  framework=__UpperCAmelCase     )

							# Generate a dummy bbox
							UpperCAmelCase_=   [48, 84, 73, 128]

							if not framework == TensorType.PYTORCH:
													raise NotImplementedError("""Exporting LayoutLM to ONNX is currently only supported for PyTorch."""     )

							if not is_torch_available():
													raise ValueError("""Cannot generate dummy inputs without PyTorch installed."""     )
							import torch

							UpperCAmelCase_,   UpperCAmelCase_=   input_dict["""input_ids"""].shape
							UpperCAmelCase_=   torch.tensor([*[box] * seq_length]     ).tile(__UpperCAmelCase						,  1						,  1     )
							return input_dict




