





# 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       :   Optional[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(42)

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

__UpperCAmelCase       :   Any				       =							"zero2"
__UpperCAmelCase       :   Tuple				       =							"zero3"
__UpperCAmelCase       :   Dict				       =							[ZEROa, ZEROa]







def 			A__							(    SCREAMING_SNAKE_CASE__					,					SCREAMING_SNAKE_CASE__					,					SCREAMING_SNAKE_CASE__)   ->    int:
  # customize the test name generator function as we want both params to appear in the sub-test
  # name, as by default it shows only the first param
  __snake_case:						Dict   =						parameterized.to_safe_name("""_""".join(str(SCREAMING_SNAKE_CASE__) for x in param.args))
  return F'''{func.__name__}_{param_based_name}'''


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







@slow
@require_deepspeed
@require_torch_gpu
class     __snake_case       (							__lowerCamelCase			):





       '''simple docstring'''







       @parameterized.expand(A							,				name_func=A		)
       def 							UpperCAmelCase__ (  self :    int							,				A :    List[str]							,				A :    List[str]		):
         self.run_and_check(
             stage=A							,				model=A							,				distributed=A							,				fpaa=A							,				)




       @require_torch_multi_gpu
       @parameterized.expand(A							,				name_func=A		)
       def 							UpperCAmelCase__ (  self :    List[Any]							,				A :    Union[str, Any]							,				A :    Optional[int]		):
         self.run_and_check(
             stage=A							,				model=A							,				distributed=A							,				fpaa=A							,				)




       @parameterized.expand(A							,				name_func=A		)
       def 							UpperCAmelCase__ (  self :    int							,				A :    Optional[int]							,				A :    List[str]		):
         self.run_and_check(
             stage=A							,				model=A							,				distributed=A							,				fpaa=A							,				)




       @require_torch_multi_gpu
       @parameterized.expand(A							,				name_func=A		)
       def 							UpperCAmelCase__ (  self :    Dict							,				A :    Dict							,				A :    Optional[Any]		):
         self.run_and_check(
             stage=A							,				model=A							,				distributed=A							,				fpaa=A							,				)




       def 							UpperCAmelCase__ (  self :    Union[str, Any]							,				A :    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 :    List[str]							,				A :    str							,				A :    str							,				A :    int = 10							,				A :    bool = True							,				A :    bool = True							,				A :    bool = True							,				):
         __snake_case:						Optional[Any]   =						models[model]

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

         self.do_checks(A		)

         return output_dir




       def 							UpperCAmelCase__ (  self :    str							,				A :    str							,				A :    str							,				A :    int = 10							,				A :    int = 1							,				A :    bool = True							,				A :    bool = True							,				):
         __snake_case:						Union[str, Any]   =						self.get_auto_remove_tmp_dir("""./xxx"""							,				after=A		)
         __snake_case:						int   =						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(A		)}
            --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
         __snake_case:						Optional[int]   =						f'''--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json'''.split()
         __snake_case:						List[Any]   =						[f'''{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py''']
         __snake_case:						List[Any]   =						self.get_launcher(A		)

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

         return output_dir






       def 							UpperCAmelCase__ (  self :    Optional[Any]							,				A :    Any=False		):
         # 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)
         __snake_case:						Any   =						min(2							,				get_gpu_count()		) if distributed else 1
         return f'''deepspeed --num_nodes 1 --num_gpus {num_gpus}'''.split()













import math







def 			A__							(    SCREAMING_SNAKE_CASE__)   ->    bool:

  if 1 < number < 4:
    # 2 and 3 are primes
    return True
  elif number < 2 or number % 2 == 0 or number % 3 == 0:
    # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
    return False

  # All primes number are in format of 6k +/- 1
  for i in range(5					,					int(math.sqrt(SCREAMING_SNAKE_CASE__) + 1)					,					6):
    if number % i == 0 or number % (i + 2) == 0:
      return False
  return True







def 			A__							(    SCREAMING_SNAKE_CASE__ = 1_0001)   ->    int:

  try:
    __snake_case:						List[str]   =						int(SCREAMING_SNAKE_CASE__)
  except (TypeError, ValueError):
    raise TypeError("""Parameter nth must be int or castable to int.""") from None
  if nth <= 0:
    raise ValueError("""Parameter nth must be greater than or equal to one.""")
  __snake_case:						list[int]   =						[]
  __snake_case:						List[str]   =						2
  while len(SCREAMING_SNAKE_CASE__) < nth:
    if is_prime(SCREAMING_SNAKE_CASE__):
      primes.append(SCREAMING_SNAKE_CASE__)
      num += 1
    else:
      num += 1
  return primes[len(SCREAMING_SNAKE_CASE__) - 1]


if __name__ == "__main__":
      print(f'{solution() = }')






