





import unittest
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory

from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline
from transformers.convert_graph_to_onnx import (
    convert,
    ensure_valid_input,
    generate_identified_filename,
    infer_shapes,
    quantize,
)
from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow





class A_					:




   def _lowerCAmelCase      (self	:Optional[int]    ,   _UpperCamelCase	:int    ,   _UpperCamelCase	:List[Any]    ,   _UpperCamelCase	:List[str] )->					Optional[int]:
         return None





class A_					:




   def _lowerCAmelCase      (self	:Tuple    ,   _UpperCamelCase	:Optional[Any]    ,   _UpperCamelCase	:Optional[int]    ,   _UpperCamelCase	:List[Any]    ,   _UpperCamelCase	:int )->					str:
         return None





class A_					(			unittest.TestCase   ):
   lowerCAmelCase__        = [
       # (model_name, model_kwargs)
       ("""bert-base-cased""", {}),
       ("""gpt2""", {"""use_cache""": False}),  # We don't support exporting GPT2 past keys anymore
   ]




   @require_tf
   @slow
   def _lowerCAmelCase      (self	:List[Any] )->					List[str]:
         for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               self._test_export(_UpperCamelCase    ,   '''tf'''    ,   12    ,   **_UpperCamelCase )




   @require_torch
   @slow
   def _lowerCAmelCase      (self	:str )->					Optional[int]:
         for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               self._test_export(_UpperCamelCase    ,   '''pt'''    ,   12    ,   **_UpperCamelCase )




   @require_torch
   @slow
   def _lowerCAmelCase      (self	:Optional[Any] )->					Optional[Any]:
         from transformers import BertModel

         __A          =				['''[UNK]''', '''[SEP]''', '''[CLS]''', '''[PAD]''', '''[MASK]''', '''some''', '''other''', '''words''']
         with NamedTemporaryFile(mode='''w+t''' ) as vocab_file:
               vocab_file.write('''\n'''.join(_UpperCamelCase ) )
               vocab_file.flush()
               __A          =				BertTokenizerFast(vocab_file.name )

         with TemporaryDirectory() as bert_save_dir:
               __A          =				BertModel(BertConfig(vocab_size=len(_UpperCamelCase ) ) )
               model.save_pretrained(_UpperCamelCase )
               self._test_export(_UpperCamelCase    ,   '''pt'''    ,   12    ,   _UpperCamelCase )




   @require_tf
   @slow
   def _lowerCAmelCase      (self	:Optional[int] )->					Tuple:
         for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               __A          =				self._test_export(_UpperCamelCase    ,   '''tf'''    ,   12    ,   **_UpperCamelCase )
               __A          =				quantize(Path(_UpperCamelCase ) )

               # Ensure the actual quantized model is not bigger than the original one
               if quantized_path.stat().st_size >= Path(_UpperCamelCase ).stat().st_size:
                     self.fail('''Quantized model is bigger than initial ONNX model''' )




   @require_torch
   @slow
   def _lowerCAmelCase      (self	:Union[str, Any] )->					Tuple:
         for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
               __A          =				self._test_export(_UpperCamelCase    ,   '''pt'''    ,   12    ,   **_UpperCamelCase )
               __A          =				quantize(_UpperCamelCase )

               # Ensure the actual quantized model is not bigger than the original one
               if quantized_path.stat().st_size >= Path(_UpperCamelCase ).stat().st_size:
                     self.fail('''Quantized model is bigger than initial ONNX model''' )




   def _lowerCAmelCase      (self	:Optional[int]    ,   _UpperCamelCase	:Union[str, Any]    ,   _UpperCamelCase	:Tuple    ,   _UpperCamelCase	:Union[str, Any]    ,   _UpperCamelCase	:Union[str, Any]=None    ,   **_UpperCamelCase	:str )->					Any:
         try:
               # Compute path
               with TemporaryDirectory() as tempdir:
                     __A          =				Path(_UpperCamelCase ).joinpath('''model.onnx''' )

               # Remove folder if exists
               if path.parent.exists():
                     path.parent.rmdir()

               # Export
               convert(_UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase    ,   **_UpperCamelCase )

               return path
         except Exception as e:
               self.fail(_UpperCamelCase )




   @require_torch
   @require_tokenizers
   @slow
   def _lowerCAmelCase      (self	:Dict )->					Optional[Any]:
         from transformers import BertModel

         __A          =				BertModel(BertConfig.from_pretrained('''lysandre/tiny-bert-random''' ) )
         __A          =				BertTokenizerFast.from_pretrained('''lysandre/tiny-bert-random''' )
         self._test_infer_dynamic_axis(_UpperCamelCase    ,   _UpperCamelCase    ,   '''pt''' )




   @require_tf
   @require_tokenizers
   @slow
   def _lowerCAmelCase      (self	:Optional[Any] )->					Union[str, Any]:
         from transformers import TFBertModel

         __A          =				TFBertModel(BertConfig.from_pretrained('''lysandre/tiny-bert-random''' ) )
         __A          =				BertTokenizerFast.from_pretrained('''lysandre/tiny-bert-random''' )
         self._test_infer_dynamic_axis(_UpperCamelCase    ,   _UpperCamelCase    ,   '''tf''' )




   def _lowerCAmelCase      (self	:Dict    ,   _UpperCamelCase	:str    ,   _UpperCamelCase	:List[str]    ,   _UpperCamelCase	:Any )->					Any:
         __A          =				FeatureExtractionPipeline(_UpperCamelCase    ,   _UpperCamelCase )

         __A          =				['''input_ids''', '''token_type_ids''', '''attention_mask''', '''output_0''', '''output_1''']
         __A   ,						__A   ,						__A   ,						__A          =				infer_shapes(_UpperCamelCase    ,   _UpperCamelCase )

         # Assert all variables are present
         self.assertEqual(len(_UpperCamelCase )    ,   len(_UpperCamelCase ) )
         self.assertTrue(all(var_name in shapes for var_name in variable_names ) )
         self.assertSequenceEqual(variable_names[:3]    ,   _UpperCamelCase )
         self.assertSequenceEqual(variable_names[3:]    ,   _UpperCamelCase )

         # Assert inputs are {0: batch, 1: sequence}
         for var_name in ["input_ids", "token_type_ids", "attention_mask"]:
               self.assertDictEqual(shapes[var_name]    ,   {0: '''batch''', 1: '''sequence'''} )

         # Assert outputs are {0: batch, 1: sequence} and {0: batch}
         self.assertDictEqual(shapes['''output_0''']    ,   {0: '''batch''', 1: '''sequence'''} )
         self.assertDictEqual(shapes['''output_1''']    ,   {0: '''batch'''} )




   def _lowerCAmelCase      (self	:Union[str, Any] )->					Optional[Any]:
         __A          =				['''input_ids''', '''attention_mask''', '''token_type_ids''']
         __A          =				{'''input_ids''': [1, 2, 3, 4], '''attention_mask''': [0, 0, 0, 0], '''token_type_ids''': [1, 1, 1, 1]}
         __A   ,						__A          =				ensure_valid_input(FuncContiguousArgs()    ,   _UpperCamelCase    ,   _UpperCamelCase )

         # Should have exactly the same number of args (all are valid)
         self.assertEqual(len(_UpperCamelCase )    ,   3 )

         # Should have exactly the same input names
         self.assertEqual(set(_UpperCamelCase )    ,   set(_UpperCamelCase ) )

         # Parameter should be reordered according to their respective place in the function:
         # (input_ids, token_type_ids, attention_mask)
         self.assertEqual(_UpperCamelCase    ,   (tokens['''input_ids'''], tokens['''token_type_ids'''], tokens['''attention_mask''']) )

         # Generated args are interleaved with another args (for instance parameter "past" in GPT2)
         __A   ,						__A          =				ensure_valid_input(FuncNonContiguousArgs()    ,   _UpperCamelCase    ,   _UpperCamelCase )

         # Should have exactly the one arg (all before the one not provided "some_other_args")
         self.assertEqual(len(_UpperCamelCase )    ,   1 )
         self.assertEqual(len(_UpperCamelCase )    ,   1 )

         # Should have only "input_ids"
         self.assertEqual(inputs_args[0]    ,   tokens['''input_ids'''] )
         self.assertEqual(ordered_input_names[0]    ,   '''input_ids''' )






   def _lowerCAmelCase      (self	:Optional[int] )->					List[Any]:
         __A          =				generate_identified_filename(Path('''/home/something/my_fake_model.onnx''' )    ,   '''-test''' )
         self.assertEqual('''/home/something/my_fake_model-test.onnx'''    ,   generated.as_posix() )












import numpy as np
from PIL import Image



def _a				(	lowerCamelCase:   np.ndarray					,				lowerCamelCase:   int					,				lowerCamelCase:   int )	->						np.ndarray:







      '''simple docstring'''






      __A          =				np.array(lowerCamelCase )
      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 _a				(	lowerCamelCase:   np.ndarray					,				lowerCamelCase:   int					,				lowerCamelCase:   int )	->						np.ndarray:







      '''simple docstring'''






      __A          =				np.array(lowerCamelCase )
      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
       snake_case__					:							Optional[Any]													=     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()





