






"""simple docstring"""

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_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 torch import nn

			from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
			from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import ViTImageProcessor

class 						A_							:







				def __init__(							self							:						Optional[int]						,    __lowerCamelCase							:						str						,    __lowerCamelCase							:						str=1_3						,    __lowerCamelCase							:						Any=3_0						,    __lowerCamelCase							:						Any=2						,    __lowerCamelCase							:						List[Any]=3						,    __lowerCamelCase							:						int=True						,    __lowerCamelCase							:						int=True						,    __lowerCamelCase							:						List[str]=3_2						,    __lowerCamelCase							:						List[Any]=5						,    __lowerCamelCase							:						List[str]=4						,    __lowerCamelCase							:						Optional[Any]=3_7						,    __lowerCamelCase							:						Union[str, Any]="gelu"						,    __lowerCamelCase							:						int=0.1						,    __lowerCamelCase							:						Optional[int]=0.1						,    __lowerCamelCase							:						Dict=1_0						,    __lowerCamelCase							:						List[str]=0.02						,    __lowerCamelCase							:						Tuple=None						,    __lowerCamelCase							:						Dict=2						,    )				->					Any:
										__magic_name__            =     parent
										__magic_name__            =     batch_size
										__magic_name__            =     image_size
										__magic_name__            =     patch_size
										__magic_name__            =     num_channels
										__magic_name__            =     is_training
										__magic_name__            =     use_labels
										__magic_name__            =     hidden_size
										__magic_name__            =     num_hidden_layers
										__magic_name__            =     num_attention_heads
										__magic_name__            =     intermediate_size
										__magic_name__            =     hidden_act
										__magic_name__            =     hidden_dropout_prob
										__magic_name__            =     attention_probs_dropout_prob
										__magic_name__            =     type_sequence_label_size
										__magic_name__            =     initializer_range
										__magic_name__            =     scope
										__magic_name__            =     encoder_stride

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







				def     _snake_case					(							self							:						Dict	)				->					Tuple:
										__magic_name__            =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

										__magic_name__            =     None
										if self.use_labels:
																__magic_name__            =     ids_tensor([self.batch_size]						,    self.type_sequence_label_size	)

										__magic_name__            =     self.get_config()

										return config, pixel_values, labels







				def     _snake_case					(							self							:						int	)				->					Any:
										return ViTConfig(
										    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						,    encoder_stride=self.encoder_stride						,    )







				def     _snake_case					(							self							:						int						,    __lowerCamelCase							:						Union[str, Any]						,    __lowerCamelCase							:						Union[str, Any]						,    __lowerCamelCase							:						str	)				->					List[str]:
										__magic_name__            =     ViTModel(config=__lowerCamelCase	)
										model.to(__lowerCamelCase	)
										model.eval()
										__magic_name__            =     model(__lowerCamelCase	)
										self.parent.assertEqual(result.last_hidden_state.shape						,    (self.batch_size, self.seq_length, self.hidden_size)	)







				def     _snake_case					(							self							:						Dict						,    __lowerCamelCase							:						List[str]						,    __lowerCamelCase							:						str						,    __lowerCamelCase							:						List[str]	)				->					Any:
										__magic_name__            =     ViTForMaskedImageModeling(config=__lowerCamelCase	)
										model.to(__lowerCamelCase	)
										model.eval()
										__magic_name__            =     model(__lowerCamelCase	)
										self.parent.assertEqual(
										    result.reconstruction.shape						,    (self.batch_size, self.num_channels, self.image_size, self.image_size)	)

										# test greyscale images
										__magic_name__            =     1
										__magic_name__            =     ViTForMaskedImageModeling(__lowerCamelCase	)
										model.to(__lowerCamelCase	)
										model.eval()

										__magic_name__            =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
										__magic_name__            =     model(__lowerCamelCase	)
										self.parent.assertEqual(result.reconstruction.shape						,    (self.batch_size, 1, self.image_size, self.image_size)	)







				def     _snake_case					(							self							:						Any						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						Union[str, Any]	)				->					Tuple:
										__magic_name__            =     self.type_sequence_label_size
										__magic_name__            =     ViTForImageClassification(__lowerCamelCase	)
										model.to(__lowerCamelCase	)
										model.eval()
										__magic_name__            =     model(__lowerCamelCase						,    labels=__lowerCamelCase	)
										self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.type_sequence_label_size)	)

										# test greyscale images
										__magic_name__            =     1
										__magic_name__            =     ViTForImageClassification(__lowerCamelCase	)
										model.to(__lowerCamelCase	)
										model.eval()

										__magic_name__            =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
										__magic_name__            =     model(__lowerCamelCase	)
										self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.type_sequence_label_size)	)







				def     _snake_case					(							self							:						List[str]	)				->					str:
										__magic_name__            =     self.prepare_config_and_inputs()
										(
										    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    (
										    __magic_name__
										)      ,    
										)            =     config_and_inputs
										__magic_name__            =     {"pixel_values": pixel_values}
										return config, inputs_dict





@require_torch
class 						A_							(      snake_case_					,							snake_case_					,							unittest.TestCase  ):
				UpperCAmelCase__        =				(
				    (
				        ViTModel,
				        ViTForImageClassification,
				        ViTForMaskedImageModeling,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCAmelCase__        =				(
				    {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
				    if is_torch_available()
				    else {}
				)
				UpperCAmelCase__        =				True

				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False







				def     _snake_case					(							self							:						Optional[int]	)				->					str:
										__magic_name__            =     ViTModelTester(self	)
										__magic_name__            =     ConfigTester(self						,    config_class=__lowerCamelCase						,    has_text_modality=__lowerCamelCase						,    hidden_size=3_7	)







				def     _snake_case					(							self							:						Any	)				->					int:
										self.config_tester.run_common_tests()







				@unittest.skip(reason="ViT does not use inputs_embeds"	)
				def     _snake_case					(							self							:						str	)				->					Dict:
										pass







				def     _snake_case					(							self							:						Optional[int]	)				->					Optional[Any]:
										__magic_name__      ,    __magic_name__            =     self.model_tester.prepare_config_and_inputs_for_common()

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







				def     _snake_case					(							self							:						Union[str, Any]	)				->					Optional[Any]:
										__magic_name__      ,    __magic_name__            =     self.model_tester.prepare_config_and_inputs_for_common()

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

																__magic_name__            =     ["pixel_values"]
																self.assertListEqual(arg_names[:1]						,    __lowerCamelCase	)







				def     _snake_case					(							self							:						Optional[int]	)				->					int:
										__magic_name__            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowerCamelCase	)







				def     _snake_case					(							self							:						Any	)				->					Optional[int]:
										__magic_name__            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase	)







				def     _snake_case					(							self							:						int	)				->					Optional[int]:
										__magic_name__            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase	)







				@slow
				def     _snake_case					(							self							:						int	)				->					str:
										for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__magic_name__            =     ViTModel.from_pretrained(__lowerCamelCase	)
																self.assertIsNotNone(__lowerCamelCase	)






def _lowerCAmelCase	(  ):

						'''simple docstring'''






						__magic_name__            =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
						return image


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







				@cached_property
				def     _snake_case					(							self							:						Dict	)				->					Union[str, Any]:
										return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"	) if is_vision_available() else None







				@slow
				def     _snake_case					(							self							:						Optional[int]	)				->					List[str]:
										__magic_name__            =     ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"	).to(__lowerCamelCase	)

										__magic_name__            =     self.default_image_processor
										__magic_name__            =     prepare_img()
										__magic_name__            =     image_processor(images=__lowerCamelCase						,    return_tensors="pt"	).to(__lowerCamelCase	)

										# forward pass
										with torch.no_grad():
																__magic_name__            =     model(**__lowerCamelCase	)

										# verify the logits
										__magic_name__            =     torch.Size((1, 1_0_0_0)	)
										self.assertEqual(outputs.logits.shape						,    __lowerCamelCase	)

										__magic_name__            =     torch.tensor([-0.2744, 0.8215, -0.0836]	).to(__lowerCamelCase	)

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







				@slow
				def     _snake_case					(							self							:						int	)				->					Any:
										# ViT models have an `interpolate_pos_encoding` argument in their forward method,
										# allowing to interpolate the pre-trained position embeddings in order to use
										# the model on higher resolutions. The DINO model by Facebook AI leverages this
										# to visualize self-attention on higher resolution images.
										__magic_name__            =     ViTModel.from_pretrained("facebook/dino-vits8"	).to(__lowerCamelCase	)

										__magic_name__            =     ViTImageProcessor.from_pretrained("facebook/dino-vits8"						,    size=4_8_0	)
										__magic_name__            =     prepare_img()
										__magic_name__            =     image_processor(images=__lowerCamelCase						,    return_tensors="pt"	)
										__magic_name__            =     inputs.pixel_values.to(__lowerCamelCase	)

										# forward pass
										with torch.no_grad():
																__magic_name__            =     model(__lowerCamelCase						,    interpolate_pos_encoding=__lowerCamelCase	)

										# verify the logits
										__magic_name__            =     torch.Size((1, 3_6_0_1, 3_8_4)	)
										self.assertEqual(outputs.last_hidden_state.shape						,    __lowerCamelCase	)

										__magic_name__            =     torch.tensor(
										    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]	).to(__lowerCamelCase	)

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







				@slow
				@require_accelerate
				@require_torch_gpu
				def     _snake_case					(							self							:						Any	)				->					List[Any]:
										__magic_name__            =     ViTModel.from_pretrained("facebook/dino-vits8"						,    torch_dtype=torch.floataa						,    device_map="auto"	)
										__magic_name__            =     self.default_image_processor

										__magic_name__            =     prepare_img()
										__magic_name__            =     image_processor(images=__lowerCamelCase						,    return_tensors="pt"	)
										__magic_name__            =     inputs.pixel_values.to(__lowerCamelCase	)

										# forward pass to make sure inference works in fp16
										with torch.no_grad():
																__magic_name__            =     model(__lowerCamelCase	)











"""simple docstring"""

from copy import deepcopy

class 						A_							:







				def __init__(							self							:						List[str]						,    __lowerCamelCase							:						list[int] | None = None						,    __lowerCamelCase							:						int | None = None	)				->					None:

										if arr is None and size is not None:
																__magic_name__            =     size
																__magic_name__            =     [0] * size
										elif arr is not None:
																self.init(__lowerCamelCase	)
										else:
																raise ValueError("Either arr or size must be specified"	)







				def     _snake_case					(							self							:						Optional[int]						,    __lowerCamelCase							:						list[int]	)				->					None:
										__magic_name__            =     len(__lowerCamelCase	)
										__magic_name__            =     deepcopy(__lowerCamelCase	)
										for i in range(1						,    self.size	):
																__magic_name__            =     self.next_(__lowerCamelCase	)
																if j < self.size:
																						self.tree[j] += self.tree[i]







				def     _snake_case					(							self							:						Any	)				->					list[int]:
										__magic_name__            =     self.tree[:]
										for i in range(self.size - 1						,    0						,    -1	):
																__magic_name__            =     self.next_(__lowerCamelCase	)
																if j < self.size:
																						arr[j] -= arr[i]
										return arr







				@staticmethod
				def     _snake_case					(							__lowerCamelCase							:						int	)				->					int:
										return index + (index & (-index))







				@staticmethod
				def     _snake_case					(							__lowerCamelCase							:						int	)				->					int:
										return index - (index & (-index))







				def     _snake_case					(							self							:						Union[str, Any]						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						int	)				->					None:
										if index == 0:
																self.tree[0] += value
																return
										while index < self.size:
																self.tree[index] += value
																__magic_name__            =     self.next_(__lowerCamelCase	)







				def     _snake_case					(							self							:						Any						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						int	)				->					None:
										self.add(__lowerCamelCase						,    value - self.get(__lowerCamelCase	)	)







				def     _snake_case					(							self							:						List[Any]						,    __lowerCamelCase							:						int	)				->					int:
										if right == 0:
																return 0
										__magic_name__            =     self.tree[0]
										right -= 1  # make right inclusive
										while right > 0:
																result += self.tree[right]
																__magic_name__            =     self.prev(__lowerCamelCase	)
										return result







				def     _snake_case					(							self							:						Any						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						int	)				->					int:
										return self.prefix(__lowerCamelCase	) - self.prefix(__lowerCamelCase	)







				def     _snake_case					(							self							:						Tuple						,    __lowerCamelCase							:						int	)				->					int:
										return self.query(__lowerCamelCase						,    index + 1	)







				def     _snake_case					(							self							:						Tuple						,    __lowerCamelCase							:						int	)				->					int:
										value -= self.tree[0]
										if value < 0:
																return -1

										__magic_name__            =     1  # Largest power of 2 <= size
										while j * 2 < self.size:
																j *= 2

										__magic_name__            =     0

										while j > 0:
																if i + j < self.size and self.tree[i + j] <= value:
																						value -= self.tree[i + j]
																						i += j
																j //= 2
										return i


if __name__ == "__main__":
			import doctest

			doctest.testmod()



