


"""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 (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


__a      			= logging.get_logger(__name__)


if is_vision_available():
						import PIL
class 				lowerCamelCase							(      __SCREAMING_SNAKE_CASE			):


							"""simple docstring"""
							SCREAMING_SNAKE_CASE              =   ["""pixel_values"""]






							def __init__(    self			:  Dict		,			lowerCamelCase_			:  bool = True		,			lowerCamelCase_			:  Dict[str, int] = None		,			lowerCamelCase_			:  PILImageResampling = PILImageResampling.BILINEAR		,			lowerCamelCase_			:  bool = True		,			lowerCamelCase_			:  bool = False		,			lowerCamelCase_			:  bool = True		,			lowerCamelCase_			:  bool = True		,			lowerCamelCase_			:  Union[int, float] = 1 / 255		,			lowerCamelCase_			:  bool = True		,			lowerCamelCase_			:  Optional[Union[float, List[float]]] = None		,			lowerCamelCase_			:  Optional[Union[float, List[float]]] = None		,			**lowerCamelCase_			:  Any		,			):
													super().__init__(**lowerCamelCase_					)

													UpperCAmelCase__												=     size if size is not None else {'''height''': 2560, '''width''': 1920}
													if isinstance(lowerCamelCase_		,			(tuple, list)					):
																			# The previous feature extractor size parameter was in (width, height) format
																			UpperCAmelCase__												=     size[::-1]
													UpperCAmelCase__												=     get_size_dict(lowerCamelCase_					)

													UpperCAmelCase__												=     do_resize
													UpperCAmelCase__												=     size
													UpperCAmelCase__												=     resample
													UpperCAmelCase__												=     do_thumbnail
													UpperCAmelCase__												=     do_align_long_axis
													UpperCAmelCase__												=     do_pad
													UpperCAmelCase__												=     do_rescale
													UpperCAmelCase__												=     rescale_factor
													UpperCAmelCase__												=     do_normalize
													UpperCAmelCase__												=     image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
													UpperCAmelCase__												=     image_std if image_std is not None else IMAGENET_STANDARD_STD






							def        lowerCAmelCase  (    self			:  Tuple		,			lowerCamelCase_			:  np.ndarray		,			lowerCamelCase_			:  Dict[str, int]		,			lowerCamelCase_			:  Optional[Union[str, ChannelDimension]] = None					):
													UpperCAmelCase__						,					UpperCAmelCase__												=     get_image_size(lowerCamelCase_					)
													UpperCAmelCase__						,					UpperCAmelCase__												=     size['''height'''], size['''width''']

													if (output_width < output_height and input_width > input_height) or (
													    output_width > output_height and input_width < input_height
													):
																			UpperCAmelCase__												=     np.rotaa(lowerCamelCase_		,			3					)

													if data_format is not None:
																			UpperCAmelCase__												=     to_channel_dimension_format(lowerCamelCase_		,			lowerCamelCase_					)

													return image






							def        lowerCAmelCase  (    self			:  Tuple		,			lowerCamelCase_			:  np.ndarray		,			lowerCamelCase_			:  Dict[str, int]		,			lowerCamelCase_			:  bool = False		,			lowerCamelCase_			:  Optional[Union[str, ChannelDimension]] = None		,			):
													UpperCAmelCase__						,					UpperCAmelCase__												=     size['''height'''], size['''width''']
													UpperCAmelCase__						,					UpperCAmelCase__												=     get_image_size(lowerCamelCase_					)

													UpperCAmelCase__												=     output_width - input_width
													UpperCAmelCase__												=     output_height - input_height

													if random_padding:
																			UpperCAmelCase__												=     np.random.randint(low=0		,			high=delta_height + 1					)
																			UpperCAmelCase__												=     np.random.randint(low=0		,			high=delta_width + 1					)
													else:
																			UpperCAmelCase__												=     delta_height // 2
																			UpperCAmelCase__												=     delta_width // 2

													UpperCAmelCase__												=     delta_height - pad_top
													UpperCAmelCase__												=     delta_width - pad_left

													UpperCAmelCase__												=     ((pad_top, pad_bottom), (pad_left, pad_right))
													return pad(lowerCamelCase_		,			lowerCamelCase_		,			data_format=lowerCamelCase_					)






							def        lowerCAmelCase  (    self			:  int		,			*lowerCamelCase_			:  Tuple		,			**lowerCamelCase_			:  Tuple					):
													logger.info('''pad is deprecated and will be removed in version 4.27. Please use pad_image instead.'''					)
													return self.pad_image(*lowerCamelCase_		,			**lowerCamelCase_					)






							def        lowerCAmelCase  (    self			:  Optional[Any]		,			lowerCamelCase_			:  np.ndarray		,			lowerCamelCase_			:  Dict[str, int]		,			lowerCamelCase_			:  PILImageResampling = PILImageResampling.BICUBIC		,			lowerCamelCase_			:  Optional[Union[str, ChannelDimension]] = None		,			**lowerCamelCase_			:  Any		,			):
													UpperCAmelCase__						,					UpperCAmelCase__												=     get_image_size(lowerCamelCase_					)
													UpperCAmelCase__						,					UpperCAmelCase__												=     size['''height'''], size['''width''']

													# We always resize to the smallest of either the input or output size.
													UpperCAmelCase__												=     min(lowerCamelCase_		,			lowerCamelCase_					)
													UpperCAmelCase__												=     min(lowerCamelCase_		,			lowerCamelCase_					)

													if height == input_height and width == input_width:
																			return image

													if input_height > input_width:
																			UpperCAmelCase__												=     int(input_width * height / input_height					)
													elif input_width > input_height:
																			UpperCAmelCase__												=     int(input_height * width / input_width					)

													return resize(
													    lowerCamelCase_		,			size=(height, width)		,			resample=lowerCamelCase_		,			reducing_gap=2.0		,			data_format=lowerCamelCase_		,			**lowerCamelCase_					)






							def        lowerCAmelCase  (    self			:  Optional[int]		,			lowerCamelCase_			:  np.ndarray		,			lowerCamelCase_			:  Dict[str, int]		,			lowerCamelCase_			:  PILImageResampling = PILImageResampling.BICUBIC		,			lowerCamelCase_			:  Optional[Union[str, ChannelDimension]] = None		,			**lowerCamelCase_			:  Optional[Any]		,			):
													UpperCAmelCase__												=     get_size_dict(lowerCamelCase_					)
													UpperCAmelCase__												=     min(size['''height''']		,			size['''width''']					)
													UpperCAmelCase__												=     get_resize_output_image_size(lowerCamelCase_		,			size=lowerCamelCase_		,			default_to_square=lowerCamelCase_					)
													UpperCAmelCase__												=     resize(lowerCamelCase_		,			size=lowerCamelCase_		,			resample=lowerCamelCase_		,			data_format=lowerCamelCase_		,			**lowerCamelCase_					)
													return resized_image






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






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






							def        lowerCAmelCase  (    self			:  Tuple		,			lowerCamelCase_			:  ImageInput		,			lowerCamelCase_			:  bool = None		,			lowerCamelCase_			:  Dict[str, int] = None		,			lowerCamelCase_			:  PILImageResampling = None		,			lowerCamelCase_			:  bool = None		,			lowerCamelCase_			:  bool = None		,			lowerCamelCase_			:  bool = None		,			lowerCamelCase_			:  bool = False		,			lowerCamelCase_			:  bool = None		,			lowerCamelCase_			:  float = None		,			lowerCamelCase_			:  bool = None		,			lowerCamelCase_			:  Optional[Union[float, List[float]]] = None		,			lowerCamelCase_			:  Optional[Union[float, List[float]]] = None		,			lowerCamelCase_			:  Optional[Union[str, TensorType]] = None		,			lowerCamelCase_			:  Optional[ChannelDimension] = ChannelDimension.FIRST		,			**lowerCamelCase_			:  List[str]		,			):
													UpperCAmelCase__												=     do_resize if do_resize is not None else self.do_resize
													UpperCAmelCase__												=     size if size is not None else self.size
													if isinstance(lowerCamelCase_		,			(tuple, list)					):
																			# Previous feature extractor had size in (width, height) format
																			UpperCAmelCase__												=     size[::-1]
													UpperCAmelCase__												=     get_size_dict(lowerCamelCase_					)
													UpperCAmelCase__												=     resample if resample is not None else self.resample
													UpperCAmelCase__												=     do_thumbnail if do_thumbnail is not None else self.do_thumbnail
													UpperCAmelCase__												=     do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
													UpperCAmelCase__												=     do_pad if do_pad is not None else self.do_pad
													UpperCAmelCase__												=     do_rescale if do_rescale is not None else self.do_rescale
													UpperCAmelCase__												=     rescale_factor if rescale_factor is not None else self.rescale_factor
													UpperCAmelCase__												=     do_normalize if do_normalize is not None else self.do_normalize
													UpperCAmelCase__												=     image_mean if image_mean is not None else self.image_mean
													UpperCAmelCase__												=     image_std if image_std is not None else self.image_std

													UpperCAmelCase__												=     make_list_of_images(lowerCamelCase_					)

													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_rescale and rescale_factor is None:
																			raise ValueError('''Rescale factor must be specified if do_rescale is True.'''					)

													if do_pad and size is None:
																			raise ValueError('''Size must be specified if do_pad 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.
													UpperCAmelCase__												=     [to_numpy_array(lowerCamelCase_					) for image in images]

													if do_align_long_axis:
																			UpperCAmelCase__												=     [self.align_long_axis(lowerCamelCase_		,			size=lowerCamelCase_					) for image in images]

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

													if do_thumbnail:
																			UpperCAmelCase__												=     [self.thumbnail(image=lowerCamelCase_		,			size=lowerCamelCase_					) for image in images]

													if do_pad:
																			UpperCAmelCase__												=     [self.pad_image(image=lowerCamelCase_		,			size=lowerCamelCase_		,			random_padding=lowerCamelCase_					) for image in images]

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

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

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

													UpperCAmelCase__												=     {'''pixel_values''': images}
													return BatchFeature(data=lowerCamelCase_		,			tensor_type=lowerCamelCase_					)



"""simple docstring"""







import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


__a      			= [nn.LayerNorm]

__a      			= logging.get_logger(__name__)

__a      			= version.parse(version.parse(torch.__version__).base_version)

__a      			= parsed_torch_version_base >= version.parse("2.0")
__a      			= parsed_torch_version_base >= version.parse("1.12")
__a      			= parsed_torch_version_base >= version.parse("1.11")
__a      			= parsed_torch_version_base < version.parse("1.11")






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      self			):





						"""simple docstring"""

						from torch import _softmax_backward_data

						if is_torch_less_than_1_11:
												return _softmax_backward_data(_UpperCamelCase						,      _UpperCamelCase						,      parent.dim						,      self			)
						else:
												return _softmax_backward_data(_UpperCamelCase						,      _UpperCamelCase						,      parent.dim						,      self.dtype			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase = 0			):





						"""simple docstring"""

						UpperCAmelCase__												=     index.to(layer.weight.device			)
						UpperCAmelCase__												=     layer.weight.index_select(_UpperCamelCase						,      _UpperCamelCase			).clone().detach()
						if layer.bias is not None:
												if dim == 1:
																		UpperCAmelCase__												=     layer.bias.clone().detach()
												else:
																		UpperCAmelCase__												=     layer.bias[index].clone().detach()
						UpperCAmelCase__												=     list(layer.weight.size()			)
						UpperCAmelCase__												=     len(_UpperCamelCase			)
						UpperCAmelCase__												=     nn.Linear(new_size[1]						,      new_size[0]						,      bias=layer.bias is not None			).to(layer.weight.device			)
						UpperCAmelCase__												=     False
						new_layer.weight.copy_(W.contiguous()			)
						UpperCAmelCase__												=     True
						if layer.bias is not None:
												UpperCAmelCase__												=     False
												new_layer.bias.copy_(b.contiguous()			)
												UpperCAmelCase__												=     True
						return new_layer
class 				lowerCamelCase							(      nn.Module			):


							"""simple docstring"""






							def __init__(    self			:  str		,			lowerCamelCase_			:  Any		,			lowerCamelCase_			:  Dict					):
													super().__init__()
													UpperCAmelCase__												=     nf
													UpperCAmelCase__												=     nn.Parameter(torch.empty(lowerCamelCase_		,			lowerCamelCase_					)					)
													UpperCAmelCase__												=     nn.Parameter(torch.zeros(lowerCamelCase_					)					)
													nn.init.normal_(self.weight		,			std=0.02					)






							def        lowerCAmelCase  (    self			:  Any		,			lowerCamelCase_			:  Tuple					):
													UpperCAmelCase__												=     x.size()[:-1] + (self.nf,)
													UpperCAmelCase__												=     torch.addmm(self.bias		,			x.view(-1		,			x.size(-1					)					)		,			self.weight					)
													UpperCAmelCase__												=     x.view(lowerCamelCase_					)
													return x






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase = 1			):





						"""simple docstring"""

						UpperCAmelCase__												=     index.to(layer.weight.device			)
						UpperCAmelCase__												=     layer.weight.index_select(_UpperCamelCase						,      _UpperCamelCase			).clone().detach()
						if dim == 0:
												UpperCAmelCase__												=     layer.bias.clone().detach()
						else:
												UpperCAmelCase__												=     layer.bias[index].clone().detach()
						UpperCAmelCase__												=     list(layer.weight.size()			)
						UpperCAmelCase__												=     len(_UpperCamelCase			)
						UpperCAmelCase__												=     ConvaD(new_size[1]						,      new_size[0]			).to(layer.weight.device			)
						UpperCAmelCase__												=     False
						new_layer.weight.copy_(W.contiguous()			)
						UpperCAmelCase__												=     True
						UpperCAmelCase__												=     False
						new_layer.bias.copy_(b.contiguous()			)
						UpperCAmelCase__												=     True
						return new_layer






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase = None			):





						"""simple docstring"""

						if isinstance(_UpperCamelCase						,      nn.Linear			):
												return prune_linear_layer(_UpperCamelCase						,      _UpperCamelCase						,      dim=0 if dim is None else dim			)
						elif isinstance(_UpperCamelCase						,      _UpperCamelCase			):
												return prune_convad_layer(_UpperCamelCase						,      _UpperCamelCase						,      dim=1 if dim is None else dim			)
						else:
												raise ValueError(F"""Can't prune layer of class {layer.__class__}"""			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      *_UpperCamelCase			):





						"""simple docstring"""

						assert len(_UpperCamelCase			) > 0, F"""{input_tensors} has to be a tuple/list of tensors"""

						# inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
						UpperCAmelCase__												=     len(inspect.signature(_UpperCamelCase			).parameters			)
						if num_args_in_forward_chunk_fn != len(_UpperCamelCase			):
												raise ValueError(
												    F"""forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(_UpperCamelCase			)} input """
												    '''tensors are given'''			)

						if chunk_size > 0:
												UpperCAmelCase__												=     input_tensors[0].shape[chunk_dim]
												for input_tensor in input_tensors:
																		if input_tensor.shape[chunk_dim] != tensor_shape:
																								raise ValueError(
																								    F"""All input tenors have to be of the same shape: {tensor_shape}, """
																								    F"""found shape {input_tensor.shape[chunk_dim]}"""			)

												if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
																		raise ValueError(
																		    F"""The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk """
																		    F"""size {chunk_size}"""			)

												UpperCAmelCase__												=     input_tensors[0].shape[chunk_dim] // chunk_size

												# chunk input tensor into tuples
												UpperCAmelCase__												=     tuple(input_tensor.chunk(_UpperCamelCase						,      dim=_UpperCamelCase			) for input_tensor in input_tensors			)
												# apply forward fn to every tuple
												UpperCAmelCase__												=     tuple(forward_fn(*_UpperCamelCase			) for input_tensors_chunk in zip(*_UpperCamelCase			)			)
												# concatenate output at same dimension
												return torch.cat(_UpperCamelCase						,      dim=_UpperCamelCase			)

						return forward_fn(*_UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase			):





						"""simple docstring"""

						UpperCAmelCase__												=     torch.ones(_UpperCamelCase						,      _UpperCamelCase			)
						UpperCAmelCase__												=     set(_UpperCamelCase			) - already_pruned_heads  # Convert to set and remove already pruned heads
						for head in heads:
												# Compute how many pruned heads are before the head and move the index accordingly
												UpperCAmelCase__												=     head - sum(1 if h < head else 0 for h in already_pruned_heads			)
												UpperCAmelCase__												=     0
						UpperCAmelCase__												=     mask.view(-1			).contiguous().eq(1			)
						UpperCAmelCase__												=     torch.arange(len(_UpperCamelCase			)			)[mask].long()
						return heads, index






def        a__			(							*_UpperCamelCase						,      _UpperCamelCase = None			):





						"""simple docstring"""

						return torch.meshgrid(*_UpperCamelCase						,      indexing=_UpperCamelCase			)






def        a__			(							_UpperCamelCase			):





						"""simple docstring"""

						return tensor.device, storage_ptr(_UpperCamelCase			), storage_size(_UpperCamelCase			)