






import importlib.util
import json
import os
import warnings
from dataclasses import dataclass, field

import torch

from ..training_args import TrainingArguments
from ..utils import cached_property, is_sagemaker_dp_enabled, logging


A_		:		Optional[int]          =			logging.get_logger(__name__)

def   snake_case ()	->    Tuple:
  UpperCamelCase_: Optional[Any]   				=    os.getenv('SM_HP_MP_PARAMETERS'     ,   '{}'  )
  try:
    # Parse it and check the field "partitions" is included, it is required for model parallel.
    UpperCamelCase_: Tuple   				=    json.loads(UpperCAmelCase__  )
    if "partitions" not in smp_options:
      return False
  except json.JSONDecodeError:
    return False

  # Get the sagemaker specific framework parameters from mpi_options variable.
  UpperCamelCase_: List[str]   				=    os.getenv('SM_FRAMEWORK_PARAMS'     ,   '{}'  )
  try:
    # Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
    UpperCamelCase_: Tuple   				=    json.loads(UpperCAmelCase__  )
    if not mpi_options.get('sagemaker_mpi_enabled'     ,   UpperCAmelCase__  ):
      return False
  except json.JSONDecodeError:
    return False
  # Lastly, check if the `smdistributed` module is present.
  return importlib.util.find_spec('smdistributed'  ) is not None


if is_sagemaker_model_parallel_available():
   import smdistributed.modelparallel.torch as smp

   smp.init()




@dataclass
class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       a    :		Dict									=field(
           default=''''''							,     metadata={'''help''': '''Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'''}							,     )





       def       _a   ( self							):
         super().__post_init__()
         warnings.warn(
             '`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use '
             '`TrainingArguments` instead.'  ,      lowerCamelCase_  ,      )





       @cached_property
       def       _a   ( self							):
         logger.info('PyTorch: setting up devices'							)
         if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1:
           logger.warning(
               'torch.distributed process group is initialized, but local_rank == -1. '
               'In order to use Torch DDP, launch your script with `python -m torch.distributed.launch'							)
         if self.no_cuda:
           UpperCamelCase_: Any   				=    torch.device('cpu'							)
           UpperCamelCase_: Optional[int]   				=    0
         elif is_sagemaker_model_parallel_available():
           UpperCamelCase_: Any   				=    smp.local_rank()
           UpperCamelCase_: Tuple   				=    torch.device('cuda'  ,      lowerCamelCase_							)
           UpperCamelCase_: Tuple   				=    1
         elif is_sagemaker_dp_enabled():
           import smdistributed.dataparallel.torch.torch_smddp  # noqa: F401

           torch.distributed.init_process_group(backend='smddp'  ,      timeout=self.ddp_timeout_delta							)
           UpperCamelCase_: Tuple   				=    int(os.getenv('SMDATAPARALLEL_LOCAL_RANK'							)							)
           UpperCamelCase_: Optional[int]   				=    torch.device('cuda'  ,      self.local_rank							)
           UpperCamelCase_: str   				=    1
         elif self.local_rank == -1:
           # if n_gpu is > 1 we'll use nn.DataParallel.
           # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
           # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
           # trigger an error that a device index is missing. Index 0 takes into account the
           # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
           # will use the first GPU in that env, i.e. GPU#1
           UpperCamelCase_: int   				=    torch.device('cuda:0' if torch.cuda.is_available() else 'cpu'							)
           # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
           # the default value.
           UpperCamelCase_: str   				=    torch.cuda.device_count()
         else:
           # Here, we'll use torch.distributed.
           # Initializes the distributed backend which will take care of synchronizing nodes/GPUs
           if not torch.distributed.is_initialized():
             torch.distributed.init_process_group(backend='nccl'  ,      timeout=self.ddp_timeout_delta							)
           UpperCamelCase_: str   				=    torch.device('cuda'  ,      self.local_rank							)
           UpperCamelCase_: Tuple   				=    1

         if device.type == "cuda":
           torch.cuda.set_device(lowerCamelCase_							)

         return device





       @property
       def       _a   ( self							):

         if is_sagemaker_model_parallel_available():
           return smp.dp_size()

         return super().world_size





       @property
       def       _a   ( self							):
         return not is_sagemaker_model_parallel_available()





       @property
       def       _a   ( self							):
         return False







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_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


A_		:		int		     =			logging.get_logger(__name__)




class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       a    :		str									=['''pixel_values''']





       def __init__( self  ,      _lowerCamelCase = True  ,      _lowerCamelCase = None  ,      _lowerCamelCase = PILImageResampling.BICUBIC  ,      _lowerCamelCase = True  ,      _lowerCamelCase = True  ,      _lowerCamelCase = 1 / 2_5_5  ,      _lowerCamelCase = None  ,      _lowerCamelCase = True  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         super().__init__(**_lowerCamelCase							)
         UpperCamelCase_: Any   				=    size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
         UpperCamelCase_: Any   				=    get_size_dict(_lowerCamelCase							)
         UpperCamelCase_: Any   				=    crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
         UpperCamelCase_: List[Any]   				=    get_size_dict(_lowerCamelCase  ,      default_to_square=_lowerCamelCase  ,      param_name='crop_size'							)

         UpperCamelCase_: Optional[int]   				=    do_resize
         UpperCamelCase_: Tuple   				=    do_rescale
         UpperCamelCase_: Dict   				=    do_normalize
         UpperCamelCase_: Optional[int]   				=    do_center_crop
         UpperCamelCase_: Tuple   				=    crop_size
         UpperCamelCase_: Optional[int]   				=    size
         UpperCamelCase_: Dict   				=    resample
         UpperCamelCase_: Tuple   				=    rescale_factor
         UpperCamelCase_: Tuple   				=    image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
         UpperCamelCase_: Dict   				=    image_std if image_std is not None else IMAGENET_DEFAULT_STD





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = PILImageResampling.BILINEAR  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         UpperCamelCase_: Optional[Any]   				=    get_size_dict(_lowerCamelCase							)

         if "shortest_edge" in size:
           UpperCamelCase_: str   				=    get_resize_output_image_size(_lowerCamelCase  ,      size=size['shortest_edge']  ,      default_to_square=_lowerCamelCase							)
           # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
         elif "height" in size and "width" in size:
           UpperCamelCase_: Any   				=    (size['height'], size['width'])
         else:
           raise ValueError(f'''Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}'''							)
         return resize(_lowerCamelCase  ,      size=_lowerCamelCase  ,      resample=_lowerCamelCase  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         UpperCamelCase_: List[str]   				=    get_size_dict(_lowerCamelCase							)
         if "height" not in size or "width" not in size:
           raise ValueError(f'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''							)
         return center_crop(_lowerCamelCase  ,      size=(size['height'], size['width'])  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      **_lowerCamelCase							):
         return rescale(_lowerCamelCase  ,      scale=_lowerCamelCase  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      **_lowerCamelCase  ,      ):
         return normalize(_lowerCamelCase  ,      mean=_lowerCamelCase  ,      std=_lowerCamelCase  ,      data_format=_lowerCamelCase  ,      **_lowerCamelCase							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = None  ,      _lowerCamelCase = ChannelDimension.FIRST  ,      **_lowerCamelCase  ,      ):
         UpperCamelCase_: List[str]   				=    do_resize if do_resize is not None else self.do_resize
         UpperCamelCase_: str   				=    do_rescale if do_rescale is not None else self.do_rescale
         UpperCamelCase_: Dict   				=    do_normalize if do_normalize is not None else self.do_normalize
         UpperCamelCase_: List[str]   				=    do_center_crop if do_center_crop is not None else self.do_center_crop
         UpperCamelCase_: Optional[Any]   				=    crop_size if crop_size is not None else self.crop_size
         UpperCamelCase_: List[str]   				=    get_size_dict(_lowerCamelCase  ,      param_name='crop_size'  ,      default_to_square=_lowerCamelCase							)
         UpperCamelCase_: Tuple   				=    resample if resample is not None else self.resample
         UpperCamelCase_: str   				=    rescale_factor if rescale_factor is not None else self.rescale_factor
         UpperCamelCase_: Dict   				=    image_mean if image_mean is not None else self.image_mean
         UpperCamelCase_: Dict   				=    image_std if image_std is not None else self.image_std

         UpperCamelCase_: Any   				=    size if size is not None else self.size
         UpperCamelCase_: Optional[Any]   				=    get_size_dict(_lowerCamelCase							)

         if not is_batched(_lowerCamelCase							):
           UpperCamelCase_: Dict   				=    [images]

         if not valid_images(_lowerCamelCase							):
           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.'							)

         # All transformations expect numpy arrays.
         UpperCamelCase_: List[str]   				=    [to_numpy_array(_lowerCamelCase							) for image in images]

         if do_resize:
           UpperCamelCase_: List[str]   				=    [self.resize(image=_lowerCamelCase  ,      size=_lowerCamelCase  ,      resample=_lowerCamelCase							) for image in images]

         if do_center_crop:
           UpperCamelCase_: List[str]   				=    [self.center_crop(image=_lowerCamelCase  ,      size=_lowerCamelCase							) for image in images]

         if do_rescale:
           UpperCamelCase_: int   				=    [self.rescale(image=_lowerCamelCase  ,      scale=_lowerCamelCase							) for image in images]

         if do_normalize:
           UpperCamelCase_: Optional[Any]   				=    [self.normalize(image=_lowerCamelCase  ,      mean=_lowerCamelCase  ,      std=_lowerCamelCase							) for image in images]

         UpperCamelCase_: List[str]   				=    [to_channel_dimension_format(_lowerCamelCase  ,      _lowerCamelCase							) for image in images]

         UpperCamelCase_: Optional[int]   				=    {'pixel_values': images}
         return BatchFeature(data=_lowerCamelCase  ,      tensor_type=_lowerCamelCase							)