


import sys
from pathlib import Path


a_					:    Any          =    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(4_2)

a_					:    Any          =    {"base": "patrickvonplaten/wav2vec2_tiny_random", "robust": "patrickvonplaten/wav2vec2_tiny_random_robust"}

a_					:    List[Any]          =    "zero2"
a_					:    str          =    "zero3"
a_					:    Tuple          =    [ZEROa, ZEROa]






def 	_A				(lowerCAmelCase__  :Union[str, Any]				,						lowerCAmelCase__  :Any				,						lowerCAmelCase__  :str		)   ->  Optional[Any]:




     '''simple docstring'''



     _a			      =							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
a_					:    Optional[Any]          =    list(itertools.product(stages, models.keys()))






@slow
@require_deepspeed
@require_torch_gpu
class  a     (     _SCREAMING_SNAKE_CASE       ):
 @parameterized.expand(__magic_name__      ,				name_func=__magic_name__						)
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__						)					->							List[Any]:
      self.run_and_check(
          stage=__magic_name__      ,				model=__magic_name__      ,				distributed=__magic_name__      ,				fpaa=__magic_name__      ,				)
 @require_torch_multi_gpu
 @parameterized.expand(__magic_name__      ,				name_func=__magic_name__						)
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__						)					->							List[Any]:
      self.run_and_check(
          stage=__magic_name__      ,				model=__magic_name__      ,				distributed=__magic_name__      ,				fpaa=__magic_name__      ,				)
 @parameterized.expand(__magic_name__      ,				name_func=__magic_name__						)
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__						)					->							str:
      self.run_and_check(
          stage=__magic_name__      ,				model=__magic_name__      ,				distributed=__magic_name__      ,				fpaa=__magic_name__      ,				)
 @require_torch_multi_gpu
 @parameterized.expand(__magic_name__      ,				name_func=__magic_name__						)
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__						)					->							Tuple:
      self.run_and_check(
          stage=__magic_name__      ,				model=__magic_name__      ,				distributed=__magic_name__      ,				fpaa=__magic_name__      ,				)
 def 							__UpperCAmelCase		( self      ,				__magic_name__						)					->							Optional[int]:
      # XXX: run_asr is premature and doesn't save any results
      # so all we check for now is that the process didn't fail
      pass
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__ = 10      ,				__magic_name__ = True      ,				__magic_name__ = True      ,				__magic_name__ = True      ,				)					->							Optional[Any]:
      _a			      =							models[model]

      _a			      =							self.run_trainer(
          stage=__magic_name__      ,				model_name=__magic_name__      ,				eval_steps=__magic_name__      ,				num_train_epochs=1      ,				distributed=__magic_name__      ,				fpaa=__magic_name__      ,				)

      self.do_checks(__magic_name__						)

      return output_dir
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__ = 10      ,				__magic_name__ = 1      ,				__magic_name__ = True      ,				__magic_name__ = True      ,				)					->							str:
      _a			      =							self.get_auto_remove_tmp_dir('./xxx'      ,				after=__magic_name__						)
      _a			      =							f'\n            --model_name_or_path {model_name}\n            --dataset_name hf-internal-testing/librispeech_asr_dummy\n            --dataset_config_name clean\n            --train_split_name validation\n            --validation_split_name validation\n            --output_dir {output_dir}\n            --num_train_epochs {str(__magic_name__						)}\n            --per_device_train_batch_size 2\n            --per_device_eval_batch_size 2\n            --evaluation_strategy steps\n            --learning_rate 5e-4\n            --warmup_steps 8\n            --orthography timit\n            --preprocessing_num_workers 1\n            --group_by_length\n            --freeze_feature_extractor\n            --report_to none\n            --save_steps 0\n            --eval_steps {eval_steps}\n            --report_to none\n        '.split()

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

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

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

      return output_dir




 def 							__UpperCAmelCase		( self      ,				__magic_name__=False						)					->							Optional[Any]:
      # 1. explicitly set --num_nodes=1 just in case these tests end up run on a multi-node setup
      # - it won't be able to handle that
      # 2. for now testing with just 2 gpus max (since some quality tests may give different
      # results with mode gpus because we use very little data)
      _a			      =							min(2      ,				get_gpu_count()						) if distributed else 1
      return f'deepspeed --num_nodes 1 --num_gpus {num_gpus}'.split()





'''simple docstring'''






import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


a_					:    Optional[Any]          =    logging.getLogger(__name__)




@dataclass(frozen=_SCREAMING_SNAKE_CASE       )
class  a     :
 _lowerCAmelCase 					=	42
 _lowerCAmelCase 					=	42
 _lowerCAmelCase 					=	None
 _lowerCAmelCase 					=	None
 _lowerCAmelCase 					=	None





@dataclass(frozen=_SCREAMING_SNAKE_CASE       )
class  a     :
 _lowerCAmelCase 					=	42
 _lowerCAmelCase 					=	None
 _lowerCAmelCase 					=	None
 _lowerCAmelCase 					=	None
 _lowerCAmelCase 					=	None


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




 class  a     (     _SCREAMING_SNAKE_CASE       ):
  _lowerCAmelCase 					=	42
  def __init__( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__ = None      ,				__magic_name__=False      ,				__magic_name__ = False      ,				)					->							Optional[int]:
       _a			      =							hans_processors[task]()

       _a			      =							os.path.join(
           __magic_name__      ,				'cached_{}_{}_{}_{}'.format(
               'dev' if evaluate else 'train'      ,				tokenizer.__class__.__name__      ,				str(__magic_name__						)      ,				__magic_name__      ,				)      ,				)
       _a			      =							processor.get_labels()
       if tokenizer.__class__ in (
           RobertaTokenizer,
           RobertaTokenizerFast,
           XLMRobertaTokenizer,
           BartTokenizer,
           BartTokenizerFast,
       ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            _a      ,   _a			      =							label_list[2], label_list[1]
       _a			      =							label_list

       # Make sure only the first process in distributed training processes the dataset,
       # and the others will use the cache.
       _a			      =							cached_features_file + '.lock'
       with FileLock(__magic_name__						):
            if os.path.exists(__magic_name__						) and not overwrite_cache:
                 logger.info(f'Loading features from cached file {cached_features_file}'						)
                 _a			      =							torch.load(__magic_name__						)
            else:
                 logger.info(f'Creating features from dataset file at {data_dir}'						)

                 _a			      =							(
                     processor.get_dev_examples(__magic_name__						) if evaluate else processor.get_train_examples(__magic_name__						)
                 )

                 logger.info('Training examples: %s'      ,				len(__magic_name__						)						)
                 _a			      =							hans_convert_examples_to_features(__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)
                 logger.info('Saving features into cached file %s'      ,				__magic_name__						)
                 torch.save(self.features      ,				__magic_name__						)
  def __len__( self						)					->							List[Any]:
       return len(self.features						)
  def __getitem__( self      ,				__magic_name__						)					->							InputFeatures:
       return self.features[i]




  def 							__UpperCAmelCase		( self						)					->							Optional[Any]:
       return self.label_list


if is_tf_available():
 import tensorflow as tf




 class  a     :
  _lowerCAmelCase 					=	42
  def __init__( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__ = 1_28      ,				__magic_name__=False      ,				__magic_name__ = False      ,				)					->							str:
       _a			      =							hans_processors[task]()
       _a			      =							processor.get_labels()
       if tokenizer.__class__ in (
           RobertaTokenizer,
           RobertaTokenizerFast,
           XLMRobertaTokenizer,
           BartTokenizer,
           BartTokenizerFast,
       ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            _a      ,   _a			      =							label_list[2], label_list[1]
       _a			      =							label_list

       _a			      =							processor.get_dev_examples(__magic_name__						) if evaluate else processor.get_train_examples(__magic_name__						)
       _a			      =							hans_convert_examples_to_features(__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)

       def gen():
            for ex_index, ex in tqdm.tqdm(enumerate(self.features						)      ,				desc='convert examples to features'						):
                 if ex_index % 1_00_00 == 0:
                      logger.info('Writing example %d of %d' % (ex_index, len(__magic_name__						))						)

                 yield (
                     {
                         "example_id": 0,
                         "input_ids": ex.input_ids,
                         "attention_mask": ex.attention_mask,
                         "token_type_ids": ex.token_type_ids,
                     },
                     ex.label,
                 )

       _a			      =							tf.data.Dataset.from_generator(
           __magic_name__      ,				(
               {
                   'example_id': tf.intaa,
                   'input_ids': tf.intaa,
                   'attention_mask': tf.intaa,
                   'token_type_ids': tf.intaa,
               },
               tf.intaa,
           )      ,				(
               {
                   'example_id': tf.TensorShape([]						),
                   'input_ids': tf.TensorShape([None, None]						),
                   'attention_mask': tf.TensorShape([None, None]						),
                   'token_type_ids': tf.TensorShape([None, None]						),
               },
               tf.TensorShape([]						),
           )      ,				)
  def 							__UpperCAmelCase		( self						)					->							Tuple:
       return self.dataset
  def __len__( self						)					->							Optional[Any]:
       return len(self.features						)
  def __getitem__( self      ,				__magic_name__						)					->							InputFeatures:
       return self.features[i]




  def 							__UpperCAmelCase		( self						)					->							List[Any]:
       return self.label_list




class  a     (     _SCREAMING_SNAKE_CASE       ):
 def 							__UpperCAmelCase		( self      ,				__magic_name__						)					->							Tuple:
      return self._create_examples(self._read_tsv(os.path.join(__magic_name__      ,				'heuristics_train_set.txt'						)						)      ,				'train'						)
 def 							__UpperCAmelCase		( self      ,				__magic_name__						)					->							List[Any]:
      return self._create_examples(self._read_tsv(os.path.join(__magic_name__      ,				'heuristics_evaluation_set.txt'						)						)      ,				'dev'						)
 def 							__UpperCAmelCase		( self						)					->							Tuple:
      return ["contradiction", "entailment", "neutral"]




 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__						)					->							Optional[Any]:
      _a			      =							[]
      for i, line in enumerate(__magic_name__						):
           if i == 0:
                continue
           _a			      =							'%s-%s' % (set_type, line[0])
           _a			      =							line[5]
           _a			      =							line[6]
           _a			      =							line[7][2:] if line[7].startswith('ex'						) else line[7]
           _a			      =							line[0]
           examples.append(InputExample(guid=__magic_name__      ,				text_a=__magic_name__      ,				text_b=__magic_name__      ,				label=__magic_name__      ,				pairID=__magic_name__						)						)
      return examples






def 	_A				(lowerCAmelCase__  :List[InputExample]				,						lowerCAmelCase__  :List[str]				,						lowerCAmelCase__  :int				,						lowerCAmelCase__  :PreTrainedTokenizer				,						)   ->  Tuple:




     '''simple docstring'''



     _a			      =							{label: i for i, label in enumerate(lowerCAmelCase__		)}

     _a			      =							[]
     for ex_index, example in tqdm.tqdm(enumerate(lowerCAmelCase__		)				,						desc='convert examples to features'		):
          if ex_index % 1_00_00 == 0:
               logger.info('Writing example %d' % (ex_index)		)

          _a			      =							tokenizer(
              example.text_a				,						example.text_b				,						add_special_tokens=lowerCAmelCase__				,						max_length=lowerCAmelCase__				,						padding='max_length'				,						truncation=lowerCAmelCase__				,						return_overflowing_tokens=lowerCAmelCase__				,						)

          _a			      =							label_map[example.label] if example.label in label_map else 0

          _a			      =							int(example.pairID		)

          features.append(InputFeatures(**lowerCAmelCase__				,						label=lowerCAmelCase__				,						pairID=lowerCAmelCase__		)		)

     for i, example in enumerate(examples[:5]		):
          logger.info('*** Example ***'		)
          logger.info(f'guid: {example}'		)
          logger.info(f'features: {features[i]}'		)

     return features


a_					:    Optional[int]          =    {
    "hans": 3,
}






a_					:    Optional[Any]          =    {
    "hans": HansProcessor,
}

