






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
						from PIL import Image

						from transformers import OwlViTImageProcessor, OwlViTProcessor


@require_vision
class 							__UpperCamelCase     (  unittest.TestCase ):







	def 			_a (	self   :				List[str]      )		->			List[str]:
		"""simple docstring"""







		__lowercase   							=	tempfile.mkdtemp()

		# fmt: off
		__lowercase   							=	["""""", """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
		# fmt: on
		__lowercase   							=	dict(zip(_lowerCAmelCase							,					range(len(_lowerCAmelCase      )      )      )      )
		__lowercase   							=	["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
		__lowercase   							=	{"""unk_token""": """<unk>"""}

		__lowercase   							=	os.path.join(self.tmpdirname							,					VOCAB_FILES_NAMES["""vocab_file"""]      )
		__lowercase   							=	os.path.join(self.tmpdirname							,					VOCAB_FILES_NAMES["""merges_file"""]      )
		with open(self.vocab_file							,					"""w"""							,					encoding="""utf-8"""      ) as fp:
			fp.write(json.dumps(_lowerCAmelCase      ) + """\n"""      )
		with open(self.merges_file							,					"""w"""							,					encoding="""utf-8"""      ) as fp:
			fp.write("""\n""".join(_lowerCAmelCase      )      )

		__lowercase   							=	{
		    """do_resize""": True,
		    """size""": 20,
		    """do_center_crop""": True,
		    """crop_size""": 18,
		    """do_normalize""": True,
		    """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
		    """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
		}
		__lowercase   							=	os.path.join(self.tmpdirname							,					_lowerCAmelCase      )
		with open(self.image_processor_file							,					"""w"""							,					encoding="""utf-8"""      ) as fp:
			json.dump(_lowerCAmelCase							,					_lowerCAmelCase      )







	def 			_a (	self   :				Optional[int]							,					**_lowerCAmelCase   :				Tuple      )		->			Optional[Any]:
		"""simple docstring"""







		return CLIPTokenizer.from_pretrained(self.tmpdirname							,					pad_token="""!"""							,					**_lowerCAmelCase      )







	def 			_a (	self   :				int							,					**_lowerCAmelCase   :				int      )		->			str:
		"""simple docstring"""







		return CLIPTokenizerFast.from_pretrained(self.tmpdirname							,					pad_token="""!"""							,					**_lowerCAmelCase      )







	def 			_a (	self   :				str							,					**_lowerCAmelCase   :				List[str]      )		->			List[str]:
		"""simple docstring"""







		return OwlViTImageProcessor.from_pretrained(self.tmpdirname							,					**_lowerCAmelCase      )







	def 			_a (	self   :				str      )		->			Tuple:
		"""simple docstring"""







		shutil.rmtree(self.tmpdirname      )







	def 			_a (	self   :				Optional[int]      )		->			Optional[int]:
		"""simple docstring"""







		__lowercase   							=	[np.random.randint(255							,					size=(3, 30, 400)							,					dtype=np.uinta      )]

		__lowercase   							=	[Image.fromarray(np.moveaxis(_lowerCAmelCase							,					0							,					-1      )      ) for x in image_inputs]

		return image_inputs







	def 			_a (	self   :				Union[str, Any]      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	self.get_tokenizer()
		__lowercase   							=	self.get_rust_tokenizer()
		__lowercase   							=	self.get_image_processor()

		__lowercase   							=	OwlViTProcessor(tokenizer=_lowerCAmelCase							,					image_processor=_lowerCAmelCase      )
		processor_slow.save_pretrained(self.tmpdirname      )
		__lowercase   							=	OwlViTProcessor.from_pretrained(self.tmpdirname							,					use_fast=_lowerCAmelCase      )

		__lowercase   							=	OwlViTProcessor(tokenizer=_lowerCAmelCase							,					image_processor=_lowerCAmelCase      )
		processor_fast.save_pretrained(self.tmpdirname      )
		__lowercase   							=	OwlViTProcessor.from_pretrained(self.tmpdirname      )

		self.assertEqual(processor_slow.tokenizer.get_vocab()							,					tokenizer_slow.get_vocab()      )
		self.assertEqual(processor_fast.tokenizer.get_vocab()							,					tokenizer_fast.get_vocab()      )
		self.assertEqual(tokenizer_slow.get_vocab()							,					tokenizer_fast.get_vocab()      )
		self.assertIsInstance(processor_slow.tokenizer							,					_lowerCAmelCase      )
		self.assertIsInstance(processor_fast.tokenizer							,					_lowerCAmelCase      )

		self.assertEqual(processor_slow.image_processor.to_json_string()							,					image_processor.to_json_string()      )
		self.assertEqual(processor_fast.image_processor.to_json_string()							,					image_processor.to_json_string()      )
		self.assertIsInstance(processor_slow.image_processor							,					_lowerCAmelCase      )
		self.assertIsInstance(processor_fast.image_processor							,					_lowerCAmelCase      )







	def 			_a (	self   :				Tuple      )		->			List[Any]:
		"""simple docstring"""







		__lowercase   							=	OwlViTProcessor(tokenizer=self.get_tokenizer()							,					image_processor=self.get_image_processor()      )
		processor.save_pretrained(self.tmpdirname      )

		__lowercase   							=	self.get_tokenizer(bos_token="""(BOS)"""							,					eos_token="""(EOS)"""      )
		__lowercase   							=	self.get_image_processor(do_normalize=_lowerCAmelCase      )

		__lowercase   							=	OwlViTProcessor.from_pretrained(
		    self.tmpdirname							,					bos_token="""(BOS)"""							,					eos_token="""(EOS)"""							,					do_normalize=_lowerCAmelCase      )

		self.assertEqual(processor.tokenizer.get_vocab()							,					tokenizer_add_kwargs.get_vocab()      )
		self.assertIsInstance(processor.tokenizer							,					_lowerCAmelCase      )

		self.assertEqual(processor.image_processor.to_json_string()							,					image_processor_add_kwargs.to_json_string()      )
		self.assertIsInstance(processor.image_processor							,					_lowerCAmelCase      )







	def 			_a (	self   :				Optional[Any]      )		->			Union[str, Any]:
		"""simple docstring"""







		__lowercase   							=	self.get_image_processor()
		__lowercase   							=	self.get_tokenizer()

		__lowercase   							=	OwlViTProcessor(tokenizer=_lowerCAmelCase							,					image_processor=_lowerCAmelCase      )

		__lowercase   							=	self.prepare_image_inputs()

		__lowercase   							=	image_processor(_lowerCAmelCase							,					return_tensors="""np"""      )
		__lowercase   							=	processor(images=_lowerCAmelCase							,					return_tensors="""np"""      )

		for key in input_image_proc.keys():
			self.assertAlmostEqual(input_image_proc[key].sum()							,					input_processor[key].sum()							,					delta=1e-2      )







	def 			_a (	self   :				List[Any]      )		->			Optional[int]:
		"""simple docstring"""







		__lowercase   							=	self.get_image_processor()
		__lowercase   							=	self.get_tokenizer()

		__lowercase   							=	OwlViTProcessor(tokenizer=_lowerCAmelCase							,					image_processor=_lowerCAmelCase      )

		__lowercase   							=	"""lower newer"""

		__lowercase   							=	processor(text=_lowerCAmelCase							,					return_tensors="""np"""      )

		__lowercase   							=	tokenizer(_lowerCAmelCase							,					return_tensors="""np"""      )

		for key in encoded_tok.keys():
			self.assertListEqual(encoded_tok[key][0].tolist()							,					encoded_processor[key][0].tolist()      )







	def 			_a (	self   :				Tuple      )		->			int:
		"""simple docstring"""







		__lowercase   							=	self.get_image_processor()
		__lowercase   							=	self.get_tokenizer()

		__lowercase   							=	OwlViTProcessor(tokenizer=_lowerCAmelCase							,					image_processor=_lowerCAmelCase      )

		__lowercase   							=	"""lower newer"""
		__lowercase   							=	self.prepare_image_inputs()

		__lowercase   							=	processor(text=_lowerCAmelCase							,					images=_lowerCAmelCase      )

		self.assertListEqual(list(inputs.keys()      )							,					["""input_ids""", """attention_mask""", """pixel_values"""]      )

		# test if it raises when no input is passed
		with pytest.raises(_lowerCAmelCase      ):
			processor()







	def 			_a (	self   :				str      )		->			Any:
		"""simple docstring"""







		__lowercase   							=	"""google/owlvit-base-patch32"""
		__lowercase   							=	OwlViTProcessor.from_pretrained(_lowerCAmelCase      )

		__lowercase   							=	["""cat""", """nasa badge"""]
		__lowercase   							=	processor(text=_lowerCAmelCase      )

		__lowercase   							=	16
		self.assertListEqual(list(inputs.keys()      )							,					["""input_ids""", """attention_mask"""]      )
		self.assertEqual(inputs["""input_ids"""].shape							,					(2, seq_length)      )

		# test if it raises when no input is passed
		with pytest.raises(_lowerCAmelCase      ):
			processor()







	def 			_a (	self   :				List[str]      )		->			Optional[int]:
		"""simple docstring"""







		__lowercase   							=	"""google/owlvit-base-patch32"""
		__lowercase   							=	OwlViTProcessor.from_pretrained(_lowerCAmelCase      )

		__lowercase   							=	[["""cat""", """nasa badge"""], ["""person"""]]
		__lowercase   							=	processor(text=_lowerCAmelCase      )

		__lowercase   							=	16
		__lowercase   							=	len(_lowerCAmelCase      )
		__lowercase   							=	max([len(_lowerCAmelCase      ) for texts in input_texts]      )

		self.assertListEqual(list(inputs.keys()      )							,					["""input_ids""", """attention_mask"""]      )
		self.assertEqual(inputs["""input_ids"""].shape							,					(batch_size * num_max_text_queries, seq_length)      )

		# test if it raises when no input is passed
		with pytest.raises(_lowerCAmelCase      ):
			processor()







	def 			_a (	self   :				Optional[Any]      )		->			Any:
		"""simple docstring"""







		__lowercase   							=	"""google/owlvit-base-patch32"""
		__lowercase   							=	OwlViTProcessor.from_pretrained(_lowerCAmelCase      )

		__lowercase   							=	["""cat""", """nasa badge"""]
		__lowercase   							=	processor(text=_lowerCAmelCase      )

		__lowercase   							=	16
		__lowercase   							=	inputs["""input_ids"""]
		__lowercase   							=	[
		    [4_9406, 2368, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		    [4_9406, 6841, 1_1301, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		]

		self.assertListEqual(list(inputs.keys()      )							,					["""input_ids""", """attention_mask"""]      )
		self.assertEqual(inputs["""input_ids"""].shape							,					(2, seq_length)      )
		self.assertListEqual(list(input_ids[0]      )							,					predicted_ids[0]      )
		self.assertListEqual(list(input_ids[1]      )							,					predicted_ids[1]      )







	def 			_a (	self   :				Optional[int]      )		->			List[Any]:
		"""simple docstring"""







		__lowercase   							=	self.get_image_processor()
		__lowercase   							=	self.get_tokenizer()

		__lowercase   							=	OwlViTProcessor(tokenizer=_lowerCAmelCase							,					image_processor=_lowerCAmelCase      )

		__lowercase   							=	self.prepare_image_inputs()
		__lowercase   							=	self.prepare_image_inputs()

		__lowercase   							=	processor(images=_lowerCAmelCase							,					query_images=_lowerCAmelCase      )

		self.assertListEqual(list(inputs.keys()      )							,					["""query_pixel_values""", """pixel_values"""]      )

		# test if it raises when no input is passed
		with pytest.raises(_lowerCAmelCase      ):
			processor()







	def 			_a (	self   :				Union[str, Any]      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	self.get_image_processor()
		__lowercase   							=	self.get_tokenizer()

		__lowercase   							=	OwlViTProcessor(tokenizer=_lowerCAmelCase							,					image_processor=_lowerCAmelCase      )

		__lowercase   							=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

		__lowercase   							=	processor.batch_decode(_lowerCAmelCase      )
		__lowercase   							=	tokenizer.batch_decode(_lowerCAmelCase      )

		self.assertListEqual(_lowerCAmelCase							,					_lowerCAmelCase      )



import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment







def 							A__       (    lowerCamelCase  ,     lowerCamelCase=()  ,     lowerCamelCase=None  ,     lowerCamelCase="no"  ,     lowerCamelCase="29500"    )	->						List[Any]:
				UpperCamelCase_:				Any						=    False
				UpperCamelCase_:				int						=    False
				if any(key.startswith("""KAGGLE"""    ) for key in os.environ.keys()    ):
								UpperCamelCase_:				str						=    True
				elif "IPython" in sys.modules:
								UpperCamelCase_:				Tuple						=    """google.colab""" in str(sys.modules["""IPython"""].get_ipython()    )

				try:
								UpperCamelCase_:				Union[str, Any]						=    PrecisionType(mixed_precision.lower()    )
				except ValueError:
								raise ValueError(
								    F'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.'''    )

				if (in_colab or in_kaggle) and (os.environ.get("""TPU_NAME"""  ,     lowerCamelCase    ) is not None):
								# TPU launch
								import torch_xla.distributed.xla_multiprocessing as xmp

								if len(AcceleratorState._shared_state    ) > 0:
												raise ValueError(
												    """To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside """
												    """your training function. Restart your notebook and make sure no cells initializes an """
												    """`Accelerator`."""    )
								if num_processes is None:
												UpperCamelCase_:				str						=    8

								UpperCamelCase_:				int						=    PrepareForLaunch(lowerCamelCase  ,     distributed_type="""TPU"""    )
								print(F'''Launching a training on {num_processes} TPU cores.'''    )
								xmp.spawn(lowerCamelCase  ,     args=lowerCamelCase  ,     nprocs=lowerCamelCase  ,     start_method="""fork"""    )
				elif in_colab:
								# No need for a distributed launch otherwise as it's either CPU or one GPU.
								if torch.cuda.is_available():
												print("""Launching training on one GPU."""    )
								else:
												print("""Launching training on one CPU."""    )
								function(*lowerCamelCase    )
				else:
								if num_processes is None:
												raise ValueError(
												    """You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call."""    )

								if num_processes > 1:
												# Multi-GPU launch
												from torch.multiprocessing import start_processes
												from torch.multiprocessing.spawn import ProcessRaisedException

												if len(AcceleratorState._shared_state    ) > 0:
																raise ValueError(
																    """To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized """
																    """inside your training function. Restart your notebook and make sure no cells initializes an """
																    """`Accelerator`."""    )

												if torch.cuda.is_initialized():
																raise ValueError(
																    """To launch a multi-GPU training from your notebook, you need to avoid running any instruction """
																    """using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA """
																    """function."""    )

												# torch.distributed will expect a few environment variable to be here. We set the ones common to each
												# process here (the other ones will be set be the launcher).
												with patch_environment(
												    world_size=lowerCamelCase  ,     master_addr="""127.0.01"""  ,     master_port=lowerCamelCase  ,     mixed_precision=lowerCamelCase    ):
																UpperCamelCase_:				Optional[int]						=    PrepareForLaunch(lowerCamelCase  ,     distributed_type="""MULTI_GPU"""    )
																print(F'''Launching training on {num_processes} GPUs.'''    )
																try:
																				start_processes(lowerCamelCase  ,     args=lowerCamelCase  ,     nprocs=lowerCamelCase  ,     start_method="""fork"""    )
																except ProcessRaisedException as e:
																				if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
																								raise RuntimeError(
																								    """CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. """
																								    """This likely stems from an outside import causing issues once the `notebook_launcher()` is called. """
																								    """Please review your imports and test them when running the `notebook_launcher()` to identify """
																								    """which one is problematic."""    ) from e

								else:
												# No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
												if is_mps_available():
																UpperCamelCase_:				List[Any]						=    """1"""
																print("""Launching training on MPS."""    )
												elif torch.cuda.is_available():
																print("""Launching training on one GPU."""    )
												else:
																print("""Launching training on CPU."""    )
												function(*lowerCamelCase    )







def 							A__       (    lowerCamelCase  ,     lowerCamelCase=()  ,     lowerCamelCase=2    )	->						List[str]:
				from torch.multiprocessing import start_processes

				with tempfile.NamedTemporaryFile() as tmp_file:
								# torch.distributed will expect a few environment variable to be here. We set the ones common to each
								# process here (the other ones will be set be the launcher).
								with patch_environment(
								    world_size=lowerCamelCase  ,     master_addr="""127.0.01"""  ,     master_port="""29500"""  ,     accelerate_mixed_precision="""no"""  ,     accelerate_debug_rdv_file=tmp_file.name  ,     accelerate_use_cpu="""yes"""  ,     ):
												UpperCamelCase_:				str						=    PrepareForLaunch(lowerCamelCase  ,     debug=lowerCamelCase    )
												start_processes(lowerCamelCase  ,     args=lowerCamelCase  ,     nprocs=lowerCamelCase  ,     start_method="""fork"""    )






