"""simple docstring"""






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

      def __init__(		self						,			__lowerCAmelCase						,			__lowerCAmelCase=13						,			__lowerCAmelCase=7						,			__lowerCAmelCase=True						,			__lowerCAmelCase=True						,			__lowerCAmelCase=True						,			__lowerCAmelCase=99						,			__lowerCAmelCase=32						,			__lowerCAmelCase=5						,			__lowerCAmelCase=4						,			__lowerCAmelCase=37						,			__lowerCAmelCase="gelu"						,			__lowerCAmelCase=0.1						,			__lowerCAmelCase=0.1						,			__lowerCAmelCase=512						,			__lowerCAmelCase=16						,			__lowerCAmelCase=2						,			__lowerCAmelCase=0.0_2						,			__lowerCAmelCase=3						,			__lowerCAmelCase=4						,			__lowerCAmelCase=None						,			):







            """simple docstring"""



            lowercase								=      parent
            lowercase								=      batch_size
            lowercase								=      seq_length
            lowercase								=      is_training
            lowercase								=      use_token_type_ids
            lowercase								=      use_labels
            lowercase								=      vocab_size
            lowercase								=      hidden_size
            lowercase								=      num_hidden_layers
            lowercase								=      num_attention_heads
            lowercase								=      intermediate_size
            lowercase								=      hidden_act
            lowercase								=      hidden_dropout_prob
            lowercase								=      attention_probs_dropout_prob
            lowercase								=      max_position_embeddings
            lowercase								=      type_vocab_size
            lowercase								=      type_sequence_label_size
            lowercase								=      initializer_range
            lowercase								=      num_labels
            lowercase								=      num_choices
            lowercase								=      scope
            lowercase								=      self.vocab_size - 1

      def 							A__			(		self		):







            """simple docstring"""



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

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

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

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

            lowercase								=      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 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			*__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      OpenAIGPTModel(config=a_		)
            model.to(a_		)
            model.eval()

            lowercase								=      model(a_						,			token_type_ids=a_						,			head_mask=a_		)
            lowercase								=      model(a_						,			token_type_ids=a_		)
            lowercase								=      model(a_		)

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







            """simple docstring"""



            lowercase								=      OpenAIGPTLMHeadModel(a_		)
            model.to(a_		)
            model.eval()

            lowercase								=      model(a_						,			token_type_ids=a_						,			labels=a_		)
            self.parent.assertEqual(result.loss.shape						,			()		)
            self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.vocab_size)		)

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			*__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      OpenAIGPTDoubleHeadsModel(a_		)
            model.to(a_		)
            model.eval()

            lowercase								=      model(a_						,			token_type_ids=a_						,			labels=a_		)
            self.parent.assertEqual(result.loss.shape						,			()		)
            self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.vocab_size)		)

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			*__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      self.num_labels
            lowercase								=      OpenAIGPTForSequenceClassification(a_		)
            model.to(a_		)
            model.eval()

            lowercase								=      ids_tensor([self.batch_size]						,			self.type_sequence_label_size		)
            lowercase								=      model(a_						,			token_type_ids=a_						,			labels=a_		)
            self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)		)







      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.prepare_config_and_inputs()
            (
                lowercase	
            )								=      config_and_inputs
            lowercase								=      {
                "input_ids": input_ids,
                "token_type_ids": token_type_ids,
                "head_mask": head_mask,
            }

            return config, inputs_dict






@require_torch
class 					_A		(			_UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase    ,       unittest.TestCase      ):
      snake_case__     :  Any				    =      (
          (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
          if is_torch_available()
          else ()
      )
      snake_case__     :  int				    =      (
          (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      snake_case__     :  Optional[Any]				    =      (
          {
              "feature-extraction": OpenAIGPTModel,
              "text-classification": OpenAIGPTForSequenceClassification,
              "text-generation": OpenAIGPTLMHeadModel,
              "zero-shot": OpenAIGPTForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """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 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase=False		):







            """simple docstring"""



            lowercase								=      super()._prepare_for_class(a_						,			a_						,			return_labels=a_		)

            if return_labels:
                  if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                        lowercase								=      torch.zeros(
                            (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)						,			dtype=torch.long						,			device=a_						,			)
                        lowercase								=      inputs_dict["labels"]
                        lowercase								=      inputs_dict["labels"]
                        lowercase								=      torch.zeros(
                            (self.model_tester.batch_size, self.model_tester.num_choices)						,			dtype=torch.long						,			device=a_						,			)
                        lowercase								=      torch.zeros(
                            self.model_tester.batch_size						,			dtype=torch.long						,			device=a_		)
            return inputs_dict

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      OpenAIGPTModelTester(self		)
            lowercase								=      ConfigTester(self						,			config_class=a_						,			n_embd=37		)

      def 							A__			(		self		):







            """simple docstring"""



            self.config_tester.run_common_tests()

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_model(*a_		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_lm_head_model(*a_		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_double_lm_head_model(*a_		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a_		)







      @slow
      def 							A__			(		self		):







            """simple docstring"""



            for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowercase								=      OpenAIGPTModel.from_pretrained(a_		)
                  self.assertIsNotNone(a_		)






@require_torch
class 					_A		(			unittest.TestCase      ):

      @slow
      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""		)
            model.to(a_		)
            lowercase								=      torch.tensor([[481, 4735, 544]]						,			dtype=torch.long						,			device=a_		)  # the president is
            lowercase								=      [
                481,
                4735,
                544,
                246,
                963,
                870,
                762,
                239,
                244,
                4_0477,
                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

            lowercase								=      model.generate(a_						,			do_sample=a_		)
            self.assertListEqual(output_ids[0].tolist()						,			a_		)










"""simple docstring"""






import functools






def      lowercase__ (					lowerCAmelCase__   :			str							,			lowerCAmelCase__   :			str			)    ->			int:



			'''simple docstring'''
			a__  :	Any		 =			len(lowerCAmelCase__			)
			a__  :	Optional[int]		 =			len(lowerCAmelCase__			)

			@functools.cache
			def min_distance(lowerCAmelCase__   :			int							,			lowerCAmelCase__   :			int			) -> int:
						# if first word index is overflow - delete all from the second word
						if indexa >= len_worda:
									return len_worda - indexa
						# if second word index is overflow - delete all from the first word
						if indexa >= len_worda:
									return len_worda - indexa
						a__  :	List[Any]		 =			int(worda[indexa] != worda[indexa]			)  # current letters not identical
						return min(
						    1 + min_distance(indexa + 1							,			lowerCAmelCase__			)							,			1 + min_distance(lowerCAmelCase__							,			indexa + 1			)							,			diff + min_distance(indexa + 1							,			indexa + 1			)							,			)

			return min_distance(0							,			0			)


if __name__ == "__main__":
		import doctest

		doctest.testmod()