

"""simple docstring"""






import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
class 	_lowerCAmelCase    (      unittest.TestCase       ):

							def 					_lowerCAmelCase  ( self       :    Dict ,		a       :    bool = False    )   ->    Any:

									"""simple docstring"""
									lowercase										=							10

									if only_cross_attention:
											lowercase										=							12
									else:
											# when only cross attention is not set, the cross attention dim must be the same as the query dim
											lowercase										=							query_dim

									return {
									    "query_dim": query_dim,
									    "cross_attention_dim": cross_attention_dim,
									    "heads": 2,
									    "dim_head": 4,
									    "added_kv_proj_dim": 6,
									    "norm_num_groups": 1,
									    "only_cross_attention": only_cross_attention,
									    "processor": AttnAddedKVProcessor(),
									}

							def 					_lowerCAmelCase  ( self       :    int ,		a       :    List[Any] ,		a       :    str    )   ->    int:

									"""simple docstring"""
									lowercase										=							2

									lowercase										=							torch.rand(a ,		a ,		3 ,		2    )
									lowercase										=							torch.rand(a ,		4 ,		a    )
									lowercase										=							None

									return {
									    "hidden_states": hidden_states,
									    "encoder_hidden_states": encoder_hidden_states,
									    "attention_mask": attention_mask,
									}

							def 					_lowerCAmelCase  ( self       :    Any    )   ->    Tuple:

									"""simple docstring"""
									# self and cross attention

									torch.manual_seed(0    )

									lowercase										=							self.get_constructor_arguments(only_cross_attention=a    )
									lowercase										=							Attention(**a    )

									self.assertTrue(attn.to_k is not None    )
									self.assertTrue(attn.to_v is not None    )

									lowercase										=							self.get_forward_arguments(
									    query_dim=constructor_args['''query_dim'''] ,		added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']    )

									lowercase										=							attn(**a    )

									# only self attention

									torch.manual_seed(0    )

									lowercase										=							self.get_constructor_arguments(only_cross_attention=a    )
									lowercase										=							Attention(**a    )

									self.assertTrue(attn.to_k is None    )
									self.assertTrue(attn.to_v is None    )

									lowercase										=							self.get_forward_arguments(
									    query_dim=constructor_args['''query_dim'''] ,		added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']    )

									lowercase										=							attn(**a    )

									self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()    )




class 	_lowerCAmelCase    (      unittest.TestCase       ):

							def 					_lowerCAmelCase  ( self       :    int    )   ->    str:

									"""simple docstring"""
									lowercase										=							DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe''' ,		safety_checker=a    )

									lowercase										=							pipe(
									    '''foo''' ,		num_inference_steps=2 ,		generator=torch.Generator('''cpu'''    ).manual_seed(0    ) ,		output_type='''np''' ,		).images

									# the initial conversion succeeds
									lowercase										=							DiffusionPipeline.from_pretrained(
									    '''hf-internal-testing/tiny-stable-diffusion-pipe''' ,		device_map='''sequential''' ,		safety_checker=a    )

									lowercase										=							pipe(
									    '''foo''' ,		num_inference_steps=2 ,		generator=torch.Generator('''cpu'''    ).manual_seed(0    ) ,		output_type='''np''' ,		).images

									with tempfile.TemporaryDirectory() as tmpdir:
											# save the converted model
											pipe.save_pretrained(a    )

											# can also load the converted weights
											lowercase										=							DiffusionPipeline.from_pretrained(a ,		device_map='''sequential''' ,		safety_checker=a    )

									lowercase										=							pipe(
									    '''foo''' ,		num_inference_steps=2 ,		generator=torch.Generator('''cpu'''    ).manual_seed(0    ) ,		output_type='''np''' ,		).images

									self.assertTrue(np.allclose(a ,		a ,		atol=1E-5    )    )
									self.assertTrue(np.allclose(a ,		a ,		atol=1E-5    )    )


"""simple docstring"""






import warnings

from ...utils import logging
from .image_processing_convnext import ConvNextImageProcessor


__lowerCAmelCase				      =   logging.get_logger(__name__)
class 	_lowerCAmelCase    (      __snake_case       ):

							def __init__( self       :    Dict ,		*a       :    Any ,		**a       :    List[str]    )   ->    None:

									"""simple docstring"""
									warnings.warn(
									    '''The class ConvNextFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
									    ''' Please use ConvNextImageProcessor instead.''' ,		a ,		)
									super().__init__(*a ,		**a    )