
'''simple docstring'''



import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, 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 import (
         OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
         OpenAIGPTConfig,
         OpenAIGPTDoubleHeadsModel,
         OpenAIGPTForSequenceClassification,
         OpenAIGPTLMHeadModel,
         OpenAIGPTModel,
     )

class  _a					:





       """simple docstring"""




       def __init__(				self							,				A__							,				A__=13							,				A__=7							,				A__=True							,				A__=True							,				A__=True							,				A__=99							,				A__=32							,				A__=5							,				A__=4							,				A__=37							,				A__="gelu"							,				A__=0.1							,				A__=0.1							,				A__=5_12							,				A__=16							,				A__=2							,				A__=0.02							,				A__=3							,				A__=4							,				A__=None							,				)     ->      Optional[int]:
          _SCREAMING_SNAKE_CASE  			=					parent
          _SCREAMING_SNAKE_CASE  			=					batch_size
          _SCREAMING_SNAKE_CASE  			=					seq_length
          _SCREAMING_SNAKE_CASE  			=					is_training
          _SCREAMING_SNAKE_CASE  			=					use_token_type_ids
          _SCREAMING_SNAKE_CASE  			=					use_labels
          _SCREAMING_SNAKE_CASE  			=					vocab_size
          _SCREAMING_SNAKE_CASE  			=					hidden_size
          _SCREAMING_SNAKE_CASE  			=					num_hidden_layers
          _SCREAMING_SNAKE_CASE  			=					num_attention_heads
          _SCREAMING_SNAKE_CASE  			=					intermediate_size
          _SCREAMING_SNAKE_CASE  			=					hidden_act
          _SCREAMING_SNAKE_CASE  			=					hidden_dropout_prob
          _SCREAMING_SNAKE_CASE  			=					attention_probs_dropout_prob
          _SCREAMING_SNAKE_CASE  			=					max_position_embeddings
          _SCREAMING_SNAKE_CASE  			=					type_vocab_size
          _SCREAMING_SNAKE_CASE  			=					type_sequence_label_size
          _SCREAMING_SNAKE_CASE  			=					initializer_range
          _SCREAMING_SNAKE_CASE  			=					num_labels
          _SCREAMING_SNAKE_CASE  			=					num_choices
          _SCREAMING_SNAKE_CASE  			=					scope
          _SCREAMING_SNAKE_CASE  			=					self.vocab_size - 1






       def 							UpperCamelCase					(				self   )     ->      List[str]:
          _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.batch_size, self.seq_length]							,				self.vocab_size   )

          _SCREAMING_SNAKE_CASE  			=					None
          if self.use_token_type_ids:
             _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.batch_size, self.seq_length]							,				self.type_vocab_size   )

          _SCREAMING_SNAKE_CASE  			=					None
          _SCREAMING_SNAKE_CASE  			=					None
          _SCREAMING_SNAKE_CASE  			=					None
          if self.use_labels:
             _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.batch_size]							,				self.type_sequence_label_size   )
             _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.batch_size, self.seq_length]							,				self.num_labels   )
             _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.batch_size]							,				self.num_choices   )

          _SCREAMING_SNAKE_CASE  			=					OpenAIGPTConfig(
              vocab_size=self.vocab_size							,				n_embd=self.hidden_size							,				n_layer=self.num_hidden_layers							,				n_head=self.num_attention_heads							,				n_positions=self.max_position_embeddings							,				pad_token_id=self.pad_token_id							,				)

          _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.num_hidden_layers, self.num_attention_heads]							,				2   )

          return (
              config,
              input_ids,
              head_mask,
              token_type_ids,
              sequence_labels,
              token_labels,
              choice_labels,
          )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				*A__   )     ->      Dict:
          _SCREAMING_SNAKE_CASE  			=					OpenAIGPTModel(config=_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()

          _SCREAMING_SNAKE_CASE  			=					model(_lowerCAmelCase							,				token_type_ids=_lowerCAmelCase							,				head_mask=_lowerCAmelCase   )
          _SCREAMING_SNAKE_CASE  			=					model(_lowerCAmelCase							,				token_type_ids=_lowerCAmelCase   )
          _SCREAMING_SNAKE_CASE  			=					model(_lowerCAmelCase   )

          self.parent.assertEqual(result.last_hidden_state.shape							,				(self.batch_size, self.seq_length, self.hidden_size)   )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				*A__   )     ->      Dict:
          _SCREAMING_SNAKE_CASE  			=					OpenAIGPTLMHeadModel(_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()

          _SCREAMING_SNAKE_CASE  			=					model(_lowerCAmelCase							,				token_type_ids=_lowerCAmelCase							,				labels=_lowerCAmelCase   )
          self.parent.assertEqual(result.loss.shape							,				()   )
          self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.seq_length, self.vocab_size)   )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				*A__   )     ->      Optional[int]:
          _SCREAMING_SNAKE_CASE  			=					OpenAIGPTDoubleHeadsModel(_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()

          _SCREAMING_SNAKE_CASE  			=					model(_lowerCAmelCase							,				token_type_ids=_lowerCAmelCase							,				labels=_lowerCAmelCase   )
          self.parent.assertEqual(result.loss.shape							,				()   )
          self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.seq_length, self.vocab_size)   )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				*A__   )     ->      int:
          _SCREAMING_SNAKE_CASE  			=					self.num_labels
          _SCREAMING_SNAKE_CASE  			=					OpenAIGPTForSequenceClassification(_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()

          _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.batch_size]							,				self.type_sequence_label_size   )
          _SCREAMING_SNAKE_CASE  			=					model(_lowerCAmelCase							,				token_type_ids=_lowerCAmelCase							,				labels=_lowerCAmelCase   )
          self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_labels)   )






       def 							UpperCamelCase					(				self   )     ->      int:
          _SCREAMING_SNAKE_CASE  			=					self.prepare_config_and_inputs()
          (
              (
              _SCREAMING_SNAKE_CASE					
          )			,				(
              _SCREAMING_SNAKE_CASE					
          )			,				(
              _SCREAMING_SNAKE_CASE					
          )			,				(
              _SCREAMING_SNAKE_CASE					
          )			,				(
              _SCREAMING_SNAKE_CASE					
          )			,				(
              _SCREAMING_SNAKE_CASE					
          )			,				(
              _SCREAMING_SNAKE_CASE					
          )			,									
          )  			=					config_and_inputs
          _SCREAMING_SNAKE_CASE  			=					{
              """input_ids""": input_ids,
              """token_type_ids""": token_type_ids,
              """head_mask""": head_mask,
          }

          return config, inputs_dict






@require_torch
class  _a					(_SCREAMING_SNAKE_CASE	,       _SCREAMING_SNAKE_CASE	,       _SCREAMING_SNAKE_CASE	,       unittest.TestCase):





       """simple docstring"""




       SCREAMING_SNAKE_CASE   =	(
           (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE   =	(
           (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
       )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
       SCREAMING_SNAKE_CASE   =	(
           {
               'feature-extraction': OpenAIGPTModel,
               'text-classification': OpenAIGPTForSequenceClassification,
               'text-generation': OpenAIGPTLMHeadModel,
               'zero-shot': OpenAIGPTForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      Dict:
          if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
             # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
             # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
             # tiny config could not be created.
             return True

          return False






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__=False   )     ->      Optional[int]:
          _SCREAMING_SNAKE_CASE  			=					super()._prepare_for_class(_lowerCAmelCase							,				_lowerCAmelCase							,				return_labels=_lowerCAmelCase   )

          if return_labels:
             if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                _SCREAMING_SNAKE_CASE  			=					torch.zeros(
                    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)							,				dtype=torch.long							,				device=_lowerCAmelCase							,				)
                _SCREAMING_SNAKE_CASE  			=					inputs_dict["""labels"""]
                _SCREAMING_SNAKE_CASE  			=					inputs_dict["""labels"""]
                _SCREAMING_SNAKE_CASE  			=					torch.zeros(
                    (self.model_tester.batch_size, self.model_tester.num_choices)							,				dtype=torch.long							,				device=_lowerCAmelCase							,				)
                _SCREAMING_SNAKE_CASE  			=					torch.zeros(
                    self.model_tester.batch_size							,				dtype=torch.long							,				device=_lowerCAmelCase   )
          return inputs_dict






       def 							UpperCamelCase					(				self   )     ->      str:
          _SCREAMING_SNAKE_CASE  			=					OpenAIGPTModelTester(self   )
          _SCREAMING_SNAKE_CASE  			=					ConfigTester(self							,				config_class=_lowerCAmelCase							,				n_embd=37   )






       def 							UpperCamelCase					(				self   )     ->      int:
          self.config_tester.run_common_tests()






       def 							UpperCamelCase					(				self   )     ->      Optional[int]:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*_lowerCAmelCase   )






       def 							UpperCamelCase					(				self   )     ->      Optional[Any]:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase   )






       def 							UpperCamelCase					(				self   )     ->      Union[str, Any]:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_double_lm_head_model(*_lowerCAmelCase   )






       def 							UpperCamelCase					(				self   )     ->      Optional[Any]:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCAmelCase   )






       @slow
       def 							UpperCamelCase					(				self   )     ->      Optional[Any]:
          for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _SCREAMING_SNAKE_CASE  			=					OpenAIGPTModel.from_pretrained(_lowerCAmelCase   )
             self.assertIsNotNone(_lowerCAmelCase   )






@require_torch
class  _a					(unittest.TestCase):





       """simple docstring"""




       @slow
       def 							UpperCamelCase					(				self   )     ->      int:
          _SCREAMING_SNAKE_CASE  			=					OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""   )
          model.to(_lowerCAmelCase   )
          _SCREAMING_SNAKE_CASE  			=					torch.tensor([[4_81, 47_35, 5_44]]							,				dtype=torch.long							,				device=_lowerCAmelCase   )  # the president is
          _SCREAMING_SNAKE_CASE  			=					[
              4_81,
              47_35,
              5_44,
              2_46,
              9_63,
              8_70,
              7_62,
              2_39,
              2_44,
              4_04_77,
              2_44,
              2_49,
              7_19,
              8_81,
              4_87,
              5_44,
              2_40,
              2_44,
              6_03,
              4_81,
          ]  # the president is a very good man. " \n " i\'m sure he is, " said the

          _SCREAMING_SNAKE_CASE  			=					model.generate(_lowerCAmelCase							,				do_sample=_lowerCAmelCase   )
          self.assertListEqual(output_ids[0].tolist()							,				_lowerCAmelCase   )












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


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

lowerCamelCase__					:  Tuple      							=				{
    'microsoft/swinv2-tiny-patch4-window8-256': (
        'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json'
    ),
}





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       "swinv2"

 lowercase_							       =       {
     "num_attention_heads": "num_heads",
     "num_hidden_layers": "num_layers",
 }



 def __init__(				self    :						Dict							,							_lowerCAmelCase    :						Optional[Any]=224							,							_lowerCAmelCase    :						Optional[int]=4							,							_lowerCAmelCase    :						Tuple=3							,							_lowerCAmelCase    :						Tuple=96							,							_lowerCAmelCase    :						Dict=[2, 2, 6, 2]							,							_lowerCAmelCase    :						Optional[Any]=[3, 6, 12, 24]							,							_lowerCAmelCase    :						str=7							,							_lowerCAmelCase    :						List[Any]=4.0							,							_lowerCAmelCase    :						List[str]=True							,							_lowerCAmelCase    :						List[Any]=0.0							,							_lowerCAmelCase    :						List[Any]=0.0							,							_lowerCAmelCase    :						Any=0.1							,							_lowerCAmelCase    :						List[Any]="gelu"							,							_lowerCAmelCase    :						str=False							,							_lowerCAmelCase    :						str=0.02							,							_lowerCAmelCase    :						List[Any]=1E-5							,							_lowerCAmelCase    :						str=32							,							**_lowerCAmelCase    :						List[Any]							,							):
       super().__init__(**_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     image_size
       SCREAMING_SNAKE_CASE_      		=     patch_size
       SCREAMING_SNAKE_CASE_      		=     num_channels
       SCREAMING_SNAKE_CASE_      		=     embed_dim
       SCREAMING_SNAKE_CASE_      		=     depths
       SCREAMING_SNAKE_CASE_      		=     len(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     num_heads
       SCREAMING_SNAKE_CASE_      		=     window_size
       SCREAMING_SNAKE_CASE_      		=     mlp_ratio
       SCREAMING_SNAKE_CASE_      		=     qkv_bias
       SCREAMING_SNAKE_CASE_      		=     hidden_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     drop_path_rate
       SCREAMING_SNAKE_CASE_      		=     hidden_act
       SCREAMING_SNAKE_CASE_      		=     use_absolute_embeddings
       SCREAMING_SNAKE_CASE_      		=     layer_norm_eps
       SCREAMING_SNAKE_CASE_      		=     initializer_range
       SCREAMING_SNAKE_CASE_      		=     encoder_stride
       # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel
       # this indicates the channel dimension after the last stage of the model
       SCREAMING_SNAKE_CASE_      		=     int(embed_dim * 2 ** (len(_lowerCAmelCase		) - 1)		)
       SCREAMING_SNAKE_CASE_      		=     (0, 0, 0, 0)