'''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						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				{}


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_=1						):




       '''simple docstring'''

       if self.graph.get(snake_case_						):
            if self.graph[u].count([w, v]						) == 0:
                 self.graph[u].append([w, v]						)
       else:
            UpperCAmelCase_							:       Optional[int]       =				[[w, v]]
       if not self.graph.get(snake_case_						):
            UpperCAmelCase_							:       Tuple       =				[]


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return list(self.graph						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       if self.graph.get(snake_case_						):
            for _ in self.graph[u]:
                 if _[1] == v:
                      self.graph[u].remove(snake_case_						)


  def _UpperCamelCase					(			self				,		snake_case_=-2				,		snake_case_=-1						):




       '''simple docstring'''

       if s == d:
            return []
       UpperCAmelCase_							:       Dict       =				[]
       UpperCAmelCase_							:       Dict       =				[]
       if s == -2:
            UpperCAmelCase_							:       List[Any]       =				list(self.graph						)[0]
       stack.append(snake_case_						)
       visited.append(snake_case_						)
       UpperCAmelCase_							:       str       =				s

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

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

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


  def _UpperCamelCase					(			self				,		snake_case_=-1						):




       '''simple docstring'''

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


  def _UpperCamelCase					(			self				,		snake_case_=-2						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				deque()
       UpperCAmelCase_							:       Tuple       =				[]
       if s == -2:
            UpperCAmelCase_							:       List[Any]       =				list(self.graph						)[0]
       d.append(snake_case_						)
       visited.append(snake_case_						)
       while d:
            UpperCAmelCase_							:       List[str]       =				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				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				0
       for x in self.graph:
            for y in self.graph[x]:
                 if y[1] == u:
                      count += 1
       return count


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       return len(self.graph[u]						)


  def _UpperCamelCase					(			self				,		snake_case_=-2						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				[]
       UpperCAmelCase_							:       Optional[Any]       =				[]
       if s == -2:
            UpperCAmelCase_							:       Tuple       =				list(self.graph						)[0]
       stack.append(snake_case_						)
       visited.append(snake_case_						)
       UpperCAmelCase_							:       List[str]       =				s
       UpperCAmelCase_							:       Dict       =				[]

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

            # check if all the children are visited
            if s == ss:
                 sorted_nodes.append(stack.pop()						)
                 if len(snake_case_						) != 0:
                      UpperCAmelCase_							:       Dict       =				stack[len(snake_case_						) - 1]
            else:
                 UpperCAmelCase_							:       List[Any]       =				ss

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				[]
       UpperCAmelCase_							:       List[Any]       =				[]
       UpperCAmelCase_							:       Optional[Any]       =				list(self.graph						)[0]
       stack.append(snake_case_						)
       visited.append(snake_case_						)
       UpperCAmelCase_							:       int       =				-2
       UpperCAmelCase_							:       Union[str, Any]       =				[]
       UpperCAmelCase_							:       List[str]       =				s
       UpperCAmelCase_							:       List[Any]       =				False
       UpperCAmelCase_							:       List[str]       =				set()

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

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

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

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

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

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

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


  def _UpperCamelCase					(			self				,		snake_case_=-2				,		snake_case_=-1						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				time()
       self.dfs(snake_case_				,		snake_case_						)
       UpperCAmelCase_							:       List[Any]       =				time()
       return end - begin






  def _UpperCamelCase					(			self				,		snake_case_=-2						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[int]       =				time()
       self.bfs(snake_case_						)
       UpperCAmelCase_							:       Optional[Any]       =				time()
       return end - begin







class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				{}


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_=1						):




       '''simple docstring'''

       if self.graph.get(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_							:       Dict       =				[[w, v]]
       # add the other way
       if self.graph.get(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_							:       List[str]       =				[[w, u]]


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       if self.graph.get(snake_case_						):
            for _ in self.graph[u]:
                 if _[1] == v:
                      self.graph[u].remove(snake_case_						)
        # the other way round
       if self.graph.get(snake_case_						):
            for _ in self.graph[v]:
                 if _[1] == u:
                      self.graph[v].remove(snake_case_						)


  def _UpperCamelCase					(			self				,		snake_case_=-2				,		snake_case_=-1						):




       '''simple docstring'''

       if s == d:
            return []
       UpperCAmelCase_							:       Tuple       =				[]
       UpperCAmelCase_							:       Any       =				[]
       if s == -2:
            UpperCAmelCase_							:       Optional[Any]       =				list(self.graph						)[0]
       stack.append(snake_case_						)
       visited.append(snake_case_						)
       UpperCAmelCase_							:       Union[str, Any]       =				s

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

            # check if all the children are visited
            if s == ss:
                 stack.pop()
                 if len(snake_case_						) != 0:
                      UpperCAmelCase_							:       int       =				stack[len(snake_case_						) - 1]
            else:
                 UpperCAmelCase_							:       List[str]       =				ss

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


  def _UpperCamelCase					(			self				,		snake_case_=-1						):




       '''simple docstring'''

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


  def _UpperCamelCase					(			self				,		snake_case_=-2						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[Any]       =				deque()
       UpperCAmelCase_							:       str       =				[]
       if s == -2:
            UpperCAmelCase_							:       Union[str, Any]       =				list(self.graph						)[0]
       d.append(snake_case_						)
       visited.append(snake_case_						)
       while d:
            UpperCAmelCase_							:       Optional[int]       =				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				,		snake_case_						):




       '''simple docstring'''

       return len(self.graph[u]						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				[]
       UpperCAmelCase_							:       Dict       =				[]
       UpperCAmelCase_							:       Dict       =				list(self.graph						)[0]
       stack.append(snake_case_						)
       visited.append(snake_case_						)
       UpperCAmelCase_							:       Tuple       =				-2
       UpperCAmelCase_							:       Union[str, Any]       =				[]
       UpperCAmelCase_							:       Dict       =				s
       UpperCAmelCase_							:       Optional[Any]       =				False
       UpperCAmelCase_							:       int       =				set()

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

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

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				[]
       UpperCAmelCase_							:       Tuple       =				[]
       UpperCAmelCase_							:       Dict       =				list(self.graph						)[0]
       stack.append(snake_case_						)
       visited.append(snake_case_						)
       UpperCAmelCase_							:       Any       =				-2
       UpperCAmelCase_							:       Optional[Any]       =				[]
       UpperCAmelCase_							:       Any       =				s
       UpperCAmelCase_							:       Dict       =				False
       UpperCAmelCase_							:       str       =				set()

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

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

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return list(self.graph						)


  def _UpperCamelCase					(			self				,		snake_case_=-2				,		snake_case_=-1						):




       '''simple docstring'''

       UpperCAmelCase_							:       Any       =				time()
       self.dfs(snake_case_				,		snake_case_						)
       UpperCAmelCase_							:       Optional[Any]       =				time()
       return end - begin






  def _UpperCamelCase					(			self				,		snake_case_=-2						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				time()
       self.bfs(snake_case_						)
       UpperCAmelCase_							:       int       =				time()
       return end - begin







'''simple docstring'''


import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
 from sagemaker import Session, TrainingJobAnalytics
 from sagemaker.huggingface import HuggingFace






@pytest.mark.skipif(
    literal_eval(os.getenv('''TEST_SAGEMAKER'''						,     '''False'''	)	) is not True						,     reason='''Skipping test because should only be run when releasing minor transformers version'''						,     )
@pytest.mark.usefixtures('''sm_env'''	)
@parameterized_class(
    [
        {
            '''framework''': '''pytorch''',
            '''script''': '''run_glue.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 650, '''eval_accuracy''': 0.6, '''eval_loss''': 0.9},
        },
        {
            '''framework''': '''tensorflow''',
            '''script''': '''run_tf.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 600, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9},
        },
    ]	)
class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       if self.framework == "pytorch":
            subprocess.run(
                F'''cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'''.split()				,		encoding='utf-8'				,		check=snake_case_				,		)
       assert hasattr(self				,		'env'						)


  def _UpperCamelCase					(			self				,		snake_case_=1						):




       '''simple docstring'''

       return HuggingFace(
           entry_point=self.script				,		source_dir=self.env.test_path				,		role=self.env.role				,		image_uri=self.env.image_uri				,		base_job_name=F'''{self.env.base_job_name}-single'''				,		instance_count=snake_case_				,		instance_type=self.instance_type				,		debugger_hook_config=snake_case_				,		hyperparameters={**self.env.hyperparameters, 'model_name_or_path': self.model_name_or_path}				,		metric_definitions=self.env.metric_definitions				,		py_version='py36'				,		)


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       TrainingJobAnalytics(snake_case_						).export_csv(F'''{self.env.test_path}/{job_name}_metrics.csv'''						)






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				self.create_estimator()

       # run training
       estimator.fit()

       # result dataframe
       UpperCAmelCase_							:       int       =				TrainingJobAnalytics(estimator.latest_training_job.name						).dataframe()

       # extract kpis
       UpperCAmelCase_							:       Dict       =				list(result_metrics_df[result_metrics_df.metric_name == 'eval_accuracy']['value']						)
       UpperCAmelCase_							:       List[str]       =				list(result_metrics_df[result_metrics_df.metric_name == 'eval_loss']['value']						)
       # get train time from SageMaker job, this includes starting, preprocessing, stopping
       UpperCAmelCase_							:       int       =				(
           Session().describe_training_job(estimator.latest_training_job.name						).get('TrainingTimeInSeconds'				,		9_9_9_9_9_9						)
       )

       # assert kpis
       assert train_runtime <= self.results["train_runtime"]
       assert all(t >= self.results['eval_accuracy'] for t in eval_accuracy						)
       assert all(t <= self.results['eval_loss'] for t in eval_loss						)

       # dump tests result into json file to share in PR
       with open(F'''{estimator.latest_training_job.name}.json'''				,		'w'						) as outfile:
            json.dump({'train_time': train_runtime, 'eval_accuracy': eval_accuracy, 'eval_loss': eval_loss}				,		snake_case_						)






