




import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
  import os

  import jax.numpy as jnp
  from jax import jit

  from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
  from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

  UpperCAmelCase_ 		=      "0.12"  # assumed parallelism: 8


if is_torch_available():
  import torch


def 						SCREAMING_SNAKE_CASE       (      a_	:							Union[str, Any]				,					a_	:							Optional[int]				,					a_	:							List[str]=None			):
  if rng is None:
    __a			=					random.Random()

  __a			=					1
  for dim in shape:
    total_dims *= dim

  __a			=					[]
  for _ in range(lowercase__			):
    values.append(rng.randint(0				,					vocab_size - 1			)			)

  __a			=					np.array(lowercase__				,					dtype=jnp.intaa			).reshape(lowercase__			)

  return output


def 						SCREAMING_SNAKE_CASE       (      a_	:							List[Any]				,					a_	:							Union[str, Any]=None			):
  __a			=					ids_tensor(lowercase__				,					vocab_size=2				,					rng=lowercase__			)
  # make sure that at least one token is attended to for each batch
  __a			=					1
  return attn_mask





@require_flax
class    __lowercase						:
   _a							=      None
   _a							=      ()







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()

     # cut to half length & take max batch_size 3
     __a			=					2
     __a			=					inputs['input_ids'].shape[-1] // 2
     __a			=					inputs['input_ids'][:max_batch_size, :sequence_length]

     __a			=					jnp.ones_like(__UpperCamelCase      )
     __a			=					attention_mask[:max_batch_size, :sequence_length]

     # generate max 5 tokens
     __a			=					input_ids.shape[-1] + 5
     if config.eos_token_id is not None and config.pad_token_id is None:
       # hack to allow generate for models such as GPT2 as is done in `generate()`
       __a			=					config.eos_token_id
     return config, input_ids, attention_mask, max_length







   @is_pt_flax_cross_test
   def 		UpperCamelCase__			(	self      )				->    Any:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					False
     __a			=					max_length
     __a			=					0

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model_class.__name__[4:]  # Skip the "Flax" at the beginning
       __a			=					getattr(__UpperCamelCase      ,							__UpperCamelCase      )
       __a			=					pt_model_class(__UpperCamelCase      ).eval()
       __a			=					load_flax_weights_in_pytorch_model(__UpperCamelCase      ,							flax_model.params      )

       __a			=					flax_model.generate(__UpperCamelCase      ).sequences
       __a			=					pt_model.generate(torch.tensor(__UpperCamelCase      ,							dtype=torch.long      )      )

       if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
         __a			=					flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

       self.assertListEqual(pt_generation_outputs.numpy().tolist()      ,							flax_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    Any:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					False
     __a			=					max_length

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					True
     __a			=					max_length

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					False
     __a			=					max_length
     __a			=					2

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    Any:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					False
     __a			=					max_length
     __a			=					2
     __a			=					2

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[0]      ,							input_ids.shape[0] * config.num_return_sequences      )







   def 		UpperCamelCase__			(	self      )				->    str:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					True
     __a			=					max_length
     __a			=					0.8
     __a			=					10
     __a			=					0.3
     __a			=					1
     __a			=					8
     __a			=					9

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    str:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					max_length
     __a			=					1
     __a			=					8
     __a			=					9

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()
     __a			=					max_length
     __a			=					2
     __a			=					1
     __a			=					8
     __a			=					9

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    str:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()

     # pad attention mask on the left
     __a			=					attention_mask.at[(0, 0)].set(0      )

     __a			=					False
     __a			=					max_length

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ,							attention_mask=__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ,							attention_mask=__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    int:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()

     # pad attention mask on the left
     __a			=					attention_mask.at[(0, 0)].set(0      )

     __a			=					True
     __a			=					max_length

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ,							attention_mask=__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ,							attention_mask=__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )







   def 		UpperCamelCase__			(	self      )				->    int:
     __a	,       __a	,       __a	,       __a			=					self._get_input_ids_and_config()

     # pad attention mask on the left
     __a			=					attention_mask.at[(0, 0)].set(0      )

     __a			=					2
     __a			=					max_length

     for model_class in self.all_generative_model_classes:
       __a			=					model_class(__UpperCamelCase      )

       __a			=					model.generate(__UpperCamelCase      ,							attention_mask=__UpperCamelCase      ).sequences
       self.assertEqual(generation_outputs.shape[-1]      ,							__UpperCamelCase      )

       __a			=					jit(model.generate      )
       __a			=					jit_generate(__UpperCamelCase      ,							attention_mask=__UpperCamelCase      ).sequences

       self.assertListEqual(generation_outputs.tolist()      ,							jit_generation_outputs.tolist()      )





@require_flax
class    __lowercase						(  unittest.TestCase ):







   def 		UpperCamelCase__			(	self      )				->    str:
     __a			=					AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert'      )
     __a			=					FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only'      )

     __a			=					'Hello world'
     __a			=					tokenizer(__UpperCamelCase      ,							return_tensors='np'      ).input_ids

     # typos are quickly detected (the correct argument is `do_sample`)
     with self.assertRaisesRegex(__UpperCamelCase      ,							'do_samples'      ):
       model.generate(__UpperCamelCase      ,							do_samples=__UpperCamelCase      )

     # arbitrary arguments that will not be used anywhere are also not accepted
     with self.assertRaisesRegex(__UpperCamelCase      ,							'foo'      ):
       __a			=					{'foo': 'bar'}
       model.generate(__UpperCamelCase      ,							**__UpperCamelCase      )









'''simple docstring'''


from typing import List

import jiwer
import jiwer.transforms as tr
from packaging import version

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse("3.8"):
  import importlib_metadata
else:
  import importlib.metadata as importlib_metadata


UpperCAmelCase_ 		=      ""


if version.parse(importlib_metadata.version("jiwer")) < version.parse("2.3.0"):





  class    __lowercase						(  tr.AbstractTransform ):







     def __init__(	self      ,							UpperCamelCase = " "      )				->    Tuple:
       __a			=					sentence_delimiter







     def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    Union[str, Any]:
       return list(UpperCamelCase      )







     def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    int:
       __a			=					[]
       for sent_idx, sentence in enumerate(UpperCamelCase      ):
         chars.extend(self.process_string(UpperCamelCase      )      )
         if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(UpperCamelCase      ) - 1:
           chars.append(self.sentence_delimiter      )
       return chars

  UpperCAmelCase_ 		=      tr.Compose(
      [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
  )
else:
  UpperCAmelCase_ 		=      tr.Compose(
      [
          tr.RemoveMultipleSpaces(),
          tr.Strip(),
          tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
          tr.ReduceToListOfListOfChars(),
      ]
  )


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

UpperCAmelCase_ 		=      "\\nCharacter error rate (CER) is a common metric of the performance of an automatic speech recognition system.\n\nCER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.\n\nCharacter error rate can be computed as:\n\nCER = (S + D + I) / N = (S + D + I) / (S + D + C)\n\nwhere\n\nS is the number of substitutions,\nD is the number of deletions,\nI is the number of insertions,\nC is the number of correct characters,\nN is the number of characters in the reference (N=S+D+C).\n\nCER's output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the\nperformance of the ASR system with a CER of 0 being a perfect score.\n"

UpperCAmelCase_ 		=      "\nComputes CER score of transcribed segments against references.\nArgs:\n    references: list of references for each speech input.\n    predictions: list of transcribtions to score.\n    concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.\nReturns:\n    (float): the character error rate\n\nExamples:\n\n    >>> predictions = [\"this is the prediction\", \"there is an other sample\"]\n    >>> references = [\"this is the reference\", \"there is another one\"]\n    >>> cer = datasets.load_metric(\"cer\")\n    >>> cer_score = cer.compute(predictions=predictions, references=references)\n    >>> print(cer_score)\n    0.34146341463414637\n"





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







   def 		UpperCamelCase__			(	self      )				->    Any:
     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',
             'https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates',
         ]      ,							)







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase=False      )				->    int:
     if concatenate_texts:
       return jiwer.compute_measures(
           UpperCamelCase      ,							UpperCamelCase      ,							truth_transform=UpperCamelCase      ,							hypothesis_transform=UpperCamelCase      ,							)["wer"]

     __a			=					0
     __a			=					0
     for prediction, reference in zip(UpperCamelCase      ,							UpperCamelCase      ):
       __a			=					jiwer.compute_measures(
           UpperCamelCase      ,							UpperCamelCase      ,							truth_transform=UpperCamelCase      ,							hypothesis_transform=UpperCamelCase      ,							)
       incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
       total += measures["substitutions"] + measures["deletions"] + measures["hits"]

     return incorrect / total



