
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      lowerCAmelCase__  (			unittest.TestCase    ):






		def __init__(	self	:							List[Any]							,    SCREAMING_SNAKE_CASE__	:							List[Any]							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=7							,    SCREAMING_SNAKE_CASE__	:							Any=3							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=18							,    SCREAMING_SNAKE_CASE__	:							int=30							,    SCREAMING_SNAKE_CASE__	:							Any=4_00							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]=True							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=None							,    SCREAMING_SNAKE_CASE__	:							Dict=True							,    SCREAMING_SNAKE_CASE__	:							List[str]=False							,    SCREAMING_SNAKE_CASE__	:							Optional[int]=[0.48145466, 0.4578275, 0.40821073]							,    SCREAMING_SNAKE_CASE__	:							int=[0.26862954, 0.26130258, 0.27577711]							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=True							,    )  ->							Any:
						__lowerCamelCase					      =			size if size is not None else {'''height''': 20, '''width''': 20}
						__lowerCamelCase					      =			parent
						__lowerCamelCase					      =			batch_size
						__lowerCamelCase					      =			num_channels
						__lowerCamelCase					      =			image_size
						__lowerCamelCase					      =			min_resolution
						__lowerCamelCase					      =			max_resolution
						__lowerCamelCase					      =			do_resize
						__lowerCamelCase					      =			size
						__lowerCamelCase					      =			do_normalize
						__lowerCamelCase					      =			image_mean
						__lowerCamelCase					      =			image_std
						__lowerCamelCase					      =			do_pad
						__lowerCamelCase					      =			do_convert_rgb






		def 				__A (	self	:							Any						)  ->							Dict:
						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 				__A (	self	:							List[Any]							,    SCREAMING_SNAKE_CASE__	:							List[str]=False							,    SCREAMING_SNAKE_CASE__	:							Any=False							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]=False						)  ->							Any:
						assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

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

						if torchify:
										__lowerCamelCase					      =			[torch.from_numpy(SCREAMING_SNAKE_CASE__						) for x in image_inputs]

						return image_inputs





@require_torch
@require_vision
class      lowerCAmelCase__  (			__lowercase						,			unittest.TestCase    ):
		a__ :		str   						=	BlipImageProcessor if is_vision_available() else None






		def 				__A (	self	:							Optional[int]						)  ->							int:
						__lowerCamelCase					      =			BlipImageProcessingTester(self						)






		@property
		def 				__A (	self	:							int						)  ->							int:
						return self.image_processor_tester.prepare_image_processor_dict()






		def 				__A (	self	:							Dict						)  ->							Optional[Any]:
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''do_resize'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''size'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''do_normalize'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''image_mean'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''image_std'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''do_convert_rgb'''						)						)






		def 				__A (	self	:							Dict						)  ->							Dict:
						pass






		def 				__A (	self	:							Dict						)  ->							Tuple:
						# Initialize image_processor
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						# create random PIL images
						__lowerCamelCase					      =			self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__						)
						for image in image_inputs:
										self.assertIsInstance(SCREAMING_SNAKE_CASE__							,    Image.Image						)

						# Test not batched input
						__lowerCamelCase					      =			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
						__lowerCamelCase					      =			image_processor(SCREAMING_SNAKE_CASE__							,    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 				__A (	self	:							str						)  ->							List[Any]:
						# Initialize image_processor
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						# create random numpy tensors
						__lowerCamelCase					      =			self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__							,    numpify=SCREAMING_SNAKE_CASE__						)
						for image in image_inputs:
										self.assertIsInstance(SCREAMING_SNAKE_CASE__							,    np.ndarray						)

						# Test not batched input
						__lowerCamelCase					      =			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
						__lowerCamelCase					      =			image_processor(SCREAMING_SNAKE_CASE__							,    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 				__A (	self	:							List[str]						)  ->							Dict:
						# Initialize image_processor
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						# create random PyTorch tensors
						__lowerCamelCase					      =			self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__							,    torchify=SCREAMING_SNAKE_CASE__						)
						for image in image_inputs:
										self.assertIsInstance(SCREAMING_SNAKE_CASE__							,    torch.Tensor						)

						# Test not batched input
						__lowerCamelCase					      =			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
						__lowerCamelCase					      =			image_processor(SCREAMING_SNAKE_CASE__							,    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      lowerCAmelCase__  (			__lowercase						,			unittest.TestCase    ):
		a__ :		Any   						=	BlipImageProcessor if is_vision_available() else None






		def 				__A (	self	:							Dict						)  ->							List[Any]:
						__lowerCamelCase					      =			BlipImageProcessingTester(self							,    num_channels=4						)
						__lowerCamelCase					      =			3






		@property
		def 				__A (	self	:							str						)  ->							Optional[int]:
						return self.image_processor_tester.prepare_image_processor_dict()






		def 				__A (	self	:							Dict						)  ->							int:
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''do_resize'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''size'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''do_normalize'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''image_mean'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''image_std'''						)						)
						self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__							,    '''do_convert_rgb'''						)						)






		def 				__A (	self	:							Union[str, Any]						)  ->							List[Any]:
						pass







		def 				__A (	self	:							Optional[Any]						)  ->							Tuple:
						# Initialize image_processor
						__lowerCamelCase					      =			self.image_processing_class(**self.image_processor_dict						)
						# create random PIL images
						__lowerCamelCase					      =			self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__						)
						for image in image_inputs:
										self.assertIsInstance(SCREAMING_SNAKE_CASE__							,    Image.Image						)

						# Test not batched input
						__lowerCamelCase					      =			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
						__lowerCamelCase					      =			image_processor(SCREAMING_SNAKE_CASE__							,    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'''],
						    )							,    )





from ..utils import deprecate
from .attention_processor import (  # noqa: F401
    Attention,
    AttentionProcessor,
    AttnAddedKVProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRALinearLayer,
    LoRAXFormersAttnProcessor,
    SlicedAttnAddedKVProcessor,
    SlicedAttnProcessor,
    XFormersAttnProcessor,
)
from .attention_processor import AttnProcessor as AttnProcessorRename  # noqa: F401


deprecate(
    "cross_attention",
    "0.20.0",
    "Importing from cross_attention is deprecated. Please import from diffusers.models.attention_processor instead.",
    standard_warn=False,
)


SCREAMING_SNAKE_CASE__     :    Optional[int]      							=					AttentionProcessor

class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Tuple							,    *SCREAMING_SNAKE_CASE__	:							Dict							,    **SCREAMING_SNAKE_CASE__	:							int						)  ->							List[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							Optional[Any]							,    **SCREAMING_SNAKE_CASE__	:							Optional[int]						)  ->							Optional[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							List[Any]							,    **SCREAMING_SNAKE_CASE__	:							List[str]						)  ->							List[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[int]							,    *SCREAMING_SNAKE_CASE__	:							Optional[int]							,    **SCREAMING_SNAKE_CASE__	:							Tuple						)  ->							Optional[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[Any]							,    *SCREAMING_SNAKE_CASE__	:							int							,    **SCREAMING_SNAKE_CASE__	:							Optional[Any]						)  ->							List[str]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Tuple							,    *SCREAMING_SNAKE_CASE__	:							Tuple							,    **SCREAMING_SNAKE_CASE__	:							Any						)  ->							Union[str, Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							Union[str, Any]							,    **SCREAMING_SNAKE_CASE__	:							Tuple						)  ->							int:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[Any]							,    *SCREAMING_SNAKE_CASE__	:							Any							,    **SCREAMING_SNAKE_CASE__	:							str						)  ->							Dict:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)



