

'''simple docstring'''
import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
         SpeechaTextaDecoder,
         SpeechaTextaForCausalLM,
     )







@require_torch
class 							A__					:





  def __init__(	self   :			Union[str, Any]							,      _a   :			Optional[Any]							,      _a   :			List[str]=99							,      _a   :			Any=13							,      _a   :			Optional[Any]=16							,      _a   :			List[str]=7							,      _a   :			str=True							,      _a   :			Union[str, Any]=True							,      _a   :			str=True							,      _a   :			Any=False							,      _a   :			Union[str, Any]=True							,      _a   :			Optional[Any]=2							,      _a   :			str=32							,      _a   :			int=4							,      _a   :			Optional[int]=4							,      _a   :			Optional[Any]=30							,      _a   :			List[Any]=0							,      _a   :			List[str]=1							,      _a   :			List[Any]=2							,      _a   :			int=None							,      )			->					int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=parent
       _SCREAMING_SNAKE_CASE												=batch_size
       _SCREAMING_SNAKE_CASE												=decoder_seq_length
       # For common tests
       _SCREAMING_SNAKE_CASE												=self.decoder_seq_length
       _SCREAMING_SNAKE_CASE												=is_training
       _SCREAMING_SNAKE_CASE												=use_attention_mask
       _SCREAMING_SNAKE_CASE												=use_labels

       _SCREAMING_SNAKE_CASE												=vocab_size
       _SCREAMING_SNAKE_CASE												=d_model
       _SCREAMING_SNAKE_CASE												=d_model
       _SCREAMING_SNAKE_CASE												=decoder_layers
       _SCREAMING_SNAKE_CASE												=decoder_layers
       _SCREAMING_SNAKE_CASE												=decoder_ffn_dim
       _SCREAMING_SNAKE_CASE												=decoder_attention_heads
       _SCREAMING_SNAKE_CASE												=decoder_attention_heads
       _SCREAMING_SNAKE_CASE												=eos_token_id
       _SCREAMING_SNAKE_CASE												=bos_token_id
       _SCREAMING_SNAKE_CASE												=pad_token_id
       _SCREAMING_SNAKE_CASE												=decoder_start_token_id
       _SCREAMING_SNAKE_CASE												=use_cache
       _SCREAMING_SNAKE_CASE												=max_position_embeddings

       _SCREAMING_SNAKE_CASE												=None
       _SCREAMING_SNAKE_CASE												=decoder_seq_length
       _SCREAMING_SNAKE_CASE												=2
       _SCREAMING_SNAKE_CASE												=1





  def 		A						(	self   :			Optional[Any]				)			->					Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=ids_tensor([self.batch_size, self.decoder_seq_length]							,      self.vocab_size				)

       _SCREAMING_SNAKE_CASE												=None
       if self.use_attention_mask:
            _SCREAMING_SNAKE_CASE												=ids_tensor([self.batch_size, self.decoder_seq_length]							,      vocab_size=2				)

       _SCREAMING_SNAKE_CASE												=None
       if self.use_labels:
            _SCREAMING_SNAKE_CASE												=ids_tensor([self.batch_size, self.decoder_seq_length]							,      self.vocab_size				)

       _SCREAMING_SNAKE_CASE												=SpeechaTextaConfig(
           vocab_size=self.vocab_size							,      d_model=self.d_model							,      decoder_layers=self.decoder_layers							,      decoder_ffn_dim=self.decoder_ffn_dim							,      decoder_attention_heads=self.decoder_attention_heads							,      eos_token_id=self.eos_token_id							,      bos_token_id=self.bos_token_id							,      use_cache=self.use_cache							,      pad_token_id=self.pad_token_id							,      decoder_start_token_id=self.decoder_start_token_id							,      max_position_embeddings=self.max_position_embeddings							,      )

       return (
           config,
           input_ids,
           attention_mask,
           lm_labels,
       )





  def 		A						(	self   :			Any							,      _a   :			Tuple							,      _a   :			str							,      _a   :			Optional[int]							,      _a   :			Tuple							,      )			->					Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=True
       _SCREAMING_SNAKE_CASE												=SpeechaTextaDecoder(config=_a				).to(_a				).eval()
       _SCREAMING_SNAKE_CASE												=input_ids[:2]

       input_ids[input_ids == 0] += 1
       # first forward pass
       _SCREAMING_SNAKE_CASE												=model(_a							,      use_cache=_a				)
       _SCREAMING_SNAKE_CASE												=model(_a				)
       _SCREAMING_SNAKE_CASE												=model(_a							,      use_cache=_a				)

       self.parent.assertTrue(len(_a				) == len(_a				)				)
       self.parent.assertTrue(len(_a				) == len(_a				) + 1				)

       _SCREAMING_SNAKE_CASE												=outputs['past_key_values']

       # create hypothetical next token and extent to next_input_ids
       _SCREAMING_SNAKE_CASE												=ids_tensor((2, 1)							,      config.vocab_size - 1				) + 1

       # append to next input_ids and
       _SCREAMING_SNAKE_CASE												=torch.cat([input_ids, next_tokens]							,      dim=-1				)

       print(_a				)

       _SCREAMING_SNAKE_CASE												=model(_a				)['last_hidden_state']
       _SCREAMING_SNAKE_CASE												=model(_a							,      past_key_values=_a				)['last_hidden_state']

       # select random slice
       _SCREAMING_SNAKE_CASE												=ids_tensor((1,)							,      output_from_past.shape[-1]				).item()
       _SCREAMING_SNAKE_CASE												=output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
       _SCREAMING_SNAKE_CASE												=output_from_past[:, 0, random_slice_idx].detach()

       # test that outputs are equal for slice
       assert torch.allclose(_a							,      _a							,      atol=1e-3				)





  def 		A						(	self   :			Optional[int]				)			->					Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.prepare_config_and_inputs()
       (
           (
           _SCREAMING_SNAKE_CASE
       )					,		(
           _SCREAMING_SNAKE_CASE
       )					,		(
           _SCREAMING_SNAKE_CASE
       )					,		(
           _SCREAMING_SNAKE_CASE
       )					,		
       )												=config_and_inputs

       _SCREAMING_SNAKE_CASE												={
           'input_ids': input_ids,
           'attention_mask': attention_mask,
       }
       return config, inputs_dict







@require_torch
class 							A__					( A__  ,     A__  ,     A__  ,     unittest.TestCase						):
  A__					=       (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
  A__					=       (SpeechaTextaForCausalLM,) if is_torch_available() else ()
  A__					=       {'text-generation': SpeechaTextaForCausalLM} if is_torch_available() else {}
  A__					=       True
  A__					=       False





  def 		A						(	self   :			Optional[int]							,      )			->					Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=SpeechaTextaStandaloneDecoderModelTester(self							,      is_training=_a				)
       _SCREAMING_SNAKE_CASE												=ConfigTester(self							,      config_class=_a				)





  def 		A						(	self   :			Dict				)			->					Optional[int]:





       '''simple docstring'''






       pass





  def 		A						(	self   :			Dict				)			->					Optional[Any]:





       '''simple docstring'''






       pass





  def 		A						(	self   :			Dict				)			->					List[str]:





       '''simple docstring'''






       pass





  def 		A						(	self   :			int				)			->					int:





       '''simple docstring'''






       self.config_tester.run_common_tests()





  def 		A						(	self   :			Any				)			->					Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past(*_a				)





  def 		A						(	self   :			Any				)			->					List[Any]:





       '''simple docstring'''






       return



'''simple docstring'''
import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


lowerCamelCase				:   int      				=  logging.getLogger(__name__)







@dataclass
class 							A__					:
  A__					=       field(
      metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}						)
  A__					=       field(
      default=A__  ,     metadata={'help': 'Pretrained config name or path if not the same as model_name'}						)
  A__					=       field(
      default=A__  ,     metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}						)
  A__					=       field(default=A__  ,     metadata={'help': 'Set this flag to use fast tokenization.'}						)
  # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
  # or just modify its tokenizer_config.json.
  A__					=       field(
      default=A__  ,     metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}  ,     )


def  _lowerCAmelCase      (		)   ->    Union[str, Any]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE												=HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)       )

     if len(sys.argv       ) == 2 and sys.argv[1].endswith('.json'       ):
          # If we pass only one argument to the script and it's the path to a json file,
          # let's parse it to get our arguments.
          _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE												=parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]       )       )
     else:
          _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE												=parser.parse_args_into_dataclasses()

     if (
         os.path.exists(training_args.output_dir       )
         and os.listdir(training_args.output_dir       )
         and training_args.do_train
         and not training_args.overwrite_output_dir
     ):
          raise ValueError(
              f"Output directory ({training_args.output_dir}) already exists and is not empty. Use"
              ' --overwrite_output_dir to overcome.'       )

     # Setup logging
     logging.basicConfig(
         format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'							,				datefmt='%m/%d/%Y %H:%M:%S'							,				level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN							,				)
     logger.warning(
         'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s'							,				training_args.local_rank							,				training_args.device							,				training_args.n_gpu							,				bool(training_args.local_rank != -1       )							,				training_args.fpaa							,				)
     # Set the verbosity to info of the Transformers logger (on main process only):
     if is_main_process(training_args.local_rank       ):
          transformers.utils.logging.set_verbosity_info()
          transformers.utils.logging.enable_default_handler()
          transformers.utils.logging.enable_explicit_format()
     logger.info('Training/evaluation parameters %s'							,				_UpperCamelCase       )

     # Prepare Question-Answering task
     # Load pretrained model and tokenizer
     #
     # Distributed training:
     # The .from_pretrained methods guarantee that only one local process can concurrently
     # download model & vocab.

     _SCREAMING_SNAKE_CASE												=AutoConfig.from_pretrained(
         model_args.config_name if model_args.config_name else model_args.model_name_or_path							,				cache_dir=model_args.cache_dir							,				)
     _SCREAMING_SNAKE_CASE												=AutoTokenizer.from_pretrained(
         model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path							,				cache_dir=model_args.cache_dir							,				use_fast=_UpperCamelCase							,				)
     _SCREAMING_SNAKE_CASE												=AutoModelForQuestionAnswering.from_pretrained(
         model_args.model_name_or_path							,				from_tf=bool('.ckpt' in model_args.model_name_or_path       )							,				config=_UpperCamelCase							,				cache_dir=model_args.cache_dir							,				)

     # Get datasets
     _SCREAMING_SNAKE_CASE												=hasattr(model.config							,				'lang2id'       )
     _SCREAMING_SNAKE_CASE												=(
         SquadDataset(
             _UpperCamelCase							,				tokenizer=_UpperCamelCase							,				is_language_sensitive=_UpperCamelCase							,				cache_dir=model_args.cache_dir       )
         if training_args.do_train
         else None
     )
     _SCREAMING_SNAKE_CASE												=(
         SquadDataset(
             _UpperCamelCase							,				tokenizer=_UpperCamelCase							,				mode='dev'							,				is_language_sensitive=_UpperCamelCase							,				cache_dir=model_args.cache_dir							,				)
         if training_args.do_eval
         else None
     )

     # Data collator
     _SCREAMING_SNAKE_CASE												=DataCollatorWithPadding(_UpperCamelCase							,				pad_to_multiple_of=8       ) if training_args.fpaa else None

     # Initialize our Trainer
     _SCREAMING_SNAKE_CASE												=Trainer(
         model=_UpperCamelCase							,				args=_UpperCamelCase							,				train_dataset=_UpperCamelCase							,				eval_dataset=_UpperCamelCase							,				data_collator=_UpperCamelCase							,				)

     # Training
     if training_args.do_train:
          trainer.train(
              model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path       ) else None       )
          trainer.save_model()
          # For convenience, we also re-save the tokenizer to the same directory,
          # so that you can share your model easily on huggingface.co/models =)
          if trainer.is_world_master():
               tokenizer.save_pretrained(training_args.output_dir       )


def  _lowerCAmelCase      (		_UpperCamelCase		:  Tuple       )   ->    List[str]:
     """simple docstring"""




     main()


if __name__ == "__main__":
     main()
