


"""simple docstring"""


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 snake_case					:







		def __init__(    self					:				List[Any]			,      a__					:				Optional[int]			,      a__					:				Optional[int]=2			,      a__					:				Optional[int]=True			,      a__					:				Union[str, Any]=True			,      a__					:				Union[str, Any]=10			,      a__					:				Union[str, Any]=10			,      a__					:				Any=8			,      a__					:				Dict=[2, 4, 4]			,      a__					:				List[str]=3			,      a__					:				List[str]=7_68			,      a__					:				int=5			,      a__					:				Any=4			,      a__					:				Optional[int]=37			,      a__					:				List[str]="gelu_fast"			,      a__					:				Optional[int]=0.0			,      a__					:				Union[str, Any]=0.0			,      a__					:				Union[str, Any]=0.0_2			,      a__					:				str=1E-0_6			,      a__					:				List[Any]=True			,      a__					:				Any=None			,      )				->  Any:





						'''simple docstring'''







						_A         =	parent
						_A         =	batch_size
						_A         =	is_training
						_A         =	use_labels
						_A         =	num_labels
						_A         =	image_size
						_A         =	num_frames
						_A         =	tubelet_size
						_A         =	num_channels
						_A         =	hidden_size
						_A         =	num_hidden_layers
						_A         =	num_attention_heads
						_A         =	intermediate_size
						_A         =	hidden_act
						_A         =	hidden_dropout_prob
						_A         =	attention_probs_dropout_prob
						_A         =	initializer_range
						_A         =	layer_norm_eps
						_A         =	qkv_bias
						_A         =	scope

						_A         =	(
						    (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        a_       (    self					:				List[str]						)				->  Optional[int]:





						'''simple docstring'''







						_A         =	floats_tensor(
						    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]						)

						_A         =	None
						if self.use_labels:
										_A         =	ids_tensor([self.batch_size]			,      self.num_labels						)

						_A         =	self.get_config()

						return config, pixel_values, labels







		def        a_       (    self					:				List[str]						)				->  Optional[Any]:





						'''simple docstring'''







						_A         =	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			,      )
						_A         =	self.num_labels
						return config







		def        a_       (    self					:				Dict			,      a__					:				Optional[int]			,      a__					:				Union[str, Any]			,      a__					:				int						)				->  int:





						'''simple docstring'''







						_A         =	VivitModel(config=a__						)
						model.to(a__						)
						model.eval()
						_A         =	model(a__						)
						self.parent.assertEqual(result.last_hidden_state.shape			,      (self.batch_size, self.seq_length, self.hidden_size)						)







		def        a_       (    self					:				Union[str, Any]			,      a__					:				Dict			,      a__					:				Tuple			,      a__					:				Union[str, Any]						)				->  Dict:





						'''simple docstring'''







						_A         =	VivitForVideoClassification(a__						)
						model.to(a__						)
						model.eval()

						_A         =	model(a__						)

						# verify the logits shape
						_A         =	torch.Size((self.batch_size, self.num_labels)						)
						self.parent.assertEqual(result.logits.shape			,      a__						)







		def        a_       (    self					:				List[Any]						)				->  Tuple:





						'''simple docstring'''







						_A         =	self.prepare_config_and_inputs()
						_A     ,						_A     ,						_A         =	config_and_inputs
						_A         =	{"pixel_values": pixel_values}
						return config, inputs_dict





@require_torch
class snake_case					(     _UpperCamelCase   , unittest.TestCase):
		__UpperCamelCase							=	(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

		__UpperCamelCase							=	False
		__UpperCamelCase							=	False
		__UpperCamelCase							=	False
		__UpperCamelCase							=	False







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





						'''simple docstring'''







						_A         =	VivitModelTester(self						)
						_A         =	ConfigTester(self			,      config_class=a__			,      has_text_modality=a__			,      hidden_size=37						)







		def        a_       (    self					:				Tuple			,      a__					:				Dict			,      a__					:				List[Any]			,      a__					:				Optional[Any]=False						)				->  List[Any]:





						'''simple docstring'''







						_A         =	copy.deepcopy(a__						)

						if return_labels:
										if model_class in get_values(a__						):
														_A         =	torch.zeros(
														    self.model_tester.batch_size			,      dtype=torch.long			,      device=a__						)

						return inputs_dict







		def        a_       (    self					:				Optional[int]						)				->  int:





						'''simple docstring'''







						self.config_tester.run_common_tests()







		@unittest.skip(reason="Vivit does not use inputs_embeds"						)
		def        a_       (    self					:				Optional[int]						)				->  int:





						'''simple docstring'''







						pass







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





						'''simple docstring'''







						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()

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







		def        a_       (    self					:				int						)				->  Dict:





						'''simple docstring'''







						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()

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

										_A         =	["pixel_values", "head_mask"]
										self.assertListEqual(arg_names[:2]			,      a__						)







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





						'''simple docstring'''







						_A         =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a__						)







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





						'''simple docstring'''







						_A         =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_video_classification(*a__						)







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





						'''simple docstring'''







						for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										_A         =	VivitModel.from_pretrained(a__						)
										self.assertIsNotNone(a__						)







		def        a_       (    self					:				Optional[Any]						)				->  List[Any]:





						'''simple docstring'''







						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()
						_A         =	True

						for model_class in self.all_model_classes:
										_A         =	self.model_tester.seq_length

										_A         =	True
										_A         =	False
										_A         =	True
										_A         =	model_class(a__						)
										model.to(a__						)
										model.eval()
										with torch.no_grad():
														_A         =	model(**self._prepare_for_class(a__			,      a__						)						)
										_A         =	outputs.attentions
										self.assertEqual(len(a__						)			,      self.model_tester.num_hidden_layers						)

										# check that output_attentions also work using config
										del inputs_dict["output_attentions"]
										_A         =	True
										_A         =	model_class(a__						)
										model.to(a__						)
										model.eval()
										with torch.no_grad():
														_A         =	model(**self._prepare_for_class(a__			,      a__						)						)
										_A         =	outputs.attentions
										self.assertEqual(len(a__						)			,      self.model_tester.num_hidden_layers						)

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

										# Check attention is always last and order is fine
										_A         =	True
										_A         =	True
										_A         =	model_class(a__						)
										model.to(a__						)
										model.eval()
										with torch.no_grad():
														_A         =	model(**self._prepare_for_class(a__			,      a__						)						)

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

										_A         =	outputs.attentions

										self.assertEqual(len(a__						)			,      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        a_       (    self					:				Optional[int]						)				->  Dict:





						'''simple docstring'''







						def check_hidden_states_output(a__					:				List[Any]			,      a__					:				Tuple			,      a__					:				List[Any]						):
										_A         =	model_class(a__						)
										model.to(a__						)
										model.eval()

										with torch.no_grad():
														_A         =	model(**self._prepare_for_class(a__			,      a__						)						)

										_A         =	outputs.hidden_states
										_A         =	self.model_tester.num_hidden_layers + 1
										self.assertEqual(len(a__						)			,      a__						)

										_A         =	self.model_tester.seq_length

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

						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										_A         =	True
										check_hidden_states_output(a__			,      a__			,      a__						)

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										_A         =	True

										check_hidden_states_output(a__			,      a__			,      a__						)







		@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
		def        a_       (    self					:				List[Any]						)				->  Optional[int]:





						'''simple docstring'''







						pass






def    a__       ( )		->		Any:
				_A         =	hf_hub_download(
				    repo_id="hf-internal-testing/spaghetti-video"   ,	filename="eating_spaghetti_32_frames.npy"   ,	repo_type="dataset"      )
				_A         =	np.load(__lowercase      )
				return list(__lowercase      )




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







		@cached_property
		def        a_       (    self					:				Dict						)				->  Tuple:





						'''simple docstring'''







						return VivitImageProcessor() if is_vision_available() else None







		@slow
		def        a_       (    self					:				int						)				->  str:





						'''simple docstring'''







						_A         =	VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"						).to(a__						)

						_A         =	self.default_image_processor
						_A         =	prepare_video()
						_A         =	image_processor(a__			,      return_tensors="pt"						).to(a__						)

						# forward pass
						with torch.no_grad():
										_A         =	model(**a__						)

						# verify the logits
						_A         =	torch.Size((1, 4_00)						)
						self.assertEqual(outputs.logits.shape			,      a__						)

						# taken from original model
						_A         =	torch.tensor([-1.0_5_4_3, 2.0_7_6_4, -0.2_1_0_4, 0.4_4_3_9, -0.9_6_5_8]						).to(a__						)

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







"""simple docstring"""


import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
   import sqlitea

   import sqlalchemy


_UpperCamelCase				:    Optional[Any]     =						datasets.utils.logging.get_logger(__name__)






@dataclass
class 						snake_case  (  datasets.BuilderConfig       ):
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     True
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     1_0000
    __magic_name__         =     None





    def 				lowerCamelCase__      (							self	:					Tuple			):


      '''simple docstring'''




      if self.sql is None:
        raise ValueError('sql must be specified'			)
      if self.con is None:
        raise ValueError('con must be specified'			)





    def 				lowerCamelCase__      (							self	:					int				,		A	:					dict				,		A	:					Optional[datasets.Features] = None				,		):


      '''simple docstring'''




      a     :			str				=   config_kwargs.copy()
      # We need to stringify the Selectable object to make its hash deterministic

      # The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
      a     :			List[str]				=   config_kwargs['sql']
      if not isinstance(A				,		A			):
        if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
          import sqlalchemy

          if isinstance(A				,		sqlalchemy.sql.Selectable			):
            a     :			Union[str, Any]				=   sqlalchemy.create_engine(config_kwargs['con'].split('://'			)[0] + '://'			)
            a     :			List[Any]				=   str(sql.compile(dialect=engine.dialect			)			)
            a     :			str				=   sql_str
          else:
            raise TypeError(
                F'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(A			)}: {sql}'''			)
        else:
          raise TypeError(
              F'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(A			)}: {sql}'''			)
      a     :			List[str]				=   config_kwargs['con']
      if not isinstance(A				,		A			):
        a     :			Optional[Any]				=   id(A			)
        logger.info(
            F'''SQL connection \'con\' of type {type(A			)} couldn\'t be hashed properly. To enable hashing, specify \'con\' as URI string instead.'''			)

      return super().create_config_id(A				,		custom_features=A			)





    @property
    def 				lowerCamelCase__      (							self	:					Optional[int]			):


      '''simple docstring'''




      a     :			int				=   {
          'index_col': self.index_col,
          'columns': self.columns,
          'params': self.params,
          'coerce_float': self.coerce_float,
          'parse_dates': self.parse_dates,
      }
      return pd_read_sql_kwargs






class 						snake_case  (  datasets.ArrowBasedBuilder       ):
    __magic_name__         =     SqlConfig





    def 				lowerCamelCase__      (							self	:					Any			):


      '''simple docstring'''




      return datasets.DatasetInfo(features=self.config.features			)





    def 				lowerCamelCase__      (							self	:					Dict				,		A	:					Optional[int]			):


      '''simple docstring'''




      return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,		gen_kwargs={}			)]





    def 				lowerCamelCase__      (							self	:					Union[str, Any]				,		A	:					pa.Table			):


      '''simple docstring'''




      if self.config.features is not None:
        a     :			List[Any]				=   self.config.features.arrow_schema
        if all(not require_storage_cast(A			) for feature in self.config.features.values()			):
          # cheaper cast
          a     :			Dict				=   pa.Table.from_arrays([pa_table[field.name] for field in schema]				,		schema=A			)
        else:
          # more expensive cast; allows str <-> int/float or str to Audio for example
          a     :			Union[str, Any]				=   table_cast(A				,		A			)
      return pa_table





    def 				lowerCamelCase__      (							self	:					Dict			):


      '''simple docstring'''




      a     :			List[Any]				=   self.config.chunksize
      a     :			Union[str, Any]				=   pd.read_sql(
          self.config.sql				,		self.config.con				,		chunksize=A				,		**self.config.pd_read_sql_kwargs			)
      a     :			Optional[Any]				=   [sql_reader] if chunksize is None else sql_reader
      for chunk_idx, df in enumerate(A			):
        a     :			List[Any]				=   pa.Table.from_pandas(A			)
        yield chunk_idx, self._cast_table(A			)




