"""simple docstring"""







import json
import os
import tempfile
from unittest.mock import patch

import torch
from torch.utils.data import DataLoader, TensorDataset

from accelerate import DistributedType, infer_auto_device_map, init_empty_weights
from accelerate.accelerator import Accelerator
from accelerate.state import GradientState, PartialState
from accelerate.test_utils import require_bnb, require_multi_gpu, slow
from accelerate.test_utils.testing import AccelerateTestCase, require_cuda
from accelerate.utils import patch_environment



def 			lowercase__(       ):
     lowercase_	:				Union[str, Any]													=	torch.nn.Linear(2     ,			4      )
     lowercase_	:				Optional[Any]													=	torch.optim.AdamW(model.parameters()     ,			lr=1.0      )
     lowercase_	:				Union[str, Any]													=	torch.optim.lr_scheduler.OneCycleLR(__SCREAMING_SNAKE_CASE     ,			max_lr=0.01     ,			steps_per_epoch=2     ,			epochs=1      )
     lowercase_	:				int													=	DataLoader(TensorDataset(torch.tensor([1, 2, 3]      )      )      )
     lowercase_	:				List[str]													=	DataLoader(TensorDataset(torch.tensor([4, 5, 6]      )      )      )

     return model, optimizer, scheduler, train_dl, valid_dl



def 			lowercase__(       __SCREAMING_SNAKE_CASE							:     Any      ):
     return (model.weight.abs().sum() + model.bias.abs().sum()).item()



def 			lowercase__(       __SCREAMING_SNAKE_CASE							:     List[str]      ):
     lowercase_	:				List[Any]													=	torch.nn.Linear(*tuple(model.weight.T.shape      )      ).state_dict()
     model.load_state_dict(__SCREAMING_SNAKE_CASE      )







class        UpperCamelCase		(			lowercase_  ):
   @require_cuda
   def 			_UpperCAmelCase   (			self			)	->      List[Any]:


        '''simple docstring'''


        lowercase_	:				Dict													=	Accelerator()
        assert PartialState._shared_state["_cpu"] is False
        assert PartialState._shared_state["device"].type == "cuda"
        with self.assertRaises(__UpperCamelCase			):
             lowercase_	:				List[str]													=	Accelerator(cpu=__UpperCamelCase			)
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


        lowercase_	:				List[str]													=	Accelerator()
        lowercase_	:				List[str]													=	GradientState()
        assert state.num_steps == 1
        lowercase_	:				Tuple													=	4
        assert state.num_steps == 4

        assert state.sync_gradients is True
        lowercase_	:				Union[str, Any]													=	False
        assert state.sync_gradients is False
        GradientState._reset_state()
   def 			_UpperCAmelCase   (			self			)	->      List[str]:


        '''simple docstring'''


        lowercase_	:				List[str]													=	Accelerator()
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				Dict													=	create_components()

        (
            (
            lowercase_
        )							,  (
            lowercase_
        )							,  (
            lowercase_
        )							,  (
            lowercase_
        )							,  (
            lowercase_
        )							,  
        )	:				Union[str, Any]													=	accelerator.prepare(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)

        self.assertTrue(prepared_model in accelerator._models			)
        self.assertTrue(prepared_optimizer in accelerator._optimizers			)
        self.assertTrue(prepared_scheduler in accelerator._schedulers			)
        self.assertTrue(prepared_train_dl in accelerator._dataloaders			)
        self.assertTrue(prepared_valid_dl in accelerator._dataloaders			)
   def 			_UpperCAmelCase   (			self			)	->      List[Any]:


        '''simple docstring'''


        lowercase_	:				int													=	Accelerator()
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				int													=	create_components()
        accelerator.prepare(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)
        accelerator.free_memory()

        self.assertTrue(len(accelerator._models			) == 0			)
        self.assertTrue(len(accelerator._optimizers			) == 0			)
        self.assertTrue(len(accelerator._schedulers			) == 0			)
        self.assertTrue(len(accelerator._dataloaders			) == 0			)
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        PartialState._reset_state()

        # Mock torch.cuda.set_device to avoid an exception as the device doesn't exist
        def noop(*__UpperCamelCase     ,**__UpperCamelCase			):
             pass

        with patch('torch.cuda.set_device'     ,__UpperCamelCase			), patch_environment(ACCELERATE_TORCH_DEVICE='cuda:64'			):
             lowercase_	:				Dict													=	Accelerator()
             self.assertEqual(str(accelerator.state.device			)     ,'cuda:64'			)
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        lowercase_	:				Tuple													=	Accelerator()
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				Union[str, Any]													=	create_components()
        accelerator.prepare(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)

        lowercase_	:				Tuple													=	get_signature(__UpperCamelCase			)

        with tempfile.TemporaryDirectory() as tmpdirname:
             accelerator.save_state(__UpperCamelCase			)

             # make sure random weights don't match
             load_random_weights(__UpperCamelCase			)
             self.assertTrue(abs(model_signature - get_signature(__UpperCamelCase			)			) > 1e-3			)

             # make sure loaded weights match
             accelerator.load_state(__UpperCamelCase			)
             self.assertTrue(abs(model_signature - get_signature(__UpperCamelCase			)			) < 1e-3			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        lowercase_	:				List[str]													=	Accelerator()
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				Dict													=	create_components()
        accelerator.prepare(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)

        lowercase_	:				Tuple													=	get_signature(__UpperCamelCase			)

        # saving hook
        def save_config(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			):
             lowercase_	:				Optional[Any]													=	{'class_name': models[0].__class__.__name__}

             with open(os.path.join(__UpperCamelCase     ,'data.json'			)     ,'w'			) as f:
                  json.dump(__UpperCamelCase     ,__UpperCamelCase			)

        # loading hook
        def load_config(__UpperCamelCase     ,__UpperCamelCase			):
             with open(os.path.join(__UpperCamelCase     ,'data.json'			)     ,'r'			) as f:
                  lowercase_	:				Any													=	json.load(__UpperCamelCase			)

             lowercase_	:				Optional[int]													=	config['class_name']

        lowercase_	:				str													=	accelerator.register_save_state_pre_hook(__UpperCamelCase			)
        lowercase_	:				int													=	accelerator.register_load_state_pre_hook(__UpperCamelCase			)

        with tempfile.TemporaryDirectory() as tmpdirname:
             accelerator.save_state(__UpperCamelCase			)

             # make sure random weights don't match with hooks
             load_random_weights(__UpperCamelCase			)
             self.assertTrue(abs(model_signature - get_signature(__UpperCamelCase			)			) > 1e-3			)

             # random class name to verify correct one is loaded
             lowercase_	:				Any													=	'random'

             # make sure loaded weights match with hooks
             accelerator.load_state(__UpperCamelCase			)
             self.assertTrue(abs(model_signature - get_signature(__UpperCamelCase			)			) < 1e-3			)

             # mode.class_name is loaded from config
             self.assertTrue(model.class_name == model.__class__.__name__			)

        # remove hooks
        save_hook.remove()
        load_hook.remove()

        with tempfile.TemporaryDirectory() as tmpdirname:
             accelerator.save_state(__UpperCamelCase			)

             # make sure random weights don't match with hooks removed
             load_random_weights(__UpperCamelCase			)
             self.assertTrue(abs(model_signature - get_signature(__UpperCamelCase			)			) > 1e-3			)

             # random class name to verify correct one is loaded
             lowercase_	:				List[Any]													=	'random'

             # make sure loaded weights match with hooks removed
             accelerator.load_state(__UpperCamelCase			)
             self.assertTrue(abs(model_signature - get_signature(__UpperCamelCase			)			) < 1e-3			)

             # mode.class_name is NOT loaded from config
             self.assertTrue(model.class_name != model.__class__.__name__			)
   def 			_UpperCAmelCase   (			self			)	->      Dict:


        '''simple docstring'''


        lowercase_	:				int													=	Accelerator()
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				int													=	create_components()
        lowercase_	:				List[str]													=	None

        # This should work
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				List[Any]													=	accelerator.prepare(
            __UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)
        self.assertTrue(dummy_obj is None			)
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Tuple													=	Accelerator()
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				Optional[int]													=	create_components()
        lowercase_	:				Dict													=	[1, 2, 3]

        # This should work
        lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_							,  lowercase_	:				Union[str, Any]													=	accelerator.prepare(
            __UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)
        self.assertEqual(
            getattr(__UpperCamelCase     ,'_is_accelerate_prepared'     ,__UpperCamelCase			)     ,__UpperCamelCase     ,'Dummy object should have `_is_accelerate_prepared` set to `True`'     ,)
        self.assertEqual(
            getattr(__UpperCamelCase     ,'_is_accelerate_prepared'     ,__UpperCamelCase			)     ,__UpperCamelCase     ,'Model is missing `_is_accelerator_prepared` or is set to `False`'     ,)
        self.assertEqual(
            getattr(__UpperCamelCase     ,'_is_accelerate_prepared'     ,__UpperCamelCase			)     ,__UpperCamelCase     ,'Optimizer is missing `_is_accelerator_prepared` or is set to `False`'     ,)
        self.assertEqual(
            getattr(__UpperCamelCase     ,'_is_accelerate_prepared'     ,__UpperCamelCase			)     ,__UpperCamelCase     ,'Scheduler is missing `_is_accelerator_prepared` or is set to `False`'     ,)
        self.assertEqual(
            getattr(__UpperCamelCase     ,'_is_accelerate_prepared'     ,__UpperCamelCase			)     ,__UpperCamelCase     ,'Train Dataloader is missing `_is_accelerator_prepared` or is set to `False`'     ,)
        self.assertEqual(
            getattr(__UpperCamelCase     ,'_is_accelerate_prepared'     ,__UpperCamelCase			)     ,__UpperCamelCase     ,'Valid Dataloader is missing `_is_accelerator_prepared` or is set to `False`'     ,)
   @slow
   @require_bnb
   def 			_UpperCAmelCase   (			self			)	->      str:


        '''simple docstring'''


        from transformers import AutoModelForCausalLM

        lowercase_	:				Union[str, Any]													=	AutoModelForCausalLM.from_pretrained(
            'EleutherAI/gpt-neo-125m'     ,load_in_abit=__UpperCamelCase     ,device_map={'': 0}     ,)
        lowercase_	:				List[Any]													=	Accelerator()

        # This should work
        lowercase_	:				Optional[int]													=	accelerator.prepare(__UpperCamelCase			)
   @slow
   @require_bnb
   def 			_UpperCAmelCase   (			self			)	->      Dict:


        '''simple docstring'''


        from transformers import AutoModelForCausalLM

        lowercase_	:				Optional[Any]													=	Accelerator()

        with init_empty_weights():
             lowercase_	:				Union[str, Any]													=	AutoModelForCausalLM.from_pretrained(
                 'EleutherAI/gpt-neo-125m'     ,)
             model.tie_weights()
             lowercase_	:				str													=	infer_auto_device_map(__UpperCamelCase			)
             lowercase_	:				str													=	'cpu'

        lowercase_	:				Any													=	AutoModelForCausalLM.from_pretrained(
            'EleutherAI/gpt-neo-125m'     ,device_map=__UpperCamelCase     ,load_in_abit=__UpperCamelCase     ,llm_inta_enable_fpaa_cpu_offload=__UpperCamelCase			)

        # This should not work and get value error
        with self.assertRaises(__UpperCamelCase			):
             lowercase_	:				List[Any]													=	accelerator.prepare(__UpperCamelCase			)
   @slow
   @require_bnb
   @require_multi_gpu
   def 			_UpperCAmelCase   (			self			)	->      List[str]:


        '''simple docstring'''


        from transformers import AutoModelForCausalLM

        lowercase_	:				Optional[Any]													=	{'distributed_type': DistributedType.MULTI_GPU}

        with init_empty_weights():
             lowercase_	:				Optional[Any]													=	AutoModelForCausalLM.from_pretrained(
                 'EleutherAI/gpt-neo-125m'     ,)
             model.tie_weights()
             lowercase_	:				Union[str, Any]													=	infer_auto_device_map(__UpperCamelCase			)
             lowercase_	:				List[str]													=	1

        lowercase_	:				int													=	AutoModelForCausalLM.from_pretrained(
            'EleutherAI/gpt-neo-125m'     ,load_in_abit=__UpperCamelCase     ,device_map=__UpperCamelCase     ,)
        lowercase_	:				str													=	Accelerator()

        # This should not work and get value error
        with self.assertRaises(__UpperCamelCase			):
             lowercase_	:				int													=	accelerator.prepare(__UpperCamelCase			)

        PartialState._reset_state()
   @slow
   @require_bnb
   @require_multi_gpu
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        from transformers import AutoModelForCausalLM

        with init_empty_weights():
             lowercase_	:				List[str]													=	AutoModelForCausalLM.from_pretrained(
                 'EleutherAI/gpt-neo-125m'     ,)
             lowercase_	:				str													=	infer_auto_device_map(__UpperCamelCase			)
             lowercase_	:				Tuple													=	1

        lowercase_	:				List[Any]													=	AutoModelForCausalLM.from_pretrained(
            'EleutherAI/gpt-neo-125m'     ,load_in_abit=__UpperCamelCase     ,device_map=__UpperCamelCase     ,)
        lowercase_	:				Optional[Any]													=	Accelerator()

        # This should work
        lowercase_	:				Optional[Any]													=	accelerator.prepare(__UpperCamelCase			)







   @require_cuda
   def 			_UpperCAmelCase   (			self			)	->      str:


        '''simple docstring'''


        lowercase_	:				Union[str, Any]													=	torch.nn.Linear(10     ,10			)
        lowercase_	:				Tuple													=	torch.optim.SGD(model.parameters()     ,lr=0.01			)
        lowercase_	:				Optional[Any]													=	Accelerator(cpu=__UpperCamelCase			)
        lowercase_	:				str													=	accelerator.prepare(__UpperCamelCase			)


"""simple docstring"""







import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
       from PIL import Image
else:







       class        UpperCamelCase		:
          @staticmethod
          def 			_UpperCAmelCase   (			*__UpperCamelCase     ,**__UpperCamelCase			)	->      Union[str, Any]:


               '''simple docstring'''


               pass







@is_pipeline_test
@require_vision
@require_torch
class        UpperCamelCase		(			unittest.TestCase  ):
   lowercase							=  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				int													=	pipeline(
            'zero-shot-object-detection'     ,model='hf-internal-testing/tiny-random-owlvit-object-detection'			)

        lowercase_	:				Optional[int]													=	[
            {
                'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                'candidate_labels': ['cat', 'remote', 'couch'],
            }
        ]
        return object_detector, examples
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      str:


        '''simple docstring'''


        lowercase_	:				Tuple													=	object_detector(examples[0]     ,threshold=0.0			)

        lowercase_	:				Any													=	len(__UpperCamelCase			)
        self.assertGreater(__UpperCamelCase     ,0			)
        self.assertEqual(
            __UpperCamelCase     ,[
                {
                    'score': ANY(__UpperCamelCase			),
                    'label': ANY(__UpperCamelCase			),
                    'box': {'xmin': ANY(__UpperCamelCase			), 'ymin': ANY(__UpperCamelCase			), 'xmax': ANY(__UpperCamelCase			), 'ymax': ANY(__UpperCamelCase			)},
                }
                for i in range(__UpperCamelCase			)
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        pass
   @require_torch
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	pipeline(
            'zero-shot-object-detection'     ,model='hf-internal-testing/tiny-random-owlvit-object-detection'			)

        lowercase_	:				Optional[int]													=	object_detector(
            './tests/fixtures/tests_samples/COCO/000000039769.png'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	object_detector(
            [
                {
                    'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                }
            ]     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                    {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                    {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                ]
            ]     ,)
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[str]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	object_detector(
            [
                {
                    'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                },
                {
                    'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                },
            ]     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


        pass
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


        lowercase_	:				Union[str, Any]													=	0.2
        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				str													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
            ]     ,)







   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Tuple													=	2
        lowercase_	:				Optional[Any]													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[Any]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,top_k=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
            ]     ,)

