


import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand




class A						(     UpperCamelCase_  ):






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  ArgumentParser				)      ->		int:
             """simple docstring"""




             _lowerCamelCase :	str						=parser.add_parser('login'    ,     help='Log in using the same credentials as on huggingface.co'				)
             login_parser.set_defaults(func=lambda lowercase_				: LoginCommand(lowercase_				)				)
             _lowerCamelCase :	List[str]						=parser.add_parser('whoami'    ,     help='Find out which huggingface.co account you are logged in as.'				)
             whoami_parser.set_defaults(func=lambda lowercase_				: WhoamiCommand(lowercase_				)				)
             _lowerCamelCase :	Any						=parser.add_parser('logout'    ,     help='Log out'				)
             logout_parser.set_defaults(func=lambda lowercase_				: LogoutCommand(lowercase_				)				)

             # new system: git-based repo system
             _lowerCamelCase :	str						=parser.add_parser(
                 'repo'    ,     help='Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos.'    ,     )
             _lowerCamelCase :	Union[str, Any]						=repo_parser.add_subparsers(
                 help='Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands'				)
             _lowerCamelCase :	Optional[int]						=repo_subparsers.add_parser(
                 'create'    ,     help='Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co'				)
             repo_create_parser.add_argument(
                 'name'    ,     type=lowercase_    ,     help='Name for your model\'s repo. Will be namespaced under your username to build the model id.'    ,     )
             repo_create_parser.add_argument('--organization'    ,     type=lowercase_    ,     help='Optional: organization namespace.'				)
             repo_create_parser.add_argument('-y'    ,     '--yes'    ,     action='store_true'    ,     help='Optional: answer Yes to the prompt'				)
             repo_create_parser.set_defaults(func=lambda lowercase_				: RepoCreateCommand(lowercase_				)				)




class A						:
      UpperCamelCase__							:						int   ='\u001b[1m'
      UpperCamelCase__							:						List[Any]   ='\u001b[31m'
      UpperCamelCase__							:						int   ='\u001b[90m'
      UpperCamelCase__							:						int   ='\u001b[0m'






      @classmethod
      def  lowerCamelCase (      cls		:  str    ,     lowercase_		:  Optional[int]				)      ->		str:
             """simple docstring"""




             return F'''{cls._bold}{s}{cls._reset}'''






      @classmethod
      def  lowerCamelCase (      cls		:  Dict    ,     lowercase_		:  Optional[int]				)      ->		Any:
             """simple docstring"""




             return F'''{cls._bold}{cls._red}{s}{cls._reset}'''






      @classmethod
      def  lowerCamelCase (      cls		:  List[str]    ,     lowercase_		:  Union[str, Any]				)      ->		Union[str, Any]:
             """simple docstring"""




             return F'''{cls._gray}{s}{cls._reset}'''




def 			a_   (					SCREAMING_SNAKE_CASE__      :   List[List[Union[str, int]]]	,    SCREAMING_SNAKE_CASE__      :   List[str]  ):
       '''simple docstring'''





       _lowerCamelCase :	Optional[Any]						=[max(len(str(SCREAMING_SNAKE_CASE__  )  ) for x in col  ) for col in zip(*SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  )]
       _lowerCamelCase :	Tuple						=('{{:{}}} ' * len(SCREAMING_SNAKE_CASE__  )).format(*SCREAMING_SNAKE_CASE__  )
       _lowerCamelCase :	List[Any]						=[]
       lines.append(row_format.format(*SCREAMING_SNAKE_CASE__  )  )
       lines.append(row_format.format(*['-' * w for w in col_widths]  )  )
       for row in rows:
              lines.append(row_format.format(*SCREAMING_SNAKE_CASE__  )  )
       return "\n".join(SCREAMING_SNAKE_CASE__  )




class A						:






      def __init__(      self		:  Tuple    ,     lowercase_		:  Union[str, Any]				)      ->		Optional[Any]:
             """simple docstring"""




             _lowerCamelCase :	List[str]						=args




class A						(     UpperCamelCase_  ):






      def  lowerCamelCase (      self		:  List[Any]				)      ->		List[str]:
             """simple docstring"""




             print(
                 ANSI.red(
                     'ERROR! `huggingface-cli login` uses an outdated login mechanism '
                     'that is not compatible with the Hugging Face Hub backend anymore. '
                     'Please use `huggingface-cli login instead.'				)				)




class A						(     UpperCamelCase_  ):






      def  lowerCamelCase (      self		:  Optional[int]				)      ->		Union[str, Any]:
             """simple docstring"""




             print(
                 ANSI.red(
                     'WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use '
                     '`huggingface-cli whoami` instead.'				)				)
             _lowerCamelCase :	Optional[Any]						=HfFolder.get_token()
             if token is None:
                    print('Not logged in'				)
                    exit()
             try:
                    _lowerCamelCase			,  _lowerCamelCase :	Any						=whoami(lowercase_				)
                    print(lowercase_				)
                    if orgs:
                           print(ANSI.bold('orgs: '				)    ,     ','.join(lowercase_				)				)
             except HTTPError as e:
                    print(lowercase_				)
                    print(ANSI.red(e.response.text				)				)
                    exit(1				)




class A						(     UpperCamelCase_  ):






      def  lowerCamelCase (      self		:  int				)      ->		str:
             """simple docstring"""




             print(
                 ANSI.red(
                     'ERROR! `transformers-cli logout` uses an outdated logout mechanism '
                     'that is not compatible with the Hugging Face Hub backend anymore. '
                     'Please use `huggingface-cli logout instead.'				)				)




class A						(     UpperCamelCase_  ):






      def  lowerCamelCase (      self		:  Dict				)      ->		List[str]:
             """simple docstring"""




             print(
                 ANSI.red(
                     'WARNING! Managing repositories through transformers-cli is deprecated. '
                     'Please use `huggingface-cli` instead.'				)				)
             _lowerCamelCase :	int						=HfFolder.get_token()
             if token is None:
                    print('Not logged in'				)
                    exit(1				)
             try:
                    _lowerCamelCase :	Optional[int]						=subprocess.check_output(['git', '--version']				).decode('utf-8'				)
                    print(ANSI.gray(stdout.strip()				)				)
             except FileNotFoundError:
                    print('Looks like you do not have git installed, please install.'				)

             try:
                    _lowerCamelCase :	str						=subprocess.check_output(['git-lfs', '--version']				).decode('utf-8'				)
                    print(ANSI.gray(stdout.strip()				)				)
             except FileNotFoundError:
                    print(
                        ANSI.red(
                            'Looks like you do not have git-lfs installed, please install.'
                            ' You can install from https://git-lfs.github.com/.'
                            ' Then run `git lfs install` (you only have to do this once).'				)				)
             print(''				)

             _lowerCamelCase			,  _lowerCamelCase :	Optional[int]						=whoami(lowercase_				)
             _lowerCamelCase :	Union[str, Any]						=self.args.organization if self.args.organization is not None else user
             _lowerCamelCase :	str						=F'''{namespace}/{self.args.name}'''
             print(F'''You are about to create {ANSI.bold(lowercase_				)}'''				)

             if not self.args.yes:
                    _lowerCamelCase :	Union[str, Any]						=input('Proceed? [Y/n] '				).lower()
                    if not (choice == "" or choice == "y" or choice == "yes"):
                           print('Abort'				)
                           exit()
             try:
                    _lowerCamelCase :	List[Any]						=create_repo(lowercase_    ,     name=self.args.name    ,     organization=self.args.organization				)
             except HTTPError as e:
                    print(lowercase_				)
                    print(ANSI.red(e.response.text				)				)
                    exit(1				)
             print('\nYour repo now lives at:'				)
             print(F'''  {ANSI.bold(lowercase_				)}'''				)
             print('\nYou can clone it locally with the command below, and commit/push as usual.'				)
             print(F'''\n  git clone {url}'''				)
             print(''				)






import argparse
import pathlib

import fairseq
import torch
from fairseq.models.roberta import RobertaModel as FairseqRobertaModel
from fairseq.modules import TransformerSentenceEncoderLayer
from packaging import version

from transformers import RobertaConfig, RobertaForMaskedLM, RobertaForSequenceClassification
from transformers.models.bert.modeling_bert import (
    BertIntermediate,
    BertLayer,
    BertOutput,
    BertSelfAttention,
    BertSelfOutput,
)
from transformers.utils import logging


if version.parse(fairseq.__version__) < version.parse('0.9.0'):
 raise Exception('requires fairseq >= 0.9.0')


logging.set_verbosity_info()
lowerCamelCase		      = logging.get_logger(__name__)

lowerCamelCase		      = 'Hello world! cécé herlolip'




def 			a_   (					SCREAMING_SNAKE_CASE__      :   str	,    SCREAMING_SNAKE_CASE__      :   str	,    SCREAMING_SNAKE_CASE__      :   bool  ):
       '''simple docstring'''





       _lowerCamelCase :	Dict						=FairseqRobertaModel.from_pretrained(SCREAMING_SNAKE_CASE__  )
       roberta.eval()  # disable dropout
       _lowerCamelCase :	int						=roberta.model.encoder.sentence_encoder
       _lowerCamelCase :	Union[str, Any]						=RobertaConfig(
           vocab_size=roberta_sent_encoder.embed_tokens.num_embeddings	,    hidden_size=roberta.args.encoder_embed_dim	,    num_hidden_layers=roberta.args.encoder_layers	,    num_attention_heads=roberta.args.encoder_attention_heads	,    intermediate_size=roberta.args.encoder_ffn_embed_dim	,    max_position_embeddings=514	,    type_vocab_size=1	,    layer_norm_eps=1e-5	,    )
       if classification_head:
              _lowerCamelCase :	int						=roberta.model.classification_heads['mnli'].out_proj.weight.shape[0]
       print('Our BERT config:'	,    SCREAMING_SNAKE_CASE__  )

       _lowerCamelCase :	Optional[int]						=RobertaForSequenceClassification(SCREAMING_SNAKE_CASE__  ) if classification_head else RobertaForMaskedLM(SCREAMING_SNAKE_CASE__  )
       model.eval()

       # Now let's copy all the weights.
       # Embeddings
       _lowerCamelCase :	List[str]						=roberta_sent_encoder.embed_tokens.weight
       _lowerCamelCase :	int						=roberta_sent_encoder.embed_positions.weight
       _lowerCamelCase :	Tuple						=torch.zeros_like(
           model.roberta.embeddings.token_type_embeddings.weight  )  # just zero them out b/c RoBERTa doesn't use them.
       _lowerCamelCase :	Optional[Any]						=roberta_sent_encoder.emb_layer_norm.weight
       _lowerCamelCase :	int						=roberta_sent_encoder.emb_layer_norm.bias

       for i in range(config.num_hidden_layers  ):
              # Encoder: start of layer
              _lowerCamelCase :	BertLayer						=model.roberta.encoder.layer[i]
              _lowerCamelCase :	TransformerSentenceEncoderLayer						=roberta_sent_encoder.layers[i]

              # self attention
              _lowerCamelCase :	BertSelfAttention						=layer.attention.self
              assert (
                  roberta_layer.self_attn.k_proj.weight.data.shape
                  == roberta_layer.self_attn.q_proj.weight.data.shape
                  == roberta_layer.self_attn.v_proj.weight.data.shape
                  == torch.Size((config.hidden_size, config.hidden_size)  )
              )

              _lowerCamelCase :	Dict						=roberta_layer.self_attn.q_proj.weight
              _lowerCamelCase :	List[Any]						=roberta_layer.self_attn.q_proj.bias
              _lowerCamelCase :	Optional[Any]						=roberta_layer.self_attn.k_proj.weight
              _lowerCamelCase :	int						=roberta_layer.self_attn.k_proj.bias
              _lowerCamelCase :	Optional[Any]						=roberta_layer.self_attn.v_proj.weight
              _lowerCamelCase :	int						=roberta_layer.self_attn.v_proj.bias

              # self-attention output
              _lowerCamelCase :	BertSelfOutput						=layer.attention.output
              assert self_output.dense.weight.shape == roberta_layer.self_attn.out_proj.weight.shape
              _lowerCamelCase :	Union[str, Any]						=roberta_layer.self_attn.out_proj.weight
              _lowerCamelCase :	str						=roberta_layer.self_attn.out_proj.bias
              _lowerCamelCase :	List[Any]						=roberta_layer.self_attn_layer_norm.weight
              _lowerCamelCase :	List[Any]						=roberta_layer.self_attn_layer_norm.bias

              # intermediate
              _lowerCamelCase :	BertIntermediate						=layer.intermediate
              assert intermediate.dense.weight.shape == roberta_layer.fca.weight.shape
              _lowerCamelCase :	str						=roberta_layer.fca.weight
              _lowerCamelCase :	Optional[int]						=roberta_layer.fca.bias

              # output
              _lowerCamelCase :	BertOutput						=layer.output
              assert bert_output.dense.weight.shape == roberta_layer.fca.weight.shape
              _lowerCamelCase :	str						=roberta_layer.fca.weight
              _lowerCamelCase :	List[Any]						=roberta_layer.fca.bias
              _lowerCamelCase :	List[str]						=roberta_layer.final_layer_norm.weight
              _lowerCamelCase :	Optional[int]						=roberta_layer.final_layer_norm.bias
              # end of layer

       if classification_head:
              _lowerCamelCase :	Any						=roberta.model.classification_heads['mnli'].dense.weight
              _lowerCamelCase :	Optional[Any]						=roberta.model.classification_heads['mnli'].dense.bias
              _lowerCamelCase :	List[str]						=roberta.model.classification_heads['mnli'].out_proj.weight
              _lowerCamelCase :	Tuple						=roberta.model.classification_heads['mnli'].out_proj.bias
       else:
              # LM Head
              _lowerCamelCase :	Optional[Any]						=roberta.model.encoder.lm_head.dense.weight
              _lowerCamelCase :	Tuple						=roberta.model.encoder.lm_head.dense.bias
              _lowerCamelCase :	Tuple						=roberta.model.encoder.lm_head.layer_norm.weight
              _lowerCamelCase :	int						=roberta.model.encoder.lm_head.layer_norm.bias
              _lowerCamelCase :	Dict						=roberta.model.encoder.lm_head.weight
              _lowerCamelCase :	str						=roberta.model.encoder.lm_head.bias

       # Let's check that we get the same results.
       _lowerCamelCase :	torch.Tensor						=roberta.encode(SCREAMING_SNAKE_CASE__  ).unsqueeze(0  )  # batch of size 1

       _lowerCamelCase :	List[Any]						=model(SCREAMING_SNAKE_CASE__  )[0]
       if classification_head:
              _lowerCamelCase :	int						=roberta.model.classification_heads['mnli'](roberta.extract_features(SCREAMING_SNAKE_CASE__  )  )
       else:
              _lowerCamelCase :	int						=roberta.model(SCREAMING_SNAKE_CASE__  )[0]
       print(our_output.shape	,    their_output.shape  )
       _lowerCamelCase :	Any						=torch.max(torch.abs(our_output - their_output  )  ).item()
       print(F'''max_absolute_diff = {max_absolute_diff}'''  )  # ~ 1e-7
       _lowerCamelCase :	Optional[Any]						=torch.allclose(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__	,    atol=1e-3  )
       print('Do both models output the same tensors?'	,    '🔥' if success else '💩'  )
       if not success:
              raise Exception('Something went wRoNg'  )

       pathlib.Path(SCREAMING_SNAKE_CASE__  ).mkdir(parents=SCREAMING_SNAKE_CASE__	,    exist_ok=SCREAMING_SNAKE_CASE__  )
       print(F'''Saving model to {pytorch_dump_folder_path}'''  )
       model.save_pretrained(SCREAMING_SNAKE_CASE__  )


if __name__ == "__main__":
 lowerCamelCase		      = argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '--roberta_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.'
 )
 parser.add_argument(
     '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
 )
 parser.add_argument(
     '--classification_head', action='store_true', help='Whether to convert a final classification head.'
 )
 lowerCamelCase		      = parser.parse_args()
 convert_roberta_checkpoint_to_pytorch(
     args.roberta_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head
 )


