"""simple docstring"""







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 		UpperCamelCase      :



				def __init__(self    :				Union[str, Any]     ,  _A    :				Optional[int]     ,  _A    :				List[str]=13     ,  _A    :				str=30     ,  _A    :				Any=2     ,  _A    :				Tuple=3     ,  _A    :				int=True     ,  _A    :				Optional[Any]=True     ,  _A    :				Tuple=32     ,  _A    :				Tuple=5     ,  _A    :				List[str]=4     ,  _A    :				str=37     ,  _A    :				Optional[Any]="gelu"     ,  _A    :				Tuple=0.1     ,  _A    :				Any=0.1     ,  _A    :				Optional[Any]=10     ,  _A    :				List[str]=0.02     ,  _A    :				str=None     ,  )   ->     Optional[int]:
					__snake_case      :							Any						=			parent
					__snake_case      :							List[str]						=			batch_size
					__snake_case      :							str						=			image_size
					__snake_case      :							Optional[Any]						=			patch_size
					__snake_case      :							Optional[Any]						=			num_channels
					__snake_case      :							Union[str, Any]						=			is_training
					__snake_case      :							Any						=			use_labels
					__snake_case      :							str						=			hidden_size
					__snake_case      :							List[str]						=			num_hidden_layers
					__snake_case      :							Optional[int]						=			num_attention_heads
					__snake_case      :							Any						=			intermediate_size
					__snake_case      :							Any						=			hidden_act
					__snake_case      :							Any						=			hidden_dropout_prob
					__snake_case      :							Dict						=			attention_probs_dropout_prob
					__snake_case      :							Union[str, Any]						=			type_sequence_label_size
					__snake_case      :							Tuple						=			initializer_range
					__snake_case      :							Union[str, Any]						=			scope

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



				def  _lowercase					(self    :				Any)   ->     Optional[int]:
					__snake_case      :							Optional[int]						=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

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

					return config, pixel_values, labels



				def  _lowercase					(self    :				Optional[int])   ->     Tuple:
					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    :				Optional[int]     ,  _A    :				Tuple     ,  _A    :				str     ,  _A    :				Optional[int])   ->     Optional[int]:
					__snake_case      :							int						=			ViTMSNModel(config=_A)
					model.to(_A)
					model.eval()
					__snake_case      :							List[Any]						=			model(_A)
					self.parent.assertEqual(result.last_hidden_state.shape     ,  (self.batch_size, self.seq_length, self.hidden_size))



				def  _lowercase					(self    :				Any     ,  _A    :				int     ,  _A    :				Dict     ,  _A    :				Optional[Any])   ->     List[Any]:
					__snake_case      :							Tuple						=			self.type_sequence_label_size
					__snake_case      :							str						=			ViTMSNForImageClassification(_A)
					model.to(_A)
					model.eval()
					__snake_case      :							List[str]						=			model(_A     ,  labels=_A)
					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
					__snake_case      :							Any						=			1
					__snake_case      :							Optional[Any]						=			ViTMSNForImageClassification(_A)
					model.to(_A)
					model.eval()

					__snake_case      :							Tuple						=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
					__snake_case      :							List[str]						=			model(_A)
					self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.type_sequence_label_size))






				def  _lowercase					(self    :				Tuple)   ->     Union[str, Any]:
					__snake_case      :							Tuple						=			self.prepare_config_and_inputs()
					__snake_case		,    __snake_case		,    __snake_case      :							Tuple						=			config_and_inputs
					__snake_case      :							Union[str, Any]						=			{'pixel_values': pixel_values}
					return config, inputs_dict







@require_torch
class 		UpperCamelCase      (   lowercase				,     lowercase				,     unittest.TestCase						):
				UpperCAmelCase    :				int   =		(ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
				UpperCAmelCase    :				Any   =		(
				    {"""feature-extraction""": ViTMSNModel, """image-classification""": ViTMSNForImageClassification}
				    if is_torch_available()
				    else {}
				)

				UpperCAmelCase    :				str   =		False
				UpperCAmelCase    :				str   =		False
				UpperCAmelCase    :				Optional[Any]   =		False
				UpperCAmelCase    :				Any   =		False



				def  _lowercase					(self    :				int)   ->     List[str]:
					__snake_case      :							Optional[int]						=			ViTMSNModelTester(self)
					__snake_case      :							Tuple						=			ConfigTester(self     ,  config_class=_A     ,  has_text_modality=_A     ,  hidden_size=37)



				def  _lowercase					(self    :				Dict)   ->     str:
					self.config_tester.run_common_tests()



				@unittest.skip(reason='ViTMSN does not use inputs_embeds')
				def  _lowercase					(self    :				Union[str, Any])   ->     str:
					pass



				def  _lowercase					(self    :				Union[str, Any])   ->     Any:
					__snake_case		,    __snake_case      :							Any						=			self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						__snake_case      :							Dict						=			model_class(_A)
						self.assertIsInstance(model.get_input_embeddings()     ,  (nn.Module))
						__snake_case      :							str						=			model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(_A     ,  nn.Linear))



				def  _lowercase					(self    :				int)   ->     Dict:
					__snake_case		,    __snake_case      :							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs_for_common()

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

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



				def  _lowercase					(self    :				List[str])   ->     List[str]:
					__snake_case      :							Any						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_A)



				def  _lowercase					(self    :				List[str])   ->     Optional[int]:
					__snake_case      :							str						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*_A)






				@slow
				def  _lowercase					(self    :				Optional[Any])   ->     Optional[int]:
					for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Optional[int]						=			ViTMSNModel.from_pretrained(_A)
						self.assertIsNotNone(_A)







def   __UpperCAmelCase    (   )							->      Any:

	'''simple docstring'''





	__snake_case      :							Optional[int]						=			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
	return image

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



				@cached_property
				def  _lowercase					(self    :				Tuple)   ->     List[Any]:
					return ViTImageProcessor.from_pretrained('facebook/vit-msn-small') if is_vision_available() else None






				@slow
				def  _lowercase					(self    :				Union[str, Any])   ->     int:
					torch.manual_seed(2)
					__snake_case      :							Optional[Any]						=			ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small').to(_A)

					__snake_case      :							str						=			self.default_image_processor
					__snake_case      :							Union[str, Any]						=			prepare_img()
					__snake_case      :							Any						=			image_processor(images=_A     ,  return_tensors='pt').to(_A)

					# forward pass
					with torch.no_grad():
						__snake_case      :							List[str]						=			model(**_A)

					# verify the logits
					__snake_case      :							List[Any]						=			torch.Size((1, 10_00))
					self.assertEqual(outputs.logits.shape     ,  _A)

					__snake_case      :							Tuple						=			torch.tensor([-0.0_803, -0.4_454, -0.2_375]).to(_A)

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






"""simple docstring"""







import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch

class 		UpperCamelCase      (   lowercase						):



				@require_torch
				def  _lowercase					(self    :				Union[str, Any])   ->     Optional[Any]:
					# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
					# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
					# while running an external program

					# python one-liner segments

					# this must be loaded before socket.socket is monkey-patched
					__snake_case      :							Any						=			'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

					__snake_case      :							Tuple						=			'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

					__snake_case      :							int						=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n        '

					# Force fetching the files so that we can use the cache
					__snake_case      :							int						=			'hf-internal-testing/tiny-random-bert'
					BertConfig.from_pretrained(_A)
					BertModel.from_pretrained(_A)
					BertTokenizer.from_pretrained(_A)
					pipeline(task='fill-mask'     ,  model=_A)

					# baseline - just load from_pretrained with normal network
					__snake_case      :							Union[str, Any]						=			[sys.executable, '-c', '\n'.join([load, run, mock])]

					# should succeed
					__snake_case      :							Union[str, Any]						=			self.get_env()
					# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
					__snake_case      :							str						=			'1'
					__snake_case      :							Union[str, Any]						=			subprocess.run(_A     ,  env=_A     ,  check=_A     ,  capture_output=_A)
					self.assertEqual(result.returncode     ,  0     ,  result.stderr)
					self.assertIn('success'     ,  result.stdout.decode())



				@require_torch
				def  _lowercase					(self    :				Union[str, Any])   ->     Union[str, Any]:
					# python one-liner segments
					# this must be loaded before socket.socket is monkey-patched
					__snake_case      :							Optional[Any]						=			'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

					__snake_case      :							Optional[Any]						=			'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

					__snake_case      :							Union[str, Any]						=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n        '

					# Force fetching the files so that we can use the cache
					__snake_case      :							str						=			'hf-internal-testing/tiny-random-bert'
					BertConfig.from_pretrained(_A)
					BertModel.from_pretrained(_A)
					BertTokenizer.from_pretrained(_A)
					pipeline(task='fill-mask'     ,  model=_A)

					# baseline - just load from_pretrained with normal network
					__snake_case      :							Any						=			[sys.executable, '-c', '\n'.join([load, run, mock])]

					# should succeed
					__snake_case      :							int						=			self.get_env()
					__snake_case      :							Tuple						=			subprocess.run(_A     ,  env=_A     ,  check=_A     ,  capture_output=_A)
					self.assertEqual(result.returncode     ,  0     ,  result.stderr)
					self.assertIn('success'     ,  result.stdout.decode())



				@require_torch
				def  _lowercase					(self    :				int)   ->     Any:
					# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
					# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
					# while running an external program

					# python one-liner segments

					# this must be loaded before socket.socket is monkey-patched
					__snake_case      :							int						=			'\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

					__snake_case      :							int						=			'\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

					__snake_case      :							Optional[int]						=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n        '

					# baseline - just load from_pretrained with normal network
					__snake_case      :							Optional[Any]						=			[sys.executable, '-c', '\n'.join([load, run])]

					# should succeed
					__snake_case      :							Optional[int]						=			self.get_env()
					__snake_case      :							Dict						=			subprocess.run(_A     ,  env=_A     ,  check=_A     ,  capture_output=_A)
					self.assertEqual(result.returncode     ,  0     ,  result.stderr)
					self.assertIn('success'     ,  result.stdout.decode())

					# next emulate no network
					__snake_case      :							Optional[Any]						=			[sys.executable, '-c', '\n'.join([load, mock, run])]

					# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
					# env["TRANSFORMERS_OFFLINE"] = "0"
					# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
					# self.assertEqual(result.returncode, 1, result.stderr)

					# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
					__snake_case      :							Union[str, Any]						=			'1'
					__snake_case      :							str						=			subprocess.run(_A     ,  env=_A     ,  check=_A     ,  capture_output=_A)
					self.assertEqual(result.returncode     ,  0     ,  result.stderr)
					self.assertIn('success'     ,  result.stdout.decode())



				@require_torch
				def  _lowercase					(self    :				str)   ->     Dict:
					__snake_case      :							Dict						=			'\nfrom transformers import pipeline\n        '
					__snake_case      :							List[Any]						=			'\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

					__snake_case      :							List[str]						=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
					__snake_case      :							str						=			self.get_env()
					__snake_case      :							Tuple						=			'1'
					__snake_case      :							Tuple						=			[sys.executable, '-c', '\n'.join([load, mock, run])]
					__snake_case      :							Optional[Any]						=			subprocess.run(_A     ,  env=_A     ,  check=_A     ,  capture_output=_A)
					self.assertEqual(result.returncode     ,  1     ,  result.stderr)
					self.assertIn(
					    'You cannot infer task automatically within `pipeline` when using offline mode'     ,  result.stderr.decode().replace('\n'     ,  '')     ,  )






				@require_torch
				def  _lowercase					(self    :				int)   ->     Optional[Any]:
					__snake_case      :							int						=			'\nfrom transformers import AutoModel\n        '
					__snake_case      :							str						=			'\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n        '

					# baseline - just load from_pretrained with normal network
					__snake_case      :							str						=			[sys.executable, '-c', '\n'.join([load, run])]

					# should succeed
					__snake_case      :							str						=			self.get_env()
					__snake_case      :							Dict						=			subprocess.run(_A     ,  env=_A     ,  check=_A     ,  capture_output=_A)
					self.assertEqual(result.returncode     ,  0     ,  result.stderr)
					self.assertIn('success'     ,  result.stdout.decode())

					# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
					__snake_case      :							List[str]						=			'1'
					__snake_case      :							Optional[int]						=			subprocess.run(_A     ,  env=_A     ,  check=_A     ,  capture_output=_A)
					self.assertEqual(result.returncode     ,  0     ,  result.stderr)
					self.assertIn('success'     ,  result.stdout.decode())





