






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

       '''simple docstring'''



       def __init__(							self			,						a__			,						a__=13			,						a__=7			,						a__=True			,						a__=True			,						a__=True			,						a__=99			,						a__=32			,						a__=5			,						a__=4			,						a__=37			,						a__="gelu"			,						a__=0.1			,						a__=0.1			,						a__=5_12			,						a__=16			,						a__=2			,						a__=0.02			,						a__=3			,						a__=4			,						a__=None			,						) ->      Optional[Any]:






            '''simple docstring'''





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



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Tuple          =						ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size    )

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

            __snake_case			:Tuple          =						None
            __snake_case			:Optional[Any]          =						None
            __snake_case			:Any          =						None
            if self.use_labels:
                 __snake_case			:List[Any]          =						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			:int          =						ids_tensor([self.batch_size]			,						self.num_choices    )

            __snake_case			: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			,						)

            __snake_case			:List[str]          =						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 					__lowercase						(							self			,						a__			,						a__			,						a__			,						a__			,						*a__    ) ->      Tuple:






            '''simple docstring'''





            __snake_case			:List[Any]          =						OpenAIGPTModel(config=_SCREAMING_SNAKE_CASE    )
            model.to(_SCREAMING_SNAKE_CASE    )
            model.eval()

            __snake_case			:Dict          =						model(_SCREAMING_SNAKE_CASE			,						token_type_ids=_SCREAMING_SNAKE_CASE			,						head_mask=_SCREAMING_SNAKE_CASE    )
            __snake_case			:Dict          =						model(_SCREAMING_SNAKE_CASE			,						token_type_ids=_SCREAMING_SNAKE_CASE    )
            __snake_case			:Optional[int]          =						model(_SCREAMING_SNAKE_CASE    )

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



       def 					__lowercase						(							self			,						a__			,						a__			,						a__			,						a__			,						*a__    ) ->      int:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						OpenAIGPTLMHeadModel(_SCREAMING_SNAKE_CASE    )
            model.to(_SCREAMING_SNAKE_CASE    )
            model.eval()

            __snake_case			:Optional[int]          =						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 					__lowercase						(							self			,						a__			,						a__			,						a__			,						a__			,						*a__    ) ->      Optional[int]:






            '''simple docstring'''





            __snake_case			:Tuple          =						OpenAIGPTDoubleHeadsModel(_SCREAMING_SNAKE_CASE    )
            model.to(_SCREAMING_SNAKE_CASE    )
            model.eval()

            __snake_case			:int          =						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 					__lowercase						(							self			,						a__			,						a__			,						a__			,						a__			,						*a__    ) ->      Tuple:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						self.num_labels
            __snake_case			:int          =						OpenAIGPTForSequenceClassification(_SCREAMING_SNAKE_CASE    )
            model.to(_SCREAMING_SNAKE_CASE    )
            model.eval()

            __snake_case			:int          =						ids_tensor([self.batch_size]			,						self.type_sequence_label_size    )
            __snake_case			:str          =						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 					__lowercase						(							self    ) ->      List[Any]:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						self.prepare_config_and_inputs()
            (
                (
                __snake_case      
            )       , (
                __snake_case      
            )       , (
                __snake_case      
            )       , (
                __snake_case      
            )       , (
                __snake_case      
            )       , (
                __snake_case      
            )       , (
                __snake_case      
            )       ,       
            )			:Optional[int]          =						config_and_inputs
            __snake_case			:List[Any]          =						{
                """input_ids""": input_ids,
                """token_type_ids""": token_type_ids,
                """head_mask""": head_mask,
            }

            return config, inputs_dict




@require_torch
class 			snake_case__   (							lowerCAmelCase__	,    lowerCAmelCase__	,    lowerCAmelCase__	,    unittest.TestCase):

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



       def 					__lowercase						(							self			,						a__			,						a__			,						a__			,						a__			,						a__    ) ->      Union[str, Any]:






            '''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 					__lowercase						(							self			,						a__			,						a__			,						a__=False    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						super()._prepare_for_class(_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						return_labels=_SCREAMING_SNAKE_CASE    )

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



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Dict          =						OpenAIGPTModelTester(self    )
            __snake_case			:Optional[Any]          =						ConfigTester(self			,						config_class=_SCREAMING_SNAKE_CASE			,						n_embd=37    )



       def 					__lowercase						(							self    ) ->      List[Any]:






            '''simple docstring'''





            self.config_tester.run_common_tests()



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*_SCREAMING_SNAKE_CASE    )



       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Any          =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_lm_head_model(*_SCREAMING_SNAKE_CASE    )



       def 					__lowercase						(							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(*_SCREAMING_SNAKE_CASE    )



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Any          =						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_SCREAMING_SNAKE_CASE    )



       @slow
       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __snake_case			:List[str]          =						OpenAIGPTModel.from_pretrained(_SCREAMING_SNAKE_CASE    )
                 self.assertIsNotNone(_SCREAMING_SNAKE_CASE    )




@require_torch
class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



       @slow
       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Tuple          =						OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""    )
            model.to(_SCREAMING_SNAKE_CASE    )
            __snake_case			:Union[str, Any]          =						torch.tensor([[4_81, 47_35, 5_44]]			,						dtype=torch.long			,						device=_SCREAMING_SNAKE_CASE    )  # the president is
            __snake_case			:Union[str, Any]          =						[
                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

            __snake_case			:Union[str, Any]          =						model.generate(_SCREAMING_SNAKE_CASE			,						do_sample=_SCREAMING_SNAKE_CASE    )
            self.assertListEqual(output_ids[0].tolist()			,						_SCREAMING_SNAKE_CASE    )










from __future__ import annotations

from collections import deque
from collections.abc import Iterator
from dataclasses import dataclass




@dataclass
class 			snake_case__   :

       '''simple docstring'''
       lowerCamelCase							:				int
       lowerCamelCase							:				int




class 			snake_case__   :

       '''simple docstring'''



       def __init__(							self			,						a__    ) ->      Any:






            '''simple docstring'''





            __snake_case			:list[list[Edge]]          =						[[] for _ in range(a__    )]
            __snake_case			:List[str]          =						size



       def __getitem__(							self			,						a__    ) ->      Iterator[Edge]:






            '''simple docstring'''





            return iter(self._graph[vertex]    )



       @property
       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            return self._size



       def 					__lowercase						(							self			,						a__			,						a__			,						a__    ) ->      List[Any]:






            '''simple docstring'''





            if weight not in (0, 1):
                 raise ValueError("""Edge weight must be either 0 or 1."""    )

            if to_vertex < 0 or to_vertex >= self.size:
                 raise ValueError("""Vertex indexes must be in [0; size)."""    )

            self._graph[from_vertex].append(Edge(a__			,						a__    )    )



       def 					__lowercase						(							self			,						a__			,						a__    ) ->      int | None:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						deque([start_vertex]    )
            __snake_case			:list[int | None]          =						[None] * self.size
            __snake_case			:Tuple          =						0

            while queue:
                 __snake_case			:List[Any]          =						queue.popleft()
                 __snake_case			:Optional[Any]          =						distances[current_vertex]
                 if current_distance is None:
                      continue

                 for edge in self[current_vertex]:
                      __snake_case			:Optional[Any]          =						current_distance + edge.weight
                      __snake_case			:Dict          =						distances[edge.destination_vertex]
                      if (
                          isinstance(a__			,						a__    )
                          and new_distance >= dest_vertex_distance
                      ):
                           continue
                      __snake_case			:Dict          =						new_distance
                      if edge.weight == 0:
                           queue.appendleft(edge.destination_vertex    )
                      else:
                           queue.append(edge.destination_vertex    )

            if distances[finish_vertex] is None:
                 raise ValueError("""No path from start_vertex to finish_vertex."""    )

            return distances[finish_vertex]


if __name__ == "__main__":
     import doctest

     doctest.testmod()


