
# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os

from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES
from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType
from ...utils.imports import is_botoa_available
from .config_args import SageMakerConfig
from .config_utils import (
    DYNAMO_BACKENDS,
    _ask_field,
    _ask_options,
    _convert_dynamo_backend,
    _convert_mixed_precision,
    _convert_sagemaker_distributed_mode,
    _convert_yes_no_to_bool,
)


if is_botoa_available():
   import botoa  # noqa: F401






def   lowerCamelCase__							(					a      )      ->   Optional[int]:
   _A:  str					=       botoa.client('''iam'''      )

   _A:  Optional[Any]					=       {
       '''Version''': '''2012-10-17''',
       '''Statement''': [
           {'''Effect''': '''Allow''', '''Principal''': {'''Service''': '''sagemaker.amazonaws.com'''}, '''Action''': '''sts:AssumeRole'''}
       ],
   }
   try:
      # create the role, associated with the chosen trust policy
      iam_client.create_role(
          RoleName=a		,		AssumeRolePolicyDocument=json.dumps(a		,		indent=2      )      )
      _A:  Optional[Any]					=       {
          '''Version''': '''2012-10-17''',
          '''Statement''': [
              {
                  '''Effect''': '''Allow''',
                  '''Action''': [
                      '''sagemaker:*''',
                      '''ecr:GetDownloadUrlForLayer''',
                      '''ecr:BatchGetImage''',
                      '''ecr:BatchCheckLayerAvailability''',
                      '''ecr:GetAuthorizationToken''',
                      '''cloudwatch:PutMetricData''',
                      '''cloudwatch:GetMetricData''',
                      '''cloudwatch:GetMetricStatistics''',
                      '''cloudwatch:ListMetrics''',
                      '''logs:CreateLogGroup''',
                      '''logs:CreateLogStream''',
                      '''logs:DescribeLogStreams''',
                      '''logs:PutLogEvents''',
                      '''logs:GetLogEvents''',
                      '''s3:CreateBucket''',
                      '''s3:ListBucket''',
                      '''s3:GetBucketLocation''',
                      '''s3:GetObject''',
                      '''s3:PutObject''',
                  ],
                  '''Resource''': '''*''',
              }
          ],
      }
      # attach policy to role
      iam_client.put_role_policy(
          RoleName=a		,		PolicyName=f"""{role_name}_policy_permission"""		,		PolicyDocument=json.dumps(a		,		indent=2      )		,		)
   except iam_client.exceptions.EntityAlreadyExistsException:
      print(f"""role {role_name} already exists. Using existing one"""      )






def   lowerCamelCase__							(					a      )      ->   Tuple:
   _A:  Optional[int]					=       botoa.client('''iam'''      )
   return iam_client.get_role(RoleName=a      )["Role"]["Arn"]






def   lowerCamelCase__							(					)      ->   List[str]:
   _A:  str					=       _ask_options(
       '''How do you want to authorize?'''		,		['''AWS Profile''', '''Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) ''']		,		a		,		)
   _A:  int					=       None
   if credentials_configuration == 0:
      _A:  Optional[Any]					=       _ask_field('''Enter your AWS Profile name: [default] '''		,		default='''default'''      )
      _A:  Union[str, Any]					=       aws_profile
   else:
      print(
          '''Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,'''
          '''`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`'''      )
      _A:  Optional[Any]					=       _ask_field('''AWS Access Key ID: '''      )
      _A:  List[str]					=       aws_access_key_id

      _A:  int					=       _ask_field('''AWS Secret Access Key: '''      )
      _A:  int					=       aws_secret_access_key

   _A:  Optional[Any]					=       _ask_field('''Enter your AWS Region: [us-east-1]'''		,		default='''us-east-1'''      )
   _A:  Tuple					=       aws_region

   _A:  List[Any]					=       _ask_options(
       '''Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?'''		,		['''Provide IAM Role name''', '''Create new IAM role using credentials''']		,		a		,		)
   if role_management == 0:
      _A:  List[Any]					=       _ask_field('''Enter your IAM role name: '''      )
   else:
      _A:  Optional[int]					=       '''accelerate_sagemaker_execution_role'''
      print(f"""Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials"""      )
      _create_iam_role_for_sagemaker(a      )

   _A:  Any					=       _ask_field(
       '''Do you want to use custom Docker image? [yes/NO]: '''		,		_convert_yes_no_to_bool		,		default=a		,		error_message='''Please enter yes or no.'''		,		)
   _A:  Union[str, Any]					=       None
   if is_custom_docker_image:
      _A:  Optional[Any]					=       _ask_field('''Enter your Docker image: '''		,		lambda a      : str(a      ).lower()      )

   _A:  Optional[Any]					=       _ask_field(
       '''Do you want to provide SageMaker input channels with data locations? [yes/NO]: '''		,		_convert_yes_no_to_bool		,		default=a		,		error_message='''Please enter yes or no.'''		,		)
   _A:  str					=       None
   if is_sagemaker_inputs_enabled:
      _A:  str					=       _ask_field(
          '''Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): '''		,		lambda a      : str(a      ).lower()		,		)

   _A:  Union[str, Any]					=       _ask_field(
       '''Do you want to enable SageMaker metrics? [yes/NO]: '''		,		_convert_yes_no_to_bool		,		default=a		,		error_message='''Please enter yes or no.'''		,		)
   _A:  List[str]					=       None
   if is_sagemaker_metrics_enabled:
      _A:  Optional[int]					=       _ask_field(
          '''Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): '''		,		lambda a      : str(a      ).lower()		,		)

   _A:  Union[str, Any]					=       _ask_options(
       '''What is the distributed mode?'''		,		['''No distributed training''', '''Data parallelism''']		,		_convert_sagemaker_distributed_mode		,		)
   _A:  List[Any]					=       {}
   _A:  str					=       _ask_field(
       '''Do you wish to optimize your script with torch dynamo?[yes/NO]:'''		,		_convert_yes_no_to_bool		,		default=a		,		error_message='''Please enter yes or no.'''		,		)
   if use_dynamo:
      _A:  str					=       '''dynamo_'''
      _A:  str					=       _ask_options(
          '''Which dynamo backend would you like to use?'''		,		[x.lower() for x in DYNAMO_BACKENDS]		,		_convert_dynamo_backend		,		default=2		,		)
      _A:  List[str]					=       _ask_field(
          '''Do you want to customize the defaults sent to torch.compile? [yes/NO]: '''		,		_convert_yes_no_to_bool		,		default=a		,		error_message='''Please enter yes or no.'''		,		)

      if use_custom_options:
         _A:  Any					=       _ask_options(
             '''Which mode do you want to use?'''		,		a		,		lambda a      : TORCH_DYNAMO_MODES[int(a      )]		,		default='''default'''		,		)
         _A:  List[str]					=       _ask_field(
             '''Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: '''		,		_convert_yes_no_to_bool		,		default=a		,		error_message='''Please enter yes or no.'''		,		)
         _A:  Optional[Any]					=       _ask_field(
             '''Do you want to enable dynamic shape tracing? [yes/NO]: '''		,		_convert_yes_no_to_bool		,		default=a		,		error_message='''Please enter yes or no.'''		,		)
   _A:  List[str]					=       '''Which EC2 instance type you want to use for your training?'''
   if distributed_type != SageMakerDistributedType.NO:
      _A:  Any					=       _ask_options(
          a		,		a		,		lambda a      : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(a      )]      )
   else:
      eca_instance_query += "? [ml.p3.2xlarge]:"
      _A:  Dict					=       _ask_field(a		,		lambda a      : str(a      ).lower()		,		default='''ml.p3.2xlarge'''      )

   _A:  Tuple					=       1
   if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
      _A:  Union[str, Any]					=       _ask_field(
          '''How many machines do you want use? [1]: '''		,		a		,		default=1		,		)

   _A:  Tuple					=       _ask_options(
       '''Do you wish to use FP16 or BF16 (mixed precision)?'''		,		['''no''', '''fp16''', '''bf16''', '''fp8''']		,		_convert_mixed_precision		,		)

   if use_dynamo and mixed_precision == "no":
      print(
          '''Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.'''      )

   return SageMakerConfig(
       image_uri=a		,		compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER		,		distributed_type=a		,		use_cpu=a		,		dynamo_config=a		,		eca_instance_type=a		,		profile=a		,		region=a		,		iam_role_name=a		,		mixed_precision=a		,		num_machines=a		,		sagemaker_inputs_file=a		,		sagemaker_metrics_file=a		,		)





import argparse

from torch import nn

# transformers_old should correspond to branch `save_old_prophetnet_model_structure` here
# original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively
from transformers_old.modeling_prophetnet import (
    ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld,
)
from transformers_old.modeling_xlm_prophetnet import (
    XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld,
)

from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging


UpperCAmelCase__		:     List[Any]     			=      logging.get_logger(__name__)
logging.set_verbosity_info()






def   lowerCamelCase__							(					a		,		a      )      ->   Dict:
   if "xprophetnet" in prophetnet_checkpoint_path:
      _A:  List[Any]					=       XLMProphetNetForConditionalGenerationOld.from_pretrained(a      )
      _A			,  _A:  Union[str, Any]					=       XLMProphetNetForConditionalGeneration.from_pretrained(
          a		,		output_loading_info=a      )
   else:
      _A:  Dict					=       ProphetNetForConditionalGenerationOld.from_pretrained(a      )
      _A			,  _A:  Tuple					=       ProphetNetForConditionalGeneration.from_pretrained(
          a		,		output_loading_info=a      )

   _A:  Optional[int]					=       ['''key_proj''', '''value_proj''', '''query_proj''']

   _A:  List[Any]					=       {
       '''self_attn''': '''ngram_self_attn''',
       '''cross_attn''': '''encoder_attn''',
       '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''',
       '''feed_forward_layer_norm''': '''final_layer_norm''',
       '''feed_forward''': '''''',
       '''intermediate''': '''fc1''',
       '''output''': '''fc2''',
       '''key_proj''': '''k_proj''',
       '''query_proj''': '''q_proj''',
       '''value_proj''': '''v_proj''',
       '''word_embeddings''': '''embed_tokens''',
       '''embeddings_layer_norm''': '''emb_layer_norm''',
       '''relative_pos_embeddings''': '''relative_linear''',
       '''ngram_embeddings''': '''ngram_input_embed''',
       '''position_embeddings''': '''embed_positions''',
   }

   for key in loading_info["missing_keys"]:
      _A:  List[str]					=       key.split('''.'''      )

      if attributes[0] == "lm_head":
         _A:  Optional[int]					=       prophet
         _A:  Tuple					=       prophet_old
      else:
         _A:  Tuple					=       prophet.prophetnet
         _A:  Any					=       prophet_old.model

      _A:  int					=       False
      for attribute in attributes:
         if attribute in mapping:
            _A:  Optional[int]					=       mapping[attribute]
            if not hasattr(a		,		a      ) and len(a      ) > 0:
               _A:  int					=       attribute
         elif hasattr(a		,		a      ):
            _A:  Tuple					=       attribute

         if attribute == "weight":
            assert old_model.weight.shape == model.weight.shape, "Shapes have to match!"
            _A:  Union[str, Any]					=       old_model.weight
            logger.info(f"""{attribute} is initialized."""      )
            _A:  Any					=       True
            break
         elif attribute == "bias":
            assert old_model.bias.shape == model.bias.shape, "Shapes have to match!"
            _A:  str					=       old_model.bias
            logger.info(f"""{attribute} is initialized"""      )
            _A:  Dict					=       True
            break
         elif attribute in special_keys and hasattr(a		,		'''in_proj_weight'''      ):
            _A:  Optional[int]					=       old_model.in_proj_weight.shape[0] // 3
            _A:  Tuple					=       getattr(a		,		a      )
            param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match"
            param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match"
            if attribute == "query_proj":
               _A:  List[str]					=       nn.Parameter(old_model.in_proj_weight[:embed_dim, :]      )
               _A:  List[Any]					=       nn.Parameter(old_model.in_proj_bias[:embed_dim]      )

            elif attribute == "key_proj":
               _A:  int					=       nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :]      )
               _A:  Optional[int]					=       nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim]      )
            elif attribute == "value_proj":
               _A:  List[Any]					=       nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :]      )
               _A:  int					=       nn.Parameter(old_model.in_proj_bias[2 * embed_dim :]      )
            _A:  Tuple					=       True
            break
         elif attribute == "position_embeddings":
            assert (
                model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1]
            ), "Hidden size has to match"
            assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings."
            _A:  Union[str, Any]					=       nn.Parameter(old_model.embed_positions.weight[:5_12, :]      )
            _A:  List[Any]					=       True
            break

         if attribute.isdigit():
            _A:  Tuple					=       model[int(a      )]
            _A:  int					=       old_model[int(a      )]
         else:
            _A:  Union[str, Any]					=       getattr(a		,		a      )

            if old_attribute == "":
               _A:  Union[str, Any]					=       old_model
            else:
               if not hasattr(a		,		a      ):
                  raise ValueError(f"""{old_model} does not have {old_attribute}"""      )
               _A:  List[Any]					=       getattr(a		,		a      )

      if not is_key_init:
         raise ValueError(f"""{key} was not correctly initialized!"""      )

   print(f"""Saving model to {pytorch_dump_folder_path}"""      )
   prophet.save_pretrained(a      )


if __name__ == "__main__":
   UpperCAmelCase__		:     Dict     			=      argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       '--prophetnet_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.'
   )
   UpperCAmelCase__		:     Tuple     			=      parser.parse_args()
   convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)



