






import argparse
import os

import jax as jnp
import numpy as onp
import torch
import torch.nn as nn
from music_spectrogram_diffusion import inference
from tax import checkpoints

from diffusers import DDPMScheduler, OnnxRuntimeModel, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder


__A  			=					'''base_with_context'''




def   __A       (  _lowercase       ,      _lowercase			):

					'''simple docstring'''







					_A										=			nn.Parameter(torch.FloatTensor(weights['''token_embedder''']['''embedding''']			)			)
					_A										=			nn.Parameter(
					    torch.FloatTensor(weights['''Embed_0''']['''embedding''']			)       ,      requires_grad=A_			)
					for lyr_num, lyr in enumerate(model.encoders			):
										_A										=			weights[f"""layers_{lyr_num}"""]
										_A										=			nn.Parameter(
										    torch.FloatTensor(ly_weight['''pre_attention_layer_norm''']['''scale''']			)			)

										_A										=			ly_weight['''attention''']
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T			)			)

										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale''']			)			)

										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T			)			)

					_A										=			nn.Parameter(torch.FloatTensor(weights['''encoder_norm''']['''scale''']			)			)
					return model




def   __A       (  _lowercase       ,      _lowercase			):

					'''simple docstring'''







					_A										=			nn.Parameter(torch.FloatTensor(weights['''input_proj''']['''kernel'''].T			)			)

					_A										=			nn.Parameter(
					    torch.FloatTensor(weights['''Embed_0''']['''embedding''']			)       ,      requires_grad=A_			)

					for lyr_num, lyr in enumerate(model.encoders			):
										_A										=			weights[f"""layers_{lyr_num}"""]
										_A										=			ly_weight['''attention''']

										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(
										    torch.FloatTensor(ly_weight['''pre_attention_layer_norm''']['''scale''']			)			)

										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale''']			)			)

					_A										=			nn.Parameter(torch.FloatTensor(weights['''encoder_norm''']['''scale''']			)			)

					return model




def   __A       (  _lowercase       ,      _lowercase			):

					'''simple docstring'''







					_A										=			nn.Parameter(torch.FloatTensor(weights['''time_emb_dense0''']['''kernel'''].T			)			)
					_A										=			nn.Parameter(torch.FloatTensor(weights['''time_emb_dense1''']['''kernel'''].T			)			)

					_A										=			nn.Parameter(
					    torch.FloatTensor(weights['''Embed_0''']['''embedding''']			)       ,      requires_grad=A_			)

					_A										=			nn.Parameter(
					    torch.FloatTensor(weights['''continuous_inputs_projection''']['''kernel'''].T			)			)

					for lyr_num, lyr in enumerate(model.decoders			):
										_A										=			weights[f"""layers_{lyr_num}"""]
										_A										=			nn.Parameter(
										    torch.FloatTensor(ly_weight['''pre_self_attention_layer_norm''']['''scale''']			)			)

										_A										=			nn.Parameter(
										    torch.FloatTensor(ly_weight['''FiLMLayer_0''']['''DenseGeneral_0''']['''kernel'''].T			)			)

										_A										=			ly_weight['''self_attention''']
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T			)			)

										_A										=			ly_weight['''MultiHeadDotProductAttention_0''']
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(
										    torch.FloatTensor(ly_weight['''pre_cross_attention_layer_norm''']['''scale''']			)			)

										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale''']			)			)
										_A										=			nn.Parameter(
										    torch.FloatTensor(ly_weight['''FiLMLayer_1''']['''DenseGeneral_0''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T			)			)
										_A										=			nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T			)			)

					_A										=			nn.Parameter(torch.FloatTensor(weights['''decoder_norm''']['''scale''']			)			)

					_A										=			nn.Parameter(torch.FloatTensor(weights['''spec_out_dense''']['''kernel'''].T			)			)

					return model




def   __A       (  _lowercase			):

					'''simple docstring'''







					_A										=			checkpoints.load_tax_checkpoint(args.checkpoint_path			)
					_A										=			jnp.tree_util.tree_map(onp.array       ,      A_			)

					_A										=			[
					    '''from __gin__ import dynamic_registration''',
					    '''from music_spectrogram_diffusion.models.diffusion import diffusion_utils''',
					    '''diffusion_utils.ClassifierFreeGuidanceConfig.eval_condition_weight = 2.0''',
					    '''diffusion_utils.DiffusionConfig.classifier_free_guidance = @diffusion_utils.ClassifierFreeGuidanceConfig()''',
					]

					_A										=			os.path.join(args.checkpoint_path       ,      '''..'''       ,      '''config.gin'''			)
					_A										=			inference.parse_training_gin_file(A_       ,      A_			)
					_A										=			inference.InferenceModel(args.checkpoint_path       ,      A_			)

					_A										=			DDPMScheduler(beta_schedule='''squaredcos_cap_v2'''       ,      variance_type='''fixed_large'''			)

					_A										=			SpectrogramNotesEncoder(
					    max_length=synth_model.sequence_length['''inputs''']       ,      vocab_size=synth_model.model.module.config.vocab_size       ,      d_model=synth_model.model.module.config.emb_dim       ,      dropout_rate=synth_model.model.module.config.dropout_rate       ,      num_layers=synth_model.model.module.config.num_encoder_layers       ,      num_heads=synth_model.model.module.config.num_heads       ,      d_kv=synth_model.model.module.config.head_dim       ,      d_ff=synth_model.model.module.config.mlp_dim       ,      feed_forward_proj='''gated-gelu'''       ,      )

					_A										=			SpectrogramContEncoder(
					    input_dims=synth_model.audio_codec.n_dims       ,      targets_context_length=synth_model.sequence_length['''targets_context''']       ,      d_model=synth_model.model.module.config.emb_dim       ,      dropout_rate=synth_model.model.module.config.dropout_rate       ,      num_layers=synth_model.model.module.config.num_encoder_layers       ,      num_heads=synth_model.model.module.config.num_heads       ,      d_kv=synth_model.model.module.config.head_dim       ,      d_ff=synth_model.model.module.config.mlp_dim       ,      feed_forward_proj='''gated-gelu'''       ,      )

					_A										=			TaFilmDecoder(
					    input_dims=synth_model.audio_codec.n_dims       ,      targets_length=synth_model.sequence_length['''targets_context''']       ,      max_decoder_noise_time=synth_model.model.module.config.max_decoder_noise_time       ,      d_model=synth_model.model.module.config.emb_dim       ,      num_layers=synth_model.model.module.config.num_decoder_layers       ,      num_heads=synth_model.model.module.config.num_heads       ,      d_kv=synth_model.model.module.config.head_dim       ,      d_ff=synth_model.model.module.config.mlp_dim       ,      dropout_rate=synth_model.model.module.config.dropout_rate       ,      )

					_A										=			load_notes_encoder(ta_checkpoint['''target''']['''token_encoder''']       ,      A_			)
					_A										=			load_continuous_encoder(ta_checkpoint['''target''']['''continuous_encoder''']       ,      A_			)
					_A										=			load_decoder(ta_checkpoint['''target''']['''decoder''']       ,      A_			)

					_A										=			OnnxRuntimeModel.from_pretrained('''kashif/soundstream_mel_decoder'''			)

					_A										=			SpectrogramDiffusionPipeline(
					    notes_encoder=A_       ,      continuous_encoder=A_       ,      decoder=A_       ,      scheduler=A_       ,      melgan=A_       ,      )
					if args.save:
										pipe.save_pretrained(args.output_path			)


if __name__ == "__main__":
				__A  			=					argparse.ArgumentParser()

				parser.add_argument('--output_path', default=None, type=str, required=True, help='Path to the converted model.')
				parser.add_argument(
				    '--save', default=True, type=bool, required=False, help='Whether to save the converted model or not.'
				)
				parser.add_argument(
				    '--checkpoint_path',
				    default=f'{MODEL}/checkpoint_500000',
				    type=str,
				    required=False,
				    help='Path to the original jax model checkpoint.',
				)
				__A  			=					parser.parse_args()

				main(args)






"""simple docstring"""





from collections import deque
from math import floor
from random import random
from time import time







class 							SCREAMING_SNAKE_CASE   :






					"""simple docstring"""



					def __init__(			self     :							Union[str, Any]						):
						lowerCAmelCase__      :		List[str]    							=      {}







					def 			__lowerCAmelCase					(			self     :							Optional[Any]  ,lowercase_     :							Optional[Any]  ,lowercase_     :							str  ,lowercase_     :							List[str]=1						):
						if self.graph.get(lowercase_						):
							if self.graph[u].count([w, v]						) == 0:
								self.graph[u].append([w, v]						)
						else:
							lowerCAmelCase__      :		Optional[int]    							=      [[w, v]]
						if not self.graph.get(lowercase_						):
							lowerCAmelCase__      :		Union[str, Any]    							=      []







					def 			__lowerCAmelCase					(			self     :							List[Any]						):
						return list(self.graph						)







					def 			__lowerCAmelCase					(			self     :							Dict  ,lowercase_     :							Tuple  ,lowercase_     :							Tuple						):
						if self.graph.get(lowercase_						):
							for _ in self.graph[u]:
								if _[1] == v:
									self.graph[u].remove(lowercase_						)







					def 			__lowerCAmelCase					(			self     :							int  ,lowercase_     :							Any=-2  ,lowercase_     :							List[Any]=-1						):
						if s == d:
							return []
						lowerCAmelCase__      :		int    							=      []
						lowerCAmelCase__      :		List[Any]    							=      []
						if s == -2:
							lowerCAmelCase__      :		int    							=      list(self.graph						)[0]
						stack.append(lowercase_						)
						visited.append(lowercase_						)
						lowerCAmelCase__      :		int    							=      s

						while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]						) != 0:
								lowerCAmelCase__      :		List[Any]    							=      s
								for node in self.graph[s]:
									if visited.count(node[1]						) < 1:
										if node[1] == d:
											visited.append(lowercase_						)
											return visited
										else:
											stack.append(node[1]						)
											visited.append(node[1]						)
											lowerCAmelCase__      :		Optional[int]    							=      node[1]
											break

            # check if all the children are visited
							if s == ss:
								stack.pop()
								if len(lowercase_						) != 0:
									lowerCAmelCase__      :		str    							=      stack[len(lowercase_						) - 1]
							else:
								lowerCAmelCase__      :		Union[str, Any]    							=      ss

							# check if se have reached the starting point
							if len(lowercase_						) == 0:
								return visited







					def 			__lowerCAmelCase					(			self     :							Dict  ,lowercase_     :							Optional[Any]=-1						):
						if c == -1:
							lowerCAmelCase__      :		Any    							=      floor(random() * 1_0_0_0_0						) + 1_0
						for i in range(lowercase_						):
							# every vertex has max 100 edges
							for _ in range(floor(random() * 1_0_2						) + 1						):
								lowerCAmelCase__      :		Any    							=      floor(random() * c						) + 1
								if n != i:
									self.add_pair(lowercase_  ,lowercase_  ,1						)







					def 			__lowerCAmelCase					(			self     :							Optional[Any]  ,lowercase_     :							Union[str, Any]=-2						):
						lowerCAmelCase__      :		Tuple    							=      deque()
						lowerCAmelCase__      :		Tuple    							=      []
						if s == -2:
							lowerCAmelCase__      :		Any    							=      list(self.graph						)[0]
						d.append(lowercase_						)
						visited.append(lowercase_						)
						while d:
							lowerCAmelCase__      :		Dict    							=      d.popleft()
							if len(self.graph[s]						) != 0:
								for node in self.graph[s]:
									if visited.count(node[1]						) < 1:
										d.append(node[1]						)
										visited.append(node[1]						)
						return visited







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]  ,lowercase_     :							Optional[Any]						):
						lowerCAmelCase__      :		Optional[Any]    							=      0
						for x in self.graph:
							for y in self.graph[x]:
								if y[1] == u:
									count += 1
						return count







					def 			__lowerCAmelCase					(			self     :							str  ,lowercase_     :							Optional[int]						):
						return len(self.graph[u]						)







					def 			__lowerCAmelCase					(			self     :							List[str]  ,lowercase_     :							List[str]=-2						):
						lowerCAmelCase__      :		Union[str, Any]    							=      []
						lowerCAmelCase__      :		Any    							=      []
						if s == -2:
							lowerCAmelCase__      :		Any    							=      list(self.graph						)[0]
						stack.append(lowercase_						)
						visited.append(lowercase_						)
						lowerCAmelCase__      :		Tuple    							=      s
						lowerCAmelCase__      :		Optional[int]    							=      []

						while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]						) != 0:
								lowerCAmelCase__      :		Optional[Any]    							=      s
								for node in self.graph[s]:
									if visited.count(node[1]						) < 1:
										stack.append(node[1]						)
										visited.append(node[1]						)
										lowerCAmelCase__      :		Dict    							=      node[1]
										break

            # check if all the children are visited
							if s == ss:
								sorted_nodes.append(stack.pop()						)
								if len(lowercase_						) != 0:
									lowerCAmelCase__      :		Union[str, Any]    							=      stack[len(lowercase_						) - 1]
							else:
								lowerCAmelCase__      :		int    							=      ss

							# check if se have reached the starting point
							if len(lowercase_						) == 0:
								return sorted_nodes







					def 			__lowerCAmelCase					(			self     :							List[Any]						):
						lowerCAmelCase__      :		str    							=      []
						lowerCAmelCase__      :		List[str]    							=      []
						lowerCAmelCase__      :		int    							=      list(self.graph						)[0]
						stack.append(lowercase_						)
						visited.append(lowercase_						)
						lowerCAmelCase__      :		List[Any]    							=      -2
						lowerCAmelCase__      :		str    							=      []
						lowerCAmelCase__      :		Optional[Any]    							=      s
						lowerCAmelCase__      :		Union[str, Any]    							=      False
						lowerCAmelCase__      :		Optional[int]    							=      set()

						while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]						) != 0:
								lowerCAmelCase__      :		List[str]    							=      s
								for node in self.graph[s]:
									if (
									    visited.count(node[1]						) > 0
									    and node[1] != parent
									    and indirect_parents.count(node[1]						) > 0
									    and not on_the_way_back
									):
										lowerCAmelCase__      :		List[Any]    							=      len(lowercase_						) - 1
										while len_stack >= 0:
											if stack[len_stack] == node[1]:
												anticipating_nodes.add(node[1]						)
												break
											else:
												anticipating_nodes.add(stack[len_stack]						)
												len_stack -= 1
									if visited.count(node[1]						) < 1:
										stack.append(node[1]						)
										visited.append(node[1]						)
										lowerCAmelCase__      :		Optional[int]    							=      node[1]
										break

            # check if all the children are visited
							if s == ss:
								stack.pop()
								lowerCAmelCase__      :		str    							=      True
								if len(lowercase_						) != 0:
									lowerCAmelCase__      :		Tuple    							=      stack[len(lowercase_						) - 1]
							else:
								lowerCAmelCase__      :		int    							=      False
								indirect_parents.append(lowercase_						)
								lowerCAmelCase__      :		str    							=      s
								lowerCAmelCase__      :		Tuple    							=      ss

							# check if se have reached the starting point
							if len(lowercase_						) == 0:
								return list(lowercase_						)







					def 			__lowerCAmelCase					(			self     :							int						):
						lowerCAmelCase__      :		Tuple    							=      []
						lowerCAmelCase__      :		Tuple    							=      []
						lowerCAmelCase__      :		Any    							=      list(self.graph						)[0]
						stack.append(lowercase_						)
						visited.append(lowercase_						)
						lowerCAmelCase__      :		List[str]    							=      -2
						lowerCAmelCase__      :		int    							=      []
						lowerCAmelCase__      :		int    							=      s
						lowerCAmelCase__      :		List[Any]    							=      False
						lowerCAmelCase__      :		List[str]    							=      set()

						while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]						) != 0:
								lowerCAmelCase__      :		Optional[int]    							=      s
								for node in self.graph[s]:
									if (
									    visited.count(node[1]						) > 0
									    and node[1] != parent
									    and indirect_parents.count(node[1]						) > 0
									    and not on_the_way_back
									):
										lowerCAmelCase__      :		List[Any]    							=      len(lowercase_						) - 1
										while len_stack_minus_one >= 0:
											if stack[len_stack_minus_one] == node[1]:
												anticipating_nodes.add(node[1]						)
												break
											else:
												return True
									if visited.count(node[1]						) < 1:
										stack.append(node[1]						)
										visited.append(node[1]						)
										lowerCAmelCase__      :		Optional[Any]    							=      node[1]
										break

            # check if all the children are visited
							if s == ss:
								stack.pop()
								lowerCAmelCase__      :		List[str]    							=      True
								if len(lowercase_						) != 0:
									lowerCAmelCase__      :		Dict    							=      stack[len(lowercase_						) - 1]
							else:
								lowerCAmelCase__      :		Union[str, Any]    							=      False
								indirect_parents.append(lowercase_						)
								lowerCAmelCase__      :		Tuple    							=      s
								lowerCAmelCase__      :		Union[str, Any]    							=      ss

							# check if se have reached the starting point
							if len(lowercase_						) == 0:
								return False







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							List[Any]=-2  ,lowercase_     :							Union[str, Any]=-1						):
						lowerCAmelCase__      :		Optional[int]    							=      time()
						self.dfs(lowercase_  ,lowercase_						)
						lowerCAmelCase__      :		Any    							=      time()
						return end - begin







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							Dict=-2						):
						lowerCAmelCase__      :		List[str]    							=      time()
						self.bfs(lowercase_						)
						lowerCAmelCase__      :		List[Any]    							=      time()
						return end - begin







class 							SCREAMING_SNAKE_CASE   :






					"""simple docstring"""



					def __init__(			self     :							Union[str, Any]						):
						lowerCAmelCase__      :		Tuple    							=      {}







					def 			__lowerCAmelCase					(			self     :							List[Any]  ,lowercase_     :							Optional[int]  ,lowercase_     :							Dict  ,lowercase_     :							Any=1						):
						# check if the u exists
						if self.graph.get(lowercase_						):
							# if there already is a edge
							if self.graph[u].count([w, v]						) == 0:
								self.graph[u].append([w, v]						)
						else:
							# if u does not exist
							lowerCAmelCase__      :		List[Any]    							=      [[w, v]]
						# add the other way
						if self.graph.get(lowercase_						):
							# if there already is a edge
							if self.graph[v].count([w, u]						) == 0:
								self.graph[v].append([w, u]						)
						else:
							# if u does not exist
							lowerCAmelCase__      :		int    							=      [[w, u]]







					def 			__lowerCAmelCase					(			self     :							Dict  ,lowercase_     :							Union[str, Any]  ,lowercase_     :							Dict						):
						if self.graph.get(lowercase_						):
							for _ in self.graph[u]:
								if _[1] == v:
									self.graph[u].remove(lowercase_						)
        # the other way round
						if self.graph.get(lowercase_						):
							for _ in self.graph[v]:
								if _[1] == u:
									self.graph[v].remove(lowercase_						)







					def 			__lowerCAmelCase					(			self     :							int  ,lowercase_     :							Optional[int]=-2  ,lowercase_     :							str=-1						):
						if s == d:
							return []
						lowerCAmelCase__      :		Optional[Any]    							=      []
						lowerCAmelCase__      :		Any    							=      []
						if s == -2:
							lowerCAmelCase__      :		Tuple    							=      list(self.graph						)[0]
						stack.append(lowercase_						)
						visited.append(lowercase_						)
						lowerCAmelCase__      :		int    							=      s

						while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]						) != 0:
								lowerCAmelCase__      :		Optional[int]    							=      s
								for node in self.graph[s]:
									if visited.count(node[1]						) < 1:
										if node[1] == d:
											visited.append(lowercase_						)
											return visited
										else:
											stack.append(node[1]						)
											visited.append(node[1]						)
											lowerCAmelCase__      :		List[str]    							=      node[1]
											break

            # check if all the children are visited
							if s == ss:
								stack.pop()
								if len(lowercase_						) != 0:
									lowerCAmelCase__      :		str    							=      stack[len(lowercase_						) - 1]
							else:
								lowerCAmelCase__      :		Dict    							=      ss

							# check if se have reached the starting point
							if len(lowercase_						) == 0:
								return visited







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]  ,lowercase_     :							List[Any]=-1						):
						if c == -1:
							lowerCAmelCase__      :		str    							=      floor(random() * 1_0_0_0_0						) + 1_0
						for i in range(lowercase_						):
							# every vertex has max 100 edges
							for _ in range(floor(random() * 1_0_2						) + 1						):
								lowerCAmelCase__      :		List[str]    							=      floor(random() * c						) + 1
								if n != i:
									self.add_pair(lowercase_  ,lowercase_  ,1						)







					def 			__lowerCAmelCase					(			self     :							Optional[Any]  ,lowercase_     :							List[Any]=-2						):
						lowerCAmelCase__      :		Tuple    							=      deque()
						lowerCAmelCase__      :		List[Any]    							=      []
						if s == -2:
							lowerCAmelCase__      :		Tuple    							=      list(self.graph						)[0]
						d.append(lowercase_						)
						visited.append(lowercase_						)
						while d:
							lowerCAmelCase__      :		Any    							=      d.popleft()
							if len(self.graph[s]						) != 0:
								for node in self.graph[s]:
									if visited.count(node[1]						) < 1:
										d.append(node[1]						)
										visited.append(node[1]						)
						return visited







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							Any						):
						return len(self.graph[u]						)







					def 			__lowerCAmelCase					(			self     :							Any						):
						lowerCAmelCase__      :		List[Any]    							=      []
						lowerCAmelCase__      :		int    							=      []
						lowerCAmelCase__      :		List[Any]    							=      list(self.graph						)[0]
						stack.append(lowercase_						)
						visited.append(lowercase_						)
						lowerCAmelCase__      :		Optional[int]    							=      -2
						lowerCAmelCase__      :		List[str]    							=      []
						lowerCAmelCase__      :		Tuple    							=      s
						lowerCAmelCase__      :		Tuple    							=      False
						lowerCAmelCase__      :		Union[str, Any]    							=      set()

						while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]						) != 0:
								lowerCAmelCase__      :		Union[str, Any]    							=      s
								for node in self.graph[s]:
									if (
									    visited.count(node[1]						) > 0
									    and node[1] != parent
									    and indirect_parents.count(node[1]						) > 0
									    and not on_the_way_back
									):
										lowerCAmelCase__      :		Tuple    							=      len(lowercase_						) - 1
										while len_stack >= 0:
											if stack[len_stack] == node[1]:
												anticipating_nodes.add(node[1]						)
												break
											else:
												anticipating_nodes.add(stack[len_stack]						)
												len_stack -= 1
									if visited.count(node[1]						) < 1:
										stack.append(node[1]						)
										visited.append(node[1]						)
										lowerCAmelCase__      :		Optional[int]    							=      node[1]
										break

            # check if all the children are visited
							if s == ss:
								stack.pop()
								lowerCAmelCase__      :		Optional[int]    							=      True
								if len(lowercase_						) != 0:
									lowerCAmelCase__      :		str    							=      stack[len(lowercase_						) - 1]
							else:
								lowerCAmelCase__      :		Tuple    							=      False
								indirect_parents.append(lowercase_						)
								lowerCAmelCase__      :		Optional[int]    							=      s
								lowerCAmelCase__      :		Optional[int]    							=      ss

							# check if se have reached the starting point
							if len(lowercase_						) == 0:
								return list(lowercase_						)







					def 			__lowerCAmelCase					(			self     :							Optional[Any]						):
						lowerCAmelCase__      :		Optional[Any]    							=      []
						lowerCAmelCase__      :		Union[str, Any]    							=      []
						lowerCAmelCase__      :		Tuple    							=      list(self.graph						)[0]
						stack.append(lowercase_						)
						visited.append(lowercase_						)
						lowerCAmelCase__      :		Dict    							=      -2
						lowerCAmelCase__      :		Union[str, Any]    							=      []
						lowerCAmelCase__      :		Optional[Any]    							=      s
						lowerCAmelCase__      :		Any    							=      False
						lowerCAmelCase__      :		Optional[int]    							=      set()

						while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]						) != 0:
								lowerCAmelCase__      :		str    							=      s
								for node in self.graph[s]:
									if (
									    visited.count(node[1]						) > 0
									    and node[1] != parent
									    and indirect_parents.count(node[1]						) > 0
									    and not on_the_way_back
									):
										lowerCAmelCase__      :		str    							=      len(lowercase_						) - 1
										while len_stack_minus_one >= 0:
											if stack[len_stack_minus_one] == node[1]:
												anticipating_nodes.add(node[1]						)
												break
											else:
												return True
									if visited.count(node[1]						) < 1:
										stack.append(node[1]						)
										visited.append(node[1]						)
										lowerCAmelCase__      :		List[str]    							=      node[1]
										break

            # check if all the children are visited
							if s == ss:
								stack.pop()
								lowerCAmelCase__      :		List[Any]    							=      True
								if len(lowercase_						) != 0:
									lowerCAmelCase__      :		List[str]    							=      stack[len(lowercase_						) - 1]
							else:
								lowerCAmelCase__      :		List[str]    							=      False
								indirect_parents.append(lowercase_						)
								lowerCAmelCase__      :		Optional[int]    							=      s
								lowerCAmelCase__      :		List[Any]    							=      ss

							# check if se have reached the starting point
							if len(lowercase_						) == 0:
								return False







					def 			__lowerCAmelCase					(			self     :							str						):
						return list(self.graph						)







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							Optional[int]=-2  ,lowercase_     :							Any=-1						):
						lowerCAmelCase__      :		Dict    							=      time()
						self.dfs(lowercase_  ,lowercase_						)
						lowerCAmelCase__      :		List[str]    							=      time()
						return end - begin







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]  ,lowercase_     :							int=-2						):
						lowerCAmelCase__      :		Dict    							=      time()
						self.bfs(lowercase_						)
						lowerCAmelCase__      :		Any    							=      time()
						return end - begin




