



"""simple docstring"""






import argparse
import json
import os
from collections import OrderedDict

import numpy as np
import tensorflow as tf
import torch






def 					snake_case__       (		__lowerCamelCase    :   Any     ):
			"""simple docstring"""
			lowerCamelCase__		:		Tuple		     =os.path.join(args.tf_model_dir							,		'''parameters.json'''     )
			lowerCamelCase__		:		List[str]		     =json.loads(open(__lowerCamelCase     ).read()     )
			if not params:
						raise ValueError(
						    f'''It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file.'''     )
			if not args.output.endswith('''.pt'''     ):
						lowerCamelCase__		:		int		     =args.output + '''.pt'''
			lowerCamelCase__		:		Tuple		     =OrderedDict()
			with tf.device('''/CPU:0'''     ):
						lowerCamelCase__		:		str		     =tf.train.load_checkpoint(args.tf_model_dir     )
						lowerCamelCase__		:		List[Any]		     =reader.get_variable_to_shape_map()
						for key_name in shapes.keys():
									lowerCamelCase__		:		Optional[int]		     =reader.get_tensor(__lowerCamelCase     ).astype(np.floataa     )
									if key_name.endswith('''/adam_m'''     ) or key_name.endswith('''/adam_v'''     ):
												continue
									if key_name.startswith('''pasts/'''     ):
												if key_name.startswith('''pasts/mlp'''     ):
															lowerCamelCase__		:		List[Any]		     =int(key_name[9]     )
												elif key_name.startswith('''pasts/out'''     ):
															lowerCamelCase__		:		List[Any]		     =8
												lowerCamelCase__		:		str		     ='''model.sqout.%d.weight''' % (player * 2)  # enter to nn.Sequencial with Tanh, so 2 at a time
												lowerCamelCase__		:		str		     =vnp.transpose([1, 0]     ).copy()  # Mesh-Tensorflow is a diagonal matrix
												lowerCamelCase__		:		Any		     =torch.tensor(__lowerCamelCase     )
									elif key_name.startswith('''model/moe'''     ):
												lowerCamelCase__		:		List[str]		     =int(key_name[9:].split('''/'''     )[0]     )
												if key_name.endswith('''/switch_gating/kernel'''     ):
															lowerCamelCase__		:		Tuple		     ='''model.blocks.%d.feed_forward.mlp.router.classifier.weight''' % player
															lowerCamelCase__		:		Union[str, Any]		     =vnp.transpose([1, 0]     ).copy()  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		Dict		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/softmlp/kernel'''     ):
															lowerCamelCase__		:		Union[str, Any]		     ='''model.blocks.%d.feed_forward.soft_bypass_mlp.weight''' % player
															lowerCamelCase__		:		Tuple		     =vnp.transpose([1, 0]     ).copy()  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		str		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/wo/kernel'''     ) or key_name.endswith('''/wi/kernel'''     ):
															lowerCamelCase__		:		str		     =key_name[-9:-7]
															for i in range(16     ):
																		lowerCamelCase__		:		str		     ='''model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight''' % (player, i, nlayer)
																		lowerCamelCase__		:		Union[str, Any]		     =(
																		    vnp[i].transpose([1, 0]     ).copy()
																		)  # In Mesh-Tensorflow, it is one array, so it is divided
																		lowerCamelCase__		:		Union[str, Any]		     =torch.tensor(__lowerCamelCase     )
									elif key_name.startswith('''model/mlp'''     ):
												lowerCamelCase__		:		Optional[int]		     =int(key_name[9:].split('''/'''     )[0]     )
												if key_name.endswith('''/p1/kernel'''     ):
															lowerCamelCase__		:		int		     ='''model.blocks.%d.feed_forward.mlp.wi.weight''' % player
															lowerCamelCase__		:		int		     =vnp.transpose([1, 0]     ).copy()  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		Tuple		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/p1/bias'''     ):
															lowerCamelCase__		:		List[str]		     ='''model.blocks.%d.feed_forward.mlp.wi.bias''' % player
															lowerCamelCase__		:		List[str]		     =vnp.copy()  # same because it is one dimensional
															lowerCamelCase__		:		Union[str, Any]		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/p2/kernel'''     ):
															lowerCamelCase__		:		Tuple		     ='''model.blocks.%d.feed_forward.mlp.wo.weight''' % player
															lowerCamelCase__		:		List[Any]		     =vnp.transpose([1, 0]     ).copy()  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		str		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/p2/bias'''     ):
															lowerCamelCase__		:		int		     ='''model.blocks.%d.feed_forward.mlp.wo.bias''' % player
															lowerCamelCase__		:		Optional[Any]		     =vnp.copy()  # same because it is one dimensional
															lowerCamelCase__		:		Optional[Any]		     =torch.tensor(__lowerCamelCase     )
									elif key_name.startswith('''model/ln'''     ):
												lowerCamelCase__		:		str		     =int(key_name[8:].split('''/'''     )[0]     )
												if key_name.endswith('''/b'''     ):
															lowerCamelCase__		:		List[str]		     ='''model.blocks.%d.feed_forward.norm.bias''' % player
															lowerCamelCase__		:		Optional[Any]		     =vnp.copy()  # same because it is one dimensional
															lowerCamelCase__		:		Tuple		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/g'''     ):
															lowerCamelCase__		:		str		     ='''model.blocks.%d.feed_forward.norm.weight''' % player
															lowerCamelCase__		:		Dict		     =vnp.copy()  # same because it is one dimensional
															lowerCamelCase__		:		Tuple		     =torch.tensor(__lowerCamelCase     )
									elif key_name.startswith('''model/att'''     ):
												lowerCamelCase__		:		Dict		     =int(key_name[9:].split('''/'''     )[0]     )
												if key_name.endswith('''/qkv/kernel'''     ):
															lowerCamelCase__		:		Dict		     =vnp.copy()  # Compute same dimension as Mesh-tensorflow using einsum
															lowerCamelCase__		:		Union[str, Any]		     =state[:, 0, :, :]
															lowerCamelCase__		:		Any		     =state[:, 1, :, :]
															lowerCamelCase__		:		str		     =state[:, 2, :, :]
															lowerCamelCase__		:		Optional[int]		     =(
															    state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]]     )
															    .transpose([1, 0]     )
															    .copy()
															)  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		str		     =(
															    state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]]     )
															    .transpose([1, 0]     )
															    .copy()
															)  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		Tuple		     =(
															    state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]]     )
															    .transpose([1, 0]     )
															    .copy()
															)  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		Optional[int]		     ='''model.blocks.%d.self_attn.self_attn.q_proj.weight''' % player
															lowerCamelCase__		:		str		     =torch.tensor(__lowerCamelCase     )
															lowerCamelCase__		:		Dict		     ='''model.blocks.%d.self_attn.self_attn.k_proj.weight''' % player
															lowerCamelCase__		:		Tuple		     =torch.tensor(__lowerCamelCase     )
															lowerCamelCase__		:		Optional[int]		     ='''model.blocks.%d.self_attn.self_attn.v_proj.weight''' % player
															lowerCamelCase__		:		Optional[Any]		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/o/kernel'''     ):
															lowerCamelCase__		:		Optional[Any]		     ='''model.blocks.%d.self_attn.self_attn.out_proj.weight''' % player
															lowerCamelCase__		:		Optional[Any]		     =(
															    vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]]     ).transpose([1, 0]     ).copy()
															)  # Mesh-Tensorflow is a diagonal matrix
															lowerCamelCase__		:		Optional[int]		     =torch.tensor(__lowerCamelCase     )
									elif key_name.startswith('''model/an'''     ):
												lowerCamelCase__		:		Any		     =int(key_name[8:].split('''/'''     )[0]     )
												if key_name.endswith('''/b'''     ):
															lowerCamelCase__		:		List[str]		     ='''model.blocks.%d.self_attn.norm.bias''' % player
															lowerCamelCase__		:		str		     =vnp.copy()  # same because it is one dimensional
															lowerCamelCase__		:		Optional[Any]		     =torch.tensor(__lowerCamelCase     )
												elif key_name.endswith('''/g'''     ):
															lowerCamelCase__		:		Optional[Any]		     ='''model.blocks.%d.self_attn.norm.weight''' % player
															lowerCamelCase__		:		Dict		     =vnp.copy()  # same because it is one dimensional
															lowerCamelCase__		:		str		     =torch.tensor(__lowerCamelCase     )
									elif (
									    key_name.startswith('''model/wte'''     )
									    or key_name.startswith('''model/wpe'''     )
									    or key_name.startswith('''model/ete'''     )
									):
												lowerCamelCase__		:		int		     ={'''wte''': '''embed_tokens''', '''wpe''': '''position_embeddings''', '''ete''': '''extra_position_embeddings'''}[
												    key_name[-3:]
												]
												lowerCamelCase__		:		Optional[int]		     ='''model.%s.weight''' % nlayer
												lowerCamelCase__		:		Dict		     =vnp.copy()  # same in embedded
												lowerCamelCase__		:		Dict		     =torch.tensor(__lowerCamelCase     )
												if key_name.startswith('''model/wte'''     ):
															lowerCamelCase__		:		List[Any]		     ='''lm_head.weight'''
															lowerCamelCase__		:		Union[str, Any]		     =vnp.copy()  # same in embedded
															lowerCamelCase__		:		List[str]		     =torch.tensor(__lowerCamelCase     )
									elif key_name.startswith('''model/wob'''     ):
												lowerCamelCase__		:		int		     ='''final_logits_bias'''
												lowerCamelCase__		:		List[str]		     =vnp.copy()  # same in embedded
												lowerCamelCase__		:		Union[str, Any]		     =state.reshape((1, -1)     )
												lowerCamelCase__		:		List[str]		     =torch.tensor(__lowerCamelCase     )
									elif key_name == "model/dense/kernel":
												lowerCamelCase__		:		List[str]		     ='''model.last_project.weight'''
												lowerCamelCase__		:		Any		     =vnp.transpose([1, 0]     ).copy()  # Mesh-Tensorflow is a diagonal matrix
												lowerCamelCase__		:		str		     =torch.tensor(__lowerCamelCase     )
									elif key_name == "model/dense_1/bias":
												lowerCamelCase__		:		Optional[int]		     ='''model.last_project.bias'''
												lowerCamelCase__		:		List[str]		     =vnp.copy()  # same because it is one dimensional
												lowerCamelCase__		:		Any		     =torch.tensor(__lowerCamelCase     )
			torch.save(__lowerCamelCase							,		args.output     )


if __name__ == "__main__":
		_lowercase			:			int										=						argparse.ArgumentParser(
		    description="model converter.", formatter_class=argparse.ArgumentDefaultsHelpFormatter
		)
		parser.add_argument("--tf_model_dir", metavar="PATH", type=str, required=True, help="import model")
		parser.add_argument("--output", metavar="PATH", type=str, required=True, help="output model")
		_lowercase			:			int										=						parser.parse_args()
		convert_tf_gptsan_to_pt(args)









"""simple docstring"""






def 					snake_case__       (		__lowerCamelCase    :   int     ):
			"""simple docstring"""
			lowerCamelCase__		:		List[Any]		     =[0] * len(__lowerCamelCase     )
			lowerCamelCase__		:		List[Any]		     =[]
			lowerCamelCase__		:		List[Any]		     =[1] * len(__lowerCamelCase     )

			for values in graph.values():
						for i in values:
									indegree[i] += 1

			for i in range(len(__lowerCamelCase     )     ):
						if indegree[i] == 0:
									queue.append(__lowerCamelCase     )

			while queue:
						lowerCamelCase__		:		Tuple		     =queue.pop(0     )
						for x in graph[vertex]:
									indegree[x] -= 1

									if long_dist[vertex] + 1 > long_dist[x]:
												lowerCamelCase__		:		Optional[Any]		     =long_dist[vertex] + 1

									if indegree[x] == 0:
												queue.append(__lowerCamelCase     )

			print(max(__lowerCamelCase     )     )


# Adjacency list of Graph
_lowercase			:			Optional[Any]										=						{0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []}

longest_distance(graph)




