



"""simple docstring"""






from dataclasses import dataclass
from typing import Optional, Tuple

import torch
from torch import nn
from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel
from transformers.utils import ModelOutput

@dataclass
class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							None
					_a             =							None
					_a             =							None
					_a             =							None





class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_       ):


					'''simple docstring'''





					def __init__(	self							:  str,  lowerCamelCase							:  str=1,  lowerCamelCase							:  Any=0,  lowerCamelCase							:  Optional[int]=2,  lowerCamelCase							:  Tuple=512,  lowerCamelCase							:  List[str]="cls",  lowerCamelCase							:  List[Any]=False,  lowerCamelCase							:  Dict=True,  **lowerCamelCase							:  Union[str, Any],  )-> Tuple:
								super().__init__(pad_token_id=lowerCamelCase,  bos_token_id=lowerCamelCase,  eos_token_id=lowerCamelCase,  **lowerCamelCase					)
								lowerCamelCase__		:		List[Any]		     =project_dim
								lowerCamelCase__		:		Union[str, Any]		     =pooler_fn
								lowerCamelCase__		:		Optional[int]		     =learn_encoder
								lowerCamelCase__		:		Union[str, Any]		     =use_attention_mask





class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							[r'pooler', r'logit_scale']
					_a             =							[r'position_ids', r'predictions.decoder.bias']
					_a             =							'roberta'
					_a             =							RobertaSeriesConfig



					def __init__(	self							:  int,  lowerCamelCase							:  Tuple					)-> Dict:
								super().__init__(lowerCamelCase					)
								lowerCamelCase__		:		Any		     =XLMRobertaModel(lowerCamelCase					)
								lowerCamelCase__		:		Dict		     =nn.Linear(config.hidden_size,  config.project_dim					)
								lowerCamelCase__		:		List[Any]		     =getattr(lowerCamelCase,  '''has_pre_transformation''',  lowerCamelCase					)
								if self.has_pre_transformation:
											lowerCamelCase__		:		int		     =nn.Linear(config.hidden_size,  config.project_dim					)
											lowerCamelCase__		:		List[Any]		     =nn.LayerNorm(config.hidden_size,  eps=config.layer_norm_eps					)
								self.post_init()




					def 		snake_case	(	self							:  Any,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[torch.Tensor] = None,  lowerCamelCase							:  Optional[bool] = None,  lowerCamelCase							:  Optional[bool] = None,  lowerCamelCase							:  Optional[bool] = None,  )-> List[Any]:
								lowerCamelCase__		:		List[Any]		     =return_dict if return_dict is not None else self.config.use_return_dict

								lowerCamelCase__		:		Tuple		     =self.base_model(
								    input_ids=lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase,  position_ids=lowerCamelCase,  head_mask=lowerCamelCase,  inputs_embeds=lowerCamelCase,  encoder_hidden_states=lowerCamelCase,  encoder_attention_mask=lowerCamelCase,  output_attentions=lowerCamelCase,  output_hidden_states=True if self.has_pre_transformation else output_hidden_states,  return_dict=lowerCamelCase,  )

								if self.has_pre_transformation:
											lowerCamelCase__		:		Optional[Any]		     =outputs['''hidden_states'''][-2]
											lowerCamelCase__		:		Optional[Any]		     =self.pre_LN(lowerCamelCase					)
											lowerCamelCase__		:		List[str]		     =self.transformation_pre(lowerCamelCase					)

											return TransformationModelOutput(
											    projection_state=lowerCamelCase,  last_hidden_state=outputs.last_hidden_state,  hidden_states=outputs.hidden_states,  attentions=outputs.attentions,  )
								else:
											lowerCamelCase__		:		Union[str, Any]		     =self.transformation(outputs.last_hidden_state					)
											return TransformationModelOutput(
											    projection_state=lowerCamelCase,  last_hidden_state=outputs.last_hidden_state,  hidden_states=outputs.hidden_states,  attentions=outputs.attentions,  )









"""simple docstring"""






import numpy as np
from PIL import Image






def 					snake_case__       (		__lowerCamelCase    :   np.ndarray							,		__lowerCamelCase    :   int							,		__lowerCamelCase    :   int     ):
			"""simple docstring"""
			lowerCamelCase__		:		List[Any]		     =np.array(__lowerCamelCase     )
			if arr.shape[0] != arr.shape[1]:
						raise ValueError('''The input array is not a square matrix'''     )
			lowerCamelCase__		:		int		     =0
			lowerCamelCase__		:		int		     =0
			lowerCamelCase__		:		Optional[int]		     =0
			lowerCamelCase__		:		List[Any]		     =0

			# compute the shape of the output matrix
			lowerCamelCase__		:		Union[str, Any]		     =(arr.shape[0] - size) // stride + 1
			# initialize the output matrix with zeros of shape maxpool_shape
			lowerCamelCase__		:		Union[str, Any]		     =np.zeros((maxpool_shape, maxpool_shape)     )

			while i < arr.shape[0]:
						if i + size > arr.shape[0]:
									# if the end of the matrix is reached, break
									break
						while j < arr.shape[1]:
									# if the end of the matrix is reached, break
									if j + size > arr.shape[1]:
												break
									# compute the maximum of the pooling matrix
									lowerCamelCase__		:		str		     =np.max(arr[i : i + size, j : j + size]     )
									# shift the pooling matrix by stride of column pixels
									j += stride
									mat_j += 1

						# shift the pooling matrix by stride of row pixels
						i += stride
						mat_i += 1

						# reset the column index to 0
						lowerCamelCase__		:		Optional[int]		     =0
						lowerCamelCase__		:		Optional[int]		     =0

			return updated_arr






def 					snake_case__       (		__lowerCamelCase    :   np.ndarray							,		__lowerCamelCase    :   int							,		__lowerCamelCase    :   int     ):
			"""simple docstring"""
			lowerCamelCase__		:		List[Any]		     =np.array(__lowerCamelCase     )
			if arr.shape[0] != arr.shape[1]:
						raise ValueError('''The input array is not a square matrix'''     )
			lowerCamelCase__		:		str		     =0
			lowerCamelCase__		:		List[Any]		     =0
			lowerCamelCase__		:		Optional[int]		     =0
			lowerCamelCase__		:		List[Any]		     =0

			# compute the shape of the output matrix
			lowerCamelCase__		:		Dict		     =(arr.shape[0] - size) // stride + 1
			# initialize the output matrix with zeros of shape avgpool_shape
			lowerCamelCase__		:		Optional[Any]		     =np.zeros((avgpool_shape, avgpool_shape)     )

			while i < arr.shape[0]:
						# if the end of the matrix is reached, break
						if i + size > arr.shape[0]:
									break
						while j < arr.shape[1]:
									# if the end of the matrix is reached, break
									if j + size > arr.shape[1]:
												break
									# compute the average of the pooling matrix
									lowerCamelCase__		:		Optional[int]		     =int(np.average(arr[i : i + size, j : j + size]     )     )
									# shift the pooling matrix by stride of column pixels
									j += stride
									mat_j += 1

						# shift the pooling matrix by stride of row pixels
						i += stride
						mat_i += 1
						# reset the column index to 0
						lowerCamelCase__		:		Optional[Any]		     =0
						lowerCamelCase__		:		int		     =0

			return updated_arr


# Main Function
if __name__ == "__main__":
		from doctest import testmod

		testmod(name="avgpooling", verbose=True)

		# Loading the image
		_lowercase			:			int										=						Image.open("path_to_image")

		# Converting the image to numpy array and maxpooling, displaying the result
		# Ensure that the image is a square matrix

		Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

		# Converting the image to numpy array and averagepooling, displaying the result
		# Ensure that the image is a square matrix

		Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()




