


"""simple docstring"""



# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


lowercase_							=							{
    'configuration_vivit': ['VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VivitConfig'],
}
try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							['VivitImageProcessor']


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							[
		    'VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'VivitModel',
		    'VivitPreTrainedModel',
		    'VivitForVideoClassification',
		]


if TYPE_CHECKING:
		from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .image_processing_vivit import VivitImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vivit import (
				    VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    VivitForVideoClassification,
				    VivitModel,
				    VivitPreTrainedModel,
				)


else:
		import sys

		lowercase_							=							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



"""simple docstring"""



from typing import Optional

import numpy as np
import torch
from torch import nn
from transformers import GPTaConfig, GPTaLMHeadModel
from transformers.modeling_utils import ModuleUtilsMixin

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin







class    __a						( __snake_case					,			__snake_case					,			__snake_case							):
				lowerCamelCase     :       Optional[int]			       =[R'h\.\d+\.attn\.bias', R'h\.\d+\.attn\.masked_bias']







				@register_to_config
				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase = None      ,	UpperCAmelCase = 5_0257      ,	UpperCAmelCase = 1024      ,	UpperCAmelCase = 768      ,	UpperCAmelCase = 12      ,	UpperCAmelCase = 12      ,	UpperCAmelCase = None      ,	UpperCAmelCase = "gelu_new"      ,	UpperCAmelCase = 0.1      ,	UpperCAmelCase = 0.1      ,	UpperCAmelCase = 0.1      ,	UpperCAmelCase = 1E-5      ,	UpperCAmelCase = 0.0_2      ,	UpperCAmelCase = True      ,	UpperCAmelCase = True      ,	UpperCAmelCase = False      ,	UpperCAmelCase = False      ,	):

					'''simple docstring'''






					super().__init__()

					lowerCAmelCase_      							=			prefix_length

					if prefix_inner_dim != n_embd and prefix_hidden_dim is None:
						raise ValueError(
						    F"""`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and"""
						    F""" `n_embd`: {n_embd} are not equal."""   )

					lowerCAmelCase_      							=			prefix_inner_dim
					lowerCAmelCase_      							=			prefix_hidden_dim

					lowerCAmelCase_      							=			(
					    nn.Linear(self.prefix_inner_dim      ,	self.prefix_hidden_dim   )
					    if self.prefix_hidden_dim is not None
					    else nn.Identity()
					)
					lowerCAmelCase_      							=			(
					    nn.Linear(self.prefix_hidden_dim      ,	UpperCAmelCase   ) if self.prefix_hidden_dim is not None else nn.Identity()
					)

					lowerCAmelCase_      							=			GPTaConfig(
					    vocab_size=UpperCAmelCase      ,	n_positions=UpperCAmelCase      ,	n_embd=UpperCAmelCase      ,	n_layer=UpperCAmelCase      ,	n_head=UpperCAmelCase      ,	n_inner=UpperCAmelCase      ,	activation_function=UpperCAmelCase      ,	resid_pdrop=UpperCAmelCase      ,	embd_pdrop=UpperCAmelCase      ,	attn_pdrop=UpperCAmelCase      ,	layer_norm_epsilon=UpperCAmelCase      ,	initializer_range=UpperCAmelCase      ,	scale_attn_weights=UpperCAmelCase      ,	use_cache=UpperCAmelCase      ,	scale_attn_by_inverse_layer_idx=UpperCAmelCase      ,	reorder_and_upcast_attn=UpperCAmelCase      ,	)
					lowerCAmelCase_      							=			GPTaLMHeadModel(UpperCAmelCase   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase = None      ,	UpperCAmelCase = None      ,	):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.transformer.transformer.wte(UpperCAmelCase   )
					lowerCAmelCase_      							=			self.encode_prefix(UpperCAmelCase   )
					lowerCAmelCase_      							=			self.decode_prefix(UpperCAmelCase   )
					lowerCAmelCase_      							=			torch.cat((prefix_embeds, embedding_text)      ,	dim=1   )

					if labels is not None:
						lowerCAmelCase_      							=			self.get_dummy_token(input_ids.shape[0]      ,	input_ids.device   )
						lowerCAmelCase_      							=			torch.cat((dummy_token, input_ids)      ,	dim=1   )
					lowerCAmelCase_      							=			self.transformer(inputs_embeds=UpperCAmelCase      ,	labels=UpperCAmelCase      ,	attention_mask=UpperCAmelCase   )
					if self.prefix_hidden_dim is not None:
						return out, hidden
					else:
						return out







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					return torch.zeros(UpperCAmelCase      ,	self.prefix_length      ,	dtype=torch.intaa      ,	device=UpperCAmelCase   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					return self.encode_prefix(UpperCAmelCase   )







				@torch.no_grad()
				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			torch.split(UpperCAmelCase      ,	1      ,	dim=0   )
					lowerCAmelCase_      							=			[]
					lowerCAmelCase_      							=			[]
					for feature in features:
						lowerCAmelCase_      							=			self.decode_prefix(feature.to(UpperCAmelCase   )   )  # back to the clip feature
						# Only support beam search for now
						lowerCAmelCase_							,	lowerCAmelCase_      							=			self.generate_beam(
						    input_embeds=UpperCAmelCase      ,	device=UpperCAmelCase      ,	eos_token_id=UpperCAmelCase   )
						generated_tokens.append(output_tokens[0]   )
						generated_seq_lengths.append(seq_lengths[0]   )
					lowerCAmelCase_      							=			torch.stack(UpperCAmelCase   )
					lowerCAmelCase_      							=			torch.stack(UpperCAmelCase   )
					return generated_tokens, generated_seq_lengths







				@torch.no_grad()
				def      lowerCamelCase_     ( self      ,	UpperCAmelCase=None      ,	UpperCAmelCase=None      ,	UpperCAmelCase=None      ,	UpperCAmelCase = 5      ,	UpperCAmelCase = 67      ,	UpperCAmelCase = 1.0      ,	UpperCAmelCase = None      ,	):

					'''simple docstring'''






					lowerCAmelCase_      							=			eos_token_id
					lowerCAmelCase_      							=			None
					lowerCAmelCase_      							=			None
					lowerCAmelCase_      							=			torch.ones(UpperCAmelCase      ,	device=UpperCAmelCase      ,	dtype=torch.int   )
					lowerCAmelCase_      							=			torch.zeros(UpperCAmelCase      ,	device=UpperCAmelCase      ,	dtype=torch.bool   )

					if input_embeds is not None:
						lowerCAmelCase_      							=			input_embeds
					else:
						lowerCAmelCase_      							=			self.transformer.transformer.wte(UpperCAmelCase   )

					for i in range(UpperCAmelCase   ):
						lowerCAmelCase_      							=			self.transformer(inputs_embeds=UpperCAmelCase   )
						lowerCAmelCase_      							=			outputs.logits
						lowerCAmelCase_      							=			logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
						lowerCAmelCase_      							=			logits.softmax(-1   ).log()

						if scores is None:
							lowerCAmelCase_							,	lowerCAmelCase_      							=			logits.topk(UpperCAmelCase      ,	-1   )
							lowerCAmelCase_      							=			generated.expand(UpperCAmelCase      ,	*generated.shape[1:]   )
							lowerCAmelCase_							,	lowerCAmelCase_      							=			next_tokens.permute(1      ,	0   ), scores.squeeze(0   )
							if tokens is None:
								lowerCAmelCase_      							=			next_tokens
							else:
								lowerCAmelCase_      							=			tokens.expand(UpperCAmelCase      ,	*tokens.shape[1:]   )
								lowerCAmelCase_      							=			torch.cat((tokens, next_tokens)      ,	dim=1   )
						else:
							lowerCAmelCase_      							=			-float(np.inf   )
							lowerCAmelCase_      							=			0
							lowerCAmelCase_      							=			scores[:, None] + logits
							seq_lengths[~is_stopped] += 1
							lowerCAmelCase_      							=			scores_sum / seq_lengths[:, None]
							lowerCAmelCase_							,	lowerCAmelCase_      							=			scores_sum_average.view(-1   ).topk(UpperCAmelCase      ,	-1   )
							lowerCAmelCase_      							=			next_tokens // scores_sum.shape[1]
							lowerCAmelCase_      							=			seq_lengths[next_tokens_source]
							lowerCAmelCase_      							=			next_tokens % scores_sum.shape[1]
							lowerCAmelCase_      							=			next_tokens.unsqueeze(1   )
							lowerCAmelCase_      							=			tokens[next_tokens_source]
							lowerCAmelCase_      							=			torch.cat((tokens, next_tokens)      ,	dim=1   )
							lowerCAmelCase_      							=			generated[next_tokens_source]
							lowerCAmelCase_      							=			scores_sum_average * seq_lengths
							lowerCAmelCase_      							=			is_stopped[next_tokens_source]

						lowerCAmelCase_      							=			self.transformer.transformer.wte(next_tokens.squeeze()   ).view(generated.shape[0]      ,	1      ,	-1   )
						lowerCAmelCase_      							=			torch.cat((generated, next_token_embed)      ,	dim=1   )
						lowerCAmelCase_      							=			is_stopped + next_tokens.eq(UpperCAmelCase   ).squeeze()
						if is_stopped.all():
							break

					lowerCAmelCase_      							=			scores / seq_lengths
					lowerCAmelCase_      							=			scores.argsort(descending=UpperCAmelCase   )
					# tokens tensors are already padded to max_seq_length
					lowerCAmelCase_      							=			[tokens[i] for i in order]
					lowerCAmelCase_      							=			torch.stack(UpperCAmelCase      ,	dim=0   )
					lowerCAmelCase_      							=			torch.tensor([seq_lengths[i] for i in order]      ,	dtype=seq_lengths.dtype   )
					return output_texts, seq_lengths