


'''simple docstring'''







import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
       import tensorflow as tf

       from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments
@require_tf
class 						UpperCamelCase__       (			unittest.TestCase						):



     """simple docstring"""






     def a						(			self   ,						snake_case__ ):

            '''simple docstring'''

            for model_result in results.values():
                   for batch_size, sequence_length in zip(model_result['bs']   ,						model_result['ss'] ):
                          _lowerCAmelCase     :				List[Any]       =							model_result['result'][batch_size][sequence_length]
                          self.assertIsNotNone(snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[Any]       =							'sshleifer/tiny-gpt2'
            _lowerCAmelCase     :				str       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						eager_mode=snake_case__   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				int       =							TensorFlowBenchmark(snake_case__ )
            _lowerCAmelCase     :				Any       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_inference_result )
            self.check_results_dict_not_empty(results.memory_inference_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							'sgugger/tiny-distilbert-classification'
            _lowerCAmelCase     :				List[str]       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						multi_process=snake_case__   ,						only_pretrain_model=snake_case__   ,						)
            _lowerCAmelCase     :				str       =							TensorFlowBenchmark(snake_case__ )
            _lowerCAmelCase     :				int       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_inference_result )
            self.check_results_dict_not_empty(results.memory_inference_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							'sshleifer/tiny-gpt2'
            _lowerCAmelCase     :				List[Any]       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				List[str]       =							TensorFlowBenchmark(snake_case__ )
            _lowerCAmelCase     :				Tuple       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_inference_result )
            self.check_results_dict_not_empty(results.memory_inference_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							'sshleifer/tiny-gpt2'
            _lowerCAmelCase     :				int       =							AutoConfig.from_pretrained(snake_case__ )
            _lowerCAmelCase     :				List[str]       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						eager_mode=snake_case__   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				Any       =							TensorFlowBenchmark(snake_case__   ,						[config] )
            _lowerCAmelCase     :				Optional[Any]       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_inference_result )
            self.check_results_dict_not_empty(results.memory_inference_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Union[str, Any]       =							'sshleifer/tiny-gpt2'
            _lowerCAmelCase     :				Any       =							AutoConfig.from_pretrained(snake_case__ )
            _lowerCAmelCase     :				List[str]       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				Dict       =							TensorFlowBenchmark(snake_case__   ,						[config] )
            _lowerCAmelCase     :				Dict       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_inference_result )
            self.check_results_dict_not_empty(results.memory_inference_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Any       =							'sshleifer/tiny-gpt2'
            _lowerCAmelCase     :				Dict       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				int       =							TensorFlowBenchmark(snake_case__ )
            _lowerCAmelCase     :				int       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_train_result )
            self.check_results_dict_not_empty(results.memory_train_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							'sshleifer/tiny-gpt2'
            _lowerCAmelCase     :				str       =							AutoConfig.from_pretrained(snake_case__ )
            _lowerCAmelCase     :				Union[str, Any]       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				int       =							TensorFlowBenchmark(snake_case__   ,						[config] )
            _lowerCAmelCase     :				Union[str, Any]       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_train_result )
            self.check_results_dict_not_empty(results.memory_train_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							'patrickvonplaten/t5-tiny-random'
            _lowerCAmelCase     :				Union[str, Any]       =							AutoConfig.from_pretrained(snake_case__ )
            _lowerCAmelCase     :				Union[str, Any]       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				List[str]       =							TensorFlowBenchmark(snake_case__   ,						configs=[config] )
            _lowerCAmelCase     :				Union[str, Any]       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_inference_result )
            self.check_results_dict_not_empty(results.memory_inference_result )







     @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU' ) ) == 0   ,						'Cannot do xla on CPU.' )
     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							'sshleifer/tiny-gpt2'
            _lowerCAmelCase     :				Dict       =							TensorFlowBenchmarkArguments(
                models=[MODEL_ID]   ,						training=snake_case__   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						use_xla=snake_case__   ,						multi_process=snake_case__   ,						)
            _lowerCAmelCase     :				str       =							TensorFlowBenchmark(snake_case__ )
            _lowerCAmelCase     :				Tuple       =							benchmark.run()
            self.check_results_dict_not_empty(results.time_inference_result )
            self.check_results_dict_not_empty(results.memory_inference_result )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							'sshleifer/tiny-gpt2'
            with tempfile.TemporaryDirectory() as tmp_dir:
                   _lowerCAmelCase     :				Tuple       =							TensorFlowBenchmarkArguments(
                       models=[MODEL_ID]   ,						inference=snake_case__   ,						save_to_csv=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						inference_time_csv_file=os.path.join(snake_case__   ,						'inf_time.csv' )   ,						inference_memory_csv_file=os.path.join(snake_case__   ,						'inf_mem.csv' )   ,						env_info_csv_file=os.path.join(snake_case__   ,						'env.csv' )   ,						multi_process=snake_case__   ,						)
                   _lowerCAmelCase     :				List[Any]       =							TensorFlowBenchmark(snake_case__ )
                   benchmark.run()
                   self.assertTrue(Path(os.path.join(snake_case__   ,						'inf_time.csv' ) ).exists() )
                   self.assertTrue(Path(os.path.join(snake_case__   ,						'inf_mem.csv' ) ).exists() )
                   self.assertTrue(Path(os.path.join(snake_case__   ,						'env.csv' ) ).exists() )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				int       =							'sshleifer/tiny-gpt2'

            def _check_summary_is_not_empty(snake_case__ ):
                   self.assertTrue(hasattr(snake_case__   ,						'sequential' ) )
                   self.assertTrue(hasattr(snake_case__   ,						'cumulative' ) )
                   self.assertTrue(hasattr(snake_case__   ,						'current' ) )
                   self.assertTrue(hasattr(snake_case__   ,						'total' ) )

            with tempfile.TemporaryDirectory() as tmp_dir:
                   _lowerCAmelCase     :				Optional[int]       =							TensorFlowBenchmarkArguments(
                       models=[MODEL_ID]   ,						inference=snake_case__   ,						sequence_lengths=[8]   ,						batch_sizes=[1]   ,						log_filename=os.path.join(snake_case__   ,						'log.txt' )   ,						log_print=snake_case__   ,						trace_memory_line_by_line=snake_case__   ,						eager_mode=snake_case__   ,						multi_process=snake_case__   ,						)
                   _lowerCAmelCase     :				Tuple       =							TensorFlowBenchmark(snake_case__ )
                   _lowerCAmelCase     :				Optional[Any]       =							benchmark.run()
                   _check_summary_is_not_empty(result.inference_summary )
                   self.assertTrue(Path(os.path.join(snake_case__   ,						'log.txt' ) ).exists() )









import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

    from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
    _SCREAMING_SNAKE_CASE   =  False

if is_vision_available():
    from PIL import Image

    from transformers import PixaStructImageProcessor






class 					SCREAMING_SNAKE_CASE_  (       unittest.TestCase ):
       """simple docstring"""


       def __init__(				self	:str,      snake_case	:int,      snake_case	:Dict=7,      snake_case	:List[Any]=3,      snake_case	:int=18,      snake_case	:List[str]=30,      snake_case	:str=400,      snake_case	:int=None,      snake_case	:List[Any]=True,      snake_case	:Optional[int]=True,      snake_case	:Union[str, Any]=None,      ):



          """simple docstring"""

          _lowercase              =size if size is not None else {'height': 20, 'width': 20}
          _lowercase              =parent
          _lowercase              =batch_size
          _lowercase              =num_channels
          _lowercase              =image_size
          _lowercase              =min_resolution
          _lowercase              =max_resolution
          _lowercase              =size
          _lowercase              =do_normalize
          _lowercase              =do_convert_rgb
          _lowercase              =[512, 1024, 2048, 4096]
          _lowercase              =patch_size if patch_size is not None else {'height': 16, 'width': 16}





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              ='https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
          _lowercase              =Image.open(requests.get(snake_case,      stream=snake_case).raw).convert('RGB')
          return raw_image






@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11  ,						reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''  ,						)
@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		Tuple					=PixaStructImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          _lowercase              =PixaStructImageProcessingTester(self)





       @property
       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.image_processor_tester.prepare_dummy_image()

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          _lowercase              =2048

          _lowercase              =image_processor(snake_case,      return_tensors='pt',      max_patches=snake_case)
          self.assertTrue(torch.allclose(inputs.flattened_patches.mean(),      torch.tensor(0.0_6_0_6),      atol=1e-3,      rtol=1e-3))





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )





       def 				UpperCamelCase__						(				self	:Union[str, Any]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          _lowercase              =True

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             with self.assertRaises(snake_case):
                _lowercase              =image_processor(
                    image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches

             _lowercase              ='Hello'

             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case,      header_text=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case,      header_text=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case,      numpify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      np.ndarray)

          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case,      torchify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      torch.Tensor)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )






@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11  ,						reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''  ,						)
@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		Optional[Any]					=PixaStructImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =PixaStructImageProcessingTester(self,      num_channels=4)
          _lowercase              =3





       @property
       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:Optional[Any]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * (self.image_processor_tester.num_channels - 1)
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )


