




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:



 '''simple docstring'''


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

       """simple docstring"""






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

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

       lowerCamelCase_		     =    None
       lowerCamelCase_		     =    decoder_seq_length
       lowerCamelCase_		     =    2
       lowerCamelCase_		     =    1

 def 				a__ (				self					:  Optional[int]  )   ->	List[str]:

       """simple docstring"""






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

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

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

       lowerCamelCase_		     =    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_					:  Union[str, Any]						,							A_					:  Any						,							A_					:  Optional[Any]						,							A_					:  Any						,							)   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    TrOCRDecoder(config=A_  ).to(A_  ).eval()
       lowerCamelCase_		     =    input_ids[:2]

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

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

       lowerCamelCase_		     =    outputs['past_key_values']

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

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

       lowerCamelCase_		     =    model(A_  )['last_hidden_state']
       lowerCamelCase_		     =    model(A_						,							past_key_values=A_  )['last_hidden_state']

       # select random slice
       lowerCamelCase_		     =    ids_tensor((1,)						,							output_from_past.shape[-1]  ).item()
       lowerCamelCase_		     =    output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
       lowerCamelCase_		     =    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					:  str  )   ->	Dict:

       """simple docstring"""






       lowerCamelCase_		     =    self.prepare_config_and_inputs()
       lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		     =    config_and_inputs

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







@require_torch
class   A(						UpperCamelCase							,      UpperCamelCase							,      UpperCamelCase							,      unittest.TestCase   ):



 '''simple docstring'''


 UpperCamelCase						   =						(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
 UpperCamelCase						   =						(TrOCRForCausalLM,) if is_torch_available() else ()
 UpperCamelCase						   =						{'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
 UpperCamelCase						   =						True
 UpperCamelCase						   =						False

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

       """simple docstring"""






       lowerCamelCase_		     =    TrOCRStandaloneDecoderModelTester(self						,							is_training=A_  )
       lowerCamelCase_		     =    ConfigTester(self						,							config_class=A_  )

 def 				a__ (				self					:  Optional[int]  )   ->	List[str]:

       """simple docstring"""






       pass

 def 				a__ (				self					:  Dict  )   ->	int:

       """simple docstring"""






       pass

 def 				a__ (				self					:  int  )   ->	Dict:

       """simple docstring"""






       pass

 def 				a__ (				self					:  int  )   ->	int:

       """simple docstring"""






       self.config_tester.run_common_tests()

 def 				a__ (				self					:  int  )   ->	int:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past(*A_  )

 def 				a__ (				self					:  Any  )   ->	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					:  Dict  )   ->	Union[str, Any]:

       """simple docstring"""






       pass









def 			_SCREAMING_SNAKE_CASE			(							lowercase	:       int	,							lowercase	:       int     ):


      '''simple docstring'''



      return int((input_a, input_a).count(1     ) != 0     )

def 			_SCREAMING_SNAKE_CASE			(							):


      '''simple docstring'''



      assert or_gate(0	,							0     ) == 0
      assert or_gate(0	,							1     ) == 1
      assert or_gate(1	,							0     ) == 1
      assert or_gate(1	,							1     ) == 1


if __name__ == "__main__":
 print(or_gate(0, 1))
 print(or_gate(1, 0))
 print(or_gate(0, 0))
 print(or_gate(1, 1))



