



from __future__ import annotations

import math

_lowercase     							=						'''2020.9.26'''
_lowercase     							=						'''xcodz-dot, cclaus, dhruvmanila'''






def      _A				(UpperCamelCase		:      float      ,			UpperCamelCase		:      float      ,			UpperCamelCase		:      float      ,			UpperCamelCase		:      float      ,			UpperCamelCase		:      float )  ->tuple[float, float]:



			'''simple docstring'''







			if not all(isinstance(UpperCamelCase      ,			(float, int) ) for val in locals().values() ):
						lowerCamelCase__ :      Tuple								=		f"Input values must either be float or int: {list(locals().values() )}"
						raise TypeError(UpperCamelCase )
			lowerCamelCase__ :      List[str]								=		((x * distance) / (z + distance)) * scale
			lowerCamelCase__ :      Optional[int]								=		((y * distance) / (z + distance)) * scale
			return projected_x, projected_y






def      _A				(UpperCamelCase		:      float      ,			UpperCamelCase		:      float      ,			UpperCamelCase		:      float      ,			UpperCamelCase		:      str      ,			UpperCamelCase		:      float )  ->tuple[float, float, float]:



			'''simple docstring'''







			if not isinstance(UpperCamelCase      ,			UpperCamelCase ):
						raise TypeError("""Axis must be a str""" )
			lowerCamelCase__ :      Dict								=		locals()
			del input_variables["axis"]
			if not all(isinstance(UpperCamelCase      ,			(float, int) ) for val in input_variables.values() ):
						lowerCamelCase__ :      Any								=		(
						    """Input values except axis must either be float or int: """
						    f"{list(input_variables.values() )}"
						)
						raise TypeError(UpperCamelCase )
			lowerCamelCase__ :      Optional[int]								=		(angle % 360) / 450 * 180 / math.pi
			if axis == "z":
						lowerCamelCase__ :      Any								=		x * math.cos(UpperCamelCase ) - y * math.sin(UpperCamelCase )
						lowerCamelCase__ :      Optional[int]								=		y * math.cos(UpperCamelCase ) + x * math.sin(UpperCamelCase )
						lowerCamelCase__ :      Any								=		z
			elif axis == "x":
						lowerCamelCase__ :      Union[str, Any]								=		y * math.cos(UpperCamelCase ) - z * math.sin(UpperCamelCase )
						lowerCamelCase__ :      int								=		z * math.cos(UpperCamelCase ) + y * math.sin(UpperCamelCase )
						lowerCamelCase__ :      int								=		x
			elif axis == "y":
						lowerCamelCase__ :      Optional[Any]								=		x * math.cos(UpperCamelCase ) - z * math.sin(UpperCamelCase )
						lowerCamelCase__ :      str								=		z * math.cos(UpperCamelCase ) + x * math.sin(UpperCamelCase )
						lowerCamelCase__ :      Any								=		y
			else:
						raise ValueError("""not a valid axis, choose one of 'x', 'y', 'z'""" )

			return new_x, new_y, new_z


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				print(F'''{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }''')
				print(F'''{rotate(1.0, 2.0, 3.0, "y", 90.0) = }''')








import inspect
import unittest

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

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


if is_torch_available():
				import torch

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


if is_vision_available():
				from PIL import Image

				from transformers import MobileViTImageProcessor
class 				__A					( A_      ):





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		self.config_class(**self.inputs_dict				)
						self.parent.assertTrue(hasattr(__magic_name__							,    """width_multiplier"""				)				)







class 				__A					:





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





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

						lowerCamelCase__ :      int								=		self.get_config()

						return config, pixel_values, labels, pixel_labels





			def        _snake_case    (self				):
						return MobileViTVaConfig(
						    image_size=self.image_size							,    patch_size=self.patch_size							,    num_channels=self.num_channels							,    hidden_act=self.hidden_act							,    conv_kernel_size=self.conv_kernel_size							,    output_stride=self.output_stride							,    classifier_dropout_prob=self.classifier_dropout_prob							,    initializer_range=self.initializer_range							,    width_multiplier=self.width_multiplier							,    ffn_dropout=self.ffn_dropout_prob							,    attn_dropout=self.attn_dropout_prob							,    )





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Any								=		MobileViTVaModel(config=__magic_name__				)
						model.to(__magic_name__				)
						model.eval()
						lowerCamelCase__ :      Optional[Any]								=		model(__magic_name__				)
						self.parent.assertEqual(
						    result.last_hidden_state.shape							,    (
						        self.batch_size,
						        self.last_hidden_size,
						        self.image_size // self.output_stride,
						        self.image_size // self.output_stride,
						    )							,    )





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      List[str]								=		self.num_labels
						lowerCamelCase__ :      Optional[int]								=		MobileViTVaForImageClassification(__magic_name__				)
						model.to(__magic_name__				)
						model.eval()
						lowerCamelCase__ :      Dict								=		model(__magic_name__							,    labels=__magic_name__				)
						self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.num_labels)				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Dict								=		self.num_labels
						lowerCamelCase__ :      Any								=		MobileViTVaForSemanticSegmentation(__magic_name__				)
						model.to(__magic_name__				)
						model.eval()
						lowerCamelCase__ :      Optional[int]								=		model(__magic_name__				)
						self.parent.assertEqual(
						    result.logits.shape							,    (
						        self.batch_size,
						        self.num_labels,
						        self.image_size // self.output_stride,
						        self.image_size // self.output_stride,
						    )							,    )
						lowerCamelCase__ :      Dict								=		model(__magic_name__							,    labels=__magic_name__				)
						self.parent.assertEqual(
						    result.logits.shape							,    (
						        self.batch_size,
						        self.num_labels,
						        self.image_size // self.output_stride,
						        self.image_size // self.output_stride,
						    )							,    )





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







@require_torch
class 				__A					( A_					, A_					, unittest.TestCase      ):
			UpperCamelCase					:Optional[Any]     =			(
			    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)

			UpperCamelCase					:List[Any]     =			(
			    {
			        '''feature-extraction''': MobileViTVaModel,
			        '''image-classification''': MobileViTVaForImageClassification,
			        '''image-segmentation''': MobileViTVaForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase					:List[Any]     =			False
			UpperCamelCase					:int     =			False
			UpperCamelCase					:Optional[int]     =			False
			UpperCamelCase					:int     =			False





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		MobileViTVaModelTester(self				)
						lowerCamelCase__ :      Optional[int]								=		MobileViTVaConfigTester(self							,    config_class=__magic_name__							,    has_text_modality=__magic_name__				)





			def        _snake_case    (self				):
						self.config_tester.run_common_tests()





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





			@unittest.skip(reason="""MobileViTV2 does not support input and output embeddings"""				)
			def        _snake_case    (self				):
						pass





			@unittest.skip(reason="""MobileViTV2 does not output attentions"""				)
			def        _snake_case    (self				):
						pass





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





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





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__ :      Union[str, Any]								=		self.model_tester.prepare_config_and_inputs_for_common()

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

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





			def        _snake_case    (self				):
						lowerCamelCase__ :      int								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__magic_name__				)





			def        _snake_case    (self				):
						def check_hidden_states_output(__magic_name__							,    __magic_name__							,    __magic_name__				):
									lowerCamelCase__ :      Dict								=		model_class(__magic_name__				)
									model.to(__magic_name__				)
									model.eval()

									with torch.no_grad():
												lowerCamelCase__ :      Optional[int]								=		model(**self._prepare_for_class(__magic_name__							,    __magic_name__				)				)

									lowerCamelCase__ :      str								=		outputs.hidden_states

									lowerCamelCase__ :      Dict								=		5
									self.assertEqual(len(__magic_name__				)							,    __magic_name__				)

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

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

						lowerCamelCase__							,lowerCamelCase__ :      Optional[Any]								=		self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									lowerCamelCase__ :      Any								=		True
									check_hidden_states_output(__magic_name__							,    __magic_name__							,    __magic_name__				)

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

									check_hidden_states_output(__magic_name__							,    __magic_name__							,    __magic_name__				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__magic_name__				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[Any]								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*__magic_name__				)





			@slow
			def        _snake_case    (self				):
						for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__ :      int								=		MobileViTVaModel.from_pretrained(__magic_name__				)
									self.assertIsNotNone(__magic_name__				)







def      _A				()  ->List[Any]:



			'''simple docstring'''







			lowerCamelCase__ :      Tuple								=		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				):
						return (
						    MobileViTImageProcessor.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""				)
						    if is_vision_available()
						    else None
						)





			@slow
			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		MobileViTVaForImageClassification.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""				).to(
						    __magic_name__				)

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

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

						# verify the logits
						lowerCamelCase__ :      List[Any]								=		torch.Size((1, 1000)				)
						self.assertEqual(outputs.logits.shape							,    __magic_name__				)

						lowerCamelCase__ :      Optional[Any]								=		torch.tensor([-1.63_36E00, -7.32_04E-02, -5.18_83E-01]				).to(__magic_name__				)

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





			@slow
			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""				)
						lowerCamelCase__ :      Dict								=		model.to(__magic_name__				)

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

						lowerCamelCase__ :      str								=		prepare_img()
						lowerCamelCase__ :      str								=		image_processor(images=__magic_name__							,    return_tensors="""pt"""				).to(__magic_name__				)

						# forward pass
						with torch.no_grad():
									lowerCamelCase__ :      Dict								=		model(**__magic_name__				)
						lowerCamelCase__ :      List[str]								=		outputs.logits

						# verify the logits
						lowerCamelCase__ :      Tuple								=		torch.Size((1, 21, 32, 32)				)
						self.assertEqual(logits.shape							,    __magic_name__				)

						lowerCamelCase__ :      Any								=		torch.tensor(
						    [
						        [[7.08_63, 7.15_25, 6.82_01], [6.69_31, 6.87_70, 6.89_33], [6.29_78, 7.03_66, 6.96_36]],
						        [[-3.71_34, -3.67_12, -3.66_75], [-3.58_25, -3.35_49, -3.47_77], [-3.34_35, -3.39_79, -3.28_57]],
						        [[-2.93_29, -2.80_03, -2.73_69], [-3.05_64, -2.47_80, -2.02_07], [-2.68_89, -1.92_98, -1.76_40]],
						    ]							,    device=__magic_name__							,    )

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





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

						lowerCamelCase__ :      Optional[int]								=		MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""				)

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

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

						lowerCamelCase__ :      str								=		outputs.logits.detach().cpu()

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

						lowerCamelCase__ :      List[str]								=		image_processor.post_process_semantic_segmentation(outputs=__magic_name__				)
						lowerCamelCase__ :      int								=		torch.Size((32, 32)				)
						self.assertEqual(segmentation[0].shape							,    __magic_name__				)



