




'''simple docstring'''

import torch
from torch import nn



class 		SCREAMING_SNAKE_CASE__  (			nn.Module	):





			def __init__(      self						:    Any      ,		a_						:    Dict      ,		a_						:    List[str]      ,		a_						:    List[str]      ,		a_						:    Any      ,		a_						:    Dict=1      ,		a_						:    List[str]=False							):



						"""simple docstring"""




						super().__init__()

						__snake_case			=		n_token
						__snake_case			=		d_embed
						__snake_case			=		d_proj

						__snake_case			=		cutoffs + [n_token]
						__snake_case			=		[0] + self.cutoffs
						__snake_case			=		div_val

						__snake_case			=		self.cutoffs[0]
						__snake_case			=		len(self.cutoffs							) - 1
						__snake_case			=		self.shortlist_size + self.n_clusters

						if self.n_clusters > 0:
									__snake_case			=		nn.Parameter(torch.zeros(self.n_clusters      ,		self.d_embed							)							)
									__snake_case			=		nn.Parameter(torch.zeros(self.n_clusters							)							)

						__snake_case			=		nn.ModuleList()
						__snake_case			=		nn.ParameterList()

						if div_val == 1:
									for i in range(len(self.cutoffs							)							):
												if d_proj != d_embed:
															self.out_projs.append(nn.Parameter(torch.FloatTensor(_UpperCAmelCase      ,		_UpperCAmelCase							)							)							)
												else:
															self.out_projs.append(_UpperCAmelCase							)

									self.out_layers.append(nn.Linear(_UpperCAmelCase      ,		_UpperCAmelCase							)							)
						else:
									for i in range(len(self.cutoffs							)							):
												__snake_case		,     __snake_case			=		self.cutoff_ends[i], self.cutoff_ends[i + 1]
												__snake_case			=		d_embed // (div_val**i)

												self.out_projs.append(nn.Parameter(torch.FloatTensor(_UpperCAmelCase      ,		_UpperCAmelCase							)							)							)

												self.out_layers.append(nn.Linear(_UpperCAmelCase      ,		r_idx - l_idx							)							)

						__snake_case			=		keep_order





			def 							A (      self						:    Tuple      ,		a_						:    str      ,		a_						:    Tuple      ,		a_						:    int      ,		a_						:    Union[str, Any]							):



						"""simple docstring"""




						if proj is None:
									__snake_case			=		nn.functional.linear(_UpperCAmelCase      ,		_UpperCAmelCase      ,		bias=_UpperCAmelCase							)
						else:
									# if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
									__snake_case			=		nn.functional.linear(_UpperCAmelCase      ,		proj.t().contiguous()							)
									__snake_case			=		nn.functional.linear(_UpperCAmelCase      ,		_UpperCAmelCase      ,		bias=_UpperCAmelCase							)
									# else:
									#     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
									#     if bias is not None:
									#         logit = logit + bias

						return logit





			def 							A (      self						:    Dict      ,		a_						:    Dict      ,		a_						:    int=None      ,		a_						:    List[str]=False							):



						"""simple docstring"""




						if labels is not None:
									# Shift so that tokens < n predict n
									__snake_case			=		hidden[..., :-1, :].contiguous()
									__snake_case			=		labels[..., 1:].contiguous()
									__snake_case			=		hidden.view(-1      ,		hidden.size(-1							)							)
									__snake_case			=		labels.view(-1							)
									if hidden.size(0							) != labels.size(0							):
												raise RuntimeError("Input and labels should have the same size in the batch dimension."							)
						else:
									__snake_case			=		hidden.view(-1      ,		hidden.size(-1							)							)

						if self.n_clusters == 0:
									__snake_case			=		self._compute_logit(_UpperCAmelCase      ,		self.out_layers[0].weight      ,		self.out_layers[0].bias      ,		self.out_projs[0]							)
									if labels is not None:
												__snake_case			=		labels != -100
												__snake_case			=		torch.zeros_like(_UpperCAmelCase      ,		dtype=hidden.dtype      ,		device=hidden.device							)
												__snake_case			=		(
												    -nn.functional.log_softmax(_UpperCAmelCase      ,		dim=-1							)[mask].gather(1      ,		labels[mask].unsqueeze(1							)							).squeeze(1							)
												)
									else:
												__snake_case			=		nn.functional.log_softmax(_UpperCAmelCase      ,		dim=-1							)
						else:
									# construct weights and biases
									__snake_case		,     __snake_case			=		[], []
									for i in range(len(self.cutoffs							)							):
												if self.div_val == 1:
															__snake_case		,     __snake_case			=		self.cutoff_ends[i], self.cutoff_ends[i + 1]
															__snake_case			=		self.out_layers[0].weight[l_idx:r_idx]
															__snake_case			=		self.out_layers[0].bias[l_idx:r_idx]
												else:
															__snake_case			=		self.out_layers[i].weight
															__snake_case			=		self.out_layers[i].bias

												if i == 0:
															__snake_case			=		torch.cat([weight_i, self.cluster_weight]      ,		dim=0							)
															__snake_case			=		torch.cat([bias_i, self.cluster_bias]      ,		dim=0							)

												weights.append(_UpperCAmelCase							)
												biases.append(_UpperCAmelCase							)

									__snake_case		,     __snake_case		,     __snake_case			=		weights[0], biases[0], self.out_projs[0]

									__snake_case			=		self._compute_logit(_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase							)
									__snake_case			=		nn.functional.log_softmax(_UpperCAmelCase      ,		dim=1							)

									if labels is None:
												__snake_case			=		hidden.new_empty((head_logit.size(0							), self.n_token)							)
									else:
												__snake_case			=		torch.zeros_like(_UpperCAmelCase      ,		dtype=hidden.dtype      ,		device=hidden.device							)

									__snake_case			=		0
									__snake_case			=		[0] + self.cutoffs
									for i in range(len(_UpperCAmelCase							) - 1							):
												__snake_case		,     __snake_case			=		cutoff_values[i], cutoff_values[i + 1]

												if labels is not None:
															__snake_case			=		(labels >= l_idx) & (labels < r_idx)
															__snake_case			=		mask_i.nonzero().squeeze()

															if indices_i.numel() == 0:
																		continue

															__snake_case			=		labels.index_select(0      ,		_UpperCAmelCase							) - l_idx
															__snake_case			=		head_logprob.index_select(0      ,		_UpperCAmelCase							)
															__snake_case			=		hidden.index_select(0      ,		_UpperCAmelCase							)
												else:
															__snake_case			=		hidden

												if i == 0:
															if labels is not None:
																		__snake_case			=		head_logprob_i.gather(1      ,		target_i[:, None]							).squeeze(1							)
															else:
																		__snake_case			=		head_logprob[:, : self.cutoffs[0]]
												else:
															__snake_case		,     __snake_case		,     __snake_case			=		weights[i], biases[i], self.out_projs[i]

															__snake_case			=		self._compute_logit(_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase							)
															__snake_case			=		nn.functional.log_softmax(_UpperCAmelCase      ,		dim=1							)
															__snake_case			=		self.cutoffs[0] + i - 1  # No probability for the head cluster
															if labels is not None:
																		__snake_case			=		head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
																		    1      ,		target_i[:, None]							).squeeze(1							)
															else:
																		__snake_case			=		head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
																		__snake_case			=		logprob_i

												if labels is not None:
															if (hasattr(self      ,		"keep_order"							) and self.keep_order) or keep_order:
																		out.index_copy_(0      ,		_UpperCAmelCase      ,		-logprob_i							)
															else:
																		out[offset : offset + logprob_i.size(0							)].copy_(-logprob_i							)
															offset += logprob_i.size(0							)

						return out






			def 							A (      self						:    List[str]      ,		a_						:    Optional[Any]							):



						"""simple docstring"""




						if self.n_clusters == 0:
									__snake_case			=		self._compute_logit(_UpperCAmelCase      ,		self.out_layers[0].weight      ,		self.out_layers[0].bias      ,		self.out_projs[0]							)
									return nn.functional.log_softmax(_UpperCAmelCase      ,		dim=-1							)
						else:
									# construct weights and biases
									__snake_case		,     __snake_case			=		[], []
									for i in range(len(self.cutoffs							)							):
												if self.div_val == 1:
															__snake_case		,     __snake_case			=		self.cutoff_ends[i], self.cutoff_ends[i + 1]
															__snake_case			=		self.out_layers[0].weight[l_idx:r_idx]
															__snake_case			=		self.out_layers[0].bias[l_idx:r_idx]
												else:
															__snake_case			=		self.out_layers[i].weight
															__snake_case			=		self.out_layers[i].bias

												if i == 0:
															__snake_case			=		torch.cat([weight_i, self.cluster_weight]      ,		dim=0							)
															__snake_case			=		torch.cat([bias_i, self.cluster_bias]      ,		dim=0							)

												weights.append(_UpperCAmelCase							)
												biases.append(_UpperCAmelCase							)

									__snake_case		,     __snake_case		,     __snake_case			=		weights[0], biases[0], self.out_projs[0]
									__snake_case			=		self._compute_logit(_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase							)

									__snake_case			=		hidden.new_empty((head_logit.size(0							), self.n_token)							)
									__snake_case			=		nn.functional.log_softmax(_UpperCAmelCase      ,		dim=1							)

									__snake_case			=		[0] + self.cutoffs
									for i in range(len(_UpperCAmelCase							) - 1							):
												__snake_case		,     __snake_case			=		cutoff_values[i], cutoff_values[i + 1]

												if i == 0:
															__snake_case			=		head_logprob[:, : self.cutoffs[0]]
												else:
															__snake_case		,     __snake_case		,     __snake_case			=		weights[i], biases[i], self.out_projs[i]

															__snake_case			=		self._compute_logit(_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase      ,		_UpperCAmelCase							)
															__snake_case			=		nn.functional.log_softmax(_UpperCAmelCase      ,		dim=1							)

															__snake_case			=		head_logprob[:, -i] + tail_logprob_i
															__snake_case			=		logprob_i

									return out













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


if is_vision_available():
 import PIL


snake_case__   :						str													=   logging.get_logger(__name__)






def      _snake_case     (__lowercase):
   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     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  A_						       = ["""pixel_values"""]




  def __init__(  self		,		_UpperCAmelCase = True		,		_UpperCAmelCase = None		,		_UpperCAmelCase = PILImageResampling.BILINEAR		,		_UpperCAmelCase = True		,		_UpperCAmelCase = None		,		_UpperCAmelCase = True		,		_UpperCAmelCase = 1 / 255		,		_UpperCAmelCase = True		,		_UpperCAmelCase = None		,		_UpperCAmelCase = None		,		**_UpperCAmelCase		,		)      ->			None:
     super().__init__(**_UpperCAmelCase			)
     UpperCamelCase_						=  size if size is not None else {'shortest_edge': 224}
     UpperCamelCase_						=  get_size_dict(_UpperCAmelCase		,		default_to_square=_UpperCAmelCase			)
     UpperCamelCase_						=  crop_size if crop_size is not None else {'height': 224, 'width': 224}
     UpperCamelCase_						=  get_size_dict(_UpperCAmelCase		,		param_name='crop_size'			)

     UpperCamelCase_						=  do_resize
     UpperCamelCase_						=  size
     UpperCamelCase_						=  do_center_crop
     UpperCamelCase_						=  crop_size
     UpperCamelCase_						=  resample
     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  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase = PILImageResampling.BILINEAR		,		_UpperCAmelCase = None		,		**_UpperCAmelCase		,		)      ->			np.ndarray:
     UpperCamelCase_						=  get_size_dict(_UpperCAmelCase		,		default_to_square=_UpperCAmelCase			)
     if "shortest_edge" in size:
        UpperCamelCase_						=  get_resize_output_image_size(_UpperCAmelCase		,		size['shortest_edge']		,		default_to_square=_UpperCAmelCase			)
     elif "height" in size and "width" in size:
        UpperCamelCase_						=  (size['height'], size['width'])
     else:
        raise ValueError(f"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"""			)
     return resize(_UpperCAmelCase		,		size=_UpperCAmelCase		,		resample=_UpperCAmelCase		,		data_format=_UpperCAmelCase		,		**_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase = None		,		**_UpperCAmelCase		,		)      ->			np.ndarray:
     UpperCamelCase_						=  get_size_dict(_UpperCAmelCase			)
     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(_UpperCAmelCase		,		size=(size['height'], size['width'])		,		data_format=_UpperCAmelCase		,		**_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase = None		,		**_UpperCAmelCase		,		)      ->			int:
     return rescale(_UpperCAmelCase		,		scale=_UpperCAmelCase		,		data_format=_UpperCAmelCase		,		**_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase = None		,		**_UpperCAmelCase		,		)      ->			np.ndarray:
     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 = ChannelDimension.FIRST		,		)      ->			np.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_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.
     UpperCamelCase_						=  to_numpy_array(_UpperCAmelCase			)

     if do_resize:
        UpperCamelCase_						=  self.resize(image=_UpperCAmelCase		,		size=_UpperCAmelCase		,		resample=_UpperCAmelCase			)

     if do_center_crop:
        UpperCamelCase_						=  self.center_crop(_UpperCAmelCase		,		size=_UpperCAmelCase			)

     if do_rescale:
        UpperCamelCase_						=  self.rescale(image=_UpperCAmelCase		,		scale=_UpperCAmelCase			)

     if do_normalize:
        UpperCamelCase_						=  self.normalize(image=_UpperCAmelCase		,		mean=_UpperCAmelCase		,		std=_UpperCAmelCase			)

     UpperCamelCase_						=  to_channel_dimension_format(_UpperCAmelCase		,		_UpperCAmelCase			)
     return image




  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		,		)      ->			PIL.Image.Image:
     UpperCamelCase_						=  do_resize if do_resize is not None else self.do_resize
     UpperCamelCase_						=  resample if resample is not None else self.resample
     UpperCamelCase_						=  do_center_crop if do_center_crop is not None else self.do_center_crop
     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_						=  size if size is not None else self.size
     UpperCamelCase_						=  get_size_dict(_UpperCAmelCase		,		default_to_square=_UpperCAmelCase			)
     UpperCamelCase_						=  crop_size if crop_size is not None else self.crop_size
     UpperCamelCase_						=  get_size_dict(_UpperCAmelCase		,		param_name='crop_size'			)

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

     UpperCamelCase_						=  make_batched(_UpperCAmelCase			)

     UpperCamelCase_						=  [
         [
             self._preprocess_image(
                 image=_UpperCAmelCase		,		do_resize=_UpperCAmelCase		,		size=_UpperCAmelCase		,		resample=_UpperCAmelCase		,		do_center_crop=_UpperCAmelCase		,		crop_size=_UpperCAmelCase		,		do_rescale=_UpperCAmelCase		,		rescale_factor=_UpperCAmelCase		,		do_normalize=_UpperCAmelCase		,		image_mean=_UpperCAmelCase		,		image_std=_UpperCAmelCase		,		data_format=_UpperCAmelCase		,		)
             for img in video
         ]
         for video in videos
     ]

     UpperCamelCase_						=  {'pixel_values': videos}
     return BatchFeature(data=_UpperCAmelCase		,		tensor_type=_UpperCAmelCase			)





