



"""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):




  def 		SCREAMING_SNAKE_CASE							(					self	:		Union[str, Any]		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		Tuple						):






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		str						):






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Union[str, Any]		,   __lowerCAmelCase	:		List[str]=0		,   __lowerCAmelCase	:		int=(4, 4, 6_4, 6_4)		,   __lowerCAmelCase	:		Dict=False						):






         """simple docstring"""






         _lowerCamelCase		:							Tuple            =					jnp.bfloataa if fpaa else jnp.floataa
         _lowerCamelCase		:							Union[str, Any]            =					jnp.array(load_hf_numpy(self.get_file_format(_lowercase		,   _lowercase						)						)		,   dtype=_lowercase						)
         return image




  def 		SCREAMING_SNAKE_CASE							(					self	:		Any		,   __lowerCAmelCase	:		List[str]=False		,   __lowerCAmelCase	:		Optional[int]="CompVis/stable-diffusion-v1-4"						):






         """simple docstring"""






         _lowerCamelCase		:							str            =					jnp.bfloataa if fpaa else jnp.floataa
         _lowerCamelCase		:							Dict            =					"bf16" if fpaa else None

         _lowerCamelCase		:							List[Any]            =					FlaxUNetaDConditionModel.from_pretrained(
             _lowercase		,   subfolder='''unet'''		,   dtype=_lowercase		,   revision=_lowercase						)
         return model, params




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]		,   __lowerCAmelCase	:		Optional[int]=0		,   __lowerCAmelCase	:		Union[str, Any]=(4, 7_7, 7_6_8)		,   __lowerCAmelCase	:		int=False						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					jnp.bfloataa if fpaa else jnp.floataa
         _lowerCamelCase		:							List[str]            =					jnp.array(load_hf_numpy(self.get_file_format(_lowercase		,   _lowercase						)						)		,   dtype=_lowercase						)
         return hidden_states




  @parameterized.expand(
      [
          # fmt: off
          [8_3, 4, [-0.23_23, -0.13_04, 0.08_13, -0.30_93, -0.09_19, -0.15_71, -0.11_25, -0.58_06]],
          [1_7, 0.55, [-0.08_31, -0.24_43, 0.09_01, -0.09_19, 0.33_96, 0.01_03, -0.37_43, 0.07_01]],
          [8, 0.89, [-0.48_63, 0.08_59, 0.08_75, -0.16_58, 0.91_99, -0.01_14, 0.48_39, 0.46_39]],
          [3, 1_0_0_0, [-0.56_49, 0.24_02, -0.55_18, 0.12_48, 1.13_28, -0.24_43, -0.03_25, -1.00_78]],
          # fmt: on
      ]						)
  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Optional[Any]						):






         """simple docstring"""






         _lowerCamelCase		:							int            =					self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4'''		,   fpaa=_lowercase						)
         _lowerCamelCase		:							Union[str, Any]            =					self.get_latents(_lowercase		,   fpaa=_lowercase						)
         _lowerCamelCase		:							int            =					self.get_encoder_hidden_states(_lowercase		,   fpaa=_lowercase						)

         _lowerCamelCase		:							Tuple            =					model.apply(
             {'''params''': params}		,   _lowercase		,   jnp.array(_lowercase		,   dtype=jnp.intaa						)		,   encoder_hidden_states=_lowercase		,   ).sample

         assert sample.shape == latents.shape

         _lowerCamelCase		:							Optional[Any]            =					jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())						)		,   dtype=jnp.floataa						)
         _lowerCamelCase		:							Any            =					jnp.array(_lowercase		,   dtype=jnp.floataa						)

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




  @parameterized.expand(
      [
          # fmt: off
          [8_3, 4, [0.15_14, 0.08_07, 0.16_24, 0.10_16, -0.18_96, 0.02_63, 0.06_77, 0.23_10]],
          [1_7, 0.55, [0.11_64, -0.02_16, 0.01_70, 0.15_89, -0.31_20, 0.10_05, -0.05_81, -0.14_58]],
          [8, 0.89, [-0.17_58, -0.01_69, 0.10_04, -0.14_11, 0.13_12, 0.11_03, -0.19_96, 0.21_39]],
          [3, 1_0_0_0, [0.12_14, 0.03_52, -0.07_31, -0.15_62, -0.09_94, -0.09_06, -0.23_40, -0.05_39]],
          # fmt: on
      ]						)
  def 		SCREAMING_SNAKE_CASE							(					self	:		List[Any]		,   __lowerCAmelCase	:		List[str]		,   __lowerCAmelCase	:		Any		,   __lowerCAmelCase	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							Union[str, Any]            =					self.get_unet_model(model_id='''stabilityai/stable-diffusion-2'''		,   fpaa=_lowercase						)
         _lowerCamelCase		:							Union[str, Any]            =					self.get_latents(_lowercase		,   shape=(4, 4, 9_6, 9_6)		,   fpaa=_lowercase						)
         _lowerCamelCase		:							str            =					self.get_encoder_hidden_states(_lowercase		,   shape=(4, 7_7, 1_0_2_4)		,   fpaa=_lowercase						)

         _lowerCamelCase		:							Tuple            =					model.apply(
             {'''params''': params}		,   _lowercase		,   jnp.array(_lowercase		,   dtype=jnp.intaa						)		,   encoder_hidden_states=_lowercase		,   ).sample

         assert sample.shape == latents.shape

         _lowerCamelCase		:							Optional[int]            =					jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())						)		,   dtype=jnp.floataa						)
         _lowerCamelCase		:							int            =					jnp.array(_lowercase		,   dtype=jnp.floataa						)

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







"""simple docstring"""







import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
A		=						'src/transformers'


# This is to make sure the transformers module imported is the one in the repo.
A		=						importlib.util.spec_from_file_location(
    'transformers',
    os.path.join(PATH_TO_TRANSFORMERS, '__init__.py'),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
A		=						spec.loader.load_module()

A		=						transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
A		=						re.compile('\[(.+?)\]\((https://huggingface\.co/.+?)\)')


A		=						{
    'CLIPConfigMixin',
    'DecisionTransformerConfigMixin',
    'EncoderDecoderConfigMixin',
    'RagConfigMixin',
    'SpeechEncoderDecoderConfigMixin',
    'VisionEncoderDecoderConfigMixin',
    'VisionTextDualEncoderConfigMixin',
}

def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						snake_case   :			Union[str, Any]						    = []

						for config_class in list(CONFIG_MAPPING.values() ):
												snake_case   :			List[str]						    = False

												# source code of `config_class`
												snake_case   :			Optional[int]						    = inspect.getsource(lowerCamelCase_ )
												snake_case   :			Optional[int]						    = _re_checkpoint.findall(lowerCamelCase_ )

												for checkpoint in checkpoints:
																		# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
																		# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
																		snake_case    ,			snake_case   :			Tuple						    = checkpoint

																		# verify the checkpoint name corresponds to the checkpoint link
																		snake_case   :			Any						    = f'''https://huggingface.co/{ckpt_name}'''
																		if ckpt_link == ckpt_link_from_name:
																								snake_case   :			List[str]						    = True
																								break

												snake_case   :			Any						    = config_class.__name__
												if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
																		configs_without_checkpoint.append(lowerCamelCase_ )

						if len(lowerCamelCase_ ) > 0:
												snake_case   :			List[str]						    = "\n".join(sorted(lowerCamelCase_ ) )
												raise ValueError(f'''The following configurations don\'t contain any valid checkpoint:\n{message}''' )


if __name__ == "__main__":
			check_config_docstrings_have_checkpoints()



