




from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


lowercase_	:				Dict  				= logging.get_logger(__name__)

# General docstring
lowercase_	:				List[str]  				= 'ResNetConfig'

# Base docstring
lowercase_	:				str  				= 'microsoft/resnet-50'
lowercase_	:				Union[str, Any]  				= [1, 20_48, 7, 7]

# Image classification docstring
lowercase_	:				List[str]  				= 'microsoft/resnet-50'
lowercase_	:				Dict  				= 'tiger cat'

lowercase_	:				str  				= [
    'microsoft/resnet-50',
    # See all resnet models at https://huggingface.co/models?filter=resnet
]






class     __lowerCAmelCase (       nn.Module ):







			def __init__(       self    :			str    ,    snake_case__    :			Tuple    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			str = 3    ,    snake_case__    :			List[str] = 1    ,    snake_case__    :			str = "relu"      ):




									"""simple docstring"""


									super().__init__()
									_UpperCAmelCase													=     nn.Convad(
									    snake_case__    ,    snake_case__    ,    kernel_size=snake_case__    ,    stride=snake_case__    ,    padding=kernel_size // 2    ,    bias=snake_case__      )
									_UpperCAmelCase													=     nn.BatchNormad(snake_case__      )
									_UpperCAmelCase													=     ACTaFN[activation] if activation is not None else nn.Identity()







			def UpperCamelCase			(       self    :			Dict    ,    snake_case__    :			Tuple      ):




									"""simple docstring"""


									_UpperCAmelCase													=     self.convolution(snake_case__      )
									_UpperCAmelCase													=     self.normalization(snake_case__      )
									_UpperCAmelCase													=     self.activation(snake_case__      )
									return hidden_state






class     __lowerCAmelCase (       nn.Module ):







			def __init__(       self    :			Optional[int]    ,    snake_case__    :			List[Any]      ):




									"""simple docstring"""


									super().__init__()
									_UpperCAmelCase													=     ResNetConvLayer(
									    config.num_channels    ,    config.embedding_size    ,    kernel_size=7    ,    stride=2    ,    activation=config.hidden_act      )
									_UpperCAmelCase													=     nn.MaxPoolad(kernel_size=3    ,    stride=2    ,    padding=1      )
									_UpperCAmelCase													=     config.num_channels







			def UpperCamelCase			(       self    :			Optional[int]    ,    snake_case__    :			Dict      ):




									"""simple docstring"""


									_UpperCAmelCase													=     pixel_values.shape[1]
									if num_channels != self.num_channels:
															raise ValueError(
															    "Make sure that the channel dimension of the pixel values match with the one set in the configuration."      )
									_UpperCAmelCase													=     self.embedder(snake_case__      )
									_UpperCAmelCase													=     self.pooler(snake_case__      )
									return embedding






class     __lowerCAmelCase (       nn.Module ):







			def __init__(       self    :			List[Any]    ,    snake_case__    :			List[Any]    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			Optional[Any] = 2      ):




									"""simple docstring"""


									super().__init__()
									_UpperCAmelCase													=     nn.Convad(snake_case__    ,    snake_case__    ,    kernel_size=1    ,    stride=snake_case__    ,    bias=snake_case__      )
									_UpperCAmelCase													=     nn.BatchNormad(snake_case__      )







			def UpperCamelCase			(       self    :			Any    ,    snake_case__    :			Optional[int]      ):




									"""simple docstring"""


									_UpperCAmelCase													=     self.convolution(snake_case__      )
									_UpperCAmelCase													=     self.normalization(snake_case__      )
									return hidden_state






class     __lowerCAmelCase (       nn.Module ):







			def __init__(       self    :			Tuple    ,    snake_case__    :			Optional[int]    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			Optional[int] = 1    ,    snake_case__    :			Any = "relu"      ):




									"""simple docstring"""


									super().__init__()
									_UpperCAmelCase													=     in_channels != out_channels or stride != 1
									_UpperCAmelCase													=     (
									    ResNetShortCut(snake_case__    ,    snake_case__    ,    stride=snake_case__      ) if should_apply_shortcut else nn.Identity()
									)
									_UpperCAmelCase													=     nn.Sequential(
									    ResNetConvLayer(snake_case__    ,    snake_case__    ,    stride=snake_case__      )    ,    ResNetConvLayer(snake_case__    ,    snake_case__    ,    activation=snake_case__      )    ,    )
									_UpperCAmelCase													=     ACTaFN[activation]







			def UpperCamelCase			(       self    :			Dict    ,    snake_case__    :			Optional[Any]      ):




									"""simple docstring"""


									_UpperCAmelCase													=     hidden_state
									_UpperCAmelCase													=     self.layer(snake_case__      )
									_UpperCAmelCase													=     self.shortcut(snake_case__      )
									hidden_state += residual
									_UpperCAmelCase													=     self.activation(snake_case__      )
									return hidden_state






class     __lowerCAmelCase (       nn.Module ):







			def __init__(       self    :			Tuple    ,    snake_case__    :			Tuple    ,    snake_case__    :			List[Any]    ,    snake_case__    :			str = 1    ,    snake_case__    :			int = "relu"    ,    snake_case__    :			Optional[Any] = 4      ):




									"""simple docstring"""


									super().__init__()
									_UpperCAmelCase													=     in_channels != out_channels or stride != 1
									_UpperCAmelCase													=     out_channels // reduction
									_UpperCAmelCase													=     (
									    ResNetShortCut(snake_case__    ,    snake_case__    ,    stride=snake_case__      ) if should_apply_shortcut else nn.Identity()
									)
									_UpperCAmelCase													=     nn.Sequential(
									    ResNetConvLayer(snake_case__    ,    snake_case__    ,    kernel_size=1      )    ,    ResNetConvLayer(snake_case__    ,    snake_case__    ,    stride=snake_case__      )    ,    ResNetConvLayer(snake_case__    ,    snake_case__    ,    kernel_size=1    ,    activation=snake_case__      )    ,    )
									_UpperCAmelCase													=     ACTaFN[activation]







			def UpperCamelCase			(       self    :			int    ,    snake_case__    :			Union[str, Any]      ):




									"""simple docstring"""


									_UpperCAmelCase													=     hidden_state
									_UpperCAmelCase													=     self.layer(snake_case__      )
									_UpperCAmelCase													=     self.shortcut(snake_case__      )
									hidden_state += residual
									_UpperCAmelCase													=     self.activation(snake_case__      )
									return hidden_state






class     __lowerCAmelCase (       nn.Module ):







			def __init__(       self    :			Optional[Any]    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			List[Any]    ,    snake_case__    :			Any    ,    snake_case__    :			Dict = 2    ,    snake_case__    :			List[str] = 2    ,    ):




									"""simple docstring"""


									super().__init__()

									_UpperCAmelCase													=     ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

									_UpperCAmelCase													=     nn.Sequential(
									    # downsampling is done in the first layer with stride of 2
									    layer(snake_case__    ,    snake_case__    ,    stride=snake_case__    ,    activation=config.hidden_act      )    ,    *[layer(snake_case__    ,    snake_case__    ,    activation=config.hidden_act      ) for _ in range(depth - 1      )]    ,    )







			def UpperCamelCase			(       self    :			str    ,    snake_case__    :			List[Any]      ):




									"""simple docstring"""


									_UpperCAmelCase													=     input
									for layer in self.layers:
															_UpperCAmelCase													=     layer(snake_case__      )
									return hidden_state






class     __lowerCAmelCase (       nn.Module ):







			def __init__(       self    :			Union[str, Any]    ,    snake_case__    :			List[Any]      ):




									"""simple docstring"""


									super().__init__()
									_UpperCAmelCase													=     nn.ModuleList([]      )
									# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
									self.stages.append(
									    ResNetStage(
									        snake_case__    ,    config.embedding_size    ,    config.hidden_sizes[0]    ,    stride=2 if config.downsample_in_first_stage else 1    ,    depth=config.depths[0]    ,    )      )
									_UpperCAmelCase													=     zip(config.hidden_sizes    ,    config.hidden_sizes[1:]      )
									for (in_channels, out_channels), depth in zip(snake_case__    ,    config.depths[1:]      ):
															self.stages.append(ResNetStage(snake_case__    ,    snake_case__    ,    snake_case__    ,    depth=snake_case__      )      )







			def UpperCamelCase			(       self    :			List[Any]    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			str = False    ,    snake_case__    :			Union[str, Any] = True      ):




									"""simple docstring"""


									_UpperCAmelCase													=     () if output_hidden_states else None

									for stage_module in self.stages:
															if output_hidden_states:
																					_UpperCAmelCase													=     hidden_states + (hidden_state,)

															_UpperCAmelCase													=     stage_module(snake_case__      )

									if output_hidden_states:
															_UpperCAmelCase													=     hidden_states + (hidden_state,)

									if not return_dict:
															return tuple(v for v in [hidden_state, hidden_states] if v is not None      )

									return BaseModelOutputWithNoAttention(
									    last_hidden_state=snake_case__    ,    hidden_states=snake_case__    ,    )






class     __lowerCAmelCase (       A_ ):
			snake_case_      : Any      =     ResNetConfig
			snake_case_      : Optional[int]      =     '''resnet'''
			snake_case_      : Optional[Any]      =     '''pixel_values'''
			snake_case_      : Tuple      =     True







			def UpperCamelCase			(       self    :			Dict    ,    snake_case__    :			Any      ):




									"""simple docstring"""


									if isinstance(snake_case__    ,    nn.Convad      ):
															nn.init.kaiming_normal_(module.weight    ,    mode="fan_out"    ,    nonlinearity="relu"      )
									elif isinstance(snake_case__    ,    (nn.BatchNormad, nn.GroupNorm)      ):
															nn.init.constant_(module.weight    ,    1      )
															nn.init.constant_(module.bias    ,    0      )







			def UpperCamelCase			(       self    :			Union[str, Any]    ,    snake_case__    :			List[str]    ,    snake_case__    :			List[Any]=False      ):




									"""simple docstring"""


									if isinstance(snake_case__    ,    snake_case__      ):
															_UpperCAmelCase													=     value




lowercase_	:				int  				= r'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

lowercase_	:				Union[str, Any]  				= r'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'






@add_start_docstrings(
    "The bare ResNet model outputting raw features without any specific head on top."					,		A_					,		)
class     __lowerCAmelCase (       A_ ):







			def __init__(       self    :			Dict    ,    snake_case__    :			List[Any]      ):




									"""simple docstring"""


									super().__init__(snake_case__      )
									_UpperCAmelCase													=     config
									_UpperCAmelCase													=     ResNetEmbeddings(snake_case__      )
									_UpperCAmelCase													=     ResNetEncoder(snake_case__      )
									_UpperCAmelCase													=     nn.AdaptiveAvgPoolad((1, 1)      )
									# Initialize weights and apply final processing
									self.post_init()







			@add_start_docstrings_to_model_forward(snake_case__      )
			@add_code_sample_docstrings(
			    checkpoint=_CHECKPOINT_FOR_DOC    ,    output_type=snake_case__    ,    config_class=_CONFIG_FOR_DOC    ,    modality="vision"    ,    expected_output=_EXPECTED_OUTPUT_SHAPE    ,    )
			def UpperCamelCase			(       self    :			Union[str, Any]    ,    snake_case__    :			List[Any]    ,    snake_case__    :			Optional[Any] = None    ,    snake_case__    :			str = None      ):




									"""simple docstring"""


									_UpperCAmelCase													=     (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)
									_UpperCAmelCase													=     return_dict if return_dict is not None else self.config.use_return_dict

									_UpperCAmelCase													=     self.embedder(snake_case__      )

									_UpperCAmelCase													=     self.encoder(
									    snake_case__    ,    output_hidden_states=snake_case__    ,    return_dict=snake_case__      )

									_UpperCAmelCase													=     encoder_outputs[0]

									_UpperCAmelCase													=     self.pooler(snake_case__      )

									if not return_dict:
															return (last_hidden_state, pooled_output) + encoder_outputs[1:]

									return BaseModelOutputWithPoolingAndNoAttention(
									    last_hidden_state=snake_case__    ,    pooler_output=snake_case__    ,    hidden_states=encoder_outputs.hidden_states    ,    )






@add_start_docstrings(
    "\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "					,		A_					,		)
class     __lowerCAmelCase (       A_ ):







			def __init__(       self    :			Optional[Any]    ,    snake_case__    :			Optional[Any]      ):




									"""simple docstring"""


									super().__init__(snake_case__      )
									_UpperCAmelCase													=     config.num_labels
									_UpperCAmelCase													=     ResNetModel(snake_case__      )
									# classification head
									_UpperCAmelCase													=     nn.Sequential(
									    nn.Flatten()    ,    nn.Linear(config.hidden_sizes[-1]    ,    config.num_labels      ) if config.num_labels > 0 else nn.Identity()    ,    )
									# initialize weights and apply final processing
									self.post_init()







			@add_start_docstrings_to_model_forward(snake_case__      )
			@add_code_sample_docstrings(
			    checkpoint=_IMAGE_CLASS_CHECKPOINT    ,    output_type=snake_case__    ,    config_class=_CONFIG_FOR_DOC    ,    expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT    ,    )
			def UpperCamelCase			(       self    :			Tuple    ,    snake_case__    :			List[Any] = None    ,    snake_case__    :			int = None    ,    snake_case__    :			str = None    ,    snake_case__    :			List[Any] = None    ,    ):




									"""simple docstring"""


									_UpperCAmelCase													=     return_dict if return_dict is not None else self.config.use_return_dict

									_UpperCAmelCase													=     self.resnet(snake_case__    ,    output_hidden_states=snake_case__    ,    return_dict=snake_case__      )

									_UpperCAmelCase													=     outputs.pooler_output if return_dict else outputs[1]

									_UpperCAmelCase													=     self.classifier(snake_case__      )

									_UpperCAmelCase													=     None

									if labels is not None:
															if self.config.problem_type is None:
																					if self.num_labels == 1:
																											_UpperCAmelCase													=     "regression"
																					elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																											_UpperCAmelCase													=     "single_label_classification"
																					else:
																											_UpperCAmelCase													=     "multi_label_classification"
															if self.config.problem_type == "regression":
																					_UpperCAmelCase													=     MSELoss()
																					if self.num_labels == 1:
																											_UpperCAmelCase													=     loss_fct(logits.squeeze()    ,    labels.squeeze()      )
																					else:
																											_UpperCAmelCase													=     loss_fct(snake_case__    ,    snake_case__      )
															elif self.config.problem_type == "single_label_classification":
																					_UpperCAmelCase													=     CrossEntropyLoss()
																					_UpperCAmelCase													=     loss_fct(logits.view(-1    ,    self.num_labels      )    ,    labels.view(-1      )      )
															elif self.config.problem_type == "multi_label_classification":
																					_UpperCAmelCase													=     BCEWithLogitsLoss()
																					_UpperCAmelCase													=     loss_fct(snake_case__    ,    snake_case__      )

									if not return_dict:
															_UpperCAmelCase													=     (logits,) + outputs[2:]
															return (loss,) + output if loss is not None else output

									return ImageClassifierOutputWithNoAttention(loss=snake_case__    ,    logits=snake_case__    ,    hidden_states=outputs.hidden_states      )






@add_start_docstrings(
    "\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    "					,		A_					,		)
class     __lowerCAmelCase (       A_					,		A_ ):







			def __init__(       self    :			Dict    ,    snake_case__    :			Optional[Any]      ):




									"""simple docstring"""


									super().__init__(snake_case__      )
									super()._init_backbone(snake_case__      )

									_UpperCAmelCase													=     [config.embedding_size] + config.hidden_sizes
									_UpperCAmelCase													=     ResNetEmbeddings(snake_case__      )
									_UpperCAmelCase													=     ResNetEncoder(snake_case__      )

									# initialize weights and apply final processing
									self.post_init()







			@add_start_docstrings_to_model_forward(snake_case__      )
			@replace_return_docstrings(output_type=snake_case__    ,    config_class=_CONFIG_FOR_DOC      )
			def UpperCamelCase			(       self    :			Any    ,    snake_case__    :			str    ,    snake_case__    :			Optional[int] = None    ,    snake_case__    :			Tuple = None      ):




									"""simple docstring"""


									_UpperCAmelCase													=     return_dict if return_dict is not None else self.config.use_return_dict
									_UpperCAmelCase													=     (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)

									_UpperCAmelCase													=     self.embedder(snake_case__      )

									_UpperCAmelCase													=     self.encoder(snake_case__    ,    output_hidden_states=snake_case__    ,    return_dict=snake_case__      )

									_UpperCAmelCase													=     outputs.hidden_states

									_UpperCAmelCase													=     ()
									for idx, stage in enumerate(self.stage_names      ):
															if stage in self.out_features:
																					feature_maps += (hidden_states[idx],)

									if not return_dict:
															_UpperCAmelCase													=     (feature_maps,)
															if output_hidden_states:
																					output += (outputs.hidden_states,)
															return output

									return BackboneOutput(
									    feature_maps=snake_case__    ,    hidden_states=outputs.hidden_states if output_hidden_states else None    ,    attentions=snake_case__    ,    )








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      ,				lowerCAmelCase      ,				lowerCAmelCase=1_3      ,				lowerCAmelCase=7      ,				lowerCAmelCase=True      ,				lowerCAmelCase=True      ,				lowerCAmelCase=True      ,				lowerCAmelCase=9_9      ,				lowerCAmelCase=3_2      ,				lowerCAmelCase=5      ,				lowerCAmelCase=4      ,				lowerCAmelCase=3_7      ,				lowerCAmelCase="gelu"      ,				lowerCAmelCase=0.1      ,				lowerCAmelCase=0.1      ,				lowerCAmelCase=5_1_2      ,				lowerCAmelCase=1_6      ,				lowerCAmelCase=2      ,				lowerCAmelCase=0.02      ,				lowerCAmelCase=3      ,				lowerCAmelCase=4      ,				lowerCAmelCase=None      ,				):
									__lowercase=			parent
									__lowercase=			batch_size
									__lowercase=			seq_length
									__lowercase=			is_training
									__lowercase=			use_token_type_ids
									__lowercase=			use_labels
									__lowercase=			vocab_size
									__lowercase=			hidden_size
									__lowercase=			num_hidden_layers
									__lowercase=			num_attention_heads
									__lowercase=			intermediate_size
									__lowercase=			hidden_act
									__lowercase=			hidden_dropout_prob
									__lowercase=			attention_probs_dropout_prob
									__lowercase=			max_position_embeddings
									__lowercase=			type_vocab_size
									__lowercase=			type_sequence_label_size
									__lowercase=			initializer_range
									__lowercase=			num_labels
									__lowercase=			num_choices
									__lowercase=			scope
									__lowercase=			self.vocab_size - 1


							def 			_A       (self					):
									__lowercase=			ids_tensor([self.batch_size, self.seq_length]      ,				self.vocab_size					)

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

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

									__lowercase=			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      ,				)

									__lowercase=			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 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				*lowerCAmelCase					):
									__lowercase=			OpenAIGPTModel(config=lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()

									__lowercase=			model(lowerCAmelCase      ,				token_type_ids=lowerCAmelCase      ,				head_mask=lowerCAmelCase					)
									__lowercase=			model(lowerCAmelCase      ,				token_type_ids=lowerCAmelCase					)
									__lowercase=			model(lowerCAmelCase					)

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


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				*lowerCAmelCase					):
									__lowercase=			OpenAIGPTLMHeadModel(lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()

									__lowercase=			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 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				*lowerCAmelCase					):
									__lowercase=			OpenAIGPTDoubleHeadsModel(lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()

									__lowercase=			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 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				*lowerCAmelCase					):
									__lowercase=			self.num_labels
									__lowercase=			OpenAIGPTForSequenceClassification(lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()

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







							def 			_A       (self					):
									__lowercase=			self.prepare_config_and_inputs()
									(
									    (
									    __lowercase
									),							(
									    __lowercase
									),							(
									    __lowercase
									),							(
									    __lowercase
									),							(
									    __lowercase
									),							(
									    __lowercase
									),							(
									    __lowercase
									),							
									)=			config_and_inputs
									__lowercase=			{
									    'input_ids': input_ids,
									    'token_type_ids': token_type_ids,
									    'head_mask': head_mask,
									}

									return config, inputs_dict





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


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									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 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase=False					):
									__lowercase=			super()._prepare_for_class(lowerCAmelCase      ,				lowerCAmelCase      ,				return_labels=lowerCAmelCase					)

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


							def 			_A       (self					):
									__lowercase=			OpenAIGPTModelTester(self					)
									__lowercase=			ConfigTester(self      ,				config_class=lowerCAmelCase      ,				n_embd=3_7					)


							def 			_A       (self					):
									self.config_tester.run_common_tests()


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase					)







							@slow
							def 			_A       (self					):
									for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowercase=			OpenAIGPTModel.from_pretrained(lowerCAmelCase					)
											self.assertIsNotNone(lowerCAmelCase					)





@require_torch
class       A    (   unittest.TestCase ):


							@slow
							def 			_A       (self					):
									__lowercase=			OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'					)
									model.to(lowerCAmelCase					)
									__lowercase=			torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]      ,				dtype=torch.long      ,				device=lowerCAmelCase					)  # the president is
									__lowercase=			[
									    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

									__lowercase=			model.generate(lowerCAmelCase      ,				do_sample=lowerCAmelCase					)
									self.assertListEqual(output_ids[0].tolist()      ,				lowerCAmelCase					)






