





'''simple docstring'''

import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
      from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor


class 					a_     :

    '''simple docstring'''


    def __init__(		self ,		A ,		A=13 ,		A=32 ,		A=2 ,		A=3 ,		A=16 ,		A=[1, 2, 1] ,		A=[2, 2, 4] ,		A=2 ,		A=2.0 ,		A=True ,		A=0.0 ,		A=0.0 ,		A=0.1 ,		A="gelu" ,		A=False ,		A=True ,		A=0.02 ,		A=1e-5 ,		A=True ,		A=None ,		A=True ,		A=10 ,		A=8 ,		)		->   Optional[Any]:
        _SCREAMING_SNAKE_CASE									=			parent
        _SCREAMING_SNAKE_CASE									=			batch_size
        _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									=			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									=			patch_norm
        _SCREAMING_SNAKE_CASE									=			layer_norm_eps
        _SCREAMING_SNAKE_CASE									=			initializer_range
        _SCREAMING_SNAKE_CASE									=			is_training
        _SCREAMING_SNAKE_CASE									=			scope
        _SCREAMING_SNAKE_CASE									=			use_labels
        _SCREAMING_SNAKE_CASE									=			type_sequence_label_size
        _SCREAMING_SNAKE_CASE									=			encoder_stride


    def 			snake_case_(		self       )		->   Union[str, Any]:
        _SCREAMING_SNAKE_CASE									=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

        _SCREAMING_SNAKE_CASE									=			self.get_config()

        return config, pixel_values, labels


    def 			snake_case_(		self       )		->   str:
        return SwinvaConfig(
            image_size=self.image_size ,		patch_size=self.patch_size ,		num_channels=self.num_channels ,		embed_dim=self.embed_dim ,		depths=self.depths ,		num_heads=self.num_heads ,		window_size=self.window_size ,		mlp_ratio=self.mlp_ratio ,		qkv_bias=self.qkv_bias ,		hidden_dropout_prob=self.hidden_dropout_prob ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob ,		drop_path_rate=self.drop_path_rate ,		hidden_act=self.hidden_act ,		use_absolute_embeddings=self.use_absolute_embeddings ,		path_norm=self.patch_norm ,		layer_norm_eps=self.layer_norm_eps ,		initializer_range=self.initializer_range ,		encoder_stride=self.encoder_stride ,		)


    def 			snake_case_(		self ,		A ,		A ,		A       )		->   Dict:
        _SCREAMING_SNAKE_CASE									=			SwinvaModel(config=A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A       )

        _SCREAMING_SNAKE_CASE									=			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
        _SCREAMING_SNAKE_CASE									=			int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

        self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, expected_seq_len, expected_dim)       )


    def 			snake_case_(		self ,		A ,		A ,		A       )		->   Tuple:
        _SCREAMING_SNAKE_CASE									=			SwinvaForMaskedImageModeling(config=A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A       )
        self.parent.assertEqual(
            result.logits.shape ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

        # test greyscale images
        _SCREAMING_SNAKE_CASE									=			1
        _SCREAMING_SNAKE_CASE									=			SwinvaForMaskedImageModeling(A       )
        model.to(A       )
        model.eval()

        _SCREAMING_SNAKE_CASE									=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
        _SCREAMING_SNAKE_CASE									=			model(A       )
        self.parent.assertEqual(result.logits.shape ,		(self.batch_size, 1, self.image_size, self.image_size)       )


    def 			snake_case_(		self ,		A ,		A ,		A       )		->   Dict:
        _SCREAMING_SNAKE_CASE									=			self.type_sequence_label_size
        _SCREAMING_SNAKE_CASE									=			SwinvaForImageClassification(A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A ,		labels=A       )
        self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.type_sequence_label_size)       )





    def 			snake_case_(		self       )		->   List[str]:
        _SCREAMING_SNAKE_CASE									=			self.prepare_config_and_inputs()
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			config_and_inputs
        _SCREAMING_SNAKE_CASE									=			{"""pixel_values""": pixel_values}
        return config, inputs_dict




@require_torch
class 					a_     (				snake_case_      ,    snake_case_      ,    unittest.TestCase       ):

    '''simple docstring'''
    UpperCamelCase     = (
        (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
    )
    UpperCamelCase     = (
        {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
        if is_torch_available()
        else {}
    )

    UpperCamelCase     = False
    UpperCamelCase     = False
    UpperCamelCase     = False
    UpperCamelCase     = False


    def 			snake_case_(		self       )		->   Dict:
        _SCREAMING_SNAKE_CASE									=			SwinvaModelTester(self       )
        _SCREAMING_SNAKE_CASE									=			ConfigTester(self ,		config_class=A ,		embed_dim=37       )


    def 			snake_case_(		self       )		->   Optional[int]:
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()


    def 			snake_case_(		self       )		->   List[str]:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A       )


    @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""       )
    def 			snake_case_(		self       )		->   Optional[Any]:
        pass


    @unittest.skip(reason="""Swinv2 does not use inputs_embeds"""       )
    def 			snake_case_(		self       )		->   int:
        pass


    def 			snake_case_(		self       )		->   List[Any]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE									=			model_class(A       )
            self.assertIsInstance(model.get_input_embeddings() ,		(nn.Module)       )
            _SCREAMING_SNAKE_CASE									=			model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(A ,		nn.Linear       )       )


    def 			snake_case_(		self       )		->   List[str]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE									=			model_class(A       )
            _SCREAMING_SNAKE_CASE									=			inspect.signature(model.forward       )
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            _SCREAMING_SNAKE_CASE									=			[*signature.parameters.keys()]

            _SCREAMING_SNAKE_CASE									=			["""pixel_values"""]
            self.assertListEqual(arg_names[:1] ,		A       )


    def 			snake_case_(		self       )		->   int:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()
        _SCREAMING_SNAKE_CASE									=			True

        for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE									=			True
            _SCREAMING_SNAKE_CASE									=			False
            _SCREAMING_SNAKE_CASE									=			True
            _SCREAMING_SNAKE_CASE									=			model_class(A       )
            model.to(A       )
            model.eval()
            with torch.no_grad():
                _SCREAMING_SNAKE_CASE									=			model(**self._prepare_for_class(A ,		A       )       )
            _SCREAMING_SNAKE_CASE									=			outputs.attentions
            _SCREAMING_SNAKE_CASE									=			len(self.model_tester.depths       )
            self.assertEqual(len(A       ) ,		A       )

            # check that output_attentions also work using config
            del inputs_dict["output_attentions"]
            _SCREAMING_SNAKE_CASE									=			True
            _SCREAMING_SNAKE_CASE									=			config.window_size**2
            _SCREAMING_SNAKE_CASE									=			model_class(A       )
            model.to(A       )
            model.eval()
            with torch.no_grad():
                _SCREAMING_SNAKE_CASE									=			model(**self._prepare_for_class(A ,		A       )       )
            _SCREAMING_SNAKE_CASE									=			outputs.attentions
            self.assertEqual(len(A       ) ,		A       )

            self.assertListEqual(
                list(attentions[0].shape[-3:]       ) ,		[self.model_tester.num_heads[0], window_size_squared, window_size_squared] ,		)
            _SCREAMING_SNAKE_CASE									=			len(A       )

            # Check attention is always last and order is fine
            _SCREAMING_SNAKE_CASE									=			True
            _SCREAMING_SNAKE_CASE									=			True
            _SCREAMING_SNAKE_CASE									=			model_class(A       )
            model.to(A       )
            model.eval()
            with torch.no_grad():
                _SCREAMING_SNAKE_CASE									=			model(**self._prepare_for_class(A ,		A       )       )

            if hasattr(self.model_tester ,		"""num_hidden_states_types"""       ):
                _SCREAMING_SNAKE_CASE									=			self.model_tester.num_hidden_states_types
            else:
                # also another +1 for reshaped_hidden_states
                _SCREAMING_SNAKE_CASE									=			2
            self.assertEqual(out_len + added_hidden_states ,		len(A       )       )

            _SCREAMING_SNAKE_CASE									=			outputs.attentions

            self.assertEqual(len(A       ) ,		A       )

            self.assertListEqual(
                list(self_attentions[0].shape[-3:]       ) ,		[self.model_tester.num_heads[0], window_size_squared, window_size_squared] ,		)


    def 			snake_case_(		self ,		A ,		A ,		A ,		A       )		->   Dict:
        _SCREAMING_SNAKE_CASE									=			model_class(A       )
        model.to(A       )
        model.eval()

        with torch.no_grad():
            _SCREAMING_SNAKE_CASE									=			model(**self._prepare_for_class(A ,		A       )       )

        _SCREAMING_SNAKE_CASE									=			outputs.hidden_states

        _SCREAMING_SNAKE_CASE									=			getattr(
            self.model_tester ,		"""expected_num_hidden_layers""" ,		len(self.model_tester.depths       ) + 1       )
        self.assertEqual(len(A       ) ,		A       )

        # Swinv2 has a different seq_length
        _SCREAMING_SNAKE_CASE									=			(
            config.patch_size
            if isinstance(config.patch_size ,		collections.abc.Iterable       )
            else (config.patch_size, config.patch_size)
        )

        _SCREAMING_SNAKE_CASE									=			(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

        self.assertListEqual(
            list(hidden_states[0].shape[-2:]       ) ,		[num_patches, self.model_tester.embed_dim] ,		)

        _SCREAMING_SNAKE_CASE									=			outputs.reshaped_hidden_states
        self.assertEqual(len(A       ) ,		A       )

        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			reshaped_hidden_states[0].shape
        _SCREAMING_SNAKE_CASE									=			(
            reshaped_hidden_states[0].view(A ,		A ,		height * width       ).permute(0 ,		2 ,		1       )
        )
        self.assertListEqual(
            list(reshaped_hidden_states.shape[-2:]       ) ,		[num_patches, self.model_tester.embed_dim] ,		)


    def 			snake_case_(		self       )		->   Optional[int]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

        _SCREAMING_SNAKE_CASE									=			(
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size ,		collections.abc.Iterable       )
            else (self.model_tester.image_size, self.model_tester.image_size)
        )

        for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE									=			True
            self.check_hidden_states_output(A ,		A ,		A ,		A       )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            _SCREAMING_SNAKE_CASE									=			True

            self.check_hidden_states_output(A ,		A ,		A ,		A       )


    def 			snake_case_(		self       )		->   Union[str, Any]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()
        _SCREAMING_SNAKE_CASE									=			3

        _SCREAMING_SNAKE_CASE									=			(
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size ,		collections.abc.Iterable       )
            else (self.model_tester.image_size, self.model_tester.image_size)
        )
        _SCREAMING_SNAKE_CASE									=			(
            config.patch_size
            if isinstance(config.patch_size ,		collections.abc.Iterable       )
            else (config.patch_size, config.patch_size)
        )

        _SCREAMING_SNAKE_CASE									=			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
        _SCREAMING_SNAKE_CASE									=			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

        for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE									=			True
            self.check_hidden_states_output(A ,		A ,		A ,		(padded_height, padded_width)       )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            _SCREAMING_SNAKE_CASE									=			True
            self.check_hidden_states_output(A ,		A ,		A ,		(padded_height, padded_width)       )


    def 			snake_case_(		self       )		->   Union[str, Any]:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*A       )


    def 			snake_case_(		self       )		->   Any:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*A       )


    @slow
    def 			snake_case_(		self       )		->   List[str]:
        for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE									=			SwinvaModel.from_pretrained(A       )
            self.assertIsNotNone(A       )





    def 			snake_case_(		self       )		->   List[Any]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

        _SCREAMING_SNAKE_CASE									=			_config_zero_init(A       )
        for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE									=			model_class(config=A       )
            for name, param in model.named_parameters():
                if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
                    self.assertIn(
                        ((param.data.mean() * 1e9).round() / 1e9).item() ,		[0.0, 1.0] ,		msg=f'Parameter {name} of model {model_class} seems not properly initialized' ,		)




@require_vision
@require_torch
class 					a_     (				unittest.TestCase       ):

    '''simple docstring'''


    @cached_property
    def 			snake_case_(		self       )		->   Optional[Any]:
        return (
            AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""       )
            if is_vision_available()
            else None
        )





    @slow
    def 			snake_case_(		self       )		->   Tuple:
        _SCREAMING_SNAKE_CASE									=			SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""       ).to(
            A       )
        _SCREAMING_SNAKE_CASE									=			self.default_image_processor

        _SCREAMING_SNAKE_CASE									=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
        _SCREAMING_SNAKE_CASE									=			image_processor(images=A ,		return_tensors="""pt"""       ).to(A       )

        # forward pass
        with torch.no_grad():
            _SCREAMING_SNAKE_CASE									=			model(**A       )

        # verify the logits
        _SCREAMING_SNAKE_CASE									=			torch.Size((1, 1000)       )
        self.assertEqual(outputs.logits.shape ,		A       )
        _SCREAMING_SNAKE_CASE									=			torch.tensor([-0.3947, -0.4306, 0.0026]       ).to(A       )
        self.assertTrue(torch.allclose(outputs.logits[0, :3] ,		A ,		atol=1e-4       )       )







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






				'''simple docstring'''



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


				def 		__magic_name__(       self			:Optional[Any]	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size	)

										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		None
										if self.use_token_type_ids:
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		ids_tensor([self.batch_size, self.seq_length]			,			self.type_vocab_size	)

										__SCREAMING_SNAKE_CASE						:     Dict					=		None
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		None
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		ids_tensor([self.batch_size]			,			self.type_sequence_label_size	)
																__SCREAMING_SNAKE_CASE						:     Tuple					=		ids_tensor([self.batch_size, self.seq_length]			,			self.num_labels	)
																__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		ids_tensor([self.batch_size]			,			self.num_choices	)

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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						:     Any					=		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 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:Any			,			*lowerCAmelCase__			:Union[str, Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		OpenAIGPTModel(config=lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Dict					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			head_mask=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		model(lowerCAmelCase__	)

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


				def 		__magic_name__(       self			:Optional[Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Any			,			lowerCAmelCase__			:Dict			,			*lowerCAmelCase__			:List[Any]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		OpenAIGPTLMHeadModel(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Tuple					=		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 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:List[str]			,			*lowerCAmelCase__			:Optional[Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		OpenAIGPTDoubleHeadsModel(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Any					=		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 		__magic_name__(       self			:Dict			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:str			,			*lowerCAmelCase__			:Optional[int]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.num_labels
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		OpenAIGPTForSequenceClassification(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     List[str]					=		ids_tensor([self.batch_size]			,			self.type_sequence_label_size	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			labels=lowerCAmelCase__	)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)	)



				def 		__magic_name__(       self			:Optional[Any]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     str					=		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
										)			,			
										)						:     List[str]					=		config_and_inputs
										__SCREAMING_SNAKE_CASE						:     List[str]					=		{
										    '''input_ids''': input_ids,
										    '''token_type_ids''': token_type_ids,
										    '''head_mask''': head_mask,
										}

										return config, inputs_dict




@require_torch
class        _lowercase      (			A__						,			A__						,			A__						,			unittest.TestCase       ):






				'''simple docstring'''



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


				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:Union[str, Any]	)		->			Tuple:
										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 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int=False	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		super()._prepare_for_class(lowerCAmelCase__			,			lowerCAmelCase__			,			return_labels=lowerCAmelCase__	)

										if return_labels:
																if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																						__SCREAMING_SNAKE_CASE						:     Any					=		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						:     Tuple					=		inputs_dict['''labels''']
																						__SCREAMING_SNAKE_CASE						:     Dict					=		inputs_dict['''labels''']
																						__SCREAMING_SNAKE_CASE						:     List[Any]					=		torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.num_choices)			,			dtype=torch.long			,			device=lowerCAmelCase__			,			)
																						__SCREAMING_SNAKE_CASE						:     Optional[int]					=		torch.zeros(
																						    self.model_tester.batch_size			,			dtype=torch.long			,			device=lowerCAmelCase__	)
										return inputs_dict


				def 		__magic_name__(       self			:Optional[int]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     int					=		OpenAIGPTModelTester(self	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		ConfigTester(self			,			config_class=lowerCAmelCase__			,			n_embd=37	)


				def 		__magic_name__(       self			:Any	)		->			Optional[Any]:
										self.config_tester.run_common_tests()


				def 		__magic_name__(       self			:List[str]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:int	)		->			int:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[str]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[str]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__	)



				@slow
				def 		__magic_name__(       self			:Any	)		->			List[Any]:
										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE						:     Dict					=		OpenAIGPTModel.from_pretrained(lowerCAmelCase__	)
																self.assertIsNotNone(lowerCAmelCase__	)




@require_torch
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				@slow
				def 		__magic_name__(       self			:Union[str, Any]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''	)
										model.to(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		torch.tensor([[481, 4_735, 544]]			,			dtype=torch.long			,			device=lowerCAmelCase__	)  # the president is
										__SCREAMING_SNAKE_CASE						:     Dict					=		[
										    481,
										    4_735,
										    544,
										    246,
										    963,
										    870,
										    762,
										    239,
										    244,
										    40_477,
										    244,
										    249,
										    719,
										    881,
										    487,
										    544,
										    240,
										    244,
										    603,
										    481,
										]  # the president is a very good man. " \n " i\'m sure he is, " said the

										__SCREAMING_SNAKE_CASE						:     Dict					=		model.generate(lowerCAmelCase__			,			do_sample=lowerCAmelCase__	)
										self.assertListEqual(output_ids[0].tolist()			,			lowerCAmelCase__	)
