import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast


@require_vision
class _a       (	unittest.TestCase ):

	'''simple docstring'''



	def 					__UpperCAmelCase(       self   ):
			__A							:   Tuple	  =				tempfile.mkdtemp()

			__A							:   List[Any]	  =				CLIPImageProcessor()
			__A							:   str	  =				BertTokenizer.from_pretrained(
			    "hf-internal-testing/tiny-random-BertModel"							,			model_input_names=["input_ids", "attention_mask"]   )

			__A							:   List[str]	  =				GitProcessor(__A							,			__A   )

			processor.save_pretrained(self.tmpdirname   )

	def 					__UpperCAmelCase(       self							,			**__UpperCAmelCase   ):
			return AutoProcessor.from_pretrained(self.tmpdirname							,			**__A   ).tokenizer

	def 					__UpperCAmelCase(       self							,			**__UpperCAmelCase   ):
			return AutoProcessor.from_pretrained(self.tmpdirname							,			**__A   ).image_processor

	def 					__UpperCAmelCase(       self   ):
			shutil.rmtree(self.tmpdirname   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   str	  =				[np.random.randint(255							,			size=(3, 30, 400)							,			dtype=np.uinta   )]

			__A							:   Any	  =				[Image.fromarray(np.moveaxis(__A							,			0							,			-1   )   ) for x in image_inputs]

			return image_inputs

	def 					__UpperCAmelCase(       self   ):
			__A							:   Tuple	  =				GitProcessor(tokenizer=self.get_tokenizer()							,			image_processor=self.get_image_processor()   )
			processor.save_pretrained(self.tmpdirname   )

			__A							:   Optional[Any]	  =				self.get_tokenizer(bos_token="(BOS)"							,			eos_token="(EOS)"   )
			__A							:   List[str]	  =				self.get_image_processor(do_normalize=__A							,			padding_value=1.0   )

			__A							:   Dict	  =				GitProcessor.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 					__UpperCAmelCase(       self   ):
			__A							:   Any	  =				self.get_image_processor()
			__A							:   Optional[int]	  =				self.get_tokenizer()

			__A							:   Optional[Any]	  =				GitProcessor(tokenizer=__A							,			image_processor=__A   )

			__A							:   List[str]	  =				self.prepare_image_inputs()

			__A							:   str	  =				image_processor(__A							,			return_tensors="np"   )
			__A							:   Union[str, Any]	  =				processor(images=__A							,			return_tensors="np"   )

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

	def 					__UpperCAmelCase(       self   ):
			__A							:   int	  =				self.get_image_processor()
			__A							:   Dict	  =				self.get_tokenizer()

			__A							:   Optional[int]	  =				GitProcessor(tokenizer=__A							,			image_processor=__A   )

			__A							:   int	  =				"lower newer"

			__A							:   str	  =				processor(text=__A   )

			__A							:   Union[str, Any]	  =				tokenizer(__A							,			return_token_type_ids=__A   )

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

	def 					__UpperCAmelCase(       self   ):
			__A							:   Dict	  =				self.get_image_processor()
			__A							:   Optional[int]	  =				self.get_tokenizer()

			__A							:   List[str]	  =				GitProcessor(tokenizer=__A							,			image_processor=__A   )

			__A							:   str	  =				"lower newer"
			__A							:   Tuple	  =				self.prepare_image_inputs()

			__A							:   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 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				self.get_image_processor()
			__A							:   Tuple	  =				self.get_tokenizer()

			__A							:   Tuple	  =				GitProcessor(tokenizer=__A							,			image_processor=__A   )

			__A							:   Optional[Any]	  =				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

			__A							:   str	  =				processor.batch_decode(__A   )
			__A							:   int	  =				tokenizer.batch_decode(__A   )

			self.assertListEqual(__A							,			__A   )







	def 					__UpperCAmelCase(       self   ):
			__A							:   List[Any]	  =				self.get_image_processor()
			__A							:   int	  =				self.get_tokenizer()

			__A							:   Optional[int]	  =				GitProcessor(tokenizer=__A							,			image_processor=__A   )

			__A							:   Optional[Any]	  =				"lower newer"
			__A							:   List[Any]	  =				self.prepare_image_inputs()

			__A							:   Optional[int]	  =				processor(text=__A							,			images=__A   )

			# For now the processor supports only ['input_ids', 'attention_mask', 'pixel_values']
			self.assertListEqual(list(inputs.keys()   )							,			["input_ids", "attention_mask", "pixel_values"]   )






import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


UpperCamelCase      			=     namedtuple('ExampleNamedTuple', 'a b c')


class _a       (	unittest.TestCase ):

	'''simple docstring'''



	def 					__UpperCAmelCase(       self   ):
			__A							:   List[str]	  =				torch.randn(5							,			2   )
			__A							:   Any	  =				torch.device("cuda"   ) if torch.cuda.is_available() else torch.device("cpu"   )

			__A							:   Union[str, Any]	  =				send_to_device(__UpperCAmelCase							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta.cpu()							,			__UpperCAmelCase   )   )

			__A							:   Dict	  =				send_to_device((tensor, [tensor, tensor], 1)							,			__UpperCAmelCase   )
			self.assertIsInstance(__UpperCAmelCase							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta[0].cpu()							,			__UpperCAmelCase   )   )
			self.assertIsInstance(resulta[1]							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta[1][0].cpu()							,			__UpperCAmelCase   )   )
			self.assertTrue(torch.equal(resulta[1][1].cpu()							,			__UpperCAmelCase   )   )
			self.assertEqual(resulta[2]							,			1   )

			__A							:   Optional[Any]	  =				send_to_device({"a": tensor, "b": [tensor, tensor], "c": 1}							,			__UpperCAmelCase   )
			self.assertIsInstance(__UpperCAmelCase							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta["a"].cpu()							,			__UpperCAmelCase   )   )
			self.assertIsInstance(resulta["b"]							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta["b"][0].cpu()							,			__UpperCAmelCase   )   )
			self.assertTrue(torch.equal(resulta["b"][1].cpu()							,			__UpperCAmelCase   )   )
			self.assertEqual(resulta["c"]							,			1   )

			__A							:   Any	  =				send_to_device(ExampleNamedTuple(a=__UpperCAmelCase							,			b=[tensor, tensor]							,			c=1   )							,			__UpperCAmelCase   )
			self.assertIsInstance(__UpperCAmelCase							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta.a.cpu()							,			__UpperCAmelCase   )   )
			self.assertIsInstance(resulta.b							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta.b[0].cpu()							,			__UpperCAmelCase   )   )
			self.assertTrue(torch.equal(resulta.b[1].cpu()							,			__UpperCAmelCase   )   )
			self.assertEqual(resulta.c							,			1   )

			__A							:   List[Any]	  =				send_to_device(UserDict({"a": tensor, "b": [tensor, tensor], "c": 1}   )							,			__UpperCAmelCase   )
			self.assertIsInstance(__UpperCAmelCase							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta["a"].cpu()							,			__UpperCAmelCase   )   )
			self.assertIsInstance(resulta["b"]							,			__UpperCAmelCase   )
			self.assertTrue(torch.equal(resulta["b"][0].cpu()							,			__UpperCAmelCase   )   )
			self.assertTrue(torch.equal(resulta["b"][1].cpu()							,			__UpperCAmelCase   )   )
			self.assertEqual(resulta["c"]							,			1   )

	def 					__UpperCAmelCase(       self   ):
			with self.assertRaises(__UpperCAmelCase   ) as cm:
					__A							:   List[Any]	  =				recursively_apply(torch.tensor							,			(torch.tensor(1   ), 1)							,			error_on_other_type=__UpperCAmelCase   )
			self.assertEqual(
			    str(cm.exception   )							,			"Unsupported types (<class 'int'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed."							,			)

	def 					__UpperCAmelCase(       self   ):
			__A							:   List[Any]	  =				torch.tensor([1, 2, 3, 4, 5]   )
			self.assertEqual(listify(__UpperCAmelCase   )							,			[1, 2, 3, 4, 5]   )

			__A							:   List[str]	  =				torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )
			self.assertEqual(listify(__UpperCAmelCase   )							,			[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )

			__A							:   Dict	  =				torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]   )
			self.assertEqual(
			    listify(__UpperCAmelCase   )							,			[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]   )

	def 					__UpperCAmelCase(       self   ):
			with patch_environment(aa=1							,			BB=2   ):
					self.assertEqual(os.environ.get("AA"   )							,			"1"   )
					self.assertEqual(os.environ.get("BB"   )							,			"2"   )

			self.assertNotIn("AA"							,			os.environ   )
			self.assertNotIn("BB"							,			os.environ   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   str	  =				RegressionModel()
			__A							:   str	  =				model.forward
			__A							:   List[Any]	  =				convert_outputs_to_fpaa(model.forward   )
			__A							:   Optional[int]	  =				extract_model_from_parallel(__UpperCAmelCase							,			keep_fpaa_wrapper=__UpperCAmelCase   )
			__A							:   Any	  =				pickle.dumps(__UpperCAmelCase   )

	@require_cuda
	def 					__UpperCAmelCase(       self   ):
			__A							:   Any	  =				RegressionModel()
			__A							:   str	  =				model.forward
			__A							:   Tuple	  =				torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
			__A							:   List[Any]	  =				convert_outputs_to_fpaa(model.forward   )
			__A							:   Optional[Any]	  =				extract_model_from_parallel(__UpperCAmelCase							,			keep_fpaa_wrapper=__UpperCAmelCase   )
			__A							:   Any	  =				pickle.dumps(__UpperCAmelCase   )

	@require_cuda
	@require_torch_min_version(version="2.0"   )
	def 					__UpperCAmelCase(       self   ):
			__A							:   Any	  =				RegressionModel()
			__A							:   Optional[int]	  =				model.forward
			__A							:   Union[str, Any]	  =				torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
			__A							:   int	  =				convert_outputs_to_fpaa(model.forward   )
			__A							:   Optional[Any]	  =				torch.compile(model.forward							,			backend="inductor"   )
			__A							:   Tuple	  =				torch.randn(4							,			10   ).cuda()
			__A							:   Union[str, Any]	  =				model(__UpperCAmelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				RegressionModel()
			# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
			__A							:   Dict	  =				torch.nn.parallel.DataParallel(__UpperCAmelCase   )
			__A							:   int	  =				extract_model_from_parallel(__UpperCAmelCase   )

			self.assertEqual(__UpperCAmelCase							,			__UpperCAmelCase   )

	@require_torch_min_version(version="2.0"   )
	def 					__UpperCAmelCase(       self   ):
			__A							:   Dict	  =				RegressionModel()
			__A							:   Dict	  =				torch.compile(__UpperCAmelCase   )

			# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
			__A							:   Dict	  =				torch.nn.parallel.DataParallel(__UpperCAmelCase   )
			__A							:   Tuple	  =				torch.compile(__UpperCAmelCase   )
			__A							:   Any	  =				extract_model_from_parallel(__UpperCAmelCase   )

			self.assertEqual(compiled_model._orig_mod							,			compiled_model_unwrapped._orig_mod   )







	def 					__UpperCAmelCase(       self   ):
			self.assertEqual(find_device([1, "a", torch.tensor([1, 2, 3]   )]   )							,			torch.device("cpu"   )   )
			self.assertEqual(find_device({"a": 1, "b": torch.tensor([1, 2, 3]   )}   )							,			torch.device("cpu"   )   )
			self.assertIsNone(find_device([1, "a"]   )   )





