


'''simple docstring'''
from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


lowerCAmelCase						=					logging.get_logger(__name__)

lowerCAmelCase						=					{
    """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json""",
    # See all Marian models at https://huggingface.co/models?filter=marian
}






class        lowerCAmelCase_  (     UpperCAmelCase	):
       __UpperCAmelCase					='marian'
       __UpperCAmelCase					=['past_key_values']
       __UpperCAmelCase					={'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}






       def __init__( self			,     _UpperCamelCase=5_8101			,     _UpperCamelCase=None			,     _UpperCamelCase=1024			,     _UpperCamelCase=12			,     _UpperCamelCase=4096			,     _UpperCamelCase=16			,     _UpperCamelCase=12			,     _UpperCamelCase=4096			,     _UpperCamelCase=16			,     _UpperCamelCase=0.0			,     _UpperCamelCase=0.0			,     _UpperCamelCase=True			,     _UpperCamelCase=True			,     _UpperCamelCase="gelu"			,     _UpperCamelCase=1024			,     _UpperCamelCase=0.1			,     _UpperCamelCase=0.0			,     _UpperCamelCase=0.0			,     _UpperCamelCase=0.02			,     _UpperCamelCase=5_8100			,     _UpperCamelCase=False			,     _UpperCamelCase=5_8100			,     _UpperCamelCase=0			,     _UpperCamelCase=0			,     _UpperCamelCase=True			,     **_UpperCamelCase			,     )->      str:
            _A				=							vocab_size
            _A				=							decoder_vocab_size or vocab_size
            _A				=							max_position_embeddings
            _A				=							d_model
            _A				=							encoder_ffn_dim
            _A				=							encoder_layers
            _A				=							encoder_attention_heads
            _A				=							decoder_ffn_dim
            _A				=							decoder_layers
            _A				=							decoder_attention_heads
            _A				=							dropout
            _A				=							attention_dropout
            _A				=							activation_dropout
            _A				=							activation_function
            _A				=							init_std
            _A				=							encoder_layerdrop
            _A				=							decoder_layerdrop
            _A				=							use_cache
            _A				=							encoder_layers
            _A				=							scale_embedding  # scale factor will be sqrt(d_model) if True
            _A				=							share_encoder_decoder_embeddings
            super().__init__(
                pad_token_id=_UpperCamelCase			,     eos_token_id=_UpperCamelCase			,     is_encoder_decoder=_UpperCamelCase			,     decoder_start_token_id=_UpperCamelCase			,     forced_eos_token_id=_UpperCamelCase			,     **_UpperCamelCase			,     )






class        lowerCAmelCase_  (     UpperCAmelCase	):






       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
       def   UpperCamelCase						( self   )->      Mapping[str, Mapping[int, str]]:
            if self.task in ["default", "seq2seq-lm"]:
                 _A				=							OrderedDict(
                     [
                         ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                         ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                     ]   )

                 if self.use_past:
                      _A				=							{0: 'batch'}
                      _A				=							{0: 'batch', 1: 'past_decoder_sequence + sequence'}
                 else:
                      _A				=							{0: 'batch', 1: 'decoder_sequence'}
                      _A				=							{0: 'batch', 1: 'decoder_sequence'}

                 if self.use_past:
                      self.fill_with_past_key_values_(_UpperCamelCase			,     direction='inputs'   )
            elif self.task == "causal-lm":
                 # TODO: figure this case out.
                 _A				=							OrderedDict(
                     [
                         ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                         ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                     ]   )
                 if self.use_past:
                      _A		,				_A				=							self.num_layers
                      for i in range(_UpperCamelCase   ):
                           _A				=							{0: 'batch', 2: 'past_sequence + sequence'}
                           _A				=							{0: 'batch', 2: 'past_sequence + sequence'}
            else:
                 _A				=							OrderedDict(
                     [
                         ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                         ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                         ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
                         ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
                     ]   )

            return common_inputs






       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
       def   UpperCamelCase						( self   )->      Mapping[str, Mapping[int, str]]:
            if self.task in ["default", "seq2seq-lm"]:
                 _A				=							super().outputs
            else:
                 _A				=							super(_UpperCamelCase			,     self   ).outputs
                 if self.use_past:
                      _A		,				_A				=							self.num_layers
                      for i in range(_UpperCamelCase   ):
                           _A				=							{0: 'batch', 2: 'past_sequence + sequence'}
                           _A				=							{0: 'batch', 2: 'past_sequence + sequence'}
            return common_outputs






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase = -1			,     _UpperCamelCase = -1			,     _UpperCamelCase = False			,     _UpperCamelCase = None			,     )->      Mapping[str, Any]:
            _A				=							self._generate_dummy_inputs_for_encoder_and_decoder(
                _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )

            # Generate decoder inputs
            _A				=							seq_length if not self.use_past else 1
            _A				=							self._generate_dummy_inputs_for_encoder_and_decoder(
                _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )
            _A				=							{F'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
            _A				=							dict(**_UpperCamelCase			,     **_UpperCamelCase   )

            if self.use_past:
                 if not is_torch_available():
                      raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'   )
                 else:
                      import torch
                 _A		,				_A				=							common_inputs['input_ids'].shape
                 _A				=							common_inputs['decoder_input_ids'].shape[1]
                 _A		,				_A				=							self.num_attention_heads
                 _A				=							(
                     batch,
                     num_encoder_attention_heads,
                     encoder_seq_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )
                 _A				=							decoder_seq_length + 3
                 _A				=							(
                     batch,
                     num_decoder_attention_heads,
                     decoder_past_length,
                     self._config.hidden_size // num_decoder_attention_heads,
                 )

                 _A				=							torch.cat(
                     [common_inputs['decoder_attention_mask'], torch.ones(_UpperCamelCase			,     _UpperCamelCase   )]			,     dim=1   )

                 _A				=							[]
                 # If the number of encoder and decoder layers are present in the model configuration, both are considered
                 _A		,				_A				=							self.num_layers
                 _A				=							min(_UpperCamelCase			,     _UpperCamelCase   )
                 _A				=							max(_UpperCamelCase			,     _UpperCamelCase   ) - min_num_layers
                 _A				=							'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

                 for _ in range(_UpperCamelCase   ):
                      common_inputs["past_key_values"].append(
                          (
                              torch.zeros(_UpperCamelCase   ),
                              torch.zeros(_UpperCamelCase   ),
                              torch.zeros(_UpperCamelCase   ),
                              torch.zeros(_UpperCamelCase   ),
                          )   )
                 # TODO: test this.
                 _A				=							encoder_shape if remaining_side_name == 'encoder' else decoder_shape
                 for _ in range(_UpperCamelCase			,     _UpperCamelCase   ):
                      common_inputs["past_key_values"].append((torch.zeros(_UpperCamelCase   ), torch.zeros(_UpperCamelCase   ))   )
            return common_inputs






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase = -1			,     _UpperCamelCase = -1			,     _UpperCamelCase = False			,     _UpperCamelCase = None			,     )->      Mapping[str, Any]:
            _A				=							self._generate_dummy_inputs_for_encoder_and_decoder(
                _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )

            if self.use_past:
                 if not is_torch_available():
                      raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'   )
                 else:
                      import torch
                 _A		,				_A				=							common_inputs['input_ids'].shape
                 # Not using the same length for past_key_values
                 _A				=							seqlen + 2
                 _A		,				_A				=							self.num_layers
                 _A		,				_A				=							self.num_attention_heads
                 _A				=							(
                     batch,
                     num_encoder_attention_heads,
                     past_key_values_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )

                 _A				=							common_inputs['attention_mask'].dtype
                 _A				=							torch.cat(
                     [common_inputs['attention_mask'], torch.ones(_UpperCamelCase			,     _UpperCamelCase			,     dtype=_UpperCamelCase   )]			,     dim=1   )
                 _A				=							[
                     (torch.zeros(_UpperCamelCase   ), torch.zeros(_UpperCamelCase   )) for _ in range(_UpperCamelCase   )
                 ]
            return common_inputs






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase = -1			,     _UpperCamelCase = -1			,     _UpperCamelCase = False			,     _UpperCamelCase = None			,     )->      Mapping[str, Any]:
            # Copied from OnnxConfig.generate_dummy_inputs
            # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
            # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
            _A				=							compute_effective_axis_dimension(
                _UpperCamelCase			,     fixed_dimension=OnnxConfig.default_fixed_batch			,     num_token_to_add=0   )

            # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
            _A				=							tokenizer.num_special_tokens_to_add(_UpperCamelCase   )
            _A				=							compute_effective_axis_dimension(
                _UpperCamelCase			,     fixed_dimension=OnnxConfig.default_fixed_sequence			,     num_token_to_add=_UpperCamelCase   )

            # Generate dummy inputs according to compute batch and sequence
            _A				=							[' '.join([tokenizer.unk_token]   ) * seq_length] * batch_size
            _A				=							dict(tokenizer(_UpperCamelCase			,     return_tensors=_UpperCamelCase   )   )
            return common_inputs






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase = -1			,     _UpperCamelCase = -1			,     _UpperCamelCase = False			,     _UpperCamelCase = None			,     )->      Mapping[str, Any]:
            if self.task in ["default", "seq2seq-lm"]:
                 _A				=							self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                     _UpperCamelCase			,     batch_size=_UpperCamelCase			,     seq_length=_UpperCamelCase			,     is_pair=_UpperCamelCase			,     framework=_UpperCamelCase   )

            else:
                 _A				=							self._generate_dummy_inputs_for_causal_lm(
                     _UpperCamelCase			,     batch_size=_UpperCamelCase			,     seq_length=_UpperCamelCase			,     is_pair=_UpperCamelCase			,     framework=_UpperCamelCase   )

            return common_inputs






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      int:
            if self.task in ["default", "seq2seq-lm"]:
                 _A				=							super()._flatten_past_key_values_(_UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )
            else:
                 _A				=							super(_UpperCamelCase			,     self   )._flatten_past_key_values_(
                     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )






       @property
       def   UpperCamelCase						( self   )->      float:
            return 1e-4









'''simple docstring'''
# 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.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


lowerCAmelCase						=					"""Create a default config file for Accelerate with only a few flags set."""

def 					lowerCamelCase_ ( __UpperCamelCase					:       Union[str, Any]="no"							,    __UpperCamelCase					:       str = default_json_config_file							,    __UpperCamelCase					:       bool = False		)					->	Tuple:







     """simple docstring"""
     _A				=							Path(__UpperCamelCase		)
     path.parent.mkdir(parents=__UpperCamelCase							,    exist_ok=__UpperCamelCase		)
     if path.exists():
          print(
              F'Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'		)
          return False
     _A				=							mixed_precision.lower()
     if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
          raise ValueError(
              F'`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'		)
     _A				=							{
         'compute_environment': 'LOCAL_MACHINE',
         'mixed_precision': mixed_precision,
     }
     if torch.cuda.is_available():
          _A				=							torch.cuda.device_count()
          _A				=							num_gpus
          _A				=							False
          if num_gpus > 1:
               _A				=							'MULTI_GPU'
          else:
               _A				=							'NO'
     elif is_xpu_available() and use_xpu:
          _A				=							torch.xpu.device_count()
          _A				=							num_xpus
          _A				=							False
          if num_xpus > 1:
               _A				=							'MULTI_XPU'
          else:
               _A				=							'NO'
     elif is_npu_available():
          _A				=							torch.npu.device_count()
          _A				=							num_npus
          _A				=							False
          if num_npus > 1:
               _A				=							'MULTI_NPU'
          else:
               _A				=							'NO'
     else:
          _A				=							0
          _A				=							True
          _A				=							1
          _A				=							'NO'
     _A				=							ClusterConfig(**__UpperCamelCase		)
     config.to_json_file(__UpperCamelCase		)
     return path

def 					lowerCamelCase_ ( __UpperCamelCase					:       List[Any]							,    __UpperCamelCase					:       str		)					->	Any:







     """simple docstring"""
     _A				=							parser.add_parser('default'							,    parents=__UpperCamelCase							,    help=__UpperCamelCase							,    formatter_class=__UpperCamelCase		)
     parser.add_argument(
         '--config_file'							,    default=__UpperCamelCase							,    help=(
             'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
             'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
             'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
             'with \'huggingface\'.'
         )							,    dest='save_location'							,    )

     parser.add_argument(
         '--mixed_precision'							,    choices=['no', 'fp16', 'bf16']							,    type=__UpperCamelCase							,    help='Whether or not to use mixed precision training. '
         'Choose between FP16 and BF16 (bfloat16) training. '
         'BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.'							,    default='no'							,    )
     parser.set_defaults(func=__UpperCamelCase		)
     return parser

def 					lowerCamelCase_ ( __UpperCamelCase					:       Any		)					->	Union[str, Any]:







     """simple docstring"""
     _A				=							write_basic_config(args.mixed_precision							,    args.save_location		)
     if config_file:
          print(F'accelerate configuration saved at {config_file}'		)





