
'''simple docstring'''


import importlib
import importlib.metadata
import os
import warnings
from distutils.util import strtobool
from functools import lru_cache

import torch
from packaging import version
from packaging.version import parse

from .environment import parse_flag_from_env
from .versions import compare_versions, is_torch_version


try:
					import torch_xla.core.xla_model as xm  # noqa: F401

					__lowercase							:				List[str]					=       True
except ImportError:
					__lowercase							:				List[Any]					=       False


# Cache this result has it's a C FFI call which can be pretty time-consuming
__lowercase							:				int					=       torch.distributed.is_available()




def lowercase_		(				_lowercase			)       ->							Dict:
					'''simple docstring'''

					lowerCamelCase_   : List[Any]       =						importlib.util.find_spec(_lowercase			) is not None
					if package_exists:
										try:
															lowerCamelCase_   : int       =						importlib.metadata.metadata(_lowercase			)
															return True
										except importlib.metadata.PackageNotFoundError:
															return False




def lowercase_		(				)       ->							bool:
					'''simple docstring'''

					return _torch_distributed_available




def lowercase_		(				)       ->							List[Any]:
					'''simple docstring'''

					try:
										pass
					except ImportError:
										print(
										    '''Intel(R) oneCCL Bindings for PyTorch* is required to run DDP on Intel(R) GPUs, but it is not'''
										    ''' detected. If you see \"ValueError: Invalid backend: \'ccl\'\" error, please install Intel(R) oneCCL'''
										    ''' Bindings for PyTorch*.'''			)
					return (
					    importlib.util.find_spec('''torch_ccl'''			) is not None
					    or importlib.util.find_spec('''oneccl_bindings_for_pytorch'''			) is not None
					)




def lowercase_		(				)       ->							Optional[Any]:
					'''simple docstring'''

					return importlib.metadata.version('''oneccl_bind_pt'''			)




def lowercase_		(				)       ->							Dict:
					'''simple docstring'''

					return _is_package_available('''transformer_engine'''			)




@lru_cache
def lowercase_		(				_lowercase=True			)       ->							Optional[int]:
					'''simple docstring'''

					if torch.cuda.is_available():
										return False
					if _tpu_available and check_device:
										try:
															# Will raise a RuntimeError if no XLA configuration is found
															lowerCamelCase_   : Tuple       =						xm.xla_device()
															return True
										except RuntimeError:
															return False
					return _tpu_available




def lowercase_		(				)       ->							Any:
					'''simple docstring'''

					return _is_package_available('''deepspeed'''			)




def lowercase_		(				_lowercase=False			)       ->							Any:
					'''simple docstring'''

					if is_tpu_available():
										return not ignore_tpu
					if torch.cuda.is_available():
										return torch.cuda.is_bfaa_supported()
					if is_npu_available():
										return False
					return True




def lowercase_		(				)       ->							Optional[int]:
					'''simple docstring'''

					lowerCamelCase_   : List[Any]       =						_is_package_available('''bitsandbytes'''			)
					if package_exists:
										lowerCamelCase_   : List[Any]       =						version.parse(importlib.metadata.version('''bitsandbytes'''			)			)
										return compare_versions(_lowercase     ,    '''>='''     ,    '''0.39.0'''			)
					return False




def lowercase_		(				)       ->							Dict:
					'''simple docstring'''

					lowerCamelCase_   : Optional[int]       =						_is_package_available('''bitsandbytes'''			)
					if package_exists:
										lowerCamelCase_   : Tuple       =						version.parse(importlib.metadata.version('''bitsandbytes'''			)			)
										return compare_versions(_lowercase     ,    '''>='''     ,    '''0.37.2'''			)
					return False




def lowercase_		(				)       ->							Union[str, Any]:
					'''simple docstring'''

					return _is_package_available('''bitsandbytes'''			)




def lowercase_		(				)       ->							int:
					'''simple docstring'''

					if strtobool(os.environ.get('''ACCELERATE_USE_MEGATRON_LM'''     ,    '''False'''			)			) == 1:
										lowerCamelCase_   : int       =						importlib.util.find_spec('''megatron'''			) is not None
										if package_exists:
															try:
																				lowerCamelCase_   : List[Any]       =						parse(importlib.metadata.version('''megatron-lm'''			)			)
																				return compare_versions(_lowercase     ,    '''>='''     ,    '''2.2.0'''			)
															except Exception as e:
																				warnings.warn(F"""Parse Megatron version failed. Exception:{e}"""			)
																				return False




def lowercase_		(				)       ->							Optional[int]:
					'''simple docstring'''

					return _is_package_available('''safetensors'''			)




def lowercase_		(				)       ->							Optional[Any]:
					'''simple docstring'''

					return _is_package_available('''transformers'''			)




def lowercase_		(				)       ->							List[Any]:
					'''simple docstring'''

					return _is_package_available('''datasets'''			)




def lowercase_		(				)       ->							Tuple:
					'''simple docstring'''

					return _is_package_available('''aim'''			)




def lowercase_		(				)       ->							int:
					'''simple docstring'''

					return _is_package_available('''tensorboard'''			) or _is_package_available('''tensorboardX'''			)




def lowercase_		(				)       ->							Optional[Any]:
					'''simple docstring'''

					return _is_package_available('''wandb'''			)




def lowercase_		(				)       ->							Dict:
					'''simple docstring'''

					return _is_package_available('''comet_ml'''			)




def lowercase_		(				)       ->							Union[str, Any]:
					'''simple docstring'''

					return _is_package_available('''boto3'''			)




def lowercase_		(				)       ->							str:
					'''simple docstring'''

					if _is_package_available('''rich'''			):
										if "ACCELERATE_DISABLE_RICH" in os.environ:
															warnings.warn(
															    '''`ACCELERATE_DISABLE_RICH` is deprecated and will be removed in v0.22.0 and deactivated by default. Please use `ACCELERATE_ENABLE_RICH` if you wish to use `rich`.'''			)
															return not parse_flag_from_env('''ACCELERATE_DISABLE_RICH'''     ,    _lowercase			)
										return parse_flag_from_env('''ACCELERATE_ENABLE_RICH'''     ,    _lowercase			)
					return False




def lowercase_		(				)       ->							List[str]:
					'''simple docstring'''

					return _is_package_available('''sagemaker'''			)




def lowercase_		(				)       ->							Dict:
					'''simple docstring'''

					return _is_package_available('''tqdm'''			)




def lowercase_		(				)       ->							Tuple:
					'''simple docstring'''

					return _is_package_available('''mlflow'''			)




def lowercase_		(				)       ->							Optional[Any]:
					'''simple docstring'''

					return is_torch_version('''>='''     ,    '''1.12'''			) and torch.backends.mps.is_available() and torch.backends.mps.is_built()




def lowercase_		(				)       ->							Dict:
					'''simple docstring'''

					def get_major_and_minor_from_version(_lowercase			):
										return str(version.parse(_lowercase			).major			) + "." + str(version.parse(_lowercase			).minor			)

					lowerCamelCase_   : Dict       =						importlib.metadata.version('''torch'''			)
					if importlib.util.find_spec('''intel_extension_for_pytorch'''			) is None:
										return False
					lowerCamelCase_   : List[str]       =						'''N/A'''
					try:
										lowerCamelCase_   : int       =						importlib.metadata.version('''intel_extension_for_pytorch'''			)
					except importlib.metadata.PackageNotFoundError:
										return False
					lowerCamelCase_   : int       =						get_major_and_minor_from_version(_torch_version			)
					lowerCamelCase_   : str       =						get_major_and_minor_from_version(_ipex_version			)
					if torch_major_and_minor != ipex_major_and_minor:
										warnings.warn(
										    F"""Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,"""
										    F""" but PyTorch {_torch_version} is found. Please switch to the matching version and run again."""			)
										return False
					return True




@lru_cache
def lowercase_		(				_lowercase=False			)       ->							str:
					'''simple docstring'''

					if importlib.util.find_spec('''torch'''			) is None or importlib.util.find_spec('''torch_npu'''			) is None:
										return False

					import torch
					import torch_npu  # noqa: F401

					if check_device:
										try:
															# Will raise a RuntimeError if no NPU is found
															lowerCamelCase_   : str       =						torch.npu.device_count()
															return torch.npu.is_available()
										except RuntimeError:
															return False
					return hasattr(_lowercase     ,    '''npu'''			) and torch.npu.is_available()






@lru_cache
def lowercase_		(				_lowercase=False			)       ->							Dict:
					'''simple docstring'''

					if not parse_flag_from_env('''ACCELERATE_USE_XPU'''     ,    default=_lowercase			):
										return False
					if is_ipex_available():
										import torch

										if is_torch_version('''<='''     ,    '''1.12'''			):
															return False
					else:
										return False

					import intel_extension_for_pytorch  # noqa: F401

					if check_device:
										try:
															# Will raise a RuntimeError if no XPU  is found
															lowerCamelCase_   : Optional[int]       =						torch.xpu.device_count()
															return torch.xpu.is_available()
										except RuntimeError:
															return False
					return hasattr(_lowercase     ,    '''xpu'''			) and torch.xpu.is_available()




'''simple docstring'''


__lowercase							:				str					=       [
    ['''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 lowercase_		(				_lowercase			)       ->							Optional[int]:
					'''simple docstring'''

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




def lowercase_		(				_lowercase     ,    _lowercase			)       ->							int:
					'''simple docstring'''

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




def lowercase_		(				_lowercase			)       ->							Optional[int]:
					'''simple docstring'''

					lowerCamelCase_   : int       =						get_distinct_edge(_lowercase			)
					lowerCamelCase_   : Any       =						{}

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




def lowercase_		(				_lowercase			)       ->							Any:
					'''simple docstring'''

					lowerCamelCase_   : Dict       =						{}
					for _, item in enumerate(_lowercase			):
										nodes.setdefault(item[2]     ,    []			).append(item[0]			)
					return nodes




def lowercase_		(				_lowercase			)       ->							str:
					'''simple docstring'''

					lowerCamelCase_   : List[Any]       =						{}
					for key, value in nodes.items():
										lowerCamelCase_   : Optional[int]       =						value
					return cluster




def lowercase_		(				_lowercase			)       ->							Optional[int]:
					'''simple docstring'''

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




def lowercase_		(				)       ->							None:
					'''simple docstring'''

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




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase			)       ->							Tuple:
					'''simple docstring'''

					for i in cluster[ca]:
										lowerCamelCase_   : str       =						0
										lowerCamelCase_   : Optional[Any]       =						ca + 1
										while ca < max(cluster.keys()			):
															for j in cluster[ca]:
																				if int(_lowercase     ,    2			) & int(_lowercase     ,    2			) == int(_lowercase     ,    2			):
																									if tuple(nodes[i]			) in graph:
																														graph[tuple(nodes[i]			)].append(nodes[j]			)
																									else:
																														lowerCamelCase_   : Union[str, Any]       =						[nodes[j]]
																									count += 1
															if count == 0:
																				lowerCamelCase_   : Any       =						ca + 1
															else:
																				break




def lowercase_		(				_lowercase     ,    _lowercase			)       ->							Dict:
					'''simple docstring'''

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




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase=None			)       ->							Dict:
					'''simple docstring'''

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




def lowercase_		(				_lowercase     ,    _lowercase     ,    _lowercase			)       ->							int:
					'''simple docstring'''

					lowerCamelCase_   : Dict       =						int(s / 100 * (len(_lowercase			) - 1)			)
					for i in cluster[k]:
										my_dfs(_lowercase     ,    tuple(cluster[k][i]			)     ,    (['''Header'''],)			)




def lowercase_		(				_lowercase			)       ->							Optional[Any]:
					'''simple docstring'''

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




def lowercase_		(				_lowercase			)       ->							int:
					'''simple docstring'''

					for i in range(len(_lowercase			)			):
										for j in range(len(edge_array[i]			)			):
															lowerCamelCase_   : Any       =						edge_array[i][j].split('''-'''			)
															lowerCamelCase_   : Optional[Any]       =						t


if __name__ == "__main__":
					preprocess(edge_array)
					__lowercase							:				List[Any]					=       get_frequency_table(edge_array)
					__lowercase							:				Optional[Any]					=       get_nodes(frequency_table)
					__lowercase							:				int					=       get_cluster(nodes)
					__lowercase							:				Any					=       get_support(cluster)
					__lowercase							:				int					=       construct_graph(cluster, nodes)
					find_freq_subgraph_given_support(60, cluster, graph)
					__lowercase							:				list					=       []
					__lowercase							:				Optional[int]					=       freq_subgraphs_edge_list(paths)
					print_all()


