


'''simple docstring'''






import unittest

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

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image
else:




		class 						a__							:





									'''simple docstring'''






									@staticmethod
									def     __SCREAMING_SNAKE_CASE				(						*lowerCamelCase_   ,       **lowerCamelCase_  )			->				Union[str, Any]:
																pass




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





							'''simple docstring'''






							lowercase__     :  Optional[Any]      		= MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				List[Any]:
														lowerCAmelCase__			  =      pipeline(
														    '''zero-shot-object-detection'''   ,       model='''hf-internal-testing/tiny-random-owlvit-object-detection'''  )

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







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				List[str]:
														lowerCAmelCase__			  =      object_detector(examples[0]   ,       threshold=0.0  )

														lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														pass







							@require_torch
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														lowerCAmelCase__			  =      pipeline(
														    '''zero-shot-object-detection'''   ,       model='''hf-internal-testing/tiny-random-owlvit-object-detection'''  )

														lowerCAmelCase__			  =      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_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														        {'''score''': 0.7_218, '''label''': '''remote''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														        {'''score''': 0.7_184, '''label''': '''couch''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														        {'''score''': 0.6_748, '''label''': '''remote''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														        {'''score''': 0.6_656, '''label''': '''cat''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														        {'''score''': 0.6_614, '''label''': '''couch''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														        {'''score''': 0.6_456, '''label''': '''remote''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
														        {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 2_74, '''xmax''': 93, '''ymax''': 2_97}},
														        {'''score''': 0.6_419, '''label''': '''cat''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
														    ]   ,       )

														lowerCAmelCase__			  =      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_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														            {'''score''': 0.7_218, '''label''': '''remote''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														            {'''score''': 0.7_184, '''label''': '''couch''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
														            {'''score''': 0.6_748, '''label''': '''remote''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														            {'''score''': 0.6_656, '''label''': '''cat''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														            {'''score''': 0.6_614, '''label''': '''couch''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
														            {'''score''': 0.6_456, '''label''': '''remote''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
														            {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 2_74, '''xmax''': 93, '''ymax''': 2_97}},
														            {'''score''': 0.6_419, '''label''': '''cat''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
														        ]
														    ]   ,       )







							@require_torch
							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      pipeline('''zero-shot-object-detection'''  )

														lowerCAmelCase__			  =      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_24, '''ymin''': 20, '''xmax''': 6_40, '''ymax''': 3_73}},
														        {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 1_77, '''ymax''': 1_15}},
														        {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														        {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
														        {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_42, '''ymax''': 4_76}},
														    ]   ,       )

														lowerCAmelCase__			  =      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_24, '''ymin''': 20, '''xmax''': 6_40, '''ymax''': 3_73}},
														            {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 1_77, '''ymax''': 1_15}},
														            {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														            {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
														            {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_42, '''ymax''': 4_76}},
														        ],
														        [
														            {'''score''': 0.2_868, '''label''': '''cat''', '''box''': {'''xmin''': 3_24, '''ymin''': 20, '''xmax''': 6_40, '''ymax''': 3_73}},
														            {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 1_77, '''ymax''': 1_15}},
														            {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														            {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
														            {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_42, '''ymax''': 4_76}},
														        ],
														    ]   ,       )







							@require_tf
							@unittest.skip('''Zero Shot Object Detection not implemented in TF'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														pass







							@require_torch
							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      0.2
														lowerCAmelCase__			  =      pipeline('''zero-shot-object-detection'''  )

														lowerCAmelCase__			  =      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_24, '''ymin''': 20, '''xmax''': 6_40, '''ymax''': 3_73}},
														        {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 1_77, '''ymax''': 1_15}},
														        {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
														    ]   ,       )







							@require_torch
							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      2
														lowerCAmelCase__			  =      pipeline('''zero-shot-object-detection'''  )

														lowerCAmelCase__			  =      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_24, '''ymin''': 20, '''xmax''': 6_40, '''ymax''': 3_73}},
														        {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 1_77, '''ymax''': 1_15}},
														    ]   ,       )



'''simple docstring'''






import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory


def    _snake_case  (	)		->       Union[str, Any]:
							raise RuntimeError('''CUDA out of memory.'''			)




class 						a__							(				nn.Module    ):





							'''simple docstring'''






							def __init__(						self  )			->				int:
														super().__init__()
														lowerCAmelCase__			  =      nn.Linear(3   ,       4  )
														lowerCAmelCase__			  =      nn.BatchNormad(4  )
														lowerCAmelCase__			  =      nn.Linear(4   ,       5  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				Optional[Any]:
														return self.lineara(self.batchnorm(self.lineara(lowerCamelCase_  )  )  )




class 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														lowerCAmelCase__			  =      []

														@find_executable_batch_size(starting_batch_size=1_28  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					nonlocal batch_sizes
																					batch_sizes.append(lowerCamelCase_  )
																					if batch_size != 8:
																												raise_fake_out_of_memory()

														mock_training_loop_function()
														self.assertListEqual(lowerCamelCase_   ,       [1_28, 64, 32, 16, 8]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      []

														@find_executable_batch_size(starting_batch_size=1_28  )
														def mock_training_loop_function(lowerCamelCase_   ,       lowerCamelCase_  ):
																					nonlocal batch_sizes
																					batch_sizes.append(lowerCamelCase_  )
																					if batch_size != 8:
																												raise_fake_out_of_memory()
																					return batch_size, arga

														lowerCAmelCase__  ,   lowerCAmelCase__			  =      mock_training_loop_function('''hello'''  )
														self.assertListEqual(lowerCamelCase_   ,       [1_28, 64, 32, 16, 8]  )
														self.assertListEqual([bs, arga]   ,       [8, '''hello''']  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														@find_executable_batch_size(starting_batch_size=0  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					pass

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function()
																					self.assertIn('''No executable batch size found, reached zero.'''   ,       cm.exception.args[0]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														@find_executable_batch_size(starting_batch_size=16  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					if batch_size > 0:
																												raise_fake_out_of_memory()
																					pass

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function()
																					self.assertIn('''No executable batch size found, reached zero.'''   ,       cm.exception.args[0]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														@find_executable_batch_size(starting_batch_size=1_28  )
														def mock_training_loop_function(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  ):
																					if batch_size != 8:
																												raise raise_fake_out_of_memory()

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function(1_28   ,       '''hello'''   ,       '''world'''  )
																					self.assertIn('''Batch size was passed into `f`'''   ,       cm.exception.args[0]  )
																					self.assertIn('''`f(arg1=\'hello\', arg2=\'world\')'''   ,       cm.exception.args[0]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														@find_executable_batch_size(starting_batch_size=16  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					raise ValueError('''Oops, we had an error!'''  )

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function()
																					self.assertIn('''Oops, we had an error!'''   ,       cm.exception.args[0]  )







							@require_cuda
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      torch.cuda.memory_allocated()
														lowerCAmelCase__			  =      ModelForTest()
														model.cuda()
														self.assertGreater(torch.cuda.memory_allocated()   ,       lowerCamelCase_  )
														lowerCAmelCase__			  =      release_memory(lowerCamelCase_  )
														self.assertEqual(torch.cuda.memory_allocated()   ,       lowerCamelCase_  )