






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


A										=      logging.get_logger(__name__)

A										=      {
    'google/bigbird-pegasus-large-arxiv': (
        'https://huggingface.co/google/bigbird-pegasus-large-arxiv/resolve/main/config.json'
    ),
    'google/bigbird-pegasus-large-pubmed': (
        'https://huggingface.co/google/bigbird-pegasus-large-pubmed/resolve/main/config.json'
    ),
    'google/bigbird-pegasus-large-bigpatent': (
        'https://huggingface.co/google/bigbird-pegasus-large-bigpatent/resolve/main/config.json'
    ),
    # See all BigBirdPegasus models at https://huggingface.co/models?filter=bigbird_pegasus
}


class 				__a				(	__A    ):





		'''simple docstring'''



		UpperCAmelCase__   :					Optional[int]					     =    """bigbird_pegasus"""
		UpperCAmelCase__   :					int					     =    ["""past_key_values"""]
		UpperCAmelCase__   :					Tuple					     =    {
		    """num_attention_heads""": """encoder_attention_heads""",
		    """hidden_size""": """d_model""",
		    """attention_probs_dropout_prob""": """attention_dropout""",
		}


		def __init__(    self			,     UpperCamelCase__=96103			,     UpperCamelCase__=4096			,     UpperCamelCase__=16			,     UpperCamelCase__=4096			,     UpperCamelCase__=16			,     UpperCamelCase__=16			,     UpperCamelCase__=4096			,     UpperCamelCase__=16			,     UpperCamelCase__=0.0			,     UpperCamelCase__=0.0			,     UpperCamelCase__=True			,     UpperCamelCase__=True			,     UpperCamelCase__="gelu_new"			,     UpperCamelCase__=1024			,     UpperCamelCase__=0.1			,     UpperCamelCase__=0.0			,     UpperCamelCase__=0.0			,     UpperCamelCase__=0.02			,     UpperCamelCase__=2			,     UpperCamelCase__=0.0			,     UpperCamelCase__=True			,     UpperCamelCase__=0			,     UpperCamelCase__=2			,     UpperCamelCase__=1			,     UpperCamelCase__="block_sparse"			,     UpperCamelCase__=64			,     UpperCamelCase__=3			,     UpperCamelCase__=False			,     **UpperCamelCase__			,     ):
						SCREAMING_SNAKE_CASE_ :						Any    				= vocab_size
						SCREAMING_SNAKE_CASE_ :						Any    				= max_position_embeddings
						SCREAMING_SNAKE_CASE_ :						int    				= d_model
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= encoder_ffn_dim
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= encoder_layers
						SCREAMING_SNAKE_CASE_ :						Tuple    				= encoder_attention_heads
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= decoder_ffn_dim
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= decoder_layers
						SCREAMING_SNAKE_CASE_ :						int    				= decoder_attention_heads
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= dropout
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= attention_dropout
						SCREAMING_SNAKE_CASE_ :						Tuple    				= activation_dropout
						SCREAMING_SNAKE_CASE_ :						int    				= activation_function
						SCREAMING_SNAKE_CASE_ :						Tuple    				= init_std
						SCREAMING_SNAKE_CASE_ :						List[str]    				= encoder_layerdrop
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= decoder_layerdrop
						SCREAMING_SNAKE_CASE_ :						str    				= classifier_dropout
						SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= use_cache
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= encoder_layers
						SCREAMING_SNAKE_CASE_ :						Tuple    				= scale_embedding  # scale factor will be sqrt(d_model) if True

						# extra config
						SCREAMING_SNAKE_CASE_ :						Tuple    				= attention_type
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= block_size
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= num_random_blocks
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= use_bias

						super().__init__(
						    pad_token_id=UpperCamelCase__			,     bos_token_id=UpperCamelCase__			,     eos_token_id=UpperCamelCase__			,     is_encoder_decoder=UpperCamelCase__			,     decoder_start_token_id=UpperCamelCase__			,     **UpperCamelCase__			,     )






class 				__a				(	__A    ):





		'''simple docstring'''



		@property
		def 	__snake_case      (    self							):
						if self.task in ["default", "seq2seq-lm"]:
										SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= OrderedDict(
										    [
										        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
										        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
										    ]							)

										if self.use_past:
														SCREAMING_SNAKE_CASE_ :						Any    				= {0: 'batch'}
														SCREAMING_SNAKE_CASE_ :						Tuple    				= {0: 'batch', 1: 'past_decoder_sequence + sequence'}
										else:
														SCREAMING_SNAKE_CASE_ :						Dict    				= {0: 'batch', 1: 'decoder_sequence'}
														SCREAMING_SNAKE_CASE_ :						Tuple    				= {0: 'batch', 1: 'decoder_sequence'}

										if self.use_past:
														self.fill_with_past_key_values_(UpperCamelCase__			,     direction='inputs'							)
						elif self.task == "causal-lm":
										# TODO: figure this case out.
										SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= OrderedDict(
										    [
										        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
										        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
										    ]							)
										if self.use_past:
														SCREAMING_SNAKE_CASE_				,							SCREAMING_SNAKE_CASE_ :						Any    				= self.num_layers
														for i in range(UpperCamelCase__							):
																		SCREAMING_SNAKE_CASE_ :						List[Any]    				= {0: 'batch', 2: 'past_sequence + sequence'}
																		SCREAMING_SNAKE_CASE_ :						List[str]    				= {0: 'batch', 2: 'past_sequence + sequence'}
						else:
										SCREAMING_SNAKE_CASE_ :						Optional[int]    				= 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 	__snake_case      (    self							):
						if self.task in ["default", "seq2seq-lm"]:
										SCREAMING_SNAKE_CASE_ :						str    				= super().outputs
						else:
										SCREAMING_SNAKE_CASE_ :						List[Any]    				= super(UpperCamelCase__			,     self							).outputs
										if self.use_past:
														SCREAMING_SNAKE_CASE_				,							SCREAMING_SNAKE_CASE_ :						Dict    				= self.num_layers
														for i in range(UpperCamelCase__							):
																		SCREAMING_SNAKE_CASE_ :						Tuple    				= {0: 'batch', 2: 'past_sequence + sequence'}
																		SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= {0: 'batch', 2: 'past_sequence + sequence'}
						return common_outputs


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = False			,     UpperCamelCase__ = None			,     ):
						SCREAMING_SNAKE_CASE_ :						int    				= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__							)

						# Generate decoder inputs
						SCREAMING_SNAKE_CASE_ :						Tuple    				= seq_length if not self.use_past else 1
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						List[Any]    				= {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
						SCREAMING_SNAKE_CASE_ :						str    				= dict(**UpperCamelCase__			,     **UpperCamelCase__							)

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

										SCREAMING_SNAKE_CASE_ :						Any    				= torch.cat(
										    [common_inputs['decoder_attention_mask'], torch.ones(UpperCamelCase__			,     UpperCamelCase__							)]			,     dim=1							)

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

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


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = False			,     UpperCamelCase__ = None			,     ):
						SCREAMING_SNAKE_CASE_ :						Optional[int]    				= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__							)

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

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


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = False			,     UpperCamelCase__ = None			,     ):
						# 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
						SCREAMING_SNAKE_CASE_ :						str    				= compute_effective_axis_dimension(
						    UpperCamelCase__			,     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
						SCREAMING_SNAKE_CASE_ :						Dict    				= tokenizer.num_special_tokens_to_add(UpperCamelCase__							)
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= compute_effective_axis_dimension(
						    UpperCamelCase__			,     fixed_dimension=OnnxConfig.default_fixed_sequence			,     num_token_to_add=UpperCamelCase__							)

						# Generate dummy inputs according to compute batch and sequence
						SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= [' '.join([tokenizer.unk_token]							) * seq_length] * batch_size
						SCREAMING_SNAKE_CASE_ :						List[str]    				= dict(tokenizer(UpperCamelCase__			,     return_tensors=UpperCamelCase__							)							)
						return common_inputs


		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = -1			,     UpperCamelCase__ = False			,     UpperCamelCase__ = None			,     ):
						if self.task in ["default", "seq2seq-lm"]:
										SCREAMING_SNAKE_CASE_ :						Tuple    				= self._generate_dummy_inputs_for_default_and_seqaseq_lm(
										    UpperCamelCase__			,     batch_size=UpperCamelCase__			,     seq_length=UpperCamelCase__			,     is_pair=UpperCamelCase__			,     framework=UpperCamelCase__							)

						elif self.task == "causal-lm":
										SCREAMING_SNAKE_CASE_ :						Tuple    				= self._generate_dummy_inputs_for_causal_lm(
										    UpperCamelCase__			,     batch_size=UpperCamelCase__			,     seq_length=UpperCamelCase__			,     is_pair=UpperCamelCase__			,     framework=UpperCamelCase__							)
						else:
										SCREAMING_SNAKE_CASE_ :						str    				= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    UpperCamelCase__			,     batch_size=UpperCamelCase__			,     seq_length=UpperCamelCase__			,     is_pair=UpperCamelCase__			,     framework=UpperCamelCase__							)

						return common_inputs






		def 	__snake_case      (    self			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__							):
						if self.task in ["default", "seq2seq-lm"]:
										SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= super()._flatten_past_key_values_(UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__							)
						else:
										SCREAMING_SNAKE_CASE_ :						int    				= super(UpperCamelCase__			,     self							)._flatten_past_key_values_(
										    UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__			,     UpperCamelCase__							)







from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve



def        _lowerCamelCase(      lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray   ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_ :						List[Any]    				= np.stack((horizontal_flow, vertical_flow)     ,     2   )

				# Create a grid of all pixel coordinates and subtract the flow to get the
				# target pixels coordinates
				SCREAMING_SNAKE_CASE_ :						List[Any]    				= np.stack(
				    np.meshgrid(np.arange(0     ,     image.shape[1]   )     ,     np.arange(0     ,     image.shape[0]   )   )     ,     2   )
				SCREAMING_SNAKE_CASE_ :						List[str]    				= np.round(grid - flow   ).astype(np.intaa   )

				# Find the locations outside of the original image
				SCREAMING_SNAKE_CASE_ :						str    				= (grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]]   ))
				SCREAMING_SNAKE_CASE_ :						Dict    				= 0

				SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= image[grid[:, :, 1], grid[:, :, 0]]

				# Set pixels at invalid locations to 0
				SCREAMING_SNAKE_CASE_ :						Optional[int]    				= 0

				return warped



def        _lowerCamelCase(      lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		SupportsIndex     ,     lowerCAmelCase__					:		float | None = None     ,     ):





				'''simple docstring'''







				if alpha is None:
								SCREAMING_SNAKE_CASE_ :						Any    				= 0.1

				# Initialize flow
				SCREAMING_SNAKE_CASE_ :						List[Any]    				= np.zeros_like(lowerCAmelCase__   )
				SCREAMING_SNAKE_CASE_ :						Any    				= np.zeros_like(lowerCAmelCase__   )

				# Prepare kernels for the calculation of the derivatives and the average velocity
				SCREAMING_SNAKE_CASE_ :						Optional[int]    				= np.array([[-1, 1], [-1, 1]]   ) * 0.25
				SCREAMING_SNAKE_CASE_ :						Tuple    				= np.array([[-1, -1], [1, 1]]   ) * 0.25
				SCREAMING_SNAKE_CASE_ :						Optional[int]    				= np.array([[1, 1], [1, 1]]   ) * 0.25
				SCREAMING_SNAKE_CASE_ :						Tuple    				= np.array(
				    [[1 / 12, 1 / 6, 1 / 12], [1 / 6, 0, 1 / 6], [1 / 12, 1 / 6, 1 / 12]]   )

				# Iteratively refine the flow
				for _ in range(lowerCAmelCase__   ):
								SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= warp(lowerCAmelCase__     ,     lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Any    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   ) + convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   ) + convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   ) + convolve(lowerCAmelCase__     ,     -kernel_t   )

								SCREAMING_SNAKE_CASE_ :						List[str]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )

								# This updates the flow as proposed in the paper (Step 12)
								SCREAMING_SNAKE_CASE_ :						Tuple    				= (
								    derivative_x * avg_horizontal_velocity
								    + derivative_y * avg_vertical_velocity
								    + derivative_t
								)
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= update / (alpha**2 + derivative_x**2 + derivative_y**2)

								SCREAMING_SNAKE_CASE_ :						List[str]    				= avg_horizontal_velocity - derivative_x * update
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= avg_vertical_velocity - derivative_y * update

				return horizontal_flow, vertical_flow


if __name__ == "__main__":
				import doctest

				doctest.testmod()