

'''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 				A__       :





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE				       =parent
       _SCREAMING_SNAKE_CASE				       =batch_size
       _SCREAMING_SNAKE_CASE				       =decoder_seq_length
       # For common tests
       _SCREAMING_SNAKE_CASE				       =self.decoder_seq_length
       _SCREAMING_SNAKE_CASE				       =is_training
       _SCREAMING_SNAKE_CASE				       =use_attention_mask
       _SCREAMING_SNAKE_CASE				       =use_labels

       _SCREAMING_SNAKE_CASE				       =vocab_size
       _SCREAMING_SNAKE_CASE				       =d_model
       _SCREAMING_SNAKE_CASE				       =d_model
       _SCREAMING_SNAKE_CASE				       =decoder_layers
       _SCREAMING_SNAKE_CASE				       =decoder_layers
       _SCREAMING_SNAKE_CASE				       =decoder_ffn_dim
       _SCREAMING_SNAKE_CASE				       =decoder_attention_heads
       _SCREAMING_SNAKE_CASE				       =decoder_attention_heads
       _SCREAMING_SNAKE_CASE				       =eos_token_id
       _SCREAMING_SNAKE_CASE				       =bos_token_id
       _SCREAMING_SNAKE_CASE				       =pad_token_id
       _SCREAMING_SNAKE_CASE				       =decoder_start_token_id
       _SCREAMING_SNAKE_CASE				       =use_cache
       _SCREAMING_SNAKE_CASE				       =max_position_embeddings

       _SCREAMING_SNAKE_CASE				       =None
       _SCREAMING_SNAKE_CASE				       =decoder_seq_length
       _SCREAMING_SNAKE_CASE				       =2
       _SCREAMING_SNAKE_CASE				       =1





  def 		A    (						self :      List[Any]					)      ->       Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE				       =ids_tensor([self.batch_size, self.decoder_seq_length]				,   self.vocab_size					)

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

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

       _SCREAMING_SNAKE_CASE				       =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 		A    (						self :      List[str]				,   _a :      Tuple				,   _a :      List[str]				,   _a :      Dict				,   _a :      Optional[int]				,   )      ->       int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE				       =True
       _SCREAMING_SNAKE_CASE				       =TrOCRDecoder(config=A__					).to(A__					).eval()
       _SCREAMING_SNAKE_CASE				       =input_ids[:2]

       input_ids[input_ids == 0] += 1
       # first forward pass
       _SCREAMING_SNAKE_CASE				       =model(A__				,   use_cache=A__					)
       _SCREAMING_SNAKE_CASE				       =model(A__					)
       _SCREAMING_SNAKE_CASE				       =model(A__				,   use_cache=A__					)

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

       _SCREAMING_SNAKE_CASE				       =outputs['''past_key_values''']

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

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

       _SCREAMING_SNAKE_CASE				       =model(A__					)['''last_hidden_state''']
       _SCREAMING_SNAKE_CASE				       =model(A__				,   past_key_values=A__					)['''last_hidden_state''']

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

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





  def 		A    (						self :      List[str]					)      ->       Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE				       =self.prepare_config_and_inputs()
       _SCREAMING_SNAKE_CASE				       =config_and_inputs

       _SCREAMING_SNAKE_CASE				       ={'''input_ids''': input_ids, '''attention_mask''': attention_mask}
       return config, inputs_dict







@require_torch
class 				A__       (			__snake_case	,  __snake_case	,  __snake_case	,  unittest.TestCase    ):
  A__											=							(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
  A__											=							(TrOCRForCausalLM,) if is_torch_available() else ()
  A__											=							{"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
  A__											=							True
  A__											=							False





  def 		A    (						self :      str					)      ->       int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE				       =TrOCRStandaloneDecoderModelTester(self				,   is_training=A__					)
       _SCREAMING_SNAKE_CASE				       =ConfigTester(self				,   config_class=A__					)





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





       '''simple docstring'''






       pass





  def 		A    (						self :      Tuple					)      ->       List[Any]:





       '''simple docstring'''






       pass





  def 		A    (						self :      List[str]					)      ->       List[Any]:





       '''simple docstring'''






       pass





  def 		A    (						self :      Union[str, Any]					)      ->       Dict:





       '''simple docstring'''






       self.config_tester.run_common_tests()





  def 		A    (						self :      List[str]					)      ->       Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE				       =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past(*A__					)





  def 		A    (						self :      Any					)      ->       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 		A    (						self :      Optional[int]					)      ->       str:





       '''simple docstring'''






       pass








from ...configuration_utils import PretrainedConfig
from ...utils import logging


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

_UpperCamelCase			:	Tuple								={
    'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json',
    # See all CANINE models at https://huggingface.co/models?filter=canine
}






class 						UpperCAmelCase__ (							__snake_case							):
		__snake_case      :       Any     =   "canine"







		def __init__(					self       ,A__=768       ,A__=12       ,A__=12       ,A__=3072       ,A__="gelu"       ,A__=0.1       ,A__=0.1       ,A__=16384       ,A__=16       ,A__=0.02       ,A__=1E-12       ,A__=0       ,A__=0xE_000       ,A__=0xE_001       ,A__=4       ,A__=4       ,A__=8       ,A__=16384       ,A__=128       ,**A__       ,):
						super().__init__(pad_token_id=A__       ,bos_token_id=A__       ,eos_token_id=A__       ,**A__		)

						_A	:				int   =      max_position_embeddings
						_A	:				Dict   =      hidden_size
						_A	:				List[str]   =      num_hidden_layers
						_A	:				Optional[Any]   =      num_attention_heads
						_A	:				Optional[Any]   =      intermediate_size
						_A	:				Optional[Any]   =      hidden_act
						_A	:				str   =      hidden_dropout_prob
						_A	:				Optional[int]   =      attention_probs_dropout_prob
						_A	:				Union[str, Any]   =      initializer_range
						_A	:				Dict   =      type_vocab_size
						_A	:				Optional[Any]   =      layer_norm_eps

						# Character config:
						_A	:				Union[str, Any]   =      downsampling_rate
						_A	:				List[str]   =      upsampling_kernel_size
						_A	:				str   =      num_hash_functions
						_A	:				Optional[Any]   =      num_hash_buckets
						_A	:				Optional[Any]   =      local_transformer_stride



