





"""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 					_UpperCAmelCase    :



      '''simple docstring'''

      def __init__(	self   , snake_case_   , snake_case_=9_9   , snake_case_=1_3   , snake_case_=1_6   , snake_case_=7   , snake_case_=True   , snake_case_=True   , snake_case_=True   , snake_case_=False   , snake_case_=True   , snake_case_=2   , snake_case_=3_2   , snake_case_=4   , snake_case_=4   , snake_case_=3_0   , snake_case_=0   , snake_case_=1   , snake_case_=2   , snake_case_=None   , ):

        """simple docstring"""

        A_    :			Optional[Any]									=    parent
        A_    :			List[Any]									=    batch_size
        A_    :			Tuple									=    decoder_seq_length
        # For common tests
        A_    :			List[str]									=    self.decoder_seq_length
        A_    :			List[str]									=    is_training
        A_    :			Optional[Any]									=    use_attention_mask
        A_    :			List[Any]									=    use_labels

        A_    :			int									=    vocab_size
        A_    :			int									=    d_model
        A_    :			Dict									=    d_model
        A_    :			Tuple									=    decoder_layers
        A_    :			Union[str, Any]									=    decoder_layers
        A_    :			Any									=    decoder_ffn_dim
        A_    :			List[Any]									=    decoder_attention_heads
        A_    :			Any									=    decoder_attention_heads
        A_    :			Optional[int]									=    eos_token_id
        A_    :			List[str]									=    bos_token_id
        A_    :			Optional[int]									=    pad_token_id
        A_    :			Tuple									=    decoder_start_token_id
        A_    :			Dict									=    use_cache
        A_    :			Any									=    max_position_embeddings

        A_    :			List[Any]									=    None
        A_    :			List[Any]									=    decoder_seq_length
        A_    :			Optional[int]									=    2
        A_    :			int									=    1

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			List[Any]									=    ids_tensor([self.batch_size, self.decoder_seq_length]   , self.vocab_size    )

        A_    :			Optional[int]									=    None
        if self.use_attention_mask:
          A_    :			List[Any]									=    ids_tensor([self.batch_size, self.decoder_seq_length]   , vocab_size=2    )

        A_    :			Union[str, Any]									=    None
        if self.use_labels:
          A_    :			Dict									=    ids_tensor([self.batch_size, self.decoder_seq_length]   , self.vocab_size    )

        A_    :			Optional[Any]									=    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    lowerCamelCase_     (	self   , snake_case_   , snake_case_   , snake_case_   , snake_case_   , ):

        """simple docstring"""

        A_    :			Dict									=    True
        A_    :			int									=    SpeechaTextaDecoder(config=snake_case_    ).to(snake_case_    ).eval()
        A_    :			Any									=    input_ids[:2]

        input_ids[input_ids == 0] += 1
        # first forward pass
        A_    :			Optional[Any]									=    model(snake_case_   , use_cache=snake_case_    )
        A_    :			Any									=    model(snake_case_    )
        A_    :			List[str]									=    model(snake_case_   , use_cache=snake_case_    )

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

        A_    :			str									=    outputs['past_key_values']

        # create hypothetical next token and extent to next_input_ids
        A_    :			List[Any]									=    ids_tensor((2, 1)   , config.vocab_size - 1    ) + 1

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

        print(snake_case_    )

        A_    :			List[str]									=    model(snake_case_    )['last_hidden_state']
        A_    :			List[str]									=    model(snake_case_   , past_key_values=snake_case_    )['last_hidden_state']

        # select random slice
        A_    :			Optional[int]									=    ids_tensor((1,)   , output_from_past.shape[-1]    ).item()
        A_    :			Union[str, Any]									=    output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
        A_    :			List[str]									=    output_from_past[:, 0, random_slice_idx].detach()

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

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Any									=    self.prepare_config_and_inputs()
        (
            (
            A_
        )		,			(
            A_
        )		,			(
            A_
        )		,			(
            A_
        )		,			
        )    :			Dict									=    config_and_inputs

        A_    :			Dict									=    {
            'input_ids': input_ids,
            'attention_mask': attention_mask,
        }
        return config, inputs_dict


@require_torch
class 					_UpperCAmelCase    (  UpperCAmelCase__     ,  UpperCAmelCase__     ,  UpperCAmelCase__     ,  unittest.TestCase						):



      '''simple docstring'''
      lowercase_ :					Union[str, Any]             =  (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
      lowercase_ :					List[Any]             =  (SpeechaTextaForCausalLM,) if is_torch_available() else ()
      lowercase_ :					Union[str, Any]             =  {"""text-generation""": SpeechaTextaForCausalLM} if is_torch_available() else {}
      lowercase_ :					int             =  True
      lowercase_ :					List[str]             =  False

      def    lowerCamelCase_     (	self   , ):

        """simple docstring"""

        A_    :			List[str]									=    SpeechaTextaStandaloneDecoderModelTester(self   , is_training=snake_case_    )
        A_    :			Dict									=    ConfigTester(self   , config_class=snake_case_    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        pass

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        pass

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        pass

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        self.config_tester.run_common_tests()

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			int									=    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_decoder_model_past(*snake_case_    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return



"""simple docstring"""







import argparse
import json
import re
import string
import sys
from collections import Counter
def _A		(		lowercase						):







    """simple docstring"""

    def remove_articles(lowercase						):
        return re.sub(R'''\b(a|an|the)\b'''			,       ''' '''			,       lowercase						)

    def white_space_fix(lowercase						):
        return " ".join(text.split()						)

    def remove_punc(lowercase						):
        a											=set(string.punctuation						)
        return "".join(ch for ch in text if ch not in exclude						)

    def lower(lowercase						):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(lowercase						)						)						)						)
def _A		(		lowercase			,       lowercase						):







    """simple docstring"""
    a											=normalize_answer(lowercase						).split()
    a											=normalize_answer(lowercase						).split()
    a											=Counter(lowercase						) & Counter(lowercase						)
    a											=sum(common.values()						)
    if num_same == 0:
        return 0
    a											=1.0 * num_same / len(lowercase						)
    a											=1.0 * num_same / len(lowercase						)
    a											=(2 * precision * recall) / (precision + recall)
    return fa
def _A		(		lowercase			,       lowercase						):







    """simple docstring"""
    return normalize_answer(lowercase						) == normalize_answer(lowercase						)
def _A		(		lowercase			,       lowercase			,       lowercase						):







    """simple docstring"""
    a											=[]
    for ground_truth in ground_truths:
        a											=metric_fn(lowercase			,       lowercase						)
        scores_for_ground_truths.append(lowercase						)
    return max(lowercase						)
def _A		(		lowercase			,       lowercase						):







    """simple docstring"""
    a											=a											=a											=0
    for article in dataset:
        for paragraph in article["paragraphs"]:
            for qa in paragraph["qas"]:
                total += 1
                if qa["id"] not in predictions:
                    a											='''Unanswered question ''' + qa['''id'''] + ''' will receive score 0.'''
                    print(lowercase			,       file=sys.stderr						)
                    continue
                a											=[x['''text'''] for x in qa['''answers''']]
                a											=predictions[qa['''id''']]
                exact_match += metric_max_over_ground_truths(lowercase			,       lowercase			,       lowercase						)
                fa += metric_max_over_ground_truths(lowercase			,       lowercase			,       lowercase						)

    a											=100.0 * exact_match / total
    a											=100.0 * fa / total

    return {"exact_match": exact_match, "f1": fa}


if __name__ == "__main__":
       lowerCamelCase_  :  List[Any]     		=    """1.1"""
       lowerCamelCase_  :  Optional[Any]     		=    argparse.ArgumentParser(description="""Evaluation for SQuAD """ + expected_version)
       parser.add_argument("""dataset_file""", help="""Dataset file""")
       parser.add_argument("""prediction_file""", help="""Prediction File""")
       lowerCamelCase_  :  Optional[Any]     		=    parser.parse_args()
       with open(args.dataset_file) as dataset_file:
              lowerCamelCase_  :  List[str]     		=    json.load(dataset_file)
              if dataset_json["version"] != expected_version:
                     print(
                         """Evaluation expects v-""" + expected_version + """, but got dataset with v-""" + dataset_json["""version"""],
                         file=sys.stderr,
                     )
              lowerCamelCase_  :  Dict     		=    dataset_json["""data"""]
       with open(args.prediction_file) as prediction_file:
              lowerCamelCase_  :  Optional[Any]     		=    json.load(prediction_file)
       print(json.dumps(evaluate(dataset, predictions)))