

from typing import Callable, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a									=  logging.get_logger(__name__)

_a									=  {
    '''microsoft/prophetnet-large-uncased''': (
        '''https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json'''
    ),
}






class 				A_					(						snake_case__   ):
     _lowercase     :	Optional[int]        =      'prophetnet'
     _lowercase     :	str        =      ['past_key_values']
     _lowercase     :	Optional[Any]        =      {
         'num_attention_heads': 'num_encoder_attention_heads',
     }


     def __init__(				self     :						Union[str, Any]     ,    UpperCAmelCase     :						Optional[float] = 0.1     ,    UpperCAmelCase     :						Optional[Union[str, Callable]] = "gelu"     ,    UpperCAmelCase     :						Optional[int] = 3_0_5_2_2     ,    UpperCAmelCase     :						Optional[int] = 1_0_2_4     ,    UpperCAmelCase     :						Optional[int] = 4_0_9_6     ,    UpperCAmelCase     :						Optional[int] = 1_2     ,    UpperCAmelCase     :						Optional[int] = 1_6     ,    UpperCAmelCase     :						Optional[int] = 4_0_9_6     ,    UpperCAmelCase     :						Optional[int] = 1_2     ,    UpperCAmelCase     :						Optional[int] = 1_6     ,    UpperCAmelCase     :						Optional[float] = 0.1     ,    UpperCAmelCase     :						Optional[float] = 0.1     ,    UpperCAmelCase     :						Optional[int] = 5_1_2     ,    UpperCAmelCase     :						Optional[float] = 0.02     ,    UpperCAmelCase     :						Optional[bool] = True     ,    UpperCAmelCase     :						Optional[bool] = True     ,    UpperCAmelCase     :						Optional[int] = 0     ,    UpperCAmelCase     :						Optional[int] = 2     ,    UpperCAmelCase     :						Optional[int] = 3_2     ,    UpperCAmelCase     :						Optional[int] = 1_2_8     ,    UpperCAmelCase     :						Optional[bool] = False     ,    UpperCAmelCase     :						Optional[float] = 0.0     ,    UpperCAmelCase     :						Optional[bool] = True     ,    UpperCAmelCase     :						Optional[int] = 0     ,    UpperCAmelCase     :						Optional[int] = 1     ,    UpperCAmelCase     :						Optional[int] = 2     ,    **UpperCAmelCase     :						Union[str, Any]     ,    )				->      List[str]:
      __lowerCAmelCase:  Optional[int]        = vocab_size
      __lowerCAmelCase:  Optional[Any]        = hidden_size
      __lowerCAmelCase:  Tuple        = encoder_ffn_dim
      __lowerCAmelCase:  Dict        = num_encoder_layers
      __lowerCAmelCase:  List[Any]        = num_encoder_attention_heads
      __lowerCAmelCase:  List[Any]        = decoder_ffn_dim
      __lowerCAmelCase:  Any        = num_decoder_layers
      __lowerCAmelCase:  Any        = num_decoder_attention_heads
      __lowerCAmelCase:  str        = max_position_embeddings
      __lowerCAmelCase:  Optional[int]        = init_std  # Normal(0, this parameter)
      __lowerCAmelCase:  Union[str, Any]        = activation_function

      # parameters for prophetnet
      __lowerCAmelCase:  Any        = ngram
      __lowerCAmelCase:  Tuple        = num_buckets
      __lowerCAmelCase:  Optional[Any]        = relative_max_distance
      __lowerCAmelCase:  List[str]        = disable_ngram_loss
      __lowerCAmelCase:  List[str]        = eps

      # 3 Types of Dropout
      __lowerCAmelCase:  List[str]        = attention_dropout
      __lowerCAmelCase:  Optional[Any]        = activation_dropout
      __lowerCAmelCase:  Optional[Any]        = dropout

      __lowerCAmelCase:  List[Any]        = use_cache

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


     @property
     def     UpperCAmelCase    (				self     :						Union[str, Any]   )				->      int:
      return self.num_encoder_layers + self.num_decoder_layers


     @num_hidden_layers.setter
     def     UpperCAmelCase    (				self     :						Tuple     ,    UpperCAmelCase     :						Dict   )				->      Any:
      raise NotImplementedError(
          'This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and'
          ' `num_decoder_layers`.'   )








from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve







def _a					(			SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     np.ndarray						)       ->       np.ndarray:

 """simple docstring"""






 __lowerCAmelCase:  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
 __lowerCAmelCase:  List[str]        = np.stack(
     np.meshgrid(np.arange(0       ,   image.shape[1]						)       ,   np.arange(0       ,   image.shape[0]						)						)       ,   2						)
 __lowerCAmelCase:  int        = np.round(grid - flow						).astype(np.intaa						)

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

 __lowerCAmelCase:  List[str]        = image[grid[:, :, 1], grid[:, :, 0]]

 # Set pixels at invalid locations to 0
 __lowerCAmelCase:  List[str]        = 0

 return warped







def _a					(			SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     SupportsIndex       ,   SCREAMING_SNAKE_CASE						:     float | None = None       ,   )       ->       tuple[np.ndarray, np.ndarray]:

 """simple docstring"""






 if alpha is None:
  __lowerCAmelCase:  Tuple        = 0.1

 # Initialize flow
 __lowerCAmelCase:  Optional[int]        = np.zeros_like(SCREAMING_SNAKE_CASE						)
 __lowerCAmelCase:  Dict        = np.zeros_like(SCREAMING_SNAKE_CASE						)

 # Prepare kernels for the calculation of the derivatives and the average velocity
 __lowerCAmelCase:  List[Any]        = np.array([[-1, 1], [-1, 1]]						) * 0.2_5
 __lowerCAmelCase:  int        = np.array([[-1, -1], [1, 1]]						) * 0.2_5
 __lowerCAmelCase:  Tuple        = np.array([[1, 1], [1, 1]]						) * 0.2_5
 __lowerCAmelCase:  Dict        = 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(SCREAMING_SNAKE_CASE						):
  __lowerCAmelCase:  Optional[int]        = warp(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  List[str]        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) + convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  int        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) + convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  List[Any]        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) + convolve(SCREAMING_SNAKE_CASE       ,   -kernel_t						)

  __lowerCAmelCase:  Any        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  Dict        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)

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

  __lowerCAmelCase:  List[Any]        = avg_horizontal_velocity - derivative_x * update
  __lowerCAmelCase:  Union[str, Any]        = avg_vertical_velocity - derivative_y * update

 return horizontal_flow, vertical_flow


if __name__ == "__main__":
  import doctest

  doctest.testmod()





