






'''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 _lowercase			(       unittest.TestCase	):
							a          = MODEL_FOR_MASKED_LM_MAPPING
							a          = TF_MODEL_FOR_MASKED_LM_MAPPING




							def 	lowerCamelCase_       (							self:       Tuple  ):
														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:       Optional[int]  ):
														lowerCamelCase__      :			Dict 						= pipeline(task="""fill-mask"""    ,							model="""sshleifer/tiny-distilroberta-base"""    ,							top_k=2    ,							framework="""tf"""  )
														lowerCamelCase__      :			Tuple 						= 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"""},
														    ]    ,							)

														lowerCamelCase__      :			int 						= 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""",
														        },
														    ]    ,							)

														lowerCamelCase__      :			Union[str, Any] 						= 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:       int  ):
														lowerCamelCase__      :			Optional[int] 						= pipeline(task="""fill-mask"""    ,							model="""sshleifer/tiny-distilroberta-base"""    ,							top_k=2    ,							framework="""pt"""  )

														lowerCamelCase__      :			int 						= 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"""},
														    ]    ,							)

														lowerCamelCase__      :			int 						= 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"""},
														    ]    ,							)

														lowerCamelCase__      :			Any 						= 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"""},
														    ]    ,							)

														lowerCamelCase__      :			List[Any] 						= 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:       Dict  ):
														lowerCamelCase__      :			Optional[int] 						= pipeline("""fill-mask"""    ,							model="""hf-internal-testing/tiny-random-distilbert"""    ,							device=0    ,							framework="""pt"""  )

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

														lowerCamelCase__      :			int 						= 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]  ):
														lowerCamelCase__      :			Optional[int] 						= pipeline(task="""fill-mask"""    ,							model="""distilroberta-base"""    ,							top_k=2    ,							framework="""pt"""  )
														self.run_large_test(UpperCamelCase__  )




							@slow
							@require_tf
							def 	lowerCamelCase_       (							self:       List[str]  ):
														lowerCamelCase__      :			str 						= pipeline(task="""fill-mask"""    ,							model="""distilroberta-base"""    ,							top_k=2    ,							framework="""tf"""  )
														self.run_large_test(UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Any    ,							UpperCamelCase__:       List[Any]  ):
														lowerCamelCase__      :			Optional[Any] 						= 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"""},
														    ]    ,							)
														lowerCamelCase__      :			List[str] 						= 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""",
														        },
														    ]    ,							)

														lowerCamelCase__      :			List[str] 						= 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:       List[str]  ):
														lowerCamelCase__      :			Dict 						= pipeline(task="""fill-mask"""    ,							model="""sshleifer/tiny-distilroberta-base"""    ,							framework="""pt"""  )
														lowerCamelCase__      :			List[str] 						= None
														lowerCamelCase__      :			Optional[int] 						= None
														self.run_pipeline_test(UpperCamelCase__    ,							[]  )




							@require_tf
							def 	lowerCamelCase_       (							self:       int  ):
														lowerCamelCase__      :			Optional[int] 						= pipeline(task="""fill-mask"""    ,							model="""sshleifer/tiny-distilroberta-base"""    ,							framework="""tf"""  )
														lowerCamelCase__      :			Optional[int] 						= None
														lowerCamelCase__      :			Optional[Any] 						= None
														self.run_pipeline_test(UpperCamelCase__    ,							[]  )




							def 	lowerCamelCase_       (							self:       int    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       Tuple  ):
														if tokenizer is None or tokenizer.mask_token_id is None:
																					self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)"""  )

														lowerCamelCase__      :			List[Any] 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__  )
														lowerCamelCase__      :			Union[str, Any] 						= [
														    F'''This is another {tokenizer.mask_token} test''',
														]
														return fill_masker, examples




							def 	lowerCamelCase_       (							self:       int    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       List[str]  ):
														lowerCamelCase__      :			str 						= fill_masker.tokenizer
														lowerCamelCase__      :			Tuple 						= fill_masker.model

														lowerCamelCase__      :			Union[str, Any] 						= 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__  )},
														    ]    ,							)

														lowerCamelCase__      :			List[Any] 						= 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__  )},
														    ]    ,							)

														lowerCamelCase__      :			Optional[int] 						= 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:       Optional[Any]    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       str  ):
														lowerCamelCase__      :			Tuple 						= tokenizer.get_vocab()
														lowerCamelCase__      :			Any 						= sorted(vocab.keys()  )[:2]
														# Pipeline argument
														lowerCamelCase__      :			int 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__    ,							targets=UpperCamelCase__  )
														lowerCamelCase__      :			str 						= 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__  )},
														    ]    ,							)
														lowerCamelCase__      :			List[str] 						= {vocab[el] for el in targets}
														self.assertEqual({el["""token"""] for el in outputs}    ,							UpperCamelCase__  )
														lowerCamelCase__      :			Any 						= [tokenizer.decode([x]  ) for x in target_ids]
														self.assertEqual({el["""token_str"""] for el in outputs}    ,							set(UpperCamelCase__  )  )

														# Call argument
														lowerCamelCase__      :			Optional[int] 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__  )
														lowerCamelCase__      :			Dict 						= 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__  )},
														    ]    ,							)
														lowerCamelCase__      :			Tuple 						= {vocab[el] for el in targets}
														self.assertEqual({el["""token"""] for el in outputs}    ,							UpperCamelCase__  )
														lowerCamelCase__      :			Optional[Any] 						= [tokenizer.decode([x]  ) for x in target_ids]
														self.assertEqual({el["""token_str"""] for el in outputs}    ,							set(UpperCamelCase__  )  )

														# Score equivalence
														lowerCamelCase__      :			Optional[Any] 						= fill_masker(F'''This is a {tokenizer.mask_token}'''    ,							targets=UpperCamelCase__  )
														lowerCamelCase__      :			List[str] 						= [top_mask["""token_str"""] for top_mask in outputs]
														lowerCamelCase__      :			List[str] 						= [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__  ):
																					lowerCamelCase__      :			int 						= fill_masker(F'''This is a {tokenizer.mask_token}'''    ,							targets=UpperCamelCase__  )
																					lowerCamelCase__      :			List[Any] 						= [top_mask["""score"""] for top_mask in unmasked_targets]
																					self.assertEqual(nested_simplify(UpperCamelCase__  )    ,							nested_simplify(UpperCamelCase__  )  )

														# Raises with invalid
														with self.assertRaises(UpperCamelCase__  ):
																					lowerCamelCase__      :			Any 						= 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__  ):
																												lowerCamelCase__      :			Optional[Any] 						= fill_masker(F'''This is a {tokenizer.mask_token}'''    ,							targets=[""""""]  )
																					with self.assertRaises(UpperCamelCase__  ):
																												lowerCamelCase__      :			Optional[int] 						= fill_masker(F'''This is a {tokenizer.mask_token}'''    ,							targets=""""""  )




							def 	lowerCamelCase_       (							self:       Tuple    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       List[Any]  ):
														lowerCamelCase__      :			int 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__    ,							top_k=2  )
														lowerCamelCase__      :			str 						= 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__  )},
														    ]    ,							)

														lowerCamelCase__      :			Any 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__  )
														lowerCamelCase__      :			Tuple 						= 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:       int    ,							UpperCamelCase__:       Optional[Any]    ,							UpperCamelCase__:       Tuple  ):
														lowerCamelCase__      :			Dict 						= tokenizer.get_vocab()
														lowerCamelCase__      :			List[Any] 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__  )

														# top_k=2, ntargets=3
														lowerCamelCase__      :			int 						= sorted(vocab.keys()  )[:3]
														lowerCamelCase__      :			List[Any] 						= 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
														lowerCamelCase__      :			List[Any] 						= [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__  ):
																					lowerCamelCase__      :			Tuple 						= 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__:       Tuple    ,							UpperCamelCase__:       List[str]  ):
														lowerCamelCase__      :			Dict 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__  )
														lowerCamelCase__      :			Optional[int] 						= tokenizer.get_vocab()
														# String duplicates + id duplicates
														lowerCamelCase__      :			int 						= sorted(vocab.keys()  )[:3]
														lowerCamelCase__      :			List[Any] 						= [targets[0], targets[1], targets[0], targets[2], targets[1]]
														lowerCamelCase__      :			str 						= 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:       Tuple    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       Dict  ):
														lowerCamelCase__      :			str 						= FillMaskPipeline(model=UpperCamelCase__    ,							tokenizer=UpperCamelCase__  )

														lowerCamelCase__      :			int 						= 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 inspect
import unittest

from transformers import MobileViTVaConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, MobileViTVaModel
			from transformers.models.mobilevitva.modeling_mobilevitva import (
			    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
			    make_divisible,
			)


if is_vision_available():
			from PIL import Image

			from transformers import MobileViTImageProcessor

class _lowercase			(       _lowercase	):




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			Dict 						= self.config_class(**self.inputs_dict  )
														self.parent.assertTrue(hasattr(UpperCamelCase__    ,							"""width_multiplier"""  )  )



class _lowercase			:




							def __init__(							self:       str    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       str=13    ,							UpperCamelCase__:       Any=64    ,							UpperCamelCase__:       Optional[Any]=2    ,							UpperCamelCase__:       str=3    ,							UpperCamelCase__:       List[str]="swish"    ,							UpperCamelCase__:       Any=3    ,							UpperCamelCase__:       Optional[int]=32    ,							UpperCamelCase__:       Union[str, Any]=0.1    ,							UpperCamelCase__:       int=0.02    ,							UpperCamelCase__:       Dict=True    ,							UpperCamelCase__:       Dict=True    ,							UpperCamelCase__:       Any=10    ,							UpperCamelCase__:       int=None    ,							UpperCamelCase__:       List[Any]=0.25    ,							UpperCamelCase__:       str=0.0    ,							UpperCamelCase__:       Optional[int]=0.0    ,							):
														lowerCamelCase__      :			Any 						= parent
														lowerCamelCase__      :			Optional[Any] 						= batch_size
														lowerCamelCase__      :			Optional[int] 						= image_size
														lowerCamelCase__      :			str 						= patch_size
														lowerCamelCase__      :			Optional[int] 						= num_channels
														lowerCamelCase__      :			Optional[Any] 						= make_divisible(512 * width_multiplier    ,							divisor=8  )
														lowerCamelCase__      :			List[str] 						= hidden_act
														lowerCamelCase__      :			Any 						= conv_kernel_size
														lowerCamelCase__      :			Any 						= output_stride
														lowerCamelCase__      :			Union[str, Any] 						= classifier_dropout_prob
														lowerCamelCase__      :			List[str] 						= use_labels
														lowerCamelCase__      :			Optional[Any] 						= is_training
														lowerCamelCase__      :			List[str] 						= num_labels
														lowerCamelCase__      :			Dict 						= initializer_range
														lowerCamelCase__      :			List[Any] 						= scope
														lowerCamelCase__      :			Tuple 						= width_multiplier
														lowerCamelCase__      :			List[Any] 						= ffn_dropout
														lowerCamelCase__      :			Any 						= attn_dropout




							def 	lowerCamelCase_       (							self:       Dict  ):
														lowerCamelCase__      :			Dict 						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

														lowerCamelCase__      :			Tuple 						= None
														lowerCamelCase__      :			Optional[Any] 						= None
														if self.use_labels:
																					lowerCamelCase__      :			List[Any] 						= ids_tensor([self.batch_size]    ,							self.num_labels  )
																					lowerCamelCase__      :			str 						= ids_tensor([self.batch_size, self.image_size, self.image_size]    ,							self.num_labels  )

														lowerCamelCase__      :			Union[str, Any] 						= self.get_config()

														return config, pixel_values, labels, pixel_labels




							def 	lowerCamelCase_       (							self:       List[Any]  ):
														return MobileViTVaConfig(
														    image_size=self.image_size    ,							patch_size=self.patch_size    ,							num_channels=self.num_channels    ,							hidden_act=self.hidden_act    ,							conv_kernel_size=self.conv_kernel_size    ,							output_stride=self.output_stride    ,							classifier_dropout_prob=self.classifier_dropout_prob    ,							initializer_range=self.initializer_range    ,							width_multiplier=self.width_multiplier    ,							ffn_dropout=self.ffn_dropout_prob    ,							attn_dropout=self.attn_dropout_prob    ,							)




							def 	lowerCamelCase_       (							self:       Union[str, Any]    ,							UpperCamelCase__:       Optional[Any]    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       Tuple    ,							UpperCamelCase__:       Optional[int]  ):
														lowerCamelCase__      :			Union[str, Any] 						= MobileViTVaModel(config=UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.eval()
														lowerCamelCase__      :			str 						= model(UpperCamelCase__  )
														self.parent.assertEqual(
														    result.last_hidden_state.shape    ,							(
														        self.batch_size,
														        self.last_hidden_size,
														        self.image_size // self.output_stride,
														        self.image_size // self.output_stride,
														    )    ,							)




							def 	lowerCamelCase_       (							self:       Union[str, Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Tuple  ):
														lowerCamelCase__      :			Tuple 						= self.num_labels
														lowerCamelCase__      :			Dict 						= MobileViTVaForImageClassification(UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.eval()
														lowerCamelCase__      :			int 						= model(UpperCamelCase__    ,							labels=UpperCamelCase__  )
														self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_labels)  )




							def 	lowerCamelCase_       (							self:       List[Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Optional[Any]    ,							UpperCamelCase__:       str  ):
														lowerCamelCase__      :			List[str] 						= self.num_labels
														lowerCamelCase__      :			Union[str, Any] 						= MobileViTVaForSemanticSegmentation(UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.eval()
														lowerCamelCase__      :			Tuple 						= model(UpperCamelCase__  )
														self.parent.assertEqual(
														    result.logits.shape    ,							(
														        self.batch_size,
														        self.num_labels,
														        self.image_size // self.output_stride,
														        self.image_size // self.output_stride,
														    )    ,							)
														lowerCamelCase__      :			List[Any] 						= model(UpperCamelCase__    ,							labels=UpperCamelCase__  )
														self.parent.assertEqual(
														    result.logits.shape    ,							(
														        self.batch_size,
														        self.num_labels,
														        self.image_size // self.output_stride,
														        self.image_size // self.output_stride,
														    )    ,							)




							def 	lowerCamelCase_       (							self:       Tuple  ):
														lowerCamelCase__      :			Any 						= self.prepare_config_and_inputs()
														lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__      :			Union[str, Any] 						= config_and_inputs
														lowerCamelCase__      :			Optional[Any] 						= {"""pixel_values""": pixel_values}
														return config, inputs_dict



@require_torch
class _lowercase			(       _lowercase       ,   _lowercase       ,   unittest.TestCase	):
							a          = (
							    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)

							a          = (
							    {
							        """feature-extraction""": MobileViTVaModel,
							        """image-classification""": MobileViTVaForImageClassification,
							        """image-segmentation""": MobileViTVaForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							a          = False
							a          = False
							a          = False
							a          = False




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														lowerCamelCase__      :			Tuple 						= MobileViTVaModelTester(self  )
														lowerCamelCase__      :			List[str] 						= MobileViTVaConfigTester(self    ,							config_class=UpperCamelCase__    ,							has_text_modality=UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Tuple  ):
														self.config_tester.run_common_tests()




							@unittest.skip(reason="""MobileViTV2 does not use inputs_embeds"""  )
							def 	lowerCamelCase_       (							self:       int  ):
														pass




							@unittest.skip(reason="""MobileViTV2 does not support input and output embeddings"""  )
							def 	lowerCamelCase_       (							self:       List[str]  ):
														pass




							@unittest.skip(reason="""MobileViTV2 does not output attentions"""  )
							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														pass




							@require_torch_multi_gpu
							@unittest.skip(reason="""Got `CUDA error: misaligned address` for tests after this one being run."""  )
							def 	lowerCamelCase_       (							self:       int  ):
														pass




							@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""  )
							def 	lowerCamelCase_       (							self:       Tuple  ):
														pass




							def 	lowerCamelCase_       (							self:       Tuple  ):
														lowerCamelCase__						,	lowerCamelCase__      :			int 						= self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCamelCase__      :			Union[str, Any] 						= model_class(UpperCamelCase__  )
																					lowerCamelCase__      :			int 						= inspect.signature(model.forward  )
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					lowerCamelCase__      :			Tuple 						= [*signature.parameters.keys()]

																					lowerCamelCase__      :			str 						= ["""pixel_values"""]
																					self.assertListEqual(arg_names[:1]    ,							UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       str  ):
														lowerCamelCase__      :			int 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       List[str]  ):
														def check_hidden_states_output(UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       Optional[Any]  ):
																					lowerCamelCase__      :			List[Any] 						= model_class(UpperCamelCase__  )
																					model.to(UpperCamelCase__  )
																					model.eval()

																					with torch.no_grad():
																												lowerCamelCase__      :			Tuple 						= model(**self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )  )

																					lowerCamelCase__      :			Optional[int] 						= outputs.hidden_states

																					lowerCamelCase__      :			List[Any] 						= 5
																					self.assertEqual(len(UpperCamelCase__  )    ,							UpperCamelCase__  )

																					# MobileViTV2's feature maps are of shape (batch_size, num_channels, height, width)
																					# with the width and height being successively divided by 2.
																					lowerCamelCase__      :			int 						= 2
																					for i in range(len(UpperCamelCase__  )  ):
																												self.assertListEqual(
																												    list(hidden_states[i].shape[-2:]  )    ,							[self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]    ,							)
																												divisor *= 2

																					self.assertEqual(self.model_tester.output_stride    ,							divisor // 2  )

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

														for model_class in self.all_model_classes:
																					lowerCamelCase__      :			int 						= True
																					check_hidden_states_output(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__  )

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

																					check_hidden_states_output(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			int 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Dict  ):
														lowerCamelCase__      :			str 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase__  )




							@slow
							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__      :			Union[str, Any] 						= MobileViTVaModel.from_pretrained(UpperCamelCase__  )
																					self.assertIsNotNone(UpperCamelCase__  )





def 				SCREAMING_SNAKE_CASE_ ()   -> Optional[int]:
							lowerCamelCase__      :			str 						= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
							return image



@require_torch
@require_vision
class _lowercase			(       unittest.TestCase	):




							@cached_property
							def 	lowerCamelCase_       (							self:       Tuple  ):
														return (
														    MobileViTImageProcessor.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""  )
														    if is_vision_available()
														    else None
														)




							@slow
							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			Optional[Any] 						= MobileViTVaForImageClassification.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""  ).to(
														    UpperCamelCase__  )

														lowerCamelCase__      :			Union[str, Any] 						= self.default_image_processor
														lowerCamelCase__      :			List[Any] 						= prepare_img()
														lowerCamelCase__      :			Any 						= image_processor(images=UpperCamelCase__    ,							return_tensors="""pt"""  ).to(UpperCamelCase__  )

														# forward pass
														with torch.no_grad():
																					lowerCamelCase__      :			int 						= model(**UpperCamelCase__  )

														# verify the logits
														lowerCamelCase__      :			str 						= torch.Size((1, 1_000)  )
														self.assertEqual(outputs.logits.shape    ,							UpperCamelCase__  )

														lowerCamelCase__      :			Optional[int] 						= torch.tensor([-1.6_336e00, -7.3_204e-02, -5.1_883e-01]  ).to(UpperCamelCase__  )

														self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,							UpperCamelCase__    ,							atol=1e-4  )  )




							@slow
							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														lowerCamelCase__      :			int 						= MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""  )
														lowerCamelCase__      :			Optional[Any] 						= model.to(UpperCamelCase__  )

														lowerCamelCase__      :			Any 						= MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""  )

														lowerCamelCase__      :			Union[str, Any] 						= prepare_img()
														lowerCamelCase__      :			Dict 						= image_processor(images=UpperCamelCase__    ,							return_tensors="""pt"""  ).to(UpperCamelCase__  )

														# forward pass
														with torch.no_grad():
																					lowerCamelCase__      :			Optional[Any] 						= model(**UpperCamelCase__  )
														lowerCamelCase__      :			str 						= outputs.logits

														# verify the logits
														lowerCamelCase__      :			List[str] 						= torch.Size((1, 21, 32, 32)  )
														self.assertEqual(logits.shape    ,							UpperCamelCase__  )

														lowerCamelCase__      :			Any 						= torch.tensor(
														    [
														        [[7.0_863, 7.1_525, 6.8_201], [6.6_931, 6.8_770, 6.8_933], [6.2_978, 7.0_366, 6.9_636]],
														        [[-3.7_134, -3.6_712, -3.6_675], [-3.5_825, -3.3_549, -3.4_777], [-3.3_435, -3.3_979, -3.2_857]],
														        [[-2.9_329, -2.8_003, -2.7_369], [-3.0_564, -2.4_780, -2.0_207], [-2.6_889, -1.9_298, -1.7_640]],
														    ]    ,							device=UpperCamelCase__    ,							)

														self.assertTrue(torch.allclose(logits[0, :3, :3, :3]    ,							UpperCamelCase__    ,							atol=1e-4  )  )




							@slow
							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														lowerCamelCase__      :			Optional[Any] 						= MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""  )
														lowerCamelCase__      :			List[Any] 						= model.to(UpperCamelCase__  )

														lowerCamelCase__      :			Union[str, Any] 						= MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""  )

														lowerCamelCase__      :			Optional[Any] 						= prepare_img()
														lowerCamelCase__      :			Dict 						= image_processor(images=UpperCamelCase__    ,							return_tensors="""pt"""  ).to(UpperCamelCase__  )

														# forward pass
														with torch.no_grad():
																					lowerCamelCase__      :			Dict 						= model(**UpperCamelCase__  )

														lowerCamelCase__      :			List[str] 						= outputs.logits.detach().cpu()

														lowerCamelCase__      :			List[Any] 						= image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__    ,							target_sizes=[(50, 60)]  )
														lowerCamelCase__      :			int 						= torch.Size((50, 60)  )
														self.assertEqual(segmentation[0].shape    ,							UpperCamelCase__  )

														lowerCamelCase__      :			Optional[Any] 						= image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__  )
														lowerCamelCase__      :			int 						= torch.Size((32, 32)  )
														self.assertEqual(segmentation[0].shape    ,							UpperCamelCase__  )

