





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


_snake_case           =			logging.get_logger(__name__)

_snake_case           =			{
    """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json""",
    # See all Marian models at https://huggingface.co/models?filter=marian
}





class  lowerCAmelCase							(       _UpperCamelCase ):
  __lowerCamelCase		=      'marian'
  __lowerCamelCase		=      ['past_key_values']
  __lowerCamelCase		=      {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







  def __init__(      self  :List[str]   ,							_lowercase  :Union[str, Any]=5_81_01   ,							_lowercase  :Tuple=None   ,							_lowercase  :int=10_24   ,							_lowercase  :Tuple=12   ,							_lowercase  :Any=40_96   ,							_lowercase  :int=16   ,							_lowercase  :Any=12   ,							_lowercase  :Optional[Any]=40_96   ,							_lowercase  :List[str]=16   ,							_lowercase  :List[str]=0.0   ,							_lowercase  :Any=0.0   ,							_lowercase  :Dict=True   ,							_lowercase  :Union[str, Any]=True   ,							_lowercase  :str="gelu"   ,							_lowercase  :Any=10_24   ,							_lowercase  :Any=0.1   ,							_lowercase  :List[str]=0.0   ,							_lowercase  :List[Any]=0.0   ,							_lowercase  :Tuple=0.02   ,							_lowercase  :Optional[int]=5_81_00   ,							_lowercase  :Dict=False   ,							_lowercase  :str=5_81_00   ,							_lowercase  :Tuple=0   ,							_lowercase  :Union[str, Any]=0   ,							_lowercase  :List[Any]=True   ,							**_lowercase  :Any   ,							):






     '''simple docstring'''





     lowercase__			       =  vocab_size
     lowercase__			       =  decoder_vocab_size or vocab_size
     lowercase__			       =  max_position_embeddings
     lowercase__			       =  d_model
     lowercase__			       =  encoder_ffn_dim
     lowercase__			       =  encoder_layers
     lowercase__			       =  encoder_attention_heads
     lowercase__			       =  decoder_ffn_dim
     lowercase__			       =  decoder_layers
     lowercase__			       =  decoder_attention_heads
     lowercase__			       =  dropout
     lowercase__			       =  attention_dropout
     lowercase__			       =  activation_dropout
     lowercase__			       =  activation_function
     lowercase__			       =  init_std
     lowercase__			       =  encoder_layerdrop
     lowercase__			       =  decoder_layerdrop
     lowercase__			       =  use_cache
     lowercase__			       =  encoder_layers
     lowercase__			       =  scale_embedding  # scale factor will be sqrt(d_model) if True
     lowercase__			       =  share_encoder_decoder_embeddings
     super().__init__(
         pad_token_id=_UpperCAmelCase   ,							eos_token_id=_UpperCAmelCase   ,							is_encoder_decoder=_UpperCAmelCase   ,							decoder_start_token_id=_UpperCAmelCase   ,							forced_eos_token_id=_UpperCAmelCase   ,							**_UpperCAmelCase   ,							)





class  lowerCAmelCase							(       _UpperCamelCase ):







  @property
  # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
  def   UpperCAmelCase					(      self  :List[str] ):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
            ] )

        if self.use_past:
           lowercase__			       =  {0: 'batch'}
           lowercase__			       =  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
        else:
           lowercase__			       =  {0: 'batch', 1: 'decoder_sequence'}
           lowercase__			       =  {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.
        lowercase__			       =  OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
            ] )
        if self.use_past:
           lowercase__			       =  self.num_layers
           for i in range(_UpperCAmelCase ):
              lowercase__			       =  {0: 'batch', 2: 'past_sequence + sequence'}
              lowercase__			       =  {0: 'batch', 2: 'past_sequence + sequence'}
     else:
        lowercase__			       =  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
  # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  super().outputs
     else:
        lowercase__			       =  super(_UpperCAmelCase   ,							self ).outputs
        if self.use_past:
           lowercase__			       =  self.num_layers
           for i in range(_UpperCAmelCase ):
              lowercase__			       =  {0: 'batch', 2: 'past_sequence + sequence'}
              lowercase__			       =  {0: 'batch', 2: 'past_sequence + sequence'}
     return common_outputs







  def   UpperCAmelCase					(      self  :Any   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase )

     # Generate decoder inputs
     lowercase__			       =  seq_length if not self.use_past else 1
     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase )
     lowercase__			       =  {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
     lowercase__			       =  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
        lowercase__			       =  common_inputs['input_ids'].shape
        lowercase__			       =  common_inputs['decoder_input_ids'].shape[1]
        lowercase__			       =  self.num_attention_heads
        lowercase__			       =  (
            batch,
            num_encoder_attention_heads,
            encoder_seq_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )
        lowercase__			       =  decoder_seq_length + 3
        lowercase__			       =  (
            batch,
            num_decoder_attention_heads,
            decoder_past_length,
            self._config.hidden_size // num_decoder_attention_heads,
        )

        lowercase__			       =  torch.cat(
            [common_inputs["decoder_attention_mask"], torch.ones(_UpperCAmelCase   ,							_UpperCAmelCase )]   ,							dim=1 )

        lowercase__			       =  []
        # If the number of encoder and decoder layers are present in the model configuration, both are considered
        lowercase__			       =  self.num_layers
        lowercase__			       =  min(_UpperCAmelCase   ,							_UpperCAmelCase )
        lowercase__			       =  max(_UpperCAmelCase   ,							_UpperCAmelCase ) - min_num_layers
        lowercase__			       =  '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.
        lowercase__			       =  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   UpperCAmelCase					(      self  :Union[str, Any]   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _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
        lowercase__			       =  common_inputs['input_ids'].shape
        # Not using the same length for past_key_values
        lowercase__			       =  seqlen + 2
        lowercase__			       =  self.num_layers
        lowercase__			       =  self.num_attention_heads
        lowercase__			       =  (
            batch,
            num_encoder_attention_heads,
            past_key_values_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )

        lowercase__			       =  common_inputs['attention_mask'].dtype
        lowercase__			       =  torch.cat(
            [common_inputs["attention_mask"], torch.ones(_UpperCAmelCase   ,							_UpperCAmelCase   ,							dtype=_UpperCAmelCase )]   ,							dim=1 )
        lowercase__			       =  [
            (torch.zeros(_UpperCAmelCase ), torch.zeros(_UpperCAmelCase )) for _ in range(_UpperCAmelCase )
        ]
     return common_inputs







  def   UpperCAmelCase					(      self  :int   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  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
     lowercase__			       =  tokenizer.num_special_tokens_to_add(_UpperCAmelCase )
     lowercase__			       =  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
     lowercase__			       =  [' '.join([tokenizer.unk_token] ) * seq_length] * batch_size
     lowercase__			       =  dict(tokenizer(_UpperCAmelCase   ,							return_tensors=_UpperCAmelCase ) )
     return common_inputs







  def   UpperCAmelCase					(      self  :List[str]   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  self._generate_dummy_inputs_for_default_and_seqaseq_lm(
            _UpperCAmelCase   ,							batch_size=_UpperCAmelCase   ,							seq_length=_UpperCAmelCase   ,							is_pair=_UpperCAmelCase   ,							framework=_UpperCAmelCase )

     else:
        lowercase__			       =  self._generate_dummy_inputs_for_causal_lm(
            _UpperCAmelCase   ,							batch_size=_UpperCAmelCase   ,							seq_length=_UpperCAmelCase   ,							is_pair=_UpperCAmelCase   ,							framework=_UpperCAmelCase )

     return common_inputs







  def   UpperCAmelCase					(      self  :Tuple   ,							_lowercase  :Union[str, Any]   ,							_lowercase  :Any   ,							_lowercase  :int   ,							_lowercase  :List[str] ):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  super()._flatten_past_key_values_(_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase )
     else:
        lowercase__			       =  super(_UpperCAmelCase   ,							self )._flatten_past_key_values_(
            _UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase )







  @property
  def   UpperCAmelCase					(      self  :Optional[int] ):






     '''simple docstring'''





     return 1e-4













from __future__ import annotations

from collections import deque





class  lowerCAmelCase							:







  def __init__(      self  :List[Any]   ,							_lowercase  :list[str] ):






     '''simple docstring'''





     lowercase__			       =  []
     self.adlist.append(
         {"value": "", "next_states": [], "fail_state": 0, "output": []} )

     for keyword in keywords:
        self.add_keyword(_lowercase )
     self.set_fail_transitions()







  def   UpperCAmelCase					(      self  :str   ,							_lowercase  :int   ,							_lowercase  :str ):






     '''simple docstring'''





     for state in self.adlist[current_state]["next_states"]:
        if char == self.adlist[state]["value"]:
           return state
     return None







  def   UpperCAmelCase					(      self  :List[str]   ,							_lowercase  :str ):






     '''simple docstring'''





     lowercase__			       =  0
     for character in keyword:
        lowercase__			       =  self.find_next_state(_lowercase   ,							_lowercase )
        if next_state is None:
           self.adlist.append(
               {
                   "value": character,
                   "next_states": [],
                   "fail_state": 0,
                   "output": [],
               } )
           self.adlist[current_state]["next_states"].append(len(self.adlist ) - 1 )
           lowercase__			       =  len(self.adlist ) - 1
        else:
           lowercase__			       =  next_state
     self.adlist[current_state]["output"].append(_lowercase )







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     lowercase__			       =  deque()
     for node in self.adlist[0]["next_states"]:
        q.append(_lowercase )
        lowercase__			       =  0
     while q:
        lowercase__			       =  q.popleft()
        for child in self.adlist[r]["next_states"]:
           q.append(_lowercase )
           lowercase__			       =  self.adlist[r]["fail_state"]
           while (
               self.find_next_state(_lowercase   ,							self.adlist[child]["value"] ) is None
               and state != 0
           ):
              lowercase__			       =  self.adlist[state]["fail_state"]
           lowercase__			       =  self.find_next_state(
               _lowercase   ,							self.adlist[child]["value"] )
           if self.adlist[child]["fail_state"] is None:
              lowercase__			       =  0
           lowercase__			       =  (
               self.adlist[child]["output"]
               + self.adlist[self.adlist[child]["fail_state"]]["output"]
           )







  def   UpperCAmelCase					(      self  :Optional[Any]   ,							_lowercase  :str ):






     '''simple docstring'''





     lowercase__			       =  {}  # returns a dict with keywords and list of its occurrences
     lowercase__			       =  0
     for i in range(len(_lowercase ) ):
        while (
            self.find_next_state(_lowercase   ,							string[i] ) is None
            and current_state != 0
        ):
           lowercase__			       =  self.adlist[current_state]["fail_state"]
        lowercase__			       =  self.find_next_state(_lowercase   ,							string[i] )
        if next_state is None:
           lowercase__			       =  0
        else:
           lowercase__			       =  next_state
           for key in self.adlist[current_state]["output"]:
              if key not in result:
                 lowercase__			       =  []
              result[key].append(i - len(_lowercase ) + 1 )
     return result


if __name__ == "__main__":
  import doctest

  doctest.testmod()






