


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

class 		__a				:
		def __init__(      self						)			->	str:




									"""simple docstring"""





									_UpperCAmelCase					=      {}
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=1						)			->	Union[str, Any]:




									"""simple docstring"""





									if self.graph.get(_SCREAMING_SNAKE_CASE						):
																if self.graph[u].count([w, v]						) == 0:
																							self.graph[u].append([w, v]						)
									else:
																_UpperCAmelCase					=      [[w, v]]
									if not self.graph.get(_SCREAMING_SNAKE_CASE						):
																_UpperCAmelCase					=      []
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									return list(self.graph						)
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)			->	Optional[int]:




									"""simple docstring"""





									if self.graph.get(_SCREAMING_SNAKE_CASE						):
																for _ in self.graph[u]:
																							if _[1] == v:
																														self.graph[u].remove(_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2      ,   _SCREAMING_SNAKE_CASE=-1						)			->	List[str]:




									"""simple docstring"""





									if s == d:
																return []
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      []
									if s == -2:
																_UpperCAmelCase					=      list(self.graph						)[0]
									stack.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      s

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

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

																# check if se have reached the starting point
																if len(_SCREAMING_SNAKE_CASE						) == 0:
																							return visited
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-1						)			->	int:




									"""simple docstring"""





									if c == -1:
																_UpperCAmelCase					=      floor(random() * 10000						) + 10
									for i in range(_SCREAMING_SNAKE_CASE						):
																# every vertex has max 100 edges
																for _ in range(floor(random() * 102						) + 1						):
																							_UpperCAmelCase					=      floor(random() * c						) + 1
																							if n != i:
																														self.add_pair(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   1						)
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2						)			->	str:




									"""simple docstring"""





									_UpperCAmelCase					=      deque()
									_UpperCAmelCase					=      []
									if s == -2:
																_UpperCAmelCase					=      list(self.graph						)[0]
									d.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									while d:
																_UpperCAmelCase					=      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      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      0
									for x in self.graph:
																for y in self.graph[x]:
																							if y[1] == u:
																														count += 1
									return count
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE						)			->	List[str]:




									"""simple docstring"""





									return len(self.graph[u]						)
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2						)			->	str:




									"""simple docstring"""





									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      []
									if s == -2:
																_UpperCAmelCase					=      list(self.graph						)[0]
									stack.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      s
									_UpperCAmelCase					=      []

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

            # check if all the children are visited
																if s == ss:
																							sorted_nodes.append(stack.pop()						)
																							if len(_SCREAMING_SNAKE_CASE						) != 0:
																														_UpperCAmelCase					=      stack[len(_SCREAMING_SNAKE_CASE						) - 1]
																else:
																							_UpperCAmelCase					=      ss

																# check if se have reached the starting point
																if len(_SCREAMING_SNAKE_CASE						) == 0:
																							return sorted_nodes
		def      UpperCAmelCase__    (      self						)			->	Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      list(self.graph						)[0]
									stack.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      -2
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      s
									_UpperCAmelCase					=      False
									_UpperCAmelCase					=      set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]						) != 0:
																							_UpperCAmelCase					=      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
																														):
																																					_UpperCAmelCase					=      len(_SCREAMING_SNAKE_CASE						) - 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]						)
																																					_UpperCAmelCase					=      node[1]
																																					break

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

																# check if se have reached the starting point
																if len(_SCREAMING_SNAKE_CASE						) == 0:
																							return list(_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      list(self.graph						)[0]
									stack.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      -2
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      s
									_UpperCAmelCase					=      False
									_UpperCAmelCase					=      set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]						) != 0:
																							_UpperCAmelCase					=      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
																														):
																																					_UpperCAmelCase					=      len(_SCREAMING_SNAKE_CASE						) - 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]						)
																																					_UpperCAmelCase					=      node[1]
																																					break

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

																# check if se have reached the starting point
																if len(_SCREAMING_SNAKE_CASE						) == 0:
																							return False
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2      ,   _SCREAMING_SNAKE_CASE=-1						)			->	List[str]:




									"""simple docstring"""





									_UpperCAmelCase					=      time()
									self.dfs(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      time()
									return end - begin






		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2						)			->	Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      time()
									self.bfs(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      time()
									return end - begin



class 		__a				:
		def __init__(      self						)			->	List[str]:




									"""simple docstring"""





									_UpperCAmelCase					=      {}
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=1						)			->	Dict:




									"""simple docstring"""





									if self.graph.get(_SCREAMING_SNAKE_CASE						):
																# 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
																_UpperCAmelCase					=      [[w, v]]
									# add the other way
									if self.graph.get(_SCREAMING_SNAKE_CASE						):
																# 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
																_UpperCAmelCase					=      [[w, u]]
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)			->	Any:




									"""simple docstring"""





									if self.graph.get(_SCREAMING_SNAKE_CASE						):
																for _ in self.graph[u]:
																							if _[1] == v:
																														self.graph[u].remove(_SCREAMING_SNAKE_CASE						)
        # the other way round
									if self.graph.get(_SCREAMING_SNAKE_CASE						):
																for _ in self.graph[v]:
																							if _[1] == u:
																														self.graph[v].remove(_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2      ,   _SCREAMING_SNAKE_CASE=-1						)			->	Union[str, Any]:




									"""simple docstring"""





									if s == d:
																return []
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      []
									if s == -2:
																_UpperCAmelCase					=      list(self.graph						)[0]
									stack.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      s

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

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

																# check if se have reached the starting point
																if len(_SCREAMING_SNAKE_CASE						) == 0:
																							return visited
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-1						)			->	Any:




									"""simple docstring"""





									if c == -1:
																_UpperCAmelCase					=      floor(random() * 10000						) + 10
									for i in range(_SCREAMING_SNAKE_CASE						):
																# every vertex has max 100 edges
																for _ in range(floor(random() * 102						) + 1						):
																							_UpperCAmelCase					=      floor(random() * c						) + 1
																							if n != i:
																														self.add_pair(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   1						)
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2						)			->	Any:




									"""simple docstring"""





									_UpperCAmelCase					=      deque()
									_UpperCAmelCase					=      []
									if s == -2:
																_UpperCAmelCase					=      list(self.graph						)[0]
									d.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									while d:
																_UpperCAmelCase					=      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      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE						)			->	List[Any]:




									"""simple docstring"""





									return len(self.graph[u]						)
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      list(self.graph						)[0]
									stack.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      -2
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      s
									_UpperCAmelCase					=      False
									_UpperCAmelCase					=      set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]						) != 0:
																							_UpperCAmelCase					=      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
																														):
																																					_UpperCAmelCase					=      len(_SCREAMING_SNAKE_CASE						) - 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]						)
																																					_UpperCAmelCase					=      node[1]
																																					break

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

																# check if se have reached the starting point
																if len(_SCREAMING_SNAKE_CASE						) == 0:
																							return list(_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      list(self.graph						)[0]
									stack.append(_SCREAMING_SNAKE_CASE						)
									visited.append(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      -2
									_UpperCAmelCase					=      []
									_UpperCAmelCase					=      s
									_UpperCAmelCase					=      False
									_UpperCAmelCase					=      set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]						) != 0:
																							_UpperCAmelCase					=      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
																														):
																																					_UpperCAmelCase					=      len(_SCREAMING_SNAKE_CASE						) - 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]						)
																																					_UpperCAmelCase					=      node[1]
																																					break

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

																# check if se have reached the starting point
																if len(_SCREAMING_SNAKE_CASE						) == 0:
																							return False
		def      UpperCAmelCase__    (      self						)			->	Optional[int]:




									"""simple docstring"""





									return list(self.graph						)
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2      ,   _SCREAMING_SNAKE_CASE=-1						)			->	List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      time()
									self.dfs(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      time()
									return end - begin






		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE=-2						)			->	Any:




									"""simple docstring"""





									_UpperCAmelCase					=      time()
									self.bfs(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      time()
									return end - begin




import argparse

import torch

from transformers import BertForMaskedLM


if __name__ == "__main__":
		lowerCAmelCase__  :Tuple             =							argparse.ArgumentParser(
		    description=(
		        '''Extraction some layers of the full BertForMaskedLM or RObertaForMaskedLM for Transfer Learned'''
		        ''' Distillation'''
		    )
		)
		parser.add_argument('''--model_type''', default='''bert''', choices=['''bert'''])
		parser.add_argument('''--model_name''', default='''bert-base-uncased''', type=str)
		parser.add_argument('''--dump_checkpoint''', default='''serialization_dir/tf_bert-base-uncased_0247911.pth''', type=str)
		parser.add_argument('''--vocab_transform''', action='''store_true''')
		lowerCAmelCase__  :Optional[int]             =							parser.parse_args()

		if args.model_type == "bert":
				lowerCAmelCase__  :Tuple             =							BertForMaskedLM.from_pretrained(args.model_name)
				lowerCAmelCase__  :Optional[int]             =							'''bert'''
		else:
				raise ValueError('''args.model_type should be "bert".''')

		lowerCAmelCase__  :Any             =							model.state_dict()
		lowerCAmelCase__  :Dict             =							{}

		for w in ["word_embeddings", "position_embeddings"]:
				lowerCAmelCase__  :List[Any]             =							state_dict[f'''{prefix}.embeddings.{w}.weight''']
		for w in ["weight", "bias"]:
				lowerCAmelCase__  :Union[str, Any]             =							state_dict[f'''{prefix}.embeddings.LayerNorm.{w}''']

		lowerCAmelCase__  :str             =							0
		for teacher_idx in [0, 2, 4, 7, 9, 1_1]:
				for w in ["weight", "bias"]:
						lowerCAmelCase__  :Any             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.self.query.{w}'''
						]
						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.self.key.{w}'''
						]
						lowerCAmelCase__  :List[str]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.self.value.{w}'''
						]

						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.output.dense.{w}'''
						]
						lowerCAmelCase__  :int             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.output.LayerNorm.{w}'''
						]

						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.intermediate.dense.{w}'''
						]
						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.output.dense.{w}'''
						]
						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.output.LayerNorm.{w}'''
						]
				std_idx += 1

		lowerCAmelCase__  :Optional[int]             =							state_dict['''cls.predictions.decoder.weight''']
		lowerCAmelCase__  :List[str]             =							state_dict['''cls.predictions.bias''']
		if args.vocab_transform:
				for w in ["weight", "bias"]:
						lowerCAmelCase__  :Any             =							state_dict[f'''cls.predictions.transform.dense.{w}''']
						lowerCAmelCase__  :List[str]             =							state_dict[f'''cls.predictions.transform.LayerNorm.{w}''']

		print(f'''N layers selected for distillation: {std_idx}''')
		print(f'''Number of params transferred for distillation: {len(compressed_sd.keys())}''')

		print(f'''Save transferred checkpoint to {args.dump_checkpoint}.''')
		torch.save(compressed_sd, args.dump_checkpoint)
