from copy import deepcopy

import torch
import torch.nn.functional as F
from torch.optim import AdamW
from torch.optim.lr_scheduler import LambdaLR
from torch.utils.data import DataLoader

from accelerate.accelerator import Accelerator
from accelerate.state import GradientState
from accelerate.test_utils import RegressionDataset, RegressionModel
from accelerate.utils import DistributedType, is_torch_version, set_seed
def       A	(    _lowercase		,  _lowercase		,  _lowercase		,  _lowercase						):
						for param, grad_param in zip(model_a.parameters()		,  model_b.parameters()						):
												if not param.requires_grad:
																		continue
												if not did_step:
																		# Grads should not be in sync
																		assert (
																		    torch.allclose(param.grad		,  grad_param.grad						) is False
																		), f"""Gradients in sync when they should not be at iteration {iteration}:\nmodel_a grad ({param.grad}) == model_b grad ({grad_param.grad})"""
												else:
																		# Grads should be in sync
																		assert (
																		    torch.allclose(param.grad		,  grad_param.grad						) is True
																		), f"""Gradients not in sync when they should be at iteration {iteration}:\nmodel_a grad ({param.grad}) != model_b grad ({grad_param.grad})"""
def       A	(    _lowercase		,  _lowercase		,  _lowercase		,  _lowercase		,  _lowercase=True						):
						model.train()
						SCREAMING_SNAKE_CASE							:    int							=							model(_lowercase						)
						SCREAMING_SNAKE_CASE							:    Tuple							=							F.mse_loss(_lowercase		,  target.to(output.device						)						)
						if not do_backward:
												loss /= accelerator.gradient_accumulation_steps
												loss.backward()
						else:
												accelerator.backward(_lowercase						)
def       A	(    _lowercase		,  _lowercase=False						):
						set_seed(42						)
						SCREAMING_SNAKE_CASE							:    Optional[int]							=							RegressionModel()
						SCREAMING_SNAKE_CASE							:    Any							=							deepcopy(_lowercase						)
						SCREAMING_SNAKE_CASE							:    Optional[Any]							=							RegressionDataset(length=80						)
						SCREAMING_SNAKE_CASE							:    Optional[Any]							=							DataLoader(_lowercase		,  batch_size=16						)
						model.to(accelerator.device						)
						if sched:
												SCREAMING_SNAKE_CASE							:    Dict							=							AdamW(params=model.parameters()		,  lr=1e-3						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							AdamW(params=ddp_model.parameters()		,  lr=1e-3						)
												SCREAMING_SNAKE_CASE							:    int							=							LambdaLR(_lowercase		,  lr_lambda=lambda _lowercase						: epoch**0.65						)
												SCREAMING_SNAKE_CASE							:    Dict							=							LambdaLR(_lowercase		,  lr_lambda=lambda _lowercase						: epoch**0.65						)
						# Make a copy of `model`
						if sched:
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Any							=							accelerator.prepare(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
						else:
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Any							=							accelerator.prepare(_lowercase		,  _lowercase						)
						if sched:
												return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched)
						return model, ddp_model, dataloader
def       A	(    _lowercase						):
						# Test when on a single CPU or GPU that the context manager does nothing
						SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Optional[Any]							=							get_training_setup(_lowercase						)
						# Use a single batch
						SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    List[str]							=							next(iter(_lowercase						)						).values()
						for iteration in range(3						):
												# Gather the distributed inputs and targs for the base model
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Optional[int]							=							accelerator.gather((ddp_input, ddp_target)						)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    List[Any]							=							input.to(accelerator.device						), target.to(accelerator.device						)
												# Perform our initial ground truth step in non "DDP"
												step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												# Do "gradient accumulation" (noop)
												if iteration % 2 == 0:
																		# Accumulate grads locally
																		with accelerator.no_sync(_lowercase						):
																								step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												else:
																		# Sync grads
																		step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)

												# Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync
												check_model_parameters(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												for param, ddp_param in zip(model.parameters()		,  ddp_model.parameters()						):
																		if not param.requires_grad:
																								continue
																		assert torch.allclose(
																		    param.grad		,  ddp_param.grad						), f"""Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})"""

												# Shuffle ddp_input on each iteration
												torch.manual_seed(1_337 + iteration						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							ddp_input[torch.randperm(len(_lowercase						)						)]
def       A	(    _lowercase						):
						# Test on distributed setup that context manager behaves properly
						SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    int							=							get_training_setup(_lowercase						)
						# Use a single batch
						SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							next(iter(_lowercase						)						).values()
						for iteration in range(3						):
												# Gather the distributed inputs and targs for the base model
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Any							=							accelerator.gather((ddp_input, ddp_target)						)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Optional[int]							=							input.to(accelerator.device						), target.to(accelerator.device						)
												# Perform our initial ground truth step in non "DDP"
												step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												# Do "gradient accumulation" (noop)
												if iteration % 2 == 0:
																		# Accumulate grads locally
																		with accelerator.no_sync(_lowercase						):
																								step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												else:
																		# Sync grads
																		step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)

												# DDP model and model should only be in sync when not (iteration % 2 == 0)
												for param, ddp_param in zip(model.parameters()		,  ddp_model.parameters()						):
																		if not param.requires_grad:
																								continue
																		if iteration % 2 == 0:
																								# Grads should not be in sync
																								assert (
																								    torch.allclose(param.grad		,  ddp_param.grad						) is False
																								), f"""Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})"""
																		else:
																								# Grads should be in sync
																								assert (
																								    torch.allclose(param.grad		,  ddp_param.grad						) is True
																								), f"""Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})"""

        # Shuffle ddp_input on each iteration
												torch.manual_seed(1_337 + iteration						)
												SCREAMING_SNAKE_CASE							:    int							=							ddp_input[torch.randperm(len(_lowercase						)						)]
def       A	(    _lowercase=False		,  _lowercase=False						):
						SCREAMING_SNAKE_CASE							:    List[str]							=							Accelerator(
						    split_batches=_lowercase		,  dispatch_batches=_lowercase		,  gradient_accumulation_steps=2						)
						# Test that context manager behaves properly
						SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Optional[Any]							=							get_training_setup(_lowercase						)
						for iteration, batch in enumerate(_lowercase						):
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    List[Any]							=							batch.values()
												# Gather the distributed inputs and targs for the base model
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Any							=							accelerator.gather((ddp_input, ddp_target)						)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Tuple							=							input.to(accelerator.device						), target.to(accelerator.device						)
												# Perform our initial ground truth step in non "DDP"
												step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												# Do "gradient accumulation" (noop)
												with accelerator.accumulate(_lowercase						):
																		step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)

												# DDP model and model should only be in sync when not (iteration % 2 == 0)
												for param, ddp_param in zip(model.parameters()		,  ddp_model.parameters()						):
																		if not param.requires_grad:
																								continue
																		if ((iteration + 1) % 2 == 0) or (iteration == len(_lowercase						) - 1):
																								# Grads should be in sync
																								assert (
																								    torch.allclose(param.grad		,  ddp_param.grad						) is True
																								), f"""Gradients not in sync when they should be at iteration {iteration}:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})"""
																		else:
																								# Grads should not be in sync
																								assert (
																								    torch.allclose(param.grad		,  ddp_param.grad						) is False
																								), f"""Gradients in sync when they should not be at iteration {iteration}:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})"""

        # Shuffle ddp_input on each iteration
												torch.manual_seed(1_337 + iteration						)
												SCREAMING_SNAKE_CASE							:    Dict							=							ddp_input[torch.randperm(len(_lowercase						)						)]
						GradientState._reset_state()
def       A	(    _lowercase=False		,  _lowercase=False						):
						SCREAMING_SNAKE_CASE							:    int							=							Accelerator(
						    split_batches=_lowercase		,  dispatch_batches=_lowercase		,  gradient_accumulation_steps=2						)
						# Test that context manager behaves properly
						SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Tuple							=							get_training_setup(_lowercase		,  _lowercase						)
						for iteration, batch in enumerate(_lowercase						):
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							batch.values()
												# Gather the distributed inputs and targs for the base model
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Dict							=							accelerator.gather((ddp_input, ddp_target)						)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Any							=							input.to(accelerator.device						), target.to(accelerator.device						)
												# Perform our initial ground truth step in non "DDP"
												model.train()
												ddp_model.train()
												step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												opt.step()

												if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(_lowercase						)):
																		if split_batches:
																								sched.step()
																		else:
																								for _ in range(accelerator.num_processes						):
																														sched.step()
												opt.zero_grad()
												# Perform gradient accumulation under wrapper
												with accelerator.accumulate(_lowercase						):
																		step_model(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
																		ddp_opt.step()
																		ddp_sched.step()
																		ddp_opt.zero_grad()

												# Learning rates should be the same
												assert (
												    opt.param_groups[0]["lr"] == ddp_opt.param_groups[0]["lr"]
												), f"""Learning rates found in each optimizer did not align\nopt: {opt.param_groups[0]['lr']}\nDDP opt: {ddp_opt.param_groups[0]['lr']}\n"""
												SCREAMING_SNAKE_CASE							:    Dict							=							(((iteration + 1) % 2) == 0) or ((iteration + 1) == len(_lowercase						))
												if accelerator.num_processes > 1:
																		check_model_parameters(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase						)
												# Shuffle ddp_input on each iteration
												torch.manual_seed(1_337 + iteration						)
						GradientState._reset_state()
def       A	(    ):
						SCREAMING_SNAKE_CASE							:    List[str]							=							Accelerator()

						SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							RegressionDataset(length=80						)
						SCREAMING_SNAKE_CASE							:    Tuple							=							DataLoader(_lowercase		,  batch_size=16						)
						SCREAMING_SNAKE_CASE							:    Tuple							=							RegressionDataset(length=96						)
						SCREAMING_SNAKE_CASE							:    Dict							=							DataLoader(_lowercase		,  batch_size=16						)
						SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Tuple							=							accelerator.prepare(_lowercase		,  _lowercase						)
						assert accelerator.gradient_state.active_dataloader is None
						for iteration, _ in enumerate(_lowercase						):
												assert id(accelerator.gradient_state.active_dataloader						) == id(_lowercase						)
												if iteration < len(_lowercase						) - 1:
																		assert not accelerator.gradient_state.end_of_dataloader
																		if iteration == 1:
																								for batch_num, _ in enumerate(_lowercase						):
																														assert id(accelerator.gradient_state.active_dataloader						) == id(_lowercase						)
																														if batch_num < len(_lowercase						) - 1:
																																				assert not accelerator.gradient_state.end_of_dataloader
																														else:
																																				assert accelerator.gradient_state.end_of_dataloader
												else:
																		assert accelerator.gradient_state.end_of_dataloader
						assert accelerator.gradient_state.active_dataloader is None
def       A	(    ):
						SCREAMING_SNAKE_CASE							:    str							=							Accelerator()
						SCREAMING_SNAKE_CASE							:    List[Any]							=							accelerator.state
						if state.local_process_index == 0:
												print('''**Test `accumulate` gradient accumulation with dataloader break**'''						)
						test_dataloader_break()
						if state.distributed_type == DistributedType.NO:
												if state.local_process_index == 0:
																		print('''**Test NOOP `no_sync` context manager**'''						)
												test_noop_sync(_lowercase						)
						if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU):
												if state.local_process_index == 0:
																		print('''**Test Distributed `no_sync` context manager**'''						)
												test_distributed_sync(_lowercase						)
						if state.distributed_type == DistributedType.MULTI_GPU:
												for split_batch in [True, False]:
																		for dispatch_batches in [True, False]:
																								if state.local_process_index == 0:
																														print(
																														    '''**Test `accumulate` gradient accumulation, '''		,  f"""`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**"""		,  )
																								test_gradient_accumulation(_lowercase		,  _lowercase						)

    # Currently will break on torch 2.0 +, need to investigate why
						if is_torch_version('''<'''		,  '''2.0'''						) or state.distributed_type == DistributedType.NO:
												if state.local_process_index == 0:
																		print(
																		    '''**Test `accumulate` gradient accumulation with optimizer and scheduler, '''		,  '''`split_batches=False`, `dispatch_batches=False`**'''		,  )
												test_gradient_accumulation_with_opt_and_scheduler()
												if state.distributed_type == DistributedType.MULTI_GPU:
																		for split_batch in [True, False]:
																								for dispatch_batches in [True, False]:
																														if not split_batch and not dispatch_batches:
																																				continue
																														if state.local_process_index == 0:
																																				print(
																																				    '''**Test `accumulate` gradient accumulation with optimizer and scheduler, '''		,  f"""`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**"""		,  )
																														test_gradient_accumulation_with_opt_and_scheduler(_lowercase		,  _lowercase						)
def       A	(    _lowercase						):
						# For xla_spawn (TPUs)
						main()


if __name__ == "__main__":
					main()






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





