


"""simple docstring"""
import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY





@is_pipeline_test
class        __A       (unittest.TestCase):





       '''simple docstring'''

       __lowercase:						Dict							 =      MODEL_FOR_MASKED_LM_MAPPING
       __lowercase:						int							 =      TF_MODEL_FOR_MASKED_LM_MAPPING



       def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->Tuple:



              """simple docstring"""



              super().tearDown()
              # clean-up as much as possible GPU memory occupied by PyTorch
              gc.collect()
              if is_torch_available():
                     import torch

                     torch.cuda.empty_cache()



       @require_tf
       def 						lowerCAmelCase    (			self							: List[str]    )   ->Optional[Any]:



              """simple docstring"""



              snake_case_          =						pipeline(task="""fill-mask"""						,    model="""sshleifer/tiny-distilroberta-base"""						,    top_k=2						,    framework="""tf"""    )
              snake_case_          =						unmasker("""My name is <mask>"""    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_						,    decimals=6    )						,    [
                      {"""sequence""": """My name is grouped""", """score""": 2.1E-05, """token""": 38_015, """token_str""": """ grouped"""},
                      {"""sequence""": """My name is accuser""", """score""": 2.1E-05, """token""": 25_506, """token_str""": """ accuser"""},
                  ]						,    )

              snake_case_          =						unmasker("""The largest city in France is <mask>"""    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_						,    decimals=6    )						,    [
                      {
                          """sequence""": """The largest city in France is grouped""",
                          """score""": 2.1E-05,
                          """token""": 38_015,
                          """token_str""": """ grouped""",
                      },
                      {
                          """sequence""": """The largest city in France is accuser""",
                          """score""": 2.1E-05,
                          """token""": 25_506,
                          """token_str""": """ accuser""",
                      },
                  ]						,    )

              snake_case_          =						unmasker("""My name is <mask>"""						,    targets=[""" Patrick""", """ Clara""", """ Teven"""]						,    top_k=3    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_						,    decimals=6    )						,    [
                      {"""sequence""": """My name is Clara""", """score""": 2E-05, """token""": 13_606, """token_str""": """ Clara"""},
                      {"""sequence""": """My name is Patrick""", """score""": 2E-05, """token""": 3_499, """token_str""": """ Patrick"""},
                      {"""sequence""": """My name is Te""", """score""": 1.9E-05, """token""": 2_941, """token_str""": """ Te"""},
                  ]						,    )



       @require_torch
       def 						lowerCAmelCase    (			self							: Optional[int]    )   ->List[str]:



              """simple docstring"""



              snake_case_          =						pipeline(task="""fill-mask"""						,    model="""sshleifer/tiny-distilroberta-base"""						,    top_k=2						,    framework="""pt"""    )

              snake_case_          =						unmasker("""My name is <mask>"""    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_						,    decimals=6    )						,    [
                      {"""sequence""": """My name is Maul""", """score""": 2.2E-05, """token""": 35_676, """token_str""": """ Maul"""},
                      {"""sequence""": """My name isELS""", """score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS"""},
                  ]						,    )

              snake_case_          =						unmasker("""The largest city in France is <mask>"""    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_						,    decimals=6    )						,    [
                      {
                          """sequence""": """The largest city in France is Maul""",
                          """score""": 2.2E-05,
                          """token""": 35_676,
                          """token_str""": """ Maul""",
                      },
                      {"""sequence""": """The largest city in France isELS""", """score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS"""},
                  ]						,    )

              snake_case_          =						unmasker("""My name is <mask>"""						,    targets=[""" Patrick""", """ Clara""", """ Teven"""]						,    top_k=3    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_						,    decimals=6    )						,    [
                      {"""sequence""": """My name is Patrick""", """score""": 2.1E-05, """token""": 3_499, """token_str""": """ Patrick"""},
                      {"""sequence""": """My name is Te""", """score""": 2E-05, """token""": 2_941, """token_str""": """ Te"""},
                      {"""sequence""": """My name is Clara""", """score""": 2E-05, """token""": 13_606, """token_str""": """ Clara"""},
                  ]						,    )

              snake_case_          =						unmasker("""My name is <mask> <mask>"""						,    top_k=2    )

              self.assertEqual(
                  nested_simplify(UpperCAmelCase_						,    decimals=6    )						,    [
                      [
                          {
                              """score""": 2.2E-05,
                              """token""": 35_676,
                              """token_str""": """ Maul""",
                              """sequence""": """<s>My name is Maul<mask></s>""",
                          },
                          {"""score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS""", """sequence""": """<s>My name isELS<mask></s>"""},
                      ],
                      [
                          {
                              """score""": 2.2E-05,
                              """token""": 35_676,
                              """token_str""": """ Maul""",
                              """sequence""": """<s>My name is<mask> Maul</s>""",
                          },
                          {"""score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS""", """sequence""": """<s>My name is<mask>ELS</s>"""},
                      ],
                  ]						,    )



       @require_torch_gpu
       def 						lowerCAmelCase    (			self							: List[Any]    )   ->Optional[int]:



              """simple docstring"""



              snake_case_          =						pipeline("""fill-mask"""						,    model="""hf-internal-testing/tiny-random-distilbert"""						,    device=0						,    framework="""pt"""    )

              # convert model to fp16
              pipe.model.half()

              snake_case_          =						pipe("""Paris is the [MASK] of France."""    )
              # We actually don't care about the result, we just want to make sure
              # it works, meaning the float16 tensor got casted back to float32
              # for postprocessing.
              self.assertIsInstance(UpperCAmelCase_						,    UpperCAmelCase_    )



       @slow
       @require_torch
       def 						lowerCAmelCase    (			self							: Optional[int]    )   ->Tuple:



              """simple docstring"""



              snake_case_          =						pipeline(task="""fill-mask"""						,    model="""distilroberta-base"""						,    top_k=2						,    framework="""pt"""    )
              self.run_large_test(UpperCAmelCase_    )



       @slow
       @require_tf
       def 						lowerCAmelCase    (			self							: Dict    )   ->Union[str, Any]:



              """simple docstring"""



              snake_case_          =						pipeline(task="""fill-mask"""						,    model="""distilroberta-base"""						,    top_k=2						,    framework="""tf"""    )
              self.run_large_test(UpperCAmelCase_    )



       def 						lowerCAmelCase    (			self							: Dict						,    UpperCAmelCase_							: Tuple    )   ->Optional[int]:



              """simple docstring"""



              snake_case_          =						unmasker("""My name is <mask>"""    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_    )						,    [
                      {"""sequence""": """My name is John""", """score""": 0.008, """token""": 610, """token_str""": """ John"""},
                      {"""sequence""": """My name is Chris""", """score""": 0.007, """token""": 1_573, """token_str""": """ Chris"""},
                  ]						,    )
              snake_case_          =						unmasker("""The largest city in France is <mask>"""    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_    )						,    [
                      {
                          """sequence""": """The largest city in France is Paris""",
                          """score""": 0.251,
                          """token""": 2_201,
                          """token_str""": """ Paris""",
                      },
                      {
                          """sequence""": """The largest city in France is Lyon""",
                          """score""": 0.214,
                          """token""": 12_790,
                          """token_str""": """ Lyon""",
                      },
                  ]						,    )

              snake_case_          =						unmasker("""My name is <mask>"""						,    targets=[""" Patrick""", """ Clara""", """ Teven"""]						,    top_k=3    )
              self.assertEqual(
                  nested_simplify(UpperCAmelCase_    )						,    [
                      {"""sequence""": """My name is Patrick""", """score""": 0.005, """token""": 3_499, """token_str""": """ Patrick"""},
                      {"""sequence""": """My name is Clara""", """score""": 0.000, """token""": 13_606, """token_str""": """ Clara"""},
                      {"""sequence""": """My name is Te""", """score""": 0.000, """token""": 2_941, """token_str""": """ Te"""},
                  ]						,    )



       @require_torch
       def 						lowerCAmelCase    (			self							: Dict    )   ->List[Any]:



              """simple docstring"""



              snake_case_          =						pipeline(task="""fill-mask"""						,    model="""sshleifer/tiny-distilroberta-base"""						,    framework="""pt"""    )
              snake_case_          =						None
              snake_case_          =						None
              self.run_pipeline_test(UpperCAmelCase_						,    []    )



       @require_tf
       def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->List[str]:



              """simple docstring"""



              snake_case_          =						pipeline(task="""fill-mask"""						,    model="""sshleifer/tiny-distilroberta-base"""						,    framework="""tf"""    )
              snake_case_          =						None
              snake_case_          =						None
              self.run_pipeline_test(UpperCAmelCase_						,    []    )



       def 						lowerCAmelCase    (			self							: Optional[int]						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: Tuple    )   ->str:



              """simple docstring"""



              if tokenizer is None or tokenizer.mask_token_id is None:
                     self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)"""    )

              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_    )
              snake_case_          =						[
                  F"""This is another {tokenizer.mask_token} test""",
              ]
              return fill_masker, examples



       def 						lowerCAmelCase    (			self							: Optional[int]						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: Dict    )   ->str:



              """simple docstring"""



              snake_case_          =						fill_masker.tokenizer
              snake_case_          =						fill_masker.model

              snake_case_          =						fill_masker(
                  F"""This is a {tokenizer.mask_token}"""						,    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                  ]						,    )

              snake_case_          =						fill_masker([F"""This is a {tokenizer.mask_token}"""]    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                  ]						,    )

              snake_case_          =						fill_masker([F"""This is a {tokenizer.mask_token}""", F"""Another {tokenizer.mask_token} great test."""]    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      [
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      ],
                      [
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      ],
                  ]						,    )

              with self.assertRaises(UpperCAmelCase_    ):
                     fill_masker([None]    )
              # No mask_token is not supported
              with self.assertRaises(UpperCAmelCase_    ):
                     fill_masker("""This is"""    )

              self.run_test_top_k(UpperCAmelCase_						,    UpperCAmelCase_    )
              self.run_test_targets(UpperCAmelCase_						,    UpperCAmelCase_    )
              self.run_test_top_k_targets(UpperCAmelCase_						,    UpperCAmelCase_    )
              self.fill_mask_with_duplicate_targets_and_top_k(UpperCAmelCase_						,    UpperCAmelCase_    )
              self.fill_mask_with_multiple_masks(UpperCAmelCase_						,    UpperCAmelCase_    )



       def 						lowerCAmelCase    (			self							: List[str]						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: Union[str, Any]    )   ->Tuple:



              """simple docstring"""



              snake_case_          =						tokenizer.get_vocab()
              snake_case_          =						sorted(vocab.keys()    )[:2]
              # Pipeline argument
              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_						,    targets=UpperCAmelCase_    )
              snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                  ]						,    )
              snake_case_          =						{vocab[el] for el in targets}
              self.assertEqual({el["""token"""] for el in outputs}						,    UpperCAmelCase_    )
              snake_case_          =						[tokenizer.decode([x]    ) for x in target_ids]
              self.assertEqual({el["""token_str"""] for el in outputs}						,    set(UpperCAmelCase_    )    )

              # Call argument
              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_    )
              snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    targets=UpperCAmelCase_    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                  ]						,    )
              snake_case_          =						{vocab[el] for el in targets}
              self.assertEqual({el["""token"""] for el in outputs}						,    UpperCAmelCase_    )
              snake_case_          =						[tokenizer.decode([x]    ) for x in target_ids]
              self.assertEqual({el["""token_str"""] for el in outputs}						,    set(UpperCAmelCase_    )    )

              # Score equivalence
              snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    targets=UpperCAmelCase_    )
              snake_case_          =						[top_mask["""token_str"""] for top_mask in outputs]
              snake_case_          =						[top_mask["""score"""] for top_mask in outputs]

              # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
              if set(UpperCAmelCase_    ) == set(UpperCAmelCase_    ):
                     snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    targets=UpperCAmelCase_    )
                     snake_case_          =						[top_mask["""score"""] for top_mask in unmasked_targets]
                     self.assertEqual(nested_simplify(UpperCAmelCase_    )						,    nested_simplify(UpperCAmelCase_    )    )

              # Raises with invalid
              with self.assertRaises(UpperCAmelCase_    ):
                     snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    targets=[]    )
              # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
              if "" not in tokenizer.get_vocab():
                     with self.assertRaises(UpperCAmelCase_    ):
                            snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    targets=[""""""]    )
                     with self.assertRaises(UpperCAmelCase_    ):
                            snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    targets=""""""    )



       def 						lowerCAmelCase    (			self							: Optional[int]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: Tuple    )   ->Union[str, Any]:



              """simple docstring"""



              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_						,    top_k=2    )
              snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                  ]						,    )

              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_    )
              snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    top_k=2    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                  ]						,    )
              self.assertEqual(nested_simplify(UpperCAmelCase_    )						,    nested_simplify(UpperCAmelCase_    )    )



       def 						lowerCAmelCase    (			self							: List[Any]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: List[Any]    )   ->int:



              """simple docstring"""



              snake_case_          =						tokenizer.get_vocab()
              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_    )

              # top_k=2, ntargets=3
              snake_case_          =						sorted(vocab.keys()    )[:3]
              snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    top_k=2						,    targets=UpperCAmelCase_    )

              # If we use the most probably targets, and filter differently, we should still
              # have the same results
              snake_case_          =						[el["""token_str"""] for el in sorted(UpperCAmelCase_						,    key=lambda UpperCAmelCase_    : x["score"]						,    reverse=UpperCAmelCase_    )]
              # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
              if set(UpperCAmelCase_    ).issubset(UpperCAmelCase_    ):
                     snake_case_          =						fill_masker(F"""This is a {tokenizer.mask_token}"""						,    top_k=3						,    targets=UpperCAmelCase_    )
                     # They should yield exactly the same result
                     self.assertEqual(nested_simplify(UpperCAmelCase_    )						,    nested_simplify(UpperCAmelCase_    )    )



       def 						lowerCAmelCase    (			self							: Union[str, Any]						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: Any    )   ->Dict:



              """simple docstring"""



              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_    )
              snake_case_          =						tokenizer.get_vocab()
              # String duplicates + id duplicates
              snake_case_          =						sorted(vocab.keys()    )[:3]
              snake_case_          =						[targets[0], targets[1], targets[0], targets[2], targets[1]]
              snake_case_          =						fill_masker(F"""My name is {tokenizer.mask_token}"""						,    targets=UpperCAmelCase_						,    top_k=10    )

              # The target list contains duplicates, so we can't output more
              # than them
              self.assertEqual(len(UpperCAmelCase_    )						,    3    )



       def 						lowerCAmelCase    (			self							: Optional[int]						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: Union[str, Any]    )   ->List[str]:



              """simple docstring"""



              snake_case_          =						FillMaskPipeline(model=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_    )

              snake_case_          =						fill_masker(
                  F"""This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"""						,    top_k=2    )
              self.assertEqual(
                  UpperCAmelCase_						,    [
                      [
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      ],
                      [
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      ],
                      [
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                          {"""sequence""": ANY(UpperCAmelCase_    ), """score""": ANY(UpperCAmelCase_    ), """token""": ANY(UpperCAmelCase_    ), """token_str""": ANY(UpperCAmelCase_    )},
                      ],
                  ]						,    )







"""simple docstring"""
import faiss  # noqa: F401 # Here to have a nice missing dependency error message early on
import numpy  # noqa: F401 # Here to have a nice missing dependency error message early on
import requests  # noqa: F401 # Here to have a nice missing dependency error message early on
import sklearn  # noqa: F401 # Here to have a nice missing dependency error message early on
import tqdm  # noqa: F401 # Here to have a nice missing dependency error message early on
from mauve import compute_mauve  # From: mauve-text

import datasets


__SCREAMING_SNAKE_CASE							:      Dict						=      '\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n  title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n  author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n  booktitle = {NeurIPS},\n  year      = {2021}\n}\n\n'

__SCREAMING_SNAKE_CASE							:      Optional[int]						=      '\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n'

__SCREAMING_SNAKE_CASE							:      Tuple						=      '\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n    predictions: list of generated text to score. Each predictions\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\nOptional Args:\n    num_buckets: the size of the histogram to quantize P and Q. Options: \'auto\' (default) or an integer\n    pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n    kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n    kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n    kmeans_max_iter: maximum number of k-means iterations. Default 500\n    featurize_model_name: name of the model from which features are obtained. Default \'gpt2-large\' Use one of [\'gpt2\', \'gpt2-medium\', \'gpt2-large\', \'gpt2-xl\'].\n    device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU\n    max_text_length: maximum number of tokens to consider. Default 1024\n    divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n    mauve_scaling_factor: "c" from the paper. Default 5.\n    verbose: If True (default), print running time updates\n    seed: random seed to initialize k-means cluster assignments.\nReturns:\n    mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n    frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n    divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n    p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n    q_hist: same as above, but with q_text.\nExamples:\n\n    >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n    >>> import datasets\n    >>> mauve = datasets.load_metric(\'mauve\')\n    >>> predictions = ["hello there", "general kenobi"]\n    >>> references = ["hello there", "general kenobi"]\n    >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n    >>> print(out.mauve) # doctest: +SKIP\n    1.0\n'





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





							'''simple docstring'''

							def 						lowerCAmelCase    (			self							: Tuple    )   ->Union[str, Any]:



														"""simple docstring"""



														return datasets.MetricInfo(
														    description=_DESCRIPTION						,    citation=_CITATION						,    homepage="""https://github.com/krishnap25/mauve"""						,    inputs_description=_KWARGS_DESCRIPTION						,    features=datasets.Features(
														        {
														            """predictions""": datasets.Value("""string"""						,    id="""sequence"""    ),
														            """references""": datasets.Value("""string"""						,    id="""sequence"""    ),
														        }    )						,    codebase_urls=["""https://github.com/krishnap25/mauve"""]						,    reference_urls=[
														        """https://arxiv.org/abs/2102.01454""",
														        """https://github.com/krishnap25/mauve""",
														    ]						,    )



							def 						lowerCAmelCase    (			self							: Optional[int]						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: Optional[Any]=None						,    UpperCAmelCase_							: Optional[Any]=None						,    UpperCAmelCase_							: List[Any]=None						,    UpperCAmelCase_							: Any=None						,    UpperCAmelCase_							: int="auto"						,    UpperCAmelCase_							: Dict=-1						,    UpperCAmelCase_							: Optional[Any]=0.9						,    UpperCAmelCase_							: Dict=5						,    UpperCAmelCase_							: Optional[int]=500						,    UpperCAmelCase_							: Any="gpt2-large"						,    UpperCAmelCase_							: Union[str, Any]=-1						,    UpperCAmelCase_							: Optional[Any]=1_024						,    UpperCAmelCase_							: Dict=25						,    UpperCAmelCase_							: Optional[Any]=5						,    UpperCAmelCase_							: int=True						,    UpperCAmelCase_							: Union[str, Any]=25						,    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						compute_mauve(
														    p_text=UpperCAmelCase_						,    q_text=UpperCAmelCase_						,    p_features=UpperCAmelCase_						,    q_features=UpperCAmelCase_						,    p_tokens=UpperCAmelCase_						,    q_tokens=UpperCAmelCase_						,    num_buckets=UpperCAmelCase_						,    pca_max_data=UpperCAmelCase_						,    kmeans_explained_var=UpperCAmelCase_						,    kmeans_num_redo=UpperCAmelCase_						,    kmeans_max_iter=UpperCAmelCase_						,    featurize_model_name=UpperCAmelCase_						,    device_id=UpperCAmelCase_						,    max_text_length=UpperCAmelCase_						,    divergence_curve_discretization_size=UpperCAmelCase_						,    mauve_scaling_factor=UpperCAmelCase_						,    verbose=UpperCAmelCase_						,    seed=UpperCAmelCase_						,    )
														return out



