


import inspect
import unittest

import torch
import torch.nn as nn

from accelerate.hooks import (
    AlignDevicesHook,
    ModelHook,
    SequentialHook,
    add_hook_to_module,
    attach_align_device_hook,
    remove_hook_from_module,
    remove_hook_from_submodules,
)
from accelerate.test_utils import require_multi_gpu







class 	__lowercase    (nn.Module  ):





							"""simple docstring"""







							def __init__(      self       ):





										"""simple docstring"""




										super().__init__()
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					nn.Linear(3			,						4       )
										SCREAMING_SNAKE_CASE_		:     int					=					nn.BatchNormad(4       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					nn.Linear(4			,						5       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__       ):





										"""simple docstring"""




										return self.lineara(self.batchnorm(self.lineara(lowerCAmelCase__       )       )       )







class 	__lowercase    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						*lowerCAmelCase__			,						**lowerCAmelCase__       ):





										"""simple docstring"""




										return (args[0] + 1,) + args[1:], kwargs







class 	__lowercase    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										return output + 1







class 	__lowercase    (unittest.TestCase  ):





							"""simple docstring"""







							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					ModelForTest()
										SCREAMING_SNAKE_CASE_		:     List[str]					=					ModelHook()

										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )
										self.assertEqual(test_model._hf_hook			,						lowerCAmelCase__       )
										self.assertTrue(hasattr(lowerCAmelCase__			,						'_old_forward'       )       )

										# Check adding the hook did not change the name or the signature
										self.assertEqual(test_model.forward.__name__			,						'forward'       )
										self.assertListEqual(list(inspect.signature(test_model.forward       ).parameters       )			,						['x']       )

										remove_hook_from_module(lowerCAmelCase__       )
										self.assertFalse(hasattr(lowerCAmelCase__			,						'_hf_hook'       )       )
										self.assertFalse(hasattr(lowerCAmelCase__			,						'_old_forward'       )       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					ModelForTest()
										SCREAMING_SNAKE_CASE_		:     str					=					ModelHook()

										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__			,						append=lowerCAmelCase__       )

										self.assertEqual(isinstance(test_model._hf_hook			,						lowerCAmelCase__       )			,						lowerCAmelCase__       )
										self.assertEqual(len(test_model._hf_hook.hooks       )			,						2       )
										self.assertTrue(hasattr(lowerCAmelCase__			,						'_old_forward'       )       )

										# Check adding the hook did not change the name or the signature
										self.assertEqual(test_model.forward.__name__			,						'forward'       )
										self.assertListEqual(list(inspect.signature(test_model.forward       ).parameters       )			,						['x']       )

										remove_hook_from_module(lowerCAmelCase__       )
										self.assertFalse(hasattr(lowerCAmelCase__			,						'_hf_hook'       )       )
										self.assertFalse(hasattr(lowerCAmelCase__			,						'_old_forward'       )       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					ModelForTest()
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     Any					=					test_model(x + 1       )
										SCREAMING_SNAKE_CASE_		:     List[str]					=					test_model(x + 2       )

										SCREAMING_SNAKE_CASE_		:     Dict					=					PreForwardHook()
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					test_model(lowerCAmelCase__       )
										self.assertTrue(torch.allclose(lowerCAmelCase__			,						lowerCAmelCase__			,						atol=1E-5       )       )

										# Attaching a hook to a model when it already has one replaces, does not chain
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					PreForwardHook()
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					test_model(lowerCAmelCase__       )
										self.assertTrue(torch.allclose(lowerCAmelCase__			,						lowerCAmelCase__			,						atol=1E-5       )       )

										# You need to use the sequential hook to chain two or more hooks
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					SequentialHook(PreForwardHook()			,						PreForwardHook()       )
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     List[Any]					=					test_model(lowerCAmelCase__       )
										assert torch.allclose(lowerCAmelCase__			,						lowerCAmelCase__			,						atol=1E-5       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					ModelForTest()
										SCREAMING_SNAKE_CASE_		:     int					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     int					=					test_model(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					PostForwardHook()
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					test_model(lowerCAmelCase__       )
										self.assertTrue(torch.allclose(lowerCAmelCase__			,						output + 1			,						atol=1E-5       )       )

										# Attaching a hook to a model when it already has one replaces, does not chain
										SCREAMING_SNAKE_CASE_		:     Dict					=					PostForwardHook()
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     int					=					test_model(lowerCAmelCase__       )
										self.assertTrue(torch.allclose(lowerCAmelCase__			,						output + 1			,						atol=1E-5       )       )

										# You need to use the sequential hook to chain two or more hooks
										SCREAMING_SNAKE_CASE_		:     Any					=					SequentialHook(PostForwardHook()			,						PostForwardHook()       )
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					test_model(lowerCAmelCase__       )
										assert torch.allclose(lowerCAmelCase__			,						output + 2			,						atol=1E-5       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					ModelForTest()
										SCREAMING_SNAKE_CASE_		:     Tuple					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					test_model(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     int					=					PostForwardHook()
										add_hook_to_module(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Any					=					test_model(lowerCAmelCase__       )
										self.assertTrue(torch.allclose(lowerCAmelCase__			,						output + 1       )       )
										self.assertTrue(outputa.requires_grad       )

										SCREAMING_SNAKE_CASE_		:     Tuple					=					True
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					test_model(lowerCAmelCase__       )
										self.assertFalse(outputa.requires_grad       )






							@require_multi_gpu
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					ModelForTest()
										# Everything is on CPU
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )

										# This will move each submodule on different devices
										add_hook_to_module(model.lineara			,						AlignDevicesHook(execution_device=0       )       )
										add_hook_to_module(model.batchnorm			,						AlignDevicesHook(execution_device=0       )       )
										add_hook_to_module(model.lineara			,						AlignDevicesHook(execution_device=1       )       )

										self.assertEqual(model.lineara.weight.device			,						torch.device(0       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device(0       )       )
										self.assertEqual(model.batchnorm.running_mean.device			,						torch.device(0       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device(1       )       )

										# We can still make a forward pass. The input does not need to be on any particular device
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     str					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						torch.device(1       )       )

										# We can add a general hook to put back output on same device as input.
										add_hook_to_module(lowerCAmelCase__			,						AlignDevicesHook(io_same_device=lowerCAmelCase__       )       )
										SCREAMING_SNAKE_CASE_		:     int					=					torch.randn(2			,						3       ).to(0       )
										SCREAMING_SNAKE_CASE_		:     int					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						torch.device(0       )       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					ModelForTest()

										# Everything is on CPU
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )

										# This will move each submodule on different devices
										SCREAMING_SNAKE_CASE_		:     int					=					{'execution_device': 0 if torch.cuda.is_available() else 'cpu', 'offload': True}

										add_hook_to_module(model.lineara			,						AlignDevicesHook(**lowerCAmelCase__       )       )
										add_hook_to_module(model.batchnorm			,						AlignDevicesHook(**lowerCAmelCase__       )       )
										add_hook_to_module(model.lineara			,						AlignDevicesHook(**lowerCAmelCase__       )       )

										# Parameters have been offloaded, so on the meta device
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										# Buffers are not included in the offload by default, so are on the execution device
										SCREAMING_SNAKE_CASE_		:     str					=					torch.device(hook_kwargs['execution_device']       )
										self.assertEqual(model.batchnorm.running_mean.device			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Tuple					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     List[str]					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						lowerCAmelCase__       )

										# Removing hooks loads back the weights in the model.
										remove_hook_from_module(model.lineara       )
										remove_hook_from_module(model.batchnorm       )
										remove_hook_from_module(model.lineara       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )

										# Now test with buffers included in the offload
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					{
										    'execution_device': 0 if torch.cuda.is_available() else 'cpu',
										    'offload': True,
										    'offload_buffers': True,
										}

										add_hook_to_module(model.lineara			,						AlignDevicesHook(**lowerCAmelCase__       )       )
										add_hook_to_module(model.batchnorm			,						AlignDevicesHook(**lowerCAmelCase__       )       )
										add_hook_to_module(model.lineara			,						AlignDevicesHook(**lowerCAmelCase__       )       )

										# Parameters have been offloaded, so on the meta device, buffers included
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.running_mean.device			,						torch.device('meta'       )       )

										SCREAMING_SNAKE_CASE_		:     Tuple					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						lowerCAmelCase__       )

										# Removing hooks loads back the weights in the model.
										remove_hook_from_module(model.lineara       )
										remove_hook_from_module(model.batchnorm       )
										remove_hook_from_module(model.lineara       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					ModelForTest()

										# Everything is on CPU
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )

										# This will move each submodule on different devices
										SCREAMING_SNAKE_CASE_		:     str					=					0 if torch.cuda.is_available() else 'cpu'
										attach_align_device_hook(lowerCAmelCase__			,						execution_device=lowerCAmelCase__			,						offload=lowerCAmelCase__       )

										# Parameters have been offloaded, so on the meta device
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										# Buffers are not included in the offload by default, so are on the execution device
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					torch.device(lowerCAmelCase__       )
										self.assertEqual(model.batchnorm.running_mean.device			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						lowerCAmelCase__       )

										# Removing hooks loads back the weights in the model.
										remove_hook_from_submodules(lowerCAmelCase__       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )

										# Now test with buffers included in the offload
										attach_align_device_hook(lowerCAmelCase__			,						execution_device=lowerCAmelCase__			,						offload=lowerCAmelCase__			,						offload_buffers=lowerCAmelCase__       )

										# Parameters have been offloaded, so on the meta device, buffers included
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.running_mean.device			,						torch.device('meta'       )       )

										SCREAMING_SNAKE_CASE_		:     int					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						lowerCAmelCase__       )

										# Removing hooks loads back the weights in the model.
										remove_hook_from_submodules(lowerCAmelCase__       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					ModelForTest()

										# Everything is on CPU
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )

										# This will move each submodule on different devices
										SCREAMING_SNAKE_CASE_		:     Dict					=					0 if torch.cuda.is_available() else 'cpu'
										attach_align_device_hook(
										    lowerCAmelCase__			,						execution_device=lowerCAmelCase__			,						offload=lowerCAmelCase__			,						weights_map=model.state_dict()       )

										# Parameters have been offloaded, so on the meta device
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										# Buffers are not included in the offload by default, so are on the execution device
										SCREAMING_SNAKE_CASE_		:     Dict					=					torch.device(lowerCAmelCase__       )
										self.assertEqual(model.batchnorm.running_mean.device			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     List[str]					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						lowerCAmelCase__       )

										# Removing hooks loads back the weights in the model.
										remove_hook_from_submodules(lowerCAmelCase__       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )

										# Now test with buffers included in the offload
										attach_align_device_hook(
										    lowerCAmelCase__			,						execution_device=lowerCAmelCase__			,						offload=lowerCAmelCase__			,						weights_map=model.state_dict()			,						offload_buffers=lowerCAmelCase__			,						)

										# Parameters have been offloaded, so on the meta device, buffers included
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('meta'       )       )
										self.assertEqual(model.batchnorm.running_mean.device			,						torch.device('meta'       )       )

										SCREAMING_SNAKE_CASE_		:     int					=					torch.randn(2			,						3       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					model(lowerCAmelCase__       )
										self.assertEqual(output.device			,						lowerCAmelCase__       )

										# Removing hooks loads back the weights in the model.
										remove_hook_from_submodules(lowerCAmelCase__       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.batchnorm.weight.device			,						torch.device('cpu'       )       )
										self.assertEqual(model.lineara.weight.device			,						torch.device('cpu'       )       )









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





											"""simple docstring"""







											@staticmethod
											def 					UpperCamelCase__     (      *lowerCAmelCase__			,						**lowerCAmelCase__       ):





														"""simple docstring"""




														pass







@is_pipeline_test
@require_vision
@require_torch
class 	__lowercase    (unittest.TestCase  ):





							"""simple docstring"""







							_UpperCAmelCase      =						MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




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

										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					[
										    {
										        'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
										        'candidate_labels': ['cat', 'remote', 'couch'],
										    }
										]
										return object_detector, examples






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					object_detector(examples[0]			,						threshold=0.0       )

										SCREAMING_SNAKE_CASE_		:     int					=					len(lowerCAmelCase__       )
										self.assertGreater(lowerCAmelCase__			,						0       )
										self.assertEqual(
										    lowerCAmelCase__			,						[
										        {
										            'score': ANY(lowerCAmelCase__       ),
										            'label': ANY(lowerCAmelCase__       ),
										            'box': {'xmin': ANY(lowerCAmelCase__       ), 'ymin': ANY(lowerCAmelCase__       ), 'xmax': ANY(lowerCAmelCase__       ), 'ymax': ANY(lowerCAmelCase__       )},
										        }
										        for i in range(lowerCAmelCase__       )
										    ]			,						)






							@require_tf
							@unittest.skip('Zero Shot Object Detection not implemented in TF'       )
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										pass






							@require_torch
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




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

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

										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,						decimals=4       )			,						[
										        {'score': 0.7_235, 'label': 'cat', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
										        {'score': 0.7_218, 'label': 'remote', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
										        {'score': 0.7_184, 'label': 'couch', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
										        {'score': 0.6_748, 'label': 'remote', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
										        {'score': 0.6_656, 'label': 'cat', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
										        {'score': 0.6_614, 'label': 'couch', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
										        {'score': 0.6_456, 'label': 'remote', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
										        {'score': 0.642, 'label': 'remote', 'box': {'xmin': 6_7, 'ymin': 2_7_4, 'xmax': 9_3, 'ymax': 2_9_7}},
										        {'score': 0.6_419, 'label': 'cat', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
										    ]			,						)

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

										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,						decimals=4       )			,						[
										        [
										            {'score': 0.7_235, 'label': 'cat', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
										            {'score': 0.7_218, 'label': 'remote', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
										            {'score': 0.7_184, 'label': 'couch', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
										            {'score': 0.6_748, 'label': 'remote', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
										            {'score': 0.6_656, 'label': 'cat', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
										            {'score': 0.6_614, 'label': 'couch', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
										            {'score': 0.6_456, 'label': 'remote', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
										            {'score': 0.642, 'label': 'remote', 'box': {'xmin': 6_7, 'ymin': 2_7_4, 'xmax': 9_3, 'ymax': 2_9_7}},
										            {'score': 0.6_419, 'label': 'cat', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
										        ]
										    ]			,						)






							@require_torch
							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[str]					=					pipeline('zero-shot-object-detection'       )

										SCREAMING_SNAKE_CASE_		:     List[str]					=					object_detector(
										    'http://images.cocodataset.org/val2017/000000039769.jpg'			,						candidate_labels=['cat', 'remote', 'couch']			,						)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,						decimals=4       )			,						[
										        {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
										        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
										        {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
										        {'score': 0.1_474, 'label': 'remote', 'box': {'xmin': 3_3_5, 'ymin': 7_4, 'xmax': 3_7_1, 'ymax': 1_8_7}},
										        {'score': 0.1_208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_4_2, 'ymax': 4_7_6}},
										    ]			,						)

										SCREAMING_SNAKE_CASE_		:     List[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(lowerCAmelCase__			,						decimals=4       )			,						[
										        [
										            {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
										            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
										            {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
										            {'score': 0.1_474, 'label': 'remote', 'box': {'xmin': 3_3_5, 'ymin': 7_4, 'xmax': 3_7_1, 'ymax': 1_8_7}},
										            {'score': 0.1_208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_4_2, 'ymax': 4_7_6}},
										        ],
										        [
										            {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
										            {'score': 0.277, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
										            {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
										            {'score': 0.1_474, 'label': 'remote', 'box': {'xmin': 3_3_5, 'ymin': 7_4, 'xmax': 3_7_1, 'ymax': 1_8_7}},
										            {'score': 0.1_208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_4_2, 'ymax': 4_7_6}},
										        ],
										    ]			,						)






							@require_tf
							@unittest.skip('Zero Shot Object Detection not implemented in TF'       )
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										pass






							@require_torch
							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					0.2
										SCREAMING_SNAKE_CASE_		:     str					=					pipeline('zero-shot-object-detection'       )

										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					object_detector(
										    'http://images.cocodataset.org/val2017/000000039769.jpg'			,						candidate_labels=['cat', 'remote', 'couch']			,						threshold=lowerCAmelCase__			,						)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,						decimals=4       )			,						[
										        {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
										        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
										        {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
										    ]			,						)






							@require_torch
							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					2
										SCREAMING_SNAKE_CASE_		:     int					=					pipeline('zero-shot-object-detection'       )

										SCREAMING_SNAKE_CASE_		:     Tuple					=					object_detector(
										    'http://images.cocodataset.org/val2017/000000039769.jpg'			,						candidate_labels=['cat', 'remote', 'couch']			,						top_k=lowerCAmelCase__			,						)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,						decimals=4       )			,						[
										        {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
										        {'score': 0.277, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
										    ]			,						)





