'''simple docstring'''




from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


_lowercase		:				int	       =		logging.get_logger(__name__)

_lowercase		:				Optional[Any]	       =		{
    "EleutherAI/gpt-j-6B": "https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json",
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}





class 					__magic_name__		(					_UpperCAmelCase):
			UpperCamelCase__       = '''gptj'''
			UpperCamelCase__       = {
			    '''max_position_embeddings''': '''n_positions''',
			    '''hidden_size''': '''n_embd''',
			    '''num_attention_heads''': '''n_head''',
			    '''num_hidden_layers''': '''n_layer''',
			}







			def __init__(			self       :						int       ,    lowercase_       :						str=50400       ,    lowercase_       :						Any=2048       ,    lowercase_       :						Any=4096       ,    lowercase_       :						int=28       ,    lowercase_       :						List[str]=16       ,    lowercase_       :						Optional[Any]=64       ,    lowercase_       :						Union[str, Any]=None       ,    lowercase_       :						Dict="gelu_new"       ,    lowercase_       :						int=0.0       ,    lowercase_       :						Union[str, Any]=0.0       ,    lowercase_       :						int=0.0       ,    lowercase_       :						Any=1E-5       ,    lowercase_       :						Optional[int]=0.02       ,    lowercase_       :						str=True       ,    lowercase_       :						str=50256       ,    lowercase_       :						List[str]=50256       ,    lowercase_       :						str=False       ,    **lowercase_       :						str       ,    ):
										lowercase_      :    Optional[Any]       		= vocab_size
										lowercase_      :    Tuple       		= n_positions
										lowercase_      :    str       		= n_embd
										lowercase_      :    Tuple       		= n_layer
										lowercase_      :    Dict       		= n_head
										lowercase_      :    Union[str, Any]       		= n_inner
										lowercase_      :    Optional[Any]       		= rotary_dim
										lowercase_      :    int       		= activation_function
										lowercase_      :    int       		= resid_pdrop
										lowercase_      :    str       		= embd_pdrop
										lowercase_      :    Union[str, Any]       		= attn_pdrop
										lowercase_      :    Tuple       		= layer_norm_epsilon
										lowercase_      :    str       		= initializer_range
										lowercase_      :    Union[str, Any]       		= use_cache

										lowercase_      :    Optional[int]       		= bos_token_id
										lowercase_      :    Any       		= eos_token_id

										super().__init__(
										    bos_token_id=lowercase_       ,    eos_token_id=lowercase_       ,    tie_word_embeddings=lowercase_       ,    **lowercase_					)





class 					__magic_name__		(					_UpperCAmelCase):







			def __init__(			self       :						Union[str, Any]       ,    lowercase_       :						PretrainedConfig       ,    lowercase_       :						str = "default"       ,    lowercase_       :						List[PatchingSpec] = None       ,    lowercase_       :						bool = False       ,    ):
										super().__init__(lowercase_       ,    task=lowercase_       ,    patching_specs=lowercase_       ,    use_past=lowercase_					)
										if not getattr(self._config       ,    """pad_token_id"""       ,    lowercase_					):
																	# TODO: how to do that better?
																	lowercase_      :    Optional[int]       		= 0







			@property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										lowercase_      :    List[str]       		= OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}					)
										if self.use_past:
																	self.fill_with_past_key_values_(lowercase_       ,    direction="""inputs"""					)
																	lowercase_      :    int       		= {0: """batch""", 1: """past_sequence + sequence"""}
										else:
																	lowercase_      :    List[Any]       		= {0: """batch""", 1: """sequence"""}

										return common_inputs







			@property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										return self._config.n_layer







			@property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										return self._config.n_head







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict       ,    lowercase_       :						PreTrainedTokenizer       ,    lowercase_       :						int = -1       ,    lowercase_       :						int = -1       ,    lowercase_       :						bool = False       ,    lowercase_       :						Optional[TensorType] = None       ,    ):
										lowercase_      :    Tuple       		= super(lowercase_       ,    self					).generate_dummy_inputs(
										    lowercase_       ,    batch_size=lowercase_       ,    seq_length=lowercase_       ,    is_pair=lowercase_       ,    framework=lowercase_					)

										# We need to order the input in the way they appears in the forward()
										lowercase_      :    Optional[Any]       		= OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}					)

										# Need to add the past_keys
										if self.use_past:
																	if not is_torch_available():
																								raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""					)
																	else:
																								import torch

																								lowercase_		,		lowercase_      :    List[Any]       		= common_inputs["""input_ids"""].shape
																								# Not using the same length for past_key_values
																								lowercase_      :    Tuple       		= seqlen + 2
																								lowercase_      :    Any       		= (
																								    batch,
																								    self.num_attention_heads,
																								    past_key_values_length,
																								    self._config.hidden_size // self.num_attention_heads,
																								)
																								lowercase_      :    List[Any]       		= [
																								    (torch.zeros(lowercase_					), torch.zeros(lowercase_					)) for _ in range(self.num_layers					)
																								]

										lowercase_      :    Any       		= common_inputs["""attention_mask"""]
										if self.use_past:
																	lowercase_      :    List[Any]       		= ordered_inputs["""attention_mask"""].dtype
																	lowercase_      :    Union[str, Any]       		= torch.cat(
																	    [ordered_inputs["""attention_mask"""], torch.ones(lowercase_       ,    lowercase_       ,    dtype=lowercase_					)]       ,    dim=1					)

										return ordered_inputs







			@property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										return 13






'''simple docstring'''




import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import TimesformerConfig
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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import (
						    MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
						    TimesformerForVideoClassification,
						    TimesformerModel,
						)
						from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from transformers import VideoMAEImageProcessor





class 					__magic_name__		:







			def __init__(			self       :						int       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						List[Any]=13       ,    lowercase_       :						List[str]=10       ,    lowercase_       :						Union[str, Any]=3       ,    lowercase_       :						str=2       ,    lowercase_       :						Optional[Any]=2       ,    lowercase_       :						int=True       ,    lowercase_       :						List[Any]=True       ,    lowercase_       :						Union[str, Any]=32       ,    lowercase_       :						Union[str, Any]=5       ,    lowercase_       :						str=4       ,    lowercase_       :						Dict=37       ,    lowercase_       :						Tuple="gelu"       ,    lowercase_       :						int=0.1       ,    lowercase_       :						Optional[Any]=0.1       ,    lowercase_       :						Any=10       ,    lowercase_       :						Tuple=0.02       ,    lowercase_       :						Any="divided_space_time"       ,    lowercase_       :						Tuple=None       ,    ):
										lowercase_      :    int       		= parent
										lowercase_      :    str       		= batch_size
										lowercase_      :    List[str]       		= image_size
										lowercase_      :    str       		= num_channels
										lowercase_      :    List[Any]       		= patch_size
										lowercase_      :    Optional[Any]       		= num_frames
										lowercase_      :    Dict       		= is_training
										lowercase_      :    int       		= use_labels
										lowercase_      :    List[str]       		= hidden_size
										lowercase_      :    Dict       		= num_hidden_layers
										lowercase_      :    Dict       		= num_attention_heads
										lowercase_      :    Any       		= intermediate_size
										lowercase_      :    Optional[int]       		= hidden_act
										lowercase_      :    Optional[Any]       		= hidden_dropout_prob
										lowercase_      :    List[Any]       		= attention_probs_dropout_prob
										lowercase_      :    Any       		= attention_type
										lowercase_      :    Union[str, Any]       		= initializer_range
										lowercase_      :    List[str]       		= scope
										lowercase_      :    Optional[int]       		= num_labels

										# in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
										lowercase_      :    Dict       		= (image_size // patch_size) ** 2
										lowercase_      :    List[Any]       		= (num_frames) * self.num_patches_per_frame + 1







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										lowercase_      :    Optional[Any]       		= floats_tensor(
										    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

										lowercase_      :    int       		= None
										if self.use_labels:
																	lowercase_      :    Optional[int]       		= ids_tensor([self.batch_size]       ,    self.num_labels					)

										lowercase_      :    Optional[Any]       		= self.get_config()

										return config, pixel_values, labels







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]					):
										lowercase_      :    int       		= TimesformerConfig(
										    image_size=self.image_size       ,    patch_size=self.patch_size       ,    num_channels=self.num_channels       ,    num_frames=self.num_frames       ,    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       ,    attention_type=self.attention_type       ,    )
										lowercase_      :    Any       		= self.num_labels
										return config







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						List[Any]       ,    lowercase_       :						Dict       ,    lowercase_       :						List[str]					):
										lowercase_      :    Optional[Any]       		= TimesformerModel(config=lowercase_					)
										model.to(lowercase_					)
										model.eval()
										lowercase_      :    int       		= model(lowercase_					)
										self.parent.assertEqual(result.last_hidden_state.shape       ,    (self.batch_size, self.seq_length, self.hidden_size)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						List[str]       ,    lowercase_       :						str					):
										lowercase_      :    Dict       		= TimesformerForVideoClassification(lowercase_					)
										model.to(lowercase_					)
										model.eval()

										lowercase_      :    int       		= model(lowercase_					)

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										lowercase_      :    List[str]       		= self.prepare_config_and_inputs()
										lowercase_		,		lowercase_		,		lowercase_      :    int       		= config_and_inputs
										lowercase_      :    List[Any]       		= {"""pixel_values""": pixel_values}
										return config, inputs_dict





@require_torch
class 					__magic_name__		(					_UpperCAmelCase,  _UpperCAmelCase,  unittest.TestCase):
			UpperCamelCase__       = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
			UpperCamelCase__       = (
			    {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = False







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										lowercase_      :    Any       		= TimesformerModelTester(self					)
										lowercase_      :    Union[str, Any]       		= ConfigTester(
										    self       ,    config_class=lowercase_       ,    has_text_modality=lowercase_       ,    hidden_size=37					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						Any       ,    lowercase_       :						List[str]       ,    lowercase_       :						Tuple=False					):
										lowercase_      :    List[Any]       		= copy.deepcopy(lowercase_					)

										if return_labels:
																	if model_class in get_values(lowercase_					):
																								lowercase_      :    List[str]       		= torch.zeros(
																								    self.model_tester.batch_size       ,    dtype=torch.long       ,    device=lowercase_					)

										return inputs_dict







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										self.config_tester.run_common_tests()







			@unittest.skip(reason="""TimeSformer does not use inputs_embeds"""					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										pass







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										lowercase_		,		lowercase_      :    Any       		= self.model_tester.prepare_config_and_inputs_for_common()

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_		,		lowercase_      :    str       		= self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowercase_      :    str       		= ["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]       ,    lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										lowercase_      :    List[str]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										lowercase_      :    str       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_video_classification(*lowercase_					)







			@slow
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_      :    Any       		= TimesformerModel.from_pretrained(lowercase_					)
																	self.assertIsNotNone(lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										if not self.has_attentions:
																	pass

										else:
																	lowercase_		,		lowercase_      :    Any       		= self.model_tester.prepare_config_and_inputs_for_common()
																	lowercase_      :    List[str]       		= True

																	for model_class in self.all_model_classes:
																								lowercase_      :    str       		= self.model_tester.seq_length
																								lowercase_      :    int       		= self.model_tester.num_frames

																								lowercase_      :    int       		= True
																								lowercase_      :    Any       		= False
																								lowercase_      :    str       		= True
																								lowercase_      :    int       		= model_class(lowercase_					)
																								model.to(lowercase_					)
																								model.eval()
																								with torch.no_grad():
																															lowercase_      :    List[Any]       		= model(**self._prepare_for_class(lowercase_       ,    lowercase_					)					)
																								lowercase_      :    List[str]       		= 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"]
																								lowercase_      :    List[str]       		= True
																								lowercase_      :    str       		= model_class(lowercase_					)
																								model.to(lowercase_					)
																								model.eval()
																								with torch.no_grad():
																															lowercase_      :    Dict       		= model(**self._prepare_for_class(lowercase_       ,    lowercase_					)					)
																								lowercase_      :    int       		= outputs.attentions
																								self.assertEqual(len(lowercase_					)       ,    self.model_tester.num_hidden_layers					)

																								# attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
																								self.assertListEqual(
																								    list(attentions[0].shape[-3:]					)       ,    [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]       ,    )
																								lowercase_      :    Optional[Any]       		= len(lowercase_					)

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

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

																								lowercase_      :    Optional[Any]       		= outputs.attentions

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

																								# attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
																								self.assertListEqual(
																								    list(self_attentions[0].shape[-3:]					)       ,    [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]       ,    )







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										def check_hidden_states_output(lowercase_       :						Union[str, Any]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Dict					):
																	lowercase_      :    List[str]       		= model_class(lowercase_					)
																	model.to(lowercase_					)
																	model.eval()

																	with torch.no_grad():
																								lowercase_      :    Optional[Any]       		= model(**self._prepare_for_class(lowercase_       ,    lowercase_					)					)

																	lowercase_      :    Dict       		= outputs.hidden_states
																	lowercase_      :    List[Any]       		= self.model_tester.num_hidden_layers + 1
																	self.assertEqual(len(lowercase_					)       ,    lowercase_					)

																	lowercase_      :    List[Any]       		= self.model_tester.seq_length

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

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

										for model_class in self.all_model_classes:
																	lowercase_      :    List[str]       		= True
																	check_hidden_states_output(lowercase_       ,    lowercase_       ,    lowercase_					)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	lowercase_      :    Optional[int]       		= True

																	check_hidden_states_output(lowercase_       ,    lowercase_       ,    lowercase_					)




def 							lowerCamelCase			(   )    ->   Optional[int]:
							lowercase_      :    List[str]       		= hf_hub_download(
							    repo_id="""hf-internal-testing/spaghetti-video"""   ,   filename="""eating_spaghetti.npy"""   ,   repo_type="""dataset"""				)
							lowercase_      :    List[Any]       		= np.load(UpperCAmelCase__				)
							return list(UpperCAmelCase__				)





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







			@cached_property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										# logits were tested with a different mean and std, so we use the same here
										return (
										    VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]       ,    image_std=[0.5, 0.5, 0.5]					)
										    if is_vision_available()
										    else None
										)







			@slow
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										lowercase_      :    Any       		= TimesformerForVideoClassification.from_pretrained("""facebook/timesformer-base-finetuned-k400"""					).to(
										    lowercase_					)

										lowercase_      :    Optional[Any]       		= self.default_image_processor
										lowercase_      :    Any       		= prepare_video()
										lowercase_      :    Optional[int]       		= image_processor(video[:8]       ,    return_tensors="""pt"""					).to(lowercase_					)

										# forward pass
										with torch.no_grad():
																	lowercase_      :    Optional[Any]       		= model(**lowercase_					)

										# verify the logits
										lowercase_      :    Any       		= torch.Size((1, 400)					)
										self.assertEqual(outputs.logits.shape       ,    lowercase_					)

										lowercase_      :    int       		= torch.tensor([-0.30_16, -0.77_13, -0.42_05]					).to(lowercase_					)

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





