





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


       def __init__(							self    :Tuple							,				__A    :Dict							,				__A    :Optional[int]=None							,				__A    :List[Any]=None							,				__A    :Tuple=None							,				__A    :str="resnet50"							,				__A    :Union[str, Any]=3							,				__A    :Optional[Any]=32							,				__A    :Dict=3							,				__A    :Union[str, Any]=True							,				__A    :Union[str, Any]=True							,				)   ->   Dict:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      parent
         SCREAMING_SNAKE_CASE__			=      out_indices if out_indices is not None else [4]
         SCREAMING_SNAKE_CASE__			=      stage_names
         SCREAMING_SNAKE_CASE__			=      out_features
         SCREAMING_SNAKE_CASE__			=      backbone
         SCREAMING_SNAKE_CASE__			=      batch_size
         SCREAMING_SNAKE_CASE__			=      image_size
         SCREAMING_SNAKE_CASE__			=      num_channels
         SCREAMING_SNAKE_CASE__			=      use_pretrained_backbone
         SCREAMING_SNAKE_CASE__			=      is_training


       def      _snake_case  (							self    :int		)   ->   str:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)
         SCREAMING_SNAKE_CASE__			=      self.get_config()

         return config, pixel_values


       def      _snake_case  (							self    :Any		)   ->   Optional[int]:



         """simple docstring"""



         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      _snake_case  (							self    :Tuple							,				__A    :Dict							,				__A    :List[Any]		)   ->   Optional[int]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      TimmBackbone(config=__UpperCamelCase		)
         model.to(__UpperCamelCase		)
         model.eval()
         with torch.no_grad():
           SCREAMING_SNAKE_CASE__			=      model(__UpperCamelCase		)
         self.parent.assertEqual(
             result.feature_map[-1].shape							,				(self.batch_size, model.channels[-1], 14, 14)							,				)


       def      _snake_case  (							self    :str		)   ->   List[str]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      self.prepare_config_and_inputs()
         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      config_and_inputs
         SCREAMING_SNAKE_CASE__			=      {"""pixel_values""": pixel_values}
         return config, inputs_dict




@require_torch
@require_timm
class   UpperCamelCase_       (     UpperCamelCase__				,  UpperCamelCase__				,  UpperCamelCase__				,  unittest.TestCase    ):
       lowerCamelCase_           =					(TimmBackbone,) if is_torch_available() else ()
       lowerCamelCase_           =					{"feature-extraction": TimmBackbone} if is_torch_available() else {}
       lowerCamelCase_           =					False
       lowerCamelCase_           =					False
       lowerCamelCase_           =					False
       lowerCamelCase_           =					False


       def      _snake_case  (							self    :str		)   ->   Optional[int]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      TimmBackboneModelTester(self		)
         SCREAMING_SNAKE_CASE__			=      ConfigTester(self							,				config_class=__UpperCamelCase							,				has_text_modality=__UpperCamelCase		)


       def      _snake_case  (							self    :Union[str, Any]		)   ->   Union[str, Any]:



         """simple docstring"""



         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      _snake_case  (							self    :Any		)   ->   Dict:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      """resnet18"""
         SCREAMING_SNAKE_CASE__			=      """microsoft/resnet-18"""

         SCREAMING_SNAKE_CASE__			=      AutoBackbone.from_pretrained(__UpperCamelCase							,				use_timm_backbone=__UpperCamelCase		)
         SCREAMING_SNAKE_CASE__			=      AutoBackbone.from_pretrained(__UpperCamelCase		)

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

         SCREAMING_SNAKE_CASE__			=      AutoBackbone.from_pretrained(__UpperCamelCase							,				use_timm_backbone=__UpperCamelCase							,				out_indices=[1, 2, 3]		)
         SCREAMING_SNAKE_CASE__			=      AutoBackbone.from_pretrained(__UpperCamelCase							,				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      _snake_case  (							self    :Optional[int]		)   ->   Union[str, Any]:



         """simple docstring"""



         pass


       @unittest.skip("""TimmBackbone doesn't have num_hidden_layers attribute"""		)
       def      _snake_case  (							self    :List[Any]		)   ->   Optional[Any]:



         """simple docstring"""



         pass


       @unittest.skip("""TimmBackbone initialization is managed on the timm side"""		)
       def      _snake_case  (							self    :Dict		)   ->   List[Any]:



         """simple docstring"""



         pass


       @unittest.skip("""TimmBackbone models doesn't have inputs_embeds"""		)
       def      _snake_case  (							self    :Union[str, Any]		)   ->   Optional[int]:



         """simple docstring"""



         pass


       @unittest.skip("""TimmBackbone models doesn't have inputs_embeds"""		)
       def      _snake_case  (							self    :Optional[Any]		)   ->   Dict:



         """simple docstring"""



         pass


       @unittest.skip("""TimmBackbone model cannot be created without specifying a backbone checkpoint"""		)
       def      _snake_case  (							self    :Any		)   ->   Union[str, Any]:



         """simple docstring"""



         pass


       @unittest.skip("""Only checkpoints on timm can be loaded into TimmBackbone"""		)
       def      _snake_case  (							self    :Any		)   ->   Optional[int]:



         """simple docstring"""



         pass


       @unittest.skip("""model weights aren't tied in TimmBackbone."""		)
       def      _snake_case  (							self    :Optional[int]		)   ->   Optional[int]:



         """simple docstring"""



         pass


       @unittest.skip("""model weights aren't tied in TimmBackbone."""		)
       def      _snake_case  (							self    :List[Any]		)   ->   str:



         """simple docstring"""



         pass


       @unittest.skip("""Only checkpoints on timm can be loaded into TimmBackbone"""		)
       def      _snake_case  (							self    :str		)   ->   Any:



         """simple docstring"""



         pass


       @unittest.skip("""Only checkpoints on timm can be loaded into TimmBackbone"""		)
       def      _snake_case  (							self    :List[Any]		)   ->   str:



         """simple docstring"""



         pass


       @unittest.skip("""TimmBackbone doesn't have hidden size info in its configuration."""		)
       def      _snake_case  (							self    :Optional[int]		)   ->   str:



         """simple docstring"""



         pass


       @unittest.skip("""TimmBackbone doesn't support output_attentions."""		)
       def      _snake_case  (							self    :List[Any]		)   ->   Union[str, Any]:



         """simple docstring"""



         pass


       @unittest.skip("""Safetensors is not supported by timm."""		)
       def      _snake_case  (							self    :List[Any]		)   ->   Tuple:



         """simple docstring"""



         pass


       @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""		)
       def      _snake_case  (							self    :Optional[int]		)   ->   Optional[Any]:



         """simple docstring"""



         pass


       def      _snake_case  (							self    :str		)   ->   List[Any]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

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

           SCREAMING_SNAKE_CASE__			=      ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]							,				__UpperCamelCase		)


       def      _snake_case  (							self    :List[Any]		)   ->   Union[str, Any]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()
         SCREAMING_SNAKE_CASE__			=      True
         SCREAMING_SNAKE_CASE__			=      self.has_attentions

         # no need to test all models as different heads yield the same functionality
         SCREAMING_SNAKE_CASE__			=      self.all_model_classes[0]
         SCREAMING_SNAKE_CASE__			=      model_class(__UpperCamelCase		)
         model.to(__UpperCamelCase		)

         SCREAMING_SNAKE_CASE__			=      self._prepare_for_class(__UpperCamelCase							,				__UpperCamelCase		)
         SCREAMING_SNAKE_CASE__			=      model(**__UpperCamelCase		)
         SCREAMING_SNAKE_CASE__			=      outputs[0][-1]

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

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

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

         self.assertIsNotNone(hidden_states.grad		)

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


       def      _snake_case  (							self    :Optional[int]		)   ->   Tuple:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           SCREAMING_SNAKE_CASE__			=      model_class(__UpperCamelCase		)
           model.to(__UpperCamelCase		)
           model.eval()
           SCREAMING_SNAKE_CASE__			=      model(**__UpperCamelCase		)

           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
           SCREAMING_SNAKE_CASE__			=      copy.deepcopy(__UpperCamelCase		)
           SCREAMING_SNAKE_CASE__			=      None
           SCREAMING_SNAKE_CASE__			=      model_class(__UpperCamelCase		)
           model.to(__UpperCamelCase		)
           model.eval()
           SCREAMING_SNAKE_CASE__			=      model(**__UpperCamelCase		)

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

           # Check backbone can be initialized with fresh weights
           SCREAMING_SNAKE_CASE__			=      copy.deepcopy(__UpperCamelCase		)
           SCREAMING_SNAKE_CASE__			=      False
           SCREAMING_SNAKE_CASE__			=      model_class(__UpperCamelCase		)
           model.to(__UpperCamelCase		)
           model.eval()
           SCREAMING_SNAKE_CASE__			=      model(**__UpperCamelCase		)






import inspect
import os
import unittest

import torch

import accelerate
from accelerate import Accelerator
from accelerate.test_utils import execute_subprocess_async, require_multi_gpu
from accelerate.utils import patch_environment




class   UpperCamelCase_       (     unittest.TestCase    ):


							def      _snake_case  (							self    :Any		)   ->   int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      inspect.getfile(accelerate.test_utils		)
									SCREAMING_SNAKE_CASE__			=      os.path.sep.join(mod_file.split(os.path.sep		)[:-1] + ["""scripts""", """test_script.py"""]		)
									SCREAMING_SNAKE_CASE__			=      os.path.sep.join(
									    mod_file.split(os.path.sep		)[:-1] + ["""scripts""", """test_distributed_data_loop.py"""]		)
									SCREAMING_SNAKE_CASE__			=      os.path.sep.join(mod_file.split(os.path.sep		)[:-1] + ["""scripts""", """test_ops.py"""]		)


							@require_multi_gpu
							def      _snake_case  (							self    :Optional[Any]		)   ->   Tuple:



									"""simple docstring"""



									print(f'''Found {torch.cuda.device_count()} devices.'''		)
									SCREAMING_SNAKE_CASE__			=      ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path]
									with patch_environment(omp_num_threads=1		):
											execute_subprocess_async(__A							,				env=os.environ.copy()		)


							@require_multi_gpu
							def      _snake_case  (							self    :Tuple		)   ->   Optional[Any]:



									"""simple docstring"""



									print(f'''Found {torch.cuda.device_count()} devices.'''		)
									SCREAMING_SNAKE_CASE__			=      ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path]
									print(f'''Command: {cmd}'''		)
									with patch_environment(omp_num_threads=1		):
											execute_subprocess_async(__A							,				env=os.environ.copy()		)


							@require_multi_gpu
							def      _snake_case  (							self    :Optional[Any]		)   ->   Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__		)]
									with patch_environment(omp_num_threads=1		):
											execute_subprocess_async(__A							,				env=os.environ.copy()		)


							@require_multi_gpu
							def      _snake_case  (							self    :Optional[int]		)   ->   str:



									"""simple docstring"""



									print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only'''		)
									SCREAMING_SNAKE_CASE__			=      ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path]
									with patch_environment(omp_num_threads=1							,				cuda_visible_devices="""0,1"""		):
											execute_subprocess_async(__A							,				env=os.environ.copy()		)


if __name__ == "__main__":
	_lowerCamelCase   				=			Accelerator()
	_lowerCamelCase   				=			(accelerator.state.process_index + 2, 10)
	_lowerCamelCase   				=			torch.randint(0, 10, shape).to(accelerator.device)

	_lowerCamelCase   				=			''

	_lowerCamelCase   				=			accelerator.pad_across_processes(tensor)
	if tensora.shape[0] != accelerator.state.num_processes + 1:
		error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
	if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor):
		error_msg += "Tensors have different values."
	if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0):
		error_msg += "Padding was not done with the right value (0)."

	_lowerCamelCase   				=			accelerator.pad_across_processes(tensor, pad_first=True)
	if tensora.shape[0] != accelerator.state.num_processes + 1:
		error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
	_lowerCamelCase   				=			accelerator.state.num_processes - accelerator.state.process_index - 1
	if not torch.equal(tensora[index:], tensor):
		error_msg += "Tensors have different values."
	if not torch.all(tensora[:index] == 0):
		error_msg += "Padding was not done with the right value (0)."

	# Raise error at the end to make sure we don't stop at the first failure.
	if len(error_msg) > 0:
		raise ValueError(error_msg)