



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):







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

														for model_result in results.values():
																					for batch_size, sequence_length in zip(model_result["bs"],			model_result["ss"]):
																												_lowerCAmelCase     :							Tuple    =    model_result["result"][batch_size][sequence_length]
																												self.assertIsNotNone(__a)







							def    snake_case__      (   self):

														'''simple docstring'''

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







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    "sgugger/tiny-distilbert-classification"
														_lowerCAmelCase     :							Union[str, Any]    =    TensorFlowBenchmarkArguments(
														    models=[MODEL_ID],			training=__a,			inference=__a,			sequence_lengths=[8],			batch_sizes=[1],			multi_process=__a,			only_pretrain_model=__a,			)
														_lowerCAmelCase     :							Tuple    =    TensorFlowBenchmark(__a)
														_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    snake_case__      (   self):

														'''simple docstring'''

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







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    "sshleifer/tiny-gpt2"
														_lowerCAmelCase     :							Dict    =    AutoConfig.from_pretrained(__a)
														_lowerCAmelCase     :							Tuple    =    TensorFlowBenchmarkArguments(
														    models=[MODEL_ID],			training=__a,			inference=__a,			sequence_lengths=[8],			batch_sizes=[1],			eager_mode=__a,			multi_process=__a,			)
														_lowerCAmelCase     :							Any    =    TensorFlowBenchmark(__a,			[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    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    "sshleifer/tiny-gpt2"
														_lowerCAmelCase     :							int    =    AutoConfig.from_pretrained(__a)
														_lowerCAmelCase     :							List[Any]    =    TensorFlowBenchmarkArguments(
														    models=[MODEL_ID],			training=__a,			inference=__a,			sequence_lengths=[8],			batch_sizes=[1],			multi_process=__a,			)
														_lowerCAmelCase     :							Any    =    TensorFlowBenchmark(__a,			[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    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    "sshleifer/tiny-gpt2"
														_lowerCAmelCase     :							List[Any]    =    TensorFlowBenchmarkArguments(
														    models=[MODEL_ID],			training=__a,			inference=__a,			sequence_lengths=[8],			batch_sizes=[1],			multi_process=__a,			)
														_lowerCAmelCase     :							List[Any]    =    TensorFlowBenchmark(__a)
														_lowerCAmelCase     :							List[str]    =    benchmark.run()
														self.check_results_dict_not_empty(results.time_train_result)
														self.check_results_dict_not_empty(results.memory_train_result)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    "sshleifer/tiny-gpt2"
														_lowerCAmelCase     :							Optional[Any]    =    AutoConfig.from_pretrained(__a)
														_lowerCAmelCase     :							Dict    =    TensorFlowBenchmarkArguments(
														    models=[MODEL_ID],			training=__a,			inference=__a,			sequence_lengths=[8],			batch_sizes=[1],			multi_process=__a,			)
														_lowerCAmelCase     :							str    =    TensorFlowBenchmark(__a,			[config])
														_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    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    "patrickvonplaten/t5-tiny-random"
														_lowerCAmelCase     :							List[str]    =    AutoConfig.from_pretrained(__a)
														_lowerCAmelCase     :							Tuple    =    TensorFlowBenchmarkArguments(
														    models=[MODEL_ID],			training=__a,			inference=__a,			sequence_lengths=[8],			batch_sizes=[1],			multi_process=__a,			)
														_lowerCAmelCase     :							Optional[Any]    =    TensorFlowBenchmark(__a,			configs=[config])
														_lowerCAmelCase     :							str    =    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    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    "sshleifer/tiny-gpt2"
														_lowerCAmelCase     :							str    =    TensorFlowBenchmarkArguments(
														    models=[MODEL_ID],			training=__a,			inference=__a,			sequence_lengths=[8],			batch_sizes=[1],			use_xla=__a,			multi_process=__a,			)
														_lowerCAmelCase     :							Tuple    =    TensorFlowBenchmark(__a)
														_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    snake_case__      (   self):

														'''simple docstring'''

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







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    "sshleifer/tiny-gpt2"

														def _check_summary_is_not_empty(__a):
																					self.assertTrue(hasattr(__a,			"sequential"))
																					self.assertTrue(hasattr(__a,			"cumulative"))
																					self.assertTrue(hasattr(__a,			"current"))
																					self.assertTrue(hasattr(__a,			"total"))

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





import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoImageProcessor, ViTImageProcessor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / "utils"))

from test_module.custom_image_processing import CustomImageProcessor  # noqa E402


_snake_case		=  get_tests_dir("fixtures")



class   UpperCAmelCase_    (       unittest.TestCase):







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    mock.Mock()
														_lowerCAmelCase     :							int    =    500
														_lowerCAmelCase     :							Tuple    =    {}
														_lowerCAmelCase     :							str    =    HTTPError
														_lowerCAmelCase     :							Union[str, Any]    =    {}

														# Download this model to make sure it's in the cache.
														_lowerCAmelCase     :							Tuple    =    ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit")
														# Under the mock environment we get a 500 error when trying to reach the model.
														with mock.patch("requests.Session.request",			return_value=__a) as mock_head:
																					_lowerCAmelCase     :							Optional[int]    =    ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit")
																					# This check we did call the fake head request
																					mock_head.assert_called()







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    ViTImageProcessor.from_pretrained(
														    "https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json")







							def    snake_case__      (   self):

														'''simple docstring'''

														with self.assertRaises(__a):
																					# config is in subfolder, the following should not work without specifying the subfolder
																					_lowerCAmelCase     :							int    =    AutoImageProcessor.from_pretrained("hf-internal-testing/stable-diffusion-all-variants")

														_lowerCAmelCase     :							Optional[Any]    =    AutoImageProcessor.from_pretrained(
														    "hf-internal-testing/stable-diffusion-all-variants",			subfolder="feature_extractor")

														self.assertIsNotNone(__a)



@is_staging_test
class   UpperCAmelCase_    (       unittest.TestCase):







							@classmethod
							def    snake_case__      (   cls):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    TOKEN
														HfFolder.save_token(__a)







							@classmethod
							def    snake_case__      (   cls):

														'''simple docstring'''

														try:
																					delete_repo(token=cls._token,			repo_id="test-image-processor")
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token,			repo_id="valid_org/test-image-processor-org")
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token,			repo_id="test-dynamic-image-processor")
														except HTTPError:
																					pass







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    ViTImageProcessor.from_pretrained(__a)
														image_processor.push_to_hub("test-image-processor",			use_auth_token=self._token)

														_lowerCAmelCase     :							str    =    ViTImageProcessor.from_pretrained(f"{USER}/test-image-processor")
														for k, v in image_processor.__dict__.items():
																					self.assertEqual(__a,			getattr(__a,			__a))

														# Reset repo
														delete_repo(token=self._token,			repo_id="test-image-processor")

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					image_processor.save_pretrained(
																					    __a,			repo_id="test-image-processor",			push_to_hub=__a,			use_auth_token=self._token)

														_lowerCAmelCase     :							Optional[int]    =    ViTImageProcessor.from_pretrained(f"{USER}/test-image-processor")
														for k, v in image_processor.__dict__.items():
																					self.assertEqual(__a,			getattr(__a,			__a))







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Any    =    ViTImageProcessor.from_pretrained(__a)
														image_processor.push_to_hub("valid_org/test-image-processor",			use_auth_token=self._token)

														_lowerCAmelCase     :							Tuple    =    ViTImageProcessor.from_pretrained("valid_org/test-image-processor")
														for k, v in image_processor.__dict__.items():
																					self.assertEqual(__a,			getattr(__a,			__a))

														# Reset repo
														delete_repo(token=self._token,			repo_id="valid_org/test-image-processor")

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					image_processor.save_pretrained(
																					    __a,			repo_id="valid_org/test-image-processor-org",			push_to_hub=__a,			use_auth_token=self._token)

														_lowerCAmelCase     :							Optional[int]    =    ViTImageProcessor.from_pretrained("valid_org/test-image-processor-org")
														for k, v in image_processor.__dict__.items():
																					self.assertEqual(__a,			getattr(__a,			__a))







							def    snake_case__      (   self):

														'''simple docstring'''

														CustomImageProcessor.register_for_auto_class()
														_lowerCAmelCase     :							List[str]    =    CustomImageProcessor.from_pretrained(__a)

														image_processor.push_to_hub("test-dynamic-image-processor",			use_auth_token=self._token)

														# This has added the proper auto_map field to the config
														self.assertDictEqual(
														    image_processor.auto_map,			{"AutoImageProcessor": "custom_image_processing.CustomImageProcessor"},			)

														_lowerCAmelCase     :							Tuple    =    AutoImageProcessor.from_pretrained(
														    f"{USER}/test-dynamic-image-processor",			trust_remote_code=__a)
														# Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module
														self.assertEqual(new_image_processor.__class__.__name__,			"CustomImageProcessor")
