



'''simple docstring'''

import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
							import torch

							from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin

class      __magic_name__						:







							def __init__(							self :	Dict     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	Any=None     ,_UpperCAmelCase :	Union[str, Any]=None     ,_UpperCAmelCase :	Union[str, Any]=None     ,_UpperCAmelCase :	Dict="resnet50"     ,_UpperCAmelCase :	Dict=3     ,_UpperCAmelCase :	Any=32     ,_UpperCAmelCase :	Optional[Any]=3     ,_UpperCAmelCase :	Optional[Any]=True     ,_UpperCAmelCase :	Any=True     ,):
									_a					:				Optional[int]				=							parent
									_a					:				int				=							out_indices if out_indices is not None else [4]
									_a					:				int				=							stage_names
									_a					:				Union[str, Any]				=							out_features
									_a					:				List[str]				=							backbone
									_a					:				Union[str, Any]				=							batch_size
									_a					:				Optional[Any]				=							image_size
									_a					:				int				=							num_channels
									_a					:				Optional[int]				=							use_pretrained_backbone
									_a					:				Optional[Any]				=							is_training







							def 							__lowercase      (							self :	Union[str, Any]		):
									_a					:				int				=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)
									_a					:				Optional[Any]				=							self.get_config()

									return config, pixel_values







							def 							__lowercase      (							self :	Tuple		):
									return TimmBackboneConfig(
									    image_size=self.image_size     ,num_channels=self.num_channels     ,out_features=self.out_features     ,out_indices=self.out_indices     ,stage_names=self.stage_names     ,use_pretrained_backbone=self.use_pretrained_backbone     ,backbone=self.backbone     ,)







							def 							__lowercase      (							self :	List[Any]     ,_UpperCAmelCase :	List[str]     ,_UpperCAmelCase :	Union[str, Any]		):
									_a					:				int				=							TimmBackbone(config=a_		)
									model.to(a_		)
									model.eval()
									with torch.no_grad():
											_a					:				Optional[Any]				=							model(a_		)
									self.parent.assertEqual(
									    result.feature_map[-1].shape     ,(self.batch_size, model.channels[-1], 14, 14)     ,)







							def 							__lowercase      (							self :	str		):
									_a					:				List[Any]				=							self.prepare_config_and_inputs()
									_a					:				Union[str, Any]				=							config_and_inputs
									_a					:				Union[str, Any]				=							{'''pixel_values''': pixel_values}
									return config, inputs_dict

@require_torch
@require_timm
class      __magic_name__						(   __snake_case      ,						__snake_case      ,						__snake_case      ,						unittest.TestCase       ):
							lowerCAmelCase	:   str           = (TimmBackbone,) if is_torch_available() else ()
							lowerCAmelCase	:   List[Any]           = {'feature-extraction': TimmBackbone} if is_torch_available() else {}
							lowerCAmelCase	:   List[str]           = False
							lowerCAmelCase	:   Optional[int]           = False
							lowerCAmelCase	:   int           = False
							lowerCAmelCase	:   Optional[Any]           = False







							def 							__lowercase      (							self :	Tuple		):
									_a					:				List[Any]				=							TimmBackboneModelTester(self		)
									_a					:				Optional[int]				=							ConfigTester(self     ,config_class=a_     ,has_text_modality=a_		)







							def 							__lowercase      (							self :	Union[str, Any]		):
									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 							__lowercase      (							self :	Optional[Any]		):
									_a					:				List[str]				=							'''resnet18'''
									_a					:				List[Any]				=							'''microsoft/resnet-18'''

									_a					:				List[str]				=							AutoBackbone.from_pretrained(a_     ,use_timm_backbone=a_		)
									_a					:				Optional[int]				=							AutoBackbone.from_pretrained(a_		)

									self.assertEqual(len(timm_model.out_features		)     ,len(transformers_model.out_features		)		)
									self.assertEqual(len(timm_model.stage_names		)     ,len(transformers_model.stage_names		)		)
									self.assertEqual(timm_model.channels     ,transformers_model.channels		)
									# Out indices are set to the last layer by default. For timm models, we don't know
									# the number of layers in advance, so we set it to (-1,), whereas for transformers
									# models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
									self.assertEqual(timm_model.out_indices     ,(-1,)		)
									self.assertEqual(transformers_model.out_indices     ,[len(timm_model.stage_names		) - 1]		)

									_a					:				List[Any]				=							AutoBackbone.from_pretrained(a_     ,use_timm_backbone=a_     ,out_indices=[1, 2, 3]		)
									_a					:				Tuple				=							AutoBackbone.from_pretrained(a_     ,out_indices=[1, 2, 3]		)

									self.assertEqual(timm_model.out_indices     ,transformers_model.out_indices		)
									self.assertEqual(len(timm_model.out_features		)     ,len(transformers_model.out_features		)		)
									self.assertEqual(timm_model.channels     ,transformers_model.channels		)







							@unittest.skip('TimmBackbone doesn\'t support feed forward chunking'		)
							def 							__lowercase      (							self :	str		):
									pass







							@unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute'		)
							def 							__lowercase      (							self :	List[Any]		):
									pass







							@unittest.skip('TimmBackbone initialization is managed on the timm side'		)
							def 							__lowercase      (							self :	Optional[Any]		):
									pass







							@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds'		)
							def 							__lowercase      (							self :	Optional[Any]		):
									pass







							@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds'		)
							def 							__lowercase      (							self :	Any		):
									pass







							@unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint'		)
							def 							__lowercase      (							self :	List[str]		):
									pass







							@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'		)
							def 							__lowercase      (							self :	List[Any]		):
									pass







							@unittest.skip('model weights aren\'t tied in TimmBackbone.'		)
							def 							__lowercase      (							self :	List[str]		):
									pass







							@unittest.skip('model weights aren\'t tied in TimmBackbone.'		)
							def 							__lowercase      (							self :	Optional[int]		):
									pass







							@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'		)
							def 							__lowercase      (							self :	Optional[Any]		):
									pass







							@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'		)
							def 							__lowercase      (							self :	str		):
									pass







							@unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.'		)
							def 							__lowercase      (							self :	Optional[Any]		):
									pass







							@unittest.skip('TimmBackbone doesn\'t support output_attentions.'		)
							def 							__lowercase      (							self :	Optional[Any]		):
									pass







							@unittest.skip('Safetensors is not supported by timm.'		)
							def 							__lowercase      (							self :	List[Any]		):
									pass







							@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
							def 							__lowercase      (							self :	Union[str, Any]		):
									pass







							def 							__lowercase      (							self :	Optional[Any]		):
									_a					:				List[Any]				=							self.model_tester.prepare_config_and_inputs_for_common()

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

											_a					:				Any				=							['''pixel_values''']
											self.assertListEqual(arg_names[:1]     ,a_		)







							def 							__lowercase      (							self :	List[Any]		):
									_a					:				Optional[int]				=							self.model_tester.prepare_config_and_inputs_for_common()
									_a					:				List[Any]				=							True
									_a					:				Optional[int]				=							self.has_attentions

									# no need to test all models as different heads yield the same functionality
									_a					:				Tuple				=							self.all_model_classes[0]
									_a					:				Dict				=							model_class(a_		)
									model.to(a_		)

									_a					:				Union[str, Any]				=							self._prepare_for_class(a_     ,a_		)
									_a					:				int				=							model(**a_		)
									_a					:				Optional[int]				=							outputs[0][-1]

									# Encoder-/Decoder-only models
									_a					:				int				=							outputs.hidden_states[0]
									hidden_states.retain_grad()

									if self.has_attentions:
											_a					:				Tuple				=							outputs.attentions[0]
											attentions.retain_grad()

									output.flatten()[0].backward(retain_graph=a_		)

									self.assertIsNotNone(hidden_states.grad		)

									if self.has_attentions:
											self.assertIsNotNone(attentions.grad		)







							def 							__lowercase      (							self :	List[str]		):
									_a					:				List[Any]				=							self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											_a					:				Union[str, Any]				=							model_class(a_		)
											model.to(a_		)
											model.eval()
											_a					:				str				=							model(**a_		)

											self.assertEqual(len(result.feature_maps		)     ,len(config.out_indices		)		)
											self.assertEqual(len(model.channels		)     ,len(config.out_indices		)		)

											# Check output of last stage is taken if out_features=None, out_indices=None
											_a					:				Dict				=							copy.deepcopy(a_		)
											_a					:				List[str]				=							None
											_a					:				List[str]				=							model_class(a_		)
											model.to(a_		)
											model.eval()
											_a					:				Union[str, Any]				=							model(**a_		)

											self.assertEqual(len(result.feature_maps		)     ,1		)
											self.assertEqual(len(model.channels		)     ,1		)

											# Check backbone can be initialized with fresh weights
											_a					:				str				=							copy.deepcopy(a_		)
											_a					:				List[Any]				=							False
											_a					:				Optional[int]				=							model_class(a_		)
											model.to(a_		)
											model.eval()
											_a					:				str				=							model(**a_		)





'''simple docstring'''

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
							import tensorflow as tf
							from tensorflow.python.eager import context
							from tensorflow.python.framework import ops

							from transformers import GradientAccumulator, create_optimizer

@require_tf
class      __magic_name__						(   unittest.TestCase       ):







							def 							__lowercase      (							self :	int     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	Optional[Any]		):
									self.assertEqual(len(_UpperCAmelCase		)     ,len(_UpperCAmelCase		)		)
									for a, b in zip(_UpperCAmelCase     ,_UpperCAmelCase		):
											self.assertAlmostEqual(_UpperCAmelCase     ,_UpperCAmelCase     ,delta=_UpperCAmelCase		)







							def 							__lowercase      (							self :	int		):
									_a					:				int				=							GradientAccumulator()
									accumulator([tf.constant([1.0, 2.0]		)]		)
									accumulator([tf.constant([-2.0, 1.0]		)]		)
									accumulator([tf.constant([-1.0, 2.0]		)]		)
									with self.assertRaises(_UpperCAmelCase		):
											accumulator([tf.constant([1.0, 1.0]		), tf.constant([2.0, 2.0]		)]		)
									self.assertEqual(accumulator.step     ,3		)
									self.assertEqual(len(accumulator.gradients		)     ,1		)
									self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()     ,[-2.0, 5.0]     ,tol=1E-2		)
									accumulator.reset()
									self.assertEqual(accumulator.step     ,0		)
									self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()     ,[0.0, 0.0]     ,tol=1E-2		)







							def 							__lowercase      (							self :	Any		):
									_a					:				int				=							None
									ops.enable_eager_execution_internal()
									_a					:				Optional[int]				=							tf.config.list_physical_devices('CPU'		)
									if len(_UpperCAmelCase		) == 1:
											tf.config.set_logical_device_configuration(
											    physical_devices[0]     ,[tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]		)
									_a					:				Tuple				=							tf.config.list_logical_devices(device_type='CPU'		)
									_a					:				List[str]				=							tf.distribute.MirroredStrategy(devices=devices[:2]		)

									with strategy.scope():
											_a					:				Tuple				=							GradientAccumulator()
											_a					:				List[Any]				=							tf.Variable([4.0, 3.0]		)
											_a	, _a					:				Dict				=							create_optimizer(5E-5     ,10     ,5		)
											_a					:				Tuple				=							tf.Variable([0.0, 0.0]     ,trainable=_UpperCAmelCase		)

									def accumulate_on_replica(_UpperCAmelCase :	str		):
											accumulator([gradient]		)

									def apply_on_replica():
											optimizer.apply_gradients(list(zip(accumulator.gradients     ,[variable]		)		)		)

									@tf.function
									def accumulate(_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Optional[int]		):
											with strategy.scope():
													_a					:				Union[str, Any]				=							strategy.experimental_local_results(_UpperCAmelCase		)
													local_variables[0].assign(_UpperCAmelCase		)
													local_variables[1].assign(_UpperCAmelCase		)
													strategy.run(_UpperCAmelCase     ,args=(gradient_placeholder,)		)

									@tf.function
									def apply_grad():
											with strategy.scope():
													strategy.run(_UpperCAmelCase		)

									def _check_local_values(_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	List[str]		):
											_a					:				List[Any]				=							strategy.experimental_local_results(accumulator._gradients[0]		)
											self.assertListAlmostEqual(values[0].value()     ,_UpperCAmelCase     ,tol=1E-2		)
											self.assertListAlmostEqual(values[1].value()     ,_UpperCAmelCase     ,tol=1E-2		)

									accumulate([1.0, 2.0]     ,[-1.0, 1.0]		)
									accumulate([3.0, -1.0]     ,[-1.0, -1.0]		)
									accumulate([-2.0, 2.0]     ,[3.0, -2.0]		)
									self.assertEqual(accumulator.step     ,3		)
									_check_local_values([2.0, 3.0]     ,[1.0, -2.0]		)
									apply_grad()
									self.assertListAlmostEqual(variable.value()     ,[4.0, 3.0]     ,tol=1E-2		)
									accumulator.reset()
									self.assertEqual(accumulator.step     ,0		)
									_check_local_values([0.0, 0.0]     ,[0.0, 0.0]		)




