




import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VivitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
							import torch
							from torch import nn

							from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VivitForVideoClassification, VivitModel
							from transformers.models.vivit.modeling_vivit import VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from transformers import VivitImageProcessor


class 				__SCREAMING_SNAKE_CASE							:

					def __init__(				self ,       __lowercase ,       __lowercase=2 ,       __lowercase=True ,       __lowercase=True ,       __lowercase=10 ,       __lowercase=10 ,       __lowercase=8 ,       __lowercase=[2, 4, 4] ,       __lowercase=3 ,       __lowercase=768 ,       __lowercase=5 ,       __lowercase=4 ,       __lowercase=37 ,       __lowercase="gelu_fast" ,       __lowercase=0.0 ,       __lowercase=0.0 ,       __lowercase=0.02 ,       __lowercase=1E-06 ,       __lowercase=True ,       __lowercase=None ,       ):







						'''simple docstring'''




						lowerCamelCase_							:		Tuple							   =							parent
						lowerCamelCase_							:		List[str]							   =							batch_size
						lowerCamelCase_							:		int							   =							is_training
						lowerCamelCase_							:		Any							   =							use_labels
						lowerCamelCase_							:		Union[str, Any]							   =							num_labels
						lowerCamelCase_							:		List[str]							   =							image_size
						lowerCamelCase_							:		int							   =							num_frames
						lowerCamelCase_							:		List[str]							   =							tubelet_size
						lowerCamelCase_							:		Tuple							   =							num_channels
						lowerCamelCase_							:		List[str]							   =							hidden_size
						lowerCamelCase_							:		Optional[Any]							   =							num_hidden_layers
						lowerCamelCase_							:		str							   =							num_attention_heads
						lowerCamelCase_							:		Union[str, Any]							   =							intermediate_size
						lowerCamelCase_							:		int							   =							hidden_act
						lowerCamelCase_							:		Union[str, Any]							   =							hidden_dropout_prob
						lowerCamelCase_							:		Optional[int]							   =							attention_probs_dropout_prob
						lowerCamelCase_							:		str							   =							initializer_range
						lowerCamelCase_							:		List[Any]							   =							layer_norm_eps
						lowerCamelCase_							:		Optional[int]							   =							qkv_bias
						lowerCamelCase_							:		str							   =							scope

						lowerCamelCase_							:		Any							   =							(
						    (self.image_size // self.tubelet_size[2])
						    * (self.image_size // self.tubelet_size[1])
						    * (self.num_frames // self.tubelet_size[0])
						) + 1  # CLS token

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		int							   =							floats_tensor(
						    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

						lowerCamelCase_							:		int							   =							None
						if self.use_labels:
							lowerCamelCase_							:		Union[str, Any]							   =							ids_tensor([self.batch_size] ,       self.num_labels					)

						lowerCamelCase_							:		Tuple							   =							self.get_config()

						return config, pixel_values, labels

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		str							   =							VivitConfig(
						    num_frames=self.num_frames ,       image_size=self.image_size ,       tubelet_size=self.tubelet_size ,       num_channels=self.num_channels ,       hidden_size=self.hidden_size ,       num_hidden_layers=self.num_hidden_layers ,       num_attention_heads=self.num_attention_heads ,       intermediate_size=self.intermediate_size ,       hidden_act=self.hidden_act ,       hidden_dropout_prob=self.hidden_dropout_prob ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob ,       initializer_range=self.initializer_range ,       layer_norm_eps=self.layer_norm_eps ,       qkv_bias=self.qkv_bias ,       )
						lowerCamelCase_							:		List[str]							   =							self.num_labels
						return config

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




						lowerCamelCase_							:		Any							   =							VivitModel(config=__lowercase					)
						model.to(__lowercase					)
						model.eval()
						lowerCamelCase_							:		str							   =							model(__lowercase					)
						self.parent.assertEqual(result.last_hidden_state.shape ,       (self.batch_size, self.seq_length, self.hidden_size)					)

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							   =							VivitForVideoClassification(__lowercase					)
						model.to(__lowercase					)
						model.eval()

						lowerCamelCase_							:		Optional[Any]							   =							model(__lowercase					)

						# verify the logits shape
						lowerCamelCase_							:		Optional[Any]							   =							torch.Size((self.batch_size, self.num_labels)					)
						self.parent.assertEqual(result.logits.shape ,       __lowercase					)





					def 	lowercase_							(				self					):







						'''simple docstring'''




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


@require_torch
class 				__SCREAMING_SNAKE_CASE							(		lowerCamelCase   ,     unittest.TestCase  ):
					_lowerCamelCase		  = (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

					_lowerCamelCase		  = False
					_lowerCamelCase		  = False
					_lowerCamelCase		  = False
					_lowerCamelCase		  = False

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		int							   =							VivitModelTester(self					)
						lowerCamelCase_							:		List[Any]							   =							ConfigTester(self ,       config_class=__lowercase ,       has_text_modality=__lowercase ,       hidden_size=37					)

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase=False					):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[int]							   =							copy.deepcopy(__lowercase					)

						if return_labels:
							if model_class in get_values(__lowercase					):
								lowerCamelCase_							:		Optional[int]							   =							torch.zeros(
								    self.model_tester.batch_size ,       dtype=torch.long ,       device=__lowercase					)

						return inputs_dict

					def 	lowercase_							(				self					):







						'''simple docstring'''




						self.config_tester.run_common_tests()

					@unittest.skip(reason='Vivit does not use inputs_embeds'					)
					def 	lowercase_							(				self					):







						'''simple docstring'''




						pass

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		List[Any]							   =							self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		List[str]							   =							model_class(__lowercase					)
							self.assertIsInstance(model.get_input_embeddings() ,       (nn.Module)					)
							lowerCamelCase_							:		List[str]							   =							model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(__lowercase ,       nn.Linear					)					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		Optional[int]							   =							self.model_tester.prepare_config_and_inputs_for_common()

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

							lowerCamelCase_							:		Union[str, Any]							   =							['pixel_values', 'head_mask']
							self.assertListEqual(arg_names[:2] ,       __lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Any							   =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							   =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_video_classification(*__lowercase					)

					@slow
					def 	lowercase_							(				self					):







						'''simple docstring'''




						for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:		Dict							   =							VivitModel.from_pretrained(__lowercase					)
							self.assertIsNotNone(__lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		str							   =							self.model_tester.prepare_config_and_inputs_for_common()
						lowerCamelCase_							:		Optional[int]							   =							True

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		List[Any]							   =							self.model_tester.seq_length

							lowerCamelCase_							:		Tuple							   =							True
							lowerCamelCase_							:		str							   =							False
							lowerCamelCase_							:		List[Any]							   =							True
							lowerCamelCase_							:		Optional[int]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()
							with torch.no_grad():
								lowerCamelCase_							:		Union[str, Any]							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)
							lowerCamelCase_							:		List[Any]							   =							outputs.attentions
							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							lowerCamelCase_							:		Optional[int]							   =							True
							lowerCamelCase_							:		List[str]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()
							with torch.no_grad():
								lowerCamelCase_							:		Any							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)
							lowerCamelCase_							:		Dict							   =							outputs.attentions
							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

							self.assertListEqual(
							    list(attentions[0].shape[-3:]					) ,       [self.model_tester.num_attention_heads, seq_len, seq_len] ,       )
							lowerCamelCase_							:		int							   =							len(__lowercase					)

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

							self.assertEqual(out_len + 1 ,       len(__lowercase					)					)

							lowerCamelCase_							:		List[Any]							   =							outputs.attentions

							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

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

					def 	lowercase_							(				self					):







						'''simple docstring'''




						def check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					):
							lowerCamelCase_							:		Optional[int]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()

							with torch.no_grad():
								lowerCamelCase_							:		Union[str, Any]							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)

							lowerCamelCase_							:		Union[str, Any]							   =							outputs.hidden_states
							lowerCamelCase_							:		List[Any]							   =							self.model_tester.num_hidden_layers + 1
							self.assertEqual(len(__lowercase					) ,       __lowercase					)

							lowerCamelCase_							:		Union[str, Any]							   =							self.model_tester.seq_length

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

						lowerCamelCase_   ,			lowerCamelCase_							:		Optional[int]							   =							self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		Optional[Any]							   =							True
							check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					)

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

							check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					)





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







						'''simple docstring'''




						pass






def 					snake_case					(							)							->							Optional[int]:




	'''simple docstring'''


	lowerCamelCase_							:		Any							   =							hf_hub_download(
	    repo_id='hf-internal-testing/spaghetti-video'						,   filename='eating_spaghetti_32_frames.npy'						,   repo_type='dataset'			)
	lowerCamelCase_							:		Union[str, Any]							   =							np.load(A			)
	return list(A			)



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

					@cached_property
					def 	lowercase_							(				self					):







						'''simple docstring'''




						return VivitImageProcessor() if is_vision_available() else None





					@slow
					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							   =							VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400'					).to(__lowercase					)

						lowerCamelCase_							:		Any							   =							self.default_image_processor
						lowerCamelCase_							:		Any							   =							prepare_video()
						lowerCamelCase_							:		List[str]							   =							image_processor(__lowercase ,       return_tensors='pt'					).to(__lowercase					)

						# forward pass
						with torch.no_grad():
							lowerCamelCase_							:		List[Any]							   =							model(**__lowercase					)

						# verify the logits
						lowerCamelCase_							:		Optional[Any]							   =							torch.Size((1, 400)					)
						self.assertEqual(outputs.logits.shape ,       __lowercase					)

						# taken from original model
						lowerCamelCase_							:		List[Any]							   =							torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]					).to(__lowercase					)

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






import argparse
import collections
import importlib.util
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_table.py
UpperCamelCase__              =						'src/diffusers'
UpperCamelCase__              =						'docs/source/en'
UpperCamelCase__              =						'.'






def 					snake_case					(							A						,   A						,   A			)							->							int:




	'''simple docstring'''


	with open(A						,   'r'						,   encoding='utf-8'						,   newline='\n'			) as f:
		lowerCamelCase_							:		int							   =							f.readlines()
	# Find the start prompt.
	lowerCamelCase_							:		Tuple							   =							0
	while not lines[start_index].startswith(A			):
		start_index += 1
	start_index += 1

	lowerCamelCase_							:		Tuple							   =							start_index
	while not lines[end_index].startswith(A			):
		end_index += 1
	end_index -= 1

	while len(lines[start_index]			) <= 1:
		start_index += 1
	while len(lines[end_index]			) <= 1:
		end_index -= 1
	end_index += 1
	return "".join(lines[start_index:end_index]			), start_index, end_index, lines


# Add here suffixes that are used to identify models, separated by |
UpperCamelCase__              =						'Model|Encoder|Decoder|ForConditionalGeneration'
# Regexes that match TF/Flax/PT model names.
UpperCamelCase__              =						re.compile(r'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
UpperCamelCase__              =						re.compile(r'Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
UpperCamelCase__              =						re.compile(r'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')


# This is to make sure the diffusers module imported is the one in the repo.
UpperCamelCase__              =						importlib.util.spec_from_file_location(
    'diffusers',
    os.path.join(TRANSFORMERS_PATH, '__init__.py'),
    submodule_search_locations=[TRANSFORMERS_PATH],
)
UpperCamelCase__              =						spec.loader.load_module()






def 					snake_case					(							A			)							->							str:




	'''simple docstring'''


	lowerCamelCase_							:		Dict							   =							re.finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)'						,   A			)
	return [m.group(0			) for m in matches]






def 					snake_case					(							A						,   A			)							->							List[Any]:




	'''simple docstring'''


	lowerCamelCase_							:		str							   =							2 if text == '✅' or text == '❌' else len(A			)
	lowerCamelCase_							:		List[str]							   =							(width - text_length) // 2
	lowerCamelCase_							:		int							   =							width - text_length - left_indent
	return " " * left_indent + text + " " * right_indent






def 					snake_case					(							)							->							Dict:




	'''simple docstring'''


	lowerCamelCase_							:		Union[str, Any]							   =							diffusers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
	lowerCamelCase_							:		Optional[Any]							   =							{
	    name: config_mapping_names[code]
	    for code, name in diffusers_module.MODEL_NAMES_MAPPING.items()
	    if code in config_mapping_names
	}
	lowerCamelCase_							:		Optional[int]							   =							{name: config.replace('ConfigMixin'						,   ''			) for name, config in model_name_to_config.items()}

	# Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax.
	lowerCamelCase_							:		Tuple							   =							collections.defaultdict(A			)
	lowerCamelCase_							:		Any							   =							collections.defaultdict(A			)
	lowerCamelCase_							:		int							   =							collections.defaultdict(A			)
	lowerCamelCase_							:		Optional[int]							   =							collections.defaultdict(A			)
	lowerCamelCase_							:		Dict							   =							collections.defaultdict(A			)

	# Let's lookup through all diffusers object (once).
	for attr_name in dir(A			):
		lowerCamelCase_							:		Tuple							   =							None
		if attr_name.endswith('Tokenizer'			):
			lowerCamelCase_							:		Union[str, Any]							   =							slow_tokenizers
			lowerCamelCase_							:		Optional[Any]							   =							attr_name[:-9]
		elif attr_name.endswith('TokenizerFast'			):
			lowerCamelCase_							:		Any							   =							fast_tokenizers
			lowerCamelCase_							:		Optional[Any]							   =							attr_name[:-13]
		elif _re_tf_models.match(A			) is not None:
			lowerCamelCase_							:		Tuple							   =							tf_models
			lowerCamelCase_							:		Optional[int]							   =							_re_tf_models.match(A			).groups()[0]
		elif _re_flax_models.match(A			) is not None:
			lowerCamelCase_							:		Union[str, Any]							   =							flax_models
			lowerCamelCase_							:		Union[str, Any]							   =							_re_flax_models.match(A			).groups()[0]
		elif _re_pt_models.match(A			) is not None:
			lowerCamelCase_							:		Union[str, Any]							   =							pt_models
			lowerCamelCase_							:		Optional[int]							   =							_re_pt_models.match(A			).groups()[0]

		if lookup_dict is not None:
			while len(A			) > 0:
				if attr_name in model_name_to_prefix.values():
					lowerCamelCase_							:		List[Any]							   =							True
					break
				# Try again after removing the last word in the name
				lowerCamelCase_							:		int							   =							''.join(camel_case_split(A			)[:-1]			)

    # Let's build that table!
	lowerCamelCase_							:		Optional[int]							   =							list(model_name_to_config.keys()			)
	model_names.sort(key=str.lower			)
	lowerCamelCase_							:		str							   =							['Model', 'Tokenizer slow', 'Tokenizer fast', 'PyTorch support', 'TensorFlow support', 'Flax Support']
	# We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side).
	lowerCamelCase_							:		str							   =							[len(A			) + 2 for c in columns]
	lowerCamelCase_							:		Optional[int]							   =							max([len(A			) for name in model_names]			) + 2

	# Build the table per se
	lowerCamelCase_							:		Optional[Any]							   =							'|' + '|'.join([_center_text(A						,   A			) for c, w in zip(A						,   A			)]			) + '|\n'
	# Use ":-----:" format to center-aligned table cell texts
	table += "|" + "|".join([':' + '-' * (w - 2) + ':' for w in widths]			) + "|\n"

	lowerCamelCase_							:		str							   =							{True: '✅', False: '❌'}
	for name in model_names:
		lowerCamelCase_							:		List[str]							   =							model_name_to_prefix[name]
		lowerCamelCase_							:		Tuple							   =							[
		    name,
		    check[slow_tokenizers[prefix]],
		    check[fast_tokenizers[prefix]],
		    check[pt_models[prefix]],
		    check[tf_models[prefix]],
		    check[flax_models[prefix]],
		]
		table += "|" + "|".join([_center_text(A						,   A			) for l, w in zip(A						,   A			)]			) + "|\n"
	return table






def 					snake_case					(							A=False			)							->							List[Any]:




	'''simple docstring'''


	lowerCamelCase_   ,			lowerCamelCase_   ,			lowerCamelCase_   ,			lowerCamelCase_							:		Tuple							   =							_find_text_in_file(
	    filename=os.path.join(A						,   'index.mdx'			)						,   start_prompt='<!--This table is updated automatically from the auto modules'						,   end_prompt='<!-- End table-->'						,   )
	lowerCamelCase_							:		Any							   =							get_model_table_from_auto_modules()

	if current_table != new_table:
		if overwrite:
			with open(os.path.join(A						,   'index.mdx'			)						,   'w'						,   encoding='utf-8'						,   newline='\n'			) as f:
				f.writelines(lines[:start_index] + [new_table] + lines[end_index:]			)
		else:
			raise ValueError(
			    'The model table in the `index.mdx` has not been updated. Run `make fix-copies` to fix this.'			)


if __name__ == "__main__":
							UpperCamelCase__              =						argparse.ArgumentParser()
							parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
							UpperCamelCase__              =						parser.parse_args()

							check_model_table(args.fix_and_overwrite)
