

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import BlipImageProcessor





class    __UpperCAmelCase	(			unittest.TestCase		):



   def __init__(				self				:	Any,						__A				:	Optional[int],						__A				:	Optional[int]=7,						__A				:	Union[str, Any]=3,						__A				:	Optional[Any]=1_8,						__A				:	List[Any]=3_0,						__A				:	Dict=4_0_0,						__A				:	List[Any]=True,						__A				:	str=None,						__A				:	List[Any]=True,						__A				:	Optional[int]=False,						__A				:	str=[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],						__A				:	str=[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],						__A				:	str=True,						):
     UpperCAmelCase			:				Dict										=					size if size is not None else {'''height''': 2_0, '''width''': 2_0}
     UpperCAmelCase			:				str										=					parent
     UpperCAmelCase			:				List[str]										=					batch_size
     UpperCAmelCase			:				Dict										=					num_channels
     UpperCAmelCase			:				Optional[int]										=					image_size
     UpperCAmelCase			:				Union[str, Any]										=					min_resolution
     UpperCAmelCase			:				Dict										=					max_resolution
     UpperCAmelCase			:				Optional[Any]										=					do_resize
     UpperCAmelCase			:				int										=					size
     UpperCAmelCase			:				int										=					do_normalize
     UpperCAmelCase			:				Tuple										=					image_mean
     UpperCAmelCase			:				Dict										=					image_std
     UpperCAmelCase			:				Tuple										=					do_pad
     UpperCAmelCase			:				Union[str, Any]										=					do_convert_rgb



   def 			__magic_name__       (				self				:	str		):
     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_convert_rgb": self.do_convert_rgb,
         "do_pad": self.do_pad,
     }





   def 			__magic_name__       (				self				:	Optional[int],						__A				:	Dict=False,						__A				:	str=False,						__A				:	Any=False		):
     assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

     if equal_resolution:
       UpperCAmelCase			:				Optional[int]										=					[]
       for i in range(self.batch_size		):
         image_inputs.append(
             np.random.randint(
                 2_5_5,						size=(self.num_channels, self.max_resolution, self.max_resolution),						dtype=np.uinta		)		)
     else:
       UpperCAmelCase			:				str										=					[]
       for i in range(self.batch_size		):
         UpperCAmelCase					,		UpperCAmelCase			:				List[str]										=					np.random.choice(np.arange(self.min_resolution,						self.max_resolution		),						2		)
         image_inputs.append(np.random.randint(2_5_5,						size=(self.num_channels, width, height),						dtype=np.uinta		)		)

     if not numpify and not torchify:
       # PIL expects the channel dimension as last dimension
       UpperCAmelCase			:				Any										=					[Image.fromarray(np.moveaxis(UpperCAmelCase_,						0,						-1		)		) for x in image_inputs]

     if torchify:
       UpperCAmelCase			:				int										=					[torch.from_numpy(UpperCAmelCase_		) for x in image_inputs]

     return image_inputs






@require_torch
@require_vision
class    __UpperCAmelCase	(			lowerCamelCase__     ,    unittest.TestCase		):
   UpperCamelCase  	=	BlipImageProcessor if is_vision_available() else None



   def 			__magic_name__       (				self				:	Any		):
     UpperCAmelCase			:				List[str]										=					BlipImageProcessingTester(self		)



   @property
   def 			__magic_name__       (				self				:	List[str]		):
     return self.image_processor_tester.prepare_image_processor_dict()



   def 			__magic_name__       (				self				:	Any		):
     UpperCAmelCase			:				Any										=					self.image_processing_class(**self.image_processor_dict		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''do_resize'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''size'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''do_normalize'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''image_mean'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''image_std'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''do_convert_rgb'''		)		)



   def 			__magic_name__       (				self				:	Dict		):
     pass



   def 			__magic_name__       (				self				:	Optional[Any]		):
     # Initialize image_processor
     UpperCAmelCase			:				List[str]										=					self.image_processing_class(**self.image_processor_dict		)
     # create random PIL images
     UpperCAmelCase			:				Union[str, Any]										=					self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase_		)
     for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase_,						Image.Image		)

     # Test not batched input
     UpperCAmelCase			:				Union[str, Any]										=					image_processor(image_inputs[0],						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)

     # Test batched
     UpperCAmelCase			:				Any										=					image_processor(UpperCAmelCase_,						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)



   def 			__magic_name__       (				self				:	Union[str, Any]		):
     # Initialize image_processor
     UpperCAmelCase			:				Union[str, Any]										=					self.image_processing_class(**self.image_processor_dict		)
     # create random numpy tensors
     UpperCAmelCase			:				int										=					self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase_,						numpify=UpperCAmelCase_		)
     for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase_,						np.ndarray		)

     # Test not batched input
     UpperCAmelCase			:				Optional[int]										=					image_processor(image_inputs[0],						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)

     # Test batched
     UpperCAmelCase			:				int										=					image_processor(UpperCAmelCase_,						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)





   def 			__magic_name__       (				self				:	List[Any]		):
     # Initialize image_processor
     UpperCAmelCase			:				Union[str, Any]										=					self.image_processing_class(**self.image_processor_dict		)
     # create random PyTorch tensors
     UpperCAmelCase			:				str										=					self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase_,						torchify=UpperCAmelCase_		)
     for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase_,						torch.Tensor		)

     # Test not batched input
     UpperCAmelCase			:				Optional[int]										=					image_processor(image_inputs[0],						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)

     # Test batched
     UpperCAmelCase			:				List[str]										=					image_processor(UpperCAmelCase_,						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)






@require_torch
@require_vision
class    __UpperCAmelCase	(			lowerCamelCase__     ,    unittest.TestCase		):
   UpperCamelCase  	=	BlipImageProcessor if is_vision_available() else None



   def 			__magic_name__       (				self				:	Optional[Any]		):
     UpperCAmelCase			:				Any										=					BlipImageProcessingTester(self,						num_channels=4		)
     UpperCAmelCase			:				Optional[int]										=					3



   @property
   def 			__magic_name__       (				self				:	Union[str, Any]		):
     return self.image_processor_tester.prepare_image_processor_dict()



   def 			__magic_name__       (				self				:	Optional[Any]		):
     UpperCAmelCase			:				Optional[int]										=					self.image_processing_class(**self.image_processor_dict		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''do_resize'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''size'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''do_normalize'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''image_mean'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''image_std'''		)		)
     self.assertTrue(hasattr(UpperCAmelCase_,						'''do_convert_rgb'''		)		)



   def 			__magic_name__       (				self				:	str		):
     pass





   def 			__magic_name__       (				self				:	List[Any]		):
     # Initialize image_processor
     UpperCAmelCase			:				Optional[int]										=					self.image_processing_class(**self.image_processor_dict		)
     # create random PIL images
     UpperCAmelCase			:				int										=					self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase_		)
     for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase_,						Image.Image		)

     # Test not batched input
     UpperCAmelCase			:				Optional[int]										=					image_processor(image_inputs[0],						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             1,
             self.expected_encoded_image_num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)

     # Test batched
     UpperCAmelCase			:				Dict										=					image_processor(UpperCAmelCase_,						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_images.shape,						(
             self.image_processor_tester.batch_size,
             self.expected_encoded_image_num_channels,
             self.image_processor_tester.size['''height'''],
             self.image_processor_tester.size['''width'''],
         ),						)












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__    :      List[Any]       					=namedtuple('''ExampleNamedTuple''', '''a b c''')

class    __A  (    unittest.TestCase     ):


				def _snake_case							(		self				):
								lowerCamelCase									=torch.randn(5 ,			2				)
								lowerCamelCase									=torch.device("""cuda"""				) if torch.cuda.is_available() else torch.device("""cpu"""				)

								lowerCamelCase									=send_to_device(UpperCAmelCase_ ,			UpperCAmelCase_				)
								self.assertTrue(torch.equal(resulta.cpu() ,			UpperCAmelCase_				)				)

								lowerCamelCase									=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				)

								lowerCamelCase									=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				)

								lowerCamelCase									=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				)

								lowerCamelCase									=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 _snake_case							(		self				):
								with self.assertRaises(UpperCAmelCase_				) as cm:
												lowerCamelCase									=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 _snake_case							(		self				):
								lowerCamelCase									=torch.tensor([1, 2, 3, 4, 5]				)
								self.assertEqual(listify(UpperCAmelCase_				) ,			[1, 2, 3, 4, 5]				)

								lowerCamelCase									=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]]				)

								lowerCamelCase									=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 _snake_case							(		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 _snake_case							(		self				):
								lowerCamelCase									=RegressionModel()
								lowerCamelCase									=model.forward
								lowerCamelCase									=convert_outputs_to_fpaa(model.forward				)
								lowerCamelCase									=extract_model_from_parallel(UpperCAmelCase_ ,			keep_fpaa_wrapper=UpperCAmelCase_				)
								lowerCamelCase									=pickle.dumps(UpperCAmelCase_				)


				@require_cuda
				def _snake_case							(		self				):
								lowerCamelCase									=RegressionModel()
								lowerCamelCase									=model.forward
								lowerCamelCase									=torch.cuda.amp.autocast(dtype=torch.floataa				)(model.forward				)
								lowerCamelCase									=convert_outputs_to_fpaa(model.forward				)
								lowerCamelCase									=extract_model_from_parallel(UpperCAmelCase_ ,			keep_fpaa_wrapper=UpperCAmelCase_				)
								lowerCamelCase									=pickle.dumps(UpperCAmelCase_				)


				@require_cuda
				@require_torch_min_version(version="""2.0"""				)
				def _snake_case							(		self				):
								lowerCamelCase									=RegressionModel()
								lowerCamelCase									=model.forward
								lowerCamelCase									=torch.cuda.amp.autocast(dtype=torch.floataa				)(model.forward				)
								lowerCamelCase									=convert_outputs_to_fpaa(model.forward				)
								lowerCamelCase									=torch.compile(model.forward ,			backend="""inductor"""				)
								lowerCamelCase									=torch.randn(4 ,			10				).cuda()
								lowerCamelCase									=model(UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=RegressionModel()
								# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
								lowerCamelCase									=torch.nn.parallel.DataParallel(UpperCAmelCase_				)
								lowerCamelCase									=extract_model_from_parallel(UpperCAmelCase_				)

								self.assertEqual(UpperCAmelCase_ ,			UpperCAmelCase_				)


				@require_torch_min_version(version="""2.0"""				)
				def _snake_case							(		self				):
								lowerCamelCase									=RegressionModel()
								lowerCamelCase									=torch.compile(UpperCAmelCase_				)

								# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
								lowerCamelCase									=torch.nn.parallel.DataParallel(UpperCAmelCase_				)
								lowerCamelCase									=torch.compile(UpperCAmelCase_				)
								lowerCamelCase									=extract_model_from_parallel(UpperCAmelCase_				)

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


				def _snake_case							(		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"""]				)				)





