


"""simple docstring"""






from __future__ import absolute_import, division, print_function, unicode_literals

from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers import RobertaConfig
from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.roberta.modeling_roberta import (
    ROBERTA_INPUTS_DOCSTRING,
    ROBERTA_START_DOCSTRING,
    RobertaEmbeddings,
)

from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy





@add_start_docstrings(
    """The RoBERTa Model transformer with early exiting (DeeRoBERTa). """     ,    A     ,    )
class      lowerCamelCase__   (     A				):







					"""simple docstring"""


					__a	       =							RobertaConfig
					__a	       =							"""roberta"""






					def __init__(       self    : int ,       UpperCamelCase    : Union[str, Any]  ):
												'''simple docstring'''






												super().__init__(UpperCamelCase  )

												__UpperCAmelCase   :						List[Any]      							=					RobertaEmbeddings(UpperCamelCase  )
												self.init_weights()






@add_start_docstrings(
    """RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,
    also takes care of multi-layer training. """     ,    A     ,    )
class      lowerCamelCase__   (     A				):







					"""simple docstring"""


					__a	       =							RobertaConfig
					__a	       =							"""roberta"""






					def __init__(       self    : Any ,       UpperCamelCase    : Dict  ):
												'''simple docstring'''






												super().__init__(UpperCamelCase  )
												__UpperCAmelCase   :						str      							=					config.num_labels
												__UpperCAmelCase   :						List[str]      							=					config.num_hidden_layers

												__UpperCAmelCase   :						Dict      							=					DeeRobertaModel(UpperCamelCase  )
												__UpperCAmelCase   :						List[str]      							=					nn.Dropout(config.hidden_dropout_prob  )
												__UpperCAmelCase   :						Optional[int]      							=					nn.Linear(config.hidden_size ,       self.config.num_labels  )






					@add_start_docstrings_to_model_forward(UpperCamelCase  )
					def lowerCamelCase__							(       self    : List[Any] ,       UpperCamelCase    : Tuple=None ,       UpperCamelCase    : Optional[Any]=None ,       UpperCamelCase    : Dict=None ,       UpperCamelCase    : List[str]=None ,       UpperCamelCase    : Any=None ,       UpperCamelCase    : Tuple=None ,       UpperCamelCase    : Dict=None ,       UpperCamelCase    : List[Any]=-1 ,       UpperCamelCase    : Union[str, Any]=False ,       ):
												'''simple docstring'''






												__UpperCAmelCase   :						Dict      							=					self.num_layers
												try:
																			__UpperCAmelCase   :						Optional[Any]      							=					self.roberta(
																			    UpperCamelCase ,       attention_mask=UpperCamelCase ,       token_type_ids=UpperCamelCase ,       position_ids=UpperCamelCase ,       head_mask=UpperCamelCase ,       inputs_embeds=UpperCamelCase ,       )

																			__UpperCAmelCase   :						Optional[int]      							=					outputs[1]

																			__UpperCAmelCase   :						Optional[int]      							=					self.dropout(UpperCamelCase  )
																			__UpperCAmelCase   :						Dict      							=					self.classifier(UpperCamelCase  )
																			__UpperCAmelCase   :						int      							=					(logits,) + outputs[2:]  # add hidden states and attention if they are here
												except HighwayException as e:
																			__UpperCAmelCase   :						Union[str, Any]      							=					e.message
																			__UpperCAmelCase   :						Optional[int]      							=					e.exit_layer
																			__UpperCAmelCase   :						Tuple      							=					outputs[0]

												if not self.training:
																			__UpperCAmelCase   :						Union[str, Any]      							=					entropy(UpperCamelCase  )
																			__UpperCAmelCase   :						Dict      							=					[]
																			__UpperCAmelCase   :						Optional[int]      							=					[]
												if labels is not None:
																			if self.num_labels == 1:
																										#  We are doing regression
																										__UpperCAmelCase   :						Optional[int]      							=					MSELoss()
																										__UpperCAmelCase   :						str      							=					loss_fct(logits.view(-1  ) ,       labels.view(-1  )  )
																			else:
																										__UpperCAmelCase   :						str      							=					CrossEntropyLoss()
																										__UpperCAmelCase   :						List[Any]      							=					loss_fct(logits.view(-1 ,       self.num_labels  ) ,       labels.view(-1  )  )

																			# work with highway exits
																			__UpperCAmelCase   :						Any      							=					[]
																			for highway_exit in outputs[-1]:
																										__UpperCAmelCase   :						Optional[Any]      							=					highway_exit[0]
																										if not self.training:
																																	highway_logits_all.append(UpperCamelCase  )
																																	highway_entropy.append(highway_exit[2]  )
																										if self.num_labels == 1:
																																	#  We are doing regression
																																	__UpperCAmelCase   :						Dict      							=					MSELoss()
																																	__UpperCAmelCase   :						Any      							=					loss_fct(highway_logits.view(-1  ) ,       labels.view(-1  )  )
																										else:
																																	__UpperCAmelCase   :						List[Any]      							=					CrossEntropyLoss()
																																	__UpperCAmelCase   :						Optional[int]      							=					loss_fct(highway_logits.view(-1 ,       self.num_labels  ) ,       labels.view(-1  )  )
																										highway_losses.append(UpperCamelCase  )

																			if train_highway:
																										__UpperCAmelCase   :						Optional[int]      							=					(sum(highway_losses[:-1]  ),) + outputs
																										# exclude the final highway, of course
																			else:
																										__UpperCAmelCase   :						Optional[int]      							=					(loss,) + outputs
												if not self.training:
																			__UpperCAmelCase   :						Union[str, Any]      							=					outputs + ((original_entropy, highway_entropy), exit_layer)
																			if output_layer >= 0:
																										__UpperCAmelCase   :						Optional[int]      							=					(
																										    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
																										)  # use the highway of the last layer

												return outputs  # (loss), logits, (hidden_states), (attentions), entropy




"""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 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


UpperCAmelCase    :     Optional[Any]   						=		logging.get_logger(__name__)
def      lowerCamelCase       (     _UpperCamelCase		:      Any       ,			_UpperCamelCase		:      Optional[Any]							)		->       int:

							'''simple docstring'''



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

							'''simple docstring'''



							__UpperCAmelCase   :						Tuple      							=					x.reshape(-1       ,			3							)
							__UpperCAmelCase   :						Optional[Any]      							=					squared_euclidean_distance(_UpperCamelCase       ,			_UpperCamelCase							)
							return np.argmin(_UpperCamelCase       ,			axis=1							)





class      lowerCamelCase__   (     A				):







					"""simple docstring"""


					__a	       =							["""pixel_values"""]






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






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






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






												__UpperCAmelCase   :						Any      							=					get_size_dict(UpperCamelCase  )
												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(
												    UpperCamelCase ,       size=(size["""height"""], size["""width"""]) ,       resample=UpperCamelCase ,       data_format=UpperCamelCase ,       **UpperCamelCase  )






					def lowerCamelCase__							(       self    : Dict ,       UpperCamelCase    : np.ndarray ,       UpperCamelCase    : Optional[Union[str, ChannelDimension]] = None ,       ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[Any]      							=					rescale(image=UpperCamelCase ,       scale=1 / 127.5 ,       data_format=UpperCamelCase  )
												__UpperCAmelCase   :						Optional[Any]      							=					image - 1
												return image






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






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

												__UpperCAmelCase   :						int      							=					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 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   :						Tuple      							=					[to_numpy_array(UpperCamelCase  ) for image in images]

												if do_resize:
																			__UpperCAmelCase   :						Dict      							=					[self.resize(image=UpperCamelCase ,       size=UpperCamelCase ,       resample=UpperCamelCase  ) for image in images]

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

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

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

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

												__UpperCAmelCase   :						List[str]      							=					{"""input_ids""": images}
												return BatchFeature(data=UpperCamelCase ,       tensor_type=UpperCamelCase  )
