
"""simple docstring"""





# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING, Dict, Optional

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.logging import get_logger
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
				import jax
				import jaxlib

_a								=						get_logger()

_a								=						None






class        _UpperCAmelCase(       TensorFormatter[Mapping, 'jax.Array', Mapping]      ):



		def __init__(   self							,						__a=None							,						__a=None							,						**__a)			->			str:






							'''simple docstring'''
							super().__init__(features=__a)
							import jax
							from jaxlib.xla_client import Device

							if isinstance(__a							,						__a):
												raise ValueError(
												    F'''Expected {device} to be a `str` not {type(__a)}, as `jaxlib.xla_extension.Device` '''
												    '''is not serializable neither with `pickle` nor with `dill`. Instead you can surround '''
												    '''the device with `str()` to get its string identifier that will be internally mapped '''
												    '''to the actual `jaxlib.xla_extension.Device`.''')
							_UpperCamelCase									=    device if isinstance(__a							,						__a) else str(jax.devices()[0])
							# using global variable since `jaxlib.xla_extension.Device` is not serializable neither
							# with `pickle` nor with `dill`, so we need to use a global variable instead
							global DEVICE_MAPPING
							if DEVICE_MAPPING is None:
												_UpperCamelCase									=    self._map_devices_to_str()
							if self.device not in list(DEVICE_MAPPING.keys()):
												logger.warning(
												    F'''Device with string identifier {self.device} not listed among the available '''
												    F'''devices: {list(DEVICE_MAPPING.keys())}, so falling back to the default '''
												    F'''device: {str(jax.devices()[0])}.''')
												_UpperCamelCase									=    str(jax.devices()[0])
							_UpperCamelCase									=    jnp_array_kwargs



		@staticmethod
		def 		UpperCAmelCase				(   )			->			Dict[str, "jaxlib.xla_extension.Device"]:






							'''simple docstring'''
							import jax

							return {str(__a): device for device in jax.devices()}



		def 		UpperCAmelCase				(   self							,						__a)			->			Any:






							'''simple docstring'''
							import jax
							import jax.numpy as jnp

							if isinstance(__a							,						__a) and column:
												if all(
												    isinstance(__a							,						jax.Array) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column):
																	return jnp.stack(__a							,						axis=0)
							return column



		def 		UpperCAmelCase				(   self							,						__a)			->			Dict:






							'''simple docstring'''
							import jax
							import jax.numpy as jnp

							if isinstance(__a							,						(str, bytes, type(__a))):
												return value
							elif isinstance(__a							,						(np.character, np.ndarray)) and np.issubdtype(value.dtype							,						np.character):
												return value.tolist()

							_UpperCamelCase									=    {}

							if isinstance(__a							,						(np.number, np.ndarray)) and np.issubdtype(value.dtype							,						np.integer):
												# the default int precision depends on the jax config
												# see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
												if jax.config.jax_enable_xaa:
																	_UpperCamelCase									=    {'''dtype''': jnp.intaa}
												else:
																	_UpperCamelCase									=    {'''dtype''': jnp.intaa}
							elif isinstance(__a							,						(np.number, np.ndarray)) and np.issubdtype(value.dtype							,						np.floating):
												_UpperCamelCase									=    {'''dtype''': jnp.floataa}
							elif config.PIL_AVAILABLE and "PIL" in sys.modules:
												import PIL.Image

												if isinstance(__a							,						PIL.Image.Image):
																	_UpperCamelCase									=    np.asarray(__a)

        # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
        # with `pickle` nor with `dill`, so we need to use a global variable instead
							global DEVICE_MAPPING
							if DEVICE_MAPPING is None:
												_UpperCamelCase									=    self._map_devices_to_str()

							with jax.default_device(DEVICE_MAPPING[self.device]):
												# calling jnp.array on a np.ndarray does copy the data
												# see https://github.com/google/jax/issues/4486
												return jnp.array(__a							,						**{**default_dtype, **self.jnp_array_kwargs})



		def 		UpperCAmelCase				(   self							,						__a)			->			str:






							'''simple docstring'''
							import jax

							# support for torch, tf, jax etc.
							if config.TORCH_AVAILABLE and "torch" in sys.modules:
												import torch

												if isinstance(__a							,						torch.Tensor):
																	return self._tensorize(data_struct.detach().cpu().numpy()[()])
							if hasattr(__a							,						'''__array__''') and not isinstance(__a							,						jax.Array):
												_UpperCamelCase									=    data_struct.__array__()
							# support for nested types like struct of list of struct
							if isinstance(__a							,						np.ndarray):
												if data_struct.dtype == object:  # jax arrays cannot be instantied from an array of objects
																	return self._consolidate([self.recursive_tensorize(__a) for substruct in data_struct])
							elif isinstance(__a							,						(list, tuple)):
												return self._consolidate([self.recursive_tensorize(__a) for substruct in data_struct])
							return self._tensorize(__a)



		def 		UpperCAmelCase				(   self							,						__a)			->			Optional[int]:






							'''simple docstring'''
							return map_nested(self._recursive_tensorize							,						__a							,						map_list=__a)



		def 		UpperCAmelCase				(   self							,						__a)			->			Mapping:






							'''simple docstring'''
							_UpperCamelCase									=    self.numpy_arrow_extractor().extract_row(__a)
							_UpperCamelCase									=    self.python_features_decoder.decode_row(__a)
							return self.recursive_tensorize(__a)



		def 		UpperCAmelCase				(   self							,						__a)			->			"jax.Array":






							'''simple docstring'''
							_UpperCamelCase									=    self.numpy_arrow_extractor().extract_column(__a)
							_UpperCamelCase									=    self.python_features_decoder.decode_column(__a							,						pa_table.column_names[0])
							_UpperCamelCase									=    self.recursive_tensorize(__a)
							_UpperCamelCase									=    self._consolidate(__a)
							return column




		def 		UpperCAmelCase				(   self							,						__a)			->			Mapping:






							'''simple docstring'''
							_UpperCamelCase									=    self.numpy_arrow_extractor().extract_batch(__a)
							_UpperCamelCase									=    self.python_features_decoder.decode_batch(__a)
							_UpperCamelCase									=    self.recursive_tensorize(__a)
							for column_name in batch:
												_UpperCamelCase									=    self._consolidate(batch[column_name])
							return batch














from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

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,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
	import PIL

_UpperCamelCase			:	Union[str, Any]								=logging.get_logger(__name__)



def  a__ (__lowercase							:str   )		-> List[List[ImageInput]]:
				if isinstance(__lowercase					,  (list, tuple)   ) and isinstance(videos[0]					,  (list, tuple)   ) and is_valid_image(videos[0][0]   ):
								return videos

				elif isinstance(__lowercase					,  (list, tuple)   ) and is_valid_image(videos[0]   ):
								return [videos]

				elif is_valid_image(__lowercase   ):
								return [[videos]]

				raise ValueError(f"""Could not make batched video from {videos}"""   )






class 						UpperCAmelCase__ (							__snake_case							):
		__snake_case      :       Optional[int]     =   ["pixel_values"]







		def __init__(					self       ,A__ = True       ,A__ = None       ,A__ = PILImageResampling.BILINEAR       ,A__ = True       ,A__ = None       ,A__ = True       ,A__ = 1 / 255       ,A__ = True       ,A__ = True       ,A__ = None       ,A__ = None       ,**A__       ,):
						super().__init__(**A__		)
						_A	:				Tuple   =      size if size is not None else {'''shortest_edge''': 256}
						_A	:				str   =      get_size_dict(A__       ,default_to_square=A__		)
						_A	:				str   =      crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
						_A	:				str   =      get_size_dict(A__       ,param_name='''crop_size'''		)

						_A	:				Tuple   =      do_resize
						_A	:				Optional[Any]   =      size
						_A	:				Optional[Any]   =      do_center_crop
						_A	:				List[str]   =      crop_size
						_A	:				Dict   =      resample
						_A	:				Tuple   =      do_rescale
						_A	:				int   =      rescale_factor
						_A	:				List[str]   =      offset
						_A	:				List[Any]   =      do_normalize
						_A	:				List[Any]   =      image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
						_A	:				List[str]   =      image_std if image_std is not None else IMAGENET_STANDARD_STD







		def  A__      (					self       ,A__       ,A__       ,A__ = PILImageResampling.BILINEAR       ,A__ = None       ,**A__       ,):
						_A	:				str   =      get_size_dict(A__       ,default_to_square=A__		)
						if "shortest_edge" in size:
										_A	:				Optional[Any]   =      get_resize_output_image_size(A__       ,size['''shortest_edge''']       ,default_to_square=A__		)
						elif "height" in size and "width" in size:
										_A	:				str   =      (size['''height'''], size['''width'''])
						else:
										raise ValueError(f"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"""		)
						return resize(A__       ,size=A__       ,resample=A__       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__ = None       ,**A__       ,):
						_A	:				Optional[int]   =      get_size_dict(A__		)
						if "height" not in size or "width" not in size:
										raise ValueError(f"""Size must have 'height' and 'width' as keys. Got {size.keys()}"""		)
						return center_crop(A__       ,size=(size['''height'''], size['''width'''])       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__ = True       ,A__ = None       ,**A__       ,):
						_A	:				Any   =      image.astype(np.floataa		)
						if offset:
										_A	:				List[str]   =      image - (scale / 2)
						return rescale(A__       ,scale=A__       ,data_format=A__       ,**A__		)







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







		def  A__      (					self       ,A__       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = ChannelDimension.FIRST       ,):
						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_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.'''		)

						if offset and not do_rescale:
										raise ValueError('''For offset, do_rescale must also be set to True.'''		)

						# All transformations expect numpy arrays.
						_A	:				Dict   =      to_numpy_array(A__		)

						if do_resize:
										_A	:				Any   =      self.resize(image=A__       ,size=A__       ,resample=A__		)

						if do_center_crop:
										_A	:				str   =      self.center_crop(A__       ,size=A__		)

						if do_rescale:
										_A	:				Optional[int]   =      self.rescale(image=A__       ,scale=A__       ,offset=A__		)

						if do_normalize:
										_A	:				Dict   =      self.normalize(image=A__       ,mean=A__       ,std=A__		)

						_A	:				Union[str, Any]   =      to_channel_dimension_format(A__       ,A__		)
						return image







		def  A__      (					self       ,A__       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = ChannelDimension.FIRST       ,**A__       ,):
						_A	:				Optional[int]   =      do_resize if do_resize is not None else self.do_resize
						_A	:				List[str]   =      resample if resample is not None else self.resample
						_A	:				Union[str, Any]   =      do_center_crop if do_center_crop is not None else self.do_center_crop
						_A	:				Tuple   =      do_rescale if do_rescale is not None else self.do_rescale
						_A	:				List[str]   =      rescale_factor if rescale_factor is not None else self.rescale_factor
						_A	:				List[str]   =      offset if offset is not None else self.offset
						_A	:				Optional[int]   =      do_normalize if do_normalize is not None else self.do_normalize
						_A	:				Optional[Any]   =      image_mean if image_mean is not None else self.image_mean
						_A	:				Dict   =      image_std if image_std is not None else self.image_std

						_A	:				str   =      size if size is not None else self.size
						_A	:				int   =      get_size_dict(A__       ,default_to_square=A__		)
						_A	:				Any   =      crop_size if crop_size is not None else self.crop_size
						_A	:				Optional[int]   =      get_size_dict(A__       ,param_name='''crop_size'''		)

						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.'''		)

						_A	:				List[str]   =      make_batched(A__		)

						_A	:				Tuple   =      [
						    [
						        self._preprocess_image(
						            image=A__       ,do_resize=A__       ,size=A__       ,resample=A__       ,do_center_crop=A__       ,crop_size=A__       ,do_rescale=A__       ,rescale_factor=A__       ,offset=A__       ,do_normalize=A__       ,image_mean=A__       ,image_std=A__       ,data_format=A__       ,)
						        for img in video
						    ]
						    for video in videos
						]

						_A	:				Optional[int]   =      {'''pixel_values''': videos}
						return BatchFeature(data=A__       ,tensor_type=A__		)



