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


 '''simple docstring'''






 def __init__(      self   :	List[str]      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	int=13      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=7      ,       SCREAMING_SNAKE_CASE__   :	List[Any]=True      ,       SCREAMING_SNAKE_CASE__   :	Any=True      ,       SCREAMING_SNAKE_CASE__   :	str=True      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=99      ,       SCREAMING_SNAKE_CASE__   :	Dict=32      ,       SCREAMING_SNAKE_CASE__   :	int=5      ,       SCREAMING_SNAKE_CASE__   :	Tuple=4      ,       SCREAMING_SNAKE_CASE__   :	Tuple=37      ,       SCREAMING_SNAKE_CASE__   :	Tuple="gelu"      ,       SCREAMING_SNAKE_CASE__   :	int=0.1      ,       SCREAMING_SNAKE_CASE__   :	int=0.1      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=5_12      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=16      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]=2      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]=0.0_2      ,       SCREAMING_SNAKE_CASE__   :	Any=3      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]=4      ,       SCREAMING_SNAKE_CASE__   :	str=None      ,       )  ->    int:
  __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 a					(      self   :	Tuple							)  ->    Any:
  __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 a					(      self   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Tuple      ,       SCREAMING_SNAKE_CASE__   :	str      ,       SCREAMING_SNAKE_CASE__   :	str      ,       *SCREAMING_SNAKE_CASE__   :	Optional[int]							)  ->    str:
  __lowerCAmelCase											=						OpenAIGPTModel(config=SCREAMING_SNAKE_CASE__							)
  model.to(SCREAMING_SNAKE_CASE__							)
  model.eval()

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

  self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)							)
 def a					(      self   :	str      ,       SCREAMING_SNAKE_CASE__   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	Tuple      ,       SCREAMING_SNAKE_CASE__   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	int      ,       *SCREAMING_SNAKE_CASE__   :	List[Any]							)  ->    List[Any]:
  __lowerCAmelCase											=						OpenAIGPTLMHeadModel(SCREAMING_SNAKE_CASE__							)
  model.to(SCREAMING_SNAKE_CASE__							)
  model.eval()

  __lowerCAmelCase											=						model(SCREAMING_SNAKE_CASE__      ,       token_type_ids=SCREAMING_SNAKE_CASE__      ,       labels=SCREAMING_SNAKE_CASE__							)
  self.parent.assertEqual(result.loss.shape      ,       ()							)
  self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)							)
 def a					(      self   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Any      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]      ,       SCREAMING_SNAKE_CASE__   :	List[str]      ,       *SCREAMING_SNAKE_CASE__   :	Dict							)  ->    Dict:
  __lowerCAmelCase											=						OpenAIGPTDoubleHeadsModel(SCREAMING_SNAKE_CASE__							)
  model.to(SCREAMING_SNAKE_CASE__							)
  model.eval()

  __lowerCAmelCase											=						model(SCREAMING_SNAKE_CASE__      ,       token_type_ids=SCREAMING_SNAKE_CASE__      ,       labels=SCREAMING_SNAKE_CASE__							)
  self.parent.assertEqual(result.loss.shape      ,       ()							)
  self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)							)
 def a					(      self   :	str      ,       SCREAMING_SNAKE_CASE__   :	str      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]      ,       SCREAMING_SNAKE_CASE__   :	str      ,       *SCREAMING_SNAKE_CASE__   :	Any							)  ->    int:
  __lowerCAmelCase											=						self.num_labels
  __lowerCAmelCase											=						OpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE__							)
  model.to(SCREAMING_SNAKE_CASE__							)
  model.eval()

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





 def a					(      self   :	Dict							)  ->    int:
  __lowerCAmelCase											=						self.prepare_config_and_inputs()
  (
      (
      __lowerCAmelCase
  )							,       (
      __lowerCAmelCase
  )							,       (
      __lowerCAmelCase
  )							,       (
      __lowerCAmelCase
  )							,       (
      __lowerCAmelCase
  )							,       (
      __lowerCAmelCase
  )							,       (
      __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 				_lowercase     (				UpperCAmelCase__  ,							UpperCAmelCase__  ,							UpperCAmelCase__  ,							unittest.TestCase			):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE     :       List[str]      	=					(
     (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
     if is_torch_available()
     else ()
 )
 _SCREAMING_SNAKE_CASE     :       Any      	=					(
     (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 _SCREAMING_SNAKE_CASE     :       str      	=					(
     {
         """feature-extraction""": OpenAIGPTModel,
         """text-classification""": OpenAIGPTForSequenceClassification,
         """text-generation""": OpenAIGPTLMHeadModel,
         """zero-shot""": OpenAIGPTForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 def a					(      self   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	str      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]      ,       SCREAMING_SNAKE_CASE__   :	List[str]      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	List[str]							)  ->    List[Any]:
  if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
   # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
   # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
   # tiny config could not be created.
   return True

  return False
 def a					(      self   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	Any      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]      ,       SCREAMING_SNAKE_CASE__   :	Any=False							)  ->    Any:
  __lowerCAmelCase											=						super()._prepare_for_class(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__      ,       return_labels=SCREAMING_SNAKE_CASE__							)

  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=SCREAMING_SNAKE_CASE__      ,       )
    __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=SCREAMING_SNAKE_CASE__      ,       )
    __lowerCAmelCase											=						torch.zeros(
        self.model_tester.batch_size      ,       dtype=torch.long      ,       device=SCREAMING_SNAKE_CASE__							)
  return inputs_dict
 def a					(      self   :	Union[str, Any]							)  ->    List[Any]:
  __lowerCAmelCase											=						OpenAIGPTModelTester(self							)
  __lowerCAmelCase											=						ConfigTester(self      ,       config_class=SCREAMING_SNAKE_CASE__      ,       n_embd=37							)
 def a					(      self   :	List[str]							)  ->    int:
  self.config_tester.run_common_tests()
 def a					(      self   :	Tuple							)  ->    Dict:
  __lowerCAmelCase											=						self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE__							)
 def a					(      self   :	str							)  ->    Tuple:
  __lowerCAmelCase											=						self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_lm_head_model(*SCREAMING_SNAKE_CASE__							)
 def a					(      self   :	Optional[Any]							)  ->    Any:
  __lowerCAmelCase											=						self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_double_lm_head_model(*SCREAMING_SNAKE_CASE__							)
 def a					(      self   :	Dict							)  ->    Union[str, Any]:
  __lowerCAmelCase											=						self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE__							)





 @slow
 def a					(      self   :	Union[str, Any]							)  ->    int:
  for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   __lowerCAmelCase											=						OpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)







@require_torch
class 				_lowercase     (				unittest.TestCase			):


 '''simple docstring'''






 @slow
 def a					(      self   :	Optional[int]							)  ->    str:
  __lowerCAmelCase											=						OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""							)
  model.to(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						torch.tensor([[4_81, 47_35, 5_44]]      ,       dtype=torch.long      ,       device=SCREAMING_SNAKE_CASE__							)  # the president is
  __lowerCAmelCase											=						[
      4_81,
      47_35,
      5_44,
      2_46,
      9_63,
      8_70,
      7_62,
      2_39,
      2_44,
      4_04_77,
      2_44,
      2_49,
      7_19,
      8_81,
      4_87,
      5_44,
      2_40,
      2_44,
      6_03,
      4_81,
  ]  # the president is a very good man. " \n " i\'m sure he is, " said the

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







'''simple docstring'''

def      UpperCamelCase_						(					snake_case_       :       list[int]						,		snake_case_       :       list[int]      )					->     tuple[float, float]:






 '''simple docstring'''

 if not len(snake_case_      ) == len(snake_case_      ) == 3:
  raise ValueError("""Please enter a valid equation."""      )
 if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0:
  raise ValueError("""Both a & b of two equations can't be zero."""      )

 # Extract the coefficients
 __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase											=						equationa
 __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase											=						equationa

 # Calculate the determinants of the matrices
 __lowerCAmelCase											=						aa * ba - aa * ba
 __lowerCAmelCase											=						ca * ba - ca * ba
 __lowerCAmelCase											=						aa * ca - aa * ca

 # Check if the system of linear equations has a solution (using Cramer's rule)
 if determinant == 0:
  if determinant_x == determinant_y == 0:
   raise ValueError("""Infinite solutions. (Consistent system)"""      )
  else:
   raise ValueError("""No solution. (Inconsistent system)"""      )
 else:
  if determinant_x == determinant_y == 0:
   # Trivial solution (Inconsistent system)
   return (0.0, 0.0)
  else:
   __lowerCAmelCase											=						determinant_x / determinant
   __lowerCAmelCase											=						determinant_y / determinant
   # Non-Trivial Solution (Consistent system)
   return (x, y)






