



'''simple docstring'''



import inspect
import unittest

from transformers import ResNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import ResNetBackbone, ResNetForImageClassification, ResNetModel
				from transformers.models.resnet.modeling_resnet import RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor






class 			_SCREAMING_SNAKE_CASE				:



			def __init__(     self		:						Optional[Any]    ,			__UpperCamelCase		:						Dict    ,			__UpperCamelCase		:						Any=3    ,			__UpperCamelCase		:						List[Any]=32    ,			__UpperCamelCase		:						Dict=3    ,			__UpperCamelCase		:						Optional[Any]=10    ,			__UpperCamelCase		:						List[Any]=[10, 20, 30, 40]    ,			__UpperCamelCase		:						Any=[1, 1, 2, 1]    ,			__UpperCamelCase		:						Optional[Any]=True    ,			__UpperCamelCase		:						str=True    ,			__UpperCamelCase		:						Tuple="relu"    ,			__UpperCamelCase		:						Dict=3    ,			__UpperCamelCase		:						Union[str, Any]=None    ,			__UpperCamelCase		:						List[str]=["stage2", "stage3", "stage4"]    ,			__UpperCamelCase		:						Any=[2, 3, 4]    ,			)      ->      List[Any]:
							"""simple docstring"""





							snake_case__      :   Union[str, Any]      =    parent
							snake_case__      :   str      =    batch_size
							snake_case__      :   Tuple      =    image_size
							snake_case__      :   List[str]      =    num_channels
							snake_case__      :   Dict      =    embeddings_size
							snake_case__      :   Optional[int]      =    hidden_sizes
							snake_case__      :   Dict      =    depths
							snake_case__      :   Optional[int]      =    is_training
							snake_case__      :   Any      =    use_labels
							snake_case__      :   List[Any]      =    hidden_act
							snake_case__      :   Union[str, Any]      =    num_labels
							snake_case__      :   Tuple      =    scope
							snake_case__      :   Optional[int]      =    len(__UpperCamelCase			)
							snake_case__      :   Any      =    out_features
							snake_case__      :   Optional[Any]      =    out_indices



			def 				lowerCAmelCase (     self		:						List[str]			)      ->      Union[str, Any]:
							"""simple docstring"""





							snake_case__      :   Tuple      =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

							snake_case__      :   Optional[Any]      =    None
							if self.use_labels:
											snake_case__      :   Any      =    ids_tensor([self.batch_size]    ,			self.num_labels			)

							snake_case__      :   str      =    self.get_config()

							return config, pixel_values, labels



			def 				lowerCAmelCase (     self		:						Any			)      ->      str:
							"""simple docstring"""





							return ResNetConfig(
							    num_channels=self.num_channels    ,			embeddings_size=self.embeddings_size    ,			hidden_sizes=self.hidden_sizes    ,			depths=self.depths    ,			hidden_act=self.hidden_act    ,			num_labels=self.num_labels    ,			out_features=self.out_features    ,			out_indices=self.out_indices    ,			)



			def 				lowerCAmelCase (     self		:						str    ,			__UpperCamelCase		:						Optional[Any]    ,			__UpperCamelCase		:						Any    ,			__UpperCamelCase		:						str			)      ->      Dict:
							"""simple docstring"""





							snake_case__      :   Optional[int]      =    ResNetModel(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							snake_case__      :   List[str]      =    model(__UpperCamelCase			)
							# expected last hidden states: B, C, H // 32, W // 32
							self.parent.assertEqual(
							    result.last_hidden_state.shape    ,			(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,			)



			def 				lowerCAmelCase (     self		:						Dict    ,			__UpperCamelCase		:						Optional[int]    ,			__UpperCamelCase		:						Optional[int]    ,			__UpperCamelCase		:						List[str]			)      ->      Optional[Any]:
							"""simple docstring"""





							snake_case__      :   Any      =    self.num_labels
							snake_case__      :   Optional[int]      =    ResNetForImageClassification(__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							snake_case__      :   int      =    model(__UpperCamelCase    ,			labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels)			)



			def 				lowerCAmelCase (     self		:						List[str]    ,			__UpperCamelCase		:						List[Any]    ,			__UpperCamelCase		:						str    ,			__UpperCamelCase		:						Any			)      ->      Tuple:
							"""simple docstring"""





							snake_case__      :   str      =    ResNetBackbone(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							snake_case__      :   List[str]      =    model(__UpperCamelCase			)

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps			)    ,			len(config.out_features			)			)
							self.parent.assertListEqual(list(result.feature_maps[0].shape			)    ,			[self.batch_size, self.hidden_sizes[1], 4, 4]			)

							# verify channels
							self.parent.assertEqual(len(model.channels			)    ,			len(config.out_features			)			)
							self.parent.assertListEqual(model.channels    ,			config.hidden_sizes[1:]			)

							# verify backbone works with out_features=None
							snake_case__      :   Optional[int]      =    None
							snake_case__      :   Tuple      =    ResNetBackbone(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							snake_case__      :   int      =    model(__UpperCamelCase			)

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps			)    ,			1			)
							self.parent.assertListEqual(list(result.feature_maps[0].shape			)    ,			[self.batch_size, self.hidden_sizes[-1], 1, 1]			)

							# verify channels
							self.parent.assertEqual(len(model.channels			)    ,			1			)
							self.parent.assertListEqual(model.channels    ,			[config.hidden_sizes[-1]]			)







			def 				lowerCAmelCase (     self		:						List[Any]			)      ->      str:
							"""simple docstring"""





							snake_case__      :   Tuple      =    self.prepare_config_and_inputs()
							snake_case__      :   Tuple      =    config_and_inputs
							snake_case__      :   List[Any]      =    {'''pixel_values''': pixel_values}
							return config, inputs_dict






@require_torch
class 			_SCREAMING_SNAKE_CASE				(lowerCAmelCase_,	lowerCAmelCase_,	unittest.TestCase      ):
			A__								=  (
			    (
			        ResNetModel,
			        ResNetForImageClassification,
			        ResNetBackbone,
			    )
			    if is_torch_available()
			    else ()
			)
			A__								=  (
			    {'feature-extraction': ResNetModel, 'image-classification': ResNetForImageClassification}
			    if is_torch_available()
			    else {}
			)

			A__								=  True
			A__								=  False
			A__								=  False
			A__								=  False
			A__								=  False



			def 				lowerCAmelCase (     self		:						Tuple			)      ->      str:
							"""simple docstring"""





							snake_case__      :   Dict      =    ResNetModelTester(self			)
							snake_case__      :   Tuple      =    ConfigTester(self    ,			config_class=__UpperCamelCase    ,			has_text_modality=__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						Dict			)      ->      Dict:
							"""simple docstring"""





							self.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()



			def 				lowerCAmelCase (     self		:						Any			)      ->      Tuple:
							"""simple docstring"""





							return



			@unittest.skip(reason='''ResNet does not use inputs_embeds'''			)
			def 				lowerCAmelCase (     self		:						Tuple			)      ->      Optional[int]:
							"""simple docstring"""





							pass



			@unittest.skip(reason='''ResNet does not support input and output embeddings'''			)
			def 				lowerCAmelCase (     self		:						Any			)      ->      Dict:
							"""simple docstring"""





							pass



			def 				lowerCAmelCase (     self		:						Tuple			)      ->      Dict:
							"""simple docstring"""





							snake_case__      :   Optional[int]      =    self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
											snake_case__      :   int      =    model_class(__UpperCamelCase			)
											snake_case__      :   Dict      =    inspect.signature(model.forward			)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											snake_case__      :   Any      =    [*signature.parameters.keys()]

											snake_case__      :   Union[str, Any]      =    ['''pixel_values''']
											self.assertListEqual(arg_names[:1]    ,			__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						str			)      ->      str:
							"""simple docstring"""





							snake_case__      :   str      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						Dict			)      ->      List[Any]:
							"""simple docstring"""





							snake_case__      :   Union[str, Any]      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						List[Any]			)      ->      Tuple:
							"""simple docstring"""





							snake_case__      :   Optional[int]      =    self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
											snake_case__      :   str      =    model_class(config=__UpperCamelCase			)
											for name, module in model.named_modules():
															if isinstance(__UpperCamelCase    ,			(nn.BatchNormad, nn.GroupNorm)			):
																			self.assertTrue(
																			    torch.all(module.weight == 1			)    ,			msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''    ,			)
																			self.assertTrue(
																			    torch.all(module.bias == 0			)    ,			msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''    ,			)



			def 				lowerCAmelCase (     self		:						List[str]			)      ->      Tuple:
							"""simple docstring"""





							def check_hidden_states_output(__UpperCamelCase		:						Tuple    ,			__UpperCamelCase		:						Any    ,			__UpperCamelCase		:						int			):
											snake_case__      :   List[str]      =    model_class(__UpperCamelCase			)
											model.to(__UpperCamelCase			)
											model.eval()

											with torch.no_grad():
															snake_case__      :   Optional[int]      =    model(**self._prepare_for_class(__UpperCamelCase    ,			__UpperCamelCase			)			)

											snake_case__      :   int      =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

											snake_case__      :   Any      =    self.model_tester.num_stages
											self.assertEqual(len(__UpperCamelCase			)    ,			expected_num_stages + 1			)

											# ResNet's feature maps are of shape (batch_size, num_channels, height, width)
											self.assertListEqual(
											    list(hidden_states[0].shape[-2:]			)    ,			[self.model_tester.image_size // 4, self.model_tester.image_size // 4]    ,			)

							snake_case__      :   Optional[Any]      =    self.model_tester.prepare_config_and_inputs_for_common()
							snake_case__      :   List[str]      =    ['''basic''', '''bottleneck''']
							for model_class in self.all_model_classes:
											for layer_type in layers_type:
															snake_case__      :   List[Any]      =    layer_type
															snake_case__      :   List[Any]      =    True
															check_hidden_states_output(__UpperCamelCase    ,			__UpperCamelCase    ,			__UpperCamelCase			)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															snake_case__      :   str      =    True

															check_hidden_states_output(__UpperCamelCase    ,			__UpperCamelCase    ,			__UpperCamelCase			)



			@unittest.skip(reason='''ResNet does not use feedforward chunking'''			)
			def 				lowerCAmelCase (     self		:						List[str]			)      ->      int:
							"""simple docstring"""





							pass



			def 				lowerCAmelCase (     self		:						Optional[Any]			)      ->      str:
							"""simple docstring"""





							snake_case__      :   Tuple      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase			)







			@slow
			def 				lowerCAmelCase (     self		:						int			)      ->      Optional[int]:
							"""simple docstring"""





							for model_name in RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case__      :   Dict      =    ResNetModel.from_pretrained(__UpperCamelCase			)
											self.assertIsNotNone(__UpperCamelCase			)



def 							__UpperCAmelCase				(				)		->						Union[str, Any]:
				snake_case__      :   str      =    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
				return image






@require_torch
@require_vision
class 			_SCREAMING_SNAKE_CASE				(unittest.TestCase      ):



			@cached_property
			def 				lowerCAmelCase (     self		:						Tuple			)      ->      Dict:
							"""simple docstring"""





							return (
							    AutoImageProcessor.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]			)
							    if is_vision_available()
							    else None
							)







			@slow
			def 				lowerCAmelCase (     self		:						List[Any]			)      ->      Optional[int]:
							"""simple docstring"""





							snake_case__      :   List[Any]      =    ResNetForImageClassification.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]			).to(__UpperCamelCase			)

							snake_case__      :   List[str]      =    self.default_image_processor
							snake_case__      :   Dict      =    prepare_img()
							snake_case__      :   Any      =    image_processor(images=__UpperCamelCase    ,			return_tensors='''pt'''			).to(__UpperCamelCase			)

							# forward pass
							with torch.no_grad():
											snake_case__      :   str      =    model(**__UpperCamelCase			)

							# verify the logits
							snake_case__      :   List[Any]      =    torch.Size((1, 1000)			)
							self.assertEqual(outputs.logits.shape    ,			__UpperCamelCase			)

							snake_case__      :   str      =    torch.tensor([-11.1069, -9.7877, -8.3777]			).to(__UpperCamelCase			)

							self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,			__UpperCamelCase    ,			atol=1e-4			)			)






@require_torch
class 			_SCREAMING_SNAKE_CASE				(lowerCAmelCase_,	unittest.TestCase      ):
			A__								=  (ResNetBackbone,) if is_torch_available() else ()
			A__								=  False
			A__								=  ResNetConfig



			def 				lowerCAmelCase (     self		:						Optional[Any]			)      ->      Union[str, Any]:
							"""simple docstring"""





							snake_case__      :   Union[str, Any]      =    ResNetModelTester(self			)









"""simple docstring"""






import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
		import torch

		from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
		import pyanvml.pyanvml as nvml


_lowercase					:					Optional[Any] 		=		logging.get_logger(__name__)

class 			__SCREAMING_SNAKE_CASE      ( lowerCAmelCase_  ):


					'''simple docstring'''





					_a       =				42
					_a       =				42
					_a       =				"PyTorch"



					@property
					def 						snake_case			(			self				:    Dict	)->   List[str]:
								return torch.__version__



					def 						snake_case			(			self				:    Optional[Any],    lowerCamelCase				:    str,    lowerCamelCase				:    int,    lowerCamelCase				:    int	)->   float:
								lowerCamelCase__						:						List[Any]						      =self._prepare_inference_func(lowerCamelCase,    lowerCamelCase,    lowerCamelCase	)
								return self._measure_speed(_inference	)



					def 						snake_case			(			self				:    List[str],    lowerCamelCase				:    str,    lowerCamelCase				:    int,    lowerCamelCase				:    int	)->   [Memory, Optional[MemorySummary]]:
								lowerCamelCase__						:						List[Any]						      =self._prepare_inference_func(lowerCamelCase,    lowerCamelCase,    lowerCamelCase	)
								return self._measure_memory(_inference	)



					def 						snake_case			(			self				:    Any,    lowerCamelCase				:    str,    lowerCamelCase				:    int,    lowerCamelCase				:    int	)->   float:
								lowerCamelCase__						:						int						      =self._prepare_train_func(lowerCamelCase,    lowerCamelCase,    lowerCamelCase	)
								return self._measure_speed(_train	)



					def 						snake_case			(			self				:    List[str],    lowerCamelCase				:    str,    lowerCamelCase				:    int,    lowerCamelCase				:    int	)->   [Memory, Optional[MemorySummary]]:
								lowerCamelCase__						:						str						      =self._prepare_train_func(lowerCamelCase,    lowerCamelCase,    lowerCamelCase	)
								return self._measure_memory(_train	)



					def 						snake_case			(			self				:    List[str],    lowerCamelCase				:    str,    lowerCamelCase				:    int,    lowerCamelCase				:    int	)->   Callable[[], None]:
								lowerCamelCase__						:						List[str]						      =self.config_dict[model_name]

								if self.args.torchscript:
											lowerCamelCase__						:						int						      =True

								lowerCamelCase__						:						Optional[Any]						      =(
								    hasattr(lowerCamelCase,    '''architectures'''	)
								    and isinstance(config.architectures,    lowerCamelCase	)
								    and len(config.architectures	) > 0
								)
								if not self.args.only_pretrain_model and has_model_class_in_config:
											try:
														lowerCamelCase__						:						int						      =config.architectures[0]
														lowerCamelCase__						:						Dict						      =__import__('''transformers''',    fromlist=[model_class]	)
														lowerCamelCase__						:						str						      =getattr(lowerCamelCase,    lowerCamelCase	)
														lowerCamelCase__						:						Tuple						      =model_cls(lowerCamelCase	)
											except ImportError:
														raise ImportError(
														    F'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
														    ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'''	)
								else:
											lowerCamelCase__						:						List[str]						      =MODEL_MAPPING[config.__class__](lowerCamelCase	)

								model.eval()
								model.to(self.args.device	)

								# encoder-decoder has vocab size saved differently
								lowerCamelCase__						:						Optional[Any]						      =config.vocab_size if hasattr(lowerCamelCase,    '''vocab_size'''	) else config.encoder.vocab_size
								lowerCamelCase__						:						int						      =torch.randint(lowerCamelCase,    (batch_size, sequence_length),    dtype=torch.long,    device=self.args.device	)

								if self.args.fpaa:
											logger.info('''Running training in Mixed Precision...'''	)
											if not self.args.is_gpu:
														raise ValueError('''Mixed precision is possible only for GPU.'''	)
											# amp seems to have memory leaks so that memory usage
											# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
											model.half()

								if self.args.torchscript:
											with torch.no_grad():
														lowerCamelCase__						:						Optional[Any]						      =torch.jit.trace(lowerCamelCase,    lowerCamelCase	)
								else:
											lowerCamelCase__						:						Tuple						      =model

								def encoder_decoder_forward():
											with torch.no_grad():
														lowerCamelCase__						:						Optional[Any]						      =inference_model(lowerCamelCase,    decoder_input_ids=lowerCamelCase	)
											return outputs

								def encoder_forward():
											with torch.no_grad():
														lowerCamelCase__						:						Any						      =inference_model(lowerCamelCase	)
											return outputs

								lowerCamelCase__						:						Optional[int]						      =encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
								return _forward



					def 						snake_case			(			self				:    Union[str, Any],    lowerCamelCase				:    str,    lowerCamelCase				:    int,    lowerCamelCase				:    int	)->   Callable[[], None]:
								lowerCamelCase__						:						Any						      =self.config_dict[model_name]

								lowerCamelCase__						:						Tuple						      =(
								    hasattr(lowerCamelCase,    '''architectures'''	)
								    and isinstance(config.architectures,    lowerCamelCase	)
								    and len(config.architectures	) > 0
								)
								if not self.args.only_pretrain_model and has_model_class_in_config:
											try:
														lowerCamelCase__						:						List[Any]						      =config.architectures[0]
														lowerCamelCase__						:						int						      =__import__('''transformers''',    fromlist=[model_class]	)
														lowerCamelCase__						:						int						      =getattr(lowerCamelCase,    lowerCamelCase	)
														lowerCamelCase__						:						Dict						      =model_cls(lowerCamelCase	)
											except ImportError:
														raise ImportError(
														    F'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
														    ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'''	)
								else:
											lowerCamelCase__						:						List[str]						      =MODEL_WITH_LM_HEAD_MAPPING[config.__class__](lowerCamelCase	)

								if self.args.torchscript:
											raise NotImplementedError('''Training for torchscript is currently not implemented'''	)
								else:
											lowerCamelCase__						:						str						      =model

								model.train()
								model.to(self.args.device	)

								# encoder-decoder has vocab size saved differently
								lowerCamelCase__						:						Dict						      =config.vocab_size if hasattr(lowerCamelCase,    '''vocab_size'''	) else config.encoder.vocab_size
								lowerCamelCase__						:						int						      =torch.randint(lowerCamelCase,    (batch_size, sequence_length),    dtype=torch.long,    device=self.args.device	)

								if self.args.fpaa:
											logger.info('''Running training in Mixed Precision...'''	)
											if not self.args.is_gpu:
														raise ValueError('''Mixed precision is possible only for GPU.'''	)

											# amp seems to have memory leaks so that memory usage
											# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
											model.half()

								def compute_loss_and_backprob_encoder():
											lowerCamelCase__						:						Optional[int]						      =train_model(lowerCamelCase,    labels=lowerCamelCase	)[0]
											loss.backward()
											return loss

								def compute_loss_and_backprob_encoder_decoder():
											lowerCamelCase__						:						Dict						      =train_model(lowerCamelCase,    decoder_input_ids=lowerCamelCase,    labels=lowerCamelCase	)[0]
											loss.backward()
											return loss

								lowerCamelCase__						:						Optional[Any]						      =(
								    compute_loss_and_backprob_encoder_decoder
								    if config.is_encoder_decoder
								    else compute_loss_and_backprob_encoder
								)
								return _train



					def 						snake_case			(			self				:    Optional[int],    lowerCamelCase				:    Optional[int]	)->   float:
								try:
											if self.args.is_tpu or self.args.torchscript:
														# run additional 10 times to stabilize compilation for tpu and torchscript
														logger.info('''Do inference on TPU or torchscript. Running model 5 times to stabilize compilation'''	)
														timeit.repeat(
														    lowerCamelCase,    repeat=1,    number=5,    )

											# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
											lowerCamelCase__						:						List[str]						      =timeit.repeat(
											    lowerCamelCase,    repeat=self.args.repeat,    number=10,    )

											if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
														import torch_xla.debug.metrics as met

														self.print_fn(met.metrics_report()	)

											return min(lowerCamelCase	) / 10.0
								except RuntimeError as e:
											self.print_fn(F'''Doesn\'t fit on GPU. {e}'''	)
											return "N/A"




					def 						snake_case			(			self				:    Optional[int],    lowerCamelCase				:    Callable[[], None]	)->   [Memory, MemorySummary]:
								try:
											if self.args.trace_memory_line_by_line:
														lowerCamelCase__						:						Optional[int]						      =start_memory_tracing('''transformers'''	)

											if self.args.is_tpu:
														# tpu
														raise NotImplementedError(
														    '''Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with'''
														    ''' `--no-memory` or `args.memory=False`'''	)
											elif self.args.is_gpu:
														if not is_pyanvml_available():
																	logger.warning(
																	    '''py3nvml not installed, we won\'t log GPU memory usage. '''
																	    '''Install py3nvml (pip install py3nvml) to log information about GPU.'''	)
																	lowerCamelCase__						:						str						      ='''N/A'''
														else:
																	logger.info(
																	    '''Measuring total GPU usage on GPU device. Make sure to not have additional processes running'''
																	    ''' on the same GPU.'''	)
																	# init nvml
																	nvml.nvmlInit()
																	func()
																	lowerCamelCase__						:						int						      =nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx	)
																	lowerCamelCase__						:						Optional[Any]						      =nvml.nvmlDeviceGetMemoryInfo(lowerCamelCase	)
																	lowerCamelCase__						:						str						      =meminfo.used
																	lowerCamelCase__						:						List[str]						      =Memory(lowerCamelCase	)
																	# shutdown nvml
																	nvml.nvmlShutdown()
											else:
														# cpu
														lowerCamelCase__						:						List[Any]						      =measure_peak_memory_cpu(lowerCamelCase	)
														lowerCamelCase__						:						List[Any]						      =Memory(lowerCamelCase	) if isinstance(lowerCamelCase,    lowerCamelCase	) else memory_bytes

											if self.args.trace_memory_line_by_line:
														lowerCamelCase__						:						List[str]						      =stop_memory_tracing(lowerCamelCase	)
											else:
														lowerCamelCase__						:						Optional[int]						      =None

											return memory, summary
								except RuntimeError as e:
											self.print_fn(F'''Doesn\'t fit on GPU. {e}'''	)
											return "N/A", None




