

"""simple docstring"""

from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers.models.openai.modeling_tf_openai import (
        TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFOpenAIGPTDoubleHeadsModel,
        TFOpenAIGPTForSequenceClassification,
        TFOpenAIGPTLMHeadModel,
        TFOpenAIGPTModel,
    )






class  __a				:





       """simple docstring"""


       def __init__(      self ,				snake_case ,				):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							parent
        lowerCAmelCase__      :							Optional[int]    =							13
        lowerCAmelCase__      :							Tuple    =							7
        lowerCAmelCase__      :							Any    =							True
        lowerCAmelCase__      :							Optional[int]    =							True
        lowerCAmelCase__      :							Any    =							True
        lowerCAmelCase__      :							Any    =							True
        lowerCAmelCase__      :							Optional[Any]    =							True
        lowerCAmelCase__      :							int    =							99
        lowerCAmelCase__      :							List[str]    =							32
        lowerCAmelCase__      :							Optional[Any]    =							2
        lowerCAmelCase__      :							Optional[int]    =							4
        lowerCAmelCase__      :							Optional[Any]    =							37
        lowerCAmelCase__      :							str    =							"gelu"
        lowerCAmelCase__      :							int    =							0.1
        lowerCAmelCase__      :							Union[str, Any]    =							0.1
        lowerCAmelCase__      :							List[Any]    =							512
        lowerCAmelCase__      :							Optional[Any]    =							16
        lowerCAmelCase__      :							Dict    =							2
        lowerCAmelCase__      :							List[str]    =							0.02
        lowerCAmelCase__      :							Optional[int]    =							3
        lowerCAmelCase__      :							Optional[Any]    =							4
        lowerCAmelCase__      :							List[Any]    =							None
        lowerCAmelCase__      :							Optional[Any]    =							self.vocab_size - 1






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[Any]    =							ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size      )

        lowerCAmelCase__      :							Dict    =							None
        if self.use_input_mask:
         lowerCAmelCase__      :							Tuple    =							random_attention_mask([self.batch_size, self.seq_length]      )

        lowerCAmelCase__      :							Tuple    =							None
        if self.use_token_type_ids:
         lowerCAmelCase__      :							Optional[Any]    =							ids_tensor([self.batch_size, self.seq_length] ,				self.type_vocab_size      )

        lowerCAmelCase__      :							Optional[Any]    =							None
        if self.use_mc_token_ids:
         lowerCAmelCase__      :							Optional[Any]    =							ids_tensor([self.batch_size, self.num_choices] ,				self.seq_length      )

        lowerCAmelCase__      :							Optional[Any]    =							None
        lowerCAmelCase__      :							Dict    =							None
        lowerCAmelCase__      :							Tuple    =							None
        if self.use_labels:
         lowerCAmelCase__      :							Optional[Any]    =							ids_tensor([self.batch_size] ,				self.type_sequence_label_size      )
         lowerCAmelCase__      :							Any    =							ids_tensor([self.batch_size, self.seq_length] ,				self.num_labels      )
         lowerCAmelCase__      :							Dict    =							ids_tensor([self.batch_size] ,				self.num_choices      )

        lowerCAmelCase__      :							Optional[Any]    =							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[int]    =							ids_tensor([self.num_hidden_layers, self.num_attention_heads] ,				2      )

        return (
            config,
            input_ids,
            input_mask,
            head_mask,
            token_type_ids,
            mc_token_ids,
            sequence_labels,
            token_labels,
            choice_labels,
        )






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				*snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[str]    =							TFOpenAIGPTModel(config=snake_case      )
        lowerCAmelCase__      :							List[str]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        lowerCAmelCase__      :							Dict    =							model(snake_case      )

        lowerCAmelCase__      :							List[Any]    =							[input_ids, input_mask]
        lowerCAmelCase__      :							List[str]    =							model(snake_case      )

        lowerCAmelCase__      :							Optional[int]    =							model(snake_case      )

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






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				*snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							Dict    =							TFOpenAIGPTLMHeadModel(config=snake_case      )
        lowerCAmelCase__      :							Optional[int]    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        lowerCAmelCase__      :							Tuple    =							model(snake_case      )
        self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.vocab_size)      )






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				*snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[Any]    =							TFOpenAIGPTDoubleHeadsModel(config=snake_case      )

        lowerCAmelCase__      :							Union[str, Any]    =							tf.tile(tf.expand_dims(snake_case ,				1      ) ,				(1, self.num_choices, 1)      )
        lowerCAmelCase__      :							Optional[Any]    =							tf.tile(tf.expand_dims(snake_case ,				1      ) ,				(1, self.num_choices, 1)      )
        lowerCAmelCase__      :							Optional[Any]    =							tf.tile(tf.expand_dims(snake_case ,				1      ) ,				(1, self.num_choices, 1)      )

        lowerCAmelCase__      :							Any    =							{
            "input_ids": multiple_choice_inputs_ids,
            "mc_token_ids": mc_token_ids,
            "attention_mask": multiple_choice_input_mask,
            "token_type_ids": multiple_choice_token_type_ids,
        }
        lowerCAmelCase__      :							str    =							model(snake_case      )
        self.parent.assertEqual(
            result.logits.shape ,				(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)      )
        self.parent.assertEqual(result.mc_logits.shape ,				(self.batch_size, self.num_choices)      )






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				*snake_case      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							self.num_labels
        lowerCAmelCase__      :							Tuple    =							ids_tensor([self.batch_size] ,				self.type_sequence_label_size      )
        lowerCAmelCase__      :							List[Any]    =							{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
            "labels": sequence_labels,
        }
        lowerCAmelCase__      :							Union[str, Any]    =							TFOpenAIGPTForSequenceClassification(snake_case      )
        lowerCAmelCase__      :							Optional[Any]    =							model(snake_case      )
        self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Dict    =							self.prepare_config_and_inputs()

        (
            (
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	
        )      :							Any    =							config_and_inputs

        lowerCAmelCase__      :							List[Any]    =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict






@require_tf
class  __a				(	__magic_name__					,  __magic_name__					,  unittest.TestCase					):





       """simple docstring"""


       __UpperCamelCase				:			int          =	(
           (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
           if is_tf_available()
           else ()
       )
       __UpperCamelCase				:			Tuple          =	(
           (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
       )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
       __UpperCamelCase				:			str          =	(
           {
               'feature-extraction': TFOpenAIGPTModel,
               'text-classification': TFOpenAIGPTForSequenceClassification,
               'text-generation': TFOpenAIGPTLMHeadModel,
               'zero-shot': TFOpenAIGPTForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __UpperCamelCase				:			Tuple          =	False
       __UpperCamelCase				:			Optional[Any]          =	False






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case      ):





        """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  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Any    =							TFOpenAIGPTModelTester(self      )
        lowerCAmelCase__      :							Union[str, Any]    =							ConfigTester(self ,				config_class=snake_case ,				n_embd=37      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        self.config_tester.run_common_tests()






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[int]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Dict    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Any    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case      )






       @slow
       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowerCAmelCase__      :							List[Any]    =							TFOpenAIGPTModel.from_pretrained(snake_case      )
         self.assertIsNotNone(snake_case      )






@require_tf
class  __a				(	unittest.TestCase					):





       """simple docstring"""


       @slow
       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							str    =							TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"      )
        lowerCAmelCase__      :							Union[str, Any]    =							tf.convert_to_tensor([[481, 4_735, 544]] ,				dtype=tf.intaa      )  # the president is
        lowerCAmelCase__      :							str    =							[
            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

        lowerCAmelCase__      :							Tuple    =							model.generate(snake_case ,				do_sample=snake_case      )
        self.assertListEqual(output_ids[0].numpy().tolist() ,				snake_case      )









"""simple docstring"""

# A Bipartite Graph is a graph whose vertices can be divided into two independent sets,
# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.
from queue import Queue



def 			SCREAMING_SNAKE_CASE       (   lowercase__      ) ->	List[str]:
 lowerCAmelCase__      :							Tuple    =							Queue()
 lowerCAmelCase__      :							Optional[int]    =							[False] * len(lowercase__      )
 lowerCAmelCase__      :							Dict    =							[-1] * len(lowercase__      )

 def bfs():
  while not queue.empty():
   lowerCAmelCase__      :							Optional[Any]    =							queue.get()
   lowerCAmelCase__      :							Union[str, Any]    =							True

   for neighbour in graph[u]:
    if neighbour == u:
     return False

    if color[neighbour] == -1:
     lowerCAmelCase__      :							Any    =							1 - color[u]
     queue.put(lowercase__      )

    elif color[neighbour] == color[u]:
     return False

  return True

 for i in range(len(lowercase__      )      ):
  if not visited[i]:
   queue.put(lowercase__      )
   lowerCAmelCase__      :							int    =							0
   if bfs() is False:
    return False

 return True


if __name__ == "__main__":
    # Adjacency List of graph
    print(check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}))






