




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


   """simple docstring"""

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

      """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 							lowerCAmelCase_      (   self :     List[Any]   ):

      """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 							lowerCAmelCase_      (   self :     Optional[Any]  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     Tuple  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     Optional[Any]  ,				):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				True
      _SCREAMING_SNAKE_CASE	=				TrOCRDecoder(config=__lowerCamelCase   ).to(__lowerCamelCase   ).eval()
      _SCREAMING_SNAKE_CASE	=				input_ids[:2]

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

      self.parent.assertTrue(len(__lowerCamelCase   ) == len(__lowerCamelCase   )   )
      self.parent.assertTrue(len(__lowerCamelCase   ) == len(__lowerCamelCase   ) + 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(__lowerCamelCase   )["last_hidden_state"]
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				past_key_values=__lowerCamelCase   )["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(__lowerCamelCase  ,				__lowerCamelCase  ,				atol=1e-3   )



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.prepare_config_and_inputs()
      _SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE	=				config_and_inputs

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




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


   """simple docstring"""

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



   def 							lowerCAmelCase_      (   self :     Any   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				TrOCRStandaloneDecoderModelTester(self  ,				is_training=__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				ConfigTester(self  ,				config_class=__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      pass



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      pass



   def 							lowerCAmelCase_      (   self :     Optional[int]   ):

      """simple docstring"""
      pass



   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      self.config_tester.run_common_tests()



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """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[int]   ):

      """simple docstring"""
      pass







'''simple docstring'''



from collections import OrderedDict
from typing import List, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_			=						logging.get_logger(__name__)

lowerCamelCase_			=						{
    'google/efficientnet-b7': 'https://huggingface.co/google/efficientnet-b7/resolve/main/config.json',
}







class 		lowercase_   (			A       ):


   """simple docstring"""

   lowerCamelCase_      =						'''efficientnet'''



   def __init__(   self :     Optional[Any]  ,				__lowerCamelCase :     int = 3  ,				__lowerCamelCase :     int = 6_0_0  ,				__lowerCamelCase :     float = 2.0  ,				__lowerCamelCase :     float = 3.1  ,				__lowerCamelCase :     int = 8  ,				__lowerCamelCase :     List[int] = [3, 3, 5, 3, 5, 5, 3]  ,				__lowerCamelCase :     List[int] = [3_2, 1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2]  ,				__lowerCamelCase :     List[int] = [1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2, 3_2_0]  ,				__lowerCamelCase :     List[int] = []  ,				__lowerCamelCase :     List[int] = [1, 2, 2, 2, 1, 2, 1]  ,				__lowerCamelCase :     List[int] = [1, 2, 2, 3, 3, 4, 1]  ,				__lowerCamelCase :     List[int] = [1, 6, 6, 6, 6, 6, 6]  ,				__lowerCamelCase :     float = 0.2_5  ,				__lowerCamelCase :     str = "swish"  ,				__lowerCamelCase :     int = 2_5_6_0  ,				__lowerCamelCase :     str = "mean"  ,				__lowerCamelCase :     float = 0.0_2  ,				__lowerCamelCase :     float = 0.0_0_1  ,				__lowerCamelCase :     float = 0.9_9  ,				__lowerCamelCase :     float = 0.5  ,				__lowerCamelCase :     float = 0.2  ,				**__lowerCamelCase :     Tuple  ,				):

      """simple docstring"""
      super().__init__(**__lowerCamelCase   )

      _SCREAMING_SNAKE_CASE	=				num_channels
      _SCREAMING_SNAKE_CASE	=				image_size
      _SCREAMING_SNAKE_CASE	=				width_coefficient
      _SCREAMING_SNAKE_CASE	=				depth_coefficient
      _SCREAMING_SNAKE_CASE	=				depth_divisor
      _SCREAMING_SNAKE_CASE	=				kernel_sizes
      _SCREAMING_SNAKE_CASE	=				in_channels
      _SCREAMING_SNAKE_CASE	=				out_channels
      _SCREAMING_SNAKE_CASE	=				depthwise_padding
      _SCREAMING_SNAKE_CASE	=				strides
      _SCREAMING_SNAKE_CASE	=				num_block_repeats
      _SCREAMING_SNAKE_CASE	=				expand_ratios
      _SCREAMING_SNAKE_CASE	=				squeeze_expansion_ratio
      _SCREAMING_SNAKE_CASE	=				hidden_act
      _SCREAMING_SNAKE_CASE	=				hidden_dim
      _SCREAMING_SNAKE_CASE	=				pooling_type
      _SCREAMING_SNAKE_CASE	=				initializer_range
      _SCREAMING_SNAKE_CASE	=				batch_norm_eps
      _SCREAMING_SNAKE_CASE	=				batch_norm_momentum
      _SCREAMING_SNAKE_CASE	=				dropout_rate
      _SCREAMING_SNAKE_CASE	=				drop_connect_rate
      _SCREAMING_SNAKE_CASE	=				sum(__lowerCamelCase   ) * 4







class 		lowercase_   (			A       ):


   """simple docstring"""

   lowerCamelCase_      =						version.parse('''1.11'''       )



   @property
   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      return OrderedDict(
          [
              ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
          ]   )



   @property
   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      return 1e-5

