



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase__					  =  logging.get_logger(__name__)

lowerCAmelCase__					  =  {
    '''xlm-mlm-en-2048''': '''https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json''',
    '''xlm-mlm-ende-1024''': '''https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json''',
    '''xlm-mlm-enfr-1024''': '''https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json''',
    '''xlm-mlm-enro-1024''': '''https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json''',
    '''xlm-mlm-tlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json''',
    '''xlm-mlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json''',
    '''xlm-clm-enfr-1024''': '''https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json''',
    '''xlm-clm-ende-1024''': '''https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json''',
    '''xlm-mlm-17-1280''': '''https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json''',
    '''xlm-mlm-100-1280''': '''https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json''',
}

class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		"""xlm"""
					lowercase_								=		{
					    """hidden_size""": """emb_dim""",
					    """num_attention_heads""": """n_heads""",
					    """num_hidden_layers""": """n_layers""",
					    """n_words""": """vocab_size""",  # For backward compatibility
					}







					def __init__(							self :     List[Any]				,       SCREAMING_SNAKE_CASE :     Optional[int]=30_145				,       SCREAMING_SNAKE_CASE :     Tuple=2_048				,       SCREAMING_SNAKE_CASE :     Optional[int]=12				,       SCREAMING_SNAKE_CASE :     List[Any]=16				,       SCREAMING_SNAKE_CASE :     int=0.1				,       SCREAMING_SNAKE_CASE :     Optional[Any]=0.1				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=True				,       SCREAMING_SNAKE_CASE :     int=False				,       SCREAMING_SNAKE_CASE :     Optional[Any]=False				,       SCREAMING_SNAKE_CASE :     Any=False				,       SCREAMING_SNAKE_CASE :     Any=1				,       SCREAMING_SNAKE_CASE :     int=True				,       SCREAMING_SNAKE_CASE :     Dict=512				,       SCREAMING_SNAKE_CASE :     str=2_048**-0.5				,       SCREAMING_SNAKE_CASE :     List[Any]=1E-1_2				,       SCREAMING_SNAKE_CASE :     Dict=0.02				,       SCREAMING_SNAKE_CASE :     str=0				,       SCREAMING_SNAKE_CASE :     Tuple=1				,       SCREAMING_SNAKE_CASE :     int=2				,       SCREAMING_SNAKE_CASE :     Dict=3				,       SCREAMING_SNAKE_CASE :     Dict=5				,       SCREAMING_SNAKE_CASE :     Dict=True				,       SCREAMING_SNAKE_CASE :     int="first"				,       SCREAMING_SNAKE_CASE :     Tuple=True				,       SCREAMING_SNAKE_CASE :     List[Any]=None				,       SCREAMING_SNAKE_CASE :     Tuple=True				,       SCREAMING_SNAKE_CASE :     Optional[Any]=0.1				,       SCREAMING_SNAKE_CASE :     Optional[Any]=5				,       SCREAMING_SNAKE_CASE :     int=5				,       SCREAMING_SNAKE_CASE :     Tuple=0				,       SCREAMING_SNAKE_CASE :     Optional[Any]=0				,       SCREAMING_SNAKE_CASE :     Optional[Any]=2				,       SCREAMING_SNAKE_CASE :     Optional[Any]=0				,       **SCREAMING_SNAKE_CASE :     Tuple				,       ):
									lowercase__					:   Optional[Any]													=					vocab_size
									lowercase__					:   List[Any]													=					emb_dim
									lowercase__					:   Union[str, Any]													=					n_layers
									lowercase__					:   Dict													=					n_heads
									lowercase__					:   Optional[Any]													=					dropout
									lowercase__					:   str													=					attention_dropout
									lowercase__					:   Any													=					gelu_activation
									lowercase__					:   Union[str, Any]													=					sinusoidal_embeddings
									lowercase__					:   Optional[Any]													=					causal
									lowercase__					:   Union[str, Any]													=					asm
									lowercase__					:   List[str]													=					n_langs
									lowercase__					:   int													=					use_lang_emb
									lowercase__					:   Optional[Any]													=					layer_norm_eps
									lowercase__					:   Any													=					bos_index
									lowercase__					:   Optional[Any]													=					eos_index
									lowercase__					:   Optional[int]													=					pad_index
									lowercase__					:   int													=					unk_index
									lowercase__					:   str													=					mask_index
									lowercase__					:   List[str]													=					is_encoder
									lowercase__					:   Union[str, Any]													=					max_position_embeddings
									lowercase__					:   str													=					embed_init_std
									lowercase__					:   Dict													=					init_std
									lowercase__					:   List[Any]													=					summary_type
									lowercase__					:   Any													=					summary_use_proj
									lowercase__					:   Optional[int]													=					summary_activation
									lowercase__					:   Any													=					summary_proj_to_labels
									lowercase__					:   Tuple													=					summary_first_dropout
									lowercase__					:   Any													=					start_n_top
									lowercase__					:   Optional[int]													=					end_n_top
									lowercase__					:   int													=					mask_token_id
									lowercase__					:   Union[str, Any]													=					lang_id

									if "n_words" in kwargs:
													lowercase__					:   int													=					kwargs["n_words"]

									super().__init__(pad_token_id=SCREAMING_SNAKE_CASE				,       bos_token_id=SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)






class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					@property
					def 			snake_case					(							self :     List[str]					):
									if self.task == "multiple-choice":
													lowercase__					:   Any													=					{0: "batch", 1: "choice", 2: "sequence"}
									else:
													lowercase__					:   str													=					{0: "batch", 1: "sequence"}
									return OrderedDict(
									    [
									        ("input_ids", dynamic_axis),
									        ("attention_mask", dynamic_axis),
									        ("token_type_ids", dynamic_axis),
									    ]					)









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""": 6_5_0, """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""": 6_0_0, """eval_accuracy""": 0.3, """eval_loss""": 0.9},
        },
    ]      )
class 				snake_case__(unittest.TestCase      ):

					"""simple docstring"""






					def 			snake_case					(							self :     Union[str, Any]					):
									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=SCREAMING_SNAKE_CASE				,       )
									assert hasattr(self				,       "env"					)







					def 			snake_case					(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     str=1					):
									# creates estimator
									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=SCREAMING_SNAKE_CASE				,       instance_type=self.instance_type				,       debugger_hook_config=SCREAMING_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 			snake_case					(							self :     Union[str, Any]				,       SCREAMING_SNAKE_CASE :     str					):
									TrainingJobAnalytics(SCREAMING_SNAKE_CASE					).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv"""					)







					def 			snake_case					(							self :     str					):
									# create estimator
									lowercase__					:   Optional[int]													=					self.create_estimator()

									# run training
									estimator.fit()

									# result dataframe
									lowercase__					:   Union[str, Any]													=					TrainingJobAnalytics(estimator.latest_training_job.name					).dataframe()

									# extract kpis
									lowercase__					:   str													=					list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"]					)
									lowercase__					:   Tuple													=					list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"]					)
									# get train time from SageMaker job, this includes starting, preprocessing, stopping
									lowercase__					:   Any													=					(
									    Session().describe_training_job(estimator.latest_training_job.name					).get("TrainingTimeInSeconds"				,       999_999					)
									)

									# 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}				,       SCREAMING_SNAKE_CASE					)




