


'''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	:		Optional[Any]						,  _A	:		List[Any]						,  _A	:		Dict=13						,  _A	:		int=7						,  _A	:		Optional[int]=True						,  _A	:		str=True						,  _A	:		Optional[int]=True						,  _A	:		Tuple=99						,  _A	:		Tuple=32						,  _A	:		Any=5						,  _A	:		Optional[Any]=4						,  _A	:		int=37						,  _A	:		List[str]="gelu"						,  _A	:		List[Any]=0.1						,  _A	:		Dict=0.1						,  _A	:		Any=512						,  _A	:		Union[str, Any]=16						,  _A	:		Union[str, Any]=2						,  _A	:		str=0.02						,  _A	:		int=3						,  _A	:		List[Any]=4						,  _A	:		Tuple=None						,  )						->				List[str]:
			__magic_name__ :					Union[str, Any]  			=  parent
			__magic_name__ :					Optional[int]  			=  batch_size
			__magic_name__ :					Union[str, Any]  			=  seq_length
			__magic_name__ :					Union[str, Any]  			=  is_training
			__magic_name__ :					Optional[Any]  			=  use_input_mask
			__magic_name__ :					Union[str, Any]  			=  use_labels
			__magic_name__ :					int  			=  vocab_size
			__magic_name__ :					int  			=  hidden_size
			__magic_name__ :					Dict  			=  num_hidden_layers
			__magic_name__ :					Union[str, Any]  			=  num_attention_heads
			__magic_name__ :					int  			=  intermediate_size
			__magic_name__ :					Union[str, Any]  			=  hidden_act
			__magic_name__ :					Dict  			=  hidden_dropout_prob
			__magic_name__ :					Optional[int]  			=  attention_probs_dropout_prob
			__magic_name__ :					Tuple  			=  max_position_embeddings
			__magic_name__ :					Dict  			=  type_vocab_size
			__magic_name__ :					Dict  			=  type_sequence_label_size
			__magic_name__ :					Union[str, Any]  			=  initializer_range
			__magic_name__ :					Any  			=  num_labels
			__magic_name__ :					List[Any]  			=  num_choices
			__magic_name__ :					str  			=  scope







		def      __lowerCAmelCase	(      self	:		str    )						->				Tuple:
			__magic_name__ :					List[str]  			=  ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size    )

			__magic_name__ :					str  			=  None
			if self.use_input_mask:
				__magic_name__ :					Tuple  			=  random_attention_mask([self.batch_size, self.seq_length]    )

			__magic_name__ :					Optional[int]  			=  None
			__magic_name__ :					Dict  			=  None
			__magic_name__ :					Optional[Any]  			=  None
			if self.use_labels:
				__magic_name__ :					Any  			=  ids_tensor([self.batch_size]						,  self.type_sequence_label_size    )
				__magic_name__ :					Any  			=  ids_tensor([self.batch_size, self.seq_length]						,  self.num_labels    )
				__magic_name__ :					Tuple  			=  ids_tensor([self.batch_size]						,  self.num_choices    )

			__magic_name__ :					Dict  			=  self.get_config()

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







		def      __lowerCAmelCase	(      self	:		Union[str, Any]    )						->				Union[str, Any]:
			__magic_name__ :					List[Any]  			=  ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size    )

			__magic_name__ :					Optional[int]  			=  None
			if self.use_input_mask:
				__magic_name__ :					Tuple  			=  random_attention_mask([self.batch_size, self.seq_length]    )
			__magic_name__ :					Optional[Any]  			=  self.get_config()

			return config, input_ids, input_mask







		def      __lowerCAmelCase	(      self	:		Any    )						->				Any:
			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      __lowerCAmelCase	(      self	:		int						,  _A	:		Union[str, Any]						,  _A	:		Optional[int]						,  _A	:		Any						,  _A	:		List[Any]						,  _A	:		int						,  _A	:		Any    )						->				Union[str, Any]:
			__magic_name__ :					str  			=  ErnieMModel(config=_A    )
			model.to(_A    )
			model.eval()
			__magic_name__ :					List[str]  			=  model(_A						,  return_dict=_A    )
			self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, self.seq_length, self.hidden_size)    )







		def      __lowerCAmelCase	(      self	:		Union[str, Any]						,  _A	:		List[str]						,  _A	:		List[Any]						,  _A	:		Optional[int]						,  _A	:		Tuple						,  _A	:		Dict						,  _A	:		List[Any]    )						->				Tuple:
			__magic_name__ :					Any  			=  ErnieMForQuestionAnswering(config=_A    )
			model.to(_A    )
			model.eval()
			__magic_name__ :					List[Any]  			=  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      __lowerCAmelCase	(      self	:		Optional[Any]						,  _A	:		Union[str, Any]						,  _A	:		str						,  _A	:		List[str]						,  _A	:		Any						,  _A	:		Any						,  _A	:		str    )						->				Optional[Any]:
			__magic_name__ :					List[Any]  			=  ErnieMForInformationExtraction(config=_A    )
			model.to(_A    )
			model.eval()
			__magic_name__ :					Optional[int]  			=  torch.ones_like(_A						,  dtype=torch.floataa    )
			__magic_name__ :					Dict  			=  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      __lowerCAmelCase	(      self	:		int						,  _A	:		Union[str, Any]						,  _A	:		str						,  _A	:		Optional[Any]						,  _A	:		Optional[Any]						,  _A	:		List[str]						,  _A	:		List[Any]    )						->				int:
			__magic_name__ :					List[str]  			=  self.num_labels
			__magic_name__ :					int  			=  ErnieMForSequenceClassification(_A    )
			model.to(_A    )
			model.eval()
			__magic_name__ :					str  			=  model(_A						,  attention_mask=_A						,  labels=_A    )
			self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_labels)    )







		def      __lowerCAmelCase	(      self	:		Union[str, Any]						,  _A	:		List[str]						,  _A	:		Any						,  _A	:		Tuple						,  _A	:		Dict						,  _A	:		str						,  _A	:		List[str]    )						->				Dict:
			__magic_name__ :					Optional[Any]  			=  self.num_labels
			__magic_name__ :					Optional[int]  			=  ErnieMForTokenClassification(config=_A    )
			model.to(_A    )
			model.eval()
			input_ids.to(_A    )
			input_mask.to(_A    )
			token_labels.to(_A    )

			__magic_name__ :					List[Any]  			=  model(_A						,  attention_mask=_A						,  labels=_A    )

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







		def      __lowerCAmelCase	(      self	:		Dict						,  _A	:		Optional[int]						,  _A	:		Optional[Any]						,  _A	:		str						,  _A	:		int						,  _A	:		List[str]						,  _A	:		Dict    )						->				List[str]:
			__magic_name__ :					List[str]  			=  self.num_choices
			__magic_name__ :					Any  			=  ErnieMForMultipleChoice(config=_A    )
			model.to(_A    )
			model.eval()
			__magic_name__ :					List[Any]  			=  input_ids.unsqueeze(1    ).expand(-1						,  self.num_choices						,  -1    ).contiguous()
			__magic_name__ :					Tuple  			=  input_mask.unsqueeze(1    ).expand(-1						,  self.num_choices						,  -1    ).contiguous()
			__magic_name__ :					Union[str, Any]  			=  model(
			    _A						,  attention_mask=_A						,  labels=_A						,  )
			self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_choices)    )







		def      __lowerCAmelCase	(      self	:		List[Any]    )						->				Any:
			__magic_name__ :					Optional[int]  			=  self.prepare_config_and_inputs()
			(
			    __magic_name__       
			) :					int  			=  config_and_inputs
			__magic_name__ :					str  			=  {'input_ids': input_ids, 'attention_mask': input_mask}
			return config, inputs_dict







@require_torch
class _lowerCamelCase (		lowercase__	,  lowercase__	,  unittest.TestCase							):



		'''simple docstring'''







		A_			:    Optional[int]   					=       (
		    (
		        ErnieMModel,
		        ErnieMForMultipleChoice,
		        ErnieMForQuestionAnswering,
		        ErnieMForSequenceClassification,
		        ErnieMForTokenClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		A_			:    str   					=       ()
		A_			:    Optional[int]   					=       (
		    {
		        """feature-extraction""": ErnieMModel,
		        """question-answering""": ErnieMForQuestionAnswering,
		        """text-classification""": ErnieMForSequenceClassification,
		        """token-classification""": ErnieMForTokenClassification,
		        """zero-shot""": ErnieMForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		A_			:    Tuple   					=       False







		def      __lowerCAmelCase	(      self	:		List[str]						,  _A	:		Optional[int]						,  _A	:		Any						,  _A	:		Optional[int]						,  _A	:		str						,  _A	:		List[str]    )						->				Any:
			if pipeline_test_casse_name == "QAPipelineTests":
				return True

			return False







		def      __lowerCAmelCase	(      self	:		Optional[Any]    )						->				Tuple:
			__magic_name__ :					Any  			=  ErnieMModelTester(self    )
			__magic_name__ :					Optional[int]  			=  ConfigTester(self						,  config_class=_A						,  hidden_size=37    )







		def      __lowerCAmelCase	(      self	:		List[Any]    )						->				int:
			self.config_tester.run_common_tests()







		def      __lowerCAmelCase	(      self	:		int    )						->				Optional[int]:
			__magic_name__ :					Union[str, Any]  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_A    )







		def      __lowerCAmelCase	(      self	:		Optional[Any]    )						->				Dict:
			__magic_name__ :					Tuple  			=  self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
				__magic_name__ :					int  			=  type
				self.model_tester.create_and_check_model(*_A    )







		def      __lowerCAmelCase	(      self	:		int    )						->				int:
			__magic_name__ :					Optional[int]  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_multiple_choice(*_A    )







		def      __lowerCAmelCase	(      self	:		List[Any]    )						->				Any:
			__magic_name__ :					str  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*_A    )







		def      __lowerCAmelCase	(      self	:		Optional[Any]    )						->				str:
			__magic_name__ :					List[str]  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*_A    )







		def      __lowerCAmelCase	(      self	:		Dict    )						->				Dict:
			__magic_name__ :					int  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_information_extraction(*_A    )







		def      __lowerCAmelCase	(      self	:		Dict    )						->				Dict:
			__magic_name__ :					Dict  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*_A    )







		@slow
		def      __lowerCAmelCase	(      self	:		int    )						->				Any:
			for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__magic_name__ :					Dict  			=  ErnieMModel.from_pretrained(_A    )
				self.assertIsNotNone(_A    )







@require_torch
class _lowerCamelCase (		unittest.TestCase							):



		'''simple docstring'''







		@slow
		def      __lowerCAmelCase	(      self	:		int    )						->				Optional[Any]:
			__magic_name__ :					Any  			=  ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'    )
			model.eval()
			__magic_name__ :					Optional[int]  			=  torch.tensor([[0, 1, 2, 3, 4, 5]]    )
			__magic_name__ :					List[str]  			=  model(_A    )[0]

			# TODO Replace vocab size
			__magic_name__ :					int  			=  768

			__magic_name__ :					Optional[int]  			=  torch.Size((1, 6, hidden_size)    )
			self.assertEqual(output.shape						,  _A    )

			__magic_name__ :					Tuple  			=  torch.tensor(
			    [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]    )

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



'''simple docstring'''

lowerCAmelCase   :int								=    [
    ['''ab-e1''', '''ac-e3''', '''ad-e5''', '''bc-e4''', '''bd-e2''', '''be-e6''', '''bh-e12''', '''cd-e2''', '''ce-e4''',
     '''de-e1''', '''df-e8''', '''dg-e5''', '''dh-e10''', '''ef-e3''', '''eg-e2''', '''fg-e6''', '''gh-e6''', '''hi-e3'''],
    ['''ab-e1''', '''ac-e3''', '''ad-e5''', '''bc-e4''', '''bd-e2''', '''be-e6''', '''cd-e2''', '''de-e1''', '''df-e8''',
     '''ef-e3''', '''eg-e2''', '''fg-e6'''],
    ['''ab-e1''', '''ac-e3''', '''bc-e4''', '''bd-e2''', '''de-e1''', '''df-e8''', '''dg-e5''', '''ef-e3''', '''eg-e2''',
     '''eh-e12''', '''fg-e6''', '''fh-e10''', '''gh-e6'''],
    ['''ab-e1''', '''ac-e3''', '''bc-e4''', '''bd-e2''', '''bh-e12''', '''cd-e2''', '''df-e8''', '''dh-e10'''],
    ['''ab-e1''', '''ac-e3''', '''ad-e5''', '''bc-e4''', '''bd-e2''', '''cd-e2''', '''ce-e4''', '''de-e1''', '''df-e8''',
     '''dg-e5''', '''ef-e3''', '''eg-e2''', '''fg-e6''']
]





def lowerCamelCase    (    lowerCAmelCase       :			Tuple	):





	"""simple docstring"""







	__magic_name__ :					Union[str, Any]  			=  set()
	for row in edge_array:
		for item in row:
			distinct_edge.add(item[0]	)
	return list(lowerCAmelCase	)





def lowerCamelCase    (    lowerCAmelCase       :			List[str]	,	lowerCAmelCase       :			Optional[int]	):





	"""simple docstring"""







	__magic_name__ :					List[Any]  			=  ['0'] * len(lowerCAmelCase	)
	for i, row in enumerate(lowerCAmelCase	):
		for item in row:
			if distinct_edge in item[0]:
				__magic_name__ :					str  			=  '1'
				break
	return "".join(lowerCAmelCase	)





def lowerCamelCase    (    lowerCAmelCase       :			Tuple	):





	"""simple docstring"""







	__magic_name__ :					Any  			=  get_distinct_edge(lowerCAmelCase	)
	__magic_name__ :					Union[str, Any]  			=  {}

	for item in distinct_edge:
		__magic_name__ :					Union[str, Any]  			=  get_bitcode(lowerCAmelCase	,	lowerCAmelCase	)
		# print('bit',bit)
		# bt=''.join(bit)
		__magic_name__ :					Dict  			=  bit.count('1'	)
		__magic_name__ :					Dict  			=  [s, bit]
	# Store [Distinct edge, WT(Bitcode), Bitcode] in descending order
	__magic_name__ :					Optional[Any]  			=  [
	    [k, v[0], v[1]]
	    for k, v in sorted(frequency_table.items()	,	key=lambda lowerCAmelCase	: v[1][0]	,	reverse=lowerCAmelCase	)
	]
	return sorted_frequency_table





def lowerCamelCase    (    lowerCAmelCase       :			List[str]	):





	"""simple docstring"""







	__magic_name__ :					Tuple  			=  {}
	for _, item in enumerate(lowerCAmelCase	):
		nodes.setdefault(item[2]	,	[]	).append(item[0]	)
	return nodes





def lowerCamelCase    (    lowerCAmelCase       :			List[str]	):





	"""simple docstring"""







	__magic_name__ :					str  			=  {}
	for key, value in nodes.items():
		__magic_name__ :					Union[str, Any]  			=  value
	return cluster





def lowerCamelCase    (    lowerCAmelCase       :			List[str]	):





	"""simple docstring"""







	return [i * 100 / len(lowerCAmelCase	) for i in cluster]





def lowerCamelCase    (    ):





	"""simple docstring"""







	print('\nNodes\n'	)
	for key, value in nodes.items():
		print(lowerCAmelCase	,	lowerCAmelCase	)
	print('\nSupport\n'	)
	print(lowerCAmelCase	)
	print('\n Cluster \n'	)
	for key, value in sorted(cluster.items()	,	reverse=lowerCAmelCase	):
		print(lowerCAmelCase	,	lowerCAmelCase	)
	print('\n Graph\n'	)
	for key, value in graph.items():
		print(lowerCAmelCase	,	lowerCAmelCase	)
	print('\n Edge List of Frequent subgraphs \n'	)
	for edge_list in freq_subgraph_edge_list:
		print(lowerCAmelCase	)





def lowerCamelCase    (    lowerCAmelCase       :			int	,	lowerCAmelCase       :			str	,	lowerCAmelCase       :			Optional[Any]	,	lowerCAmelCase       :			int	):





	"""simple docstring"""







	for i in cluster[ca]:
		__magic_name__ :					Tuple  			=  0
		__magic_name__ :					Any  			=  ca + 1
		while ca < max(cluster.keys()	):
			for j in cluster[ca]:
				if int(lowerCAmelCase	,	2	) & int(lowerCAmelCase	,	2	) == int(lowerCAmelCase	,	2	):
					if tuple(nodes[i]	) in graph:
						graph[tuple(nodes[i]	)].append(nodes[j]	)
					else:
						__magic_name__ :					List[Any]  			=  [nodes[j]]
					count += 1
			if count == 0:
				__magic_name__ :					Tuple  			=  ca + 1
			else:
				break





def lowerCamelCase    (    lowerCAmelCase       :			Any	,	lowerCAmelCase       :			List[Any]	):





	"""simple docstring"""







	__magic_name__ :					str  			=  cluster[max(cluster.keys()	)]
	__magic_name__ :					Dict  			=  'Header'
	__magic_name__ :					Optional[Any]  			=  {}
	for i in x:
		if (["Header"],) in graph:
			graph[(["Header"],)].append(x[i]	)
		else:
			__magic_name__ :					List[str]  			=  [x[i]]
	for i in x:
		__magic_name__ :					Union[str, Any]  			=  [['Header']]
	__magic_name__ :					List[str]  			=  1
	while i < max(lowerCAmelCase	) - 1:
		create_edge(lowerCAmelCase	,	lowerCAmelCase	,	lowerCAmelCase	,	lowerCAmelCase	)
		__magic_name__ :					Dict  			=  i + 1
	return graph





def lowerCamelCase    (    lowerCAmelCase       :			Dict	,	lowerCAmelCase       :			Tuple	,	lowerCAmelCase       :			List[Any]	,	lowerCAmelCase       :			Union[str, Any]=None	):





	"""simple docstring"""







	__magic_name__ :					str  			=  (path or []) + [start]
	if start == end:
		paths.append(lowerCAmelCase	)
	for node in graph[start]:
		if tuple(lowerCAmelCase	) not in path:
			my_dfs(lowerCAmelCase	,	tuple(lowerCAmelCase	)	,	lowerCAmelCase	,	lowerCAmelCase	)





def lowerCamelCase    (    lowerCAmelCase       :			Optional[Any]	,	lowerCAmelCase       :			Tuple	,	lowerCAmelCase       :			Union[str, Any]	):





	"""simple docstring"""







	__magic_name__ :					Union[str, Any]  			=  int(s / 100 * (len(lowerCAmelCase	) - 1)	)
	for i in cluster[k]:
		my_dfs(lowerCAmelCase	,	tuple(cluster[k][i]	)	,	(['Header'],)	)





def lowerCamelCase    (    lowerCAmelCase       :			Optional[int]	):





	"""simple docstring"""







	__magic_name__ :					str  			=  []
	for edges in paths:
		__magic_name__ :					List[Any]  			=  []
		for j in range(len(lowerCAmelCase	) - 1	):
			__magic_name__ :					List[str]  			=  list(edges[j]	)
			for e in temp:
				__magic_name__ :					Optional[int]  			=  (e[0], e[1])
				el.append(lowerCAmelCase	)
		freq_sub_el.append(lowerCAmelCase	)
	return freq_sub_el





def lowerCamelCase    (    lowerCAmelCase       :			str	):





	"""simple docstring"""







	for i in range(len(lowerCAmelCase	)	):
		for j in range(len(edge_array[i]	)	):
			__magic_name__ :					Any  			=  edge_array[i][j].split('-'	)
			__magic_name__ :					Tuple  			=  t


if __name__ == "__main__":
							preprocess(edge_array)
							lowerCAmelCase   :List[str]								=    get_frequency_table(edge_array)
							lowerCAmelCase   :List[str]								=    get_nodes(frequency_table)
							lowerCAmelCase   :Optional[int]								=    get_cluster(nodes)
							lowerCAmelCase   :Tuple								=    get_support(cluster)
							lowerCAmelCase   :Tuple								=    construct_graph(cluster, nodes)
							find_freq_subgraph_given_support(6_0, cluster, graph)
							lowerCAmelCase   :list								=    []
							lowerCAmelCase   :str								=    freq_subgraphs_edge_list(paths)
							print_all()