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






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											return F'''gaussian_noise_s={seed}_shape={'_'.join([str(__SCREAMING_SNAKE_CASE		) for s in shape]		)}.npy'''






					def 	_snake_case    (   self		):


											"""simple docstring"""





											super().tearDown()
											gc.collect()






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=(4, 4, 64, 64)				,							__SCREAMING_SNAKE_CASE=False		):


											"""simple docstring"""





											lowercase_	:     Any													=       jnp.bfloataa if fpaa else jnp.floataa
											lowercase_	:     int													=       jnp.array(load_hf_numpy(self.get_file_format(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)		)				,							dtype=__SCREAMING_SNAKE_CASE		)
											return image






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE="CompVis/stable-diffusion-v1-4"		):


											"""simple docstring"""





											lowercase_	:     Union[str, Any]													=       jnp.bfloataa if fpaa else jnp.floataa
											lowercase_	:     Union[str, Any]													=       '''bf16''' if fpaa else None

											lowercase_						, lowercase_	:     Dict													=       FlaxUNetaDConditionModel.from_pretrained(
											    __SCREAMING_SNAKE_CASE				,							subfolder='''unet'''				,							dtype=__SCREAMING_SNAKE_CASE				,							revision=__SCREAMING_SNAKE_CASE		)
											return model, params






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=(4, 77, 7_68)				,							__SCREAMING_SNAKE_CASE=False		):


											"""simple docstring"""





											lowercase_	:     Optional[Any]													=       jnp.bfloataa if fpaa else jnp.floataa
											lowercase_	:     List[str]													=       jnp.array(load_hf_numpy(self.get_file_format(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)		)				,							dtype=__SCREAMING_SNAKE_CASE		)
											return hidden_states






					@parameterized.expand(
					    [
					        # fmt: off
					        [83, 4, [-0.2_323, -0.1_304, 0.0_813, -0.3_093, -0.0_919, -0.1_571, -0.1_125, -0.5_806]],
					        [17, 0.55, [-0.0_831, -0.2_443, 0.0_901, -0.0_919, 0.3_396, 0.0_103, -0.3_743, 0.0_701]],
					        [8, 0.89, [-0.4_863, 0.0_859, 0.0_875, -0.1_658, 0.9_199, -0.0_114, 0.4_839, 0.4_639]],
					        [3, 10_00, [-0.5_649, 0.2_402, -0.5_518, 0.1_248, 1.1_328, -0.2_443, -0.0_325, -1.0_078]],
					        # fmt: on
					    ]		)
					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_						, lowercase_	:     Tuple													=       self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4'''				,							fpaa=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Any													=       self.get_latents(__SCREAMING_SNAKE_CASE				,							fpaa=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[Any]													=       self.get_encoder_hidden_states(__SCREAMING_SNAKE_CASE				,							fpaa=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       model.apply(
											    {'''params''': params}				,							__SCREAMING_SNAKE_CASE				,							jnp.array(__SCREAMING_SNAKE_CASE				,							dtype=jnp.intaa		)				,							encoder_hidden_states=__SCREAMING_SNAKE_CASE				,							).sample

											assert sample.shape == latents.shape

											lowercase_	:     Dict													=       jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())		)				,							dtype=jnp.floataa		)
											lowercase_	:     Tuple													=       jnp.array(__SCREAMING_SNAKE_CASE				,							dtype=jnp.floataa		)

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






					@parameterized.expand(
					    [
					        # fmt: off
					        [83, 4, [0.1_514, 0.0_807, 0.1_624, 0.1_016, -0.1_896, 0.0_263, 0.0_677, 0.2_310]],
					        [17, 0.55, [0.1_164, -0.0_216, 0.0_170, 0.1_589, -0.3_120, 0.1_005, -0.0_581, -0.1_458]],
					        [8, 0.89, [-0.1_758, -0.0_169, 0.1_004, -0.1_411, 0.1_312, 0.1_103, -0.1_996, 0.2_139]],
					        [3, 10_00, [0.1_214, 0.0_352, -0.0_731, -0.1_562, -0.0_994, -0.0_906, -0.2_340, -0.0_539]],
					        # fmt: on
					    ]		)
					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_						, lowercase_	:     Union[str, Any]													=       self.get_unet_model(model_id='''stabilityai/stable-diffusion-2'''				,							fpaa=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     List[Any]													=       self.get_latents(__SCREAMING_SNAKE_CASE				,							shape=(4, 4, 96, 96)				,							fpaa=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[Any]													=       self.get_encoder_hidden_states(__SCREAMING_SNAKE_CASE				,							shape=(4, 77, 10_24)				,							fpaa=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       model.apply(
											    {'''params''': params}				,							__SCREAMING_SNAKE_CASE				,							jnp.array(__SCREAMING_SNAKE_CASE				,							dtype=jnp.intaa		)				,							encoder_hidden_states=__SCREAMING_SNAKE_CASE				,							).sample

											assert sample.shape == latents.shape

											lowercase_	:     Optional[int]													=       jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())		)				,							dtype=jnp.floataa		)
											lowercase_	:     List[Any]													=       jnp.array(__SCREAMING_SNAKE_CASE				,							dtype=jnp.floataa		)

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





'''simple docstring'''







import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowercase :  Optional[Any]    							=       logging.get_logger(__name__)

_lowercase :  List[str]    							=       {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}






class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_text_model'''
					lowerCAmelCase_        =     ['''past_key_values''']
					lowerCAmelCase_        =     {
					    '''hidden_size''': '''hidden_size''',
					    '''num_attention_heads''': '''num_heads''',
					    '''num_hidden_layers''': '''num_layers''',
					}






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=5_02_44				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=1				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											lowercase_	:     Any													=       vocab_size
											lowercase_	:     Tuple													=       hidden_size
											lowercase_	:     Optional[Any]													=       d_kv
											lowercase_	:     List[str]													=       d_ff
											lowercase_	:     List[str]													=       num_layers
											lowercase_	:     Optional[Any]													=       num_heads
											lowercase_	:     Union[str, Any]													=       relative_attention_num_buckets
											lowercase_	:     Optional[int]													=       relative_attention_max_distance
											lowercase_	:     Union[str, Any]													=       dropout_rate
											lowercase_	:     Dict													=       layer_norm_epsilon
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     List[Any]													=       use_cache

											lowercase_	:     Optional[int]													=       eos_token_id
											lowercase_	:     Optional[int]													=       decoder_start_token_id

											# for backwards compatibility
											lowercase_	:     Any													=       dense_act_fn

											super().__init__(
											    pad_token_id=__SCREAMING_SNAKE_CASE				,							eos_token_id=__SCREAMING_SNAKE_CASE				,							decoder_start_token_id=__SCREAMING_SNAKE_CASE				,							tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE				,							)






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     Optional[int]													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the text config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     List[Any]													=       config_dict['''text_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_vision_model'''






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=1E-1_0				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=40_96				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Union[str, Any]													=       hidden_size
											lowercase_	:     Any													=       patch_embed_hidden_size
											lowercase_	:     List[Any]													=       d_ff
											lowercase_	:     Dict													=       dropout_rate
											lowercase_	:     Any													=       num_hidden_layers
											lowercase_	:     Any													=       num_attention_heads
											lowercase_	:     int													=       initializer_range
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     Dict													=       attention_dropout
											lowercase_	:     Optional[Any]													=       layer_norm_eps
											lowercase_	:     str													=       dense_act_fn
											lowercase_	:     Dict													=       seq_len
											lowercase_	:     List[Any]													=       relative_attention_num_buckets
											lowercase_	:     int													=       relative_attention_max_distance
											lowercase_	:     Optional[int]													=       d_kv






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     str													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the vision config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     Optional[int]													=       config_dict['''vision_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct'''
					lowerCAmelCase_        =     True






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_encoder_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											if text_config is None:
																	lowercase_	:     Optional[Any]													=       {}
																	logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''		)

											if vision_config is None:
																	lowercase_	:     Dict													=       {}
																	logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''		)

											lowercase_	:     str													=       PixaStructTextConfig(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       PixaStructVisionConfig(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       self.text_config.decoder_start_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.pad_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.eos_token_id

											lowercase_	:     int													=       initializer_factor
											lowercase_	:     Any													=       initializer_range

											lowercase_	:     str													=       self.initializer_range
											lowercase_	:     str													=       self.initializer_range

											lowercase_	:     int													=       is_vqa






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											return cls(text_config=text_config.to_dict()				,							vision_config=vision_config.to_dict()				,							**__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       copy.deepcopy(self.__dict__		)
											lowercase_	:     Any													=       self.text_config.to_dict()
											lowercase_	:     Optional[Any]													=       self.vision_config.to_dict()
											lowercase_	:     Optional[int]													=       self.__class__.model_type
											return output



