

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  ,   A_  ,   A_=13  ,   A_=7  ,   A_=True  ,   A_=True  ,   A_=True  ,   A_=99  ,   A_=32  ,   A_=5  ,   A_=4  ,   A_=37  ,   A_="gelu"  ,   A_=0.1  ,   A_=0.1  ,   A_=512  ,   A_=16  ,   A_=2  ,   A_=0.02  ,   A_=3  ,   A_=4  ,   A_=None  ,   ):







										'''simple docstring'''


										_UpperCAmelCase    :						Any							=      parent
										_UpperCAmelCase    :						Optional[Any]							=      batch_size
										_UpperCAmelCase    :						Optional[int]							=      seq_length
										_UpperCAmelCase    :						Tuple							=      is_training
										_UpperCAmelCase    :						Union[str, Any]							=      use_token_type_ids
										_UpperCAmelCase    :						int							=      use_labels
										_UpperCAmelCase    :						Tuple							=      vocab_size
										_UpperCAmelCase    :						int							=      hidden_size
										_UpperCAmelCase    :						str							=      num_hidden_layers
										_UpperCAmelCase    :						str							=      num_attention_heads
										_UpperCAmelCase    :						Any							=      intermediate_size
										_UpperCAmelCase    :						Union[str, Any]							=      hidden_act
										_UpperCAmelCase    :						Optional[Any]							=      hidden_dropout_prob
										_UpperCAmelCase    :						Dict							=      attention_probs_dropout_prob
										_UpperCAmelCase    :						int							=      max_position_embeddings
										_UpperCAmelCase    :						List[str]							=      type_vocab_size
										_UpperCAmelCase    :						Optional[Any]							=      type_sequence_label_size
										_UpperCAmelCase    :						Tuple							=      initializer_range
										_UpperCAmelCase    :						List[str]							=      num_labels
										_UpperCAmelCase    :						str							=      num_choices
										_UpperCAmelCase    :						Optional[int]							=      scope
										_UpperCAmelCase    :						Union[str, Any]							=      self.vocab_size - 1


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						int							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.vocab_size		)

										_UpperCAmelCase    :						List[Any]							=      None
										if self.use_token_type_ids:
														_UpperCAmelCase    :						Union[str, Any]							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.type_vocab_size		)

										_UpperCAmelCase    :						Union[str, Any]							=      None
										_UpperCAmelCase    :						int							=      None
										_UpperCAmelCase    :						Optional[int]							=      None
										if self.use_labels:
														_UpperCAmelCase    :						int							=      ids_tensor([self.batch_size]  ,   self.type_sequence_label_size		)
														_UpperCAmelCase    :						str							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.num_labels		)
														_UpperCAmelCase    :						Optional[int]							=      ids_tensor([self.batch_size]  ,   self.num_choices		)

										_UpperCAmelCase    :						Dict							=      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  ,   )

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







										'''simple docstring'''


										_UpperCAmelCase    :						Dict							=      OpenAIGPTModel(config=A_		)
										model.to(A_		)
										model.eval()

										_UpperCAmelCase    :						Dict							=      model(A_  ,   token_type_ids=A_  ,   head_mask=A_		)
										_UpperCAmelCase    :						str							=      model(A_  ,   token_type_ids=A_		)
										_UpperCAmelCase    :						Any							=      model(A_		)

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


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   *A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Union[str, Any]							=      OpenAIGPTLMHeadModel(A_		)
										model.to(A_		)
										model.eval()

										_UpperCAmelCase    :						Optional[Any]							=      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     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   *A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						str							=      OpenAIGPTDoubleHeadsModel(A_		)
										model.to(A_		)
										model.eval()

										_UpperCAmelCase    :						int							=      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     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   *A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						int							=      self.num_labels
										_UpperCAmelCase    :						Dict							=      OpenAIGPTForSequenceClassification(A_		)
										model.to(A_		)
										model.eval()

										_UpperCAmelCase    :						Union[str, Any]							=      ids_tensor([self.batch_size]  ,   self.type_sequence_label_size		)
										_UpperCAmelCase    :						int							=      model(A_  ,   token_type_ids=A_  ,   labels=A_		)
										self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.num_labels)		)





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Tuple							=      self.prepare_config_and_inputs()
										(
										    (
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							(
										    _UpperCAmelCase
										)						,							
										)    :						Any							=      config_and_inputs
										_UpperCAmelCase    :						Dict							=      {
										    "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			):
						_lowercase        =     (
						    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
						    if is_torch_available()
						    else ()
						)
						_lowercase        =     (
						    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
						)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
						_lowercase        =     (
						    {
						        "feature-extraction": OpenAIGPTModel,
						        "text-classification": OpenAIGPTForSequenceClassification,
						        "text-generation": OpenAIGPTLMHeadModel,
						        "zero-shot": OpenAIGPTForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_		):







										'''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     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_=False		):







										'''simple docstring'''


										_UpperCAmelCase    :						Tuple							=      super()._prepare_for_class(A_  ,   A_  ,   return_labels=A_		)

										if return_labels:
														if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																		_UpperCAmelCase    :						Optional[Any]							=      torch.zeros(
																		    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)  ,   dtype=torch.long  ,   device=A_  ,   )
																		_UpperCAmelCase    :						List[str]							=      inputs_dict["labels"]
																		_UpperCAmelCase    :						Tuple							=      inputs_dict["labels"]
																		_UpperCAmelCase    :						int							=      torch.zeros(
																		    (self.model_tester.batch_size, self.model_tester.num_choices)  ,   dtype=torch.long  ,   device=A_  ,   )
																		_UpperCAmelCase    :						Tuple							=      torch.zeros(
																		    self.model_tester.batch_size  ,   dtype=torch.long  ,   device=A_		)
										return inputs_dict


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						str							=      OpenAIGPTModelTester(self		)
										_UpperCAmelCase    :						Union[str, Any]							=      ConfigTester(self  ,   config_class=A_  ,   n_embd=37		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										self.config_tester.run_common_tests()


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Any							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Union[str, Any]							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[str]							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A_		)





						@slow
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_UpperCAmelCase    :						Dict							=      OpenAIGPTModel.from_pretrained(A_		)
														self.assertIsNotNone(A_		)







@require_torch
class 			a					(		unittest.TestCase			):


						@slow
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"		)
										model.to(A_		)
										_UpperCAmelCase    :						Tuple							=      torch.tensor([[481, 4735, 544]]  ,   dtype=torch.long  ,   device=A_		)  # the president is
										_UpperCAmelCase    :						Optional[Any]							=      [
										    481,
										    4735,
										    544,
										    246,
										    963,
										    870,
										    762,
										    239,
										    244,
										    40477,
										    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

										_UpperCAmelCase    :						Optional[int]							=      model.generate(A_  ,   do_sample=A_		)
										self.assertListEqual(output_ids[0].tolist()  ,   A_		)





def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					str					, lowerCAmelCase:					str	)				->    bool:
				_UpperCAmelCase    :						Optional[Any]							=      len(lowerCAmelCase	) + 1
				_UpperCAmelCase    :						Optional[int]							=      len(lowerCAmelCase	) + 1

				# dp is a 2d matrix where dp[i][j] denotes whether prefix string of
				# length i of input_string matches with prefix string of length j of
				# given pattern.
				# "dp" stands for dynamic programming.
				_UpperCAmelCase    :						List[str]							=      [[0 for i in range(lowerCAmelCase	)] for j in range(lowerCAmelCase	)]

				# since string of zero length match pattern of zero length
				_UpperCAmelCase    :						List[Any]							=      1

				# since pattern of zero length will never match with string of non-zero length
				for i in range(1					, lowerCAmelCase	):
								_UpperCAmelCase    :						Dict							=      0

				# since string of zero length will match with pattern where there
				# is at least one * alternatively
				for j in range(1					, lowerCAmelCase	):
								_UpperCAmelCase    :						Tuple							=      dp[0][j - 2] if pattern[j - 1] == "*" else 0

				# now using bottom-up approach to find for all remaining lengths
				for i in range(1					, lowerCAmelCase	):
								for j in range(1					, lowerCAmelCase	):
												if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".":
																_UpperCAmelCase    :						Optional[Any]							=      dp[i - 1][j - 1]

												elif pattern[j - 1] == "*":
																if dp[i][j - 2] == 1:
																				_UpperCAmelCase    :						List[str]							=      1
																elif pattern[j - 2] in (input_string[i - 1], "."):
																				_UpperCAmelCase    :						str							=      dp[i - 1][j]
																else:
																				_UpperCAmelCase    :						int							=      0
												else:
																_UpperCAmelCase    :						List[Any]							=      0

				return bool(dp[-1][-1]	)


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					# inputing the strings
					# input_string = input("input a string :")
					# pattern = input("input a pattern :")

					SCREAMING_SNAKE_CASE_            = 'aab'
					SCREAMING_SNAKE_CASE_            = 'c*a*b'

					# using function to check whether given string matches the given pattern
					if match_pattern(input_string, pattern):
										print(F'''{input_string} matches the given pattern {pattern}''')
					else:
										print(F'''{input_string} does not match with the given pattern {pattern}''')


