'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case__					:      List[str]				      =  logging.get_logger(__name__)

snake_case__					:      Optional[int]				      =  {
    '''tiiuae/falcon-40b''': '''https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json''',
    '''tiiuae/falcon-7b''': '''https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json''',
}






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:str						   =		'''falcon'''
  lowerCamelCase_		:Optional[Any]						   =		['''past_key_values''']


  def __init__(			self				,		snake_case_=6_5_0_2_4				,		snake_case_=4_5_4_4				,		snake_case_=3_2				,		snake_case_=7_1				,		snake_case_=1E-5				,		snake_case_=0.02				,		snake_case_=True				,		snake_case_=0.0				,		snake_case_=0.0				,		snake_case_=None				,		snake_case_=False				,		snake_case_=False				,		snake_case_=True				,		snake_case_=True				,		snake_case_=False				,		snake_case_=1_1				,		snake_case_=1_1				,		**snake_case_				,		):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[int]       =				vocab_size
       # Backward compatibility with n_embed kwarg
       UpperCAmelCase_							:       Optional[Any]       =				kwargs.pop('n_embed'				,		snake_case_						)
       UpperCAmelCase_							:       int       =				hidden_size if n_embed is None else n_embed
       UpperCAmelCase_							:       Optional[Any]       =				num_hidden_layers
       UpperCAmelCase_							:       Any       =				num_attention_heads
       UpperCAmelCase_							:       Optional[int]       =				layer_norm_epsilon
       UpperCAmelCase_							:       Optional[Any]       =				initializer_range
       UpperCAmelCase_							:       Dict       =				use_cache
       UpperCAmelCase_							:       List[Any]       =				hidden_dropout
       UpperCAmelCase_							:       List[Any]       =				attention_dropout

       UpperCAmelCase_							:       Union[str, Any]       =				bos_token_id
       UpperCAmelCase_							:       Optional[int]       =				eos_token_id
       UpperCAmelCase_							:       int       =				num_attention_heads if num_kv_heads is None else num_kv_heads
       UpperCAmelCase_							:       Dict       =				alibi
       UpperCAmelCase_							:       List[str]       =				new_decoder_architecture
       UpperCAmelCase_							:       List[str]       =				multi_query  # Ignored when new_decoder_architecture is True
       UpperCAmelCase_							:       List[str]       =				parallel_attn
       UpperCAmelCase_							:       Union[str, Any]       =				bias

       super().__init__(bos_token_id=snake_case_				,		eos_token_id=snake_case_				,		**snake_case_						)


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return self.hidden_size // self.num_attention_heads






  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return not self.alibi







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






