



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





 """simple docstring"""



 def __init__(  self		,			a		,			)		->							Optional[int]:




   """simple docstring"""







   _A							=			parent
   _A							=			1_3
   _A							=			7
   _A							=			True
   _A							=			True
   _A							=			True
   _A							=			True
   _A							=			True
   _A							=			9_9
   _A							=			3_2
   _A							=			2
   _A							=			4
   _A							=			3_7
   _A							=			'gelu'
   _A							=			0.1
   _A							=			0.1
   _A							=			5_1_2
   _A							=			1_6
   _A							=			2
   _A							=			0.02
   _A							=			3
   _A							=			4
   _A							=			None
   _A							=			self.vocab_size - 1
 def lowercase_						(  self    )		->							List[str]:




   """simple docstring"""







   _A							=			ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size    )

   _A							=			None
   if self.use_input_mask:
     _A							=			random_attention_mask([self.batch_size, self.seq_length]    )

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

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

   _A							=			None
   _A							=			None
   _A							=			None
   if self.use_labels:
     _A							=			ids_tensor([self.batch_size]		,			self.type_sequence_label_size    )
     _A							=			ids_tensor([self.batch_size, self.seq_length]		,			self.num_labels    )
     _A							=			ids_tensor([self.batch_size]		,			self.num_choices    )

   _A							=			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		,			)

   _A							=			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 lowercase_						(  self		,			a		,			a		,			a		,			a		,			a		,			*a    )		->							Union[str, Any]:




   """simple docstring"""







   _A							=			TFOpenAIGPTModel(config=a    )
   _A							=			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
   _A							=			model(a    )

   _A							=			[input_ids, input_mask]
   _A							=			model(a    )

   _A							=			model(a    )

   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		,			*a    )		->							Union[str, Any]:




   """simple docstring"""







   _A							=			TFOpenAIGPTLMHeadModel(config=a    )
   _A							=			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
   _A							=			model(a    )
   self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.vocab_size)    )
 def lowercase_						(  self		,			a		,			a		,			a		,			a		,			a		,			a		,			*a    )		->							Tuple:




   """simple docstring"""







   _A							=			TFOpenAIGPTDoubleHeadsModel(config=a    )

   _A							=			tf.tile(tf.expand_dims(a		,			1    )		,			(1, self.num_choices, 1)    )
   _A							=			tf.tile(tf.expand_dims(a		,			1    )		,			(1, self.num_choices, 1)    )
   _A							=			tf.tile(tf.expand_dims(a		,			1    )		,			(1, self.num_choices, 1)    )

   _A							=			{
       '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,
   }
   _A							=			model(a    )
   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 lowercase_						(  self		,			a		,			a		,			a		,			a		,			a		,			*a    )		->							List[str]:




   """simple docstring"""







   _A							=			self.num_labels
   _A							=			ids_tensor([self.batch_size]		,			self.type_sequence_label_size    )
   _A							=			{
       'input_ids': input_ids,
       'attention_mask': input_mask,
       'token_type_ids': token_type_ids,
       'labels': sequence_labels,
   }
   _A							=			TFOpenAIGPTForSequenceClassification(a    )
   _A							=			model(a    )
   self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_labels)    )



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




   """simple docstring"""







   _A							=			self.prepare_config_and_inputs()

   (
       _A					
   )							=			config_and_inputs

   _A							=			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
   return config, inputs_dict







@require_tf
class 						_snake_case					(	a_    ,a_    ,unittest.TestCase ):





 """simple docstring"""



 lowerCamelCase_								=	(
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 lowerCamelCase_								=	(
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 lowerCamelCase_								=	(
     {
         'feature-extraction': TFOpenAIGPTModel,
         'text-classification': TFOpenAIGPTForSequenceClassification,
         'text-generation': TFOpenAIGPTLMHeadModel,
         'zero-shot': TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowerCamelCase_								=	False
 lowerCamelCase_								=	False
 def lowercase_						(  self		,			a		,			a		,			a		,			a		,			a    )		->							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 lowercase_						(  self    )		->							Optional[Any]:




   """simple docstring"""







   _A							=			TFOpenAIGPTModelTester(self    )
   _A							=			ConfigTester(self		,			config_class=a		,			n_embd=3_7    )
 def lowercase_						(  self    )		->							Tuple:




   """simple docstring"""







   self.config_tester.run_common_tests()
 def lowercase_						(  self    )		->							Union[str, Any]:




   """simple docstring"""







   _A							=			self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_openai_gpt_model(*a    )
 def lowercase_						(  self    )		->							Tuple:




   """simple docstring"""







   _A							=			self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_openai_gpt_lm_head(*a    )
 def lowercase_						(  self    )		->							List[Any]:




   """simple docstring"""







   _A							=			self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_openai_gpt_double_head(*a    )
 def lowercase_						(  self    )		->							Dict:




   """simple docstring"""







   _A							=			self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a    )



 @slow
 def lowercase_						(  self    )		->							Any:




   """simple docstring"""







   for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _A							=			TFOpenAIGPTModel.from_pretrained(a    )
     self.assertIsNotNone(a    )







@require_tf
class 						_snake_case					(	unittest.TestCase ):





 """simple docstring"""



 @slow
 def lowercase_						(  self    )		->							Any:




   """simple docstring"""







   _A							=			TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''    )
   _A							=			tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]		,			dtype=tf.intaa    )  # the president is
   _A							=			[
       4_8_1,
       4_7_3_5,
       5_4_4,
       2_4_6,
       9_6_3,
       8_7_0,
       7_6_2,
       2_3_9,
       2_4_4,
       4_0_4_7_7,
       2_4_4,
       2_4_9,
       7_1_9,
       8_8_1,
       4_8_7,
       5_4_4,
       2_4_0,
       2_4_4,
       6_0_3,
       4_8_1,
   ]  # the president is a very good man. " \n " i\'m sure he is, " said the

   _A							=			model.generate(a		,			do_sample=a    )
   self.assertListEqual(output_ids[0].numpy().tolist()		,			a    )
from typing import List

from ...processing_utils import ProcessorMixin
from ...utils import is_torch_available


if is_torch_available():
 import torch




class       __lowerCAmelCase     (				a_  ):



 '''simple docstring'''
 _lowerCamelCase  :		Dict 			=       ['image_processor', 'tokenizer']
 _lowerCamelCase  :		Tuple 			=       'OneFormerImageProcessor'
 _lowerCamelCase  :		Dict 			=       ('CLIPTokenizer', 'CLIPTokenizerFast')

 def __init__(	self				:						str				,	lowerCamelCase				:						Any=None				,	lowerCamelCase				:						List[Any]=None				,	lowerCamelCase				:						int = 7_7				,	lowerCamelCase				:						int = 7_7				,	**lowerCamelCase				:						Union[str, Any]     )       ->							Any:
        if image_processor is None:
               raise ValueError('You need to specify an `image_processor`.'     )
        if tokenizer is None:
               raise ValueError('You need to specify a `tokenizer`.'     )

        _UpperCamelCase   :				int       =     max_seq_length
        _UpperCamelCase   :				Optional[Any]       =     task_seq_length

        super().__init__(lowerCamelCase				,	lowerCamelCase     )

 def     __snake_case    (	self				:						List[str]				,	lowerCamelCase				:						int=None				,	lowerCamelCase				:						Optional[int]=7_7     )       ->							Tuple:
        if text_list is None:
               raise ValueError('tokens cannot be None.'     )

        _UpperCamelCase   :				Dict       =     self.tokenizer(lowerCamelCase				,	padding='max_length'				,	max_length=lowerCamelCase				,	truncation=lowerCamelCase     )

        _UpperCamelCase  ,					_UpperCamelCase   :				List[str]       =     tokens['attention_mask'], tokens['input_ids']

        _UpperCamelCase   :				Optional[int]       =     []
        for attn_mask, input_id in zip(lowerCamelCase				,	lowerCamelCase     ):
               _UpperCamelCase   :				int       =     torch.tensor(lowerCamelCase     ) * torch.tensor(lowerCamelCase     )
               token_inputs.append(token.unsqueeze(0     )     )

        _UpperCamelCase   :				Dict       =     torch.cat(lowerCamelCase				,	dim=0     )
        return token_inputs

 def __call__(	self				:						Union[str, Any]				,	lowerCamelCase				:						int=None				,	lowerCamelCase				:						str=None				,	lowerCamelCase				:						Any=None				,	**lowerCamelCase				:						List[Any]     )       ->							List[Any]:

        if task_inputs is None:
               raise ValueError('You have to specify the task_input. Found None.'     )
        elif images is None:
               raise ValueError('You have to specify the image. Found None.'     )

        if not all(task in ['semantic', 'instance', 'panoptic'] for task in task_inputs     ):
               raise ValueError('task_inputs must be semantic, instance, or panoptic.'     )

        _UpperCamelCase   :				Optional[Any]       =     self.image_processor(lowerCamelCase				,	lowerCamelCase				,	lowerCamelCase				,	**lowerCamelCase     )

        if isinstance(lowerCamelCase				,	lowerCamelCase     ):
               _UpperCamelCase   :				int       =     [task_inputs]

        if isinstance(lowerCamelCase				,	lowerCamelCase     ) and all(isinstance(lowerCamelCase				,	lowerCamelCase     ) for task_input in task_inputs     ):
               _UpperCamelCase   :				str       =     []
               for task in task_inputs:
                      _UpperCamelCase   :				Optional[Any]       =     f"the task is {task}"
                      task_token_inputs.append(lowerCamelCase     )
               _UpperCamelCase   :				Dict       =     self._preprocess_text(lowerCamelCase				,	max_length=self.task_seq_length     )
        else:
               raise TypeError('Task Inputs should be a string or a list of strings.'     )

        if hasattr(lowerCamelCase				,	'text_inputs'     ):
               _UpperCamelCase   :				str       =     encoded_inputs.text_inputs

               _UpperCamelCase   :				Optional[Any]       =     []
               for texts in texts_list:
                      _UpperCamelCase   :				Optional[int]       =     self._preprocess_text(lowerCamelCase				,	max_length=self.max_seq_length     )
                      text_inputs.append(text_input_list.unsqueeze(0     )     )

               _UpperCamelCase   :				Optional[int]       =     torch.cat(lowerCamelCase				,	dim=0     )

        return encoded_inputs

 def     __snake_case    (	self				:						List[str]				,	lowerCamelCase				:						str=None				,	lowerCamelCase				:						str=None				,	lowerCamelCase				:						int=None				,	**lowerCamelCase				:						List[str]     )       ->							Tuple:

        if task_inputs is None:
               raise ValueError('You have to specify the task_input. Found None.'     )
        elif images is None:
               raise ValueError('You have to specify the image. Found None.'     )

        if not all(task in ['semantic', 'instance', 'panoptic'] for task in task_inputs     ):
               raise ValueError('task_inputs must be semantic, instance, or panoptic.'     )

        _UpperCamelCase   :				Any       =     self.image_processor.encode_inputs(lowerCamelCase				,	lowerCamelCase				,	lowerCamelCase				,	**lowerCamelCase     )

        if isinstance(lowerCamelCase				,	lowerCamelCase     ):
               _UpperCamelCase   :				int       =     [task_inputs]

        if isinstance(lowerCamelCase				,	lowerCamelCase     ) and all(isinstance(lowerCamelCase				,	lowerCamelCase     ) for task_input in task_inputs     ):
               _UpperCamelCase   :				int       =     []
               for task in task_inputs:
                      _UpperCamelCase   :				List[str]       =     f"the task is {task}"
                      task_token_inputs.append(lowerCamelCase     )
               _UpperCamelCase   :				int       =     self._preprocess_text(lowerCamelCase				,	max_length=self.task_seq_length     )
        else:
               raise TypeError('Task Inputs should be a string or a list of strings.'     )

        if hasattr(lowerCamelCase				,	'text_inputs'     ):
               _UpperCamelCase   :				Optional[int]       =     encoded_inputs.text_inputs

               _UpperCamelCase   :				Any       =     []
               for texts in texts_list:
                      _UpperCamelCase   :				Any       =     self._preprocess_text(lowerCamelCase				,	max_length=self.max_seq_length     )
                      text_inputs.append(text_input_list.unsqueeze(0     )     )

               _UpperCamelCase   :				Optional[Any]       =     torch.cat(lowerCamelCase				,	dim=0     )

        return encoded_inputs

 def     __snake_case    (	self				:						Dict				,	*lowerCamelCase				:						Tuple				,	**lowerCamelCase				:						Any     )       ->							str:
        return self.image_processor.post_process_semantic_segmentation(*lowerCamelCase				,	**lowerCamelCase     )

 def     __snake_case    (	self				:						Union[str, Any]				,	*lowerCamelCase				:						Optional[int]				,	**lowerCamelCase				:						Any     )       ->							Any:
        return self.image_processor.post_process_instance_segmentation(*lowerCamelCase				,	**lowerCamelCase     )

 def     __snake_case    (	self				:						Dict				,	*lowerCamelCase				:						List[str]				,	**lowerCamelCase				:						int     )       ->							List[Any]:
        return self.image_processor.post_process_panoptic_segmentation(*lowerCamelCase				,	**lowerCamelCase     )



