



'''simple docstring'''




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 CLIPImageProcessor, CLIPProcessor

@require_vision
class 			UpperCamelCase_ (							unittest.TestCase			):





	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		List[Any]							     =		tempfile.mkdtemp()

								# fmt: off
								UpperCAmelCase     :		str							     =		["""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
								UpperCAmelCase     :		Tuple							     =		dict(zip(A		,		range(len(A    )    )    )    )
								UpperCAmelCase     :		str							     =		["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
								UpperCAmelCase     :		List[Any]							     =		{"""unk_token""": """<unk>"""}

								UpperCAmelCase     :		Any							     =		os.path.join(self.tmpdirname		,		VOCAB_FILES_NAMES["""vocab_file"""]    )
								UpperCAmelCase     :		List[str]							     =		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(A    ) + """\n"""    )
								with open(self.merges_file		,		"""w"""		,		encoding="""utf-8"""    ) as fp:
															fp.write("""\n""".join(A    )    )

								UpperCAmelCase     :		str							     =		{
								    """do_resize""": True,
								    """size""": 20,
								    """do_center_crop""": True,
								    """crop_size""": 18,
								    """do_normalize""": True,
								    """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
								    """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
								}
								UpperCAmelCase     :		int							     =		os.path.join(self.tmpdirname		,		A    )
								with open(self.image_processor_file		,		"""w"""		,		encoding="""utf-8"""    ) as fp:
															json.dump(A		,		A    )





	def  _lowercase(  self		,		**A    )			->			Any:
								return CLIPTokenizer.from_pretrained(self.tmpdirname		,		**A    )





	def  _lowercase(  self		,		**A    )			->			Union[str, Any]:
								return CLIPTokenizerFast.from_pretrained(self.tmpdirname		,		**A    )





	def  _lowercase(  self		,		**A    )			->			str:
								return CLIPImageProcessor.from_pretrained(self.tmpdirname		,		**A    )





	def  _lowercase(  self    )			->			Union[str, Any]:
								shutil.rmtree(self.tmpdirname    )





	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		Union[str, Any]							     =		[np.random.randint(255		,		size=(3, 30, 400)		,		dtype=np.uinta    )]

								UpperCAmelCase     :		Tuple							     =		[Image.fromarray(np.moveaxis(A		,		0		,		-1    )    ) for x in image_inputs]

								return image_inputs





	def  _lowercase(  self    )			->			int:
								UpperCAmelCase     :		List[str]							     =		self.get_tokenizer()
								UpperCAmelCase     :		Tuple							     =		self.get_rust_tokenizer()
								UpperCAmelCase     :		Tuple							     =		self.get_image_processor()

								UpperCAmelCase     :		str							     =		CLIPProcessor(tokenizer=A		,		image_processor=A    )
								processor_slow.save_pretrained(self.tmpdirname    )
								UpperCAmelCase     :		Optional[int]							     =		CLIPProcessor.from_pretrained(self.tmpdirname		,		use_fast=A    )

								UpperCAmelCase     :		str							     =		CLIPProcessor(tokenizer=A		,		image_processor=A    )
								processor_fast.save_pretrained(self.tmpdirname    )
								UpperCAmelCase     :		int							     =		CLIPProcessor.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		,		A    )
								self.assertIsInstance(processor_fast.tokenizer		,		A    )

								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		,		A    )
								self.assertIsInstance(processor_fast.image_processor		,		A    )





	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		Optional[int]							     =		CLIPProcessor(tokenizer=self.get_tokenizer()		,		image_processor=self.get_image_processor()    )
								processor.save_pretrained(self.tmpdirname    )

								UpperCAmelCase     :		List[str]							     =		self.get_tokenizer(bos_token="""(BOS)"""		,		eos_token="""(EOS)"""    )
								UpperCAmelCase     :		Tuple							     =		self.get_image_processor(do_normalize=A		,		padding_value=1.0    )

								UpperCAmelCase     :		List[Any]							     =		CLIPProcessor.from_pretrained(
								    self.tmpdirname		,		bos_token="""(BOS)"""		,		eos_token="""(EOS)"""		,		do_normalize=A		,		padding_value=1.0    )

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

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





	def  _lowercase(  self    )			->			Dict:
								UpperCAmelCase     :		str							     =		self.get_image_processor()
								UpperCAmelCase     :		Any							     =		self.get_tokenizer()

								UpperCAmelCase     :		str							     =		CLIPProcessor(tokenizer=A		,		image_processor=A    )

								UpperCAmelCase     :		str							     =		self.prepare_image_inputs()

								UpperCAmelCase     :		Optional[int]							     =		image_processor(A		,		return_tensors="""np"""    )
								UpperCAmelCase     :		Union[str, Any]							     =		processor(images=A		,		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  _lowercase(  self    )			->			int:
								UpperCAmelCase     :		List[Any]							     =		self.get_image_processor()
								UpperCAmelCase     :		Any							     =		self.get_tokenizer()

								UpperCAmelCase     :		Tuple							     =		CLIPProcessor(tokenizer=A		,		image_processor=A    )

								UpperCAmelCase     :		Optional[Any]							     =		"""lower newer"""

								UpperCAmelCase     :		List[Any]							     =		processor(text=A    )

								UpperCAmelCase     :		List[str]							     =		tokenizer(A    )

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





	def  _lowercase(  self    )			->			Dict:
								UpperCAmelCase     :		str							     =		self.get_image_processor()
								UpperCAmelCase     :		Optional[Any]							     =		self.get_tokenizer()

								UpperCAmelCase     :		Any							     =		CLIPProcessor(tokenizer=A		,		image_processor=A    )

								UpperCAmelCase     :		str							     =		"""lower newer"""
								UpperCAmelCase     :		str							     =		self.prepare_image_inputs()

								UpperCAmelCase     :		str							     =		processor(text=A		,		images=A    )

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

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





	def  _lowercase(  self    )			->			List[str]:
								UpperCAmelCase     :		int							     =		self.get_image_processor()
								UpperCAmelCase     :		str							     =		self.get_tokenizer()

								UpperCAmelCase     :		Any							     =		CLIPProcessor(tokenizer=A		,		image_processor=A    )

								UpperCAmelCase     :		List[str]							     =		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								UpperCAmelCase     :		Optional[Any]							     =		processor.batch_decode(A    )
								UpperCAmelCase     :		str							     =		tokenizer.batch_decode(A    )

								self.assertListEqual(A		,		A    )






	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		Tuple							     =		self.get_image_processor()
								UpperCAmelCase     :		Optional[Any]							     =		self.get_tokenizer()

								UpperCAmelCase     :		List[str]							     =		CLIPProcessor(tokenizer=A		,		image_processor=A    )

								UpperCAmelCase     :		Union[str, Any]							     =		"""lower newer"""
								UpperCAmelCase     :		Union[str, Any]							     =		self.prepare_image_inputs()

								UpperCAmelCase     :		Tuple							     =		processor(text=A		,		images=A    )

								self.assertListEqual(list(inputs.keys()    )		,		processor.model_input_names    )






'''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       __lowerCamelCase  (					)      ->			Any:
							raise RuntimeError("""CUDA out of memory."""		)

class 			UpperCamelCase_ (							nn.Module			):





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






	def  _lowercase(  self		,		A    )			->			Any:
								return self.lineara(self.batchnorm(self.lineara(A    )    )    )




class 			UpperCamelCase_ (							unittest.TestCase			):





	def  _lowercase(  self    )			->			Optional[Any]:
								UpperCAmelCase     :		Optional[int]							     =		[]

								@find_executable_batch_size(starting_batch_size=128    )
								def mock_training_loop_function(A    ):
															nonlocal batch_sizes
															batch_sizes.append(A    )
															if batch_size != 8:
																						raise_fake_out_of_memory()

								mock_training_loop_function()
								self.assertListEqual(A		,		[128, 64, 32, 16, 8]    )





	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		Optional[Any]							     =		[]

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

								UpperCAmelCase     ,     UpperCAmelCase     :		Optional[int]							     =		mock_training_loop_function("""hello"""    )
								self.assertListEqual(A		,		[128, 64, 32, 16, 8]    )
								self.assertListEqual([bs, arga]		,		[8, """hello"""]    )





	def  _lowercase(  self    )			->			Any:
								@find_executable_batch_size(starting_batch_size=0    )
								def mock_training_loop_function(A    ):
															pass

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





	def  _lowercase(  self    )			->			Optional[int]:
								@find_executable_batch_size(starting_batch_size=16    )
								def mock_training_loop_function(A    ):
															if batch_size > 0:
																						raise_fake_out_of_memory()
															pass

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





	def  _lowercase(  self    )			->			Optional[Any]:
								@find_executable_batch_size(starting_batch_size=128    )
								def mock_training_loop_function(A		,		A		,		A    ):
															if batch_size != 8:
																						raise raise_fake_out_of_memory()

								with self.assertRaises(A    ) as cm:
															mock_training_loop_function(128		,		"""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  _lowercase(  self    )			->			int:
								@find_executable_batch_size(starting_batch_size=16    )
								def mock_training_loop_function(A    ):
															raise ValueError("""Oops, we had an error!"""    )

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






	@require_cuda
	def  _lowercase(  self    )			->			Optional[int]:
								UpperCAmelCase     :		Optional[Any]							     =		torch.cuda.memory_allocated()
								UpperCAmelCase     :		List[str]							     =		ModelForTest()
								model.cuda()
								self.assertGreater(torch.cuda.memory_allocated()		,		A    )
								UpperCAmelCase     :		Tuple							     =		release_memory(A    )
								self.assertEqual(torch.cuda.memory_allocated()		,		A    )

