

"""simple docstring"""





import sys
from pathlib import Path


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

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

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




def   _A  (  UpperCamelCase_					:	List[Any],      UpperCamelCase_					:	List[str],      UpperCamelCase_					:	int)     ->						List[Any]:
   '''simple docstring'''


   __lowercase								=						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							=							list(itertools.product(stages, models.keys()))




@slow
@require_deepspeed
@require_torch_gpu
class  _lowerCAmelCase  (     __SCREAMING_SNAKE_CASE       ):







 """simple docstring"""







 @parameterized.expand(__UpperCAmelCase, name_func=__UpperCAmelCase     )
 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      Union[str, Any], UpperCAmelCase__							:      Union[str, Any]     ):
    self.run_and_check(
        stage=__UpperCAmelCase, model=__UpperCAmelCase, distributed=__UpperCAmelCase, fpaa=__UpperCAmelCase, )


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


 @parameterized.expand(__UpperCAmelCase, name_func=__UpperCAmelCase     )
 def  _lowercase			(      self							:      Tuple, UpperCAmelCase__							:      Dict, UpperCAmelCase__							:      List[str]     ):
    self.run_and_check(
        stage=__UpperCAmelCase, model=__UpperCAmelCase, distributed=__UpperCAmelCase, fpaa=__UpperCAmelCase, )


 @require_torch_multi_gpu
 @parameterized.expand(__UpperCAmelCase, name_func=__UpperCAmelCase     )
 def  _lowercase			(      self							:      Tuple, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Dict     ):
    self.run_and_check(
        stage=__UpperCAmelCase, model=__UpperCAmelCase, distributed=__UpperCAmelCase, fpaa=__UpperCAmelCase, )


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      Union[str, Any]     ):
    pass


 def  _lowercase			(      self							:      int, UpperCAmelCase__							:      str, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Union[str, Any] = 1_0, UpperCAmelCase__							:      List[str] = True, UpperCAmelCase__							:      Dict = True, UpperCAmelCase__							:      Optional[Any] = True, ):
    __lowercase								=						models[model]

    __lowercase								=						self.run_trainer(
        stage=__UpperCAmelCase, model_name=__UpperCAmelCase, eval_steps=__UpperCAmelCase, num_train_epochs=1, distributed=__UpperCAmelCase, fpaa=__UpperCAmelCase, )

    self.do_checks(__UpperCAmelCase     )

    return output_dir


 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      int, UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      Dict = 1_0, UpperCAmelCase__							:      str = 1, UpperCAmelCase__							:      Any = True, UpperCAmelCase__							:      Union[str, Any] = True, ):
    __lowercase								=						self.get_auto_remove_tmp_dir("./xxx", after=__UpperCAmelCase     )
    __lowercase								=						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(__UpperCAmelCase     )}\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
    __lowercase								=						F"""--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json""".split()
    __lowercase								=						[F"""{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py"""]
    __lowercase								=						self.get_launcher(__UpperCAmelCase     )

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

    return output_dir



 def  _lowercase			(      self							:      Optional[Any], UpperCAmelCase__							:      Any=False     ):
    __lowercase								=						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 Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


_a							=							logging.get_logger(__name__)




class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : Optional[Any]       		=     ["pixel_values"]


 def __init__(      self							:      int, UpperCAmelCase__							:      bool = True, UpperCAmelCase__							:      Optional[Dict[str, int]] = None, UpperCAmelCase__							:      PILImageResampling = PILImageResampling.BILINEAR, UpperCAmelCase__							:      bool = True, UpperCAmelCase__							:      Dict[str, int] = None, UpperCAmelCase__							:      bool = True, UpperCAmelCase__							:      Union[int, float] = 1 / 2_5_5, UpperCAmelCase__							:      bool = True, UpperCAmelCase__							:      Optional[Union[float, List[float]]] = None, UpperCAmelCase__							:      Optional[Union[float, List[float]]] = None, **UpperCAmelCase__							:      str, ):
    super().__init__(**UpperCAmelCase__     )
    __lowercase								=						size if size is not None else {"shortest_edge": 2_5_6}
    __lowercase								=						get_size_dict(UpperCAmelCase__, default_to_square=UpperCAmelCase__     )
    __lowercase								=						crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
    __lowercase								=						get_size_dict(UpperCAmelCase__     )
    __lowercase								=						do_resize
    __lowercase								=						size
    __lowercase								=						resample
    __lowercase								=						do_center_crop
    __lowercase								=						crop_size
    __lowercase								=						do_rescale
    __lowercase								=						rescale_factor
    __lowercase								=						do_normalize
    __lowercase								=						image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
    __lowercase								=						image_std if image_std is not None else IMAGENET_STANDARD_STD


 def  _lowercase			(      self							:      int, UpperCAmelCase__							:      np.ndarray, UpperCAmelCase__							:      Dict[str, int], UpperCAmelCase__							:      PILImageResampling = PILImageResampling.BICUBIC, UpperCAmelCase__							:      Optional[Union[str, ChannelDimension]] = None, **UpperCAmelCase__							:      Dict, ):
    __lowercase								=						get_size_dict(UpperCAmelCase__, default_to_square=UpperCAmelCase__     )
    if "shortest_edge" not in size:
       raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""     )
    __lowercase								=						get_resize_output_image_size(UpperCAmelCase__, size=size["shortest_edge"], default_to_square=UpperCAmelCase__     )
    return resize(UpperCAmelCase__, size=UpperCAmelCase__, resample=UpperCAmelCase__, data_format=UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      np.ndarray, UpperCAmelCase__							:      Dict[str, int], UpperCAmelCase__							:      Optional[Union[str, ChannelDimension]] = None, **UpperCAmelCase__							:      Dict, ):
    __lowercase								=						get_size_dict(UpperCAmelCase__     )
    return center_crop(UpperCAmelCase__, size=(size["height"], size["width"]), data_format=UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      np.ndarray, UpperCAmelCase__							:      float, UpperCAmelCase__							:      Optional[Union[str, ChannelDimension]] = None, **UpperCAmelCase__							:      Union[str, Any]     ):
    return rescale(UpperCAmelCase__, scale=UpperCAmelCase__, data_format=UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      np.ndarray, UpperCAmelCase__							:      Union[float, List[float]], UpperCAmelCase__							:      Union[float, List[float]], UpperCAmelCase__							:      Optional[Union[str, ChannelDimension]] = None, **UpperCAmelCase__							:      int, ):
    return normalize(UpperCAmelCase__, mean=UpperCAmelCase__, std=UpperCAmelCase__, data_format=UpperCAmelCase__, **UpperCAmelCase__     )



 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      ImageInput, UpperCAmelCase__							:      Optional[bool] = None, UpperCAmelCase__							:      Dict[str, int] = None, UpperCAmelCase__							:      PILImageResampling = None, UpperCAmelCase__							:      bool = None, UpperCAmelCase__							:      Dict[str, int] = None, UpperCAmelCase__							:      Optional[bool] = None, UpperCAmelCase__							:      Optional[float] = None, UpperCAmelCase__							:      Optional[bool] = None, UpperCAmelCase__							:      Optional[Union[float, List[float]]] = None, UpperCAmelCase__							:      Optional[Union[float, List[float]]] = None, UpperCAmelCase__							:      Optional[Union[str, TensorType]] = None, UpperCAmelCase__							:      Union[str, ChannelDimension] = ChannelDimension.FIRST, **UpperCAmelCase__							:      Optional[int], ):
    __lowercase								=						do_resize if do_resize is not None else self.do_resize
    __lowercase								=						size if size is not None else self.size
    __lowercase								=						get_size_dict(UpperCAmelCase__, default_to_square=UpperCAmelCase__     )
    __lowercase								=						resample if resample is not None else self.resample
    __lowercase								=						do_center_crop if do_center_crop is not None else self.do_center_crop
    __lowercase								=						crop_size if crop_size is not None else self.crop_size
    __lowercase								=						get_size_dict(UpperCAmelCase__     )
    __lowercase								=						do_rescale if do_rescale is not None else self.do_rescale
    __lowercase								=						rescale_factor if rescale_factor is not None else self.rescale_factor
    __lowercase								=						do_normalize if do_normalize is not None else self.do_normalize
    __lowercase								=						image_mean if image_mean is not None else self.image_mean
    __lowercase								=						image_std if image_std is not None else self.image_std

    __lowercase								=						make_list_of_images(UpperCAmelCase__     )

    if not valid_images(UpperCAmelCase__     ):
       raise ValueError(
           "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
           "torch.Tensor, tf.Tensor or jax.ndarray."     )

    if do_resize and size is None:
       raise ValueError("Size must be specified if do_resize is True."     )

    if do_center_crop and crop_size is None:
       raise ValueError("Crop size must be specified if do_center_crop is True."     )

    if do_rescale and rescale_factor is None:
       raise ValueError("Rescale factor must be specified if do_rescale is True."     )

    if do_normalize and (image_mean is None or image_std is None):
       raise ValueError("Image mean and std must be specified if do_normalize is True."     )

    # All transformations expect numpy arrays.
    __lowercase								=						[to_numpy_array(UpperCAmelCase__     ) for image in images]

    if do_resize:
       __lowercase								=						[self.resize(image=UpperCAmelCase__, size=UpperCAmelCase__, resample=UpperCAmelCase__     ) for image in images]

    if do_center_crop:
       __lowercase								=						[self.center_crop(image=UpperCAmelCase__, size=UpperCAmelCase__     ) for image in images]

    if do_rescale:
       __lowercase								=						[self.rescale(image=UpperCAmelCase__, scale=UpperCAmelCase__     ) for image in images]

    if do_normalize:
       __lowercase								=						[self.normalize(image=UpperCAmelCase__, mean=UpperCAmelCase__, std=UpperCAmelCase__     ) for image in images]

    __lowercase								=						[to_channel_dimension_format(UpperCAmelCase__, UpperCAmelCase__     ) for image in images]

    __lowercase								=						{"pixel_values": images}
    return BatchFeature(data=UpperCAmelCase__, tensor_type=UpperCAmelCase__     )




