





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





					"""simple docstring"""


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





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       ids_tensor([self.batch_size, self.seq_length]	,  self.vocab_size	)

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

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

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

										lowerCamelCase__       =       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	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  *_lowerCAmelCase	):
										lowerCamelCase__       =       OpenAIGPTModel(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()

										lowerCamelCase__       =       model(UpperCamelCase__	,  token_type_ids=UpperCamelCase__	,  head_mask=UpperCamelCase__	)
										lowerCamelCase__       =       model(UpperCamelCase__	,  token_type_ids=UpperCamelCase__	)
										lowerCamelCase__       =       model(UpperCamelCase__	)

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





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  *_lowerCAmelCase	):
										lowerCamelCase__       =       OpenAIGPTLMHeadModel(UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()

										lowerCamelCase__       =       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	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  *_lowerCAmelCase	):
										lowerCamelCase__       =       OpenAIGPTDoubleHeadsModel(UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()

										lowerCamelCase__       =       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	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  *_lowerCAmelCase	):
										lowerCamelCase__       =       self.num_labels
										lowerCamelCase__       =       OpenAIGPTForSequenceClassification(UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()

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






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

										return config, inputs_dict

@require_torch
class SCREAMING_SNAKE_CASE_ (					__A							,       __A							,       __A							,       unittest.TestCase   ):





					"""simple docstring"""


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





					def 					__magic_name__ ( 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 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase=False	):
										lowerCamelCase__       =       super()._prepare_for_class(UpperCamelCase__	,  UpperCamelCase__	,  return_labels=UpperCamelCase__	)

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





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       OpenAIGPTModelTester(self	)
										lowerCamelCase__       =       ConfigTester(self	,  config_class=UpperCamelCase__	,  n_embd=37	)





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





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase__	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*UpperCamelCase__	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase__	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase__	)






					@slow
					def 					__magic_name__ ( self	):
										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCamelCase__       =       OpenAIGPTModel.from_pretrained(UpperCamelCase__	)
															self.assertIsNotNone(UpperCamelCase__	)

@require_torch
class SCREAMING_SNAKE_CASE_ (					unittest.TestCase   ):





					"""simple docstring"""


					@slow
					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"	)
										model.to(UpperCamelCase__	)
										lowerCamelCase__       =       torch.tensor([[481, 4735, 544]]	,  dtype=torch.long	,  device=UpperCamelCase__	)  # the president is
										lowerCamelCase__       =       [
										    481,
										    4735,
										    544,
										    246,
										    963,
										    870,
										    762,
										    239,
										    244,
										    4_0477,
										    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

										lowerCamelCase__       =       model.generate(UpperCamelCase__	,  do_sample=UpperCamelCase__	)
										self.assertListEqual(output_ids[0].tolist()	,  UpperCamelCase__	)












from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload

class SCREAMING_SNAKE_CASE_ :





					"""simple docstring"""


					def __init__( self	,  _lowerCAmelCase = None	):
										if components is None:
															lowerCamelCase__       =       []
										lowerCamelCase__       =       list(_lowerCAmelCase	)





					def __len__( self	):
										return len(self.__components	)





					def __str__( self	):
										return "(" + ",".join(map(_lowerCAmelCase	,  self.__components	)	) + ")"





					def __add__( self	,  _lowerCAmelCase	):
										lowerCamelCase__       =       len(self	)
										if size == len(_lowerCAmelCase	):
															lowerCamelCase__       =       [self.__components[i] + other.component(_lowerCAmelCase	) for i in range(_lowerCAmelCase	)]
															return Vector(_lowerCAmelCase	)
										else:
															raise Exception("must have the same size"	)





					def __sub__( self	,  _lowerCAmelCase	):
										lowerCamelCase__       =       len(self	)
										if size == len(_lowerCAmelCase	):
															lowerCamelCase__       =       [self.__components[i] - other.component(_lowerCAmelCase	) for i in range(_lowerCAmelCase	)]
															return Vector(_lowerCAmelCase	)
										else:  # error case
															raise Exception("must have the same size"	)





					@overload
					def __mul__( self	,  _lowerCAmelCase	):
										...





					@overload
					def __mul__( self	,  _lowerCAmelCase	):
										...





					def __mul__( self	,  _lowerCAmelCase	):
										if isinstance(_lowerCAmelCase	,  (float, int)	):
															lowerCamelCase__       =       [c * other for c in self.__components]
															return Vector(_lowerCAmelCase	)
										elif isinstance(_lowerCAmelCase	,  _lowerCAmelCase	) and len(self	) == len(_lowerCAmelCase	):
															lowerCamelCase__       =       len(self	)
															lowerCamelCase__       =       [self.__components[i] * other.component(_lowerCAmelCase	) for i in range(_lowerCAmelCase	)]
															return sum(_lowerCAmelCase	)
										else:  # error case
															raise Exception("invalid operand!"	)





					def 					__magic_name__ ( self	):
										return Vector(self.__components	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										if isinstance(_lowerCAmelCase	,  _lowerCAmelCase	) and -len(self.__components	) <= i < len(self.__components	):
															return self.__components[i]
										else:
															raise Exception("index out of range"	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										assert -len(self.__components	) <= pos < len(self.__components	)
										lowerCamelCase__       =       value





					def 					__magic_name__ ( self	):
										if len(self.__components	) == 0:
															raise Exception("Vector is empty"	)
										lowerCamelCase__       =       [c**2 for c in self.__components]
										return math.sqrt(sum(_lowerCAmelCase	)	)






					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase = False	):
										lowerCamelCase__       =       self * other
										lowerCamelCase__       =       self.euclidean_length() * other.euclidean_length()
										if deg:
															return math.degrees(math.acos(num / den	)	)
										else:
															return math.acos(num / den	)





def    __UpperCamelCase      (    a) ->Vector:
					assert isinstance(a,     a)
					return Vector([0] * dimension)





def    __UpperCamelCase      (    a,     a) ->Vector:
					assert isinstance(a,     a) and (isinstance(a,     a))
					lowerCamelCase__       =       [0] * dimension
					lowerCamelCase__       =       1
					return Vector(a)





def    __UpperCamelCase      (    a,     a,     a) ->Vector:
					assert (
					    isinstance(a,     a)
					    and isinstance(a,     a)
					    and (isinstance(a,     (int, float)))
					)
					return x * scalar + y





def    __UpperCamelCase      (    a,     a,     a) ->Vector:
					random.seed(a)
					lowerCamelCase__       =       [random.randint(a,     a) for _ in range(a)]
					return Vector(a)

class SCREAMING_SNAKE_CASE_ :





					"""simple docstring"""


					def __init__( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       matrix
										lowerCamelCase__       =       w
										lowerCamelCase__       =       h





					def __str__( self	):
										lowerCamelCase__       =       ""
										for i in range(self.__height	):
															ans += "|"
															for j in range(self.__width	):
																				if j < self.__width - 1:
																									ans += str(self.__matrix[i][j]	) + ","
																				else:
																									ans += str(self.__matrix[i][j]	) + "|\n"
										return ans





					def __add__( self	,  _lowerCAmelCase	):
										if self.__width == other.width() and self.__height == other.height():
															lowerCamelCase__       =       []
															for i in range(self.__height	):
																				lowerCamelCase__       =       [
																				    self.__matrix[i][j] + other.component(_lowerCAmelCase	,  _lowerCAmelCase	)
																				    for j in range(self.__width	)
																				]
																				matrix.append(_lowerCAmelCase	)
															return Matrix(_lowerCAmelCase	,  self.__width	,  self.__height	)
										else:
															raise Exception("matrix must have the same dimension!"	)





					def __sub__( self	,  _lowerCAmelCase	):
										if self.__width == other.width() and self.__height == other.height():
															lowerCamelCase__       =       []
															for i in range(self.__height	):
																				lowerCamelCase__       =       [
																				    self.__matrix[i][j] - other.component(_lowerCAmelCase	,  _lowerCAmelCase	)
																				    for j in range(self.__width	)
																				]
																				matrix.append(_lowerCAmelCase	)
															return Matrix(_lowerCAmelCase	,  self.__width	,  self.__height	)
										else:
															raise Exception("matrices must have the same dimension!"	)





					@overload
					def __mul__( self	,  _lowerCAmelCase	):
										...





					@overload
					def __mul__( self	,  _lowerCAmelCase	):
										...





					def __mul__( self	,  _lowerCAmelCase	):
										if isinstance(_lowerCAmelCase	,  _lowerCAmelCase	):  # matrix-vector
															if len(_lowerCAmelCase	) == self.__width:
																				lowerCamelCase__       =       zero_vector(self.__height	)
																				for i in range(self.__height	):
																									lowerCamelCase__       =       [
																									    self.__matrix[i][j] * other.component(_lowerCAmelCase	)
																									    for j in range(self.__width	)
																									]
																									ans.change_component(_lowerCAmelCase	,  sum(_lowerCAmelCase	)	)
																				return ans
															else:
																				raise Exception(
																				    "vector must have the same size as the "
																				    "number of columns of the matrix!"	)
										elif isinstance(_lowerCAmelCase	,  (int, float)	):  # matrix-scalar
															lowerCamelCase__       =       [
															    [self.__matrix[i][j] * other for j in range(self.__width	)]
															    for i in range(self.__height	)
															]
															return Matrix(_lowerCAmelCase	,  self.__width	,  self.__height	)
										return None





					def 					__magic_name__ ( self	):
										return self.__height





					def 					__magic_name__ ( self	):
										return self.__width





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										if 0 <= x < self.__height and 0 <= y < self.__width:
															return self.__matrix[x][y]
										else:
															raise Exception("change_component: indices out of bounds"	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										if 0 <= x < self.__height and 0 <= y < self.__width:
															lowerCamelCase__       =       value
										else:
															raise Exception("change_component: indices out of bounds"	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										if self.__height != self.__width:
															raise Exception("Matrix is not square"	)
										lowerCamelCase__       =       self.__matrix[:x] + self.__matrix[x + 1 :]
										for i in range(len(_lowerCAmelCase	)	):
															lowerCamelCase__       =       minor[i][:y] + minor[i][y + 1 :]
										return Matrix(_lowerCAmelCase	,  self.__width - 1	,  self.__height - 1	).determinant()





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										if self.__height != self.__width:
															raise Exception("Matrix is not square"	)
										if 0 <= x < self.__height and 0 <= y < self.__width:
															return (-1) ** (x + y) * self.minor(_lowerCAmelCase	,  _lowerCAmelCase	)
										else:
															raise Exception("Indices out of bounds"	)






					def 					__magic_name__ ( self	):
										if self.__height != self.__width:
															raise Exception("Matrix is not square"	)
										if self.__height < 1:
															raise Exception("Matrix has no element"	)
										elif self.__height == 1:
															return self.__matrix[0][0]
										elif self.__height == 2:
															return (
															    self.__matrix[0][0] * self.__matrix[1][1]
															    - self.__matrix[0][1] * self.__matrix[1][0]
															)
										else:
															lowerCamelCase__       =       [
															    self.__matrix[0][y] * self.cofactor(0	,  _lowerCAmelCase	) for y in range(self.__width	)
															]
															return sum(_lowerCAmelCase	)





def    __UpperCamelCase      (    a) ->Matrix:
					lowerCamelCase__       =       [[0] * n for _ in range(a)]
					return Matrix(a,     a,     a)







def    __UpperCamelCase      (    a,     a,     a,     a) ->Matrix:
					random.seed(a)
					lowerCamelCase__       =       [
					    [random.randint(a,     a) for _ in range(a)] for _ in range(a)
					]
					return Matrix(a,     a,     a)

