




"""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


SCREAMING_SNAKE_CASE_	     =       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)

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

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





def      __snake_case (				_lowercase			,_lowercase			,_lowercase    ):


    """simple docstring"""







    UpperCamelCase              =	parameterized.to_safe_name('''_'''.join(str(_lowercase    ) for x in param.args    )    )
    return f'{func.__name__}_{param_based_name}'


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


@slow
@require_deepspeed
@require_torch_gpu
class 				snake_case_			(					lowerCamelCase_			):







   """simple docstring"""




   @parameterized.expand(lowerCamelCase_       ,					name_func=lowerCamelCase_)
   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_)				->   str:
       self.run_and_check(
           stage=lowerCamelCase_       ,					model=lowerCamelCase_       ,					distributed=lowerCamelCase_       ,					fpaa=lowerCamelCase_       ,					)







   @require_torch_multi_gpu
   @parameterized.expand(lowerCamelCase_       ,					name_func=lowerCamelCase_)
   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_)				->   Optional[int]:
       self.run_and_check(
           stage=lowerCamelCase_       ,					model=lowerCamelCase_       ,					distributed=lowerCamelCase_       ,					fpaa=lowerCamelCase_       ,					)







   @parameterized.expand(lowerCamelCase_       ,					name_func=lowerCamelCase_)
   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_)				->   List[Any]:
       self.run_and_check(
           stage=lowerCamelCase_       ,					model=lowerCamelCase_       ,					distributed=lowerCamelCase_       ,					fpaa=lowerCamelCase_       ,					)







   @require_torch_multi_gpu
   @parameterized.expand(lowerCamelCase_       ,					name_func=lowerCamelCase_)
   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_)				->   Dict:
       self.run_and_check(
           stage=lowerCamelCase_       ,					model=lowerCamelCase_       ,					distributed=lowerCamelCase_       ,					fpaa=lowerCamelCase_       ,					)







   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_)				->   Optional[Any]:
       # 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       ,					lowerCamelCase_       ,					lowerCamelCase_       ,					lowerCamelCase_ = 1_0       ,					lowerCamelCase_ = True       ,					lowerCamelCase_ = True       ,					lowerCamelCase_ = True       ,					)				->   Union[str, Any]:
       UpperCamelCase              =	models[model]

       UpperCamelCase              =	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   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_       ,					lowerCamelCase_ = 1_0       ,					lowerCamelCase_ = 1       ,					lowerCamelCase_ = True       ,					lowerCamelCase_ = True       ,					)				->   Dict:
       UpperCamelCase              =	self.get_auto_remove_tmp_dir('''./xxx'''       ,					after=lowerCamelCase_)
       UpperCamelCase              =	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(lowerCamelCase_)}\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
       UpperCamelCase              =	F'--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json'.split()
       UpperCamelCase              =	[F'{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py']
       UpperCamelCase              =	self.get_launcher(lowerCamelCase_)

       UpperCamelCase              =	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   UpperCAmelCase__			(		self       ,					lowerCamelCase_=False)				->   List[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)
       UpperCamelCase              =	min(2       ,					get_gpu_count()) if distributed else 1
       return F'deepspeed --num_nodes 1 --num_gpus {num_gpus}'.split()






"""simple docstring"""






from __future__ import annotations






def      lowercase__ (					lowerCAmelCase__   :			int			)    ->			list[int]:



			'''simple docstring'''
			a__  :	Tuple		 =			[True] * limit
			a__  :	Tuple		 =			False
			a__  :	int		 =			False
			a__  :	Tuple		 =			True

			for i in range(3							,			int(limit**0.5 + 1			)							,			2			):
						a__  :	int		 =			i * 2
						while index < limit:
									a__  :	Union[str, Any]		 =			False
									a__  :	int		 =			index + i

			a__  :	str		 =			[2]

			for i in range(3							,			lowerCAmelCase__							,			2			):
						if is_prime[i]:
									primes.append(lowerCAmelCase__			)

			return primes






def      lowercase__ (					lowerCAmelCase__   :			int = 1_0_0_0_0_0_0			)    ->			int:



			'''simple docstring'''
			a__  :	List[Any]		 =			prime_sieve(lowerCAmelCase__			)
			a__  :	int		 =			0
			a__  :	str		 =			0

			for i in range(len(lowerCAmelCase__			)			):
						for j in range(i + length							,			len(lowerCAmelCase__			)			):
									a__  :	List[str]		 =			sum(primes[i:j]			)
									if sol >= ceiling:
												break

									if sol in primes:
												a__  :	Tuple		 =			j - i
												a__  :	Union[str, Any]		 =			sol

			return largest


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