



'''simple docstring'''

import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
				import jax
				import jax.numpy as jnp

@slow
@require_flax
class __snake_case(				unittest.TestCase ):


						'''simple docstring'''



						def     __snake_case   ( self     ,	A_     ,	A_				)     ->     Dict:
							return f'gaussian_noise_s={seed}_shape={"_".join([str(A_				) for s in shape]				)}.npy'


						def     __snake_case   ( self				)     ->     List[str]:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()


						def     __snake_case   ( self     ,	A_=0     ,	A_=(4, 4, 64, 64)     ,	A_=False				)     ->     List[Any]:
							lowerCAmelCase   =     jnp.bfloataa if fpaa else jnp.floataa
							lowerCAmelCase   =     jnp.array(load_hf_numpy(self.get_file_format(A_     ,	A_				)				)     ,	dtype=A_				)
							return image


						def     __snake_case   ( self     ,	A_=False     ,	A_="CompVis/stable-diffusion-v1-4"				)     ->     str:
							lowerCAmelCase   =     jnp.bfloataa if fpaa else jnp.floataa
							lowerCAmelCase   =     """bf16""" if fpaa else None

							lowerCAmelCase,       lowerCAmelCase   =     FlaxUNetaDConditionModel.from_pretrained(
							    A_     ,	subfolder="""unet"""     ,	dtype=A_     ,	revision=A_				)
							return model, params


						def     __snake_case   ( self     ,	A_=0     ,	A_=(4, 77, 768)     ,	A_=False				)     ->     List[str]:
							lowerCAmelCase   =     jnp.bfloataa if fpaa else jnp.floataa
							lowerCAmelCase   =     jnp.array(load_hf_numpy(self.get_file_format(A_     ,	A_				)				)     ,	dtype=A_				)
							return hidden_states


						@parameterized.expand(
						    [
						        # fmt: off
						        [83, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]],
						        [17, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]],
						        [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]],
						        [3, 1000, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]],
						        # fmt: on
						    ]				)
						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_				)     ->     List[str]:
							lowerCAmelCase,       lowerCAmelCase   =     self.get_unet_model(model_id="""CompVis/stable-diffusion-v1-4"""     ,	fpaa=A_				)
							lowerCAmelCase   =     self.get_latents(A_     ,	fpaa=A_				)
							lowerCAmelCase   =     self.get_encoder_hidden_states(A_     ,	fpaa=A_				)

							lowerCAmelCase   =     model.apply(
							    {"""params""": params}     ,	A_     ,	jnp.array(A_     ,	dtype=jnp.intaa				)     ,	encoder_hidden_states=A_     ,	).sample

							assert sample.shape == latents.shape

							lowerCAmelCase   =     jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())				)     ,	dtype=jnp.floataa				)
							lowerCAmelCase   =     jnp.array(A_     ,	dtype=jnp.floataa				)

							# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
							assert jnp.allclose(A_     ,	A_     ,	atol=1e-2				)




						@parameterized.expand(
						    [
						        # fmt: off
						        [83, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]],
						        [17, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]],
						        [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]],
						        [3, 1000, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]],
						        # fmt: on
						    ]				)
						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_				)     ->     Optional[Any]:
							lowerCAmelCase,       lowerCAmelCase   =     self.get_unet_model(model_id="""stabilityai/stable-diffusion-2"""     ,	fpaa=A_				)
							lowerCAmelCase   =     self.get_latents(A_     ,	shape=(4, 4, 96, 96)     ,	fpaa=A_				)
							lowerCAmelCase   =     self.get_encoder_hidden_states(A_     ,	shape=(4, 77, 1024)     ,	fpaa=A_				)

							lowerCAmelCase   =     model.apply(
							    {"""params""": params}     ,	A_     ,	jnp.array(A_     ,	dtype=jnp.intaa				)     ,	encoder_hidden_states=A_     ,	).sample

							assert sample.shape == latents.shape

							lowerCAmelCase   =     jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())				)     ,	dtype=jnp.floataa				)
							lowerCAmelCase   =     jnp.array(A_     ,	dtype=jnp.floataa				)

							# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
							assert jnp.allclose(A_     ,	A_     ,	atol=1e-2				)




'''simple docstring'''

from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType

class __snake_case(				_lowerCAmelCase ):


						'''simple docstring'''



						UpperCAmelCase							:   Union[str, Any]									=		["image_processor", "tokenizer"]
						UpperCAmelCase							:   Tuple									=		"BlipImageProcessor"
						UpperCAmelCase							:   Union[str, Any]									=		("BertTokenizer", "BertTokenizerFast")


						def __init__( self     ,	A_     ,	A_				)     ->     Dict:
							lowerCAmelCase   =     False
							super().__init__(A_     ,	A_				)
							lowerCAmelCase   =     self.image_processor


						def __call__( self     ,	A_ = None     ,	A_ = None     ,	A_ = True     ,	A_ = False     ,	A_ = None     ,	A_ = None     ,	A_ = 0     ,	A_ = None     ,	A_ = None     ,	A_ = False     ,	A_ = False     ,	A_ = False     ,	A_ = False     ,	A_ = False     ,	A_ = True     ,	A_ = None     ,	**A_     ,	)     ->     BatchEncoding:
							if images is None and text is None:
								raise ValueError("""You have to specify either images or text."""				)

							# Get only text
							if images is None:
								lowerCAmelCase   =     self.tokenizer
								lowerCAmelCase   =     self.tokenizer(
								    text=A_     ,	add_special_tokens=A_     ,	padding=A_     ,	truncation=A_     ,	max_length=A_     ,	stride=A_     ,	pad_to_multiple_of=A_     ,	return_attention_mask=A_     ,	return_overflowing_tokens=A_     ,	return_special_tokens_mask=A_     ,	return_offsets_mapping=A_     ,	return_token_type_ids=A_     ,	return_length=A_     ,	verbose=A_     ,	return_tensors=A_     ,	**A_     ,	)
								return text_encoding

							# add pixel_values
							lowerCAmelCase   =     self.image_processor(A_     ,	return_tensors=A_				)

							if text is not None:
								lowerCAmelCase   =     self.tokenizer(
								    text=A_     ,	add_special_tokens=A_     ,	padding=A_     ,	truncation=A_     ,	max_length=A_     ,	stride=A_     ,	pad_to_multiple_of=A_     ,	return_attention_mask=A_     ,	return_overflowing_tokens=A_     ,	return_special_tokens_mask=A_     ,	return_offsets_mapping=A_     ,	return_token_type_ids=A_     ,	return_length=A_     ,	verbose=A_     ,	return_tensors=A_     ,	**A_     ,	)
							else:
								lowerCAmelCase   =     None

							if text_encoding is not None:
								encoding_image_processor.update(A_				)

							return encoding_image_processor


						def     __snake_case   ( self     ,	*A_     ,	**A_				)     ->     Union[str, Any]:
							return self.tokenizer.batch_decode(*A_     ,	**A_				)


						def     __snake_case   ( self     ,	*A_     ,	**A_				)     ->     Tuple:
							return self.tokenizer.decode(*A_     ,	**A_				)




						@property
						def     __snake_case   ( self				)     ->     str:
							lowerCAmelCase   =     self.tokenizer.model_input_names
							lowerCAmelCase   =     self.image_processor.model_input_names
							return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names				)				)