


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




       """simple docstring"""



       def __init__(      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE=99				,       _SCREAMING_SNAKE_CASE=13				,       _SCREAMING_SNAKE_CASE=16				,       _SCREAMING_SNAKE_CASE=7				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=2				,       _SCREAMING_SNAKE_CASE=32				,       _SCREAMING_SNAKE_CASE=4				,       _SCREAMING_SNAKE_CASE=4				,       _SCREAMING_SNAKE_CASE=30				,       _SCREAMING_SNAKE_CASE=0				,       _SCREAMING_SNAKE_CASE=1				,       _SCREAMING_SNAKE_CASE=2				,       _SCREAMING_SNAKE_CASE=None				,       )->Dict:




         '''simple docstring'''





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

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

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



       def        _snake_case       (      self		)->int:




         '''simple docstring'''





         A_      :				int		     =					ids_tensor([self.batch_size, self.decoder_seq_length]				,       self.vocab_size		)

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

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

         A_      :				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        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       )->Dict:




         '''simple docstring'''





         A_      :				Union[str, Any]		     =					True
         A_      :				Dict		     =					TrOCRDecoder(config=SCREAMING_SNAKE_CASE__		).to(SCREAMING_SNAKE_CASE__		).eval()
         A_      :				List[Any]		     =					input_ids[:2]

         input_ids[input_ids == 0] += 1
         # first forward pass
         A_      :				Tuple		     =					model(SCREAMING_SNAKE_CASE__				,       use_cache=SCREAMING_SNAKE_CASE__		)
         A_      :				List[str]		     =					model(SCREAMING_SNAKE_CASE__		)
         A_      :				List[Any]		     =					model(SCREAMING_SNAKE_CASE__				,       use_cache=SCREAMING_SNAKE_CASE__		)

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

         A_      :				Union[str, Any]		     =					outputs['''past_key_values''']

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

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

         A_      :				Tuple		     =					model(SCREAMING_SNAKE_CASE__		)['''last_hidden_state''']
         A_      :				Union[str, Any]		     =					model(SCREAMING_SNAKE_CASE__				,       past_key_values=SCREAMING_SNAKE_CASE__		)['''last_hidden_state''']

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

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







       def        _snake_case       (      self		)->Optional[int]:




         '''simple docstring'''





         A_      :				List[Any]		     =					self.prepare_config_and_inputs()
         A_     ,       A_     ,       A_     ,       A_      :				List[str]		     =					config_and_inputs

         A_      :				Any		     =					{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
         return config, inputs_dict






@require_torch
class   _lowerCamelCase (					UpperCAmelCase__					,							UpperCAmelCase__					,							UpperCAmelCase__					,							unittest.TestCase			):




       """simple docstring"""
       snake_case      =    (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
       snake_case      =    (TrOCRForCausalLM,) if is_torch_available() else ()
       snake_case      =    {"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
       snake_case      =    True
       snake_case      =    False



       def        _snake_case       (      self		)->str:




         '''simple docstring'''





         A_      :				str		     =					TrOCRStandaloneDecoderModelTester(self				,       is_training=SCREAMING_SNAKE_CASE__		)
         A_      :				Any		     =					ConfigTester(self				,       config_class=SCREAMING_SNAKE_CASE__		)



       def        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         pass



       def        _snake_case       (      self		)->Any:




         '''simple docstring'''





         pass



       def        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         pass



       def        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         self.config_tester.run_common_tests()



       def        _snake_case       (      self		)->Optional[int]:




         '''simple docstring'''





         A_      :				str		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past(*SCREAMING_SNAKE_CASE__		)



       def        _snake_case       (      self		)->Optional[Any]:




         '''simple docstring'''





         return







       @unittest.skip('''The model doesn\'t support left padding'''		)  # and it's not used enough to be worth fixing :)
       def        _snake_case       (      self		)->List[str]:




         '''simple docstring'''





         pass





'''simple docstring'''

import warnings

from ...utils import logging
from .image_processing_segformer import SegformerImageProcessor


_A			:      Optional[int]					      =				logging.get_logger(__name__)







class 				_lowercase     (				UpperCAmelCase__			):


 '''simple docstring'''






 def __init__(      self   :	Dict      ,       *SCREAMING_SNAKE_CASE__   :	Optional[int]      ,       **SCREAMING_SNAKE_CASE__   :	List[str]							)  ->    None:
  warnings.warn(
      """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers."""
      """ Please use SegformerImageProcessor instead."""      ,       SCREAMING_SNAKE_CASE__      ,       )
  super().__init__(*SCREAMING_SNAKE_CASE__      ,       **SCREAMING_SNAKE_CASE__							)






