






'''simple docstring'''




from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
		from ... import FeatureExtractionMixin, TensorType

lowerCAmelCase__          =						logging.get_logger(__name__)

lowerCAmelCase__          =						{
    'openai/imagegpt-small': '',
    'openai/imagegpt-medium': '',
    'openai/imagegpt-large': '',
}







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''imagegpt'''
	_lowerCamelCase              =  ['''past_key_values''']
	_lowerCamelCase              =  {
	    '''hidden_size''': '''n_embd''',
	    '''max_position_embeddings''': '''n_positions''',
	    '''num_attention_heads''': '''n_head''',
	    '''num_hidden_layers''': '''n_layer''',
	}

	def __init__(      self   :						Tuple  ,					UpperCAmelCase_   :						Dict=512 + 1  ,					UpperCAmelCase_   :						int=32 * 32  ,					UpperCAmelCase_   :						Optional[Any]=512  ,					UpperCAmelCase_   :						List[Any]=24  ,					UpperCAmelCase_   :						Any=8  ,					UpperCAmelCase_   :						int=None  ,					UpperCAmelCase_   :						List[str]="quick_gelu"  ,					UpperCAmelCase_   :						Union[str, Any]=0.1  ,					UpperCAmelCase_   :						Optional[Any]=0.1  ,					UpperCAmelCase_   :						Union[str, Any]=0.1  ,					UpperCAmelCase_   :						Optional[int]=1e-5  ,					UpperCAmelCase_   :						Dict=0.02  ,					UpperCAmelCase_   :						List[str]=True  ,					UpperCAmelCase_   :						List[str]=True  ,					UpperCAmelCase_   :						List[Any]=False  ,					UpperCAmelCase_   :						List[Any]=False  ,					UpperCAmelCase_   :						Any=False  ,					**UpperCAmelCase_   :						List[str]  ,					):
			UpperCamelCase__				:					int					=					vocab_size
			UpperCamelCase__				:					int					=					n_positions
			UpperCamelCase__				:					List[str]					=					n_embd
			UpperCamelCase__				:					Tuple					=					n_layer
			UpperCamelCase__				:					Dict					=					n_head
			UpperCamelCase__				:					Any					=					n_inner
			UpperCamelCase__				:					Union[str, Any]					=					activation_function
			UpperCamelCase__				:					Tuple					=					resid_pdrop
			UpperCamelCase__				:					str					=					embd_pdrop
			UpperCamelCase__				:					List[Any]					=					attn_pdrop
			UpperCamelCase__				:					str					=					layer_norm_epsilon
			UpperCamelCase__				:					Optional[int]					=					initializer_range
			UpperCamelCase__				:					Dict					=					scale_attn_weights
			UpperCamelCase__				:					Dict					=					use_cache
			UpperCamelCase__				:					Union[str, Any]					=					scale_attn_by_inverse_layer_idx
			UpperCamelCase__				:					int					=					reorder_and_upcast_attn
			UpperCamelCase__				:					List[Any]					=					tie_word_embeddings

			super().__init__(tie_word_embeddings=UpperCAmelCase_  ,					**UpperCAmelCase_)







class       __lowercase					(__lowerCamelCase    ):

	@property
	def      __UpperCamelCase							(      self   :						Optional[Any]):
			return OrderedDict(
			    [
			        ('input_ids', {0: 'batch', 1: 'sequence'}),
			    ])





	def      __UpperCamelCase							(      self   :						List[Any]  ,					UpperCAmelCase_   :						"FeatureExtractionMixin"  ,					UpperCAmelCase_   :						int = 1  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						bool = False  ,					UpperCAmelCase_   :						Optional["TensorType"] = None  ,					UpperCAmelCase_   :						int = 3  ,					UpperCAmelCase_   :						int = 32  ,					UpperCAmelCase_   :						int = 32  ,					):
			UpperCamelCase__				:					str					=					self._generate_dummy_images(UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)
			UpperCamelCase__				:					List[str]					=					dict(preprocessor(images=UpperCAmelCase_  ,					return_tensors=UpperCAmelCase_))

			return inputs












'''simple docstring'''




import numpy as np
from PIL import Image







def       __UpperCAmelCase						(  lowerCamelCase_     ,      lowerCamelCase_     ,      lowerCamelCase_)							->     np.ndarray:
		UpperCamelCase__				:					List[Any]					=					np.array(lowerCamelCase_)
		if arr.shape[0] != arr.shape[1]:
				raise ValueError('The input array is not a square matrix')
		UpperCamelCase__				:					Tuple					=					0
		UpperCamelCase__				:					int					=					0
		UpperCamelCase__				:					Optional[int]					=					0
		UpperCamelCase__				:					str					=					0

		# compute the shape of the output matrix
		UpperCamelCase__				:					int					=					(arr.shape[0] - size) // stride + 1
		# initialize the output matrix with zeros of shape maxpool_shape
		UpperCamelCase__				:					Dict					=					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
						UpperCamelCase__				:					Dict					=					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
				UpperCamelCase__				:					List[Any]					=					0
				UpperCamelCase__				:					Optional[int]					=					0

		return updated_arr







def       __UpperCAmelCase						(  lowerCamelCase_     ,      lowerCamelCase_     ,      lowerCamelCase_)							->     np.ndarray:
		UpperCamelCase__				:					Tuple					=					np.array(lowerCamelCase_)
		if arr.shape[0] != arr.shape[1]:
				raise ValueError('The input array is not a square matrix')
		UpperCamelCase__				:					Optional[int]					=					0
		UpperCamelCase__				:					int					=					0
		UpperCamelCase__				:					List[str]					=					0
		UpperCamelCase__				:					List[Any]					=					0

		# compute the shape of the output matrix
		UpperCamelCase__				:					str					=					(arr.shape[0] - size) // stride + 1
		# initialize the output matrix with zeros of shape avgpool_shape
		UpperCamelCase__				:					Union[str, 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
						UpperCamelCase__				:					List[Any]					=					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
				UpperCamelCase__				:					Union[str, Any]					=					0
				UpperCamelCase__				:					Optional[Any]					=					0

		return updated_arr


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

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

		# Loading the image
		lowerCAmelCase__          =						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()




