






from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


__snake_case      :      List[Any]             =       logging.get_logger(__name__)







def 		_UpperCamelCase				(      UpperCamelCase_    :   Union[tf.Tensor, np.ndarray]						)					->				List[int]:

						"""simple docstring"""



						if isinstance(UpperCamelCase_      ,    np.ndarray						):
												return list(tensor.shape						)

						lowerCAmelCase__        =  tf.shape(UpperCamelCase_						)

						if tensor.shape == tf.TensorShape(UpperCamelCase_						):
												return dynamic

						lowerCAmelCase__        =  tensor.shape.as_list()

						return [dynamic[i] if s is None else s for i, s in enumerate(UpperCamelCase_						)]







def 		_UpperCamelCase				(      UpperCamelCase_    :   tf.Tensor      ,    UpperCamelCase_    :   Optional[int] = None      ,    UpperCamelCase_    :   Optional[str] = None						)					->				tf.Tensor:

						"""simple docstring"""



						return tf.nn.softmax(logits=logits + 1e-9      ,    axis=UpperCamelCase_      ,    name=UpperCamelCase_						)







def 		_UpperCamelCase				(      UpperCamelCase_    :   Tuple      ,    UpperCamelCase_    :   Optional[Any]      ,    UpperCamelCase_    :   List[Any]      ,    UpperCamelCase_    :   Tuple=1e-5      ,    UpperCamelCase_    :   Union[str, Any]=-1						)					->				Union[str, Any]:

						"""simple docstring"""



						if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(UpperCamelCase_      ,    UpperCamelCase_						):
												raise NotImplementedError('Only 1D weight and bias tensors are supported for now, with only a single axis.'						)

						# Get mean and variance on the axis to be normalized
						lowerCAmelCase__  ,    lowerCAmelCase__        =  tf.nn.moments(UpperCamelCase_      ,    axes=[axis]      ,    keepdims=UpperCamelCase_						)

						if axis != -1:
												# Reshape scale and weight to have the same rank as inputs, but with 1 dimensions
												# on every dimension except axis
												lowerCAmelCase__        =  [1] * inputs.shape.rank
												lowerCAmelCase__        =  shape_list(UpperCamelCase_						)[axis]
												lowerCAmelCase__        =  tf.reshape(UpperCamelCase_      ,    UpperCamelCase_						)
												lowerCAmelCase__        =  tf.reshape(UpperCamelCase_      ,    UpperCamelCase_						)

						# Compute layer normalization using the batch_normalization
						# function.
						lowerCAmelCase__        =  tf.nn.batch_normalization(
						    UpperCamelCase_      ,    UpperCamelCase_      ,    UpperCamelCase_      ,    offset=UpperCamelCase_      ,    scale=UpperCamelCase_      ,    variance_epsilon=UpperCamelCase_      ,    )
						return outputs







def 		_UpperCamelCase				(      UpperCamelCase_    :   Any      ,    UpperCamelCase_    :   Optional[int]=0      ,    UpperCamelCase_    :   List[str]=-1						)					->				List[str]:

						"""simple docstring"""



						if end_dim < 0:
												end_dim += input.shape.rank
						if start_dim < 0:
												start_dim += input.shape.rank

						if start_dim == end_dim:
												return input

						lowerCAmelCase__        =  tf.shape(UpperCamelCase_						)
						lowerCAmelCase__        =  tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]						)
						lowerCAmelCase__        =  tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]]      ,    axis=0						)
						return tf.reshape(UpperCamelCase_      ,    UpperCamelCase_						)







def 		_UpperCamelCase				(      UpperCamelCase_    :   tf.Tensor						)					->				tf.Tensor:

						"""simple docstring"""



						if not isinstance(UpperCamelCase_      ,    tf.Tensor						):
												lowerCAmelCase__        =  tf.convert_to_tensor(UpperCamelCase_						)  # Catches stray NumPy inputs
						if encoder_attention_mask.shape.rank == 3:
												lowerCAmelCase__        =  encoder_attention_mask[:, None, :, :]
						if encoder_attention_mask.shape.rank == 2:
												lowerCAmelCase__        =  encoder_attention_mask[:, None, None, :]
						# T5 has a mask that can compare sequence ids, we can simulate this here with this transposition
						# Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow
						# /transformer/transformer_layers.py#L270
						# encoder_extended_attention_mask = (encoder_extended_attention_mask ==
						# encoder_extended_attention_mask.transpose(-1, -2))
						lowerCAmelCase__        =  (
						    tf.cast(1      ,    encoder_attention_mask.dtype						) - encoder_extended_attention_mask
						) * encoder_extended_attention_mask.dtype.min

						return encoder_extended_attention_mask







def 		_UpperCamelCase				(      UpperCamelCase_    :   tf.Tensor      ,    UpperCamelCase_    :   int      ,    UpperCamelCase_    :   str = "input_ids"						)					->				None:

						"""simple docstring"""



						tf.debugging.assert_less(
						    UpperCamelCase_      ,    tf.cast(UpperCamelCase_      ,    dtype=tensor.dtype						)      ,    message=(
						        F"The maximum value of {tensor_name} ({tf.math.reduce_max(UpperCamelCase_						)}) must be smaller than the embedding "
						        F"layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time."
						    )      ,    )







def 		_UpperCamelCase				(      UpperCamelCase_    :   List[Any]      ,    UpperCamelCase_    :   List[str]      ,    UpperCamelCase_    :   str						)					->				List[str]:

						"""simple docstring"""



						lowerCAmelCase__        =  6_4512
						# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`
						# because in that case even chunking the array would not make the saving
						# possible.
						lowerCAmelCase__        =  [x for x in data if len(UpperCamelCase_						) > HDF5_OBJECT_HEADER_LIMIT]

						# Expecting this to never be true.
						if bad_attributes:
												raise RuntimeError(
												    'The following attributes cannot be saved to HDF5 file because '
												    F"they are larger than {HDF5_OBJECT_HEADER_LIMIT} "
												    F"bytes: {bad_attributes}"						)

						lowerCAmelCase__        =  np.asarray(UpperCamelCase_						)

						lowerCAmelCase__        =  1
						lowerCAmelCase__        =  np.array_split(UpperCamelCase_      ,    UpperCamelCase_						)

						# This will never loop forever thanks to the test above.
						while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data						):
												num_chunks += 1
												lowerCAmelCase__        =  np.array_split(UpperCamelCase_      ,    UpperCamelCase_						)

						if num_chunks > 1:
												for chunk_id, chunk_data in enumerate(UpperCamelCase_						):
																		lowerCAmelCase__        =  chunk_data
						else:
												lowerCAmelCase__        =  data







def 		_UpperCamelCase				(      UpperCamelCase_    :   List[Any]      ,    UpperCamelCase_    :   int						)					->				Any:

						"""simple docstring"""



						if name in group.attrs:
												lowerCAmelCase__        =  [n.decode('utf8'						) if hasattr(UpperCamelCase_      ,    'decode'						) else n for n in group.attrs[name]]
						else:
												lowerCAmelCase__        =  []
												lowerCAmelCase__        =  0
												while "%s%d" % (name, chunk_id) in group.attrs:
																		data.extend(
																		    [n.decode('utf8'						) if hasattr(UpperCamelCase_      ,    'decode'						) else n for n in group.attrs['%s%d' % (name, chunk_id)]]						)
																		chunk_id += 1
						return data







def 		_UpperCamelCase				(      UpperCamelCase_    :   Union[str, Any]						)					->				str:

						"""simple docstring"""



						def _expand_single_ad_tensor(UpperCamelCase_    :   Union[str, Any]						):
												if isinstance(UpperCamelCase_      ,    tf.Tensor						) and t.shape.rank == 1:
																		return tf.expand_dims(UpperCamelCase_      ,    axis=-1						)
												return t

						return tf.nest.map_structure(_expand_single_ad_tensor      ,    UpperCamelCase_						)













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


__snake_case      :      Any             =       logging.get_logger(__name__)


class       __SCREAMING_SNAKE_CASE   (				__lowercase):
						_SCREAMING_SNAKE_CASE		:    List[Any]						=   ['''pixel_values''']






						def __init__(		self       ,  _UpperCamelCase = True       ,  _UpperCamelCase = None       ,  _UpperCamelCase = PILImageResampling.BILINEAR       ,  _UpperCamelCase = True       ,  _UpperCamelCase = None       ,  _UpperCamelCase = True       ,  _UpperCamelCase = 1 / 2_55       ,  _UpperCamelCase = True       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												super().__init__(**_UpperCamelCase				)
												lowerCAmelCase__        =  size if size is not None else {'shortest_edge': 2_56}
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase       ,  default_to_square=_UpperCamelCase				)
												lowerCAmelCase__        =  crop_size if crop_size is not None else {'height': 2_24, 'width': 2_24}
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase				)
												lowerCAmelCase__        =  do_resize
												lowerCAmelCase__        =  size
												lowerCAmelCase__        =  resample
												lowerCAmelCase__        =  do_center_crop
												lowerCAmelCase__        =  crop_size
												lowerCAmelCase__        =  do_rescale
												lowerCAmelCase__        =  rescale_factor
												lowerCAmelCase__        =  do_normalize
												lowerCAmelCase__        =  image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
												lowerCAmelCase__        =  image_std if image_std is not None else IMAGENET_STANDARD_STD






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = PILImageResampling.BICUBIC       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  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()}"				)
												lowerCAmelCase__        =  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  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase				)
												return center_crop(_UpperCamelCase       ,  size=(size['height'], size['width'])       ,  data_format=_UpperCamelCase       ,  **_UpperCamelCase				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  **_UpperCamelCase				):






												"""simple docstring"""
												return rescale(_UpperCamelCase       ,  scale=_UpperCamelCase       ,  data_format=_UpperCamelCase       ,  **_UpperCamelCase				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												return normalize(_UpperCamelCase       ,  mean=_UpperCamelCase       ,  std=_UpperCamelCase       ,  data_format=_UpperCamelCase       ,  **_UpperCamelCase				)







						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = None       ,  _UpperCamelCase = ChannelDimension.FIRST       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  do_resize if do_resize is not None else self.do_resize
												lowerCAmelCase__        =  size if size is not None else self.size
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase       ,  default_to_square=_UpperCamelCase				)
												lowerCAmelCase__        =  resample if resample is not None else self.resample
												lowerCAmelCase__        =  do_center_crop if do_center_crop is not None else self.do_center_crop
												lowerCAmelCase__        =  crop_size if crop_size is not None else self.crop_size
												lowerCAmelCase__        =  get_size_dict(_UpperCamelCase				)
												lowerCAmelCase__        =  do_rescale if do_rescale is not None else self.do_rescale
												lowerCAmelCase__        =  rescale_factor if rescale_factor is not None else self.rescale_factor
												lowerCAmelCase__        =  do_normalize if do_normalize is not None else self.do_normalize
												lowerCAmelCase__        =  image_mean if image_mean is not None else self.image_mean
												lowerCAmelCase__        =  image_std if image_std is not None else self.image_std

												lowerCAmelCase__        =  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.
												lowerCAmelCase__        =  [to_numpy_array(_UpperCamelCase				) for image in images]

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

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

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

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

												lowerCAmelCase__        =  [to_channel_dimension_format(_UpperCamelCase       ,  _UpperCamelCase				) for image in images]

												lowerCAmelCase__        =  {'pixel_values': images}
												return BatchFeature(data=_UpperCamelCase       ,  tensor_type=_UpperCamelCase				)





