
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    _SCREAMING_SNAKE_CASE					:
      def __init__(     self						,		lowerCamelCase						,		):
          snake_case__  =			parent
          snake_case__  =			13
          snake_case__  =			7
          snake_case__  =			True
          snake_case__  =			True
          snake_case__  =			True
          snake_case__  =			True
          snake_case__  =			True
          snake_case__  =			99
          snake_case__  =			32
          snake_case__  =			2
          snake_case__  =			4
          snake_case__  =			37
          snake_case__  =			"gelu"
          snake_case__  =			0.1
          snake_case__  =			0.1
          snake_case__  =			5_12
          snake_case__  =			16
          snake_case__  =			2
          snake_case__  =			0.0_2
          snake_case__  =			3
          snake_case__  =			4
          snake_case__  =			None
          snake_case__  =			self.vocab_size - 1
      def A_     (     self	):
          snake_case__  =			ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size	)

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

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

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

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

          snake_case__  =			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__  =			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 A_     (     self						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		*lowerCamelCase	):
          snake_case__  =			TFOpenAIGPTModel(config=lowerCamelCase	)
          snake_case__  =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          snake_case__  =			model(lowerCamelCase	)

          snake_case__  =			[input_ids, input_mask]
          snake_case__  =			model(lowerCamelCase	)

          snake_case__  =			model(lowerCamelCase	)

          self.parent.assertEqual(result.last_hidden_state.shape						,		(self.batch_size, self.seq_length, self.hidden_size)	)
      def A_     (     self						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		*lowerCamelCase	):
          snake_case__  =			TFOpenAIGPTLMHeadModel(config=lowerCamelCase	)
          snake_case__  =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          snake_case__  =			model(lowerCamelCase	)
          self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.vocab_size)	)
      def A_     (     self						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		*lowerCamelCase	):
          snake_case__  =			TFOpenAIGPTDoubleHeadsModel(config=lowerCamelCase	)

          snake_case__  =			tf.tile(tf.expand_dims(lowerCamelCase						,		1	)						,		(1, self.num_choices, 1)	)
          snake_case__  =			tf.tile(tf.expand_dims(lowerCamelCase						,		1	)						,		(1, self.num_choices, 1)	)
          snake_case__  =			tf.tile(tf.expand_dims(lowerCamelCase						,		1	)						,		(1, self.num_choices, 1)	)

          snake_case__  =			{
              "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,
          }
          snake_case__  =			model(lowerCamelCase	)
          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 A_     (     self						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		*lowerCamelCase	):
          snake_case__  =			self.num_labels
          snake_case__  =			ids_tensor([self.batch_size]						,		self.type_sequence_label_size	)
          snake_case__  =			{
              "input_ids": input_ids,
              "attention_mask": input_mask,
              "token_type_ids": token_type_ids,
              "labels": sequence_labels,
          }
          snake_case__  =			TFOpenAIGPTForSequenceClassification(lowerCamelCase	)
          snake_case__  =			model(lowerCamelCase	)
          self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels)	)



      def A_     (     self	):
          snake_case__  =			self.prepare_config_and_inputs()

          (
              (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,   (
              snake_case__   
          )  ,      
          )  =			config_and_inputs

          snake_case__  =			{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict





@require_tf
class    _SCREAMING_SNAKE_CASE					( __UpperCamelCase    ,   __UpperCamelCase    ,   unittest.TestCase	):
      _A  :						int      		=       (
          (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
          if is_tf_available()
          else ()
      )
      _A  :						int      		=       (
          (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      _A  :						List[str]      		=       (
          {
              'feature-extraction': TFOpenAIGPTModel,
              'text-classification': TFOpenAIGPTForSequenceClassification,
              'text-generation': TFOpenAIGPTLMHeadModel,
              'zero-shot': TFOpenAIGPTForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      _A  :						List[Any]      		=       False
      _A  :						Tuple      		=       False
      def A_     (     self						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase						,		lowerCamelCase	):
          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 A_     (     self	):
          snake_case__  =			TFOpenAIGPTModelTester(self	)
          snake_case__  =			ConfigTester(self						,		config_class=lowerCamelCase						,		n_embd=37	)
      def A_     (     self	):
          self.config_tester.run_common_tests()
      def A_     (     self	):
          snake_case__  =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase	)
      def A_     (     self	):
          snake_case__  =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCamelCase	)
      def A_     (     self	):
          snake_case__  =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_double_head(*lowerCamelCase	)
      def A_     (     self	):
          snake_case__  =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase	)



      @slow
      def A_     (     self	):
          for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              snake_case__  =			TFOpenAIGPTModel.from_pretrained(lowerCamelCase	)
              self.assertIsNotNone(lowerCamelCase	)





@require_tf
class    _SCREAMING_SNAKE_CASE					( unittest.TestCase	):
      @slow
      def A_     (     self	):
          snake_case__  =			TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"	)
          snake_case__  =			tf.convert_to_tensor([[4_81, 47_35, 5_44]]						,		dtype=tf.intaa	)  # the president is
          snake_case__  =			[
              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__  =			model.generate(lowerCamelCase						,		do_sample=lowerCamelCase	)
          self.assertListEqual(output_ids[0].numpy().tolist()						,		lowerCamelCase	)




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__magic_name__           =					{
    '''configuration_convnextv2''': [
        '''CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''ConvNextV2Config''',
    ]
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __magic_name__           =					[
         '''CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''ConvNextV2ForImageClassification''',
         '''ConvNextV2Model''',
         '''ConvNextV2PreTrainedModel''',
         '''ConvNextV2Backbone''',
     ]


if TYPE_CHECKING:
     from .configuration_convnextva import (
         CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
         ConvNextVaConfig,
     )

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_convnextva import (
              CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST,
              ConvNextVaBackbone,
              ConvNextVaForImageClassification,
              ConvNextVaModel,
              ConvNextVaPreTrainedModel,
          )

else:
     import sys

     __magic_name__           =					_LazyModule(__name__, globals()['''__file__'''], _import_structure)



