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

from ...configuration_utils import PretrainedConfig
from ...feature_extraction_utils import FeatureExtractionMixin
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import TensorType, logging


SCREAMING_SNAKE_CASE__		:       Dict       						=       logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__		:       List[Any]       						=       {
    "deepmind/language-perceiver": "https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json",
    # See all Perceiver models at https://huggingface.co/models?filter=perceiver
}

class    snake_case       (   UpperCamelCase_    ):
  lowercase_								=  'perceiver'




  def __init__(   self  :						List[Any]  ,			a_  :						List[Any]=256  ,			a_  :						List[Any]=1280  ,			a_  :						str=768  ,			a_  :						List[Any]=1  ,			a_  :						Tuple=26  ,			a_  :						Optional[Any]=8  ,			a_  :						str=8  ,			a_  :						int=None  ,			a_  :						Dict=None  ,			a_  :						Dict="kv"  ,			a_  :						List[Any]=1  ,			a_  :						Dict=1  ,			a_  :						Dict="gelu"  ,			a_  :						Dict=0.1  ,			a_  :						Optional[Any]=0.02  ,			a_  :						Dict=1e-1_2  ,			a_  :						Union[str, Any]=True  ,			a_  :						Optional[int]=262  ,			a_  :						str=2048  ,			a_  :						List[str]=56  ,			a_  :						Dict=[368, 496]  ,			a_  :						int=16  ,			a_  :						str=1920  ,			a_  :						List[str]=16  ,			a_  :						List[str]=[1, 16, 224, 224]  ,			**a_  :						Union[str, Any]  ,			)->							Tuple:




       """simple docstring"""
       super().__init__(**a_					)

       SCREAMING_SNAKE_CASE__					:					Optional[int]      =		num_latents
       SCREAMING_SNAKE_CASE__					:					Dict      =		d_latents
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		d_model
       SCREAMING_SNAKE_CASE__					:					str      =		num_blocks
       SCREAMING_SNAKE_CASE__					:					Tuple      =		num_self_attends_per_block
       SCREAMING_SNAKE_CASE__					:					List[str]      =		num_self_attention_heads
       SCREAMING_SNAKE_CASE__					:					Tuple      =		num_cross_attention_heads
       SCREAMING_SNAKE_CASE__					:					Any      =		qk_channels
       SCREAMING_SNAKE_CASE__					:					Tuple      =		v_channels
       SCREAMING_SNAKE_CASE__					:					Any      =		cross_attention_shape_for_attention
       SCREAMING_SNAKE_CASE__					:					List[str]      =		self_attention_widening_factor
       SCREAMING_SNAKE_CASE__					:					Dict      =		cross_attention_widening_factor
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		hidden_act
       SCREAMING_SNAKE_CASE__					:					List[str]      =		attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		initializer_range
       SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		layer_norm_eps
       SCREAMING_SNAKE_CASE__					:					Tuple      =		use_query_residual
       # masked language modeling attributes
       SCREAMING_SNAKE_CASE__					:					Optional[Any]      =		vocab_size
       SCREAMING_SNAKE_CASE__					:					str      =		max_position_embeddings
       # image classification attributes
       SCREAMING_SNAKE_CASE__					:					List[Any]      =		image_size
       # flow attributes
       SCREAMING_SNAKE_CASE__					:					Dict      =		train_size
       # multimodal autoencoding attributes
       SCREAMING_SNAKE_CASE__					:					Tuple      =		num_frames
       SCREAMING_SNAKE_CASE__					:					int      =		audio_samples_per_frame
       SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		samples_per_patch
       SCREAMING_SNAKE_CASE__					:					Optional[int]      =		output_shape




class    snake_case       (   UpperCamelCase_    ):




  @property
  def 						__lowercase(   self  :						str					)->							Mapping[str, Mapping[int, str]]:




       """simple docstring"""
       if self.task == "multiple-choice":
            SCREAMING_SNAKE_CASE__					:					Optional[int]      =		{0: 'batch', 1: 'choice', 2: 'sequence'}
       else:
            SCREAMING_SNAKE_CASE__					:					List[str]      =		{0: 'batch', 1: 'sequence'}
       return OrderedDict(
           [
               ('inputs', dynamic_axis),
               ('attention_mask', dynamic_axis),
           ]					)




  @property
  def 						__lowercase(   self  :						Tuple					)->							float:




       """simple docstring"""
       return 1e-4




  def 						__lowercase(   self  :						List[str]  ,			a_  :						Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]  ,			a_  :						int = -1  ,			a_  :						int = -1  ,			a_  :						int = -1  ,			a_  :						bool = False  ,			a_  :						Optional[TensorType] = None  ,			a_  :						int = 3  ,			a_  :						int = 40  ,			a_  :						int = 40  ,			)->							Mapping[str, Any]:




       """simple docstring"""
       # copied from `transformers.onnx.config.OnnxConfig` and slightly altered/simplified

       if isinstance(a_  ,			a_					):
            # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
            SCREAMING_SNAKE_CASE__					:					List[Any]      =		compute_effective_axis_dimension(
                a_  ,			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
            SCREAMING_SNAKE_CASE__					:					Any      =		preprocessor.num_special_tokens_to_add(a_					)
            SCREAMING_SNAKE_CASE__					:					str      =		compute_effective_axis_dimension(
                a_  ,			fixed_dimension=OnnxConfig.default_fixed_sequence  ,			num_token_to_add=a_					)
            # Generate dummy inputs according to compute batch and sequence
            SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		[' '.join(['a']					) * seq_length] * batch_size
            SCREAMING_SNAKE_CASE__					:					Tuple      =		dict(preprocessor(a_  ,			return_tensors=a_					)					)
            SCREAMING_SNAKE_CASE__					:					Dict      =		inputs.pop('input_ids'					)
            return inputs
       elif isinstance(a_  ,			a_					) and preprocessor.model_input_names[0] == "pixel_values":
            # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
            SCREAMING_SNAKE_CASE__					:					Dict      =		compute_effective_axis_dimension(a_  ,			fixed_dimension=OnnxConfig.default_fixed_batch					)
            SCREAMING_SNAKE_CASE__					:					Union[str, Any]      =		self._generate_dummy_images(a_  ,			a_  ,			a_  ,			a_					)
            SCREAMING_SNAKE_CASE__					:					Tuple      =		dict(preprocessor(images=a_  ,			return_tensors=a_					)					)
            SCREAMING_SNAKE_CASE__					:					str      =		inputs.pop('pixel_values'					)
            return inputs
       else:
            raise ValueError(
                'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.'					)





"""simple docstring"""


from jiwer import compute_measures

import datasets


A_    							=					"""\
@inproceedings{inproceedings,
    author = {Morris, Andrew and Maier, Viktoria and Green, Phil},
    year = {2004},
    month = {01},
    pages = {},
    title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}
}
"""

A_    							=					"""\
Word error rate (WER) is a common metric of the performance of an automatic speech recognition system.

The general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort.

This problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate.

Word error rate can then be computed as:

WER = (S + D + I) / N = (S + D + I) / (S + D + C)

where

S is the number of substitutions,
D is the number of deletions,
I is the number of insertions,
C is the number of correct words,
N is the number of words in the reference (N=S+D+C).

This value indicates the average number of errors per reference word. The lower the value, the better the
performance of the ASR system with a WER of 0 being a perfect score.
"""

A_    							=					"""
Compute WER score of transcribed segments against references.

Args:
    references: List of references for each speech input.
    predictions: List of transcriptions to score.
    concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively.

Returns:
    (float): the word error rate

Examples:

    >>> predictions = [\"this is the prediction\", \"there is an other sample\"]
    >>> references = [\"this is the reference\", \"there is another one\"]
    >>> wer = datasets.load_metric(\"wer\")
    >>> wer_score = wer.compute(predictions=predictions, references=references)
    >>> print(wer_score)
    0.5
"""




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION						,	_KWARGS_DESCRIPTION			)
class  __lowerCamelCase  ( datasets.Metric			):




				def        UpperCAmelCase__						(				self   ):
							return datasets.MetricInfo(
							    description=_DESCRIPTION							,						citation=_CITATION							,						inputs_description=_KWARGS_DESCRIPTION							,						features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''string'''							,						id='''sequence'''   ),
							            '''references''': datasets.Value('''string'''							,						id='''sequence'''   ),
							        }   )							,						codebase_urls=['''https://github.com/jitsi/jiwer/''']							,						reference_urls=[
							        '''https://en.wikipedia.org/wiki/Word_error_rate''',
							    ]							,						)





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase=None							,						UpperCAmelCase=None							,						UpperCAmelCase=False   ):
							if concatenate_texts:
										return compute_measures(UpperCAmelCase							,						UpperCAmelCase   )["wer"]
							else:
										lowerCamelCase_					=  0
										lowerCamelCase_					=  0
										for prediction, reference in zip(UpperCAmelCase							,						UpperCAmelCase   ):
													lowerCamelCase_					=  compute_measures(UpperCAmelCase							,						UpperCAmelCase   )
													incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
													total += measures["substitutions"] + measures["deletions"] + measures["hits"]
										return incorrect / total



