

import json
import os
import shutil
import tempfile
from unittest import TestCase

from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow
from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available


if is_torch_available() and is_datasets_available() and is_faiss_available():
					from transformers.models.rag.configuration_rag import RagConfig
					from transformers.models.rag.tokenization_rag import RagTokenizer

@require_faiss
@require_torch
class __UpperCamelCase     (				_a		):






					'''simple docstring'''



					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	List[str]									= tempfile.mkdtemp()
						UpperCAmelCase__:	Optional[int]									= 8

						# DPR tok
						UpperCAmelCase__:	Any									= [
						    "[UNK]",
						    "[CLS]",
						    "[SEP]",
						    "[PAD]",
						    "[MASK]",
						    "want",
						    "##want",
						    "##ed",
						    "wa",
						    "un",
						    "runn",
						    "##ing",
						    ",",
						    "low",
						    "lowest",
						]
						UpperCAmelCase__:	Tuple									= os.path.join(self.tmpdirname				,      "dpr_tokenizer"						)
						os.makedirs(lowerCamelCase__				,      exist_ok=lowerCamelCase__						)
						UpperCAmelCase__:	str									= os.path.join(lowerCamelCase__				,      DPR_VOCAB_FILES_NAMES["vocab_file"]						)
						with open(self.vocab_file				,      "w"				,      encoding="utf-8"						) as vocab_writer:
							vocab_writer.write("".join([x + "\n" for x in vocab_tokens]						)						)

						# BART tok
						UpperCAmelCase__:	List[str]									= [
						    "l",
						    "o",
						    "w",
						    "e",
						    "r",
						    "s",
						    "t",
						    "i",
						    "d",
						    "n",
						    "\u0120",
						    "\u0120l",
						    "\u0120n",
						    "\u0120lo",
						    "\u0120low",
						    "er",
						    "\u0120lowest",
						    "\u0120newer",
						    "\u0120wider",
						    "<unk>",
						]
						UpperCAmelCase__:	Tuple									= dict(zip(lowerCamelCase__				,      range(len(lowerCamelCase__						)						)						)						)
						UpperCAmelCase__:	Tuple									= ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""]
						UpperCAmelCase__:	List[str]									= {"unk_token": "<unk>"}

						UpperCAmelCase__:	Optional[int]									= os.path.join(self.tmpdirname				,      "bart_tokenizer"						)
						os.makedirs(lowerCamelCase__				,      exist_ok=lowerCamelCase__						)
						UpperCAmelCase__:	Optional[int]									= os.path.join(lowerCamelCase__				,      BART_VOCAB_FILES_NAMES["vocab_file"]						)
						UpperCAmelCase__:	str									= os.path.join(lowerCamelCase__				,      BART_VOCAB_FILES_NAMES["merges_file"]						)
						with open(self.vocab_file				,      "w"				,      encoding="utf-8"						) as fp:
							fp.write(json.dumps(lowerCamelCase__						) + "\n"						)
						with open(self.merges_file				,      "w"				,      encoding="utf-8"						) as fp:
							fp.write("\n".join(lowerCamelCase__						)						)





					def    _UpperCAmelCase    (				self						):
						return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname				,      "dpr_tokenizer"						)						)





					def    _UpperCAmelCase    (				self						):
						return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname				,      "bart_tokenizer"						)						)





					def    _UpperCAmelCase    (				self						):
						shutil.rmtree(self.tmpdirname						)





					@require_tokenizers
					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Dict									= os.path.join(self.tmpdirname				,      "rag_tokenizer"						)
						UpperCAmelCase__:	Any									= RagConfig(question_encoder=DPRConfig().to_dict()				,      generator=BartConfig().to_dict()						)
						UpperCAmelCase__:	str									= RagTokenizer(question_encoder=self.get_dpr_tokenizer()				,      generator=self.get_bart_tokenizer()						)
						rag_config.save_pretrained(lowerCamelCase__						)
						rag_tokenizer.save_pretrained(lowerCamelCase__						)
						UpperCAmelCase__:	Optional[int]									= RagTokenizer.from_pretrained(lowerCamelCase__				,      config=lowerCamelCase__						)
						self.assertIsInstance(new_rag_tokenizer.question_encoder				,      lowerCamelCase__						)
						self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab()				,      rag_tokenizer.question_encoder.get_vocab()						)
						self.assertIsInstance(new_rag_tokenizer.generator				,      lowerCamelCase__						)
						self.assertEqual(new_rag_tokenizer.generator.get_vocab()				,      rag_tokenizer.generator.get_vocab()						)





					@slow
					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Optional[int]									= RagTokenizer.from_pretrained("facebook/rag-token-nq"						)
						UpperCAmelCase__:	Dict									= [
						    "who got the first nobel prize in physics",
						    "when is the next deadpool movie being released",
						    "which mode is used for short wave broadcast service",
						    "who is the owner of reading football club",
						    "when is the next scandal episode coming out",
						    "when is the last time the philadelphia won the superbowl",
						    "what is the most current adobe flash player version",
						    "how many episodes are there in dragon ball z",
						    "what is the first step in the evolution of the eye",
						    "where is gall bladder situated in human body",
						    "what is the main mineral in lithium batteries",
						    "who is the president of usa right now",
						    "where do the greasers live in the outsiders",
						    "panda is a national animal of which country",
						    "what is the name of manchester united stadium",
						]
						UpperCAmelCase__:	str									= tokenizer(lowerCamelCase__						)
						self.assertIsNotNone(lowerCamelCase__						)





					@slow
					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	List[Any]									= RagTokenizer.from_pretrained("facebook/rag-sequence-nq"						)
						UpperCAmelCase__:	Union[str, Any]									= [
						    "who got the first nobel prize in physics",
						    "when is the next deadpool movie being released",
						    "which mode is used for short wave broadcast service",
						    "who is the owner of reading football club",
						    "when is the next scandal episode coming out",
						    "when is the last time the philadelphia won the superbowl",
						    "what is the most current adobe flash player version",
						    "how many episodes are there in dragon ball z",
						    "what is the first step in the evolution of the eye",
						    "where is gall bladder situated in human body",
						    "what is the main mineral in lithium batteries",
						    "who is the president of usa right now",
						    "where do the greasers live in the outsiders",
						    "panda is a national animal of which country",
						    "what is the name of manchester united stadium",
						]
						UpperCAmelCase__:	Union[str, Any]									= tokenizer(lowerCamelCase__						)
						self.assertIsNotNone(lowerCamelCase__						)


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
_lowerCAmelCase  :      int			 ="""src/transformers"""


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

_lowerCAmelCase  :      List[str]			 =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)`
_lowerCAmelCase  :      int			 =re.compile(r"""\[(.+?)\]\((https://huggingface\.co/.+?)\)""")


_lowerCAmelCase  :      Union[str, Any]			 ={
    """DecisionTransformerConfig""",
    """EncoderDecoderConfig""",
    """MusicgenConfig""",
    """RagConfig""",
    """SpeechEncoderDecoderConfig""",
    """TimmBackboneConfig""",
    """VisionEncoderDecoderConfig""",
    """VisionTextDualEncoderConfig""",
    """LlamaConfig""",
}



def 			_A	(      SCREAMING_SNAKE_CASE      ):
	UpperCAmelCase__:	str									= None

	# source code of `config_class`
	UpperCAmelCase__:	Optional[int]									= inspect.getsource(SCREAMING_SNAKE_CASE      )
	UpperCAmelCase__:	List[Any]									= _re_checkpoint.findall(SCREAMING_SNAKE_CASE      )

	# 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("/"      ):
			UpperCAmelCase__:	Any									= ckpt_link[:-1]

		# verify the checkpoint name corresponds to the checkpoint link
		UpperCAmelCase__:	int									= f"https://huggingface.co/{ckpt_name}"
		if ckpt_link == ckpt_link_from_name:
			UpperCAmelCase__:	Optional[int]									= ckpt_name
			break

	return checkpoint



def 			_A	(      ):
	UpperCAmelCase__:	List[str]									= []

	for config_class in list(CONFIG_MAPPING.values()      ):
		# Skip deprecated models
		if "models.deprecated" in config_class.__module__:
			continue
		UpperCAmelCase__:	Optional[Any]									= get_checkpoint_from_config_class(SCREAMING_SNAKE_CASE      )

		UpperCAmelCase__:	List[str]									= config_class.__name__
		if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
			configs_without_checkpoint.append(SCREAMING_SNAKE_CASE      )

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


if __name__ == "__main__":
					check_config_docstrings_have_checkpoints()