






from collections import OrderedDict
from typing import Any, Mapping, Optional

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


snake_case__      :	str       	=      logging.get_logger(__name__)

snake_case__      :	Dict       	=      {
    """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json""",
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}


class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   """blenderbot-small"""
  A_						=   ["""past_key_values"""]
  A_						=   {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}




  def __init__(     self		,		_UpperCAmelCase=50265		,		_UpperCAmelCase=512		,		_UpperCAmelCase=8		,		_UpperCAmelCase=2048		,		_UpperCAmelCase=16		,		_UpperCAmelCase=8		,		_UpperCAmelCase=2048		,		_UpperCAmelCase=16		,		_UpperCAmelCase=0.0		,		_UpperCAmelCase=0.0		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=512		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=0.0		,		_UpperCAmelCase=0.0		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=1		,		_UpperCAmelCase=False		,		_UpperCAmelCase=0		,		_UpperCAmelCase=1		,		_UpperCAmelCase=2		,		_UpperCAmelCase=2		,		**_UpperCAmelCase		,		)			-> Optional[Any]:
     UpperCamelCase_									=     vocab_size
     UpperCamelCase_									=     max_position_embeddings
     UpperCamelCase_									=     d_model
     UpperCamelCase_									=     encoder_ffn_dim
     UpperCamelCase_									=     encoder_layers
     UpperCamelCase_									=     encoder_attention_heads
     UpperCamelCase_									=     decoder_ffn_dim
     UpperCamelCase_									=     decoder_layers
     UpperCamelCase_									=     decoder_attention_heads
     UpperCamelCase_									=     dropout
     UpperCamelCase_									=     attention_dropout
     UpperCamelCase_									=     activation_dropout
     UpperCamelCase_									=     activation_function
     UpperCamelCase_									=     init_std
     UpperCamelCase_									=     encoder_layerdrop
     UpperCamelCase_									=     decoder_layerdrop
     UpperCamelCase_									=     use_cache
     UpperCamelCase_									=     encoder_layers
     UpperCamelCase_									=     scale_embedding  # scale factor will be sqrt(d_model) if True

     super().__init__(
         pad_token_id=_UpperCAmelCase		,		bos_token_id=_UpperCAmelCase		,		eos_token_id=_UpperCAmelCase		,		is_encoder_decoder=_UpperCAmelCase		,		decoder_start_token_id=_UpperCAmelCase		,		forced_eos_token_id=_UpperCAmelCase		,		**_UpperCAmelCase		,		)




class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  @property
  def    _UpperCAmelCase     (     self )			-> Mapping[str, Mapping[int, str]]:
     if self.task in ["default", "seq2seq-lm"]:
        UpperCamelCase_									=     OrderedDict(
            [
                ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
            ] )

        if self.use_past:
           UpperCamelCase_									=     {0: 'batch'}
           UpperCamelCase_									=     {0: 'batch', 1: 'past_decoder_sequence + sequence'}
        else:
           UpperCamelCase_									=     {0: 'batch', 1: 'decoder_sequence'}
           UpperCamelCase_									=     {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.
        UpperCamelCase_									=     OrderedDict(
            [
                ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
            ] )
        if self.use_past:
           UpperCamelCase_		,      UpperCamelCase_									=     self.num_layers
           for i in range(_UpperCAmelCase ):
              UpperCamelCase_									=     {0: 'batch', 2: 'past_sequence + sequence'}
              UpperCamelCase_									=     {0: 'batch', 2: 'past_sequence + sequence'}
     else:
        UpperCamelCase_									=     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
  def    _UpperCAmelCase     (     self )			-> Mapping[str, Mapping[int, str]]:
     if self.task in ["default", "seq2seq-lm"]:
        UpperCamelCase_									=     super().outputs
     else:
        UpperCamelCase_									=     super(_UpperCAmelCase		,		self ).outputs
        if self.use_past:
           UpperCamelCase_		,      UpperCamelCase_									=     self.num_layers
           for i in range(_UpperCAmelCase ):
              UpperCamelCase_									=     {0: 'batch', 2: 'past_sequence + sequence'}
              UpperCamelCase_									=     {0: 'batch', 2: 'past_sequence + sequence'}
     return common_outputs




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = -1		,		_UpperCAmelCase = -1		,		_UpperCAmelCase = False		,		_UpperCAmelCase = None		,		)			-> Mapping[str, Any]:
     UpperCamelCase_									=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         _UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )

     # Generate decoder inputs
     UpperCamelCase_									=     seq_length if not self.use_past else 1
     UpperCamelCase_									=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         _UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )
     UpperCamelCase_									=     {f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
     UpperCamelCase_									=     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
        UpperCamelCase_		,      UpperCamelCase_									=     common_inputs['input_ids'].shape
        UpperCamelCase_									=     common_inputs['decoder_input_ids'].shape[1]
        UpperCamelCase_		,      UpperCamelCase_									=     self.num_attention_heads
        UpperCamelCase_									=     (
            batch,
            num_encoder_attention_heads,
            encoder_seq_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )
        UpperCamelCase_									=     decoder_seq_length + 3
        UpperCamelCase_									=     (
            batch,
            num_decoder_attention_heads,
            decoder_past_length,
            self._config.hidden_size // num_decoder_attention_heads,
        )

        UpperCamelCase_									=     torch.cat(
            [common_inputs['decoder_attention_mask'], torch.ones(_UpperCAmelCase		,		_UpperCAmelCase )]		,		dim=1 )

        UpperCamelCase_									=     []
        # If the number of encoder and decoder layers are present in the model configuration, both are considered
        UpperCamelCase_		,      UpperCamelCase_									=     self.num_layers
        UpperCamelCase_									=     min(_UpperCAmelCase		,		_UpperCAmelCase )
        UpperCamelCase_									=     max(_UpperCAmelCase		,		_UpperCAmelCase ) - min_num_layers
        UpperCamelCase_									=     '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.
        UpperCamelCase_									=     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]:
     UpperCamelCase_									=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         _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
        UpperCamelCase_		,      UpperCamelCase_									=     common_inputs['input_ids'].shape
        # Not using the same length for past_key_values
        UpperCamelCase_									=     seqlen + 2
        UpperCamelCase_		,      UpperCamelCase_									=     self.num_layers
        UpperCamelCase_		,      UpperCamelCase_									=     self.num_attention_heads
        UpperCamelCase_									=     (
            batch,
            num_encoder_attention_heads,
            past_key_values_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )

        UpperCamelCase_									=     common_inputs['attention_mask'].dtype
        UpperCamelCase_									=     torch.cat(
            [common_inputs['attention_mask'], torch.ones(_UpperCAmelCase		,		_UpperCAmelCase		,		dtype=_UpperCAmelCase )]		,		dim=1 )
        UpperCamelCase_									=     [
            (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
     UpperCamelCase_									=     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
     UpperCamelCase_									=     tokenizer.num_special_tokens_to_add(_UpperCAmelCase )
     UpperCamelCase_									=     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
     UpperCamelCase_									=     [' '.join([tokenizer.unk_token] ) * seq_length] * batch_size
     UpperCamelCase_									=     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"]:
        UpperCamelCase_									=     self._generate_dummy_inputs_for_default_and_seqaseq_lm(
            _UpperCAmelCase		,		batch_size=_UpperCAmelCase		,		seq_length=_UpperCAmelCase		,		is_pair=_UpperCAmelCase		,		framework=_UpperCAmelCase )

     elif self.task == "causal-lm":
        UpperCamelCase_									=     self._generate_dummy_inputs_for_causal_lm(
            _UpperCAmelCase		,		batch_size=_UpperCAmelCase		,		seq_length=_UpperCAmelCase		,		is_pair=_UpperCAmelCase		,		framework=_UpperCAmelCase )
     else:
        UpperCamelCase_									=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            _UpperCAmelCase		,		batch_size=_UpperCAmelCase		,		seq_length=_UpperCAmelCase		,		is_pair=_UpperCAmelCase		,		framework=_UpperCAmelCase )

     return common_inputs




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Tuple:
     if self.task in ["default", "seq2seq-lm"]:
        UpperCamelCase_									=     super()._flatten_past_key_values_(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )
     else:
        UpperCamelCase_									=     super(_UpperCAmelCase		,		self )._flatten_past_key_values_(
            _UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )













import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command


class 						_a       (  unittest.TestCase    ):






  """simple docstring"""





  A_						=   inspect.getfile(accelerate.test_utils    )
  A_						=   os.path.sep.join(mod_file.split(os.path.sep    )[:-1] + ["""scripts""", """test_cli.py"""]    )

  A_						=   ["""accelerate""", """launch"""]
  A_						=   Path.home() / """.cache/huggingface/accelerate"""
  A_						=   """default_config.yaml"""
  A_						=   config_folder / config_file
  A_						=   config_folder / """_default_config.yaml"""

  A_						=   Path("""tests/test_configs"""    )




  @classmethod
  def    _UpperCAmelCase     (     cls )			-> Union[str, Any]:
     if cls.config_path.is_file():
        cls.config_path.rename(cls.changed_path )




  @classmethod
  def    _UpperCAmelCase     (     cls )			-> List[str]:
     if cls.changed_path.is_file():
        cls.changed_path.rename(cls.config_path )




  def    _UpperCAmelCase     (     self )			-> Optional[Any]:
     UpperCamelCase_									=     self.base_cmd
     if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
        cmd += ["--multi_gpu"]
     execute_subprocess_async(cmd + [self.test_file_path]		,		env=os.environ.copy() )




  def    _UpperCAmelCase     (     self )			-> Optional[int]:
     for config in sorted(self.test_config_path.glob('**/*.yaml' ) ):
        with self.subTest(config_file=_UpperCAmelCase ):
           execute_subprocess_async(
               self.base_cmd + ['--config_file', str(_UpperCAmelCase ), self.test_file_path]		,		env=os.environ.copy() )




  def    _UpperCAmelCase     (     self )			-> Tuple:
     execute_subprocess_async(['accelerate', 'test']		,		env=os.environ.copy() )




class 						_a       (  unittest.TestCase    ):






  """simple docstring"""





  A_						=   """test-tpu"""
  A_						=   """us-central1-a"""
  A_						=   """ls"""
  A_						=   ["""accelerate""", """tpu-config"""]
  A_						=   """cd /usr/share"""
  A_						=   """tests/test_samples/test_command_file.sh"""
  A_						=   """Running gcloud compute tpus tpu-vm ssh"""




  def    _UpperCAmelCase     (     self )			-> Any:
     UpperCamelCase_									=     run_command(
         self.cmd
         + ['--command', self.command, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug']		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> Any:
     UpperCamelCase_									=     run_command(
         self.cmd
         + [
             '--config_file',
             'tests/test_configs/0_12_0.yaml',
             '--command',
             self.command,
             '--tpu_zone',
             self.tpu_zone,
             '--tpu_name',
             self.tpu_name,
             '--debug',
         ]		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> Any:
     UpperCamelCase_									=     run_command(
         self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--debug']		,		return_stdout=_UpperCAmelCase )
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> Tuple:
     UpperCamelCase_									=     run_command(
         self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--command', self.command, '--debug']		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> Optional[Any]:
     UpperCamelCase_									=     run_command(
         self.cmd
         + [
             '--config_file',
             'tests/test_configs/latest.yaml',
             '--command',
             self.command,
             '--command',
             'echo "Hello World"',
             '--debug',
         ]		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> Dict:
     UpperCamelCase_									=     run_command(
         self.cmd
         + ['--config_file', 'tests/test_configs/latest.yaml', '--command_file', self.command_file, '--debug']		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> Dict:
     UpperCamelCase_									=     run_command(
         self.cmd
         + [
             '--config_file',
             'tests/test_configs/0_12_0.yaml',
             '--command_file',
             self.command_file,
             '--tpu_zone',
             self.tpu_zone,
             '--tpu_name',
             self.tpu_name,
             '--debug',
         ]		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> List[Any]:
     UpperCamelCase_									=     run_command(
         self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--install_accelerate', '--debug']		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all"""		,		_UpperCAmelCase		,		)




  def    _UpperCAmelCase     (     self )			-> Optional[Any]:
     UpperCamelCase_									=     run_command(
         self.cmd
         + [
             '--config_file',
             'tests/test_configs/latest.yaml',
             '--install_accelerate',
             '--accelerate_version',
             '12.0.0',
             '--debug',
         ]		,		return_stdout=_UpperCAmelCase		,		)
     self.assertIn(
         f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all"""		,		_UpperCAmelCase		,		)





