



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





		lowerCAmelCase			:					Tuple   	=				MODEL_FOR_MASKED_LM_MAPPING
		lowerCAmelCase			:					Any   	=				TF_MODEL_FOR_MASKED_LM_MAPPING







		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		int:






				"""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 					UpperCAmelCase    (	self						:  List[str]						)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    Dict				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,top_k=2			,framework='''tf'''						)
				lowercase__   :    List[str]				=			unmasker('''My name is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,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'''},
				    ]			,)

				lowercase__   :    List[str]				=			unmasker('''The largest city in France is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,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''',
				        },
				    ]			,)

				lowercase__   :    Any				=			unmasker('''My name is <mask>'''			,targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,top_k=3						)
				self.assertEqual(
				    nested_simplify(_snake_case			,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 					UpperCAmelCase    (	self						:  Dict						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,top_k=2			,framework='''pt'''						)

				lowercase__   :    Any				=			unmasker('''My name is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,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'''},
				    ]			,)

				lowercase__   :    int				=			unmasker('''The largest city in France is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,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'''},
				    ]			,)

				lowercase__   :    Optional[int]				=			unmasker('''My name is <mask>'''			,targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,top_k=3						)
				self.assertEqual(
				    nested_simplify(_snake_case			,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'''},
				    ]			,)

				lowercase__   :    Any				=			unmasker('''My name is <mask> <mask>'''			,top_k=2						)

				self.assertEqual(
				    nested_simplify(_snake_case			,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 					UpperCAmelCase    (	self						:  str						)					->		int:






				"""simple docstring"""



				lowercase__   :    List[str]				=			pipeline('''fill-mask'''			,model='''hf-internal-testing/tiny-random-distilbert'''			,device=0			,framework='''pt'''						)

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

				lowercase__   :    Union[str, Any]				=			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(_snake_case			,_snake_case						)







		@slow
		@require_torch
		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Dict				=			pipeline(task='''fill-mask'''			,model='''distilroberta-base'''			,top_k=2			,framework='''pt'''						)
				self.run_large_test(_snake_case						)







		@slow
		@require_tf
		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Dict				=			pipeline(task='''fill-mask'''			,model='''distilroberta-base'''			,top_k=2			,framework='''tf'''						)
				self.run_large_test(_snake_case						)







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






				"""simple docstring"""



				lowercase__   :    Tuple				=			unmasker('''My name is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case						)			,[
				        {'''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'''},
				    ]			,)
				lowercase__   :    int				=			unmasker('''The largest city in France is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case						)			,[
				        {
				            '''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''',
				        },
				    ]			,)

				lowercase__   :    Dict				=			unmasker('''My name is <mask>'''			,targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,top_k=3						)
				self.assertEqual(
				    nested_simplify(_snake_case						)			,[
				        {'''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 					UpperCAmelCase    (	self						:  Dict						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    str				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,framework='''pt'''						)
				lowercase__   :    Tuple				=			None
				lowercase__   :    str				=			None
				self.run_pipeline_test(_snake_case			,[]						)







		@require_tf
		def 					UpperCAmelCase    (	self						:  int						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,framework='''tf'''						)
				lowercase__   :    str				=			None
				lowercase__   :    List[Any]				=			None
				self.run_pipeline_test(_snake_case			,[]						)







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






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

				lowercase__   :    List[str]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    List[str]				=			[
				    f"""This is another {tokenizer.mask_token} test""",
				]
				return fill_masker, examples







		def 					UpperCAmelCase    (	self						:  List[Any]			,_snake_case						:  Union[str, Any]			,_snake_case						:  str						)					->		Optional[Any]:






				"""simple docstring"""



				lowercase__   :    List[str]				=			fill_masker.tokenizer
				lowercase__   :    List[Any]				=			fill_masker.model

				lowercase__   :    str				=			fill_masker(
				    f"""This is a {tokenizer.mask_token}"""			,)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)

				lowercase__   :    int				=			fill_masker([f"""This is a {tokenizer.mask_token}"""]						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)

				lowercase__   :    Optional[Any]				=			fill_masker([f"""This is a {tokenizer.mask_token}""", f"""Another {tokenizer.mask_token} great test."""]						)
				self.assertEqual(
				    _snake_case			,[
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				    ]			,)

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

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







		def 					UpperCAmelCase    (	self						:  List[Any]			,_snake_case						:  Any			,_snake_case						:  Dict						)					->		Tuple:






				"""simple docstring"""



				lowercase__   :    Tuple				=			tokenizer.get_vocab()
				lowercase__   :    List[Any]				=			sorted(vocab.keys()						)[:2]
				# Pipeline argument
				lowercase__   :    Dict				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case			,targets=_snake_case						)
				lowercase__   :    Optional[Any]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)
				lowercase__   :    Tuple				=			{vocab[el] for el in targets}
				self.assertEqual({el['''token'''] for el in outputs}			,_snake_case						)
				lowercase__   :    Tuple				=			[tokenizer.decode([x]						) for x in target_ids]
				self.assertEqual({el['''token_str'''] for el in outputs}			,set(_snake_case						)						)

				# Call argument
				lowercase__   :    str				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    Union[str, Any]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=_snake_case						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)
				lowercase__   :    int				=			{vocab[el] for el in targets}
				self.assertEqual({el['''token'''] for el in outputs}			,_snake_case						)
				lowercase__   :    Dict				=			[tokenizer.decode([x]						) for x in target_ids]
				self.assertEqual({el['''token_str'''] for el in outputs}			,set(_snake_case						)						)

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

				# Raises with invalid
				with self.assertRaises(_snake_case						):
						lowercase__   :    str				=			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(_snake_case						):
								lowercase__   :    str				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=['''''']						)
						with self.assertRaises(_snake_case						):
								lowercase__   :    Tuple				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=''''''						)







		def 					UpperCAmelCase    (	self						:  Tuple			,_snake_case						:  Any			,_snake_case						:  Optional[int]						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case			,top_k=2						)
				lowercase__   :    List[str]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)

				lowercase__   :    Optional[Any]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    Dict				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,top_k=2						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)
				self.assertEqual(nested_simplify(_snake_case						)			,nested_simplify(_snake_case						)						)







		def 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  Optional[int]			,_snake_case						:  List[str]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    int				=			tokenizer.get_vocab()
				lowercase__   :    Optional[int]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)

				# top_k=2, ntargets=3
				lowercase__   :    Tuple				=			sorted(vocab.keys()						)[:3]
				lowercase__   :    int				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,top_k=2			,targets=_snake_case						)

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







		def 					UpperCAmelCase    (	self						:  List[str]			,_snake_case						:  List[Any]			,_snake_case						:  Any						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    Tuple				=			tokenizer.get_vocab()
				# String duplicates + id duplicates
				lowercase__   :    Optional[int]				=			sorted(vocab.keys()						)[:3]
				lowercase__   :    List[str]				=			[targets[0], targets[1], targets[0], targets[2], targets[1]]
				lowercase__   :    Optional[int]				=			fill_masker(f"""My name is {tokenizer.mask_token}"""			,targets=_snake_case			,top_k=10						)

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







		def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  Dict			,_snake_case						:  Union[str, Any]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)

				lowercase__   :    int				=			fill_masker(
				    f"""This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"""			,top_k=2						)
				self.assertEqual(
				    _snake_case			,[
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				    ]			,)







import inspect
import unittest

from transformers import ViTMSNConfig
from transformers.testing_utils import require_torch, 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 torch import nn

    from transformers import ViTMSNForImageClassification, ViTMSNModel
    from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import ViTImageProcessor





class  A__    :




   """simple docstring"""



   def __init__(					self					,  lowercase					,  lowercase=13					,  lowercase=30					,  lowercase=2					,  lowercase=3					,  lowercase=True					,  lowercase=True					,  lowercase=32					,  lowercase=5					,  lowercase=4					,  lowercase=37					,  lowercase="gelu"					,  lowercase=0.1					,  lowercase=0.1					,  lowercase=10					,  lowercase=0.02					,  lowercase=None					,  )			->						Union[str, Any]:







          '''simple docstring'''

          a__						:	Optional[int] =				parent
          a__						:	List[str] =				batch_size
          a__						:	List[str] =				image_size
          a__						:	Dict =				patch_size
          a__						:	Optional[Any] =				num_channels
          a__						:	List[Any] =				is_training
          a__						:	str =				use_labels
          a__						:	Dict =				hidden_size
          a__						:	Tuple =				num_hidden_layers
          a__						:	Tuple =				num_attention_heads
          a__						:	Union[str, Any] =				intermediate_size
          a__						:	List[str] =				hidden_act
          a__						:	List[str] =				hidden_dropout_prob
          a__						:	Any =				attention_probs_dropout_prob
          a__						:	Dict =				type_sequence_label_size
          a__						:	Tuple =				initializer_range
          a__						:	Optional[int] =				scope

          # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
          a__						:	List[str] =				(image_size // patch_size) ** 2
          a__						:	Any =				num_patches + 1

   def 							__lowercase		(					self)			->						int:







          '''simple docstring'''

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

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

          a__						:	List[str] =				self.get_config()

          return config, pixel_values, labels

   def 							__lowercase		(					self)			->						Optional[int]:







          '''simple docstring'''

          return ViTMSNConfig(
              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					,  initializer_range=self.initializer_range					,  )

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase)			->						List[str]:







          '''simple docstring'''

          a__						:	int =				ViTMSNModel(config=lowercase)
          model.to(lowercase)
          model.eval()
          a__						:	int =				model(lowercase)
          self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size))

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase)			->						Tuple:







          '''simple docstring'''

          a__						:	Optional[Any] =				self.type_sequence_label_size
          a__						:	List[str] =				ViTMSNForImageClassification(lowercase)
          model.to(lowercase)
          model.eval()
          a__						:	int =				model(lowercase					,  labels=lowercase)
          print('Pixel and labels shape: {pixel_values.shape}, {labels.shape}')
          print('Labels: {labels}')
          self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.type_sequence_label_size))

          # test greyscale images
          a__						:	List[str] =				1
          a__						:	Optional[int] =				ViTMSNForImageClassification(lowercase)
          model.to(lowercase)
          model.eval()

          a__						:	Dict =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          a__						:	Dict =				model(lowercase)
          self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.type_sequence_label_size))

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

          a__						:	List[Any] =				self.prepare_config_and_inputs()
          a__ ,							a__ ,							a__						:	Optional[Any] =				config_and_inputs
          a__						:	Dict =				{'pixel_values': pixel_values}
          return config, inputs_dict





@require_torch
class  A__    (							__UpperCAmelCase ,  __UpperCAmelCase ,  unittest.TestCase	):




   """simple docstring"""



   __A   :				Any         =  (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
   __A   :				Tuple         =  (
       {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification}
       if is_torch_available()
       else {}
   )

   __A   :				List[str]         =  False
   __A   :				Optional[Any]         =  False
   __A   :				Union[str, Any]         =  False
   __A   :				Any         =  False

   def 							__lowercase		(					self)			->						List[str]:







          '''simple docstring'''

          a__						:	Optional[int] =				ViTMSNModelTester(self)
          a__						:	Union[str, Any] =				ConfigTester(self					,  config_class=lowercase					,  has_text_modality=lowercase					,  hidden_size=37)

   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          self.config_tester.run_common_tests()

   @unittest.skip(reason='ViTMSN does not use inputs_embeds')
   def 							__lowercase		(					self)			->						Any:







          '''simple docstring'''

          pass

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

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

          for model_class in self.all_model_classes:
                 a__						:	Union[str, Any] =				model_class(lowercase)
                 self.assertIsInstance(model.get_input_embeddings()					,  (nn.Module))
                 a__						:	str =				model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(lowercase					,  nn.Linear))

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

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

          for model_class in self.all_model_classes:
                 a__						:	Dict =				model_class(lowercase)
                 a__						:	List[Any] =				inspect.signature(model.forward)
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 a__						:	str =				[*signature.parameters.keys()]

                 a__						:	Optional[Any] =				['pixel_values']
                 self.assertListEqual(arg_names[:1]					,  lowercase)

   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

          a__						:	List[str] =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase)

   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

          a__						:	Any =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowercase)

   @slow
   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a__						:	Tuple =				ViTMSNModel.from_pretrained(lowercase)
                 self.assertIsNotNone(lowercase)



def A_	(		)  ->		Dict:
       a__						:	List[str] =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
       return image





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




   """simple docstring"""



   @cached_property
   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          return ViTImageProcessor.from_pretrained('facebook/vit-msn-small') if is_vision_available() else None

   @slow
   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          torch.manual_seed(2)
          a__						:	List[str] =				ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small').to(lowercase)

          a__						:	Any =				self.default_image_processor
          a__						:	List[Any] =				prepare_img()
          a__						:	Optional[Any] =				image_processor(images=lowercase					,  return_tensors='pt').to(lowercase)

          # forward pass
          with torch.no_grad():
                 a__						:	Tuple =				model(**lowercase)

          # verify the logits
          a__						:	Union[str, Any] =				torch.Size((1, 1000))
          self.assertEqual(outputs.logits.shape					,  lowercase)

          a__						:	Any =				torch.tensor([-0.08_03, -0.44_54, -0.23_75]).to(lowercase)

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


