


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




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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )
       a		:List[str]									=			self.get_config()

       return config, pixel_values




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       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 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:Tuple									=			TimmBackbone(config=_lowerCamelCase       )
       model.to(_lowerCamelCase       )
       model.eval()
       with torch.no_grad():
             a		:Optional[int]									=			model(_lowerCamelCase       )
       self.parent.assertEqual(
           result.feature_map[-1].shape   ,    (self.batch_size, model.channels[-1], 14, 14)   ,    )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			self.prepare_config_and_inputs()
       a				,							a		:Dict									=			config_and_inputs
       a		:Optional[Any]									=			{'''pixel_values''': pixel_values}
       return config, inputs_dict







@require_torch
@require_timm
class 						_snake_case    ( _snake_case		,       _snake_case		,       _snake_case		,       unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			(TimmBackbone,) if is_torch_available() else ()
 SCREAMING_SNAKE_CASE__ =			{'feature-extraction': TimmBackbone} if is_torch_available() else {}
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			TimmBackboneModelTester(self       )
       a		:List[str]									=			ConfigTester(self   ,    config_class=_lowerCamelCase   ,    has_text_modality=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       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 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Any									=			'''resnet18'''
       a		:List[Any]									=			'''microsoft/resnet-18'''

       a		:Dict									=			AutoBackbone.from_pretrained(_lowerCamelCase   ,    use_timm_backbone=_lowerCamelCase       )
       a		:Optional[int]									=			AutoBackbone.from_pretrained(_lowerCamelCase       )

       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		:Any									=			AutoBackbone.from_pretrained(_lowerCamelCase   ,    use_timm_backbone=_lowerCamelCase   ,    out_indices=[1, 2, 3]       )
       a		:Any									=			AutoBackbone.from_pretrained(_lowerCamelCase   ,    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 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 @unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 @unittest.skip('''TimmBackbone initialization is managed on the timm side'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




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




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




 @unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




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




 @unittest.skip('''model weights aren\'t tied in TimmBackbone.'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 @unittest.skip('''model weights aren\'t tied in TimmBackbone.'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




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




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




 @unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 @unittest.skip('''TimmBackbone doesn\'t support output_attentions.'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 @unittest.skip('''Safetensors is not supported by timm.'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:Dict									=			self.model_tester.prepare_config_and_inputs_for_common()

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

             a		:Dict									=			['''pixel_values''']
             self.assertListEqual(arg_names[:1]   ,    _lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:Tuple									=			self.model_tester.prepare_config_and_inputs_for_common()
       a		:Optional[int]									=			True
       a		:List[str]									=			self.has_attentions

       # no need to test all models as different heads yield the same functionality
       a		:Any									=			self.all_model_classes[0]
       a		:int									=			model_class(_lowerCamelCase       )
       model.to(_lowerCamelCase       )

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

       # Encoder-/Decoder-only models
       a		:Tuple									=			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=_lowerCamelCase       )

       self.assertIsNotNone(hidden_states.grad       )

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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:Any									=			self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             a		:List[str]									=			model_class(_lowerCamelCase       )
             model.to(_lowerCamelCase       )
             model.eval()
             a		:List[Any]									=			model(**_lowerCamelCase       )

             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(_lowerCamelCase       )
             a		:str									=			None
             a		:Optional[int]									=			model_class(_lowerCamelCase       )
             model.to(_lowerCamelCase       )
             model.eval()
             a		:List[str]									=			model(**_lowerCamelCase       )

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

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









import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
     import torch
     from torch import nn

     from transformers import (
         Adafactor,
         AdamW,
         get_constant_schedule,
         get_constant_schedule_with_warmup,
         get_cosine_schedule_with_warmup,
         get_cosine_with_hard_restarts_schedule_with_warmup,
         get_inverse_sqrt_schedule,
         get_linear_schedule_with_warmup,
         get_polynomial_decay_schedule_with_warmup,
     )
def 							__lowerCamelCase		(					UpperCAmelCase_		:						str					,    UpperCAmelCase_		:						Union[str, Any]=10		):




      """simple docstring"""





      a		:Tuple									=			[]
      for _ in range(UpperCAmelCase_		):
            lrs.append(scheduler.get_lr()[0]		)
            scheduler.step()
      return lrs
def 							__lowerCamelCase		(					UpperCAmelCase_		:						str					,    UpperCAmelCase_		:						Tuple=10		):




      """simple docstring"""





      a		:List[Any]									=			[]
      for step in range(UpperCAmelCase_		):
            lrs.append(scheduler.get_lr()[0]		)
            scheduler.step()
            if step == num_steps // 2:
                  with tempfile.TemporaryDirectory() as tmpdirname:
                        a		:Union[str, Any]									=			os.path.join(UpperCAmelCase_					,    '''schedule.bin'''		)
                        torch.save(scheduler.state_dict()					,    UpperCAmelCase_		)

                        a		:int									=			torch.load(UpperCAmelCase_		)
                        scheduler.load_state_dict(UpperCAmelCase_		)
      return lrs







@require_torch
class 						_snake_case    ( unittest.TestCase				):




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       self.assertEqual(len(_lowerCamelCase       )   ,    len(_lowerCamelCase       )       )
       for a, b in zip(_lowerCamelCase   ,    _lowerCamelCase       ):
             self.assertAlmostEqual(_lowerCamelCase   ,    _lowerCamelCase   ,    delta=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			torch.tensor([0.1, -0.2, -0.1]   ,    requires_grad=_lowerCamelCase       )
       a		:int									=			torch.tensor([0.4, 0.2, -0.5]       )
       a		:int									=			nn.MSELoss()
       # No warmup, constant schedule, no gradient clipping
       a		:Tuple									=			AdamW(params=[w]   ,    lr=2e-1   ,    weight_decay=0.0       )
       for _ in range(100       ):
             a		:int									=			criterion(_lowerCamelCase   ,    _lowerCamelCase       )
             loss.backward()
             optimizer.step()
             w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
             w.grad.zero_()
       self.assertListAlmostEqual(w.tolist()   ,    [0.4, 0.2, -0.5]   ,    tol=1e-2       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			torch.tensor([0.1, -0.2, -0.1]   ,    requires_grad=_lowerCamelCase       )
       a		:Union[str, Any]									=			torch.tensor([0.4, 0.2, -0.5]       )
       a		:List[Any]									=			nn.MSELoss()
       # No warmup, constant schedule, no gradient clipping
       a		:Any									=			Adafactor(
           params=[w]   ,    lr=1e-2   ,    eps=(1e-30, 1e-3)   ,    clip_threshold=1.0   ,    decay_rate=-0.8   ,    betaa=_lowerCamelCase   ,    weight_decay=0.0   ,    relative_step=_lowerCamelCase   ,    scale_parameter=_lowerCamelCase   ,    warmup_init=_lowerCamelCase   ,    )
       for _ in range(1000       ):
             a		:str									=			criterion(_lowerCamelCase   ,    _lowerCamelCase       )
             loss.backward()
             optimizer.step()
             w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
             w.grad.zero_()
       self.assertListAlmostEqual(w.tolist()   ,    [0.4, 0.2, -0.5]   ,    tol=1e-2       )







@require_torch
class 						_snake_case    ( unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			nn.Linear(50		,       50				) if is_torch_available() else None
 SCREAMING_SNAKE_CASE__ =			AdamW(m.parameters()		,       lr=10.0				) if is_torch_available() else None
 SCREAMING_SNAKE_CASE__ =			10




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase=None       ):
       self.assertEqual(len(_lowerCamelCase       )   ,    len(_lowerCamelCase       )       )
       for a, b in zip(_lowerCamelCase   ,    _lowerCamelCase       ):
             self.assertAlmostEqual(_lowerCamelCase   ,    _lowerCamelCase   ,    delta=_lowerCamelCase   ,    msg=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Union[str, Any]									=			{'''num_warmup_steps''': 2, '''num_training_steps''': 10}
       # schedulers doct format
       # function: (sched_args_dict, expected_learning_rates)
       a		:Any									=			{
           get_constant_schedule: ({}, [10.0] * self.num_steps),
           get_constant_schedule_with_warmup: (
               {'''num_warmup_steps''': 4},
               [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
           ),
           get_linear_schedule_with_warmup: (
               {**common_kwargs},
               [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25],
           ),
           get_cosine_schedule_with_warmup: (
               {**common_kwargs},
               [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38],
           ),
           get_cosine_with_hard_restarts_schedule_with_warmup: (
               {**common_kwargs, '''num_cycles''': 2},
               [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46],
           ),
           get_polynomial_decay_schedule_with_warmup: (
               {**common_kwargs, '''power''': 2.0, '''lr_end''': 1e-7},
               [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156],
           ),
           get_inverse_sqrt_schedule: (
               {'''num_warmup_steps''': 2},
               [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714],
           ),
       }

       for scheduler_func, data in scheds.items():
             a				,							a		:List[str]									=			data

             a		:str									=			scheduler_func(self.optimizer   ,    **_lowerCamelCase       )
             self.assertEqual(len([scheduler.get_lr()[0]]       )   ,    1       )
             a		:Optional[Any]									=			unwrap_schedule(_lowerCamelCase   ,    self.num_steps       )
             self.assertListAlmostEqual(
                 _lowerCamelCase   ,    _lowerCamelCase   ,    tol=1e-2   ,    msg=F'''failed for {scheduler_func} in normal scheduler'''   ,    )

             a		:List[Any]									=			scheduler_func(self.optimizer   ,    **_lowerCamelCase       )
             if scheduler_func.__name__ != "get_constant_schedule":
                   LambdaScheduleWrapper.wrap_scheduler(_lowerCamelCase       )  # wrap to test picklability of the schedule
             a		:Dict									=			unwrap_and_save_reload_schedule(_lowerCamelCase   ,    self.num_steps       )
             self.assertListEqual(_lowerCamelCase   ,    _lowerCamelCase   ,    msg=F'''failed for {scheduler_func} in save and reload'''       )







class 						_snake_case    :




 def __init__(						self   ,    _lowerCamelCase       ):
       a		:Dict									=			fn




 def __call__(						self   ,    *_lowerCamelCase   ,    **_lowerCamelCase       ):
       return self.fn(*_lowerCamelCase   ,    **_lowerCamelCase       )




 @classmethod
 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase       ):
       a		:str									=			list(map(self   ,    scheduler.lr_lambdas       )       )





