




'''simple docstring'''







import subprocess
import sys

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


class 			_a  (	UpperCAmelCase__ ):
       @require_torch
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           UpperCAmelCase			       =		'''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

           UpperCAmelCase			       =		'''
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\")
        '''

           UpperCAmelCase			       =		'''
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
           UpperCAmelCase			       =		'''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
           UpperCAmelCase			       =		[sys.executable, '''-c''', '''\n'''.join([load, run, mock]				)]

           # should succeed
           UpperCAmelCase			       =		self.get_env()
           # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
           UpperCAmelCase			       =		'''1'''
           UpperCAmelCase			       =		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     A      (	self						:	List[str]				):



           '''simple docstring'''

           UpperCAmelCase			       =		'''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

           UpperCAmelCase			       =		'''
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\")
        '''

           UpperCAmelCase			       =		'''
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
           UpperCAmelCase			       =		'''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
           UpperCAmelCase			       =		[sys.executable, '''-c''', '''\n'''.join([load, run, mock]				)]

           # should succeed
           UpperCAmelCase			       =		self.get_env()
           UpperCAmelCase			       =		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     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		'''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

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

           UpperCAmelCase			       =		'''
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
           UpperCAmelCase			       =		[sys.executable, '''-c''', '''\n'''.join([load, run]				)]

           # should succeed
           UpperCAmelCase			       =		self.get_env()
           UpperCAmelCase			       =		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
           UpperCAmelCase			       =		[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
           UpperCAmelCase			       =		'''1'''
           UpperCAmelCase			       =		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     A      (	self						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		'''
from transformers import pipeline
        '''
           UpperCAmelCase			       =		'''
mname = \"hf-internal-testing/tiny-random-bert\"
pipe = pipeline(model=mname)
        '''

           UpperCAmelCase			       =		'''
import socket
def offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")
socket.socket = offline_socket
        '''
           UpperCAmelCase			       =		self.get_env()
           UpperCAmelCase			       =		'''1'''
           UpperCAmelCase			       =		[sys.executable, '''-c''', '''\n'''.join([load, mock, run]				)]
           UpperCAmelCase			       =		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     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		'''
from transformers import AutoModel
        '''
           UpperCAmelCase			       =		'''
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
           UpperCAmelCase			       =		[sys.executable, '''-c''', '''\n'''.join([load, run]				)]

           # should succeed
           UpperCAmelCase			       =		self.get_env()
           UpperCAmelCase			       =		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
           UpperCAmelCase			       =		'''1'''
           UpperCAmelCase			       =		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 argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration







def      UpperCamelCase_						(					snake_case_       :       Any      )					->     Optional[Any]:






 '''simple docstring'''

 __lowerCAmelCase											=						[
     """encoder.version""",
     """decoder.version""",
     """model.encoder.version""",
     """model.decoder.version""",
     """decoder.output_projection.weight""",
     """_float_tensor""",
     """encoder.embed_positions._float_tensor""",
     """decoder.embed_positions._float_tensor""",
 ]
 for k in ignore_keys:
  state_dict.pop(snake_case_						,		snake_case_      )







def      UpperCamelCase_						(					snake_case_       :       Optional[Any]      )					->     List[Any]:






 '''simple docstring'''

 __lowerCAmelCase							,       __lowerCAmelCase											=						emb.weight.shape
 __lowerCAmelCase											=						nn.Linear(snake_case_						,		snake_case_						,		bias=snake_case_      )
 __lowerCAmelCase											=						emb.weight.data
 return lin_layer







def      UpperCamelCase_						(					snake_case_       :       Any      )					->     Any:






 '''simple docstring'''

 __lowerCAmelCase											=						torch.load(snake_case_						,		map_location="""cpu"""      )
 __lowerCAmelCase											=						mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""]
 __lowerCAmelCase											=						mam_aaa["""model"""]
 remove_ignore_keys_(snake_case_      )
 __lowerCAmelCase											=						state_dict["""encoder.embed_tokens.weight"""].shape[0]

 __lowerCAmelCase											=						MaMaaaConfig(
     vocab_size=snake_case_						,		max_position_embeddings=10_24						,		encoder_layers=args.encoder_layers						,		decoder_layers=args.decoder_layers						,		encoder_attention_heads=args.encoder_attention_heads						,		decoder_attention_heads=args.decoder_attention_heads						,		encoder_ffn_dim=args.encoder_ffn_embed_dim						,		decoder_ffn_dim=args.decoder_ffn_embed_dim						,		d_model=args.encoder_embed_dim						,		encoder_layerdrop=args.encoder_layerdrop						,		decoder_layerdrop=args.decoder_layerdrop						,		dropout=args.dropout						,		attention_dropout=args.attention_dropout						,		activation_dropout=args.activation_dropout						,		activation_function="""relu"""						,		)

 __lowerCAmelCase											=						state_dict["""decoder.embed_tokens.weight"""]
 __lowerCAmelCase											=						MaMaaaForConditionalGeneration(snake_case_      )
 model.model.load_state_dict(snake_case_						,		strict=snake_case_      )
 __lowerCAmelCase											=						make_linear_from_emb(model.model.shared      )

 return model


if __name__ == "__main__":
 _A			:      Union[str, Any]					      =				argparse.ArgumentParser()
 # Required parameters
 parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''')
 parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
 _A			:      str					      =				parser.parse_args()
 _A			:      Optional[int]					      =				convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
 model.save_pretrained(args.pytorch_dump_folder_path)






