


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


lowerCAmelCase__				    =       ''''''


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


     class 				SCREAMING_SNAKE_CASE__    (		tr.AbstractTransform    ):
            """simple docstring"""


            def __init__(						self		,					snake_case__ = " "						):






              """simple docstring"""





              lowerCAmelCase					:					List[Any]								=							sentence_delimiter

            def 	lowercase__							(						self		,					snake_case__						):






              """simple docstring"""





              return list(snake_case__						)







            def 	lowercase__							(						self		,					snake_case__						):






              """simple docstring"""





              lowerCAmelCase					:					Any								=							[]
              for sent_idx, sentence in enumerate(snake_case__						):
                chars.extend(self.process_string(snake_case__						)						)
                if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(snake_case__						) - 1:
                  chars.append(self.sentence_delimiter						)
              return chars

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


lowerCAmelCase__				    =       '''\
@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.}
}
'''

lowerCAmelCase__				    =       '''\
Character error rate (CER) is a common metric of the performance of an automatic speech recognition system.

CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.

Character error rate can be computed as:

CER = (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 characters,
N is the number of characters in the reference (N=S+D+C).

CER\'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
performance of the ASR system with a CER of 0 being a perfect score.
'''

lowerCAmelCase__				    =       '''
Computes CER score of transcribed segments against references.
Args:
    references: list of references for each speech input.
    predictions: list of transcribtions to score.
    concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.
Returns:
    (float): the character error rate

Examples:

    >>> predictions = ["this is the prediction", "there is an other sample"]
    >>> references = ["this is the reference", "there is another one"]
    >>> cer = datasets.load_metric("cer")
    >>> cer_score = cer.compute(predictions=predictions, references=references)
    >>> print(cer_score)
    0.34146341463414637
'''


@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       ,   _KWARGS_DESCRIPTION    )
class 				SCREAMING_SNAKE_CASE__    (		datasets.Metric    ):
       """simple docstring"""


       def 	lowercase__							(						self						):






         """simple docstring"""





         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 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__=False						):






         """simple docstring"""





         if concatenate_texts:
           return jiwer.compute_measures(
               snake_case__		,					snake_case__		,					truth_transform=snake_case__		,					hypothesis_transform=snake_case__		,					)["wer"]

         lowerCAmelCase					:					List[Any]								=							0
         lowerCAmelCase					:					Optional[Any]								=							0
         for prediction, reference in zip(snake_case__		,					snake_case__						):
           lowerCAmelCase					:					Optional[int]								=							jiwer.compute_measures(
               snake_case__		,					snake_case__		,					truth_transform=snake_case__		,					hypothesis_transform=snake_case__		,					)
           incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
           total += measures["substitutions"] + measures["deletions"] + measures["hits"]

         return incorrect / total









"""simple docstring"""







import inspect
import unittest
from typing import List

import numpy as np

from transformers import EfficientFormerConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFEfficientFormerForImageClassification,
         TFEfficientFormerForImageClassificationWithTeacher,
         TFEfficientFormerModel,
     )
     from transformers.models.efficientformer.modeling_tf_efficientformer import (
         TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
     )


if is_vision_available():
     from PIL import Image

     from transformers import EfficientFormerImageProcessor


class 				SCREAMING_SNAKE_CASE__    :
       """simple docstring"""


       def __init__(						self		,					snake_case__		,					snake_case__ = 13		,					snake_case__ = 64		,					snake_case__ = 2		,					snake_case__ = 3		,					snake_case__ = 3		,					snake_case__ = True		,					snake_case__ = True		,					snake_case__ = 128		,					snake_case__=[16, 32, 64, 128]		,					snake_case__ = 7		,					snake_case__ = 4		,					snake_case__ = 37		,					snake_case__ = "gelu"		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 10		,					snake_case__ = 0.02		,					snake_case__ = 2		,					snake_case__ = 1		,					snake_case__ = 128		,					snake_case__ = [2, 2, 2, 2]		,					snake_case__ = 2		,					snake_case__ = 2		,					):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							parent
         lowerCAmelCase					:					str								=							batch_size
         lowerCAmelCase					:					int								=							image_size
         lowerCAmelCase					:					int								=							patch_size
         lowerCAmelCase					:					Union[str, Any]								=							num_channels
         lowerCAmelCase					:					int								=							is_training
         lowerCAmelCase					:					Tuple								=							use_labels
         lowerCAmelCase					:					List[Any]								=							hidden_size
         lowerCAmelCase					:					Dict								=							num_hidden_layers
         lowerCAmelCase					:					Tuple								=							num_attention_heads
         lowerCAmelCase					:					List[Any]								=							intermediate_size
         lowerCAmelCase					:					int								=							hidden_act
         lowerCAmelCase					:					str								=							hidden_dropout_prob
         lowerCAmelCase					:					Tuple								=							attention_probs_dropout_prob
         lowerCAmelCase					:					Optional[int]								=							type_sequence_label_size
         lowerCAmelCase					:					List[str]								=							initializer_range
         lowerCAmelCase					:					List[str]								=							encoder_stride
         lowerCAmelCase					:					Union[str, Any]								=							num_attention_outputs
         lowerCAmelCase					:					Any								=							embed_dim
         lowerCAmelCase					:					Tuple								=							embed_dim + 1
         lowerCAmelCase					:					str								=							resolution
         lowerCAmelCase					:					Optional[Any]								=							depths
         lowerCAmelCase					:					Any								=							hidden_sizes
         lowerCAmelCase					:					List[str]								=							dim
         lowerCAmelCase					:					str								=							mlp_expansion_ratio

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

         lowerCAmelCase					:					Tuple								=							None
         if self.use_labels:
           lowerCAmelCase					:					Any								=							ids_tensor([self.batch_size]		,					self.type_sequence_label_size						)

         lowerCAmelCase					:					Tuple								=							self.get_config()

         return config, pixel_values, labels

       def 	lowercase__							(						self						):






         """simple docstring"""





         return EfficientFormerConfig(
             image_size=self.image_size		,					patch_size=self.patch_size		,					num_channels=self.num_channels		,					hidden_size=self.hidden_size		,					num_hidden_layers=self.num_hidden_layers		,					num_attention_heads=self.num_attention_heads		,					intermediate_size=self.intermediate_size		,					hidden_act=self.hidden_act		,					hidden_dropout_prob=self.hidden_dropout_prob		,					attention_probs_dropout_prob=self.attention_probs_dropout_prob		,					is_decoder=snake_case__		,					initializer_range=self.initializer_range		,					encoder_stride=self.encoder_stride		,					resolution=self.resolution		,					depths=self.depths		,					hidden_sizes=self.hidden_sizes		,					dim=self.dim		,					mlp_expansion_ratio=self.mlp_expansion_ratio		,					)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							TFEfficientFormerModel(config=snake_case__						)
         lowerCAmelCase					:					Optional[int]								=							model(snake_case__		,					training=snake_case__						)
         self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.hidden_size)						)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							self.type_sequence_label_size
         lowerCAmelCase					:					Dict								=							TFEfficientFormerForImageClassification(snake_case__						)
         lowerCAmelCase					:					Tuple								=							model(snake_case__		,					labels=snake_case__		,					training=snake_case__						)
         self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.type_sequence_label_size)						)

         # test greyscale images
         lowerCAmelCase					:					str								=							1
         lowerCAmelCase					:					Any								=							TFEfficientFormerForImageClassification(snake_case__						)

         lowerCAmelCase					:					List[str]								=							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
         lowerCAmelCase					:					str								=							model(snake_case__		,					labels=snake_case__						)
         self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.type_sequence_label_size)						)







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							self.prepare_config_and_inputs()
         lowerCAmelCase						,  lowerCAmelCase						,  lowerCAmelCase					:					int								=							config_and_inputs
         lowerCAmelCase					:					List[str]								=							{"pixel_values": pixel_values}
         return config, inputs_dict


@require_tf
class 				SCREAMING_SNAKE_CASE__    (		lowercase       ,   lowercase       ,   unittest.TestCase    ):
       """simple docstring"""


       a  :    Optional[int]       =(
           (
               TFEfficientFormerModel,
               TFEfficientFormerForImageClassificationWithTeacher,
               TFEfficientFormerForImageClassification,
           )
           if is_tf_available()
           else ()
       )
       a  :    Union[str, Any]       =(
           {
               "feature-extraction": TFEfficientFormerModel,
               "image-classification": (
                   TFEfficientFormerForImageClassification,
                   TFEfficientFormerForImageClassificationWithTeacher,
               ),
           }
           if is_tf_available()
           else {}
       )

       a  :    int       =False

       a  :    Optional[Any]       =False
       a  :    List[Any]       =False
       a  :    str       =False
       a  :    List[Any]       =False

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							TFEfficientFormerModelTester(self						)
         lowerCAmelCase					:					Dict								=							ConfigTester(
             self		,					config_class=snake_case__		,					has_text_modality=snake_case__		,					hidden_size=37						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         self.config_tester.run_common_tests()

       @unittest.skip(reason="EfficientFormer does not use inputs_embeds"						)
       def 	lowercase__							(						self						):






         """simple docstring"""





         pass

       @unittest.skip(reason="EfficientFormer does not support input and output embeddings"						)
       def 	lowercase__							(						self						):






         """simple docstring"""





         pass

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase						,  lowerCAmelCase					:					Optional[int]								=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowerCAmelCase					:					Tuple								=							model_class(snake_case__						)
           lowerCAmelCase					:					str								=							inspect.signature(model.call						)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowerCAmelCase					:					Optional[int]								=							[*signature.parameters.keys()]

           lowerCAmelCase					:					str								=							["pixel_values"]
           self.assertListEqual(arg_names[:1]		,					snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         def check_hidden_states_output(snake_case__		,					snake_case__		,					snake_case__						):
           lowerCAmelCase					:					List[Any]								=							model_class(snake_case__						)

           lowerCAmelCase					:					List[str]								=							model(**self._prepare_for_class(snake_case__		,					snake_case__						)		,					training=snake_case__						)
           lowerCAmelCase					:					Dict								=							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           lowerCAmelCase					:					List[Any]								=							getattr(
               self.model_tester		,					"expected_num_hidden_layers"		,					self.model_tester.num_hidden_layers + 1						)
           self.assertEqual(len(snake_case__						)		,					snake_case__						)

           if hasattr(self.model_tester		,					"encoder_seq_length"						):
             lowerCAmelCase					:					Union[str, Any]								=							self.model_tester.encoder_seq_length
             if hasattr(self.model_tester		,					"chunk_length"						) and self.model_tester.chunk_length > 1:
               lowerCAmelCase					:					Tuple								=							seq_length * self.model_tester.chunk_length
           else:
             lowerCAmelCase					:					List[str]								=							self.model_tester.seq_length

           self.assertListEqual(
               list(hidden_states[-1].shape[-2:]						)		,					[seq_length, self.model_tester.hidden_size]		,					)

           if config.is_encoder_decoder:
             lowerCAmelCase					:					Tuple								=							outputs.decoder_hidden_states

             self.asseretIsInstance(snake_case__		,					(list, tuple)						)
             self.assertEqual(len(snake_case__						)		,					snake_case__						)
             lowerCAmelCase					:					int								=							getattr(self.model_tester		,					"seq_length"		,					snake_case__						)
             lowerCAmelCase					:					Tuple								=							getattr(self.model_tester		,					"decoder_seq_length"		,					snake_case__						)

             self.assertListEqual(
                 list(hidden_states[-1].shape[-2:]						)		,					[decoder_seq_length, self.model_tester.hidden_size]		,					)

         lowerCAmelCase						,  lowerCAmelCase					:					List[str]								=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowerCAmelCase					:					str								=							True
           check_hidden_states_output(snake_case__		,					snake_case__		,					snake_case__						)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           lowerCAmelCase					:					Dict								=							True

           check_hidden_states_output(snake_case__		,					snake_case__		,					snake_case__						)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__=False						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							super()._prepare_for_class(snake_case__		,					snake_case__		,					return_labels=snake_case__						)

         if return_labels:
           if model_class.__name__ == "TFEfficientFormerForImageClassificationWithTeacher":
             del inputs_dict["labels"]

         return inputs_dict

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*snake_case__						)

       @unittest.skip(reason="EfficientFormer does not implement masked image modeling yet"						)
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*snake_case__						)

       @slow
       def 	lowercase__							(						self						):






         """simple docstring"""





         for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowerCAmelCase					:					Optional[Any]								=							TFEfficientFormerModel.from_pretrained(snake_case__						)
           self.assertIsNotNone(snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase						,  lowerCAmelCase					:					List[Any]								=							self.model_tester.prepare_config_and_inputs_for_common()
         lowerCAmelCase					:					int								=							True

         lowerCAmelCase					:					Union[str, Any]								=							getattr(self.model_tester		,					"seq_length"		,					snake_case__						)
         lowerCAmelCase					:					Dict								=							getattr(self.model_tester		,					"encoder_seq_length"		,					snake_case__						)
         lowerCAmelCase					:					Union[str, Any]								=							getattr(self.model_tester		,					"key_length"		,					snake_case__						)
         lowerCAmelCase					:					List[str]								=							getattr(self.model_tester		,					"chunk_length"		,					snake_case__						)

         if chunk_length is not None and hasattr(self.model_tester		,					"num_hashes"						):
           lowerCAmelCase					:					Dict								=							encoder_seq_length * self.model_tester.num_hashes

         for model_class in self.all_model_classes:
           lowerCAmelCase					:					int								=							True
           lowerCAmelCase					:					int								=							False
           lowerCAmelCase					:					Dict								=							True
           lowerCAmelCase					:					List[Any]								=							model_class(snake_case__						)

           lowerCAmelCase					:					Tuple								=							model(**self._prepare_for_class(snake_case__		,					snake_case__						)		,					training=snake_case__						)
           lowerCAmelCase					:					str								=							outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
           self.assertEqual(len(snake_case__						)		,					self.model_tester.num_attention_outputs						)

           # check that output_attentions also work using config
           del inputs_dict["output_attentions"]
           lowerCAmelCase					:					int								=							True
           lowerCAmelCase					:					Dict								=							model_class(snake_case__						)
           lowerCAmelCase					:					int								=							model(**self._prepare_for_class(snake_case__		,					snake_case__						)		,					training=snake_case__						)

           lowerCAmelCase					:					int								=							outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
           self.assertEqual(len(snake_case__						)		,					self.model_tester.num_attention_outputs						)

           if chunk_length is not None:
             self.assertListEqual(
                 list(attentions[0].shape[-4:]						)		,					[self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length]		,					)
           else:
             self.assertListEqual(
                 list(attentions[0].shape[-3:]						)		,					[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]		,					)







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase						,  lowerCAmelCase					:					Tuple								=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           # Prepare our model
           lowerCAmelCase					:					List[str]								=							model_class(snake_case__						)
           # These are maximally general inputs for the model, with multiple None dimensions
           # Hopefully this will catch any conditionals that fail for flexible shapes
           lowerCAmelCase					:					Optional[Any]								=							{
               key: tf.keras.Input(shape=val.shape[1:]		,					dtype=val.dtype		,					name=snake_case__						)
               for key, val in model.input_signature.items()
               if key in model.dummy_inputs
           }
           lowerCAmelCase					:					Optional[Any]								=							model(snake_case__						)
           self.assertTrue(outputs_dict is not None						)

def  a__      (    ):


  '''simple docstring'''







  lowerCAmelCase					:					Optional[Any]								=							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
  return image





@require_tf
@require_vision
class 				SCREAMING_SNAKE_CASE__    (		unittest.TestCase    ):
       """simple docstring"""


       @cached_property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return (
             EfficientFormerImageProcessor.from_pretrained("snap-research/efficientformer-l1-300"						)
             if is_vision_available()
             else None
         )

       @slow
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							TFEfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"						)
         lowerCAmelCase					:					Tuple								=							self.default_image_processor
         lowerCAmelCase					:					Tuple								=							prepare_img()
         lowerCAmelCase					:					List[str]								=							image_processor(images=snake_case__		,					return_tensors="tf"						)
         # forward pass
         lowerCAmelCase					:					Optional[int]								=							model(**snake_case__		,					training=snake_case__						)
         # verify the logits
         lowerCAmelCase					:					Union[str, Any]								=							tf.TensorShape((1, 1_000)						)
         self.assertEqual(outputs.logits.shape		,					snake_case__						)
         lowerCAmelCase					:					Tuple								=							tf.constant([-0.0555, 0.4825, -0.0852]						)
         self.assertTrue(np.allclose(outputs.logits[0, :3]		,					snake_case__		,					atol=1e-4						)						)







       @slow
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[Any]								=							TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
             "snap-research/efficientformer-l1-300"						)
         lowerCAmelCase					:					Optional[int]								=							self.default_image_processor
         lowerCAmelCase					:					Optional[Any]								=							prepare_img()
         lowerCAmelCase					:					Tuple								=							image_processor(images=snake_case__		,					return_tensors="tf"						)
         # forward pass
         lowerCAmelCase					:					Dict								=							model(**snake_case__		,					training=snake_case__						)
         # verify the logits
         lowerCAmelCase					:					Optional[int]								=							tf.TensorShape((1, 1_000)						)
         self.assertEqual(outputs.logits.shape		,					snake_case__						)
         lowerCAmelCase					:					Any								=							tf.constant([-0.1312, 0.4353, -1.0499]						)
         self.assertTrue(np.allclose(outputs.logits[0, :3]		,					snake_case__		,					atol=1e-4						)						)





