import inspect
import os

import torch
from transformers import AutoModel
from transformers.testing_utils import mockenv_context
from transformers.trainer_utils import set_seed

import accelerate
from accelerate.accelerator import Accelerator
from accelerate.state import AcceleratorState
from accelerate.test_utils.testing import (
    AccelerateTestCase,
    TempDirTestCase,
    execute_subprocess_async,
    require_cuda,
    require_fsdp,
    require_multi_gpu,
    slow,
)
from accelerate.utils.constants import (
    FSDP_AUTO_WRAP_POLICY,
    FSDP_BACKWARD_PREFETCH,
    FSDP_SHARDING_STRATEGY,
    FSDP_STATE_DICT_TYPE,
)
from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin
from accelerate.utils.other import patch_environment


set_seed(42)

__UpperCamelCase		:       Union[str, Any]									=     'bert-base-cased'
__UpperCamelCase		:       Dict									=     'fp16'
__UpperCamelCase		:       int									=     'bf16'
__UpperCamelCase		:       Optional[Any]									=     [FPaa, BFaa]





@require_fsdp
@require_cuda
class 					lowercase__						(   UpperCamelCase_):


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												super().setUp()

												SCREAMING_SNAKE_CASE							:    Tuple							=							dict(
												    ACCELERATE_USE_FSDP='''true'''    ,		MASTER_ADDR='''localhost'''    ,		MASTER_PORT='''10999'''    ,		RANK='''0'''    ,		LOCAL_RANK='''0'''    ,		WORLD_SIZE='''1'''    ,		)


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy

												for i, strategy in enumerate(UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.dist_env.copy()
																		SCREAMING_SNAKE_CASE							:    Any							=							f"""{i + 1}"""
																		SCREAMING_SNAKE_CASE							:    str							=							strategy
																		with mockenv_context(**UpperCamelCase__						):
																								SCREAMING_SNAKE_CASE							:    Dict							=							FullyShardedDataParallelPlugin()
																								self.assertEqual(fsdp_plugin.sharding_strategy    ,		ShardingStrategy(i + 1						)						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch

												for i, prefetch_policy in enumerate(UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    str							=							self.dist_env.copy()
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							prefetch_policy
																		with mockenv_context(**UpperCamelCase__						):
																								SCREAMING_SNAKE_CASE							:    Dict							=							FullyShardedDataParallelPlugin()
																								if prefetch_policy == "NO_PREFETCH":
																														self.assertIsNone(fsdp_plugin.backward_prefetch						)
																								else:
																														self.assertEqual(fsdp_plugin.backward_prefetch    ,		BackwardPrefetch(i + 1						)						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType

												for i, state_dict_type in enumerate(UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    str							=							self.dist_env.copy()
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							state_dict_type
																		with mockenv_context(**UpperCamelCase__						):
																								SCREAMING_SNAKE_CASE							:    List[str]							=							FullyShardedDataParallelPlugin()
																								self.assertEqual(fsdp_plugin.state_dict_type    ,		StateDictType(i + 1						)						)
																								if state_dict_type == "FULL_STATE_DICT":
																														self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu						)
																														self.assertTrue(fsdp_plugin.state_dict_config.ranka_only						)


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							AutoModel.from_pretrained(UpperCamelCase__						)
												for policy in FSDP_AUTO_WRAP_POLICY:
																		SCREAMING_SNAKE_CASE							:    Any							=							self.dist_env.copy()
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							policy
																		if policy == "TRANSFORMER_BASED_WRAP":
																								SCREAMING_SNAKE_CASE							:    List[Any]							=							'''BertLayer'''
																		elif policy == "SIZE_BASED_WRAP":
																								SCREAMING_SNAKE_CASE							:    List[Any]							=							'''2000'''
																		with mockenv_context(**UpperCamelCase__						):
																								SCREAMING_SNAKE_CASE							:    Optional[Any]							=							FullyShardedDataParallelPlugin()
																								fsdp_plugin.set_auto_wrap_policy(UpperCamelCase__						)
																								if policy == "NO_WRAP":
																														self.assertIsNone(fsdp_plugin.auto_wrap_policy						)
																								else:
																														self.assertIsNotNone(fsdp_plugin.auto_wrap_policy						)

												SCREAMING_SNAKE_CASE							:    Tuple							=							self.dist_env.copy()
												SCREAMING_SNAKE_CASE							:    int							=							'''TRANSFORMER_BASED_WRAP'''
												SCREAMING_SNAKE_CASE							:    Dict							=							'''T5Layer'''
												with mockenv_context(**UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							FullyShardedDataParallelPlugin()
																		with self.assertRaises(UpperCamelCase__						) as cm:
																								fsdp_plugin.set_auto_wrap_policy(UpperCamelCase__						)
																		self.assertTrue('''Could not find the transformer layer class to wrap in the model.''' in str(cm.exception						)						)

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.dist_env.copy()
												SCREAMING_SNAKE_CASE							:    str							=							'''SIZE_BASED_WRAP'''
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							'''0'''
												with mockenv_context(**UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							FullyShardedDataParallelPlugin()
																		fsdp_plugin.set_auto_wrap_policy(UpperCamelCase__						)
																		self.assertIsNone(fsdp_plugin.auto_wrap_policy						)


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision
												from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler

												for mp_dtype in dtypes:
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.dist_env.copy()
																		SCREAMING_SNAKE_CASE							:    Tuple							=							mp_dtype
																		with mockenv_context(**UpperCamelCase__						):
																								SCREAMING_SNAKE_CASE							:    str							=							Accelerator()
																								if mp_dtype == "fp16":
																														SCREAMING_SNAKE_CASE							:    str							=							torch.floataa
																								elif mp_dtype == "bf16":
																														SCREAMING_SNAKE_CASE							:    Optional[int]							=							torch.bfloataa
																								SCREAMING_SNAKE_CASE							:    str							=							MixedPrecision(param_dtype=UpperCamelCase__    ,		reduce_dtype=UpperCamelCase__    ,		buffer_dtype=UpperCamelCase__						)
																								self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy    ,		UpperCamelCase__						)
																								if mp_dtype == FPaa:
																														self.assertTrue(isinstance(accelerator.scaler    ,		UpperCamelCase__						)						)
																								elif mp_dtype == BFaa:
																														self.assertIsNone(accelerator.scaler						)
																								AcceleratorState._reset_state(UpperCamelCase__						)



						def        __A       (       self	:				str						):





												'''simple docstring'''





												from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload

												for flag in [True, False]:
																		SCREAMING_SNAKE_CASE							:    Dict							=							self.dist_env.copy()
																		SCREAMING_SNAKE_CASE							:    Dict							=							str(UpperCamelCase__						).lower()
																		with mockenv_context(**UpperCamelCase__						):
																								SCREAMING_SNAKE_CASE							:    int							=							FullyShardedDataParallelPlugin()
																								self.assertEqual(fsdp_plugin.cpu_offload    ,		CPUOffload(offload_params=UpperCamelCase__						)						)






@require_fsdp
@require_multi_gpu
@slow
class 					lowercase__						(   UpperCamelCase_):


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												super().setUp()
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							0.82
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							[
												    '''fsdp_shard_grad_op_transformer_based_wrap''',
												    '''fsdp_full_shard_transformer_based_wrap''',
												]
												SCREAMING_SNAKE_CASE							:    str							=							{
												    '''multi_gpu_fp16''': 3200,
												    '''fsdp_shard_grad_op_transformer_based_wrap_fp16''': 2000,
												    '''fsdp_full_shard_transformer_based_wrap_fp16''': 1900,
												    # Disabling below test as it overwhelms the RAM memory usage
												    # on CI self-hosted runner leading to tests getting killed.
												    # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500,  # fp16 was leading to indefinite hang
												}
												SCREAMING_SNAKE_CASE							:    List[Any]							=							160
												SCREAMING_SNAKE_CASE							:    str							=							160

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							inspect.getfile(accelerate.test_utils						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							os.path.sep.join(mod_file.split(os.path.sep						)[:-1] + ['''scripts''', '''external_deps''']						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							os.path.join(self.test_scripts_folder    ,		'''test_performance.py'''						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							['''accelerate''', '''launch''', '''--num_processes=2''', '''--num_machines=1''', '''--machine_rank=0''', '''--use_fsdp''']
												for config in self.performance_configs:
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							cmd.copy()
																		for i, strategy in enumerate(UpperCamelCase__						):
																								if strategy.lower() in config:
																														cmd_config.append(f"""--fsdp_sharding_strategy={i+1}"""						)
																														break

																		if "fp32" in config:
																								cmd_config.append('''--mixed_precision=no'''						)
																		else:
																								cmd_config.append('''--mixed_precision=fp16'''						)

																		if "cpu_offload" in config:
																								cmd_config.append('''--fsdp_offload_params=True'''						)

																		for policy in FSDP_AUTO_WRAP_POLICY:
																								if policy.lower() in config:
																														cmd_config.append(f"""--fsdp_auto_wrap_policy={policy}"""						)
																														break

																		if policy == "TRANSFORMER_BASED_WRAP":
																								cmd_config.append('''--fsdp_transformer_layer_cls_to_wrap=BertLayer'''						)
																		elif policy == "SIZE_BASED_WRAP":
																								cmd_config.append('''--fsdp_min_num_params=2000'''						)

																		cmd_config.extend(
																		    [
																		        self.test_file_path,
																		        f"""--output_dir={self.tmpdir}""",
																		        f"""--performance_lower_bound={self.performance_lower_bound}""",
																		    ]						)
																		with patch_environment(omp_num_threads=1						):
																								execute_subprocess_async(UpperCamelCase__    ,		env=os.environ.copy()						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							os.path.join(self.test_scripts_folder    ,		'''test_checkpointing.py'''						)
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							[
												    '''accelerate''',
												    '''launch''',
												    '''--num_processes=2''',
												    '''--num_machines=1''',
												    '''--machine_rank=0''',
												    '''--use_fsdp''',
												    '''--mixed_precision=fp16''',
												    '''--fsdp_transformer_layer_cls_to_wrap=BertLayer''',
												]

												for i, strategy in enumerate(UpperCamelCase__						):
																		SCREAMING_SNAKE_CASE							:    List[str]							=							cmd.copy()
																		cmd_config.append(f"""--fsdp_sharding_strategy={i+1}"""						)
																		if strategy != "FULL_SHARD":
																								continue
																		SCREAMING_SNAKE_CASE							:    int							=							len(UpperCamelCase__						)
																		for state_dict_type in FSDP_STATE_DICT_TYPE:
																								SCREAMING_SNAKE_CASE							:    List[Any]							=							cmd_config[:state_dict_config_index]
																								cmd_config.append(f"""--fsdp_state_dict_type={state_dict_type}"""						)
																								cmd_config.extend(
																								    [
																								        self.test_file_path,
																								        f"""--output_dir={self.tmpdir}""",
																								        '''--partial_train_epoch=1''',
																								    ]						)
																								with patch_environment(omp_num_threads=1						):
																														execute_subprocess_async(UpperCamelCase__    ,		env=os.environ.copy()						)

																								SCREAMING_SNAKE_CASE							:    List[str]							=							cmd_config[:-1]
																								SCREAMING_SNAKE_CASE							:    Tuple							=							os.path.join(self.tmpdir    ,		'''epoch_0'''						)
																								cmd_config.extend(
																								    [
																								        f"""--resume_from_checkpoint={resume_from_checkpoint}""",
																								    ]						)
																								with patch_environment(omp_num_threads=1						):
																														execute_subprocess_async(UpperCamelCase__    ,		env=os.environ.copy()						)



						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							os.path.join(self.test_scripts_folder    ,		'''test_peak_memory_usage.py'''						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							[
												    '''accelerate''',
												    '''launch''',
												    '''--num_processes=2''',
												    '''--num_machines=1''',
												    '''--machine_rank=0''',
												]
												for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items():
																		SCREAMING_SNAKE_CASE							:    int							=							cmd.copy()
																		if "fp16" in spec:
																								cmd_config.extend(['''--mixed_precision=fp16''']						)
																		else:
																								cmd_config.extend(['''--mixed_precision=no''']						)

																		if "multi_gpu" in spec:
																								continue
																		else:
																								cmd_config.extend(['''--use_fsdp''']						)
																								for i, strategy in enumerate(UpperCamelCase__						):
																														if strategy.lower() in spec:
																																				cmd_config.append(f"""--fsdp_sharding_strategy={i+1}"""						)
																																				break

																								if "cpu_offload" in spec:
																														cmd_config.append('''--fsdp_offload_params=True'''						)

																								for policy in FSDP_AUTO_WRAP_POLICY:
																														if policy.lower() in spec:
																																				cmd_config.append(f"""--fsdp_auto_wrap_policy={policy}"""						)
																																				break

																								if policy == "TRANSFORMER_BASED_WRAP":
																														cmd_config.append('''--fsdp_transformer_layer_cls_to_wrap=BertLayer'''						)
																								elif policy == "SIZE_BASED_WRAP":
																														cmd_config.append('''--fsdp_min_num_params=2000'''						)

																		cmd_config.extend(
																		    [
																		        self.test_file_path,
																		        f"""--output_dir={self.tmpdir}""",
																		        f"""--peak_memory_upper_bound={peak_mem_upper_bound}""",
																		        f"""--n_train={self.n_train}""",
																		        f"""--n_val={self.n_val}""",
																		    ]						)
																		with patch_environment(omp_num_threads=1						):
																								execute_subprocess_async(UpperCamelCase__    ,		env=os.environ.copy()						)






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


											@staticmethod
											def        __A       (       *UpperCamelCase__	:				Union[str, Any]    ,		**UpperCamelCase__	:				Optional[Any]						):





																	'''simple docstring'''





																	pass





@is_pipeline_test
@require_vision
@require_torch
class 					lowercase__						(   unittest.TestCase):
						UpperCamelCase_     =		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING


						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





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

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


						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							object_detector(examples[0]    ,		threshold=0.0						)

												SCREAMING_SNAKE_CASE							:    Tuple							=							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        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												pass


						@require_torch
						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							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(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}},
												    ]    ,		)

												SCREAMING_SNAKE_CASE							:    str							=							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        __A       (       self	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							pipeline('''zero-shot-object-detection'''						)

												SCREAMING_SNAKE_CASE							:    Optional[int]							=							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}},
												    ]    ,		)

												SCREAMING_SNAKE_CASE							:    int							=							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        __A       (       self	:				str						):





												'''simple docstring'''





												pass


						@require_torch
						@slow
						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							0.2
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							pipeline('''zero-shot-object-detection'''						)

												SCREAMING_SNAKE_CASE							:    Dict							=							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        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							2
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							pipeline('''zero-shot-object-detection'''						)

												SCREAMING_SNAKE_CASE							:    List[str]							=							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}},
												    ]    ,		)





