





"""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  _UpperCAmelCase       :


     def __init__( self      :Optional[Any]					,	__UpperCamelCase      :Union[str, Any]					,	):
            A		=  parent
            A		=  13
            A		=  7
            A		=  True
            A		=  True
            A		=  True
            A		=  True
            A		=  True
            A		=  99
            A		=  32
            A		=  2
            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
            A		=  self.vocab_size - 1


     def      lowerCamelCase	( self      :List[Any]		):
            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      lowerCamelCase	( self      :Tuple					,	__UpperCamelCase      :Optional[int]					,	__UpperCamelCase      :Any					,	__UpperCamelCase      :int					,	__UpperCamelCase      :Dict					,	__UpperCamelCase      :Any					,	*__UpperCamelCase      :List[str]		):
            A		=  TFOpenAIGPTModel(config=__UpperCamelCase		)
            A		=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            A		=  model(__UpperCamelCase		)

            A		=  [input_ids, input_mask]
            A		=  model(__UpperCamelCase		)

            A		=  model(__UpperCamelCase		)

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


     def      lowerCamelCase	( self      :Optional[Any]					,	__UpperCamelCase      :List[Any]					,	__UpperCamelCase      :Optional[Any]					,	__UpperCamelCase      :Any					,	__UpperCamelCase      :Tuple					,	__UpperCamelCase      :str					,	*__UpperCamelCase      :Any		):
            A		=  TFOpenAIGPTLMHeadModel(config=__UpperCamelCase		)
            A		=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            A		=  model(__UpperCamelCase		)
            self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.vocab_size)		)


     def      lowerCamelCase	( self      :Tuple					,	__UpperCamelCase      :int					,	__UpperCamelCase      :Dict					,	__UpperCamelCase      :List[str]					,	__UpperCamelCase      :Any					,	__UpperCamelCase      :int					,	__UpperCamelCase      :List[Any]					,	*__UpperCamelCase      :Dict		):
            A		=  TFOpenAIGPTDoubleHeadsModel(config=__UpperCamelCase		)

            A		=  tf.tile(tf.expand_dims(__UpperCamelCase					,	1		)					,	(1, self.num_choices, 1)		)
            A		=  tf.tile(tf.expand_dims(__UpperCamelCase					,	1		)					,	(1, self.num_choices, 1)		)
            A		=  tf.tile(tf.expand_dims(__UpperCamelCase					,	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(__UpperCamelCase		)
            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      lowerCamelCase	( self      :Dict					,	__UpperCamelCase      :Optional[int]					,	__UpperCamelCase      :str					,	__UpperCamelCase      :Tuple					,	__UpperCamelCase      :Any					,	__UpperCamelCase      :Any					,	*__UpperCamelCase      :str		):
            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(__UpperCamelCase		)
            A		=  model(__UpperCamelCase		)
            self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_labels)		)


     def      lowerCamelCase	( self      :Tuple		):
            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  _UpperCAmelCase       (     _lowerCamelCase					,							_lowerCamelCase					,							unittest.TestCase			):
     UpperCamelCase								=							(
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     UpperCamelCase								=							(
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     UpperCamelCase								=							(
         {
             '''feature-extraction''': TFOpenAIGPTModel,
             '''text-classification''': TFOpenAIGPTForSequenceClassification,
             '''text-generation''': TFOpenAIGPTLMHeadModel,
             '''zero-shot''': TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     UpperCamelCase								=							False
     UpperCamelCase								=							False


     def      lowerCamelCase	( self      :Tuple					,	__UpperCamelCase      :Tuple					,	__UpperCamelCase      :int					,	__UpperCamelCase      :Optional[Any]					,	__UpperCamelCase      :Tuple					,	__UpperCamelCase      :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      lowerCamelCase	( self      :Optional[Any]		):
            A		=  TFOpenAIGPTModelTester(self		)
            A		=  ConfigTester(self					,	config_class=__UpperCamelCase					,	n_embd=37		)


     def      lowerCamelCase	( self      :Dict		):
            self.config_tester.run_common_tests()


     def      lowerCamelCase	( self      :int		):
            A		=  self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*__UpperCamelCase		)


     def      lowerCamelCase	( self      :Any		):
            A		=  self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_lm_head(*__UpperCamelCase		)


     def      lowerCamelCase	( self      :List[Any]		):
            A		=  self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_double_head(*__UpperCamelCase		)


     def      lowerCamelCase	( self      :Tuple		):
            A		=  self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCamelCase		)


     @slow
     def      lowerCamelCase	( self      :Optional[int]		):







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







@require_tf
class  _UpperCAmelCase       (     unittest.TestCase			):


     @slow
     def      lowerCamelCase	( self      :List[str]		):
            A		=  TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"		)
            A		=  tf.convert_to_tensor([[4_81, 47_35, 5_44]]					,	dtype=tf.intaa		)  # the president is
            A		=  [
                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

            A		=  model.generate(__UpperCamelCase					,	do_sample=__UpperCamelCase		)
            self.assertListEqual(output_ids[0].numpy().tolist()					,	__UpperCamelCase		)









"""simple docstring"""
import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType






class  _UpperCAmelCase       (     lowercase_			):
					UpperCamelCase								=							['''image_processor''', '''tokenizer''']
					UpperCamelCase								=							'''LayoutLMv2ImageProcessor'''
					UpperCamelCase								=							('''LayoutLMv2Tokenizer''', '''LayoutLMv2TokenizerFast''')


					def __init__( self      :List[Any]					,	__UpperCamelCase      :List[Any]=None					,	__UpperCamelCase      :Tuple=None					,	**__UpperCamelCase      :int		):
												A		=  None
												if "feature_extractor" in kwargs:
																			warnings.warn(
																			    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
																			    " instead."					,	__UpperCamelCase					,	)
																			A		=  kwargs.pop("feature_extractor"		)

												A		=  image_processor if image_processor is not None else feature_extractor
												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`."		)

												super().__init__(__UpperCamelCase					,	__UpperCamelCase		)


					def __call__( self      :Optional[Any]					,	__UpperCamelCase      :int					,	__UpperCamelCase      :Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None					,	__UpperCamelCase      :Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None					,	__UpperCamelCase      :Union[List[List[int]], List[List[List[int]]]] = None					,	__UpperCamelCase      :Optional[Union[List[int], List[List[int]]]] = None					,	__UpperCamelCase      :bool = True					,	__UpperCamelCase      :Union[bool, str, PaddingStrategy] = False					,	__UpperCamelCase      :Union[bool, str, TruncationStrategy] = False					,	__UpperCamelCase      :Optional[int] = None					,	__UpperCamelCase      :int = 0					,	__UpperCamelCase      :Optional[int] = None					,	__UpperCamelCase      :Optional[bool] = None					,	__UpperCamelCase      :Optional[bool] = None					,	__UpperCamelCase      :bool = False					,	__UpperCamelCase      :bool = False					,	__UpperCamelCase      :bool = False					,	__UpperCamelCase      :bool = False					,	__UpperCamelCase      :bool = True					,	__UpperCamelCase      :Optional[Union[str, TensorType]] = None					,	**__UpperCamelCase      :int					,	):
												# verify input
												if self.image_processor.apply_ocr and (boxes is not None):
																			raise ValueError(
																			    "You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True."		)

												if self.image_processor.apply_ocr and (word_labels is not None):
																			raise ValueError(
																			    "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."		)

												if return_overflowing_tokens is True and return_offsets_mapping is False:
																			raise ValueError("You cannot return overflowing tokens without returning the offsets mapping."		)

												# first, apply the image processor
												A		=  self.image_processor(images=__UpperCamelCase					,	return_tensors=__UpperCamelCase		)

												# second, apply the tokenizer
												if text is not None and self.image_processor.apply_ocr and text_pair is None:
																			if isinstance(__UpperCamelCase					,	__UpperCamelCase		):
																										A		=  [text]  # add batch dimension (as the image processor always adds a batch dimension)
																			A		=  features["words"]

												A		=  self.tokenizer(
												    text=text if text is not None else features["words"]					,	text_pair=text_pair if text_pair is not None else None					,	boxes=boxes if boxes is not None else features["boxes"]					,	word_labels=__UpperCamelCase					,	add_special_tokens=__UpperCamelCase					,	padding=__UpperCamelCase					,	truncation=__UpperCamelCase					,	max_length=__UpperCamelCase					,	stride=__UpperCamelCase					,	pad_to_multiple_of=__UpperCamelCase					,	return_token_type_ids=__UpperCamelCase					,	return_attention_mask=__UpperCamelCase					,	return_overflowing_tokens=__UpperCamelCase					,	return_special_tokens_mask=__UpperCamelCase					,	return_offsets_mapping=__UpperCamelCase					,	return_length=__UpperCamelCase					,	verbose=__UpperCamelCase					,	return_tensors=__UpperCamelCase					,	**__UpperCamelCase					,	)

												# add pixel values
												A		=  features.pop("pixel_values"		)
												if return_overflowing_tokens is True:
																			A		=  self.get_overflowing_images(__UpperCamelCase					,	encoded_inputs["overflow_to_sample_mapping"]		)
												A		=  images

												return encoded_inputs


					def      lowerCamelCase	( self      :Union[str, Any]					,	__UpperCamelCase      :List[str]					,	__UpperCamelCase      :List[str]		):
												# in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
												A		=  []
												for sample_idx in overflow_to_sample_mapping:
																			images_with_overflow.append(images[sample_idx]		)

												if len(__UpperCamelCase		) != len(__UpperCamelCase		):
																			raise ValueError(
																			    "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
																			    f" {len(__UpperCamelCase		)} and {len(__UpperCamelCase		)}"		)

												return images_with_overflow


					def      lowerCamelCase	( self      :Tuple					,	*__UpperCamelCase      :Union[str, Any]					,	**__UpperCamelCase      :Tuple		):
												return self.tokenizer.batch_decode(*__UpperCamelCase					,	**__UpperCamelCase		)


					def      lowerCamelCase	( self      :Union[str, Any]					,	*__UpperCamelCase      :str					,	**__UpperCamelCase      :List[str]		):
												return self.tokenizer.decode(*__UpperCamelCase					,	**__UpperCamelCase		)


					@property
					def      lowerCamelCase	( self      :str		):
												return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]


					@property
					def      lowerCamelCase	( self      :str		):
												warnings.warn(
												    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."					,	__UpperCamelCase					,	)
												return self.image_processor_class


					@property
					def      lowerCamelCase	( self      :int		):
												warnings.warn(
												    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."					,	__UpperCamelCase					,	)
												return self.image_processor


