






'''simple docstring'''







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

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


__lowerCAmelCase       		=      logging.get_logger(__name__)

__lowerCAmelCase       		=      {
    'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/config.json',
    # See all BART models at https://huggingface.co/models?filter=bart
}





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				"bart"
			snake_case__  =				["past_key_values"]
			snake_case__  =				{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}






			def __init__( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=5_0265  ,						__SCREAMING_SNAKE_CASE      :			int=1024  ,						__SCREAMING_SNAKE_CASE      :			Tuple=12  ,						__SCREAMING_SNAKE_CASE      :			int=4096  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=16  ,						__SCREAMING_SNAKE_CASE      :			int=12  ,						__SCREAMING_SNAKE_CASE      :			Any=4096  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=16  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=0.0  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=0.0  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]="gelu"  ,						__SCREAMING_SNAKE_CASE      :			str=1024  ,						__SCREAMING_SNAKE_CASE      :			Dict=0.1  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=0.0  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=0.0  ,						__SCREAMING_SNAKE_CASE      :			int=0.02  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=0.0  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			str=True  ,						__SCREAMING_SNAKE_CASE      :			List[str]=3  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=1  ,						__SCREAMING_SNAKE_CASE      :			str=0  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=2  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=True  ,						__SCREAMING_SNAKE_CASE      :			Any=2  ,						__SCREAMING_SNAKE_CASE      :			Dict=2  ,						**__SCREAMING_SNAKE_CASE      :			Tuple  ,						)			->			List[Any]:
							a_		:  Tuple											=      vocab_size
							a_		:  Dict											=      max_position_embeddings
							a_		:  List[str]											=      d_model
							a_		:  Optional[int]											=      encoder_ffn_dim
							a_		:  Dict											=      encoder_layers
							a_		:  List[str]											=      encoder_attention_heads
							a_		:  int											=      decoder_ffn_dim
							a_		:  Dict											=      decoder_layers
							a_		:  Tuple											=      decoder_attention_heads
							a_		:  str											=      dropout
							a_		:  Any											=      attention_dropout
							a_		:  int											=      activation_dropout
							a_		:  Dict											=      activation_function
							a_		:  Tuple											=      init_std
							a_		:  Any											=      encoder_layerdrop
							a_		:  Any											=      decoder_layerdrop
							a_		:  int											=      classifier_dropout
							a_		:  Tuple											=      use_cache
							a_		:  List[str]											=      encoder_layers
							a_		:  List[str]											=      scale_embedding  # scale factor will be sqrt(d_model) if True

							super().__init__(
							    num_labels=__SCREAMING_SNAKE_CASE  ,						pad_token_id=__SCREAMING_SNAKE_CASE  ,						bos_token_id=__SCREAMING_SNAKE_CASE  ,						eos_token_id=__SCREAMING_SNAKE_CASE  ,						is_encoder_decoder=__SCREAMING_SNAKE_CASE  ,						decoder_start_token_id=__SCREAMING_SNAKE_CASE  ,						forced_eos_token_id=__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE  ,						)

							# ensure backward compatibility for BART CNN models
							if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated'''  ,						__SCREAMING_SNAKE_CASE					):
											a_		:  Dict											=      self.bos_token_id
											warnings.warn(
											    f'Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '
											    '''The config can simply be saved and uploaded again to be fixed.'''					)





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Mapping[str, Mapping[int, str]]:
							if self.task in ["default", "seq2seq-lm"]:
											a_		:  Optional[int]											=      OrderedDict(
											    [
											        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
											        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
											    ]					)

											if self.use_past:
															a_		:  List[Any]											=      {0: '''batch'''}
															a_		:  Any											=      {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
											else:
															a_		:  Dict											=      {0: '''batch''', 1: '''decoder_sequence'''}
															a_		:  List[str]											=      {0: '''batch''', 1: '''decoder_sequence'''}

											if self.use_past:
															self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE  ,						direction='''inputs'''					)
							elif self.task == "causal-lm":
											# TODO: figure this case out.
											a_		:  str											=      OrderedDict(
											    [
											        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
											        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
											    ]					)
											if self.use_past:
															a_				,			a_		:  Any											=      self.num_layers
															for i in range(__SCREAMING_SNAKE_CASE					):
																			a_		:  Dict											=      {0: '''batch''', 2: '''past_sequence + sequence'''}
																			a_		:  Optional[int]											=      {0: '''batch''', 2: '''past_sequence + sequence'''}
							else:
											a_		:  Any											=      OrderedDict(
											    [
											        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
											        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
											        ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
											        ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
											    ]					)

							return common_inputs






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Mapping[str, Mapping[int, str]]:
							if self.task in ["default", "seq2seq-lm"]:
											a_		:  str											=      super().outputs
							else:
											a_		:  str											=      super(__SCREAMING_SNAKE_CASE  ,						self					).outputs
											if self.use_past:
															a_				,			a_		:  Union[str, Any]											=      self.num_layers
															for i in range(__SCREAMING_SNAKE_CASE					):
																			a_		:  Dict											=      {0: '''batch''', 2: '''past_sequence + sequence'''}
																			a_		:  Tuple											=      {0: '''batch''', 2: '''past_sequence + sequence'''}
							return common_outputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			PreTrainedTokenizer  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						__SCREAMING_SNAKE_CASE      :			Optional[TensorType] = None  ,						)			->			Mapping[str, Any]:
							a_		:  Any											=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)

							# Generate decoder inputs
							a_		:  List[str]											=      seq_length if not self.use_past else 1
							a_		:  str											=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
							a_		:  List[Any]											=      {f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
							a_		:  Optional[int]											=      dict(**__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)

							if self.use_past:
											if not is_torch_available():
															raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''					)
											else:
															import torch
											a_				,			a_		:  List[str]											=      common_inputs['''input_ids'''].shape
											a_		:  Dict											=      common_inputs['''decoder_input_ids'''].shape[1]
											a_				,			a_		:  Tuple											=      self.num_attention_heads
											a_		:  Optional[Any]											=      (
											    batch,
											    num_encoder_attention_heads,
											    encoder_seq_length,
											    self._config.hidden_size // num_encoder_attention_heads,
											)
											a_		:  Dict											=      decoder_seq_length + 3
											a_		:  Union[str, Any]											=      (
											    batch,
											    num_decoder_attention_heads,
											    decoder_past_length,
											    self._config.hidden_size // num_decoder_attention_heads,
											)

											a_		:  List[str]											=      torch.cat(
											    [common_inputs['''decoder_attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)]  ,						dim=1					)

											a_		:  Union[str, Any]											=      []
											# If the number of encoder and decoder layers are present in the model configuration, both are considered
											a_				,			a_		:  List[str]											=      self.num_layers
											a_		:  int											=      min(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
											a_		:  int											=      max(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					) - min_num_layers
											a_		:  int											=      '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

											for _ in range(__SCREAMING_SNAKE_CASE					):
															common_inputs["past_key_values"].append(
															    (
															        torch.zeros(__SCREAMING_SNAKE_CASE					),
															        torch.zeros(__SCREAMING_SNAKE_CASE					),
															        torch.zeros(__SCREAMING_SNAKE_CASE					),
															        torch.zeros(__SCREAMING_SNAKE_CASE					),
															    )					)
											# TODO: test this.
											a_		:  Union[str, Any]											=      encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
											for _ in range(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					):
															common_inputs["past_key_values"].append((torch.zeros(__SCREAMING_SNAKE_CASE					), torch.zeros(__SCREAMING_SNAKE_CASE					))					)
							return common_inputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			PreTrainedTokenizer  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						__SCREAMING_SNAKE_CASE      :			Optional[TensorType] = None  ,						)			->			Mapping[str, Any]:
							a_		:  Any											=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)

							if self.use_past:
											if not is_torch_available():
															raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''					)
											else:
															import torch
											a_				,			a_		:  Optional[int]											=      common_inputs['''input_ids'''].shape
											# Not using the same length for past_key_values
											a_		:  Optional[Any]											=      seqlen + 2
											a_				,			a_		:  str											=      self.num_layers
											a_				,			a_		:  Any											=      self.num_attention_heads
											a_		:  str											=      (
											    batch,
											    num_encoder_attention_heads,
											    past_key_values_length,
											    self._config.hidden_size // num_encoder_attention_heads,
											)

											a_		:  Dict											=      common_inputs['''attention_mask'''].dtype
											a_		:  int											=      torch.cat(
											    [common_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						dtype=__SCREAMING_SNAKE_CASE					)]  ,						dim=1					)
											a_		:  int											=      [
											    (torch.zeros(__SCREAMING_SNAKE_CASE					), torch.zeros(__SCREAMING_SNAKE_CASE					)) for _ in range(__SCREAMING_SNAKE_CASE					)
											]
							return common_inputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any  ,						__SCREAMING_SNAKE_CASE      :			PreTrainedTokenizer  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						__SCREAMING_SNAKE_CASE      :			Optional[TensorType] = None  ,						)			->			Mapping[str, Any]:
							# Copied from OnnxConfig.generate_dummy_inputs
							# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
							# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
							a_		:  Tuple											=      compute_effective_axis_dimension(
							    __SCREAMING_SNAKE_CASE  ,						fixed_dimension=OnnxConfig.default_fixed_batch  ,						num_token_to_add=0					)

							# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
							a_		:  Optional[Any]											=      tokenizer.num_special_tokens_to_add(__SCREAMING_SNAKE_CASE					)
							a_		:  str											=      compute_effective_axis_dimension(
							    __SCREAMING_SNAKE_CASE  ,						fixed_dimension=OnnxConfig.default_fixed_sequence  ,						num_token_to_add=__SCREAMING_SNAKE_CASE					)

							# Generate dummy inputs according to compute batch and sequence
							a_		:  Any											=      [''' '''.join([tokenizer.unk_token]					) * seq_length] * batch_size
							a_		:  int											=      dict(tokenizer(__SCREAMING_SNAKE_CASE  ,						return_tensors=__SCREAMING_SNAKE_CASE					)					)
							return common_inputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			PreTrainedTokenizer  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						__SCREAMING_SNAKE_CASE      :			Optional[TensorType] = None  ,						)			->			Mapping[str, Any]:
							if self.task in ["default", "seq2seq-lm"]:
											a_		:  List[str]											=      self._generate_dummy_inputs_for_default_and_seqaseq_lm(
											    __SCREAMING_SNAKE_CASE  ,						batch_size=__SCREAMING_SNAKE_CASE  ,						seq_length=__SCREAMING_SNAKE_CASE  ,						is_pair=__SCREAMING_SNAKE_CASE  ,						framework=__SCREAMING_SNAKE_CASE					)

							elif self.task == "causal-lm":
											a_		:  Any											=      self._generate_dummy_inputs_for_causal_lm(
											    __SCREAMING_SNAKE_CASE  ,						batch_size=__SCREAMING_SNAKE_CASE  ,						seq_length=__SCREAMING_SNAKE_CASE  ,						is_pair=__SCREAMING_SNAKE_CASE  ,						framework=__SCREAMING_SNAKE_CASE					)
							else:
											a_		:  Any											=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
											    __SCREAMING_SNAKE_CASE  ,						batch_size=__SCREAMING_SNAKE_CASE  ,						seq_length=__SCREAMING_SNAKE_CASE  ,						is_pair=__SCREAMING_SNAKE_CASE  ,						framework=__SCREAMING_SNAKE_CASE					)

							return common_inputs







			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			List[str]					)			->			Optional[Any]:
							if self.task in ["default", "seq2seq-lm"]:
											a_		:  Union[str, Any]											=      super()._flatten_past_key_values_(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
							else:
											a_		:  Any											=      super(__SCREAMING_SNAKE_CASE  ,						self					)._flatten_past_key_values_(
											    __SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)












'''simple docstring'''







import torch

from transformers import AutoModel





class 	SCREAMING_SNAKE_CASE					(					torch.nn.Module       ):






			def __init__( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			int="sayef/fsner-bert-base-uncased"					)			->			str:
							super(__SCREAMING_SNAKE_CASE  ,						self					).__init__()

							a_		:  str											=      AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE  ,						return_dict=__SCREAMING_SNAKE_CASE					)
							a_		:  Optional[int]											=      torch.nn.CosineSimilarity(3  ,						1e-08					)
							a_		:  Dict											=      torch.nn.Softmax(dim=1					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						**__SCREAMING_SNAKE_CASE      :			int					)			->			str:
							return self.bert(**__SCREAMING_SNAKE_CASE					).last_hidden_state






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]					)			->			Optional[int]:
							return token_embeddings.sum(2  ,						keepdim=__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			int=1					)			->			Dict:
							return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]					)			->			Any:
							a_		:  Dict											=      W_supports['''sizes'''].tolist()
							a_		:  Tuple											=      W_supports['''start_token_id'''].item()
							a_		:  List[Any]											=      W_supports['''end_token_id'''].item()

							del W_supports["sizes"]
							del W_supports["start_token_id"]
							del W_supports["end_token_id"]

							a_		:  int											=      self.BERT(**__SCREAMING_SNAKE_CASE					)
							a_		:  Any											=      self.BERT(**__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[Any]											=      None
							a_		:  Tuple											=      None

							a_		:  List[str]											=      W_supports['''input_ids'''] == start_token_id
							a_		:  Dict											=      W_supports['''input_ids'''] == end_token_id

							for i, size in enumerate(__SCREAMING_SNAKE_CASE					):
											if i == 0:
															a_		:  str											=      0
											else:
															a_		:  str											=      support_sizes[i - 1]

											a_		:  Union[str, Any]											=      S[s : s + size][start_token_masks[s : s + size]]
											a_		:  Tuple											=      S[s : s + size][end_token_masks[s : s + size]]

											a_		:  Tuple											=      torch.matmul(q[i]  ,						s_start.T					).sum(1					).softmax(0					)
											a_		:  Optional[Any]											=      torch.matmul(q[i]  ,						s_end.T					).sum(1					).softmax(0					)

											if p_starts is not None:
															a_		:  Any											=      torch.vstack((p_starts, p_start)					)
															a_		:  Dict											=      torch.vstack((p_ends, p_end)					)
											else:
															a_		:  Optional[int]											=      p_start
															a_		:  List[Any]											=      p_end

							return p_starts, p_ends




