


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 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[Any]    =  "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

             lowerCamelCase__		:				List[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__		:				str    =  "\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__		:				Tuple    =  "hf-internal-testing/tiny-random-bert"
             BertConfig.from_pretrained(__lowerCamelCase       )
             BertModel.from_pretrained(__lowerCamelCase       )
             BertTokenizer.from_pretrained(__lowerCamelCase       )
             pipeline(task="fill-mask"					,      model=__lowerCamelCase       )

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

             # should succeed
             lowerCamelCase__		:				List[Any]    =  self.get_env()
             # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
             lowerCamelCase__		:				int    =  "1"
             lowerCamelCase__		:				Optional[Any]    =  subprocess.run(__lowerCamelCase					,      env=__lowerCamelCase					,      check=__lowerCamelCase					,      capture_output=__lowerCamelCase       )
             self.assertEqual(result.returncode					,      0					,      result.stderr       )
             self.assertIn("success"					,      result.stdout.decode()       )





       @require_torch
       def 						lowerCAmelCase     (   self							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[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__		:				Dict    =  "\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__		:				List[str]    =  "hf-internal-testing/tiny-random-bert"
             BertConfig.from_pretrained(__lowerCamelCase       )
             BertModel.from_pretrained(__lowerCamelCase       )
             BertTokenizer.from_pretrained(__lowerCamelCase       )
             pipeline(task="fill-mask"					,      model=__lowerCamelCase       )

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

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





       @require_torch
       def 						lowerCAmelCase     (   self							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				Dict    =  "\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        "

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

             lowerCamelCase__		:				Optional[Any]    =  "\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__		:				List[str]    =  [sys.executable, "-c", "\n".join([load, run]       )]

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

             # next emulate no network
             lowerCamelCase__		:				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
             lowerCamelCase__		:				int    =  "1"
             lowerCamelCase__		:				List[str]    =  subprocess.run(__lowerCamelCase					,      env=__lowerCamelCase					,      check=__lowerCamelCase					,      capture_output=__lowerCamelCase       )
             self.assertEqual(result.returncode					,      0					,      result.stderr       )
             self.assertIn("success"					,      result.stdout.decode()       )





       @require_torch
       def 						lowerCAmelCase     (   self							:			Union[str, Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  "\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__		:				int    =  self.get_env()
             lowerCamelCase__		:				Optional[int]    =  "1"
             lowerCamelCase__		:				Tuple    =  [sys.executable, "-c", "\n".join([load, mock, run]       )]
             lowerCamelCase__		:				Union[str, Any]    =  subprocess.run(__lowerCamelCase					,      env=__lowerCamelCase					,      check=__lowerCamelCase					,      capture_output=__lowerCamelCase       )
             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 						lowerCAmelCase     (   self							:			Optional[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Union[str, Any]    =  "\nfrom transformers import AutoModel\n        "
             lowerCamelCase__		:				List[str]    =  "\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__		:				Optional[int]    =  [sys.executable, "-c", "\n".join([load, run]       )]

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






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A      :    str		=					logging.get_logger(__name__)

A      :    int		=					{
    "alibaba-damo/mgp-str-base": "https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json",
}







class 			_lowercase (							lowercase__):


       """simple docstring"""




       A__											=			"mgp-str"





       def __init__(   self							:			List[str]					,      __lowerCamelCase							:			List[Any]=[32, 128]					,      __lowerCamelCase							:			Optional[int]=4					,      __lowerCamelCase							:			str=3					,      __lowerCamelCase							:			Optional[Any]=27					,      __lowerCamelCase							:			List[str]=38					,      __lowerCamelCase							:			Dict=50257					,      __lowerCamelCase							:			List[Any]=30522					,      __lowerCamelCase							:			Optional[Any]=768					,      __lowerCamelCase							:			str=12					,      __lowerCamelCase							:			Optional[int]=12					,      __lowerCamelCase							:			List[str]=4.0					,      __lowerCamelCase							:			Union[str, Any]=True					,      __lowerCamelCase							:			Any=False					,      __lowerCamelCase							:			Dict=1E-5					,      __lowerCamelCase							:			int=0.0					,      __lowerCamelCase							:			Optional[int]=0.0					,      __lowerCamelCase							:			List[Any]=0.0					,      __lowerCamelCase							:			Union[str, Any]=False					,      __lowerCamelCase							:			str=0.0_2					,      **__lowerCamelCase							:			Dict					,      ):


             '''simple docstring'''



             super().__init__(**__lowerCamelCase       )

             lowerCamelCase__		:				int    =  image_size
             lowerCamelCase__		:				Union[str, Any]    =  patch_size
             lowerCamelCase__		:				Dict    =  num_channels
             lowerCamelCase__		:				Union[str, Any]    =  max_token_length
             lowerCamelCase__		:				Optional[int]    =  num_character_labels
             lowerCamelCase__		:				Union[str, Any]    =  num_bpe_labels
             lowerCamelCase__		:				Optional[int]    =  num_wordpiece_labels
             lowerCamelCase__		:				List[str]    =  hidden_size
             lowerCamelCase__		:				Dict    =  num_hidden_layers
             lowerCamelCase__		:				Any    =  num_attention_heads
             lowerCamelCase__		:				Any    =  mlp_ratio
             lowerCamelCase__		:				List[Any]    =  distilled
             lowerCamelCase__		:				Optional[Any]    =  layer_norm_eps
             lowerCamelCase__		:				Union[str, Any]    =  drop_rate
             lowerCamelCase__		:				List[Any]    =  qkv_bias
             lowerCamelCase__		:				int    =  attn_drop_rate
             lowerCamelCase__		:				List[Any]    =  drop_path_rate
             lowerCamelCase__		:				List[str]    =  output_aa_attentions
             lowerCamelCase__		:				Dict    =  initializer_range


