import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available
def 							lowerCAmelCase(     *__lowerCamelCase						):
					if not isinstance(__lowerCamelCase				,    __lowerCamelCase						):
										__a		 =       list(__lowerCamelCase						)
					for i in range(len(__lowerCamelCase						)						):
										__a		 =       None
					gc.collect()
					if is_xpu_available():
										torch.xpu.empty_cache()
					elif is_npu_available():
										torch.npu.empty_cache()
					else:
										torch.cuda.empty_cache()
					return objects
def 							lowerCAmelCase(     __lowerCamelCase						):
					__a		 =       [
					    'CUDA out of memory.',  # CUDA OOM
					    'cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.',  # CUDNN SNAFU
					    'DefaultCPUAllocator: can\'t allocate memory',  # CPU OOM
					]
					if isinstance(__lowerCamelCase				,    __lowerCamelCase						) and len(exception.args						) == 1:
										return any(err in exception.args[0] for err in _statements						)
					return False



def 							lowerCAmelCase(     __lowerCamelCase = None				,    __lowerCamelCase = 128						):
					if function is None:
										return functools.partial(__lowerCamelCase				,    starting_batch_size=__lowerCamelCase						)

					__a		 =       starting_batch_size

					def decorator(*__lowerCamelCase				,    **__lowerCamelCase						):
										nonlocal batch_size
										gc.collect()
										if is_xpu_available():
															torch.xpu.empty_cache()
										elif is_npu_available():
															torch.npu.empty_cache()
										else:
															torch.cuda.empty_cache()
										__a		 =       list(inspect.signature(__lowerCamelCase						).parameters.keys()						)
										# Guard against user error
										if len(__lowerCamelCase						) < (len(__lowerCamelCase						) + 1):
															__a		 =       ', '.join([f'''{arg}={value}''' for arg, value in zip(params[1:]				,    args[1:]						)]						)
															raise TypeError(
															    f'''Batch size was passed into `{function.__name__}` as the first argument when called.'''
															    f'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`'''						)
										while True:
															if batch_size == 0:
																				raise RuntimeError('No executable batch size found, reached zero.'						)
															try:
																				return function(__lowerCamelCase				,    *__lowerCamelCase				,    **__lowerCamelCase						)
															except Exception as e:
																				if should_reduce_batch_size(__lowerCamelCase						):
																									gc.collect()
																									if is_xpu_available():
																														torch.xpu.empty_cache()
																									elif is_npu_available():
																														torch.npu.empty_cache()
																									else:
																														torch.cuda.empty_cache()
																									batch_size //= 2
																				else:
																									raise

					return decorator





import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs


class 						a__							(       __snake_case		):



			def __init__(						self				,						UpperCAmelCase				,						UpperCAmelCase=None				,						UpperCAmelCase=True				,						UpperCAmelCase=None				,						**UpperCAmelCase   )			->   List[Any]:
								__a		 =       parent
								__a		 =       config_class
								__a		 =       has_text_modality
								__a		 =       kwargs
								__a		 =       common_properties



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								__a		 =       self.config_class(**self.inputs_dict   )
								__a		 =       (
								    ['hidden_size', 'num_attention_heads', 'num_hidden_layers']
								    if self.common_properties is None
								    else self.common_properties
								)

								# Add common fields for text models
								if self.has_text_modality:
													common_properties.extend(['vocab_size']   )

								# Test that config has the common properties as getters
								for prop in common_properties:
													self.parent.assertTrue(hasattr(UpperCAmelCase				,						UpperCAmelCase   )				,						msg=f'''`{prop}` does not exist'''   )

								# Test that config has the common properties as setter
								for idx, name in enumerate(UpperCAmelCase   ):
													try:
																		setattr(UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )
																		self.parent.assertEqual(
																		    getattr(UpperCAmelCase				,						UpperCAmelCase   )				,						UpperCAmelCase				,						msg=f'''`{name} value {idx} expected, but was {getattr(UpperCAmelCase				,						UpperCAmelCase   )}'''   )
													except NotImplementedError:
																		# Some models might not be able to implement setters for common_properties
																		# In that case, a NotImplementedError is raised
																		pass

        # Test if config class can be called with Config(prop_name=..)
								for idx, name in enumerate(UpperCAmelCase   ):
													try:
																		__a		 =       self.config_class(**{name: idx}   )
																		self.parent.assertEqual(
																		    getattr(UpperCAmelCase				,						UpperCAmelCase   )				,						UpperCAmelCase				,						msg=f'''`{name} value {idx} expected, but was {getattr(UpperCAmelCase				,						UpperCAmelCase   )}'''   )
													except NotImplementedError:
																		# Some models might not be able to implement setters for common_properties
																		# In that case, a NotImplementedError is raised
																		pass



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )
								__a		 =       json.loads(config.to_json_string()   )
								for key, value in self.inputs_dict.items():
													self.parent.assertEqual(obj[key]				,						UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
													__a		 =       os.path.join(UpperCAmelCase				,						'config.json'   )
													config_first.to_json_file(UpperCAmelCase   )
													__a		 =       self.config_class.from_json_file(UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
													config_first.save_pretrained(UpperCAmelCase   )
													__a		 =       self.config_class.from_pretrained(UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       self.config_class(**self.inputs_dict   )

								__a		 =       'test'
								with tempfile.TemporaryDirectory() as tmpdirname:
													__a		 =       os.path.join(UpperCAmelCase				,						UpperCAmelCase   )
													config_first.save_pretrained(UpperCAmelCase   )
													__a		 =       self.config_class.from_pretrained(UpperCAmelCase				,						subfolder=UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Union[str, Any]:
								__a		 =       self.config_class(**self.inputs_dict				,						num_labels=5   )
								self.parent.assertEqual(len(config.idalabel   )				,						5   )
								self.parent.assertEqual(len(config.labelaid   )				,						5   )

								__a		 =       3
								self.parent.assertEqual(len(config.idalabel   )				,						3   )
								self.parent.assertEqual(len(config.labelaid   )				,						3   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								if self.config_class.is_composition:
													return
								__a		 =       self.config_class()
								self.parent.assertIsNotNone(UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Union[str, Any]:
								__a		 =       copy.deepcopy(UpperCAmelCase   )
								__a		 =       self.config_class(**UpperCAmelCase   )
								__a		 =       []
								for key, value in config_common_kwargs.items():
													if key == "torch_dtype":
																		if not is_torch_available():
																							continue
																		else:
																							import torch

																							if config.torch_dtype != torch.floataa:
																												wrong_values.append(('torch_dtype', config.torch_dtype, torch.floataa)   )
													elif getattr(UpperCAmelCase				,						UpperCAmelCase   ) != value:
																		wrong_values.append((key, getattr(UpperCAmelCase				,						UpperCAmelCase   ), value)   )

								if len(UpperCAmelCase   ) > 0:
													__a		 =       '\n'.join([f'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values]   )
													raise ValueError(f'''The following keys were not properly set in the config:\n{errors}'''   )







			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								self.create_and_test_config_common_properties()
								self.create_and_test_config_to_json_string()
								self.create_and_test_config_to_json_file()
								self.create_and_test_config_from_and_save_pretrained()
								self.create_and_test_config_from_and_save_pretrained_subfolder()
								self.create_and_test_config_with_num_labels()
								self.check_config_can_be_init_without_params()
								self.check_config_arguments_init()




