'''simple docstring'''





import unittest

from transformers import TrOCRConfig
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.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM




@require_torch
class  __magic_name__  :





   """simple docstring"""




   def __init__(	self					:  Dict		,  _lowercase					:  List[str]		,  _lowercase					:  int=99		,  _lowercase					:  Any=13		,  _lowercase					:  Dict=16		,  _lowercase					:  Optional[Any]=7		,  _lowercase					:  Optional[Any]=True		,  _lowercase					:  Tuple=True		,  _lowercase					:  Any=True		,  _lowercase					:  Any=False		,  _lowercase					:  List[str]=True		,  _lowercase					:  Union[str, Any]=2		,  _lowercase					:  Optional[int]=32		,  _lowercase					:  int=4		,  _lowercase					:  Dict=4		,  _lowercase					:  int=30		,  _lowercase					:  Dict=0		,  _lowercase					:  int=1		,  _lowercase					:  List[Any]=2		,  _lowercase					:  Optional[int]=None		,  ):



    """simple docstring"""



    _UpperCamelCase:     Optional[Any]							= parent
    _UpperCamelCase:     List[Any]							= batch_size
    _UpperCamelCase:     Optional[Any]							= decoder_seq_length
    # For common tests
    _UpperCamelCase:     Optional[Any]							= self.decoder_seq_length
    _UpperCamelCase:     Any							= is_training
    _UpperCamelCase:     Union[str, Any]							= use_attention_mask
    _UpperCamelCase:     List[str]							= use_labels

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

    _UpperCamelCase:     Dict							= None
    _UpperCamelCase:     Union[str, Any]							= decoder_seq_length
    _UpperCamelCase:     Optional[Any]							= 2
    _UpperCamelCase:     Union[str, Any]							= 1



   def   lowerCAmelCase						(	self					:  Any							):



    """simple docstring"""



    _UpperCamelCase:     Tuple							= ids_tensor([self.batch_size, self.decoder_seq_length]		,  self.vocab_size							)

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

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

    _UpperCamelCase:     List[str]							= TrOCRConfig(
        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					:  Tuple		,  _lowercase					:  List[Any]		,  _lowercase					:  Optional[int]		,  _lowercase					:  Optional[int]		,  _lowercase					:  Dict		,  ):



    """simple docstring"""



    _UpperCamelCase:     Optional[Any]							= True
    _UpperCamelCase:     Any							= TrOCRDecoder(config=_lowercase							).to(_lowercase							).eval()
    _UpperCamelCase:     Any							= input_ids[:2]

    input_ids[input_ids == 0] += 1
    # first forward pass
    _UpperCamelCase:     str							= model(_lowercase		,  use_cache=_lowercase							)
    _UpperCamelCase:     int							= model(_lowercase							)
    _UpperCamelCase:     Any							= model(_lowercase		,  use_cache=_lowercase							)

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

    _UpperCamelCase:     Tuple							= outputs['''past_key_values''']

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

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

    _UpperCamelCase:     int							= model(_lowercase							)['''last_hidden_state''']
    _UpperCamelCase:     List[str]							= model(_lowercase		,  past_key_values=_lowercase							)['''last_hidden_state''']

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

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



   def   lowerCAmelCase						(	self					:  Optional[Any]							):



    """simple docstring"""



    _UpperCamelCase:     int							= self.prepare_config_and_inputs()
    _UpperCamelCase:     Union[str, Any]							= config_and_inputs

    _UpperCamelCase:     List[Any]							= {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
    return config, inputs_dict




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





   """simple docstring"""




   lowerCAmelCase      :    List[str]  			=   (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
   lowerCAmelCase      :    Union[str, Any]  			=   (TrOCRForCausalLM,) if is_torch_available() else ()
   lowerCAmelCase      :    Dict  			=   {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
   lowerCAmelCase      :    Optional[int]  			=   True
   lowerCAmelCase      :    List[Any]  			=   False



   def   lowerCAmelCase						(	self					:  Dict							):



    """simple docstring"""



    _UpperCamelCase:     int							= TrOCRStandaloneDecoderModelTester(self		,  is_training=_lowercase							)
    _UpperCamelCase:     Any							= ConfigTester(self		,  config_class=_lowercase							)



   def   lowerCAmelCase						(	self					:  str							):



    """simple docstring"""



    pass



   def   lowerCAmelCase						(	self					:  Optional[int]							):



    """simple docstring"""



    pass



   def   lowerCAmelCase						(	self					:  Optional[int]							):



    """simple docstring"""



    pass



   def   lowerCAmelCase						(	self					:  Any							):



    """simple docstring"""



    self.config_tester.run_common_tests()



   def   lowerCAmelCase						(	self					:  Any							):



    """simple docstring"""



    _UpperCamelCase:     Tuple							= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_decoder_model_past(*_lowercase							)



   def   lowerCAmelCase						(	self					:  List[str]							):



    """simple docstring"""



    return



   @unittest.skip('''The model doesn\'t support left padding'''							)  # and it's not used enough to be worth fixing :)
   def   lowerCAmelCase						(	self					:  Optional[Any]							):



    """simple docstring"""



    pass
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase_			    =							{'''configuration_xlnet''': ['''XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLNetConfig''']}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_			    =							['''XLNetTokenizer''']

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_			    =							['''XLNetTokenizerFast''']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_			    =							[
      '''XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''XLNetForMultipleChoice''',
      '''XLNetForQuestionAnswering''',
      '''XLNetForQuestionAnsweringSimple''',
      '''XLNetForSequenceClassification''',
      '''XLNetForTokenClassification''',
      '''XLNetLMHeadModel''',
      '''XLNetModel''',
      '''XLNetPreTrainedModel''',
      '''load_tf_weights_in_xlnet''',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_			    =							[
      '''TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TFXLNetForMultipleChoice''',
      '''TFXLNetForQuestionAnsweringSimple''',
      '''TFXLNetForSequenceClassification''',
      '''TFXLNetForTokenClassification''',
      '''TFXLNetLMHeadModel''',
      '''TFXLNetMainLayer''',
      '''TFXLNetModel''',
      '''TFXLNetPreTrainedModel''',
  ]


if TYPE_CHECKING:
  from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet import XLNetTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet_fast import XLNetTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlnet import (
        XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLNetForMultipleChoice,
        XLNetForQuestionAnswering,
        XLNetForQuestionAnsweringSimple,
        XLNetForSequenceClassification,
        XLNetForTokenClassification,
        XLNetLMHeadModel,
        XLNetModel,
        XLNetPreTrainedModel,
        load_tf_weights_in_xlnet,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlnet import (
        TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLNetForMultipleChoice,
        TFXLNetForQuestionAnsweringSimple,
        TFXLNetForSequenceClassification,
        TFXLNetForTokenClassification,
        TFXLNetLMHeadModel,
        TFXLNetMainLayer,
        TFXLNetModel,
        TFXLNetPreTrainedModel,
    )

else:
  import sys

  UpperCAmelCase_			    =							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)