



'''simple docstring'''


import inspect
import re

from transformers.utils import direct_transformers_import


# 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
lowercase							       =			'''src/transformers'''


# This is to make sure the transformers module imported is the one in the repo.
lowercase							       =			direct_transformers_import(PATH_TO_TRANSFORMERS)

lowercase							       =			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)`
lowercase							       =			re.compile(R'''\[(.+?)\]\((https://huggingface\.co/.+?)\)''')


lowercase							       =			{
    '''DecisionTransformerConfig''',
    '''EncoderDecoderConfig''',
    '''MusicgenConfig''',
    '''RagConfig''',
    '''SpeechEncoderDecoderConfig''',
    '''TimmBackboneConfig''',
    '''VisionEncoderDecoderConfig''',
    '''VisionTextDualEncoderConfig''',
    '''LlamaConfig''',
}






def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		=None

     # source code of `config_class`
     a_       		=inspect.getsource(lowercase__       )
     a_       		=_re_checkpoint.findall(lowercase__       )

     # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
     # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
     for ckpt_name, ckpt_link in checkpoints:
          # allow the link to end with `/`
          if ckpt_link.endswith("/"       ):
               a_       		=ckpt_link[:-1]

          # verify the checkpoint name corresponds to the checkpoint link
          a_       		=F"""https://huggingface.co/{ckpt_name}"""
          if ckpt_link == ckpt_link_from_name:
               a_       		=ckpt_name
               break

     return checkpoint






def  UpperCAmelCase_       (		):


     '''simple docstring'''







     a_       		=[]

     for config_class in list(CONFIG_MAPPING.values()       ):
          # Skip deprecated models
          if "models.deprecated" in config_class.__module__:
               continue
          a_       		=get_checkpoint_from_config_class(lowercase__       )

          a_       		=config_class.__name__
          if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
               configs_without_checkpoint.append(lowercase__       )

     if len(lowercase__       ) > 0:
          a_       		="\n".join(sorted(lowercase__       )       )
          raise ValueError(F"""The following configurations don't contain any valid checkpoint:\n{message}"""       )


if __name__ == "__main__":
  check_config_docstrings_have_checkpoints()







'''simple docstring'''


import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch







@require_torch
class 	UpperCAmelCase	:


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      return self.get_dummy_input()




 @property
 def       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







      if self.block_type == "down":
           return (4, 3_2, 1_6, 1_6)
      elif self.block_type == "mid":
           return (4, 3_2, 3_2, 3_2)
      elif self.block_type == "up":
           return (4, 3_2, 6_4, 6_4)

      raise ValueError(f"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""")




 def       lowercase_  (   self							,  lowerCAmelCase_=True							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  ) ->      Dict:



      """simple docstring"""







      a_       		=4
      a_       		=3_2
      a_       		=(3_2, 3_2)

      a_       		=torch.manual_seed(0)
      a_       		=torch.device(lowerCAmelCase_)
      a_       		=(batch_size, num_channels) + sizes
      a_       		=randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)
      a_       		={"hidden_states": hidden_states}

      if include_temb:
           a_       		=1_2_8
           a_       		=randn_tensor((batch_size, temb_channels)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      if include_res_hidden_states_tuple:
           a_       		=torch.manual_seed(1)
           a_       		=(randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_),)

      if include_encoder_hidden_states:
           a_       		=floats_tensor((batch_size, 3_2, 3_2)).to(lowerCAmelCase_)

      if include_skip_sample:
           a_       		=randn_tensor(((batch_size, 3) + sizes)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      return dummy_input




 def       lowercase_  (   self) ->      Optional[int]:



      """simple docstring"""







      a_       		={
          "in_channels": 3_2,
          "out_channels": 3_2,
          "temb_channels": 1_2_8,
      }
      if self.block_type == "up":
           a_       		=3_2

      if self.block_type == "mid":
           init_dict.pop("out_channels")

      a_       		=self.dummy_input
      return init_dict, inputs_dict




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      unet_block.to(lowerCAmelCase_)
      unet_block.eval()

      with torch.no_grad():
           a_       		=unet_block(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      self.assertEqual(output.shape							,  self.output_shape)

      a_       		=output[0, -1, -3:, -3:]
      a_       		=torch.tensor(lowerCAmelCase_).to(lowerCAmelCase_)
      assert torch_all_close(output_slice.flatten()							,  lowerCAmelCase_							,  atol=5e-3)




 @unittest.skipIf(torch_device == "mps"							,  "Training is not supported in mps")
 def       lowercase_  (   self) ->      Optional[Any]:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      model.to(lowerCAmelCase_)
      model.train()
      a_       		=model(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      a_       		=torch.device(lowerCAmelCase_)
      a_       		=randn_tensor(output.shape							,  device=lowerCAmelCase_)
      a_       		=torch.nn.functional.mse_loss(lowerCAmelCase_							,  lowerCAmelCase_)
      loss.backward()


