


"""simple docstring"""







import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment
def _A						(						lowercase       ,    lowercase=()       ,    lowercase=None       ,    lowercase="no"       ,    lowercase="29500"		):







    """simple docstring"""
    a 		=False
    a 		=False
    if any(key.startswith('''KAGGLE'''		) for key in os.environ.keys()		):
        a 		=True
    elif "IPython" in sys.modules:
        a 		='''google.colab''' in str(sys.modules['''IPython'''].get_ipython()		)

    try:
        a 		=PrecisionType(mixed_precision.lower()		)
    except ValueError:
        raise ValueError(
            f'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.'''		)

    if (in_colab or in_kaggle) and (os.environ.get('''TPU_NAME'''       ,    lowercase		) is not None):
        # TPU launch
        import torch_xla.distributed.xla_multiprocessing as xmp

        if len(AcceleratorState._shared_state		) > 0:
            raise ValueError(
                '''To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside '''
                '''your training function. Restart your notebook and make sure no cells initializes an '''
                '''`Accelerator`.'''		)
        if num_processes is None:
            a 		=8

        a 		=PrepareForLaunch(lowercase       ,    distributed_type='''TPU'''		)
        print(f'''Launching a training on {num_processes} TPU cores.'''		)
        xmp.spawn(lowercase       ,    args=lowercase       ,    nprocs=lowercase       ,    start_method='''fork'''		)
    elif in_colab:
        # No need for a distributed launch otherwise as it's either CPU or one GPU.
        if torch.cuda.is_available():
            print('''Launching training on one GPU.'''		)
        else:
            print('''Launching training on one CPU.'''		)
        function(*lowercase		)
    else:
        if num_processes is None:
            raise ValueError(
                '''You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.'''		)

        if num_processes > 1:
            # Multi-GPU launch
            from torch.multiprocessing import start_processes
            from torch.multiprocessing.spawn import ProcessRaisedException

            if len(AcceleratorState._shared_state		) > 0:
                raise ValueError(
                    '''To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized '''
                    '''inside your training function. Restart your notebook and make sure no cells initializes an '''
                    '''`Accelerator`.'''		)

            if torch.cuda.is_initialized():
                raise ValueError(
                    '''To launch a multi-GPU training from your notebook, you need to avoid running any instruction '''
                    '''using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA '''
                    '''function.'''		)

            # torch.distributed will expect a few environment variable to be here. We set the ones common to each
            # process here (the other ones will be set be the launcher).
            with patch_environment(
                world_size=lowercase       ,    master_addr='''127.0.01'''       ,    master_port=lowercase       ,    mixed_precision=lowercase		):
                a 		=PrepareForLaunch(lowercase       ,    distributed_type='''MULTI_GPU'''		)
                print(f'''Launching training on {num_processes} GPUs.'''		)
                try:
                    start_processes(lowercase       ,    args=lowercase       ,    nprocs=lowercase       ,    start_method='''fork'''		)
                except ProcessRaisedException as e:
                    if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
                        raise RuntimeError(
                            '''CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. '''
                            '''This likely stems from an outside import causing issues once the `notebook_launcher()` is called. '''
                            '''Please review your imports and test them when running the `notebook_launcher()` to identify '''
                            '''which one is problematic.'''		) from e

        else:
            # No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
            if is_mps_available():
                a 		='''1'''
                print('''Launching training on MPS.'''		)
            elif torch.cuda.is_available():
                print('''Launching training on one GPU.'''		)
            else:
                print('''Launching training on CPU.'''		)
            function(*lowercase		)





def _A						(						lowercase       ,    lowercase=()       ,    lowercase=2		):







    """simple docstring"""
    from torch.multiprocessing import start_processes

    with tempfile.NamedTemporaryFile() as tmp_file:
        # torch.distributed will expect a few environment variable to be here. We set the ones common to each
        # process here (the other ones will be set be the launcher).
        with patch_environment(
            world_size=lowercase       ,    master_addr='''127.0.01'''       ,    master_port='''29500'''       ,    accelerate_mixed_precision='''no'''       ,    accelerate_debug_rdv_file=tmp_file.name       ,    accelerate_use_cpu='''yes'''       ,    ):
            a 		=PrepareForLaunch(lowercase       ,    debug=lowercase		)
            start_processes(lowercase       ,    args=lowercase       ,    nprocs=lowercase       ,    start_method='''fork'''		)




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 rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
				import PIL


__A   			=logging.get_logger(__name__)

def        a						(      _UpperCAmelCase							:					Optional[Any] ,   _UpperCAmelCase							:					Dict						):







		'''simple docstring'''





		__UpperCAmelCase    :     List[str]													=	b.T
		__UpperCAmelCase    :     Any													=	np.sum(np.square(_UpperCAmelCase						) ,   axis=1						)
		__UpperCAmelCase    :     int													=	np.sum(np.square(_UpperCAmelCase						) ,   axis=0						)
		__UpperCAmelCase    :     Optional[int]													=	np.matmul(_UpperCAmelCase ,   _UpperCAmelCase						)
		__UpperCAmelCase    :     List[str]													=	aa[:, None] - 2 * ab + ba[None, :]
		return d

def        a						(      _UpperCAmelCase							:					Optional[int] ,   _UpperCAmelCase							:					Optional[Any]						):







		'''simple docstring'''





		__UpperCAmelCase    :     str													=	x.reshape(-1 ,   3						)
		__UpperCAmelCase    :     Optional[int]													=	squared_euclidean_distance(_UpperCAmelCase ,   _UpperCAmelCase						)
		return np.argmin(_UpperCAmelCase ,   axis=1						)

class 		UpperCAmelCase__			(			__UpperCamelCase   ):




			'''simple docstring'''






			UpperCamelCase									=       ["""pixel_values"""]




			def __init__(   self							: str       ,       a_							: Optional[Union[List[List[int]], np.ndarray]] = None       ,       a_							: bool = True       ,       a_							: Dict[str, int] = None       ,       a_							: PILImageResampling = PILImageResampling.BILINEAR       ,       a_							: bool = True       ,       a_							: bool = True       ,       **a_							: List[str]       ,       ):
					'''simple docstring'''





					super().__init__(**a_ )
					__UpperCAmelCase    :     Optional[int]													=	size if size is not None else {'''height''': 2_56, '''width''': 2_56}
					__UpperCAmelCase    :     List[str]													=	get_size_dict(a_ )
					__UpperCAmelCase    :     str													=	np.array(a_ ) if clusters is not None else None
					__UpperCAmelCase    :     Dict													=	do_resize
					__UpperCAmelCase    :     Tuple													=	size
					__UpperCAmelCase    :     Union[str, Any]													=	resample
					__UpperCAmelCase    :     Tuple													=	do_normalize
					__UpperCAmelCase    :     Optional[int]													=	do_color_quantize




			def  snake_case__     (   self							: Optional[Any]       ,       a_							: np.ndarray       ,       a_							: Dict[str, int]       ,       a_							: PILImageResampling = PILImageResampling.BILINEAR       ,       a_							: Optional[Union[str, ChannelDimension]] = None       ,       **a_							: Dict       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Tuple													=	get_size_dict(a_ )
					if "height" not in size or "width" not in size:
							raise ValueError(F'Size dictionary must contain both height and width keys. Got {size.keys()}' )
					return resize(
					    a_       ,       size=(size['''height'''], size['''width'''])       ,       resample=a_       ,       data_format=a_       ,       **a_ )




			def  snake_case__     (   self							: Tuple       ,       a_							: np.ndarray       ,       a_							: Optional[Union[str, ChannelDimension]] = None       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Dict													=	rescale(image=a_       ,       scale=1 / 1_2_7.5       ,       data_format=a_ )
					__UpperCAmelCase    :     Union[str, Any]													=	image - 1
					return image




			def  snake_case__     (   self							: int       ,       a_							: ImageInput       ,       a_							: bool = None       ,       a_							: Dict[str, int] = None       ,       a_							: PILImageResampling = None       ,       a_							: bool = None       ,       a_							: Optional[bool] = None       ,       a_							: Optional[Union[List[List[int]], np.ndarray]] = None       ,       a_							: Optional[Union[str, TensorType]] = None       ,       a_							: Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST       ,       **a_							: Any       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	do_resize if do_resize is not None else self.do_resize
					__UpperCAmelCase    :     List[str]													=	size if size is not None else self.size
					__UpperCAmelCase    :     Any													=	get_size_dict(a_ )
					__UpperCAmelCase    :     Optional[int]													=	resample if resample is not None else self.resample
					__UpperCAmelCase    :     List[str]													=	do_normalize if do_normalize is not None else self.do_normalize
					__UpperCAmelCase    :     int													=	do_color_quantize if do_color_quantize is not None else self.do_color_quantize
					__UpperCAmelCase    :     Optional[int]													=	clusters if clusters is not None else self.clusters
					__UpperCAmelCase    :     Any													=	np.array(a_ )

					__UpperCAmelCase    :     Optional[int]													=	make_list_of_images(a_ )

					if not valid_images(a_ ):
							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 or resample is None:
							raise ValueError('''Size and resample must be specified if do_resize is True.''' )

					if do_color_quantize and clusters is None:
							raise ValueError('''Clusters must be specified if do_color_quantize is True.''' )

					# All transformations expect numpy arrays.
					__UpperCAmelCase    :     List[Any]													=	[to_numpy_array(a_ ) for image in images]

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

					if do_normalize:
							__UpperCAmelCase    :     Dict													=	[self.normalize(image=a_ ) for image in images]

					if do_color_quantize:
							__UpperCAmelCase    :     int													=	[to_channel_dimension_format(a_       ,       ChannelDimension.LAST ) for image in images]
							# color quantize from (batch_size, height, width, 3) to (batch_size, height, width)
							__UpperCAmelCase    :     List[str]													=	np.array(a_ )
							__UpperCAmelCase    :     Dict													=	color_quantize(a_       ,       a_ ).reshape(images.shape[:-1] )

							# flatten to (batch_size, height*width)
							__UpperCAmelCase    :     Any													=	images.shape[0]
							__UpperCAmelCase    :     Any													=	images.reshape(a_       ,       -1 )

							# We need to convert back to a list of images to keep consistent behaviour across processors.
							__UpperCAmelCase    :     List[Any]													=	list(a_ )
					else:
							__UpperCAmelCase    :     int													=	[to_channel_dimension_format(a_       ,       a_ ) for image in images]

					__UpperCAmelCase    :     int													=	{'''input_ids''': images}
					return BatchFeature(data=a_       ,       tensor_type=a_ )





