

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


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

__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(r'''\[(.+?)\]\((https://huggingface\.co/.+?)\)''')


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



def 				snake_case_(_UpperCamelCase					)							->     List[str]:



  """simple docstring"""






  _snake_case						=							None

  # source code of `config_class`
  _snake_case						=							inspect.getsource(_UpperCamelCase					)
  _snake_case						=							_re_checkpoint.findall(_UpperCamelCase					)

  # 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('''/'''					):
      _snake_case						=							ckpt_link[:-1]

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

  return checkpoint



def 				snake_case_()							->     Tuple:



  """simple docstring"""






  _snake_case						=							[]

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

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

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


if __name__ == "__main__":
 check_config_docstrings_have_checkpoints()




__A      		=     '''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'''



def 				snake_case_(_UpperCamelCase					)							->     bytes:



  """simple docstring"""






  if not isinstance(_UpperCamelCase			,	_UpperCamelCase					):
    _snake_case						=							F"""a bytes-like object is required, not '{data.__class__.__name__}'"""
    raise TypeError(_UpperCamelCase					)

  _snake_case						=							''''''.join(bin(_UpperCamelCase					)[2:].zfill(8					) for byte in data					)

  _snake_case						=							len(_UpperCamelCase					) % 6 != 0

  if padding_needed:
    # The padding that will be added later
    _snake_case						=							b'''=''' * ((6 - len(_UpperCamelCase					) % 6) // 2)

    # Append binary_stream with arbitrary binary digits (0's by default) to make its
    # length a multiple of 6.
    binary_stream += "0" * (6 - len(_UpperCamelCase					) % 6)
  else:
    _snake_case						=							b''''''

  # Encode every 6 binary digits to their corresponding Base64 character
  return (
      "".join(
          B64_CHARSET[int(binary_stream[index : index + 6]			,	2					)]
          for index in range(0			,	len(_UpperCamelCase					)			,	6					)					).encode()
      + padding
  )



def 				snake_case_(_UpperCamelCase					)							->     bytes:



  """simple docstring"""






  if not isinstance(_UpperCamelCase			,	_UpperCamelCase					) and not isinstance(_UpperCamelCase			,	_UpperCamelCase					):
    _snake_case						=							(
        '''argument should be a bytes-like object or ASCII string, '''
        F"""not '{encoded_data.__class__.__name__}'"""
    )
    raise TypeError(_UpperCamelCase					)

  # In case encoded_data is a bytes-like object, make sure it contains only
  # ASCII characters so we convert it to a string object
  if isinstance(_UpperCamelCase			,	_UpperCamelCase					):
    try:
      _snake_case						=							encoded_data.decode('''utf-8'''					)
    except UnicodeDecodeError:
      raise ValueError('''base64 encoded data should only contain ASCII characters'''					)

  _snake_case						=							encoded_data.count('''='''					)

  # Check if the encoded string contains non base64 characters
  if padding:
    assert all(
        char in B64_CHARSET for char in encoded_data[:-padding]					), "Invalid base64 character(s) found."
  else:
    assert all(
        char in B64_CHARSET for char in encoded_data					), "Invalid base64 character(s) found."

  # Check the padding
  assert len(_UpperCamelCase					) % 4 == 0 and padding < 3, "Incorrect padding"

  if padding:
    # Remove padding if there is one
    _snake_case						=							encoded_data[:-padding]

    _snake_case						=							''''''.join(
        bin(B64_CHARSET.index(_UpperCamelCase					)					)[2:].zfill(6					) for char in encoded_data					)[: -padding * 2]
  else:
    _snake_case						=							''''''.join(
        bin(B64_CHARSET.index(_UpperCamelCase					)					)[2:].zfill(6					) for char in encoded_data					)

  _snake_case						=							[
      int(binary_stream[index : index + 8]			,	2					)
      for index in range(0			,	len(_UpperCamelCase					)			,	8					)
  ]

  return bytes(_UpperCamelCase					)


if __name__ == "__main__":
 import doctest

 doctest.testmod()

