




"""simple docstring"""
import subprocess
import sys

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

class __lowercase(      lowercase__				):




					'''simple docstring'''


					@require_torch
					def     snake_case_				(    self     ):
								# 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
								__lowerCamelCase					:	str	       =				'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

								__lowerCamelCase					:	Union[str, Any]	       =				'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

								__lowerCamelCase					:	Union[str, Any]	       =				'\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n        '

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

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

								# should succeed
								__lowerCamelCase					:	int	       =				self.get_env()
								# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
								__lowerCamelCase					:	Optional[int]	       =				'1'
								__lowerCamelCase					:	Optional[int]	       =				subprocess.run(__a						,	env=__a						,	check=__a						,	capture_output=__a     )
								self.assertEqual(result.returncode						,	0						,	result.stderr     )
								self.assertIn('success'						,	result.stdout.decode()     )
					@require_torch
					def     snake_case_				(    self     ):
								# python one-liner segments
								# this must be loaded before socket.socket is monkey-patched
								__lowerCamelCase					:	Tuple	       =				'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

								__lowerCamelCase					:	List[str]	       =				'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

								__lowerCamelCase					:	Tuple	       =				'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n        '

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

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

								# should succeed
								__lowerCamelCase					:	int	       =				self.get_env()
								__lowerCamelCase					:	Union[str, Any]	       =				subprocess.run(__a						,	env=__a						,	check=__a						,	capture_output=__a     )
								self.assertEqual(result.returncode						,	0						,	result.stderr     )
								self.assertIn('success'						,	result.stdout.decode()     )
					@require_torch
					def     snake_case_				(    self     ):
								# 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
								__lowerCamelCase					:	Dict	       =				'\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

								__lowerCamelCase					:	List[Any]	       =				'\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

								__lowerCamelCase					:	int	       =				'\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n        '

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

								# should succeed
								__lowerCamelCase					:	Any	       =				self.get_env()
								__lowerCamelCase					:	int	       =				subprocess.run(__a						,	env=__a						,	check=__a						,	capture_output=__a     )
								self.assertEqual(result.returncode						,	0						,	result.stderr     )
								self.assertIn('success'						,	result.stdout.decode()     )

								# next emulate no network
								__lowerCamelCase					:	Tuple	       =				[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
								__lowerCamelCase					:	Dict	       =				'1'
								__lowerCamelCase					:	Union[str, Any]	       =				subprocess.run(__a						,	env=__a						,	check=__a						,	capture_output=__a     )
								self.assertEqual(result.returncode						,	0						,	result.stderr     )
								self.assertIn('success'						,	result.stdout.decode()     )
					@require_torch
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Union[str, Any]	       =				'\nfrom transformers import pipeline\n        '
								__lowerCamelCase					:	Dict	       =				'\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

								__lowerCamelCase					:	Optional[int]	       =				'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
								__lowerCamelCase					:	Tuple	       =				self.get_env()
								__lowerCamelCase					:	Dict	       =				'1'
								__lowerCamelCase					:	List[str]	       =				[sys.executable, '-c', '\n'.join([load, mock, run]     )]
								__lowerCamelCase					:	List[Any]	       =				subprocess.run(__a						,	env=__a						,	check=__a						,	capture_output=__a     )
								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     ):
								__lowerCamelCase					:	int	       =				'\nfrom transformers import AutoModel\n        '
								__lowerCamelCase					:	Any	       =				'\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n        '

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

								# should succeed
								__lowerCamelCase					:	Dict	       =				self.get_env()
								__lowerCamelCase					:	Optional[Any]	       =				subprocess.run(__a						,	env=__a						,	check=__a						,	capture_output=__a     )
								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
								__lowerCamelCase					:	Dict	       =				'1'
								__lowerCamelCase					:	int	       =				subprocess.run(__a						,	env=__a						,	check=__a						,	capture_output=__a     )
								self.assertEqual(result.returncode						,	0						,	result.stderr     )
								self.assertIn('success'						,	result.stdout.decode()     )









"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_				:    Tuple										=     logging.get_logger(__name__)

a_				:    str										=     {'''openai-gpt''': '''https://huggingface.co/openai-gpt/resolve/main/config.json'''}

class __lowercase(      lowercase__				):




					'''simple docstring'''


					__a						:						Union[str, Any]     		= 'openai-gpt'
					__a						:						List[Any]     		= {
					    'max_position_embeddings': 'n_positions',
					    'hidden_size': 'n_embd',
					    'num_attention_heads': 'n_head',
					    'num_hidden_layers': 'n_layer',
					}
					def __init__(    self						,	__a=40478						,	__a=512						,	__a=768						,	__a=12						,	__a=12						,	__a="gelu"						,	__a=0.1						,	__a=0.1						,	__a=0.1						,	__a=1E-5						,	__a=0.02						,	__a="cls_index"						,	__a=True						,	__a=None						,	__a=True						,	__a=0.1						,	**__a						,	):
								__lowerCamelCase					:	List[str]	       =				vocab_size
								__lowerCamelCase					:	Union[str, Any]	       =				n_positions
								__lowerCamelCase					:	Optional[Any]	       =				n_embd
								__lowerCamelCase					:	int	       =				n_layer
								__lowerCamelCase					:	List[str]	       =				n_head
								__lowerCamelCase					:	Dict	       =				afn
								__lowerCamelCase					:	Optional[Any]	       =				resid_pdrop
								__lowerCamelCase					:	int	       =				embd_pdrop
								__lowerCamelCase					:	int	       =				attn_pdrop
								__lowerCamelCase					:	Dict	       =				layer_norm_epsilon
								__lowerCamelCase					:	List[str]	       =				initializer_range
								__lowerCamelCase					:	Any	       =				summary_type
								__lowerCamelCase					:	Tuple	       =				summary_use_proj
								__lowerCamelCase					:	List[Any]	       =				summary_activation
								__lowerCamelCase					:	str	       =				summary_first_dropout
								__lowerCamelCase					:	Tuple	       =				summary_proj_to_labels
								super().__init__(**__a     )



