

import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY





@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class 	snake_case_   (		unittest.TestCase					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   Dict									=				MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING







			def 				snake_case__(     self :				Any							,   _UpperCamelCase :				Dict							,   _UpperCamelCase :				Any							,   _UpperCamelCase :				List[str]							)      ->str:
									snake_case_   =	hf_hub_download(
									    repo_id='''nateraw/video-demo'''							,   filename='''archery.mp4'''							,   repo_type='''dataset'''							)
									snake_case_   =	VideoClassificationPipeline(model=_UpperCamelCase							,   image_processor=_UpperCamelCase							,   top_k=2							)
									snake_case_   =	[
									    example_video_filepath,
									    '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
									]
									return video_classifier, examples







			def 				snake_case__(     self :				int							,   _UpperCamelCase :				int							,   _UpperCamelCase :				Dict							)      ->Optional[int]:
									for example in examples:
															snake_case_   =	video_classifier(_UpperCamelCase							)

															self.assertEqual(
															    _UpperCamelCase							,   [
															        {'''score''': ANY(_UpperCamelCase							), '''label''': ANY(_UpperCamelCase							)},
															        {'''score''': ANY(_UpperCamelCase							), '''label''': ANY(_UpperCamelCase							)},
															    ]							,   )







			@require_torch
			def 				snake_case__(     self :				Dict							)      ->Any:
									snake_case_   =	'''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
									snake_case_   =	VideoMAEFeatureExtractor(
									    size={'''shortest_edge''': 1_0}							,   crop_size={'''height''': 1_0, '''width''': 1_0}							)
									snake_case_   =	pipeline(
									    '''video-classification'''							,   model=_UpperCamelCase							,   feature_extractor=_UpperCamelCase							,   frame_sampling_rate=4							)

									snake_case_   =	hf_hub_download(repo_id='''nateraw/video-demo'''							,   filename='''archery.mp4'''							,   repo_type='''dataset'''							)
									snake_case_   =	video_classifier(_UpperCamelCase							,   top_k=2							)
									self.assertEqual(
									    nested_simplify(_UpperCamelCase							,   decimals=4							)							,   [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}]							,   )

									snake_case_   =	video_classifier(
									    [
									        video_file_path,
									        video_file_path,
									    ]							,   top_k=2							,   )
									self.assertEqual(
									    nested_simplify(_UpperCamelCase							,   decimals=4							)							,   [
									        [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
									        [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
									    ]							,   )







			@require_tf
			def 				snake_case__(     self :				Optional[int]							)      ->Any:
									pass




import collections
import inspect
import unittest

from transformers import SwinvaConfig
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, _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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
 from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor




class 					snake_case__						:






     """simple docstring"""


     def __init__(       self  :  Tuple, _snake_case  :  Any, _snake_case  :  int=1_3, _snake_case  :  Optional[int]=3_2, _snake_case  :  Tuple=2, _snake_case  :  Any=3, _snake_case  :  Tuple=1_6, _snake_case  :  Tuple=[1, 2, 1], _snake_case  :  Dict=[2, 2, 4], _snake_case  :  str=2, _snake_case  :  Union[str, Any]=2.0, _snake_case  :  Dict=True, _snake_case  :  Dict=0.0, _snake_case  :  str=0.0, _snake_case  :  str=0.1, _snake_case  :  List[str]="gelu", _snake_case  :  int=False, _snake_case  :  Optional[Any]=True, _snake_case  :  List[Any]=0.0_2, _snake_case  :  Union[str, Any]=1e-5, _snake_case  :  Union[str, Any]=True, _snake_case  :  List[Any]=None, _snake_case  :  Any=True, _snake_case  :  List[Any]=1_0, _snake_case  :  str=8, )   ->Union[str, Any]:
            snake_case__							:	Any   	=	parent
            snake_case__							:	Tuple   	=	batch_size
            snake_case__							:	Tuple   	=	image_size
            snake_case__							:	Any   	=	patch_size
            snake_case__							:	Optional[int]   	=	num_channels
            snake_case__							:	Tuple   	=	embed_dim
            snake_case__							:	Any   	=	depths
            snake_case__							:	Any   	=	num_heads
            snake_case__							:	List[str]   	=	window_size
            snake_case__							:	Dict   	=	mlp_ratio
            snake_case__							:	Optional[int]   	=	qkv_bias
            snake_case__							:	Optional[Any]   	=	hidden_dropout_prob
            snake_case__							:	List[str]   	=	attention_probs_dropout_prob
            snake_case__							:	Union[str, Any]   	=	drop_path_rate
            snake_case__							:	str   	=	hidden_act
            snake_case__							:	Union[str, Any]   	=	use_absolute_embeddings
            snake_case__							:	Union[str, Any]   	=	patch_norm
            snake_case__							:	Any   	=	layer_norm_eps
            snake_case__							:	Tuple   	=	initializer_range
            snake_case__							:	Dict   	=	is_training
            snake_case__							:	Any   	=	scope
            snake_case__							:	Optional[Any]   	=	use_labels
            snake_case__							:	str   	=	type_sequence_label_size
            snake_case__							:	List[Any]   	=	encoder_stride






     def    lowercase_   (       self  :  Tuple					)   ->str:
            snake_case__							:	Tuple   	=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

            snake_case__							:	Any   	=	self.get_config()

            return config, pixel_values, labels






     def    lowercase_   (       self  :  Optional[int]					)   ->Optional[int]:
            return SwinvaConfig(
                image_size=self.image_size, patch_size=self.patch_size, num_channels=self.num_channels, embed_dim=self.embed_dim, depths=self.depths, num_heads=self.num_heads, window_size=self.window_size, mlp_ratio=self.mlp_ratio, qkv_bias=self.qkv_bias, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, drop_path_rate=self.drop_path_rate, hidden_act=self.hidden_act, use_absolute_embeddings=self.use_absolute_embeddings, path_norm=self.patch_norm, layer_norm_eps=self.layer_norm_eps, initializer_range=self.initializer_range, encoder_stride=self.encoder_stride, )






     def    lowercase_   (       self  :  Optional[int], _snake_case  :  str, _snake_case  :  List[str], _snake_case  :  int					)   ->Dict:
            snake_case__							:	List[Any]   	=	SwinvaModel(config=_snake_case					)
            model.to(_snake_case					)
            model.eval()
            snake_case__							:	Optional[int]   	=	model(_snake_case					)

            snake_case__							:	List[Any]   	=	((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths					) - 1))
            snake_case__							:	List[Any]   	=	int(config.embed_dim * 2 ** (len(config.depths					) - 1)					)

            self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, expected_seq_len, expected_dim)					)






     def    lowercase_   (       self  :  Optional[Any], _snake_case  :  Any, _snake_case  :  List[str], _snake_case  :  Dict					)   ->List[Any]:
            snake_case__							:	List[str]   	=	SwinvaForMaskedImageModeling(config=_snake_case					)
            model.to(_snake_case					)
            model.eval()
            snake_case__							:	Union[str, Any]   	=	model(_snake_case					)
            self.parent.assertEqual(
                result.logits.shape, (self.batch_size, self.num_channels, self.image_size, self.image_size)					)

            # test greyscale images
            snake_case__							:	Optional[Any]   	=	1
            snake_case__							:	Optional[int]   	=	SwinvaForMaskedImageModeling(_snake_case					)
            model.to(_snake_case					)
            model.eval()

            snake_case__							:	Tuple   	=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
            snake_case__							:	Any   	=	model(_snake_case					)
            self.parent.assertEqual(result.logits.shape, (self.batch_size, 1, self.image_size, self.image_size)					)






     def    lowercase_   (       self  :  List[str], _snake_case  :  int, _snake_case  :  List[Any], _snake_case  :  Optional[int]					)   ->Any:
            snake_case__							:	Tuple   	=	self.type_sequence_label_size
            snake_case__							:	int   	=	SwinvaForImageClassification(_snake_case					)
            model.to(_snake_case					)
            model.eval()
            snake_case__							:	Tuple   	=	model(_snake_case, labels=_snake_case					)
            self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size)					)






     def    lowercase_   (       self  :  Any					)   ->Dict:
            snake_case__							:	str   	=	self.prepare_config_and_inputs()
            snake_case__	,				snake_case__	,				snake_case__							:	List[str]   	=	config_and_inputs
            snake_case__							:	Union[str, Any]   	=	{'pixel_values': pixel_values}
            return config, inputs_dict




@require_torch
class 					snake_case__						(       lowerCAmelCase_				,     lowerCAmelCase_				,     unittest.TestCase     ):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE						=	(
         (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
     )
     _SCREAMING_SNAKE_CASE						=	(
         {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification}
         if is_torch_available()
         else {}
     )

     _SCREAMING_SNAKE_CASE						=	False
     _SCREAMING_SNAKE_CASE						=	False
     _SCREAMING_SNAKE_CASE						=	False
     _SCREAMING_SNAKE_CASE						=	False






     def    lowercase_   (       self  :  Union[str, Any]					)   ->Dict:
            snake_case__							:	Optional[int]   	=	SwinvaModelTester(self					)
            snake_case__							:	int   	=	ConfigTester(self, config_class=_snake_case, embed_dim=3_7					)






     def    lowercase_   (       self  :  Tuple					)   ->int:
            self.config_tester.create_and_test_config_to_json_string()
            self.config_tester.create_and_test_config_to_json_file()
            self.config_tester.create_and_test_config_from_and_save_pretrained()
            self.config_tester.create_and_test_config_with_num_labels()
            self.config_tester.check_config_can_be_init_without_params()
            self.config_tester.check_config_arguments_init()






     def    lowercase_   (       self  :  Any					)   ->str:
            snake_case__							:	int   	=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_snake_case					)






     @unittest.skip(reason='Got `CUDA error: misaligned address` with PyTorch 2.0.0.'					)
     def    lowercase_   (       self  :  Any					)   ->Union[str, Any]:
            pass






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






     def    lowercase_   (       self  :  Optional[Any]					)   ->Union[str, Any]:
            snake_case__	,				snake_case__							:	Optional[int]   	=	self.model_tester.prepare_config_and_inputs_for_common()

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






     def    lowercase_   (       self  :  List[str]					)   ->Optional[int]:
            snake_case__	,				snake_case__							:	Any   	=	self.model_tester.prepare_config_and_inputs_for_common()

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

                   snake_case__							:	List[Any]   	=	['pixel_values']
                   self.assertListEqual(arg_names[:1], _snake_case					)






     def    lowercase_   (       self  :  str					)   ->Union[str, Any]:
            snake_case__	,				snake_case__							:	Any   	=	self.model_tester.prepare_config_and_inputs_for_common()
            snake_case__							:	int   	=	True

            for model_class in self.all_model_classes:
                   snake_case__							:	str   	=	True
                   snake_case__							:	Union[str, Any]   	=	False
                   snake_case__							:	Tuple   	=	True
                   snake_case__							:	int   	=	model_class(_snake_case					)
                   model.to(_snake_case					)
                   model.eval()
                   with torch.no_grad():
                          snake_case__							:	Optional[int]   	=	model(**self._prepare_for_class(_snake_case, _snake_case					)					)
                   snake_case__							:	List[str]   	=	outputs.attentions
                   snake_case__							:	List[Any]   	=	len(self.model_tester.depths					)
                   self.assertEqual(len(_snake_case					), _snake_case					)

                   # check that output_attentions also work using config
                   del inputs_dict["output_attentions"]
                   snake_case__							:	str   	=	True
                   snake_case__							:	Tuple   	=	config.window_size**2
                   snake_case__							:	Optional[int]   	=	model_class(_snake_case					)
                   model.to(_snake_case					)
                   model.eval()
                   with torch.no_grad():
                          snake_case__							:	str   	=	model(**self._prepare_for_class(_snake_case, _snake_case					)					)
                   snake_case__							:	Tuple   	=	outputs.attentions
                   self.assertEqual(len(_snake_case					), _snake_case					)

                   self.assertListEqual(
                       list(attentions[0].shape[-3:]					), [self.model_tester.num_heads[0], window_size_squared, window_size_squared], )
                   snake_case__							:	Optional[Any]   	=	len(_snake_case					)

                   # Check attention is always last and order is fine
                   snake_case__							:	Optional[int]   	=	True
                   snake_case__							:	Dict   	=	True
                   snake_case__							:	List[Any]   	=	model_class(_snake_case					)
                   model.to(_snake_case					)
                   model.eval()
                   with torch.no_grad():
                          snake_case__							:	Optional[int]   	=	model(**self._prepare_for_class(_snake_case, _snake_case					)					)

                   if hasattr(self.model_tester, 'num_hidden_states_types'					):
                          snake_case__							:	str   	=	self.model_tester.num_hidden_states_types
                   else:
                          # also another +1 for reshaped_hidden_states
                          snake_case__							:	Dict   	=	2
                   self.assertEqual(out_len + added_hidden_states, len(_snake_case					)					)

                   snake_case__							:	Any   	=	outputs.attentions

                   self.assertEqual(len(_snake_case					), _snake_case					)

                   self.assertListEqual(
                       list(self_attentions[0].shape[-3:]					), [self.model_tester.num_heads[0], window_size_squared, window_size_squared], )






     def    lowercase_   (       self  :  Dict, _snake_case  :  Tuple, _snake_case  :  Any, _snake_case  :  int, _snake_case  :  Optional[int]					)   ->str:
            snake_case__							:	Dict   	=	model_class(_snake_case					)
            model.to(_snake_case					)
            model.eval()

            with torch.no_grad():
                   snake_case__							:	List[Any]   	=	model(**self._prepare_for_class(_snake_case, _snake_case					)					)

            snake_case__							:	Dict   	=	outputs.hidden_states

            snake_case__							:	int   	=	getattr(
                self.model_tester, 'expected_num_hidden_layers', len(self.model_tester.depths					) + 1					)
            self.assertEqual(len(_snake_case					), _snake_case					)

            # Swinv2 has a different seq_length
            snake_case__							:	int   	=	(
                config.patch_size
                if isinstance(config.patch_size, collections.abc.Iterable					)
                else (config.patch_size, config.patch_size)
            )

            snake_case__							:	Optional[Any]   	=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

            self.assertListEqual(
                list(hidden_states[0].shape[-2:]					), [num_patches, self.model_tester.embed_dim], )

            snake_case__							:	Union[str, Any]   	=	outputs.reshaped_hidden_states
            self.assertEqual(len(_snake_case					), _snake_case					)

            snake_case__	,				snake_case__	,				snake_case__	,				snake_case__							:	str   	=	reshaped_hidden_states[0].shape
            snake_case__							:	Any   	=	(
                reshaped_hidden_states[0].view(_snake_case, _snake_case, height * width					).permute(0, 2, 1					)
            )
            self.assertListEqual(
                list(reshaped_hidden_states.shape[-2:]					), [num_patches, self.model_tester.embed_dim], )






     def    lowercase_   (       self  :  str					)   ->List[Any]:
            snake_case__	,				snake_case__							:	Any   	=	self.model_tester.prepare_config_and_inputs_for_common()

            snake_case__							:	List[Any]   	=	(
                self.model_tester.image_size
                if isinstance(self.model_tester.image_size, collections.abc.Iterable					)
                else (self.model_tester.image_size, self.model_tester.image_size)
            )

            for model_class in self.all_model_classes:
                   snake_case__							:	Optional[int]   	=	True
                   self.check_hidden_states_output(_snake_case, _snake_case, _snake_case, _snake_case					)

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

                   self.check_hidden_states_output(_snake_case, _snake_case, _snake_case, _snake_case					)






     def    lowercase_   (       self  :  List[str]					)   ->str:
            snake_case__	,				snake_case__							:	Optional[Any]   	=	self.model_tester.prepare_config_and_inputs_for_common()
            snake_case__							:	List[str]   	=	3

            snake_case__							:	Union[str, Any]   	=	(
                self.model_tester.image_size
                if isinstance(self.model_tester.image_size, collections.abc.Iterable					)
                else (self.model_tester.image_size, self.model_tester.image_size)
            )
            snake_case__							:	str   	=	(
                config.patch_size
                if isinstance(config.patch_size, collections.abc.Iterable					)
                else (config.patch_size, config.patch_size)
            )

            snake_case__							:	Tuple   	=	image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
            snake_case__							:	Optional[Any]   	=	image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

            for model_class in self.all_model_classes:
                   snake_case__							:	int   	=	True
                   self.check_hidden_states_output(_snake_case, _snake_case, _snake_case, (padded_height, padded_width)					)

                   # check that output_hidden_states also work using config
                   del inputs_dict["output_hidden_states"]
                   snake_case__							:	List[str]   	=	True
                   self.check_hidden_states_output(_snake_case, _snake_case, _snake_case, (padded_height, padded_width)					)






     def    lowercase_   (       self  :  List[str]					)   ->Optional[int]:
            snake_case__							:	Any   	=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case					)






     def    lowercase_   (       self  :  List[Any]					)   ->str:
            snake_case__							:	Any   	=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*_snake_case					)






     @slow
     def    lowercase_   (       self  :  str					)   ->Union[str, Any]:
            for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   snake_case__							:	Dict   	=	SwinvaModel.from_pretrained(_snake_case					)
                   self.assertIsNotNone(_snake_case					)






     def    lowercase_   (       self  :  Optional[int]					)   ->List[str]:
            snake_case__	,				snake_case__							:	Any   	=	self.model_tester.prepare_config_and_inputs_for_common()

            snake_case__							:	List[Any]   	=	_config_zero_init(_snake_case					)
            for model_class in self.all_model_classes:
                   snake_case__							:	List[str]   	=	model_class(config=_snake_case					)
                   for name, param in model.named_parameters():
                          if "embeddings" not in name and "logit_scale" not in name and 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''', )




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






     """simple docstring"""


     @cached_property
     def    lowercase_   (       self  :  Union[str, Any]					)   ->List[str]:
            return (
                AutoImageProcessor.from_pretrained('microsoft/swinv2-tiny-patch4-window8-256'					)
                if is_vision_available()
                else None
            )






     @slow
     def    lowercase_   (       self  :  int					)   ->List[Any]:
            snake_case__							:	Any   	=	SwinvaForImageClassification.from_pretrained('microsoft/swinv2-tiny-patch4-window8-256'					).to(
                _snake_case					)
            snake_case__							:	int   	=	self.default_image_processor

            snake_case__							:	Union[str, Any]   	=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
            snake_case__							:	Optional[Any]   	=	image_processor(images=_snake_case, return_tensors='pt'					).to(_snake_case					)

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

            # verify the logits
            snake_case__							:	int   	=	torch.Size((1, 1_0_0_0)					)
            self.assertEqual(outputs.logits.shape, _snake_case					)
            snake_case__							:	Optional[int]   	=	torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6]					).to(_snake_case					)
            self.assertTrue(torch.allclose(outputs.logits[0, :3], _snake_case, atol=1e-4					)					)






