



'''simple docstring'''







import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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 (
		    ErnieMForInformationExtraction,
		    ErnieMForMultipleChoice,
		    ErnieMForQuestionAnswering,
		    ErnieMForSequenceClassification,
		    ErnieMForTokenClassification,
		    ErnieMModel,
		)
		from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST

class      lowerCamelCase__     :






	'''simple docstring'''

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




								'''simple docstring'''





								lowerCAmelCase__  =   parent
								lowerCAmelCase__  =   batch_size
								lowerCAmelCase__  =   seq_length
								lowerCAmelCase__  =   is_training
								lowerCAmelCase__  =   use_input_mask
								lowerCAmelCase__  =   use_labels
								lowerCAmelCase__  =   vocab_size
								lowerCAmelCase__  =   hidden_size
								lowerCAmelCase__  =   num_hidden_layers
								lowerCAmelCase__  =   num_attention_heads
								lowerCAmelCase__  =   intermediate_size
								lowerCAmelCase__  =   hidden_act
								lowerCAmelCase__  =   hidden_dropout_prob
								lowerCAmelCase__  =   attention_probs_dropout_prob
								lowerCAmelCase__  =   max_position_embeddings
								lowerCAmelCase__  =   type_vocab_size
								lowerCAmelCase__  =   type_sequence_label_size
								lowerCAmelCase__  =   initializer_range
								lowerCAmelCase__  =   num_labels
								lowerCAmelCase__  =   num_choices
								lowerCAmelCase__  =   scope




	def      lowercase__							(  self		:  Tuple					)     ->			Dict:




								'''simple docstring'''





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

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

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

								lowerCAmelCase__  =   self.get_config()

								return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels




	def      lowercase__							(  self		:  Dict					)     ->			Any:




								'''simple docstring'''





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

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

								return config, input_ids, input_mask




	def      lowercase__							(  self		:  List[str]					)     ->			List[Any]:




								'''simple docstring'''





								return ErnieMConfig(
								    vocab_size=self.vocab_size   ,		hidden_size=self.hidden_size   ,		num_hidden_layers=self.num_hidden_layers   ,		num_attention_heads=self.num_attention_heads   ,		intermediate_size=self.intermediate_size   ,		hidden_act=self.hidden_act   ,		hidden_dropout_prob=self.hidden_dropout_prob   ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,		max_position_embeddings=self.max_position_embeddings   ,		type_vocab_size=self.type_vocab_size   ,		initializer_range=self.initializer_range   ,		)




	def      lowercase__							(  self		:  Union[str, Any]   ,		__A		:  int   ,		__A		:  Union[str, Any]   ,		__A		:  Optional[int]   ,		__A		:  List[str]   ,		__A		:  List[Any]   ,		__A		:  str					)     ->			Tuple:




								'''simple docstring'''





								lowerCAmelCase__  =   ErnieMModel(config=__A					)
								model.to(__A					)
								model.eval()
								lowerCAmelCase__  =   model(__A   ,		return_dict=__A					)
								self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, self.seq_length, self.hidden_size)					)




	def      lowercase__							(  self		:  Optional[int]   ,		__A		:  Union[str, Any]   ,		__A		:  Tuple   ,		__A		:  Any   ,		__A		:  Union[str, Any]   ,		__A		:  List[str]   ,		__A		:  List[Any]					)     ->			Union[str, Any]:




								'''simple docstring'''





								lowerCAmelCase__  =   ErnieMForQuestionAnswering(config=__A					)
								model.to(__A					)
								model.eval()
								lowerCAmelCase__  =   model(
								    __A   ,		attention_mask=__A   ,		start_positions=__A   ,		end_positions=__A   ,		)
								self.parent.assertEqual(result.start_logits.shape   ,		(self.batch_size, self.seq_length)					)
								self.parent.assertEqual(result.end_logits.shape   ,		(self.batch_size, self.seq_length)					)




	def      lowercase__							(  self		:  Optional[Any]   ,		__A		:  Optional[Any]   ,		__A		:  List[Any]   ,		__A		:  Any   ,		__A		:  str   ,		__A		:  List[Any]   ,		__A		:  List[str]					)     ->			Union[str, Any]:




								'''simple docstring'''





								lowerCAmelCase__  =   ErnieMForInformationExtraction(config=__A					)
								model.to(__A					)
								model.eval()
								lowerCAmelCase__  =   torch.ones_like(__A   ,		dtype=torch.floataa					)
								lowerCAmelCase__  =   model(
								    __A   ,		attention_mask=__A   ,		start_positions=__A   ,		end_positions=__A   ,		)
								self.parent.assertEqual(result.start_logits.shape   ,		(self.batch_size, self.seq_length)					)
								self.parent.assertEqual(result.end_logits.shape   ,		(self.batch_size, self.seq_length)					)




	def      lowercase__							(  self		:  Tuple   ,		__A		:  str   ,		__A		:  Optional[int]   ,		__A		:  Union[str, Any]   ,		__A		:  Optional[Any]   ,		__A		:  Union[str, Any]   ,		__A		:  Optional[Any]					)     ->			Any:




								'''simple docstring'''





								lowerCAmelCase__  =   self.num_labels
								lowerCAmelCase__  =   ErnieMForSequenceClassification(__A					)
								model.to(__A					)
								model.eval()
								lowerCAmelCase__  =   model(__A   ,		attention_mask=__A   ,		labels=__A					)
								self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.num_labels)					)




	def      lowercase__							(  self		:  Dict   ,		__A		:  Any   ,		__A		:  List[Any]   ,		__A		:  Union[str, Any]   ,		__A		:  Union[str, Any]   ,		__A		:  Union[str, Any]   ,		__A		:  Optional[Any]					)     ->			Tuple:




								'''simple docstring'''





								lowerCAmelCase__  =   self.num_labels
								lowerCAmelCase__  =   ErnieMForTokenClassification(config=__A					)
								model.to(__A					)
								model.eval()
								input_ids.to(__A					)
								input_mask.to(__A					)
								token_labels.to(__A					)

								lowerCAmelCase__  =   model(__A   ,		attention_mask=__A   ,		labels=__A					)

								self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.num_labels)					)




	def      lowercase__							(  self		:  Tuple   ,		__A		:  Optional[int]   ,		__A		:  Dict   ,		__A		:  Union[str, Any]   ,		__A		:  Dict   ,		__A		:  List[Any]   ,		__A		:  Dict					)     ->			str:




								'''simple docstring'''





								lowerCAmelCase__  =   self.num_choices
								lowerCAmelCase__  =   ErnieMForMultipleChoice(config=__A					)
								model.to(__A					)
								model.eval()
								lowerCAmelCase__  =   input_ids.unsqueeze(1					).expand(-1   ,		self.num_choices   ,		-1					).contiguous()
								lowerCAmelCase__  =   input_mask.unsqueeze(1					).expand(-1   ,		self.num_choices   ,		-1					).contiguous()
								lowerCAmelCase__  =   model(
								    __A   ,		attention_mask=__A   ,		labels=__A   ,		)
								self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.num_choices)					)




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




								'''simple docstring'''





								lowerCAmelCase__  =   self.prepare_config_and_inputs()
								(
								    (
								    lowerCAmelCase__
								)  ,(
								    lowerCAmelCase__
								)  ,(
								    lowerCAmelCase__
								)  ,(
								    lowerCAmelCase__
								)  ,(
								    lowerCAmelCase__
								)  ,(
								    lowerCAmelCase__
								)  ,
								)  =   config_and_inputs
								lowerCAmelCase__  =   {"""input_ids""": input_ids, """attention_mask""": input_mask}
								return config, inputs_dict





@require_torch
class      lowerCamelCase__     (     _A,     _A,     unittest.TestCase				):






	'''simple docstring'''

	A__		=	(
	    (
	        ErnieMModel,
	        ErnieMForMultipleChoice,
	        ErnieMForQuestionAnswering,
	        ErnieMForSequenceClassification,
	        ErnieMForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	A__		=	()
	A__		=	(
	    {
	        '''feature-extraction''': ErnieMModel,
	        '''question-answering''': ErnieMForQuestionAnswering,
	        '''text-classification''': ErnieMForSequenceClassification,
	        '''token-classification''': ErnieMForTokenClassification,
	        '''zero-shot''': ErnieMForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	A__		=	False




	def      lowercase__							(  self		:  Dict   ,		__A		:  str   ,		__A		:  List[str]   ,		__A		:  int   ,		__A		:  Union[str, Any]   ,		__A		:  List[str]					)     ->			Tuple:




								'''simple docstring'''





								if pipeline_test_casse_name == "QAPipelineTests":
															return True

								return False




	def      lowercase__							(  self		:  int					)     ->			int:




								'''simple docstring'''





								lowerCAmelCase__  =   ErnieMModelTester(self					)
								lowerCAmelCase__  =   ConfigTester(self   ,		config_class=__A   ,		hidden_size=37					)




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




								'''simple docstring'''





								self.config_tester.run_common_tests()




	def      lowercase__							(  self		:  str					)     ->			str:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__A					)




	def      lowercase__							(  self		:  Optional[Any]					)     ->			Optional[int]:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
															lowerCAmelCase__  =   type
															self.model_tester.create_and_check_model(*__A					)




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




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*__A					)




	def      lowercase__							(  self		:  int					)     ->			int:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__A					)




	def      lowercase__							(  self		:  List[Any]					)     ->			List[Any]:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*__A					)




	def      lowercase__							(  self		:  Tuple					)     ->			List[Any]:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_information_extraction(*__A					)




	def      lowercase__							(  self		:  List[str]					)     ->			Dict:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__A					)




	@slow
	def      lowercase__							(  self		:  List[str]					)     ->			Optional[int]:




								'''simple docstring'''





								for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCAmelCase__  =   ErnieMModel.from_pretrained(__A					)
															self.assertIsNotNone(__A					)





@require_torch
class      lowerCamelCase__     (     unittest.TestCase				):






	'''simple docstring'''

	@slow
	def      lowercase__							(  self		:  Tuple					)     ->			Optional[int]:




								'''simple docstring'''





								lowerCAmelCase__  =   ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""					)
								model.eval()
								lowerCAmelCase__  =   torch.tensor([[0, 1, 2, 3, 4, 5]]					)
								lowerCAmelCase__  =   model(__A					)[0]

								# TODO Replace vocab size
								lowerCAmelCase__  =   768

								lowerCAmelCase__  =   torch.Size((1, 6, hidden_size)					)
								self.assertEqual(output.shape   ,		__A					)

								lowerCAmelCase__  =   torch.tensor(
								    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]]					)

								self.assertTrue(torch.allclose(output[:, :3, :3]   ,		__A   ,		atol=1E-3					)					)










'''simple docstring'''







def 			_lowerCAmelCase(      UpperCAmelCase_      :	str					, UpperCAmelCase_      :	str					, UpperCAmelCase_      :	int					, UpperCAmelCase_      :	int					, UpperCAmelCase_      :	int					, UpperCAmelCase_      :	int					, ) ->							tuple[list[list[int]], list[list[str]]]:
							lowerCAmelCase__  =   list(UpperCAmelCase_						)
							lowerCAmelCase__  =   list(UpperCAmelCase_						)
							lowerCAmelCase__  =   len(UpperCAmelCase_						)
							lowerCAmelCase__  =   len(UpperCAmelCase_						)

							lowerCAmelCase__  =   [
							    [0 for _ in range(len_destination_seq + 1						)] for _ in range(len_source_seq + 1						)
							]
							lowerCAmelCase__  =   [
							    ["""0""" for _ in range(len_destination_seq + 1						)] for _ in range(len_source_seq + 1						)
							]

							for i in range(1					, len_source_seq + 1						):
														lowerCAmelCase__  =   i * delete_cost
														lowerCAmelCase__  =   F'''D{source_seq[i - 1]:c}'''

							for i in range(1					, len_destination_seq + 1						):
														lowerCAmelCase__  =   i * insert_cost
														lowerCAmelCase__  =   F'''I{destination_seq[i - 1]:c}'''

							for i in range(1					, len_source_seq + 1						):
														for j in range(1					, len_destination_seq + 1						):
																					if source_seq[i - 1] == destination_seq[j - 1]:
																												lowerCAmelCase__  =   costs[i - 1][j - 1] + copy_cost
																												lowerCAmelCase__  =   F'''C{source_seq[i - 1]:c}'''
																					else:
																												lowerCAmelCase__  =   costs[i - 1][j - 1] + replace_cost
																												lowerCAmelCase__  =   F'''R{source_seq[i - 1]:c}''' + str(destination_seq[j - 1]						)

																					if costs[i - 1][j] + delete_cost < costs[i][j]:
																												lowerCAmelCase__  =   costs[i - 1][j] + delete_cost
																												lowerCAmelCase__  =   F'''D{source_seq[i - 1]:c}'''

																					if costs[i][j - 1] + insert_cost < costs[i][j]:
																												lowerCAmelCase__  =   costs[i][j - 1] + insert_cost
																												lowerCAmelCase__  =   F'''I{destination_seq[j - 1]:c}'''

							return costs, ops
def 			_lowerCAmelCase(      UpperCAmelCase_      :	list[list[str]]					, UpperCAmelCase_      :	int					, UpperCAmelCase_      :	int						) ->							list[str]:
							if i == 0 and j == 0:
														return []
							else:
														if ops[i][j][0] == "C" or ops[i][j][0] == "R":
																					lowerCAmelCase__  =   assemble_transformation(UpperCAmelCase_					, i - 1					, j - 1						)
																					seq.append(ops[i][j]						)
																					return seq
														elif ops[i][j][0] == "D":
																					lowerCAmelCase__  =   assemble_transformation(UpperCAmelCase_					, i - 1					, UpperCAmelCase_						)
																					seq.append(ops[i][j]						)
																					return seq
														else:
																					lowerCAmelCase__  =   assemble_transformation(UpperCAmelCase_					, UpperCAmelCase_					, j - 1						)
																					seq.append(ops[i][j]						)
																					return seq


if __name__ == "__main__":
		_UpperCamelCase						, _UpperCamelCase  						=			compute_transform_tables("""Python""", """Algorithms""", -1, 1, 2, 2)

		_UpperCamelCase  						=			len(operations)
		_UpperCamelCase  						=			len(operations[0])
		_UpperCamelCase  						=			assemble_transformation(operations, m - 1, n - 1)

		_UpperCamelCase  						=			list("""Python""")
		_UpperCamelCase  						=			0
		_UpperCamelCase  						=			0

		with open("""min_cost.txt""", """w""") as file:
				for op in sequence:
						print("""""".join(string))

						if op[0] == "C":
								file.write("""%-16s""" % """Copy %c""" % op[1])
								file.write("""\t\t\t""" + """""".join(string))
								file.write("""\r\n""")

								cost -= 1
						elif op[0] == "R":
								_UpperCamelCase  						=			op[2]

								file.write("""%-16s""" % ("""Replace %c""" % op[1] + """ with """ + str(op[2])))
								file.write("""\t\t""" + """""".join(string))
								file.write("""\r\n""")

								cost += 1
						elif op[0] == "D":
								string.pop(i)

								file.write("""%-16s""" % """Delete %c""" % op[1])
								file.write("""\t\t\t""" + """""".join(string))
								file.write("""\r\n""")

								cost += 2
						else:
								string.insert(i, op[1])

								file.write("""%-16s""" % """Insert %c""" % op[1])
								file.write("""\t\t\t""" + """""".join(string))
								file.write("""\r\n""")

								cost += 2

						i += 1

				print("""""".join(string))
				print("""Cost: """, cost)

				file.write("""\r\nMinimum cost: """ + str(cost))





