






from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    get_torch_dist_unique_port,
    require_torch_multi_gpu,
    require_torch_neuroncore,
)
from transformers.training_args import ParallelMode
from transformers.utils import logging


SCREAMING_SNAKE_CASE 							=						logging.get_logger(__name__)


if is_torch_available():
  import torch
  from torch import nn
  from torch.utils.data import Dataset

  from transformers import Trainer
  class 							A_    (						__a			):






   '''simple docstring'''




   def __init__(	self							,  _A = 101) ->		str:

       """simple docstring"""



       _UpperCAmelCase				:      Optional[Any]			  =				length


   def __len__(	self) ->		str:

       """simple docstring"""



       return self.length


   def __getitem__(	self							,  _A) ->		Any:

       """simple docstring"""



       return i



  class 							A_    :






   '''simple docstring'''




   def __call__(	self							,  _A) ->		Optional[Any]:

       """simple docstring"""



       return {"input_ids": torch.tensor(a_), "labels": torch.tensor(a_)}



  class 							A_    (						nn.Module			):






   '''simple docstring'''




   def __init__(	self) ->		Any:

       """simple docstring"""



       super().__init__()
       # Add some (unused) params otherwise DDP will complain.
       _UpperCAmelCase				:      Optional[int]			  =				nn.Linear(120							,  80)


   def  snake_case__				(	self							,  _A							,  _A=None) ->		Any:

       """simple docstring"""



       if labels is not None:
           return torch.tensor(0.0							,  device=input_ids.device), input_ids
       else:
           return input_ids
class 							A_    (						__a			):






 '''simple docstring'''




 @require_torch_neuroncore
 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				f'''--nproc_per_node=2\n            --master_port={get_torch_dist_unique_port()}\n            {self.test_file_dir}/test_trainer_distributed.py\n        '''.split()
     _UpperCAmelCase				:      Union[str, Any]			  =				self.get_auto_remove_tmp_dir()
     _UpperCAmelCase				:      int			  =				f'''--output_dir {output_dir}'''.split()
     _UpperCAmelCase				:      List[Any]			  =				["""torchrun"""] + distributed_args + args
     execute_subprocess_async(a_							,  env=self.get_env())
     # successful return here == success - any errors would have caused an error in the sub-call



class 							A_    (						__a			):






 '''simple docstring'''




 @require_torch_multi_gpu
 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				f'''--nproc_per_node={torch.cuda.device_count()}\n            --master_port={get_torch_dist_unique_port()}\n            {self.test_file_dir}/test_trainer_distributed.py\n        '''.split()
     _UpperCAmelCase				:      Tuple			  =				self.get_auto_remove_tmp_dir()
     _UpperCAmelCase				:      Tuple			  =				f'''--output_dir {output_dir}'''.split()
     _UpperCAmelCase				:      Optional[int]			  =				["""torchrun"""] + distributed_args + args
     execute_subprocess_async(a_							,  env=self.get_env())
     # successful return here == success - any errors would have caused an error in the sub-call


if __name__ == "__main__":
  # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs:
  #
  # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py

  SCREAMING_SNAKE_CASE 							=						HfArgumentParser((TrainingArguments,))
  SCREAMING_SNAKE_CASE 							=						parser.parse_args_into_dataclasses()[0]

  logger.warning(
      F'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, '
      F'distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}'
  )

  # Essentially, what we want to verify in the distributed case is that we get all samples back,
  # in the right order. (this is crucial for prediction for instance)
  for dataset_length in [101, 40, 7]:
    SCREAMING_SNAKE_CASE 							=						DummyDataset(dataset_length)

    def      _lowerCamelCase	(						__A    :							EvalPrediction )      ->					Dict:
        _UpperCAmelCase				:      Tuple			  =				list(range(len(snake_case__ ) ) )
        _UpperCAmelCase				:      Union[str, Any]			  =				p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
        if not success and training_args.local_rank == 0:
            logger.warning(
                '''Predictions and/or labels do not match expected results:\n  - predictions: '''
                f'''{p.predictions.tolist()}\n  - labels: {p.label_ids.tolist()}\n  - expected: {sequential}''' )
        return {"success": success}

    SCREAMING_SNAKE_CASE 							=						Trainer(
        model=DummyModel(),
        args=training_args,
        data_collator=DummyDataCollator(),
        eval_dataset=dataset,
        compute_metrics=compute_metrics,
    )
    SCREAMING_SNAKE_CASE 							=						trainer.evaluate()
    logger.info(metrics)
    if metrics["eval_success"] is not True:
      logger.error(metrics)
      exit(1)

    SCREAMING_SNAKE_CASE 							=						trainer.predict(dataset)
    logger.info(p.metrics)
    if p.metrics["test_success"] is not True:
      logger.error(p.metrics)
      exit(1)

    SCREAMING_SNAKE_CASE 							=						2

    SCREAMING_SNAKE_CASE 							=						trainer.evaluate()
    logger.info(metrics)
    if metrics["eval_success"] is not True:
      logger.error(metrics)
      exit(1)

    SCREAMING_SNAKE_CASE 							=						trainer.predict(dataset)
    logger.info(p.metrics)
    if p.metrics["test_success"] is not True:
      logger.error(p.metrics)
      exit(1)


    SCREAMING_SNAKE_CASE 							=						None







import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402


SCREAMING_SNAKE_CASE 							=						get_tests_dir('fixtures/dummy-config.json')
class 							A_    (						unittest.TestCase			):






 '''simple docstring'''




 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				0


 def  snake_case__				(	self) ->		Union[str, Any]:

     """simple docstring"""



     self.assertIsNotNone(transformers.models.auto.__spec__)
     self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''))


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				AutoConfig.from_pretrained('''bert-base-uncased''')
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				AutoConfig.from_pretrained(_A)
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				AutoConfig.from_pretrained(_A)
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				AutoConfig.for_model('''roberta''')
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     with tempfile.TemporaryDirectory() as tmp_dir:
         # This model name contains bert and roberta, but roberta ends up being picked.
         _UpperCAmelCase				:      str			  =				os.path.join(_A							,  '''fake-roberta''')
         os.makedirs(_A							,  exist_ok=_A)
         with open(os.path.join(_A							,  '''config.json''')							,  '''w''') as f:
             f.write(json.dumps({}))
         _UpperCAmelCase				:      Any			  =				AutoConfig.from_pretrained(_A)
         self.assertEqual(type(_A)							,  _A)


 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     try:
         AutoConfig.register('''custom'''							,  _A)
         # Wrong model type will raise an error
         with self.assertRaises(_A):
             AutoConfig.register('''model'''							,  _A)
         # Trying to register something existing in the Transformers library will raise an error
         with self.assertRaises(_A):
             AutoConfig.register('''bert'''							,  _A)

         # Now that the config is registered, it can be used as any other config with the auto-API
         _UpperCAmelCase				:      Tuple			  =				CustomConfig()
         with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(_A)
             _UpperCAmelCase				:      Tuple			  =				AutoConfig.from_pretrained(_A)
             self.assertIsInstance(_A							,  _A)

     finally:
         if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]


 def  snake_case__				(	self) ->		Union[str, Any]:

     """simple docstring"""



     with self.assertRaisesRegex(
         _A							,  '''bert-base is not a local folder and is not a valid model identifier'''):
         _UpperCAmelCase				:      Tuple			  =				AutoConfig.from_pretrained('''bert-base''')


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     with self.assertRaisesRegex(
         _A							,  R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''):
         _UpperCAmelCase				:      List[Any]			  =				AutoConfig.from_pretrained(_A							,  revision='''aaaaaa''')


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     with self.assertRaisesRegex(
         _A							,  '''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''							,  ):
         _UpperCAmelCase				:      List[str]			  =				AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo''')


 def  snake_case__				(	self) ->		List[str]:

     """simple docstring"""



     with self.assertRaises(_A):
         _UpperCAmelCase				:      Dict			  =				AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''')
     # If remote code is disabled, we can't load this config.
     with self.assertRaises(_A):
         _UpperCAmelCase				:      Optional[int]			  =				AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''							,  trust_remote_code=_A)

     _UpperCAmelCase				:      int			  =				AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''							,  trust_remote_code=_A)
     self.assertEqual(config.__class__.__name__							,  '''NewModelConfig''')

     # Test config can be reloaded.
     with tempfile.TemporaryDirectory() as tmp_dir:
         config.save_pretrained(_A)
         _UpperCAmelCase				:      Any			  =				AutoConfig.from_pretrained(_A							,  trust_remote_code=_A)
     self.assertEqual(reloaded_config.__class__.__name__							,  '''NewModelConfig''')


 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     class 							A_    (						__lowercase			):






      '''simple docstring'''




      _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					"new-model"

     try:
         AutoConfig.register('''new-model'''							,  _A)
         # If remote code is not set, the default is to use local
         _UpperCAmelCase				:      List[str]			  =				AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''')
         self.assertEqual(config.__class__.__name__							,  '''NewModelConfigLocal''')

         # If remote code is disabled, we load the local one.
         _UpperCAmelCase				:      List[Any]			  =				AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''							,  trust_remote_code=_A)
         self.assertEqual(config.__class__.__name__							,  '''NewModelConfigLocal''')

         # If remote is enabled, we load from the Hub
         _UpperCAmelCase				:      Dict			  =				AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''							,  trust_remote_code=_A)
         self.assertEqual(config.__class__.__name__							,  '''NewModelConfig''')

     finally:
         if "new-model" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["new-model"]




