"""simple docstring"""
import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

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


if is_torch_available():
  import torch
  from torch import nn

  from transformers import MaskFormerSwinBackbone
  from transformers.models.maskformer import MaskFormerSwinModel



class      _UpperCAmelCase						:






    """simple docstring"""







    def __init__(			self    , _lowercase    , _lowercase=13    , _lowercase=32    , _lowercase=2    , _lowercase=3    , _lowercase=16    , _lowercase=[1, 2, 1]    , _lowercase=[2, 2, 4]    , _lowercase=2    , _lowercase=2.0    , _lowercase=True    , _lowercase=0.0    , _lowercase=0.0    , _lowercase=0.1    , _lowercase="gelu"    , _lowercase=False    , _lowercase=True    , _lowercase=0.02    , _lowercase=1E-5    , _lowercase=True    , _lowercase=None    , _lowercase=True    , _lowercase=10    , _lowercase=8    , _lowercase=["stage1", "stage2", "stage3"]    , _lowercase=[1, 2, 3]    , )	->	List[Any]:
        _lowerCamelCase	: int       =       parent
        _lowerCamelCase	: Optional[Any]       =       batch_size
        _lowerCamelCase	: Any       =       image_size
        _lowerCamelCase	: List[Any]       =       patch_size
        _lowerCamelCase	: Tuple       =       num_channels
        _lowerCamelCase	: str       =       embed_dim
        _lowerCamelCase	: List[str]       =       depths
        _lowerCamelCase	: int       =       num_heads
        _lowerCamelCase	: Optional[int]       =       window_size
        _lowerCamelCase	: Union[str, Any]       =       mlp_ratio
        _lowerCamelCase	: List[str]       =       qkv_bias
        _lowerCamelCase	: Union[str, Any]       =       hidden_dropout_prob
        _lowerCamelCase	: List[str]       =       attention_probs_dropout_prob
        _lowerCamelCase	: Optional[int]       =       drop_path_rate
        _lowerCamelCase	: int       =       hidden_act
        _lowerCamelCase	: str       =       use_absolute_embeddings
        _lowerCamelCase	: Tuple       =       patch_norm
        _lowerCamelCase	: Dict       =       layer_norm_eps
        _lowerCamelCase	: Tuple       =       initializer_range
        _lowerCamelCase	: Tuple       =       is_training
        _lowerCamelCase	: List[Any]       =       scope
        _lowerCamelCase	: str       =       use_labels
        _lowerCamelCase	: Any       =       type_sequence_label_size
        _lowerCamelCase	: Optional[Any]       =       encoder_stride
        _lowerCamelCase	: Dict       =       out_features
        _lowerCamelCase	: List[Any]       =       out_indices







    def 				a__					(			self					)	->	Optional[int]:
        _lowerCamelCase	: Optional[Any]       =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

        _lowerCamelCase	: Optional[Any]       =       None
        if self.use_labels:
            _lowerCamelCase	: Dict       =       ids_tensor([self.batch_size]    , self.type_sequence_label_size					)

        _lowerCamelCase	: Optional[int]       =       self.get_config()

        return config, pixel_values, labels







    def 				a__					(			self					)	->	Optional[int]:
        return MaskFormerSwinConfig(
            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    , out_features=self.out_features    , out_indices=self.out_indices    , )







    def 				a__					(			self    , _lowercase    , _lowercase    , _lowercase					)	->	Union[str, Any]:
        _lowerCamelCase	: Any       =       MaskFormerSwinModel(config=__lowerCAmelCase					)
        model.to(__lowerCAmelCase					)
        model.eval()
        _lowerCamelCase	: Tuple       =       model(__lowerCAmelCase					)

        _lowerCamelCase	: int       =       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths					) - 1))
        _lowerCamelCase	: Any       =       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 				a__					(			self    , _lowercase    , _lowercase    , _lowercase					)	->	Dict:
        _lowerCamelCase	: Dict       =       MaskFormerSwinBackbone(config=__lowerCAmelCase					)
        model.to(__lowerCAmelCase					)
        model.eval()
        _lowerCamelCase	: Dict       =       model(__lowerCAmelCase					)

        # verify feature maps
        self.parent.assertEqual(len(result.feature_maps					)    , len(config.out_features					)					)
        self.parent.assertListEqual(list(result.feature_maps[0].shape					)    , [13, 16, 16, 16]					)

        # verify channels
        self.parent.assertEqual(len(model.channels					)    , len(config.out_features					)					)
        self.parent.assertListEqual(model.channels    , [16, 32, 64]					)

        # verify ValueError
        with self.parent.assertRaises(__lowerCAmelCase					):
            _lowerCamelCase	: Union[str, Any]       =       ['''stem''']
            _lowerCamelCase	: Optional[Any]       =       MaskFormerSwinBackbone(config=__lowerCAmelCase					)







    def 				a__					(			self					)	->	str:
        _lowerCamelCase	: List[str]       =       self.prepare_config_and_inputs()
        _lowerCamelCase,					_lowerCamelCase,					_lowerCamelCase	: Optional[Any]       =       config_and_inputs
        _lowerCamelCase	: Optional[Any]       =       {'''pixel_values''': pixel_values}
        return config, inputs_dict





@require_torch
class      _UpperCAmelCase						( UpperCAmelCase__ ,			UpperCAmelCase__ ,			unittest.TestCase						):






    """simple docstring"""
    __snake_case    =       (
        (
            MaskFormerSwinModel,
            MaskFormerSwinBackbone,
        )
        if is_torch_available()
        else ()
    )
    __snake_case    =       {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {}
    __snake_case    =       False
    __snake_case    =       False
    __snake_case    =       False
    __snake_case    =       False
    __snake_case    =       False







    def 				a__					(			self					)	->	int:
        _lowerCamelCase	: int       =       MaskFormerSwinModelTester(self					)
        _lowerCamelCase	: Tuple       =       ConfigTester(self    , config_class=__lowerCAmelCase    , embed_dim=37					)







    @require_torch_multi_gpu
    @unittest.skip(
        reason=(
            '''`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with'''
            ''' `nn.DataParallel`'''
        )					)
    def 				a__					(			self					)	->	List[Any]:
        pass







    def 				a__					(			self					)	->	Optional[Any]:
        self.create_and_test_config_common_properties()
        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 				a__					(			self					)	->	List[str]:
        return







    def 				a__					(			self					)	->	Optional[Any]:
        _lowerCamelCase	: List[str]       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowerCAmelCase					)







    def 				a__					(			self					)	->	Optional[Any]:
        _lowerCamelCase	: Optional[Any]       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*__lowerCAmelCase					)







    @unittest.skip('''Swin does not use inputs_embeds'''					)
    def 				a__					(			self					)	->	Any:
        pass







    @unittest.skip('''Swin does not support feedforward chunking'''					)
    def 				a__					(			self					)	->	Union[str, Any]:
        pass







    def 				a__					(			self					)	->	Tuple:
        _lowerCamelCase,					_lowerCamelCase	: Optional[Any]       =       self.model_tester.prepare_config_and_inputs_for_common()

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







    def 				a__					(			self					)	->	Optional[Any]:
        _lowerCamelCase,					_lowerCamelCase	: int       =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            _lowerCamelCase	: int       =       model_class(__lowerCAmelCase					)
            _lowerCamelCase	: List[str]       =       inspect.signature(model.forward					)
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            _lowerCamelCase	: Dict       =       [*signature.parameters.keys()]

            _lowerCamelCase	: Tuple       =       ['''pixel_values''']
            self.assertListEqual(arg_names[:1]    , __lowerCAmelCase					)







    @unittest.skip(reason='''MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'''					)
    def 				a__					(			self					)	->	Optional[Any]:
        pass







    @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone'''					)
    def 				a__					(			self					)	->	Union[str, Any]:
        pass







    def 				a__					(			self    , _lowercase    , _lowercase    , _lowercase    , _lowercase					)	->	Any:
        _lowerCamelCase	: str       =       model_class(__lowerCAmelCase					)
        model.to(__lowerCAmelCase					)
        model.eval()

        with torch.no_grad():
            _lowerCamelCase	: Union[str, Any]       =       model(**self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase					)					)

        _lowerCamelCase	: List[Any]       =       outputs.hidden_states

        _lowerCamelCase	: Optional[int]       =       getattr(
            self.model_tester    , '''expected_num_hidden_layers'''    , len(self.model_tester.depths					) + 1					)
        self.assertEqual(len(__lowerCAmelCase					)    , __lowerCAmelCase					)

        # Swin has a different seq_length
        _lowerCamelCase	: Union[str, Any]       =       (
            config.patch_size
            if isinstance(config.patch_size    , collections.abc.Iterable					)
            else (config.patch_size, config.patch_size)
        )

        _lowerCamelCase	: Any       =       (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]    , )







    def 				a__					(			self					)	->	Tuple:
        _lowerCamelCase,					_lowerCamelCase	: Tuple       =       self.model_tester.prepare_config_and_inputs_for_common()

        _lowerCamelCase	: Optional[int]       =       (
            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:
            _lowerCamelCase	: Optional[Any]       =       True
            self.check_hidden_states_output(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase					)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            _lowerCamelCase	: int       =       True

            self.check_hidden_states_output(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase					)







    def 				a__					(			self					)	->	Optional[Any]:
        _lowerCamelCase,					_lowerCamelCase	: Union[str, Any]       =       self.model_tester.prepare_config_and_inputs_for_common()
        _lowerCamelCase	: List[str]       =       3

        _lowerCamelCase	: Tuple       =       (
            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)
        )
        _lowerCamelCase	: Union[str, Any]       =       (
            config.patch_size
            if isinstance(config.patch_size    , collections.abc.Iterable					)
            else (config.patch_size, config.patch_size)
        )

        _lowerCamelCase	: Any       =       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
        _lowerCamelCase	: str       =       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

        for model_class in self.all_model_classes:
            _lowerCamelCase	: Tuple       =       True
            self.check_hidden_states_output(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase    , (padded_height, padded_width)					)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            _lowerCamelCase	: Optional[Any]       =       True
            self.check_hidden_states_output(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase    , (padded_height, padded_width)					)







    @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints'''					)
    def 				a__					(			self					)	->	List[str]:
        pass







    @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''					)
    def 				a__					(			self					)	->	int:
        pass







    @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''					)
    def 				a__					(			self					)	->	Tuple:
        pass







    def 				a__					(			self					)	->	Optional[int]:
        _lowerCamelCase,					_lowerCamelCase	: Optional[Any]       =       self.model_tester.prepare_config_and_inputs_for_common()

        def set_nan_tensor_to_zero(_lowercase					):
            _lowerCamelCase	: Tuple       =       0
            return t

        def check_equivalence(_lowercase    , _lowercase    , _lowercase    , _lowercase={}					):
            with torch.no_grad():
                _lowerCamelCase	: List[str]       =       model(**__lowerCAmelCase    , return_dict=__lowerCAmelCase    , **__lowerCAmelCase					)
                _lowerCamelCase	: Dict       =       model(**__lowerCAmelCase    , return_dict=__lowerCAmelCase    , **__lowerCAmelCase					).to_tuple()

                def recursive_check(_lowercase    , _lowercase					):
                    if isinstance(__lowerCAmelCase    , (List, Tuple)					):
                        for tuple_iterable_value, dict_iterable_value in zip(__lowerCAmelCase    , __lowerCAmelCase					):
                            recursive_check(__lowerCAmelCase    , __lowerCAmelCase					)
                    elif isinstance(__lowerCAmelCase    , __lowerCAmelCase					):
                        for tuple_iterable_value, dict_iterable_value in zip(
                            tuple_object.values()    , dict_object.values()					):
                            recursive_check(__lowerCAmelCase    , __lowerCAmelCase					)
                    elif tuple_object is None:
                        return
                    else:
                        self.assertTrue(
                            torch.allclose(
                                set_nan_tensor_to_zero(__lowerCAmelCase					)    , set_nan_tensor_to_zero(__lowerCAmelCase					)    , atol=1E-5					)    , msg=(
                                '''Tuple and dict output are not equal. Difference:'''
                                F''' {torch.max(torch.abs(tuple_object - dict_object					)					)}. Tuple has `nan`:'''
                                F''' {torch.isnan(__lowerCAmelCase					).any()} and `inf`: {torch.isinf(__lowerCAmelCase					)}. Dict has'''
                                F''' `nan`: {torch.isnan(__lowerCAmelCase					).any()} and `inf`: {torch.isinf(__lowerCAmelCase					)}.'''
                            )    , )

                recursive_check(__lowerCAmelCase    , __lowerCAmelCase					)

        for model_class in self.all_model_classes:
            _lowerCamelCase	: Any       =       model_class(__lowerCAmelCase					)
            model.to(__lowerCAmelCase					)
            model.eval()

            _lowerCamelCase	: int       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase					)
            _lowerCamelCase	: Tuple       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase					)
            check_equivalence(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase					)

            _lowerCamelCase	: int       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase    , return_labels=__lowerCAmelCase					)
            _lowerCamelCase	: List[str]       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase    , return_labels=__lowerCAmelCase					)
            check_equivalence(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase					)

            _lowerCamelCase	: List[Any]       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase					)
            _lowerCamelCase	: Tuple       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase					)
            check_equivalence(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase    , {'''output_hidden_states''': True}					)

            _lowerCamelCase	: str       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase    , return_labels=__lowerCAmelCase					)
            _lowerCamelCase	: Any       =       self._prepare_for_class(__lowerCAmelCase    , __lowerCAmelCase    , return_labels=__lowerCAmelCase					)
            check_equivalence(__lowerCAmelCase    , __lowerCAmelCase    , __lowerCAmelCase    , {'''output_hidden_states''': True}					)





@require_torch
class      _UpperCAmelCase						( unittest.TestCase ,			UpperCAmelCase__						):






    """simple docstring"""
    __snake_case    =       (MaskFormerSwinBackbone,) if is_torch_available() else ()
    __snake_case    =       MaskFormerSwinConfig







    def 				a__					(			self					)	->	str:
        _lowerCamelCase	: Dict       =       MaskFormerSwinModelTester(self					)







    def 				a__					(			self					)	->	int:
        _lowerCamelCase,					_lowerCamelCase	: int       =       self.model_tester.prepare_config_and_inputs_for_common()
        _lowerCamelCase	: int       =       inputs_dict['''pixel_values'''].shape[0]

        for backbone_class in self.all_model_classes:
            _lowerCamelCase	: Union[str, Any]       =       backbone_class(__lowerCAmelCase					)
            backbone.to(__lowerCAmelCase					)
            backbone.eval()

            _lowerCamelCase	: Tuple       =       backbone(**__lowerCAmelCase					)

            # Test default outputs and verify feature maps
            self.assertIsInstance(outputs.feature_maps    , __lowerCAmelCase					)
            self.assertTrue(len(outputs.feature_maps					) == len(backbone.channels					)					)
            for feature_map, n_channels in zip(outputs.feature_maps    , backbone.channels					):
                self.assertTrue(feature_map.shape[:2]    , (batch_size, n_channels)					)
            self.assertIsNone(outputs.hidden_states					)
            self.assertIsNone(outputs.attentions					)

            # Test output_hidden_states=True
            _lowerCamelCase	: int       =       backbone(**__lowerCAmelCase    , output_hidden_states=__lowerCAmelCase					)
            self.assertIsNotNone(outputs.hidden_states					)
            self.assertTrue(len(outputs.hidden_states					)    , len(backbone.stage_names					)					)
            # We skip the stem layer
            for hidden_states, n_channels in zip(outputs.hidden_states[1:]    , backbone.channels					):
                for hidden_state in hidden_states:
                    # Hidden states are in the format (batch_size, (height * width), n_channels)
                    _lowerCamelCase,					_lowerCamelCase,					_lowerCamelCase	: int       =       hidden_state.shape
                    self.assertTrue((h_batch_size, h_n_channels)    , (batch_size, n_channels)					)

            # Test output_attentions=True
            if self.has_attentions:
                _lowerCamelCase	: Optional[int]       =       backbone(**__lowerCAmelCase    , output_attentions=__lowerCAmelCase					)
                self.assertIsNotNone(outputs.attentions					)








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




	def __init__(       self     :  Optional[Any]       ,		__lowerCAmelCase     :  Optional[Any]       ,		__lowerCAmelCase     :  Optional[int]=1_3       ,		__lowerCAmelCase     :  Tuple=7       ,		__lowerCAmelCase     :  Optional[int]=True       ,		__lowerCAmelCase     :  Any=True       ,		__lowerCAmelCase     :  List[Any]=True       ,		__lowerCAmelCase     :  Union[str, Any]=9_9       ,		__lowerCAmelCase     :  Any=3_2       ,		__lowerCAmelCase     :  Tuple=5       ,		__lowerCAmelCase     :  int=4       ,		__lowerCAmelCase     :  List[str]=3_7       ,		__lowerCAmelCase     :  List[Any]="gelu"       ,		__lowerCAmelCase     :  Union[str, Any]=0.1       ,		__lowerCAmelCase     :  int=0.1       ,		__lowerCAmelCase     :  Any=5_1_2       ,		__lowerCAmelCase     :  Dict=1_6       ,		__lowerCAmelCase     :  Dict=2       ,		__lowerCAmelCase     :  Union[str, Any]=0.02       ,		__lowerCAmelCase     :  Optional[Any]=3       ,		__lowerCAmelCase     :  str=4       ,		__lowerCAmelCase     :  List[str]=None       ,		):
							__snake_case						 =   parent
							__snake_case						 =   batch_size
							__snake_case						 =   seq_length
							__snake_case						 =   is_training
							__snake_case						 =   use_token_type_ids
							__snake_case						 =   use_labels
							__snake_case						 =   vocab_size
							__snake_case						 =   hidden_size
							__snake_case						 =   num_hidden_layers
							__snake_case						 =   num_attention_heads
							__snake_case						 =   intermediate_size
							__snake_case						 =   hidden_act
							__snake_case						 =   hidden_dropout_prob
							__snake_case						 =   attention_probs_dropout_prob
							__snake_case						 =   max_position_embeddings
							__snake_case						 =   type_vocab_size
							__snake_case						 =   type_sequence_label_size
							__snake_case						 =   initializer_range
							__snake_case						 =   num_labels
							__snake_case						 =   num_choices
							__snake_case						 =   scope
							__snake_case						 =   self.vocab_size - 1




	def   lowercase__   (       self     :  Union[str, Any]      ):
							__snake_case						 =   ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size      )

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

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

							__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       ,		)

							__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   lowercase__   (       self     :  List[str]       ,		__lowerCAmelCase     :  Tuple       ,		__lowerCAmelCase     :  Optional[Any]       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  Union[str, Any]       ,		*__lowerCAmelCase     :  List[str]      ):
							__snake_case						 =   OpenAIGPTModel(config=__lowerCAmelCase      )
							model.to(__lowerCAmelCase      )
							model.eval()

							__snake_case						 =   model(__lowerCAmelCase       ,		token_type_ids=__lowerCAmelCase       ,		head_mask=__lowerCAmelCase      )
							__snake_case						 =   model(__lowerCAmelCase       ,		token_type_ids=__lowerCAmelCase      )
							__snake_case						 =   model(__lowerCAmelCase      )

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




	def   lowercase__   (       self     :  Union[str, Any]       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  Dict       ,		__lowerCAmelCase     :  Dict       ,		__lowerCAmelCase     :  Dict       ,		*__lowerCAmelCase     :  str      ):
							__snake_case						 =   OpenAIGPTLMHeadModel(__lowerCAmelCase      )
							model.to(__lowerCAmelCase      )
							model.eval()

							__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   lowercase__   (       self     :  Optional[Any]       ,		__lowerCAmelCase     :  str       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  List[Any]       ,		*__lowerCAmelCase     :  Tuple      ):
							__snake_case						 =   OpenAIGPTDoubleHeadsModel(__lowerCAmelCase      )
							model.to(__lowerCAmelCase      )
							model.eval()

							__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   lowercase__   (       self     :  Optional[int]       ,		__lowerCAmelCase     :  Union[str, Any]       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  Tuple       ,		__lowerCAmelCase     :  int       ,		*__lowerCAmelCase     :  int      ):
							__snake_case						 =   self.num_labels
							__snake_case						 =   OpenAIGPTForSequenceClassification(__lowerCAmelCase      )
							model.to(__lowerCAmelCase      )
							model.eval()

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




	def   lowercase__   (       self     :  List[str]      ):
							__snake_case						 =   self.prepare_config_and_inputs()
							(
							    (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       
							)						 =   config_and_inputs
							__snake_case						 =   {
							    'input_ids': input_ids,
							    'token_type_ids': token_type_ids,
							    'head_mask': head_mask,
							}

							return config, inputs_dict





@require_torch
class 						a_				(			UpperCAmelCase__     ,						UpperCAmelCase__     ,						UpperCAmelCase__     ,						unittest.TestCase						):
	lowercase_       :     List[str]			  =      (
	    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
	    if is_torch_available()
	    else ()
	)
	lowercase_       :     Union[str, Any]			  =      (
	    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
	)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
	lowercase_       :     str			  =      (
	    {
	        '''feature-extraction''': OpenAIGPTModel,
	        '''text-classification''': OpenAIGPTForSequenceClassification,
	        '''text-generation''': OpenAIGPTLMHeadModel,
	        '''zero-shot''': OpenAIGPTForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)




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

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




	def   lowercase__   (       self     :  Tuple      ):
							__snake_case						 =   OpenAIGPTModelTester(self      )
							__snake_case						 =   ConfigTester(self       ,		config_class=__lowerCAmelCase       ,		n_embd=3_7      )




	def   lowercase__   (       self     :  int      ):
							self.config_tester.run_common_tests()




	def   lowercase__   (       self     :  Optional[Any]      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  int      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  Any      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_double_lm_head_model(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  str      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowerCAmelCase      )




	@slow
	def   lowercase__   (       self     :  str      ):
							for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__snake_case						 =   OpenAIGPTModel.from_pretrained(__lowerCAmelCase      )
													self.assertIsNotNone(__lowerCAmelCase      )





@require_torch
class 						a_				(			unittest.TestCase						):




	@slow
	def   lowercase__   (       self     :  int      ):
							__snake_case						 =   OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'      )
							model.to(__lowerCAmelCase      )
							__snake_case						 =   torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]       ,		dtype=torch.long       ,		device=__lowerCAmelCase      )  # the president is
							__snake_case						 =   [
							    4_8_1,
							    4_7_3_5,
							    5_4_4,
							    2_4_6,
							    9_6_3,
							    8_7_0,
							    7_6_2,
							    2_3_9,
							    2_4_4,
							    4_0_4_7_7,
							    2_4_4,
							    2_4_9,
							    7_1_9,
							    8_8_1,
							    4_8_7,
							    5_4_4,
							    2_4_0,
							    2_4_4,
							    6_0_3,
							    4_8_1,
							]  # the president is a very good man. " \n " i\'m sure he is, " said the

							__snake_case						 =   model.generate(__lowerCAmelCase       ,		do_sample=__lowerCAmelCase      )
							self.assertListEqual(output_ids[0].tolist()       ,		__lowerCAmelCase      )

