



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






			"""simple docstring"""





			def __init__(				self	:       Union[str, Any]			,     lowercase	:       Tuple			,     lowercase	:       int=13			,     lowercase	:       Dict=7			,     lowercase	:       Optional[Any]=True			,     lowercase	:       str=True			,     lowercase	:       Any=True			,     lowercase	:       List[Any]=99			,     lowercase	:       List[str]=32			,     lowercase	:       str=5			,     lowercase	:       List[str]=4			,     lowercase	:       Tuple=37			,     lowercase	:       Union[str, Any]="gelu"			,     lowercase	:       Optional[int]=0.1			,     lowercase	:       Tuple=0.1			,     lowercase	:       List[str]=512			,     lowercase	:       Union[str, Any]=16			,     lowercase	:       str=2			,     lowercase	:       Tuple=0.02			,     lowercase	:       int=3			,     lowercase	:       Tuple=4			,     lowercase	:       Optional[Any]=None			,     )						->					List[Any]:
									"""simple docstring"""







									__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 		snake_case__			(				self	:       Union[str, Any]   )						->					int:
									"""simple docstring"""







									__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 		snake_case__			(				self	:       Any			,     lowercase	:       int			,     lowercase	:       Optional[Any]			,     lowercase	:       Any			,     lowercase	:       List[str]			,     *lowercase	:       Any   )						->					int:
									"""simple docstring"""







									__lowercase					=				OpenAIGPTModel(config=lowercase   )
									model.to(lowercase   )
									model.eval()

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

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





			def 		snake_case__			(				self	:       Tuple			,     lowercase	:       List[str]			,     lowercase	:       Tuple			,     lowercase	:       Any			,     lowercase	:       Optional[int]			,     *lowercase	:       str   )						->					List[Any]:
									"""simple docstring"""







									__lowercase					=				OpenAIGPTLMHeadModel(lowercase   )
									model.to(lowercase   )
									model.eval()

									__lowercase					=				model(lowercase			,     token_type_ids=lowercase			,     labels=lowercase   )
									self.parent.assertEqual(result.loss.shape			,     ()   )
									self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size)   )





			def 		snake_case__			(				self	:       List[str]			,     lowercase	:       Union[str, Any]			,     lowercase	:       List[Any]			,     lowercase	:       List[Any]			,     lowercase	:       List[str]			,     *lowercase	:       Dict   )						->					str:
									"""simple docstring"""







									__lowercase					=				OpenAIGPTDoubleHeadsModel(lowercase   )
									model.to(lowercase   )
									model.eval()

									__lowercase					=				model(lowercase			,     token_type_ids=lowercase			,     labels=lowercase   )
									self.parent.assertEqual(result.loss.shape			,     ()   )
									self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size)   )





			def 		snake_case__			(				self	:       List[Any]			,     lowercase	:       Dict			,     lowercase	:       Dict			,     lowercase	:       List[str]			,     lowercase	:       Any			,     *lowercase	:       List[str]   )						->					Any:
									"""simple docstring"""







									__lowercase					=				self.num_labels
									__lowercase					=				OpenAIGPTForSequenceClassification(lowercase   )
									model.to(lowercase   )
									model.eval()

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







			def 		snake_case__			(				self	:       str   )						->					Any:
									"""simple docstring"""







									__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 			_lowerCAmelCase   ( _UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					unittest.TestCase			):






			"""simple docstring"""





			lowercase__      :  Union[str, Any]      =			(
			    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
			    if is_torch_available()
			    else ()
			)
			lowercase__      :  Tuple      =			(
			    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			lowercase__      :  Optional[Any]      =			(
			    {
			        """feature-extraction""": OpenAIGPTModel,
			        """text-classification""": OpenAIGPTForSequenceClassification,
			        """text-generation""": OpenAIGPTLMHeadModel,
			        """zero-shot""": OpenAIGPTForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)





			def 		snake_case__			(				self	:       Any			,     lowercase	:       Union[str, Any]			,     lowercase	:       List[Any]			,     lowercase	:       Union[str, Any]			,     lowercase	:       List[Any]			,     lowercase	:       Tuple   )						->					Tuple:
									"""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 		snake_case__			(				self	:       Union[str, Any]			,     lowercase	:       Tuple			,     lowercase	:       Union[str, Any]			,     lowercase	:       Dict=False   )						->					int:
									"""simple docstring"""







									__lowercase					=				super()._prepare_for_class(lowercase			,     lowercase			,     return_labels=lowercase   )

									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=lowercase			,     )
																					__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=lowercase			,     )
																					__lowercase					=				torch.zeros(
																					    self.model_tester.batch_size			,     dtype=torch.long			,     device=lowercase   )
									return inputs_dict





			def 		snake_case__			(				self	:       Optional[Any]   )						->					Tuple:
									"""simple docstring"""







									__lowercase					=				OpenAIGPTModelTester(self   )
									__lowercase					=				ConfigTester(self			,     config_class=lowercase			,     n_embd=37   )





			def 		snake_case__			(				self	:       Tuple   )						->					str:
									"""simple docstring"""







									self.config_tester.run_common_tests()





			def 		snake_case__			(				self	:       Any   )						->					Any:
									"""simple docstring"""







									__lowercase					=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*lowercase   )





			def 		snake_case__			(				self	:       Optional[int]   )						->					Optional[int]:
									"""simple docstring"""







									__lowercase					=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*lowercase   )





			def 		snake_case__			(				self	:       str   )						->					int:
									"""simple docstring"""







									__lowercase					=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*lowercase   )





			def 		snake_case__			(				self	:       List[Any]   )						->					Optional[int]:
									"""simple docstring"""







									__lowercase					=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase   )







			@slow
			def 		snake_case__			(				self	:       Optional[Any]   )						->					List[str]:
									"""simple docstring"""







									for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__lowercase					=				OpenAIGPTModel.from_pretrained(lowercase   )
															self.assertIsNotNone(lowercase   )





@require_torch
class 			_lowerCAmelCase   ( unittest.TestCase			):






			"""simple docstring"""





			@slow
			def 		snake_case__			(				self	:       int   )						->					str:
									"""simple docstring"""







									__lowercase					=				OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""   )
									model.to(lowercase   )
									__lowercase					=				torch.tensor([[481, 4_735, 544]]			,     dtype=torch.long			,     device=lowercase   )  # the president is
									__lowercase					=				[
									    481,
									    4_735,
									    544,
									    246,
									    963,
									    870,
									    762,
									    239,
									    244,
									    40_477,
									    244,
									    249,
									    719,
									    881,
									    487,
									    544,
									    240,
									    244,
									    603,
									    481,
									]  # the president is a very good man. " \n " i\'m sure he is, " said the

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










import unittest

import numpy as np






def  UpperCAmelCase__    (    lowercase__						,       lowercase__						,       lowercase__						,       lowercase__ = None						,       ) ->      np.ndarray:
						__lowercase					=				np.shape(lowercase__   )
						__lowercase					=				np.shape(lowercase__   )
						__lowercase					=				np.shape(lowercase__   )

						if shape_a[0] != shape_b[0]:
												__lowercase					=				(
												    """Expected the same number of rows for A and B. """
												    F"Instead found A of size {shape_a} and B of size {shape_b}"
												)
												raise ValueError(lowercase__   )

						if shape_b[1] != shape_c[1]:
												__lowercase					=				(
												    """Expected the same number of columns for B and C. """
												    F"Instead found B of size {shape_b} and C of size {shape_c}"
												)
												raise ValueError(lowercase__   )

						__lowercase					=				pseudo_inv
						if a_inv is None:
												try:
																		__lowercase					=				np.linalg.inv(lowercase__   )
												except np.linalg.LinAlgError:
																		raise ValueError(
																		    """Input matrix A is not invertible. Cannot compute Schur complement."""   )

						return mat_c - mat_b.T @ a_inv @ mat_b
class 			_lowerCAmelCase   ( unittest.TestCase			):






			"""simple docstring"""





			def 		snake_case__			(				self	:       Dict   )						->					None:
									"""simple docstring"""







									__lowercase					=				np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]   )
									__lowercase					=				np.array([[0, 3], [3, 0], [2, 3]]   )
									__lowercase					=				np.array([[2, 1], [6, 3]]   )

									__lowercase					=				schur_complement(lowercase			,     lowercase			,     lowercase   )

									__lowercase					=				np.block([[a, b], [b.T, c]]   )

									__lowercase					=				np.linalg.det(lowercase   )
									__lowercase					=				np.linalg.det(lowercase   )
									__lowercase					=				np.linalg.det(lowercase   )

									self.assertAlmostEqual(lowercase			,     det_a * det_s   )





			def 		snake_case__			(				self	:       Tuple   )						->					None:
									"""simple docstring"""







									__lowercase					=				np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]   )
									__lowercase					=				np.array([[0, 3], [3, 0], [2, 3]]   )
									__lowercase					=				np.array([[2, 1], [6, 3]]   )

									with self.assertRaises(lowercase   ):
															schur_complement(lowercase			,     lowercase			,     lowercase   )







			def 		snake_case__			(				self	:       Tuple   )						->					None:
									"""simple docstring"""







									__lowercase					=				np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]   )
									__lowercase					=				np.array([[0, 3], [3, 0], [2, 3]]   )
									__lowercase					=				np.array([[2, 1, 3], [6, 3, 5]]   )

									with self.assertRaises(lowercase   ):
															schur_complement(lowercase			,     lowercase			,     lowercase   )


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			unittest.main()





