




"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase				:     int					=				logging.get_logger(__name__)

lowerCAmelCase				:     Optional[Any]					=				{
    """facebook/data2vec-vision-base-ft""": (
        """https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json"""
    ),
}






class        __magic_name__				(			UpperCAmelCase__							):






				'''simple docstring'''
				__UpperCamelCase         =		"data2vec-vision"





				def __init__(			self						,					_a=768						,					_a=12						,					_a=12						,					_a=3_072						,					_a="gelu"						,					_a=0.0						,					_a=0.0						,					_a=0.02						,					_a=1e-1_2						,					_a=224						,					_a=16						,					_a=3						,					_a=False						,					_a=False						,					_a=False						,					_a=False						,					_a=0.1						,					_a=0.1						,					_a=True						,					_a=[3, 5, 7, 11]						,					_a=[1, 2, 3, 6]						,					_a=True						,					_a=0.4						,					_a=256						,					_a=1						,					_a=False						,					_a=255						,					**_a						,					):





							"""simple docstring"""
							super().__init__(**_a					)

							lowerCamelCase      =   hidden_size
							lowerCamelCase      =   num_hidden_layers
							lowerCamelCase      =   num_attention_heads
							lowerCamelCase      =   intermediate_size
							lowerCamelCase      =   hidden_act
							lowerCamelCase      =   hidden_dropout_prob
							lowerCamelCase      =   attention_probs_dropout_prob
							lowerCamelCase      =   initializer_range
							lowerCamelCase      =   layer_norm_eps

							lowerCamelCase      =   image_size
							lowerCamelCase      =   patch_size
							lowerCamelCase      =   num_channels
							lowerCamelCase      =   use_mask_token
							lowerCamelCase      =   use_absolute_position_embeddings
							lowerCamelCase      =   use_relative_position_bias
							lowerCamelCase      =   use_shared_relative_position_bias
							lowerCamelCase      =   layer_scale_init_value
							lowerCamelCase      =   drop_path_rate
							lowerCamelCase      =   use_mean_pooling
							# decode head attributes (semantic segmentation)
							lowerCamelCase      =   out_indices
							lowerCamelCase      =   pool_scales
							# auxiliary head attributes (semantic segmentation)
							lowerCamelCase      =   use_auxiliary_head
							lowerCamelCase      =   auxiliary_loss_weight
							lowerCamelCase      =   auxiliary_channels
							lowerCamelCase      =   auxiliary_num_convs
							lowerCamelCase      =   auxiliary_concat_input
							lowerCamelCase      =   semantic_loss_ignore_index






class        __magic_name__				(			UpperCAmelCase__							):






				'''simple docstring'''
				__UpperCamelCase         =		version.parse("1.11"							)





				@property
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							return OrderedDict(
							    [
							        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
							    ]					)





				@property
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							return 1e-4









"""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        __magic_name__				(			UpperCAmelCase__		,   UpperCAmelCase__		,   UpperCAmelCase__							):






				'''simple docstring'''
				__UpperCamelCase         =		[R"h\.\d+\.attn\.bias", R"h\.\d+\.attn\.masked_bias"]





				@register_to_config
				def __init__(			self						,					_a						,					_a						,					_a = None						,					_a = 50_257						,					_a = 1_024						,					_a = 768						,					_a = 12						,					_a = 12						,					_a = None						,					_a = "gelu_new"						,					_a = 0.1						,					_a = 0.1						,					_a = 0.1						,					_a = 1e-5						,					_a = 0.02						,					_a = True						,					_a = True						,					_a = False						,					_a = 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						,					_a					) if self.prefix_hidden_dim is not None else nn.Identity()
							)

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





				def   _lowerCAmelCase							(			self						,					_a						,					_a						,					_a = None						,					_a = None						,					):





							"""simple docstring"""
							lowerCamelCase      =   self.transformer.transformer.wte(_a					)
							lowerCamelCase      =   self.encode_prefix(_a					)
							lowerCamelCase      =   self.decode_prefix(_a					)
							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=_a						,					labels=_a						,					attention_mask=_a					)
							if self.prefix_hidden_dim is not None:
										return out, hidden
							else:
										return out





				def   _lowerCAmelCase							(			self						,					_a						,					_a					):





							"""simple docstring"""
							return torch.zeros(_a						,					self.prefix_length						,					dtype=torch.intaa						,					device=_a					)





				def   _lowerCAmelCase							(			self						,					_a					):





							"""simple docstring"""
							return self.encode_prefix(_a					)





				@torch.no_grad()
				def   _lowerCAmelCase							(			self						,					_a						,					_a						,					_a					):





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





				@torch.no_grad()
				def   _lowerCAmelCase							(			self						,					_a=None						,					_a=None						,					_a=None						,					_a = 5						,					_a = 67						,					_a = 1.0						,					_a = None						,					):





							"""simple docstring"""
							lowerCamelCase      =   eos_token_id
							lowerCamelCase      =   None
							lowerCamelCase      =   None
							lowerCamelCase      =   torch.ones(_a						,					device=_a						,					dtype=torch.int					)
							lowerCamelCase      =   torch.zeros(_a						,					device=_a						,					dtype=torch.bool					)

							if input_embeds is not None:
										lowerCamelCase      =   input_embeds
							else:
										lowerCamelCase      =   self.transformer.transformer.wte(_a					)

							for i in range(_a					):
										lowerCamelCase      =   self.transformer(inputs_embeds=_a					)
										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(_a						,					-1					)
													lowerCamelCase      =   generated.expand(_a						,					*generated.shape[1:]					)
													lowerCamelCase ,  lowerCamelCase      =   next_tokens.permute(1						,					0					), scores.squeeze(0					)
													if tokens is None:
																lowerCamelCase      =   next_tokens
													else:
																lowerCamelCase      =   tokens.expand(_a						,					*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(_a						,					-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(_a					).squeeze()
										if is_stopped.all():
													break

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



