import inspect
import unittest

from transformers import YolosConfig
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_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import YolosForObjectDetection, YolosModel
      from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor





class      A							:


   def __init__(self     :						Any       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						int=1_3       ,  __UpperCAmelCase     :						Optional[int]=[3_0, 3_0]       ,  __UpperCAmelCase     :						Tuple=2       ,  __UpperCAmelCase     :						Tuple=3       ,  __UpperCAmelCase     :						int=True       ,  __UpperCAmelCase     :						Optional[Any]=True       ,  __UpperCAmelCase     :						Any=3_2       ,  __UpperCAmelCase     :						Tuple=5       ,  __UpperCAmelCase     :						Any=4       ,  __UpperCAmelCase     :						List[str]=3_7       ,  __UpperCAmelCase     :						str="gelu"       ,  __UpperCAmelCase     :						Dict=0.1       ,  __UpperCAmelCase     :						List[str]=0.1       ,  __UpperCAmelCase     :						List[str]=1_0       ,  __UpperCAmelCase     :						Union[str, Any]=0.02       ,  __UpperCAmelCase     :						List[str]=3       ,  __UpperCAmelCase     :						Dict=None       ,  __UpperCAmelCase     :						Any=8       ,  __UpperCAmelCase     :						List[str]=1_0       ,  )						->				Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  batch_size
         UpperCAmelCase__								=  image_size
         UpperCAmelCase__								=  patch_size
         UpperCAmelCase__								=  num_channels
         UpperCAmelCase__								=  is_training
         UpperCAmelCase__								=  use_labels
         UpperCAmelCase__								=  hidden_size
         UpperCAmelCase__								=  num_hidden_layers
         UpperCAmelCase__								=  num_attention_heads
         UpperCAmelCase__								=  intermediate_size
         UpperCAmelCase__								=  hidden_act
         UpperCAmelCase__								=  hidden_dropout_prob
         UpperCAmelCase__								=  attention_probs_dropout_prob
         UpperCAmelCase__								=  type_sequence_label_size
         UpperCAmelCase__								=  initializer_range
         UpperCAmelCase__								=  num_labels
         UpperCAmelCase__								=  scope
         UpperCAmelCase__								=  n_targets
         UpperCAmelCase__								=  num_detection_tokens
         # we set the expected sequence length (which is used in several tests)
         # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
         UpperCAmelCase__								=  (image_size[1] // patch_size) * (image_size[0] // patch_size)
         UpperCAmelCase__								=  num_patches + 1 + self.num_detection_tokens


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



         UpperCAmelCase__								=  floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]		)

         UpperCAmelCase__								=  None
         if self.use_labels:
               # labels is a list of Dict (each Dict being the labels for a given example in the batch)
               UpperCAmelCase__								=  []
               for i in range(self.batch_size		):
                     UpperCAmelCase__								=  {}
                     UpperCAmelCase__								=  torch.randint(
                         high=self.num_labels       ,  size=(self.n_targets,)       ,  device=__UpperCAmelCase		)
                     UpperCAmelCase__								=  torch.rand(self.n_targets       ,  4       ,  device=__UpperCAmelCase		)
                     labels.append(__UpperCAmelCase		)

         UpperCAmelCase__								=  self.get_config()

         return config, pixel_values, labels


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



         return YolosConfig(
             image_size=self.image_size       ,  patch_size=self.patch_size       ,  num_channels=self.num_channels       ,  hidden_size=self.hidden_size       ,  num_hidden_layers=self.num_hidden_layers       ,  num_attention_heads=self.num_attention_heads       ,  intermediate_size=self.intermediate_size       ,  hidden_act=self.hidden_act       ,  hidden_dropout_prob=self.hidden_dropout_prob       ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,  is_decoder=__UpperCAmelCase       ,  initializer_range=self.initializer_range       ,  num_detection_tokens=self.num_detection_tokens       ,  num_labels=self.num_labels       ,  )


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



         UpperCAmelCase__								=  YolosModel(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(
             result.last_hidden_state.shape       ,  (self.batch_size, self.expected_seq_len, self.hidden_size)		)


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



         UpperCAmelCase__								=  YolosForObjectDetection(__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()

         UpperCAmelCase__								=  model(pixel_values=__UpperCAmelCase		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)

         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_detection_tokens, self.num_labels + 1)		)
         self.parent.assertEqual(result.pred_boxes.shape       ,  (self.batch_size, self.num_detection_tokens, 4)		)

         UpperCAmelCase__								=  model(pixel_values=__UpperCAmelCase       ,  labels=__UpperCAmelCase		)

         self.parent.assertEqual(result.loss.shape       ,  ()		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_detection_tokens, self.num_labels + 1)		)
         self.parent.assertEqual(result.pred_boxes.shape       ,  (self.batch_size, self.num_detection_tokens, 4)		)





   def 			lowercase_      (self     :						Dict		)						->				Any:
         """simple docstring"""



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





@require_torch
class      A							(				UpperCAmelCase_		,				UpperCAmelCase_		,				unittest.TestCase     ):
   __UpperCAmelCase : int      =			(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
   __UpperCAmelCase : Tuple      =			(
       {'feature-extraction': YolosModel, 'object-detection': YolosForObjectDetection} if is_torch_available() else {}
   )

   __UpperCAmelCase : Union[str, Any]      =			False
   __UpperCAmelCase : int      =			False
   __UpperCAmelCase : List[str]      =			False
   __UpperCAmelCase : Dict      =			False


   def 			lowercase_      (self     :						Union[str, Any]       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						str=False		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  super()._prepare_for_class(__UpperCAmelCase       ,  __UpperCAmelCase       ,  return_labels=__UpperCAmelCase		)

         if return_labels:
               if model_class.__name__ == "YolosForObjectDetection":
                     UpperCAmelCase__								=  []
                     for i in range(self.model_tester.batch_size		):
                           UpperCAmelCase__								=  {}
                           UpperCAmelCase__								=  torch.ones(
                               size=(self.model_tester.n_targets,)       ,  device=__UpperCAmelCase       ,  dtype=torch.long		)
                           UpperCAmelCase__								=  torch.ones(
                               self.model_tester.n_targets       ,  4       ,  device=__UpperCAmelCase       ,  dtype=torch.float		)
                           labels.append(__UpperCAmelCase		)
                     UpperCAmelCase__								=  labels

         return inputs_dict


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



         UpperCAmelCase__								=  YolosModelTester(self		)
         UpperCAmelCase__								=  ConfigTester(self       ,  config_class=__UpperCAmelCase       ,  has_text_modality=__UpperCAmelCase       ,  hidden_size=3_7		)


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



         self.config_tester.run_common_tests()


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



         pass


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



         UpperCAmelCase__		,      UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               self.assertIsInstance(model.get_input_embeddings()       ,  (nn.Module)		)
               UpperCAmelCase__								=  model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(__UpperCAmelCase       ,  nn.Linear		)		)


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



         UpperCAmelCase__		,      UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase__								=  ["pixel_values"]
               self.assertListEqual(arg_names[:1]       ,  __UpperCAmelCase		)


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



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Tuple		)						->				Optional[int]:
         """simple docstring"""



         UpperCAmelCase__		,      UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase__								=  True

         # in YOLOS, the seq_len is different
         UpperCAmelCase__								=  self.model_tester.expected_seq_len
         for model_class in self.all_model_classes:
               UpperCAmelCase__								=  True
               UpperCAmelCase__								=  False
               UpperCAmelCase__								=  True
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               model.to(__UpperCAmelCase		)
               model.eval()
               with torch.no_grad():
                     UpperCAmelCase__								=  model(**self._prepare_for_class(__UpperCAmelCase       ,  __UpperCAmelCase		)		)
               UpperCAmelCase__								=  outputs.attentions
               self.assertEqual(len(__UpperCAmelCase		)       ,  self.model_tester.num_hidden_layers		)

               # check that output_attentions also work using config
               del inputs_dict["output_attentions"]
               UpperCAmelCase__								=  True
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               model.to(__UpperCAmelCase		)
               model.eval()
               with torch.no_grad():
                     UpperCAmelCase__								=  model(**self._prepare_for_class(__UpperCAmelCase       ,  __UpperCAmelCase		)		)
               UpperCAmelCase__								=  outputs.attentions
               self.assertEqual(len(__UpperCAmelCase		)       ,  self.model_tester.num_hidden_layers		)

               self.assertListEqual(
                   list(attentions[0].shape[-3:]		)       ,  [self.model_tester.num_attention_heads, seq_len, seq_len]       ,  )
               UpperCAmelCase__								=  len(__UpperCAmelCase		)

               # Check attention is always last and order is fine
               UpperCAmelCase__								=  True
               UpperCAmelCase__								=  True
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               model.to(__UpperCAmelCase		)
               model.eval()
               with torch.no_grad():
                     UpperCAmelCase__								=  model(**self._prepare_for_class(__UpperCAmelCase       ,  __UpperCAmelCase		)		)

               UpperCAmelCase__								=  1
               self.assertEqual(out_len + added_hidden_states       ,  len(__UpperCAmelCase		)		)

               UpperCAmelCase__								=  outputs.attentions

               self.assertEqual(len(__UpperCAmelCase		)       ,  self.model_tester.num_hidden_layers		)
               self.assertListEqual(
                   list(self_attentions[0].shape[-3:]		)       ,  [self.model_tester.num_attention_heads, seq_len, seq_len]       ,  )


   def 			lowercase_      (self     :						Any		)						->				Dict:
         """simple docstring"""



         def check_hidden_states_output(__UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Union[str, Any]		):
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               model.to(__UpperCAmelCase		)
               model.eval()

               with torch.no_grad():
                     UpperCAmelCase__								=  model(**self._prepare_for_class(__UpperCAmelCase       ,  __UpperCAmelCase		)		)

               UpperCAmelCase__								=  outputs.hidden_states

               UpperCAmelCase__								=  getattr(
                   self.model_tester       ,  "expected_num_hidden_layers"       ,  self.model_tester.num_hidden_layers + 1		)
               self.assertEqual(len(__UpperCAmelCase		)       ,  __UpperCAmelCase		)

               # YOLOS has a different seq_length
               UpperCAmelCase__								=  self.model_tester.expected_seq_len

               self.assertListEqual(
                   list(hidden_states[0].shape[-2:]		)       ,  [seq_length, self.model_tester.hidden_size]       ,  )

         UpperCAmelCase__		,      UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               UpperCAmelCase__								=  True
               check_hidden_states_output(__UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase		)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               UpperCAmelCase__								=  True

               check_hidden_states_output(__UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase		)


   def 			lowercase_      (self     :						str		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_object_detection(*__UpperCAmelCase		)





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



         for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase__								=  YolosModel.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)




def    lowerCAmelCase_ (     )     ->				List[str]:







      '''simple docstring'''


      UpperCAmelCase__								=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
      return image







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


   @cached_property
   def 			lowercase_      (self     :						Union[str, Any]		)						->				Any:
         """simple docstring"""



         return AutoImageProcessor.from_pretrained("hustvl/yolos-small"		) if is_vision_available() else None





   @slow
   def 			lowercase_      (self     :						Union[str, Any]		)						->				Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  YolosForObjectDetection.from_pretrained("hustvl/yolos-small"		).to(__UpperCAmelCase		)

         UpperCAmelCase__								=  self.default_image_processor
         UpperCAmelCase__								=  prepare_img()
         UpperCAmelCase__								=  image_processor(images=__UpperCAmelCase       ,  return_tensors="pt"		).to(__UpperCAmelCase		)

         # forward pass
         with torch.no_grad():
               UpperCAmelCase__								=  model(inputs.pixel_values		)

         # verify outputs
         UpperCAmelCase__								=  torch.Size((1, 1_0_0, 9_2)		)
         self.assertEqual(outputs.logits.shape       ,  __UpperCAmelCase		)

         UpperCAmelCase__								=  torch.tensor(
             [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]       ,  device=__UpperCAmelCase       ,  )
         UpperCAmelCase__								=  torch.tensor(
             [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]       ,  device=__UpperCAmelCase		)
         self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]       ,  __UpperCAmelCase       ,  atol=1E-4		)		)
         self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]       ,  __UpperCAmelCase       ,  atol=1E-4		)		)

         # verify postprocessing
         UpperCAmelCase__								=  image_processor.post_process_object_detection(
             __UpperCAmelCase       ,  threshold=0.3       ,  target_sizes=[image.size[::-1]]		)[0]
         UpperCAmelCase__								=  torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]		).to(__UpperCAmelCase		)
         UpperCAmelCase__								=  [7_5, 7_5, 1_7, 6_3, 1_7]
         UpperCAmelCase__								=  torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]		).to(__UpperCAmelCase		)

         self.assertEqual(len(results["scores"]		)       ,  5		)
         self.assertTrue(torch.allclose(results["scores"]       ,  __UpperCAmelCase       ,  atol=1E-4		)		)
         self.assertSequenceEqual(results["labels"].tolist()       ,  __UpperCAmelCase		)
         self.assertTrue(torch.allclose(results["boxes"][0, :]       ,  __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      A							(				unittest.TestCase     ):


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



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


   @require_multi_gpu
   def 			lowercase_      (self     :						List[str]		)						->				Any:
         """simple docstring"""



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


   @require_multi_gpu
   def 			lowercase_      (self     :						str		)						->				str:
         """simple docstring"""



         print(f"""Found {torch.cuda.device_count()} devices."""		)
         UpperCAmelCase__								=  ["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(__UpperCAmelCase       ,  env=os.environ.copy()		)


   @require_multi_gpu
   def 			lowercase_      (self     :						Tuple		)						->				int:
         """simple docstring"""



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





   @require_multi_gpu
   def 			lowercase_      (self     :						Dict		)						->				str:
         """simple docstring"""



         print(f"""Found {torch.cuda.device_count()} devices, using 2 devices only"""		)
         UpperCAmelCase__								=  ["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(__UpperCAmelCase       ,  env=os.environ.copy()		)


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

      UpperCamelCase__											=					''

      UpperCamelCase__											=					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)."

      UpperCamelCase__											=					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."
      UpperCamelCase__											=					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)



