

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


  """simple docstring"""



  def __init__(						self     :					Dict  ,	A_     :					Tuple  ,	A_     :					str=2  ,	A_     :					Any=True  ,	A_     :					List[str]=True  ,	A_     :					int=10  ,	A_     :					Optional[Any]=10  ,	A_     :					List[Any]=8  ,	A_     :					List[str]=[2, 4, 4]  ,	A_     :					int=3  ,	A_     :					Tuple=768  ,	A_     :					Tuple=5  ,	A_     :					List[Any]=4  ,	A_     :					Union[str, Any]=37  ,	A_     :					int="gelu_fast"  ,	A_     :					str=0.0  ,	A_     :					List[str]=0.0  ,	A_     :					Any=0.02  ,	A_     :					Tuple=1E-0_6  ,	A_     :					Optional[int]=True  ,	A_     :					Dict=None  ,	)       ->			Union[str, Any]:
      SCREAMING_SNAKE_CASE__         =    parent
      SCREAMING_SNAKE_CASE__         =    batch_size
      SCREAMING_SNAKE_CASE__         =    is_training
      SCREAMING_SNAKE_CASE__         =    use_labels
      SCREAMING_SNAKE_CASE__         =    num_labels
      SCREAMING_SNAKE_CASE__         =    image_size
      SCREAMING_SNAKE_CASE__         =    num_frames
      SCREAMING_SNAKE_CASE__         =    tubelet_size
      SCREAMING_SNAKE_CASE__         =    num_channels
      SCREAMING_SNAKE_CASE__         =    hidden_size
      SCREAMING_SNAKE_CASE__         =    num_hidden_layers
      SCREAMING_SNAKE_CASE__         =    num_attention_heads
      SCREAMING_SNAKE_CASE__         =    intermediate_size
      SCREAMING_SNAKE_CASE__         =    hidden_act
      SCREAMING_SNAKE_CASE__         =    hidden_dropout_prob
      SCREAMING_SNAKE_CASE__         =    attention_probs_dropout_prob
      SCREAMING_SNAKE_CASE__         =    initializer_range
      SCREAMING_SNAKE_CASE__         =    layer_norm_eps
      SCREAMING_SNAKE_CASE__         =    qkv_bias
      SCREAMING_SNAKE_CASE__         =    scope

      SCREAMING_SNAKE_CASE__         =    (
          (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   _UpperCamelCase     (						self     :					List[Any]						)       ->			Optional[Any]:
      SCREAMING_SNAKE_CASE__         =    floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]						)

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

      SCREAMING_SNAKE_CASE__         =    self.get_config()

      return config, pixel_values, labels



  def   _UpperCamelCase     (						self     :					int						)       ->			str:
      SCREAMING_SNAKE_CASE__         =    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  ,	)
      SCREAMING_SNAKE_CASE__         =    self.num_labels
      return config



  def   _UpperCamelCase     (						self     :					Optional[int]  ,	A_     :					Optional[Any]  ,	A_     :					List[str]  ,	A_     :					Any						)       ->			str:
      SCREAMING_SNAKE_CASE__         =    VivitModel(config=A_						)
      model.to(A_						)
      model.eval()
      SCREAMING_SNAKE_CASE__         =    model(A_						)
      self.parent.assertEqual(result.last_hidden_state.shape  ,	(self.batch_size, self.seq_length, self.hidden_size)						)



  def   _UpperCamelCase     (						self     :					int  ,	A_     :					Dict  ,	A_     :					int  ,	A_     :					Union[str, Any]						)       ->			Union[str, Any]:
      SCREAMING_SNAKE_CASE__         =    VivitForVideoClassification(A_						)
      model.to(A_						)
      model.eval()

      SCREAMING_SNAKE_CASE__         =    model(A_						)

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







  def   _UpperCamelCase     (						self     :					int						)       ->			Tuple:
      SCREAMING_SNAKE_CASE__         =    self.prepare_config_and_inputs()
      SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__         =    config_and_inputs
      SCREAMING_SNAKE_CASE__         =    {"pixel_values": pixel_values}
      return config, inputs_dict







@require_torch
class  UpperCamelCase_      (	_snake_case		,  unittest.TestCase      ):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__					:			Dict					    =   (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

  SCREAMING_SNAKE_CASE__					:			Dict					    =   False
  SCREAMING_SNAKE_CASE__					:			int					    =   False
  SCREAMING_SNAKE_CASE__					:			Dict					    =   False
  SCREAMING_SNAKE_CASE__					:			Union[str, Any]					    =   False



  def   _UpperCamelCase     (						self     :					Optional[int]						)       ->			int:
      SCREAMING_SNAKE_CASE__         =    VivitModelTester(self						)
      SCREAMING_SNAKE_CASE__         =    ConfigTester(self  ,	config_class=A_  ,	has_text_modality=A_  ,	hidden_size=37						)



  def   _UpperCamelCase     (						self     :					Optional[int]  ,	A_     :					str  ,	A_     :					Tuple  ,	A_     :					List[str]=False						)       ->			str:
      SCREAMING_SNAKE_CASE__         =    copy.deepcopy(A_						)

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

      return inputs_dict



  def   _UpperCamelCase     (						self     :					List[str]						)       ->			int:
      self.config_tester.run_common_tests()



  @unittest.skip(reason="Vivit does not use inputs_embeds"						)
  def   _UpperCamelCase     (						self     :					Union[str, Any]						)       ->			Tuple:
      pass



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

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



  def   _UpperCamelCase     (						self     :					List[str]						)       ->			Dict:
      SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__         =    self.model_tester.prepare_config_and_inputs_for_common()

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

          SCREAMING_SNAKE_CASE__         =    ["pixel_values", "head_mask"]
          self.assertListEqual(arg_names[:2]  ,	A_						)



  def   _UpperCamelCase     (						self     :					Optional[Any]						)       ->			int:
      SCREAMING_SNAKE_CASE__         =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A_						)



  def   _UpperCamelCase     (						self     :					List[Any]						)       ->			List[Any]:
      SCREAMING_SNAKE_CASE__         =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_video_classification(*A_						)



  @slow
  def   _UpperCamelCase     (						self     :					Tuple						)       ->			Tuple:
      for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          SCREAMING_SNAKE_CASE__         =    VivitModel.from_pretrained(A_						)
          self.assertIsNotNone(A_						)



  def   _UpperCamelCase     (						self     :					Optional[int]						)       ->			Tuple:
      SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__         =    self.model_tester.prepare_config_and_inputs_for_common()
      SCREAMING_SNAKE_CASE__         =    True

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

          SCREAMING_SNAKE_CASE__         =    True
          SCREAMING_SNAKE_CASE__         =    False
          SCREAMING_SNAKE_CASE__         =    True
          SCREAMING_SNAKE_CASE__         =    model_class(A_						)
          model.to(A_						)
          model.eval()
          with torch.no_grad():
              SCREAMING_SNAKE_CASE__         =    model(**self._prepare_for_class(A_  ,	A_						)						)
          SCREAMING_SNAKE_CASE__         =    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"]
          SCREAMING_SNAKE_CASE__         =    True
          SCREAMING_SNAKE_CASE__         =    model_class(A_						)
          model.to(A_						)
          model.eval()
          with torch.no_grad():
              SCREAMING_SNAKE_CASE__         =    model(**self._prepare_for_class(A_  ,	A_						)						)
          SCREAMING_SNAKE_CASE__         =    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]  ,	)
          SCREAMING_SNAKE_CASE__         =    len(A_						)

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

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

          SCREAMING_SNAKE_CASE__         =    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   _UpperCamelCase     (						self     :					List[Any]						)       ->			Dict:
      def check_hidden_states_output(A_     :					Tuple  ,	A_     :					Optional[Any]  ,	A_     :					str						):
          SCREAMING_SNAKE_CASE__         =    model_class(A_						)
          model.to(A_						)
          model.eval()

          with torch.no_grad():
              SCREAMING_SNAKE_CASE__         =    model(**self._prepare_for_class(A_  ,	A_						)						)

          SCREAMING_SNAKE_CASE__         =    outputs.hidden_states
          SCREAMING_SNAKE_CASE__         =    self.model_tester.num_hidden_layers + 1
          self.assertEqual(len(A_						)  ,	A_						)

          SCREAMING_SNAKE_CASE__         =    self.model_tester.seq_length

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

      SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__         =    self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
          SCREAMING_SNAKE_CASE__         =    True
          check_hidden_states_output(A_  ,	A_  ,	A_						)

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          SCREAMING_SNAKE_CASE__         =    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   _UpperCamelCase     (						self     :					List[Any]						)       ->			List[Any]:
      pass





def 				__a							(       ):
    SCREAMING_SNAKE_CASE__         =    hf_hub_download(
        repo_id="hf-internal-testing/spaghetti-video"			,							filename="eating_spaghetti_32_frames.npy"			,							repo_type="dataset"						)
    SCREAMING_SNAKE_CASE__         =    np.load(__A						)
    return list(__A						)







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


  """simple docstring"""



  @cached_property
  def   _UpperCamelCase     (						self     :					int						)       ->			List[str]:
      return VivitImageProcessor() if is_vision_available() else None







  @slow
  def   _UpperCamelCase     (						self     :					Optional[int]						)       ->			List[Any]:
      SCREAMING_SNAKE_CASE__         =    VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"						).to(A_						)

      SCREAMING_SNAKE_CASE__         =    self.default_image_processor
      SCREAMING_SNAKE_CASE__         =    prepare_video()
      SCREAMING_SNAKE_CASE__         =    image_processor(A_  ,	return_tensors="pt"						).to(A_						)

      # forward pass
      with torch.no_grad():
          SCREAMING_SNAKE_CASE__         =    model(**A_						)

      # verify the logits
      SCREAMING_SNAKE_CASE__         =    torch.Size((1, 400)						)
      self.assertEqual(outputs.logits.shape  ,	A_						)

      # taken from original model
      SCREAMING_SNAKE_CASE__         =    torch.tensor([-1.0_543, 2.0_764, -0.2_104, 0.4_439, -0.9_658]						).to(A_						)

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





from __future__ import annotations





def 				__a							(       __A						):
    SCREAMING_SNAKE_CASE__         =    {1}
    SCREAMING_SNAKE_CASE__         =    [1]
    for _ in range(2			,							depth + 1						):
        SCREAMING_SNAKE_CASE__         =    [*previous_coefficients, 0]
        SCREAMING_SNAKE_CASE__         =    [0, *previous_coefficients]
        SCREAMING_SNAKE_CASE__         =    []
        for x, y in zip(__A			,							__A						):
            coefficients.add(x + y						)
            previous_coefficients.append(x + y						)
    return coefficients





def 				__a							(       __A						):
    SCREAMING_SNAKE_CASE__         =    set()
    for number in unique_coefficients:
        SCREAMING_SNAKE_CASE__         =    2
        SCREAMING_SNAKE_CASE__         =    number
        while divisor**2 <= copy_number:
            SCREAMING_SNAKE_CASE__         =    0
            while copy_number % divisor == 0:
                copy_number //= divisor
                multiplicity += 1
            if multiplicity >= 2:
                non_squarefrees.add(__A						)
                break
            divisor += 1

    return unique_coefficients.difference(__A						)





def 				__a							(       __A = 51						):
    SCREAMING_SNAKE_CASE__         =    get_pascal_triangle_unique_coefficients(__A						)
    SCREAMING_SNAKE_CASE__         =    get_squarefrees(__A						)
    return sum(__A						)


if __name__ == "__main__":
  print(f'''{solution() = }''')


