





"""simple docstring"""




import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import (
						    MODEL_MAPPING,
						    BeitForImageClassification,
						    BeitForMaskedImageModeling,
						    BeitForSemanticSegmentation,
						    BeitModel,
						)
						from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						import PIL
						from PIL import Image

						from transformers import BeitImageProcessor




class 			_a						:






				def __init__(			self		:   Dict,			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   Tuple=1_0_0,			lowerCAmelCase__		:   str=1_3,			lowerCAmelCase__		:   Any=3_0,			lowerCAmelCase__		:   Tuple=2,			lowerCAmelCase__		:   List[Any]=3,			lowerCAmelCase__		:   int=True,			lowerCAmelCase__		:   Union[str, Any]=True,			lowerCAmelCase__		:   int=3_2,			lowerCAmelCase__		:   str=4,			lowerCAmelCase__		:   str=4,			lowerCAmelCase__		:   str=3_7,			lowerCAmelCase__		:   Union[str, Any]="gelu",			lowerCAmelCase__		:   Optional[int]=0.1,			lowerCAmelCase__		:   Optional[int]=0.1,			lowerCAmelCase__		:   Optional[int]=1_0,			lowerCAmelCase__		:   List[Any]=0.02,			lowerCAmelCase__		:   Tuple=3,			lowerCAmelCase__		:   Dict=None,			lowerCAmelCase__		:   Any=[0, 1, 2, 3],			)     ->				List[str]:




										'''simple docstring'''



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

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






				def     a     (			self		:   str							)     ->				Dict:




										'''simple docstring'''



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

										_UpperCamelCase      : Any         =   None
										_UpperCamelCase      : str         =   None
										if self.use_labels:
																_UpperCamelCase      : Dict         =   ids_tensor([self.batch_size],			self.type_sequence_label_size							)
																_UpperCamelCase      : Optional[int]         =   ids_tensor([self.batch_size, self.image_size, self.image_size],			self.num_labels							)

										_UpperCamelCase      : Tuple         =   self.get_config()

										return config, pixel_values, labels, pixel_labels






				def     a     (			self		:   Optional[Any]							)     ->				Any:




										'''simple docstring'''



										return BeitConfig(
										    vocab_size=self.vocab_size,			image_size=self.image_size,			patch_size=self.patch_size,			num_channels=self.num_channels,			hidden_size=self.hidden_size,			num_hidden_layers=self.num_hidden_layers,			num_attention_heads=self.num_attention_heads,			intermediate_size=self.intermediate_size,			hidden_act=self.hidden_act,			hidden_dropout_prob=self.hidden_dropout_prob,			attention_probs_dropout_prob=self.attention_probs_dropout_prob,			is_decoder=lowerCAmelCase__,			initializer_range=self.initializer_range,			out_indices=self.out_indices,			)






				def     a     (			self		:   Any,			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   Dict							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   BeitModel(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										_UpperCamelCase      : Optional[Any]         =   model(lowerCAmelCase__							)
										self.parent.assertEqual(result.last_hidden_state.shape,			(self.batch_size, self.seq_length, self.hidden_size)							)






				def     a     (			self		:   Any,			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   Tuple,			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Dict							)     ->				Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   BeitForMaskedImageModeling(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										_UpperCamelCase      : Any         =   model(lowerCAmelCase__							)
										self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.seq_length - 1, self.vocab_size)							)






				def     a     (			self		:   Optional[Any],			lowerCAmelCase__		:   Tuple,			lowerCAmelCase__		:   Dict,			lowerCAmelCase__		:   Tuple,			lowerCAmelCase__		:   int							)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.type_sequence_label_size
										_UpperCamelCase      : Dict         =   BeitForImageClassification(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										_UpperCamelCase      : List[Any]         =   model(lowerCAmelCase__,			labels=lowerCAmelCase__							)
										self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.type_sequence_label_size)							)

										# test greyscale images
										_UpperCamelCase      : Optional[Any]         =   1
										_UpperCamelCase      : List[str]         =   BeitForImageClassification(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()

										_UpperCamelCase      : Dict         =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
										_UpperCamelCase      : Optional[int]         =   model(lowerCAmelCase__,			labels=lowerCAmelCase__							)
										self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.type_sequence_label_size)							)






				def     a     (			self		:   Dict,			lowerCAmelCase__		:   Dict,			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Tuple							)     ->				List[str]:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.num_labels
										_UpperCamelCase      : Tuple         =   BeitForSemanticSegmentation(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										_UpperCamelCase      : Dict         =   model(lowerCAmelCase__							)
										self.parent.assertEqual(
										    result.logits.shape,			(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)							)
										_UpperCamelCase      : Union[str, Any]         =   model(lowerCAmelCase__,			labels=lowerCAmelCase__							)
										self.parent.assertEqual(
										    result.logits.shape,			(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)							)






				def     a     (			self		:   Dict							)     ->				List[str]:




										'''simple docstring'''



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




@require_torch
class 			_a						(				_lowerCAmelCase     ,    _lowerCAmelCase     ,    unittest.TestCase    ):
				UpperCamelCase								=   (
				    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				UpperCamelCase								=   (
				    {
				        '''feature-extraction''': BeitModel,
				        '''image-classification''': BeitForImageClassification,
				        '''image-segmentation''': BeitForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				UpperCamelCase								=   False
				UpperCamelCase								=   False
				UpperCamelCase								=   False






				def     a     (			self		:   Dict							)     ->				List[str]:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   BeitModelTester(self							)
										_UpperCamelCase      : List[Any]         =   ConfigTester(self,			config_class=lowerCAmelCase__,			has_text_modality=lowerCAmelCase__,			hidden_size=3_7							)






				def     a     (			self		:   Optional[int]							)     ->				Tuple:




										'''simple docstring'''



										self.config_tester.run_common_tests()






				@unittest.skip(reason='''BEiT does not use inputs_embeds'''							)
				def     a     (			self		:   Dict							)     ->				Dict:




										'''simple docstring'''



										pass






				@require_torch_multi_gpu
				@unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''							)
				def     a     (			self		:   Optional[Any]							)     ->				Tuple:




										'''simple docstring'''



										pass






				def     a     (			self		:   Optional[Any]							)     ->				Optional[int]:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																_UpperCamelCase      : Any         =   model_class(lowerCAmelCase__							)
																self.assertIsInstance(model.get_input_embeddings(),			(nn.Module)							)
																_UpperCamelCase      : int         =   model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(lowerCAmelCase__,			nn.Linear							)							)






				def     a     (			self		:   Dict							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.model_tester.prepare_config_and_inputs_for_common()

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

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






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




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__							)






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




										'''simple docstring'''



										_UpperCamelCase      : str         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__							)






				def     a     (			self		:   Dict							)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__							)






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




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase__							)






				def     a     (			self		:   Dict							)     ->				List[str]:




										'''simple docstring'''



										if not self.model_tester.is_training:
																return

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

										for model_class in self.all_model_classes:
																# we don't test BeitForMaskedImageModeling
																if model_class in [*get_values(lowerCAmelCase__							), BeitForMaskedImageModeling]:
																						continue

																_UpperCamelCase      : str         =   model_class(lowerCAmelCase__							)
																model.to(lowerCAmelCase__							)
																model.train()
																_UpperCamelCase      : Optional[Any]         =   self._prepare_for_class(lowerCAmelCase__,			lowerCAmelCase__,			return_labels=lowerCAmelCase__							)
																_UpperCamelCase      : Optional[Any]         =   model(**lowerCAmelCase__							).loss
																loss.backward()






				def     a     (			self		:   Tuple							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : int         =   self.model_tester.prepare_config_and_inputs_for_common()
										if not self.model_tester.is_training:
																return

										_UpperCamelCase      : Optional[int]         =   False
										_UpperCamelCase      : Dict         =   True

										for model_class in self.all_model_classes:
																# we don't test BeitForMaskedImageModeling
																if (
																    model_class in [*get_values(lowerCAmelCase__							), BeitForMaskedImageModeling]
																    or not model_class.supports_gradient_checkpointing
																):
																						continue

																_UpperCamelCase      : Any         =   model_class(lowerCAmelCase__							)
																model.gradient_checkpointing_enable()
																model.to(lowerCAmelCase__							)
																model.train()
																_UpperCamelCase      : int         =   self._prepare_for_class(lowerCAmelCase__,			lowerCAmelCase__,			return_labels=lowerCAmelCase__							)
																_UpperCamelCase      : Optional[int]         =   model(**lowerCAmelCase__							).loss
																loss.backward()






				def     a     (			self		:   Optional[int]							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.model_tester.prepare_config_and_inputs_for_common()

										_UpperCamelCase      : Optional[int]         =   _config_zero_init(lowerCAmelCase__							)
										for model_class in self.all_model_classes:
																_UpperCamelCase      : int         =   model_class(config=lowerCAmelCase__							)
																for name, param in model.named_parameters():
																						# we skip lambda parameters as these require special initial values
																						# determined by config.layer_scale_init_value
																						if "lambda" in name:
																												continue
																						if param.requires_grad:
																												self.assertIn(
																												    ((param.data.mean() * 1e9).round() / 1e9).item(),			[0.0, 1.0],			msg=f"""Parameter {name} of model {model_class} seems not properly initialized""",			)






				@slow
				def     a     (			self		:   Any							)     ->				str:




										'''simple docstring'''



										for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase      : List[str]         =   BeitModel.from_pretrained(lowerCAmelCase__							)
																self.assertIsNotNone(lowerCAmelCase__							)





def 	a_    (							):
						_UpperCamelCase      : Any         =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
						return image






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






				@cached_property
				def     a     (			self		:   Optional[Any]							)     ->				int:




										'''simple docstring'''



										return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224'''							) if is_vision_available() else None






				@slow
				def     a     (			self		:   Tuple							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''							).to(lowerCAmelCase__							)

										_UpperCamelCase      : Tuple         =   self.default_image_processor
										_UpperCamelCase      : Tuple         =   prepare_img()
										_UpperCamelCase      : List[Any]         =   image_processor(images=lowerCAmelCase__,			return_tensors='''pt'''							).pixel_values.to(lowerCAmelCase__							)

										# prepare bool_masked_pos
										_UpperCamelCase      : List[str]         =   torch.ones((1, 1_9_6),			dtype=torch.bool							).to(lowerCAmelCase__							)

										# forward pass
										with torch.no_grad():
																_UpperCamelCase      : List[str]         =   model(pixel_values=lowerCAmelCase__,			bool_masked_pos=lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   outputs.logits

										# verify the logits
										_UpperCamelCase      : Tuple         =   torch.Size((1, 1_9_6, 8_1_9_2)							)
										self.assertEqual(logits.shape,			lowerCAmelCase__							)

										_UpperCamelCase      : List[Any]         =   torch.tensor(
										    [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]]							).to(lowerCAmelCase__							)

										self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3],			lowerCAmelCase__,			atol=1e-2							)							)






				@slow
				def     a     (			self		:   Optional[int]							)     ->				List[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Tuple         =   BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''							).to(lowerCAmelCase__							)

										_UpperCamelCase      : Tuple         =   self.default_image_processor
										_UpperCamelCase      : Optional[int]         =   prepare_img()
										_UpperCamelCase      : Optional[Any]         =   image_processor(images=lowerCAmelCase__,			return_tensors='''pt'''							).to(lowerCAmelCase__							)

										# forward pass
										with torch.no_grad():
																_UpperCamelCase      : Tuple         =   model(**lowerCAmelCase__							)
										_UpperCamelCase      : str         =   outputs.logits

										# verify the logits
										_UpperCamelCase      : Any         =   torch.Size((1, 1_0_0_0)							)
										self.assertEqual(logits.shape,			lowerCAmelCase__							)

										_UpperCamelCase      : int         =   torch.tensor([-1.2_385, -1.0_987, -1.0_108]							).to(lowerCAmelCase__							)

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

										_UpperCamelCase      : Dict         =   2_8_1
										self.assertEqual(logits.argmax(-1							).item(),			lowerCAmelCase__							)






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




										'''simple docstring'''



										_UpperCamelCase      : int         =   BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''							).to(
										    lowerCAmelCase__							)

										_UpperCamelCase      : int         =   self.default_image_processor
										_UpperCamelCase      : Optional[Any]         =   prepare_img()
										_UpperCamelCase      : Optional[Any]         =   image_processor(images=lowerCAmelCase__,			return_tensors='''pt'''							).to(lowerCAmelCase__							)

										# forward pass
										with torch.no_grad():
																_UpperCamelCase      : List[Any]         =   model(**lowerCAmelCase__							)
										_UpperCamelCase      : Optional[Any]         =   outputs.logits

										# verify the logits
										_UpperCamelCase      : str         =   torch.Size((1, 2_1_8_4_1)							)
										self.assertEqual(logits.shape,			lowerCAmelCase__							)

										_UpperCamelCase      : str         =   torch.tensor([1.6_881, -0.2_787, 0.5_901]							).to(lowerCAmelCase__							)

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

										_UpperCamelCase      : Optional[int]         =   2_3_9_6
										self.assertEqual(logits.argmax(-1							).item(),			lowerCAmelCase__							)






				@slow
				def     a     (			self		:   Tuple							)     ->				List[str]:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''							)
										_UpperCamelCase      : str         =   model.to(lowerCAmelCase__							)

										_UpperCamelCase      : Optional[int]         =   BeitImageProcessor(do_resize=lowerCAmelCase__,			size=6_4_0,			do_center_crop=lowerCAmelCase__							)

										_UpperCamelCase      : str         =   load_dataset('''hf-internal-testing/fixtures_ade20k''',			split='''test'''							)
										_UpperCamelCase      : Any         =   Image.open(ds[0]['''file''']							)
										_UpperCamelCase      : Optional[int]         =   image_processor(images=lowerCAmelCase__,			return_tensors='''pt'''							).to(lowerCAmelCase__							)

										# forward pass
										with torch.no_grad():
																_UpperCamelCase      : int         =   model(**lowerCAmelCase__							)
										_UpperCamelCase      : Union[str, Any]         =   outputs.logits

										# verify the logits
										_UpperCamelCase      : Optional[Any]         =   torch.Size((1, 1_5_0, 1_6_0, 1_6_0)							)
										self.assertEqual(logits.shape,			lowerCAmelCase__							)

										_UpperCamelCase      : Tuple         =   version.parse(PIL.__version__							) < version.parse('''9.0.0'''							)

										if is_pillow_less_than_a:
																_UpperCamelCase      : List[str]         =   torch.tensor(
																    [
																        [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]],
																        [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]],
																        [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]],
																    ],			device=lowerCAmelCase__,			)
										else:
																_UpperCamelCase      : List[Any]         =   torch.tensor(
																    [
																        [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]],
																        [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]],
																        [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]],
																    ],			device=lowerCAmelCase__,			)

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






				@slow
				def     a     (			self		:   Dict							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : str         =   BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''							)
										_UpperCamelCase      : Optional[Any]         =   model.to(lowerCAmelCase__							)

										_UpperCamelCase      : List[Any]         =   BeitImageProcessor(do_resize=lowerCAmelCase__,			size=6_4_0,			do_center_crop=lowerCAmelCase__							)

										_UpperCamelCase      : str         =   load_dataset('''hf-internal-testing/fixtures_ade20k''',			split='''test'''							)
										_UpperCamelCase      : List[str]         =   Image.open(ds[0]['''file''']							)
										_UpperCamelCase      : Optional[int]         =   image_processor(images=lowerCAmelCase__,			return_tensors='''pt'''							).to(lowerCAmelCase__							)

										# forward pass
										with torch.no_grad():
																_UpperCamelCase      : Tuple         =   model(**lowerCAmelCase__							)

										_UpperCamelCase      : Any         =   outputs.logits.detach().cpu()

										_UpperCamelCase      : int         =   image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase__,			target_sizes=[(5_0_0, 3_0_0)]							)
										_UpperCamelCase      : int         =   torch.Size((5_0_0, 3_0_0)							)
										self.assertEqual(segmentation[0].shape,			lowerCAmelCase__							)

										_UpperCamelCase      : Tuple         =   image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase__							)
										_UpperCamelCase      : Dict         =   torch.Size((1_6_0, 1_6_0)							)
										self.assertEqual(segmentation[0].shape,			lowerCAmelCase__							)










"""simple docstring"""




import numpy as np
import torch
from imwatermark import WatermarkEncoder


# Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66
UpperCamelCase_									=0b1_0_1_1_0_0_1_1_1_1_1_0_1_1_0_0_1_0_0_1_0_0_0_0_0_1_1_1_1_0_1_1_1_0_1_1_0_0_0_1_1_0_0_1_1_1_1_0
# bin(x)[2:] gives bits of x as str, use int to convert them to 0/1
UpperCamelCase_									=[int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]]




class 			_a						:






				def __init__(			self		:   str							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : str         =   WATERMARK_BITS
										_UpperCamelCase      : Optional[int]         =   WatermarkEncoder()

										self.encoder.set_watermark('''bits''',			self.watermark							)






				def     snake_case     (			self		:   Dict,			lowerCAmelCase__		:   torch.FloatTensor							)     ->				int:




										'''simple docstring'''



										if images.shape[-1] < 2_5_6:
																return images

										_UpperCamelCase      : Union[str, Any]         =   (2_5_5 * (images / 2 + 0.5)).cpu().permute(0,			2,			3,			1							).float().numpy()

										_UpperCamelCase      : List[str]         =   [self.encoder.encode(lowerCAmelCase__,			'''dwtDct'''							) for image in images]

										_UpperCamelCase      : Dict         =   torch.from_numpy(np.array(lowerCAmelCase__							)							).permute(0,			3,			1,			2							)

										_UpperCamelCase      : Optional[int]         =   torch.clamp(2 * (images / 2_5_5 - 0.5),			min=-1.0,			max=1.0							)
										return images



