
'''simple docstring'''





import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch

class 						a   (				__magic_name__    ):




				@require_torch
				def 	__snake_case							(							self	:				int      ):
										# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
										# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
										# while running an external program

										# python one-liner segments

										# this must be loaded before socket.socket is monkey-patched
										snake_case       :     int =    '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

										snake_case       :     str =    '''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

										snake_case       :     str =    '''
import socket
def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")
socket.socket = offline_socket
        '''

										# Force fetching the files so that we can use the cache
										snake_case       :     Optional[int] =    '''hf-internal-testing/tiny-random-bert'''
										BertConfig.from_pretrained(SCREAMING_SNAKE_CASE_      )
										BertModel.from_pretrained(SCREAMING_SNAKE_CASE_      )
										BertTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_      )
										pipeline(task='''fill-mask''',    model=SCREAMING_SNAKE_CASE_      )

										# baseline - just load from_pretrained with normal network
										snake_case       :     int =    [sys.executable, '''-c''', '''\n'''.join([load, run, mock]      )]

										# should succeed
										snake_case       :     str =    self.get_env()
										# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
										snake_case       :     int =    '''1'''
										snake_case       :     Any =    subprocess.run(SCREAMING_SNAKE_CASE_,    env=SCREAMING_SNAKE_CASE_,    check=SCREAMING_SNAKE_CASE_,    capture_output=SCREAMING_SNAKE_CASE_      )
										self.assertEqual(result.returncode,    0,    result.stderr      )
										self.assertIn('''success''',    result.stdout.decode()      )




				@require_torch
				def 	__snake_case							(							self	:				str      ):
										# python one-liner segments
										# this must be loaded before socket.socket is monkey-patched
										snake_case       :     Any =    '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

										snake_case       :     Optional[Any] =    '''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

										snake_case       :     Tuple =    '''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")
socket.socket = offline_socket
        '''

										# Force fetching the files so that we can use the cache
										snake_case       :     Union[str, Any] =    '''hf-internal-testing/tiny-random-bert'''
										BertConfig.from_pretrained(SCREAMING_SNAKE_CASE_      )
										BertModel.from_pretrained(SCREAMING_SNAKE_CASE_      )
										BertTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_      )
										pipeline(task='''fill-mask''',    model=SCREAMING_SNAKE_CASE_      )

										# baseline - just load from_pretrained with normal network
										snake_case       :     Dict =    [sys.executable, '''-c''', '''\n'''.join([load, run, mock]      )]

										# should succeed
										snake_case       :     Optional[Any] =    self.get_env()
										snake_case       :     Dict =    subprocess.run(SCREAMING_SNAKE_CASE_,    env=SCREAMING_SNAKE_CASE_,    check=SCREAMING_SNAKE_CASE_,    capture_output=SCREAMING_SNAKE_CASE_      )
										self.assertEqual(result.returncode,    0,    result.stderr      )
										self.assertIn('''success''',    result.stdout.decode()      )




				@require_torch
				def 	__snake_case							(							self	:				Dict      ):
										# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
										# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
										# while running an external program

										# python one-liner segments

										# this must be loaded before socket.socket is monkey-patched
										snake_case       :     Optional[Any] =    '''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

										snake_case       :     str =    '''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

										snake_case       :     List[Any] =    '''
import socket
def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")
socket.socket = offline_socket
        '''

										# baseline - just load from_pretrained with normal network
										snake_case       :     Optional[int] =    [sys.executable, '''-c''', '''\n'''.join([load, run]      )]

										# should succeed
										snake_case       :     List[Any] =    self.get_env()
										snake_case       :     Any =    subprocess.run(SCREAMING_SNAKE_CASE_,    env=SCREAMING_SNAKE_CASE_,    check=SCREAMING_SNAKE_CASE_,    capture_output=SCREAMING_SNAKE_CASE_      )
										self.assertEqual(result.returncode,    0,    result.stderr      )
										self.assertIn('''success''',    result.stdout.decode()      )

										# next emulate no network
										snake_case       :     Optional[Any] =    [sys.executable, '''-c''', '''\n'''.join([load, mock, run]      )]

										# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
										# env["TRANSFORMERS_OFFLINE"] = "0"
										# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
										# self.assertEqual(result.returncode, 1, result.stderr)

										# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
										snake_case       :     Union[str, Any] =    '''1'''
										snake_case       :     List[Any] =    subprocess.run(SCREAMING_SNAKE_CASE_,    env=SCREAMING_SNAKE_CASE_,    check=SCREAMING_SNAKE_CASE_,    capture_output=SCREAMING_SNAKE_CASE_      )
										self.assertEqual(result.returncode,    0,    result.stderr      )
										self.assertIn('''success''',    result.stdout.decode()      )




				@require_torch
				def 	__snake_case							(							self	:				int      ):
										snake_case       :     Any =    '''
from transformers import pipeline
        '''
										snake_case       :     int =    '''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

										snake_case       :     List[str] =    '''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
        '''
										snake_case       :     Dict =    self.get_env()
										snake_case       :     str =    '''1'''
										snake_case       :     Any =    [sys.executable, '''-c''', '''\n'''.join([load, mock, run]      )]
										snake_case       :     Union[str, Any] =    subprocess.run(SCREAMING_SNAKE_CASE_,    env=SCREAMING_SNAKE_CASE_,    check=SCREAMING_SNAKE_CASE_,    capture_output=SCREAMING_SNAKE_CASE_      )
										self.assertEqual(result.returncode,    1,    result.stderr      )
										self.assertIn(
										    '''You cannot infer task automatically within `pipeline` when using offline mode''',    result.stderr.decode().replace('''\n''',    ''''''      ),    )




				@require_torch
				def 	__snake_case							(							self	:				Optional[Any]      ):
										snake_case       :     Dict =    '''
from transformers import AutoModel
        '''
										snake_case       :     List[Any] =    '''
mname = "hf-internal-testing/test_dynamic_model"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print("success")
        '''

										# baseline - just load from_pretrained with normal network
										snake_case       :     int =    [sys.executable, '''-c''', '''\n'''.join([load, run]      )]

										# should succeed
										snake_case       :     Optional[Any] =    self.get_env()
										snake_case       :     Union[str, Any] =    subprocess.run(SCREAMING_SNAKE_CASE_,    env=SCREAMING_SNAKE_CASE_,    check=SCREAMING_SNAKE_CASE_,    capture_output=SCREAMING_SNAKE_CASE_      )
										self.assertEqual(result.returncode,    0,    result.stderr      )
										self.assertIn('''success''',    result.stdout.decode()      )

										# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
										snake_case       :     Union[str, Any] =    '''1'''
										snake_case       :     Union[str, Any] =    subprocess.run(SCREAMING_SNAKE_CASE_,    env=SCREAMING_SNAKE_CASE_,    check=SCREAMING_SNAKE_CASE_,    capture_output=SCREAMING_SNAKE_CASE_      )
										self.assertEqual(result.returncode,    0,    result.stderr      )
										self.assertIn('''success''',    result.stdout.decode()      )



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


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

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


UpperCAmelCase    					=   {
    "DecisionTransformerConfig",
    "EncoderDecoderConfig",
    "MusicgenConfig",
    "RagConfig",
    "SpeechEncoderDecoderConfig",
    "TimmBackboneConfig",
    "VisionEncoderDecoderConfig",
    "VisionTextDualEncoderConfig",
    "LlamaConfig",
}

def      A (       A_							:					Union[str, Any]       ):
						snake_case       :     Tuple =    None

						# source code of `config_class`
						snake_case       :     Union[str, Any] =    inspect.getsource(A_       )
						snake_case       :     List[Any] =    _re_checkpoint.findall(A_       )

						# 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       :     Optional[int] =    ckpt_link[:-1]

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

						return checkpoint

def      A (       ):
						snake_case       :     List[str] =    []

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

												snake_case       :     Optional[Any] =    config_class.__name__
												if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
																		configs_without_checkpoint.append(A_       )

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


if __name__ == "__main__":
						check_config_docstrings_have_checkpoints()

