"""simple docstring"""




import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import BertGenerationDecoder, BertGenerationEncoder
class     UpperCamelCase__:





						def __init__(       self		,__UpperCAmelCase		,__UpperCAmelCase=13		,__UpperCAmelCase=7		,__UpperCAmelCase=True		,__UpperCAmelCase=True		,__UpperCAmelCase=99		,__UpperCAmelCase=32		,__UpperCAmelCase=5		,__UpperCAmelCase=4		,__UpperCAmelCase=37		,__UpperCAmelCase="gelu"		,__UpperCAmelCase=0.1		,__UpperCAmelCase=0.1		,__UpperCAmelCase=50		,__UpperCAmelCase=0.0_2		,__UpperCAmelCase=True		,__UpperCAmelCase=None		,)					->				List[Any]:
													A__	     =							parent
													A__	     =							batch_size
													A__	     =							seq_length
													A__	     =							is_training
													A__	     =							use_input_mask
													A__	     =							vocab_size
													A__	     =							hidden_size
													A__	     =							num_hidden_layers
													A__	     =							num_attention_heads
													A__	     =							intermediate_size
													A__	     =							hidden_act
													A__	     =							hidden_dropout_prob
													A__	     =							attention_probs_dropout_prob
													A__	     =							max_position_embeddings
													A__	     =							initializer_range
													A__	     =							use_labels
													A__	     =							scope





						def 				snake_case__	(       self     )					->				str:
													A__	     =							ids_tensor([self.batch_size, self.seq_length]		,self.vocab_size     )

													A__	     =							None
													if self.use_input_mask:
																				A__	     =							random_attention_mask([self.batch_size, self.seq_length]     )

													if self.use_labels:
																				A__	     =							ids_tensor([self.batch_size, self.seq_length]		,self.vocab_size     )

													A__	     =							self.get_config()

													return config, input_ids, input_mask, token_labels





						def 				snake_case__	(       self     )					->				Optional[Any]:
													return BertGenerationConfig(
													    vocab_size=self.vocab_size		,hidden_size=self.hidden_size		,num_hidden_layers=self.num_hidden_layers		,num_attention_heads=self.num_attention_heads		,intermediate_size=self.intermediate_size		,hidden_act=self.hidden_act		,hidden_dropout_prob=self.hidden_dropout_prob		,attention_probs_dropout_prob=self.attention_probs_dropout_prob		,max_position_embeddings=self.max_position_embeddings		,is_decoder=__UpperCAmelCase		,initializer_range=self.initializer_range		,)





						def 				snake_case__	(       self     )					->				List[str]:
													(
													    (
													    A__
													)     ,  (
													    A__
													)     ,  (
													    A__
													)     ,  (
													    A__
													)     ,  
													)	     =							self.prepare_config_and_inputs()

													A__	     =							True
													A__	     =							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
													A__	     =							ids_tensor([self.batch_size, self.seq_length]		,vocab_size=2     )

													return (
													    config,
													    input_ids,
													    input_mask,
													    token_labels,
													    encoder_hidden_states,
													    encoder_attention_mask,
													)





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,**__UpperCAmelCase		,)					->				Optional[Any]:
													A__	     =							BertGenerationEncoder(config=__UpperCAmelCase     )
													model.to(__UpperCAmelCase     )
													model.eval()
													A__	     =							model(__UpperCAmelCase		,attention_mask=__UpperCAmelCase     )
													A__	     =							model(__UpperCAmelCase     )
													self.parent.assertEqual(result.last_hidden_state.shape		,(self.batch_size, self.seq_length, self.hidden_size)     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,**__UpperCAmelCase		,)					->				Union[str, Any]:
													A__	     =							True
													A__	     =							BertGenerationEncoder(config=__UpperCAmelCase     )
													model.to(__UpperCAmelCase     )
													model.eval()
													A__	     =							model(
													    __UpperCAmelCase		,attention_mask=__UpperCAmelCase		,encoder_hidden_states=__UpperCAmelCase		,encoder_attention_mask=__UpperCAmelCase		,)
													A__	     =							model(
													    __UpperCAmelCase		,attention_mask=__UpperCAmelCase		,encoder_hidden_states=__UpperCAmelCase		,)
													self.parent.assertEqual(result.last_hidden_state.shape		,(self.batch_size, self.seq_length, self.hidden_size)     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,**__UpperCAmelCase		,)					->				List[str]:
													A__	     =							True
													A__	     =							True
													A__	     =							BertGenerationDecoder(config=__UpperCAmelCase     ).to(__UpperCAmelCase     ).eval()

													# first forward pass
													A__	     =							model(
													    __UpperCAmelCase		,attention_mask=__UpperCAmelCase		,encoder_hidden_states=__UpperCAmelCase		,encoder_attention_mask=__UpperCAmelCase		,use_cache=__UpperCAmelCase		,)
													A__	     =							outputs.past_key_values

													# create hypothetical multiple next token and extent to next_input_ids
													A__	     =							ids_tensor((self.batch_size, 3)		,config.vocab_size     )
													A__	     =							ids_tensor((self.batch_size, 3)		,vocab_size=2     )

													# append to next input_ids and
													A__	     =							torch.cat([input_ids, next_tokens]		,dim=-1     )
													A__	     =							torch.cat([input_mask, next_mask]		,dim=-1     )

													A__	     =							model(
													    __UpperCAmelCase		,attention_mask=__UpperCAmelCase		,encoder_hidden_states=__UpperCAmelCase		,encoder_attention_mask=__UpperCAmelCase		,output_hidden_states=__UpperCAmelCase		,)['hidden_states'][0]
													A__	     =							model(
													    __UpperCAmelCase		,attention_mask=__UpperCAmelCase		,encoder_hidden_states=__UpperCAmelCase		,encoder_attention_mask=__UpperCAmelCase		,past_key_values=__UpperCAmelCase		,output_hidden_states=__UpperCAmelCase		,)['hidden_states'][0]

													# select random slice
													A__	     =							ids_tensor((1,)		,output_from_past.shape[-1]     ).item()
													A__	     =							output_from_no_past[:, -3:, random_slice_idx].detach()
													A__	     =							output_from_past[:, :, random_slice_idx].detach()

													self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]     )

													# test that outputs are equal for slice
													self.parent.assertTrue(torch.allclose(__UpperCAmelCase		,__UpperCAmelCase		,atol=1e-3     )     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,*__UpperCAmelCase		,)					->				Union[str, Any]:
													A__	     =							BertGenerationDecoder(__UpperCAmelCase     )
													model.to(__UpperCAmelCase     )
													model.eval()
													A__	     =							model(__UpperCAmelCase		,attention_mask=__UpperCAmelCase		,labels=__UpperCAmelCase     )
													self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.seq_length, self.vocab_size)     )





						def 				snake_case__	(       self     )					->				int:
													A__     ,  A__     ,  A__     ,  A__	     =							self.prepare_config_and_inputs()
													A__	     =							{'input_ids': input_ids, 'attention_mask': input_mask}
													return config, inputs_dict






@require_torch
class     UpperCamelCase__(			__A		,						__A		,						__A		,						unittest.TestCase ):
						lowerCAmelCase__  :						List[str]													=		(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
						lowerCAmelCase__  :						Dict													=		(BertGenerationDecoder,) if is_torch_available() else ()
						lowerCAmelCase__  :						List[str]													=		(
						    {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
						    if is_torch_available()
						    else {}
						)





						def 				snake_case__	(       self     )					->				Tuple:
													A__	     =							BertGenerationEncoderTester(self     )
													A__	     =							ConfigTester(self		,config_class=__UpperCAmelCase		,hidden_size=37     )





						def 				snake_case__	(       self     )					->				Optional[Any]:
													self.config_tester.run_common_tests()





						def 				snake_case__	(       self     )					->				int:
													A__	     =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__UpperCAmelCase     )





						def 				snake_case__	(       self     )					->				Any:
													A__     ,  A__     ,  A__     ,  A__	     =							self.model_tester.prepare_config_and_inputs()
													A__	     =							'bert'
													self.model_tester.create_and_check_model(__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase     )





						def 				snake_case__	(       self     )					->				Union[str, Any]:
													A__	     =							self.model_tester.prepare_config_and_inputs_for_decoder()
													self.model_tester.create_and_check_model_as_decoder(*__UpperCAmelCase     )





						def 				snake_case__	(       self     )					->				str:
													A__	     =							self.model_tester.prepare_config_and_inputs_for_decoder()
													self.model_tester.create_and_check_decoder_model_past_large_inputs(*__UpperCAmelCase     )





						def 				snake_case__	(       self     )					->				Any:
													# This regression test was failing with PyTorch < 1.3
													(
													    (
													    A__
													)     ,  (
													    A__
													)     ,  (
													    A__
													)     ,  (
													    A__
													)     ,  (
													    A__
													)     ,  (
													    A__
													)     ,  
													)	     =							self.model_tester.prepare_config_and_inputs_for_decoder()

													A__	     =							None

													self.model_tester.create_and_check_model_as_decoder(
													    __UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,)





						def 				snake_case__	(       self     )					->				Dict:
													A__	     =							self.model_tester.prepare_config_and_inputs_for_decoder()
													self.model_tester.create_and_check_for_causal_lm(*__UpperCAmelCase     )





						@slow
						def 				snake_case__	(       self     )					->				Union[str, Any]:
													A__	     =							BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'     )
													self.assertIsNotNone(__UpperCAmelCase     )






@require_torch
class     UpperCamelCase__(			unittest.TestCase ):





						@slow
						def 				snake_case__	(       self     )					->				Dict:
													A__	     =							BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'     )
													A__	     =							torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]     )
													with torch.no_grad():
																				A__	     =							model(__UpperCAmelCase     )[0]
													A__	     =							torch.Size([1, 8, 10_24]     )
													self.assertEqual(output.shape		,__UpperCAmelCase     )
													A__	     =							torch.tensor(
													    [[[0.1_7_7_5, 0.0_0_8_3, -0.0_3_2_1], [1.6_0_0_2, 0.1_2_8_7, 0.3_9_1_2], [2.1_4_7_3, 0.5_7_9_1, 0.6_0_6_6]]]     )
													self.assertTrue(torch.allclose(output[:, :3, :3]		,__UpperCAmelCase		,atol=1e-4     )     )






@require_torch
class     UpperCamelCase__(			unittest.TestCase ):





						@slow
						def 				snake_case__	(       self     )					->				Dict:
													A__	     =							BertGenerationDecoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'     )
													A__	     =							torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]     )
													with torch.no_grad():
																				A__	     =							model(__UpperCAmelCase     )[0]
													A__	     =							torch.Size([1, 8, 5_03_58]     )
													self.assertEqual(output.shape		,__UpperCAmelCase     )
													A__	     =							torch.tensor(
													    [[[-0.5_7_8_8, -2.5_9_9_4, -3.7_0_5_4], [0.0_4_3_8, 4.7_9_9_7, 1.8_7_9_5], [1.5_8_6_2, 6.6_4_0_9, 4.4_6_3_8]]]     )
													self.assertTrue(torch.allclose(output[:, :3, :3]		,__UpperCAmelCase		,atol=1e-4     )     )






"""simple docstring"""




import numpy as np
from PIL import Image






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							np.array(UpperCamelCase__				)
							if arr.shape[0] != arr.shape[1]:
														raise ValueError('The input array is not a square matrix'				)
							A__	     =							0
							A__	     =							0
							A__	     =							0
							A__	     =							0

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

							return updated_arr






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							np.array(UpperCamelCase__				)
							if arr.shape[0] != arr.shape[1]:
														raise ValueError('The input array is not a square matrix'				)
							A__	     =							0
							A__	     =							0
							A__	     =							0
							A__	     =							0

							# compute the shape of the output matrix
							A__	     =							(arr.shape[0] - size) // stride + 1
							# initialize the output matrix with zeros of shape avgpool_shape
							A__	     =							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
																					A__	     =							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
														A__	     =							0
														A__	     =							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()





