

'''simple docstring'''


import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
			    OpenAIGPTConfig,
			    OpenAIGPTDoubleHeadsModel,
			    OpenAIGPTForSequenceClassification,
			    OpenAIGPTLMHeadModel,
			    OpenAIGPTModel,
			)




class     a			:






						"""simple docstring"""




						def __init__(							self					:      List[Any]			,			snake_case_					:      str			,			snake_case_					:      Tuple=1_3			,			snake_case_					:      List[str]=7			,			snake_case_					:      Union[str, Any]=True			,			snake_case_					:      Optional[int]=True			,			snake_case_					:      Tuple=True			,			snake_case_					:      List[str]=9_9			,			snake_case_					:      List[Any]=3_2			,			snake_case_					:      Optional[int]=5			,			snake_case_					:      Dict=4			,			snake_case_					:      Any=3_7			,			snake_case_					:      List[Any]="gelu"			,			snake_case_					:      int=0.1			,			snake_case_					:      Dict=0.1			,			snake_case_					:      List[str]=5_1_2			,			snake_case_					:      Dict=1_6			,			snake_case_					:      Dict=2			,			snake_case_					:      int=0.0_2			,			snake_case_					:      Optional[int]=3			,			snake_case_					:      List[str]=4			,			snake_case_					:      Any=None			,			):



								'''simple docstring'''



								snake_case__						:       Optional[Any]       						=  parent
								snake_case__						:       Dict       						=  batch_size
								snake_case__						:       List[str]       						=  seq_length
								snake_case__						:       Dict       						=  is_training
								snake_case__						:       List[Any]       						=  use_token_type_ids
								snake_case__						:       List[str]       						=  use_labels
								snake_case__						:       Union[str, Any]       						=  vocab_size
								snake_case__						:       Optional[int]       						=  hidden_size
								snake_case__						:       Any       						=  num_hidden_layers
								snake_case__						:       Dict       						=  num_attention_heads
								snake_case__						:       Tuple       						=  intermediate_size
								snake_case__						:       Dict       						=  hidden_act
								snake_case__						:       List[str]       						=  hidden_dropout_prob
								snake_case__						:       List[Any]       						=  attention_probs_dropout_prob
								snake_case__						:       Dict       						=  max_position_embeddings
								snake_case__						:       List[str]       						=  type_vocab_size
								snake_case__						:       Any       						=  type_sequence_label_size
								snake_case__						:       Dict       						=  initializer_range
								snake_case__						:       List[str]       						=  num_labels
								snake_case__						:       Union[str, Any]       						=  num_choices
								snake_case__						:       List[str]       						=  scope
								snake_case__						:       Optional[Any]       						=  self.vocab_size - 1







						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



								snake_case__						:       Optional[int]       						=  ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size  )

								snake_case__						:       List[Any]       						=  None
								if self.use_token_type_ids:
										snake_case__						:       Optional[int]       						=  ids_tensor([self.batch_size, self.seq_length]			,			self.type_vocab_size  )

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

								snake_case__						:       Tuple       						=  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__						:       List[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					:      Any			,			snake_case_					:      Optional[int]			,			snake_case_					:      str			,			snake_case_					:      List[Any]			,			snake_case_					:      Any			,			*snake_case_					:      str  ):



								'''simple docstring'''



								snake_case__						:       str       						=  OpenAIGPTModel(config=UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								snake_case__						:       Optional[Any]       						=  model(UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__			,			head_mask=UpperCAmelCase__  )
								snake_case__						:       int       						=  model(UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__  )
								snake_case__						:       Optional[Any]       						=  model(UpperCAmelCase__  )

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







						def       __magic_name__							(							self					:      int			,			snake_case_					:      List[str]			,			snake_case_					:      Union[str, Any]			,			snake_case_					:      Tuple			,			snake_case_					:      Dict			,			*snake_case_					:      Tuple  ):



								'''simple docstring'''



								snake_case__						:       List[str]       						=  OpenAIGPTLMHeadModel(UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								snake_case__						:       Any       						=  model(UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__			,			labels=UpperCAmelCase__  )
								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					:      List[str]			,			snake_case_					:      Tuple			,			snake_case_					:      Optional[int]			,			snake_case_					:      List[str]			,			snake_case_					:      int			,			*snake_case_					:      Optional[Any]  ):



								'''simple docstring'''



								snake_case__						:       str       						=  OpenAIGPTDoubleHeadsModel(UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								snake_case__						:       int       						=  model(UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__			,			labels=UpperCAmelCase__  )
								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					:      Optional[Any]			,			snake_case_					:      Any			,			snake_case_					:      Any			,			snake_case_					:      int			,			snake_case_					:      Union[str, Any]			,			*snake_case_					:      Optional[int]  ):



								'''simple docstring'''



								snake_case__						:       Dict       						=  self.num_labels
								snake_case__						:       List[str]       						=  OpenAIGPTForSequenceClassification(UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								snake_case__						:       Union[str, Any]       						=  ids_tensor([self.batch_size]			,			self.type_sequence_label_size  )
								snake_case__						:       str       						=  model(UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__			,			labels=UpperCAmelCase__  )
								self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)  )







						def       __magic_name__							(							self					:      List[str]  ):



								'''simple docstring'''



								snake_case__						:       int       						=  self.prepare_config_and_inputs()
								(
								    snake_case__	
								)						:       List[Any]       						=  config_and_inputs
								snake_case__						:       Tuple       						=  {
								    '''input_ids''': input_ids,
								    '''token_type_ids''': token_type_ids,
								    '''head_mask''': head_mask,
								}

								return config, inputs_dict





@require_torch
class     a			(				SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE			,			unittest.TestCase       ):






						"""simple docstring"""




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







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      int			,			snake_case_					:      Tuple			,			snake_case_					:      Optional[int]			,			snake_case_					:      Dict			,			snake_case_					:      List[str]  ):



								'''simple docstring'''



								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					:      Dict			,			snake_case_					:      List[Any]			,			snake_case_					:      List[Any]			,			snake_case_					:      Optional[Any]=False  ):



								'''simple docstring'''



								snake_case__						:       str       						=  super()._prepare_for_class(UpperCAmelCase__			,			UpperCAmelCase__			,			return_labels=UpperCAmelCase__  )

								if return_labels:
										if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
												snake_case__						:       Union[str, Any]       						=  torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)			,			dtype=torch.long			,			device=UpperCAmelCase__			,			)
												snake_case__						:       Dict       						=  inputs_dict['''labels''']
												snake_case__						:       Optional[int]       						=  inputs_dict['''labels''']
												snake_case__						:       Dict       						=  torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.num_choices)			,			dtype=torch.long			,			device=UpperCAmelCase__			,			)
												snake_case__						:       Dict       						=  torch.zeros(
												    self.model_tester.batch_size			,			dtype=torch.long			,			device=UpperCAmelCase__  )
								return inputs_dict







						def       __magic_name__							(							self					:      str  ):



								'''simple docstring'''



								snake_case__						:       Optional[Any]       						=  OpenAIGPTModelTester(self  )
								snake_case__						:       int       						=  ConfigTester(self			,			config_class=UpperCAmelCase__			,			n_embd=3_7  )







						def       __magic_name__							(							self					:      Optional[int]  ):



								'''simple docstring'''



								self.config_tester.run_common_tests()







						def       __magic_name__							(							self					:      int  ):



								'''simple docstring'''



								snake_case__						:       Tuple       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__  )







						def       __magic_name__							(							self					:      str  ):



								'''simple docstring'''



								snake_case__						:       Any       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase__  )







						def       __magic_name__							(							self					:      Any  ):



								'''simple docstring'''



								snake_case__						:       List[str]       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase__  )







						def       __magic_name__							(							self					:      List[str]  ):



								'''simple docstring'''



								snake_case__						:       Dict       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__  )







						@slow
						def       __magic_name__							(							self					:      Union[str, Any]  ):



								'''simple docstring'''



								for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										snake_case__						:       Any       						=  OpenAIGPTModel.from_pretrained(UpperCAmelCase__  )
										self.assertIsNotNone(UpperCAmelCase__  )





@require_torch
class     a			(				unittest.TestCase       ):






						"""simple docstring"""




						@slow
						def       __magic_name__							(							self					:      Optional[int]  ):



								'''simple docstring'''



								snake_case__						:       List[Any]       						=  OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''  )
								model.to(UpperCAmelCase__  )
								snake_case__						:       Optional[Any]       						=  torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]			,			dtype=torch.long			,			device=UpperCAmelCase__  )  # the president is
								snake_case__						:       str       						=  [
								    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__						:       Tuple       						=  model.generate(UpperCAmelCase__			,			do_sample=UpperCAmelCase__  )
								self.assertListEqual(output_ids[0].tolist()			,			UpperCAmelCase__  )






'''simple docstring'''


from dataclasses import dataclass
from typing import Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, randn_tensor
from .scheduling_utils import SchedulerMixin




@dataclass
class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    42
						__UpperCAmelCase			     =    42
						__UpperCAmelCase			     =    None





class     a			(				SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    2







						@register_to_config
						def __init__(							self					:      int			,			snake_case_					:      float = 0.0_2			,			snake_case_					:      float = 1_0_0			,			snake_case_					:      float = 1.0_0_7			,			snake_case_					:      float = 8_0			,			snake_case_					:      float = 0.0_5			,			snake_case_					:      float = 5_0			,			):



								'''simple docstring'''



								snake_case__						:       List[Any]       						=  sigma_max

								# setable values
								snake_case__						:       int       						=  None
								snake_case__						:       np.IntTensor       						=  None
								snake_case__						:       torch.FloatTensor       						=  None  # sigma(t_i)







						def       __magic_name__							(							self					:      Any			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      Optional[int] = None  ):



								'''simple docstring'''



								return sample







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      int			,			snake_case_					:      Union[str, torch.device] = None  ):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  num_inference_steps
								snake_case__						:       Tuple       						=  np.arange(0			,			self.num_inference_steps  )[::-1].copy()
								snake_case__						:       int       						=  torch.from_numpy(snake_case_  ).to(snake_case_  )
								snake_case__						:       List[str]       						=  [
								    (
								        self.config.sigma_max**2
								        * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
								    )
								    for i in self.timesteps
								]
								snake_case__						:       Union[str, Any]       						=  torch.tensor(snake_case_			,			dtype=torch.floataa			,			device=snake_case_  )







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      float			,			snake_case_					:      Optional[torch.Generator] = None  ):



								'''simple docstring'''



								if self.config.s_min <= sigma <= self.config.s_max:
										snake_case__						:       List[Any]       						=  min(self.config.s_churn / self.num_inference_steps			,			2**0.5 - 1  )
								else:
										snake_case__						:       List[Any]       						=  0

								# sample eps ~ N(0, S_noise^2 * I)
								snake_case__						:       Any       						=  self.config.s_noise * randn_tensor(sample.shape			,			generator=snake_case_  ).to(sample.device  )
								snake_case__						:       Tuple       						=  sigma + gamma * sigma
								snake_case__						:       Dict       						=  sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps)

								return sample_hat, sigma_hat







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      float			,			snake_case_					:      float			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      bool = True			,			):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  sample_hat + sigma_hat * model_output
								snake_case__						:       List[Any]       						=  (sample_hat - pred_original_sample) / sigma_hat
								snake_case__						:       Any       						=  sample_hat + (sigma_prev - sigma_hat) * derivative

								if not return_dict:
										return (sample_prev, derivative)

								return KarrasVeOutput(
								    prev_sample=snake_case_			,			derivative=snake_case_			,			pred_original_sample=snake_case_  )







						def       __magic_name__							(							self					:      Tuple			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      float			,			snake_case_					:      float			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      bool = True			,			):



								'''simple docstring'''



								snake_case__						:       str       						=  sample_prev + sigma_prev * model_output
								snake_case__						:       List[Any]       						=  (sample_prev - pred_original_sample) / sigma_prev
								snake_case__						:       Any       						=  sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr)

								if not return_dict:
										return (sample_prev, derivative)

								return KarrasVeOutput(
								    prev_sample=snake_case_			,			derivative=snake_case_			,			pred_original_sample=snake_case_  )







						def       __magic_name__							(							self					:      str			,			snake_case_					:      List[Any]			,			snake_case_					:      Optional[int]			,			snake_case_					:      Tuple  ):



								'''simple docstring'''



								raise NotImplementedError()



