


import gc
import unittest

from transformers import CTRLConfig, 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, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
			    CTRLForSequenceClassification,
			    CTRLLMHeadModel,
			    CTRLModel,
			)


class        _a							:

							def __init__(							self:	int    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	List[str]=14    ,						UpperCamelCase_:	Optional[Any]=7    ,						UpperCamelCase_:	Any=True    ,						UpperCamelCase_:	Tuple=True    ,						UpperCamelCase_:	str=True    ,						UpperCamelCase_:	Dict=True    ,						UpperCamelCase_:	Any=True    ,						UpperCamelCase_:	Optional[Any]=99    ,						UpperCamelCase_:	int=32    ,						UpperCamelCase_:	Tuple=5    ,						UpperCamelCase_:	str=4    ,						UpperCamelCase_:	str=37    ,						UpperCamelCase_:	Any="gelu"    ,						UpperCamelCase_:	int=0.1    ,						UpperCamelCase_:	Optional[Any]=0.1    ,						UpperCamelCase_:	Optional[Any]=512    ,						UpperCamelCase_:	Dict=16    ,						UpperCamelCase_:	Dict=2    ,						UpperCamelCase_:	Any=0.02    ,						UpperCamelCase_:	Any=3    ,						UpperCamelCase_:	Dict=4    ,						UpperCamelCase_:	int=None    ,						)			->   str:





									"""simple docstring"""







									lowercase__            =      parent
									lowercase__            =      batch_size
									lowercase__            =      seq_length
									lowercase__            =      is_training
									lowercase__            =      use_token_type_ids
									lowercase__            =      use_input_mask
									lowercase__            =      use_labels
									lowercase__            =      use_mc_token_ids
									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       lowerCamelCase_ (							self:	Union[str, Any]							)			->   str:





									"""simple docstring"""







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

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

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

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

									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__            =      self.get_config()

									lowercase__            =      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:	Dict							)			->   int:





									"""simple docstring"""







									return CTRLConfig(
									    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    ,						)

							def       lowerCamelCase_ (							self:	int    ,						UpperCamelCase_:	Tuple    ,						UpperCamelCase_:	List[str]    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	Optional[int]    ,						*UpperCamelCase_:	Any							)			->   Dict:





									"""simple docstring"""







									lowercase__            =      CTRLModel(config=UpperCamelCase_							)
									model.to(UpperCamelCase_							)
									model.eval()

									model(UpperCamelCase_    ,						token_type_ids=UpperCamelCase_    ,						head_mask=UpperCamelCase_							)
									model(UpperCamelCase_    ,						token_type_ids=UpperCamelCase_							)
									lowercase__            =      model(UpperCamelCase_							)
									self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.hidden_size)							)
									self.parent.assertEqual(len(result.past_key_values							)    ,						config.n_layer							)

							def       lowerCamelCase_ (							self:	List[str]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	Tuple    ,						UpperCamelCase_:	Optional[Any]    ,						*UpperCamelCase_:	int							)			->   int:





									"""simple docstring"""







									lowercase__            =      CTRLLMHeadModel(UpperCamelCase_							)
									model.to(UpperCamelCase_							)
									model.eval()

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

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Dict:





									"""simple docstring"""







									lowercase__            =      self.prepare_config_and_inputs()

									(
									    (
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		
									)            =      config_and_inputs

									lowercase__            =      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

									return config, inputs_dict


							def       lowerCamelCase_ (							self:	Optional[Any]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	List[str]    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	Any    ,						*UpperCamelCase_:	Tuple							)			->   int:





									"""simple docstring"""







									lowercase__            =      self.num_labels
									lowercase__            =      CTRLForSequenceClassification(UpperCamelCase_							)
									model.to(UpperCamelCase_							)
									model.eval()
									lowercase__            =      ids_tensor([self.batch_size]    ,						self.type_sequence_label_size							)
									lowercase__            =      model(UpperCamelCase_    ,						token_type_ids=UpperCamelCase_    ,						labels=UpperCamelCase_							)
									self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)							)


@require_torch
class        _a							(			UpperCamelCase__     ,	UpperCamelCase__     ,	UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			Union[str, Any]        = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
							_lowercase	:			Optional[int]        = (CTRLLMHeadModel,) if is_torch_available() else ()
							_lowercase	:			str        = (
							    {
							        '''feature-extraction''': CTRLModel,
							        '''text-classification''': CTRLForSequenceClassification,
							        '''text-generation''': CTRLLMHeadModel,
							        '''zero-shot''': CTRLForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							_lowercase	:			List[str]        = True
							_lowercase	:			Tuple        = False
							_lowercase	:			Optional[int]        = False

							def       lowerCamelCase_ (							self:	Tuple    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	Tuple							)			->   List[Any]:





									"""simple docstring"""







									if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
											# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
											# `CTRLConfig` 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:	Dict							)			->   str:





									"""simple docstring"""







									lowercase__            =      CTRLModelTester(self							)
									lowercase__            =      ConfigTester(self    ,						config_class=UpperCamelCase_    ,						n_embd=37							)

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Union[str, Any]:





									"""simple docstring"""







									super().tearDown()
									# clean-up as much as possible GPU memory occupied by PyTorch
									gc.collect()
									torch.cuda.empty_cache()

							def       lowerCamelCase_ (							self:	int							)			->   int:





									"""simple docstring"""







									self.config_tester.run_common_tests()

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   List[Any]:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_ctrl_model(*UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	Tuple							)			->   str:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*UpperCamelCase_							)

							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''							)
							def       lowerCamelCase_ (							self:	Dict							)			->   str:





									"""simple docstring"""







									pass

							@slow
							def       lowerCamelCase_ (							self:	List[str]							)			->   List[Any]:





									"""simple docstring"""







									for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      CTRLModel.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)


							@unittest.skip('''The model doesn\'t support left padding'''							)  # and it's not used enough to be worth fixing :)
							def       lowerCamelCase_ (							self:	List[str]							)			->   Tuple:





									"""simple docstring"""







									pass


@require_torch
class        _a							(			unittest.TestCase			):

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





									"""simple docstring"""







									super().tearDown()
									# clean-up as much as possible GPU memory occupied by PyTorch
									gc.collect()
									torch.cuda.empty_cache()


							@slow
							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   str:





									"""simple docstring"""







									lowercase__            =      CTRLLMHeadModel.from_pretrained('''ctrl'''							)
									model.to(UpperCamelCase_							)
									lowercase__            =      torch.tensor(
									    [[11_859, 0, 1_611, 8]]    ,						dtype=torch.long    ,						device=UpperCamelCase_							)  # Legal the president is
									lowercase__            =      [
									    11_859,
									    0,
									    1_611,
									    8,
									    5,
									    150,
									    26_449,
									    2,
									    19,
									    348,
									    469,
									    3,
									    2_595,
									    48,
									    20_740,
									    246_533,
									    246_533,
									    19,
									    30,
									    5,
									]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

									lowercase__            =      model.generate(UpperCamelCase_    ,						do_sample=UpperCamelCase_							)
									self.assertListEqual(output_ids[0].tolist()    ,						UpperCamelCase_							)






class        _a							:

							def __init__(							self:	Any							)			->   Tuple:





									"""simple docstring"""







									lowercase__            =      ''''''
									lowercase__            =      ''''''
									lowercase__            =      []

							def       lowerCamelCase_ (							self:	Union[str, Any]    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	int							)			->   int:





									"""simple docstring"""







									if m == -1:
											return n + 1
									elif n == -1:
											return m + 1
									elif self.dp[m][n] > -1:
											return self.dp[m][n]
									else:
											if self.worda[m] == self.worda[n]:
													lowercase__            =      self.__min_dist_top_down_dp(m - 1    ,						n - 1							)
											else:
													lowercase__            =      self.__min_dist_top_down_dp(UpperCamelCase_    ,						n - 1							)
													lowercase__            =      self.__min_dist_top_down_dp(m - 1    ,						UpperCamelCase_							)
													lowercase__            =      self.__min_dist_top_down_dp(m - 1    ,						n - 1							)
													lowercase__            =      1 + min(UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_							)

											return self.dp[m][n]

							def       lowerCamelCase_ (							self:	Union[str, Any]    ,						UpperCamelCase_:	str    ,						UpperCamelCase_:	str							)			->   int:





									"""simple docstring"""







									lowercase__            =      worda
									lowercase__            =      worda
									lowercase__            =      [[-1 for _ in range(len(UpperCamelCase_							)							)] for _ in range(len(UpperCamelCase_							)							)]

									return self.__min_dist_top_down_dp(len(UpperCamelCase_							) - 1    ,						len(UpperCamelCase_							) - 1							)


							def       lowerCamelCase_ (							self:	int    ,						UpperCamelCase_:	str    ,						UpperCamelCase_:	str							)			->   int:





									"""simple docstring"""







									lowercase__            =      worda
									lowercase__            =      worda
									lowercase__            =      len(UpperCamelCase_							)
									lowercase__            =      len(UpperCamelCase_							)
									lowercase__            =      [[0 for _ in range(n + 1							)] for _ in range(m + 1							)]

									for i in range(m + 1							):
											for j in range(n + 1							):
													if i == 0:  # first string is empty
															lowercase__            =      j
													elif j == 0:  # second string is empty
															lowercase__            =      i
													elif worda[i - 1] == worda[j - 1]:  # last characters are equal
															lowercase__            =      self.dp[i - 1][j - 1]
													else:
															lowercase__            =      self.dp[i][j - 1]
															lowercase__            =      self.dp[i - 1][j]
															lowercase__            =      self.dp[i - 1][j - 1]
															lowercase__            =      1 + min(UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_							)
									return self.dp[m][n]


if __name__ == "__main__":
			lowerCAmelCase       =							EditDistance()

			print('****************** Testing Edit Distance DP Algorithm ******************')
			print()

			lowerCAmelCase       =							input('Enter the first string: ').strip()
			lowerCAmelCase       =							input('Enter the second string: ').strip()

			print()
			print(f"""The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}""")
			print(f"""The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}""")
			print()
			print('*************** End of Testing Edit Distance DP Algorithm ***************')


