


"""simple docstring"""



# XXX: we want transformers master here - in the absense of conftest manipulating sys.path:
# hack it in for now:
import sys
from pathlib import Path


UpperCAmelCase      		=			Path(__file__).resolve().parents[3] / 'src'
sys.path.insert(1, str(git_repo_path))

import dataclasses  # noqa
import io  # noqa
import itertools  # noqa
import json  # noqa
import os  # noqa
import unittest  # noqa
from copy import deepcopy  # noqa

from parameterized import parameterized  # noqa
from transformers import TrainingArguments, is_torch_available  # noqa
from transformers.deepspeed import is_deepspeed_available  # noqa
from transformers.file_utils import WEIGHTS_NAME  # noqa
from transformers.testing_utils import (  # noqa
    CaptureLogger,
    ExtendSysPath,
    TestCasePlus,
    execute_subprocess_async,
    get_gpu_count,
    mockenv_context,
    require_deepspeed,
    require_torch_gpu,
    require_torch_multi_gpu,
    slow,
)
from transformers.trainer_utils import set_seed  # noqa


set_seed(42)

UpperCAmelCase      		=			{'base': 'patrickvonplaten/wav2vec2_tiny_random', 'robust': 'patrickvonplaten/wav2vec2_tiny_random_robust'}

UpperCAmelCase      		=			'zero2'
UpperCAmelCase      		=			'zero3'
UpperCAmelCase      		=			[ZEROa, ZEROa]






def       lowerCamelCase					(a_						:Union[str, Any]				,				a_						:Tuple				,				a_						:Any)     ->							str:
       lowercase		:Union[str, Any]       							=				parameterized.to_safe_name('''_'''.join(str(lowerCamelCase_) for x in param.args))
       return F"""{func.__name__}_{param_based_name}"""


# Cartesian-product of zero stages with models to test
UpperCAmelCase      		=			list(itertools.product(stages, models.keys()))




@slow
@require_deepspeed
@require_torch_gpu
class 					__magic_name__		(			lowerCamelCase__     ):






       @parameterized.expand(__lowerCamelCase  ,     name_func=__lowerCamelCase				)
       def  __snake_case						(   self  :    str  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    List[str]				):

              '''simple docstring'''






              self.run_and_check(
                  stage=__lowerCamelCase  ,     model=__lowerCamelCase  ,     distributed=__lowerCamelCase  ,     fpaa=__lowerCamelCase  ,     )






       @require_torch_multi_gpu
       @parameterized.expand(__lowerCamelCase  ,     name_func=__lowerCamelCase				)
       def  __snake_case						(   self  :    Optional[int]  ,     snake_case__  :    Tuple  ,     snake_case__  :    Dict				):

              '''simple docstring'''






              self.run_and_check(
                  stage=__lowerCamelCase  ,     model=__lowerCamelCase  ,     distributed=__lowerCamelCase  ,     fpaa=__lowerCamelCase  ,     )






       @parameterized.expand(__lowerCamelCase  ,     name_func=__lowerCamelCase				)
       def  __snake_case						(   self  :    Optional[Any]  ,     snake_case__  :    str  ,     snake_case__  :    Dict				):

              '''simple docstring'''






              self.run_and_check(
                  stage=__lowerCamelCase  ,     model=__lowerCamelCase  ,     distributed=__lowerCamelCase  ,     fpaa=__lowerCamelCase  ,     )






       @require_torch_multi_gpu
       @parameterized.expand(__lowerCamelCase  ,     name_func=__lowerCamelCase				)
       def  __snake_case						(   self  :    str  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    Tuple				):

              '''simple docstring'''






              self.run_and_check(
                  stage=__lowerCamelCase  ,     model=__lowerCamelCase  ,     distributed=__lowerCamelCase  ,     fpaa=__lowerCamelCase  ,     )






       def  __snake_case						(   self  :    Optional[Any]  ,     snake_case__  :    int				):

              '''simple docstring'''






              pass






       def  __snake_case						(   self  :    Optional[int]  ,     snake_case__  :    Tuple  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    Optional[int] = 1_0  ,     snake_case__  :    List[Any] = True  ,     snake_case__  :    List[Any] = True  ,     snake_case__  :    List[str] = True  ,     ):

              '''simple docstring'''






              lowercase		:Tuple       							=				models[model]

              lowercase		:Optional[Any]       							=				self.run_trainer(
                  stage=__lowerCamelCase  ,     model_name=__lowerCamelCase  ,     eval_steps=__lowerCamelCase  ,     num_train_epochs=1  ,     distributed=__lowerCamelCase  ,     fpaa=__lowerCamelCase  ,     )

              self.do_checks(__lowerCamelCase				)

              return output_dir






       def  __snake_case						(   self  :    Optional[int]  ,     snake_case__  :    Any  ,     snake_case__  :    Dict  ,     snake_case__  :    str = 1_0  ,     snake_case__  :    Union[str, Any] = 1  ,     snake_case__  :    Optional[int] = True  ,     snake_case__  :    Optional[int] = True  ,     ):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				self.get_auto_remove_tmp_dir('''./xxx'''  ,     after=__lowerCamelCase				)
              lowercase		:Tuple       							=				f"""
            --model_name_or_path {model_name}
            --dataset_name hf-internal-testing/librispeech_asr_dummy
            --dataset_config_name clean
            --train_split_name validation
            --validation_split_name validation
            --output_dir {output_dir}
            --num_train_epochs {str(__lowerCamelCase				)}
            --per_device_train_batch_size 2
            --per_device_eval_batch_size 2
            --evaluation_strategy steps
            --learning_rate 5e-4
            --warmup_steps 8
            --orthography timit
            --preprocessing_num_workers 1
            --group_by_length
            --freeze_feature_extractor
            --report_to none
            --save_steps 0
            --eval_steps {eval_steps}
            --report_to none
        """.split()

              if fpaa:
                     args.extend(['''--fp16''']				)

              # currently ds_config_wav2vec2_zero.json requires "zero_optimization.find_unused_parameters": true,
              # hence the separate config files
              lowercase		:Optional[int]       							=				f"""--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json""".split()
              lowercase		:Tuple       							=				[f"""{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py"""]
              lowercase		:Optional[Any]       							=				self.get_launcher(__lowerCamelCase				)

              lowercase		:Optional[int]       							=				launcher + script + args + ds_args
              # keep for quick debug
              # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die
              execute_subprocess_async(__lowerCamelCase  ,     env=self.get_env()				)

              return output_dir







       def  __snake_case						(   self  :    Optional[int]  ,     snake_case__  :    Optional[Any]=False				):

              '''simple docstring'''






              lowercase		:Dict       							=				min(2  ,     get_gpu_count()				) if distributed else 1
              return f"""deepspeed --num_nodes 1 --num_gpus {num_gpus}""".split()





"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase      		=			{
    '''configuration_xlm_roberta''': [
        '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''XLMRobertaConfig''',
        '''XLMRobertaOnnxConfig''',
    ],
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			['''XLMRobertaTokenizer''']

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			['''XLMRobertaTokenizerFast''']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''XLMRobertaForCausalLM''',
     '''XLMRobertaForMaskedLM''',
     '''XLMRobertaForMultipleChoice''',
     '''XLMRobertaForQuestionAnswering''',
     '''XLMRobertaForSequenceClassification''',
     '''XLMRobertaForTokenClassification''',
     '''XLMRobertaModel''',
     '''XLMRobertaPreTrainedModel''',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''TFXLMRobertaForCausalLM''',
     '''TFXLMRobertaForMaskedLM''',
     '''TFXLMRobertaForMultipleChoice''',
     '''TFXLMRobertaForQuestionAnswering''',
     '''TFXLMRobertaForSequenceClassification''',
     '''TFXLMRobertaForTokenClassification''',
     '''TFXLMRobertaModel''',
     '''TFXLMRobertaPreTrainedModel''',
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''FlaxXLMRobertaForMaskedLM''',
     '''FlaxXLMRobertaForCausalLM''',
     '''FlaxXLMRobertaForMultipleChoice''',
     '''FlaxXLMRobertaForQuestionAnswering''',
     '''FlaxXLMRobertaForSequenceClassification''',
     '''FlaxXLMRobertaForTokenClassification''',
     '''FlaxXLMRobertaModel''',
     '''FlaxXLMRobertaPreTrainedModel''',
 ]

if TYPE_CHECKING:
 from .configuration_xlm_roberta import (
     XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP,
     XLMRobertaConfig,
     XLMRobertaOnnxConfig,
 )

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_xlm_roberta import XLMRobertaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_xlm_roberta import (
      XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      XLMRobertaForCausalLM,
      XLMRobertaForMaskedLM,
      XLMRobertaForMultipleChoice,
      XLMRobertaForQuestionAnswering,
      XLMRobertaForSequenceClassification,
      XLMRobertaForTokenClassification,
      XLMRobertaModel,
      XLMRobertaPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_xlm_roberta import (
      TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFXLMRobertaForCausalLM,
      TFXLMRobertaForMaskedLM,
      TFXLMRobertaForMultipleChoice,
      TFXLMRobertaForQuestionAnswering,
      TFXLMRobertaForSequenceClassification,
      TFXLMRobertaForTokenClassification,
      TFXLMRobertaModel,
      TFXLMRobertaPreTrainedModel,
  )

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_xlm_roberta import (
      FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      FlaxXLMRobertaForCausalLM,
      FlaxXLMRobertaForMaskedLM,
      FlaxXLMRobertaForMultipleChoice,
      FlaxXLMRobertaForQuestionAnswering,
      FlaxXLMRobertaForSequenceClassification,
      FlaxXLMRobertaForTokenClassification,
      FlaxXLMRobertaModel,
      FlaxXLMRobertaPreTrainedModel,
  )

else:
 import sys

 UpperCAmelCase      		=			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

