


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					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				int=13    ,	SCREAMING_SNAKE_CASE_					:				List[Any]=7    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]=True    ,	SCREAMING_SNAKE_CASE_					:				int=True    ,	SCREAMING_SNAKE_CASE_					:				List[str]=True    ,	SCREAMING_SNAKE_CASE_					:				Dict=99    ,	SCREAMING_SNAKE_CASE_					:				Tuple=32    ,	SCREAMING_SNAKE_CASE_					:				Any=5    ,	SCREAMING_SNAKE_CASE_					:				str=4    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]=37    ,	SCREAMING_SNAKE_CASE_					:				List[str]="gelu"    ,	SCREAMING_SNAKE_CASE_					:				str=0.1    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]=0.1    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]=512    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]=16    ,	SCREAMING_SNAKE_CASE_					:				List[Any]=2    ,	SCREAMING_SNAKE_CASE_					:				int=0.02    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]=3    ,	SCREAMING_SNAKE_CASE_					:				int=4    ,	SCREAMING_SNAKE_CASE_					:				List[Any]=None    ,	):
        __lowerCamelCase:				Union[str, Any]     =	parent
        __lowerCamelCase:				Dict     =	batch_size
        __lowerCamelCase:				str     =	seq_length
        __lowerCamelCase:				Union[str, Any]     =	is_training
        __lowerCamelCase:				Tuple     =	use_token_type_ids
        __lowerCamelCase:				Union[str, Any]     =	use_labels
        __lowerCamelCase:				str     =	vocab_size
        __lowerCamelCase:				Optional[Any]     =	hidden_size
        __lowerCamelCase:				Tuple     =	num_hidden_layers
        __lowerCamelCase:				Optional[Any]     =	num_attention_heads
        __lowerCamelCase:				int     =	intermediate_size
        __lowerCamelCase:				Optional[Any]     =	hidden_act
        __lowerCamelCase:				Dict     =	hidden_dropout_prob
        __lowerCamelCase:				List[str]     =	attention_probs_dropout_prob
        __lowerCamelCase:				Optional[Any]     =	max_position_embeddings
        __lowerCamelCase:				str     =	type_vocab_size
        __lowerCamelCase:				List[Any]     =	type_sequence_label_size
        __lowerCamelCase:				Optional[int]     =	initializer_range
        __lowerCamelCase:				int     =	num_labels
        __lowerCamelCase:				Any     =	num_choices
        __lowerCamelCase:				Dict     =	scope
        __lowerCamelCase:				str     =	self.vocab_size - 1





     def  SCREAMING_SNAKE_CASE__       (    self					:				Tuple							):
        __lowerCamelCase:				List[str]     =	ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size							)

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

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

        __lowerCamelCase:				List[str]     =	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:				Optional[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  SCREAMING_SNAKE_CASE__       (    self					:				int    ,	SCREAMING_SNAKE_CASE_					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				str    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]    ,	*SCREAMING_SNAKE_CASE_					:				Tuple							):
        __lowerCamelCase:				str     =	OpenAIGPTModel(config=UpperCamelCase_							)
        model.to(UpperCamelCase_							)
        model.eval()

        __lowerCamelCase:				List[str]     =	model(UpperCamelCase_    ,	token_type_ids=UpperCamelCase_    ,	head_mask=UpperCamelCase_							)
        __lowerCamelCase:				Any     =	model(UpperCamelCase_    ,	token_type_ids=UpperCamelCase_							)
        __lowerCamelCase:				Union[str, Any]     =	model(UpperCamelCase_							)

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





     def  SCREAMING_SNAKE_CASE__       (    self					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	*SCREAMING_SNAKE_CASE_					:				Optional[Any]							):
        __lowerCamelCase:				Any     =	OpenAIGPTLMHeadModel(UpperCamelCase_							)
        model.to(UpperCamelCase_							)
        model.eval()

        __lowerCamelCase:				List[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  SCREAMING_SNAKE_CASE__       (    self					:				Any    ,	SCREAMING_SNAKE_CASE_					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				str    ,	SCREAMING_SNAKE_CASE_					:				Tuple    ,	*SCREAMING_SNAKE_CASE_					:				int							):
        __lowerCamelCase:				Any     =	OpenAIGPTDoubleHeadsModel(UpperCamelCase_							)
        model.to(UpperCamelCase_							)
        model.eval()

        __lowerCamelCase:				str     =	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  SCREAMING_SNAKE_CASE__       (    self					:				str    ,	SCREAMING_SNAKE_CASE_					:				str    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]    ,	*SCREAMING_SNAKE_CASE_					:				Optional[int]							):
        __lowerCamelCase:				Tuple     =	self.num_labels
        __lowerCamelCase:				int     =	OpenAIGPTForSequenceClassification(UpperCamelCase_							)
        model.to(UpperCamelCase_							)
        model.eval()

        __lowerCamelCase:				str     =	ids_tensor([self.batch_size]    ,	self.type_sequence_label_size							)
        __lowerCamelCase:				Tuple     =	model(UpperCamelCase_    ,	token_type_ids=UpperCamelCase_    ,	labels=UpperCamelCase_							)
        self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_labels)							)







     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				int     =	self.prepare_config_and_inputs()
        (
            __lowerCamelCase 
        ):				Union[str, Any]     =	config_and_inputs
        __lowerCamelCase:				List[str]     =	{
            "input_ids": input_ids,
            "token_type_ids": token_type_ids,
            "head_mask": head_mask,
        }

        return config, inputs_dict



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





     def  SCREAMING_SNAKE_CASE__       (    self					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				List[str]							):
        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  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				List[Any]=False							):
        __lowerCamelCase:				Union[str, Any]     =	super()._prepare_for_class(UpperCamelCase_    ,	UpperCamelCase_    ,	return_labels=UpperCamelCase_							)

        if return_labels:
           if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
              __lowerCamelCase:				List[str]     =	torch.zeros(
                  (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)    ,	dtype=torch.long    ,	device=UpperCamelCase_    ,	)
              __lowerCamelCase:				str     =	inputs_dict["labels"]
              __lowerCamelCase:				int     =	inputs_dict["labels"]
              __lowerCamelCase:				List[Any]     =	torch.zeros(
                  (self.model_tester.batch_size, self.model_tester.num_choices)    ,	dtype=torch.long    ,	device=UpperCamelCase_    ,	)
              __lowerCamelCase:				Any     =	torch.zeros(
                  self.model_tester.batch_size    ,	dtype=torch.long    ,	device=UpperCamelCase_							)
        return inputs_dict





     def  SCREAMING_SNAKE_CASE__       (    self					:				List[str]							):
        __lowerCamelCase:				Union[str, Any]     =	OpenAIGPTModelTester(self							)
        __lowerCamelCase:				Optional[int]     =	ConfigTester(self    ,	config_class=UpperCamelCase_    ,	n_embd=37							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				str							):
        self.config_tester.run_common_tests()





     def  SCREAMING_SNAKE_CASE__       (    self					:				Dict							):
        __lowerCamelCase:				int     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[Any]							):
        __lowerCamelCase:				Union[str, Any]     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*UpperCamelCase_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Dict							):
        __lowerCamelCase:				Tuple     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				Optional[Any]     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase_							)







     @slow
     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]							):
        for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:				Dict     =	OpenAIGPTModel.from_pretrained(UpperCamelCase_							)
           self.assertIsNotNone(UpperCamelCase_							)



@require_torch
class        a					(			unittest.TestCase   ):





     @slow
     def  SCREAMING_SNAKE_CASE__       (    self					:				Union[str, Any]							):
        __lowerCamelCase:				Any     =	OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""							)
        model.to(UpperCamelCase_							)
        __lowerCamelCase:				Any     =	torch.tensor([[481, 4735, 544]]    ,	dtype=torch.long    ,	device=UpperCamelCase_							)  # the president is
        __lowerCamelCase:				List[str]     =	[
            481,
            4735,
            544,
            246,
            963,
            870,
            762,
            239,
            244,
            40477,
            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:				Tuple     =	model.generate(UpperCamelCase_    ,	do_sample=UpperCamelCase_							)
        self.assertListEqual(output_ids[0].tolist()    ,	UpperCamelCase_							)






from __future__ import annotations

from typing import Any



class        a					:





     def __init__(    self					:				str    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				float = 0							):
        __lowerCamelCase   ,     __lowerCamelCase:				Dict     =	row, column
        __lowerCamelCase:				Optional[Any]     =	[[default_value for c in range(SCREAMING_SNAKE_CASE_							)] for r in range(SCREAMING_SNAKE_CASE_							)]





     def __str__(    self					:				Union[str, Any]							):
        __lowerCamelCase:				str     =	F'Matrix consist of {self.row} rows and {self.column} columns\n'

        # Make string identifier
        __lowerCamelCase:				Optional[int]     =	0
        for row_vector in self.array:
           for obj in row_vector:
              __lowerCamelCase:				int     =	max(SCREAMING_SNAKE_CASE_    ,	len(str(SCREAMING_SNAKE_CASE_							)							)							)
        __lowerCamelCase:				Union[str, Any]     =	F'%{max_element_length}s'

        # Make string and return
        def single_line(SCREAMING_SNAKE_CASE_					:				list[float]							) -> str:
           nonlocal string_format_identifier
           __lowerCamelCase:				Any     =	"""["""
           line += ", ".join(string_format_identifier % (obj,) for obj in row_vector							)
           line += "]"
           return line

        s += "\n".join(single_line(SCREAMING_SNAKE_CASE_							) for row_vector in self.array							)
        return s





     def __repr__(    self					:				Union[str, Any]							):
        return str(self							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Any    ,	SCREAMING_SNAKE_CASE_					:				tuple[int, int]							):
        if not (isinstance(SCREAMING_SNAKE_CASE_    ,	(list, tuple)							) and len(SCREAMING_SNAKE_CASE_							) == 2):
           return False
        elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column):
           return False
        else:
           return True





     def __getitem__(    self					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				tuple[int, int]							):
        assert self.validate_indicies(SCREAMING_SNAKE_CASE_							)
        return self.array[loc[0]][loc[1]]





     def __setitem__(    self					:				Any    ,	SCREAMING_SNAKE_CASE_					:				tuple[int, int]    ,	SCREAMING_SNAKE_CASE_					:				float							):
        assert self.validate_indicies(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				List[Any]     =	value





     def __add__(    self					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				Matrix							):
        assert isinstance(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							)
        assert self.row == another.row and self.column == another.column

        # Add
        __lowerCamelCase:				Any     =	Matrix(self.row    ,	self.column							)
        for r in range(self.row							):
           for c in range(self.column							):
              __lowerCamelCase:				Tuple     =	self[r, c] + another[r, c]
        return result





     def __neg__(    self					:				Any							):
        __lowerCamelCase:				List[str]     =	Matrix(self.row    ,	self.column							)
        for r in range(self.row							):
           for c in range(self.column							):
              __lowerCamelCase:				int     =	-self[r, c]
        return result





     def __sub__(    self					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				Matrix							):
        return self + (-another)





     def __mul__(    self					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				int | float | Matrix							):

        if isinstance(SCREAMING_SNAKE_CASE_    ,	(int, float)							):  # Scalar multiplication
           __lowerCamelCase:				List[str]     =	Matrix(self.row    ,	self.column							)
           for r in range(self.row							):
              for c in range(self.column							):
                 __lowerCamelCase:				List[Any]     =	self[r, c] * another
           return result
        elif isinstance(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							):  # Matrix multiplication
           assert self.column == another.row
           __lowerCamelCase:				Optional[int]     =	Matrix(self.row    ,	another.column							)
           for r in range(self.row							):
              for c in range(another.column							):
                 for i in range(self.column							):
                    result[r, c] += self[r, i] * another[i, c]
           return result
        else:
           __lowerCamelCase:				int     =	F'Unsupported type given for another ({type(SCREAMING_SNAKE_CASE_							)})'
           raise TypeError(SCREAMING_SNAKE_CASE_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Any							):
        __lowerCamelCase:				int     =	Matrix(self.column    ,	self.row							)
        for r in range(self.row							):
           for c in range(self.column							):
              __lowerCamelCase:				List[str]     =	self[r, c]
        return result







     def  SCREAMING_SNAKE_CASE__       (    self					:				int    ,	SCREAMING_SNAKE_CASE_					:				Matrix    ,	SCREAMING_SNAKE_CASE_					:				Matrix							):
        assert isinstance(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							) and isinstance(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							)
        assert self.row == self.column == u.row == v.row  # u, v should be column vector
        assert u.column == v.column == 1  # u, v should be column vector

        # Calculate
        __lowerCamelCase:				Optional[Any]     =	v.transpose()
        __lowerCamelCase:				Any     =	(v_t * self * u)[0, 0] + 1
        if numerator_factor == 0:
           return None  # It's not invertable
        return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor))


# Testing
if __name__ == "__main__":



   def     __lowerCAmelCase						(							)       ->							None:
      # a^(-1)
      __lowerCamelCase:				int     =	Matrix(3						,      3						,      0 )
      for i in range(3 ):
         __lowerCamelCase:				List[Any]     =	1
      print(f'a^(-1) is {ainv}' )
      # u, v
      __lowerCamelCase:				List[str]     =	Matrix(3						,      1						,      0 )
      __lowerCamelCase   ,     __lowerCamelCase   ,     __lowerCamelCase:				Optional[Any]     =	1, 2, -3
      __lowerCamelCase:				Dict     =	Matrix(3						,      1						,      0 )
      __lowerCamelCase   ,     __lowerCamelCase   ,     __lowerCamelCase:				Union[str, Any]     =	4, -2, 5
      print(f'u is {u}' )
      print(f'v is {v}' )
      print(f'uv^T is {u * v.transpose()}' )
      # Sherman Morrison
      print(f'(a + uv^T)^(-1) is {ainv.sherman_morrison(snake_case						,      snake_case )}' )



   def     __lowerCAmelCase						(							)       ->							None:
      import doctest

      doctest.testmod()







   testa()


