




import os
from pickle import UnpicklingError
from typing import Dict, Tuple

import jax
import jax.numpy as jnp
import numpy as np
from flax.serialization import from_bytes
from flax.traverse_util import flatten_dict, unflatten_dict

import transformers

from .utils import logging


lowerCamelCase	:	List[Any]				=   logging.get_logger(__name__)




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase						,lowercase						,lowercase=False					)  ->       List[Any]:
   try:
      import torch  # noqa: F401
   except ImportError:
      logger.error(
          """Loading a PyTorch model in Flax, requires both PyTorch and Flax to be installed. Please see"""
          """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation"""
          """ instructions."""					)
      raise

   if not is_sharded:
      snake_case				:    int           =	os.path.abspath(lowerCAmelCase__					)
      logger.info(f"""Loading PyTorch weights from {pt_path}"""					)

      snake_case				:    Optional[int]           =	torch.load(lowerCAmelCase__						,map_location="""cpu"""					)
      logger.info(f"""PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values()					):,} parameters."""					)

      snake_case				:    Optional[Any]           =	convert_pytorch_state_dict_to_flax(lowerCAmelCase__						,lowerCAmelCase__					)
   else:
      # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files
      snake_case				:    List[str]           =	convert_pytorch_sharded_state_dict_to_flax(lowerCAmelCase__						,lowerCAmelCase__					)
   return flax_state_dict




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase						,lowercase						,lowercase						,)  ->       str:

   def is_key_or_prefix_key_in_dict(lowercase					) -> bool:
      return len(set(lowerCAmelCase__					) & {key, (model_prefix,) + key}					) > 0

   # layer norm
   snake_case				:    List[Any]           =	pt_tuple_key[:-1] + ("""scale""",)
   if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(lowerCAmelCase__					):
      return renamed_pt_tuple_key, pt_tensor

   # batch norm layer mean
   snake_case				:    List[str]           =	pt_tuple_key[:-1] + ("""mean""",)
   if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(lowerCAmelCase__					):
      return renamed_pt_tuple_key, pt_tensor

   # batch norm layer var
   snake_case				:    Tuple           =	pt_tuple_key[:-1] + ("""var""",)
   if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(lowerCAmelCase__					):
      return renamed_pt_tuple_key, pt_tensor

   # embedding
   snake_case				:    List[Any]           =	pt_tuple_key[:-1] + ("""embedding""",)
   if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(lowerCAmelCase__					):
      return renamed_pt_tuple_key, pt_tensor

   # conv layer
   snake_case				:    List[Any]           =	pt_tuple_key[:-1] + ("""kernel""",)
   if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(lowerCAmelCase__					):
      snake_case				:    Any           =	pt_tensor.transpose(2						,3						,1						,0					)
      return renamed_pt_tuple_key, pt_tensor

   # linear layer
   snake_case				:    Any           =	pt_tuple_key[:-1] + ("""kernel""",)
   if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(lowerCAmelCase__					):
      snake_case				:    List[str]           =	pt_tensor.T
      return renamed_pt_tuple_key, pt_tensor

   # old PyTorch layer norm weight
   snake_case				:    Optional[Any]           =	pt_tuple_key[:-1] + ("""weight""",)
   if pt_tuple_key[-1] == "gamma":
      return renamed_pt_tuple_key, pt_tensor

   # old PyTorch layer norm bias
   snake_case				:    Optional[int]           =	pt_tuple_key[:-1] + ("""bias""",)
   if pt_tuple_key[-1] == "beta":
      return renamed_pt_tuple_key, pt_tensor

   # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030
   snake_case				:    List[str]           =	None
   if pt_tuple_key[-3::2] == ("parametrizations", "original0"):
      snake_case				:    List[Any]           =	pt_tuple_key[-2] + """_g"""
   elif pt_tuple_key[-3::2] == ("parametrizations", "original1"):
      snake_case				:    Any           =	pt_tuple_key[-2] + """_v"""
   if name is not None:
      snake_case				:    Optional[int]           =	pt_tuple_key[:-3] + (name,)
      return renamed_pt_tuple_key, pt_tensor

   return pt_tuple_key, pt_tensor




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       Tuple:
   snake_case				:    str           =	{k: v.numpy() for k, v in pt_state_dict.items()}

   snake_case				:    Union[str, Any]           =	flax_model.base_model_prefix

   # use params dict if the model contains batch norm layers
   if "params" in flax_model.params:
      snake_case				:    Tuple           =	flax_model.params["""params"""]
   else:
      snake_case				:    List[str]           =	flax_model.params
   snake_case				:    Any           =	flatten_dict(lowerCAmelCase__					)

   # add batch_stats keys,values to dict
   if "batch_stats" in flax_model.params:
      snake_case				:    Optional[int]           =	flatten_dict(flax_model.params["""batch_stats"""]					)
      random_flax_state_dict.update(lowerCAmelCase__					)

   snake_case				:    List[str]           =	{}

   snake_case				:    Union[str, Any]           =	(model_prefix not in flax_model_params) and (
       model_prefix in {k.split("""."""					)[0] for k in pt_state_dict.keys()}
   )
   snake_case				:    Any           =	(model_prefix in flax_model_params) and (
       model_prefix not in {k.split("""."""					)[0] for k in pt_state_dict.keys()}
   )

   # Need to change some parameters name to match Flax names
   for pt_key, pt_tensor in pt_state_dict.items():
      snake_case				:    str           =	tuple(pt_key.split("""."""					)					)

      # remove base model prefix if necessary
      snake_case				:    Dict           =	pt_tuple_key[0] == model_prefix
      if load_model_with_head_into_base_model and has_base_model_prefix:
         snake_case				:    Any           =	pt_tuple_key[1:]

      # Correctly rename weight parameters
      snake_case     , snake_case				:    List[Any]           =	rename_key_and_reshape_tensor(
          lowerCAmelCase__						,lowerCAmelCase__						,lowerCAmelCase__						,lowerCAmelCase__					)

      # add model prefix if necessary
      snake_case				:    List[str]           =	(model_prefix,) + flax_key in random_flax_state_dict
      if load_base_model_into_model_with_head and require_base_model_prefix:
         snake_case				:    Any           =	(model_prefix,) + flax_key

      if flax_key in random_flax_state_dict:
         if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
            raise ValueError(
                f"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """
                f"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."""					)

        # add batch stats if the model contains batchnorm layers
      if "batch_stats" in flax_model.params:
         if "mean" in flax_key[-1] or "var" in flax_key[-1]:
            snake_case				:    int           =	jnp.asarray(lowerCAmelCase__					)
            continue
         # remove num_batches_tracked key
         if "num_batches_tracked" in flax_key[-1]:
            flax_state_dict.pop(lowerCAmelCase__						,lowerCAmelCase__					)
            continue

         # also add unexpected weight so that warning is thrown
         snake_case				:    List[str]           =	jnp.asarray(lowerCAmelCase__					)

      else:
         # also add unexpected weight so that warning is thrown
         snake_case				:    Any           =	jnp.asarray(lowerCAmelCase__					)

   return unflatten_dict(lowerCAmelCase__					)




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       Optional[Any]:
   import torch

   # Load the index
   snake_case				:    str           =	{}
   for shard_file in shard_filenames:
      # load using msgpack utils
      snake_case				:    Tuple           =	torch.load(lowerCAmelCase__					)
      snake_case				:    Optional[Any]           =	{k: v.numpy() for k, v in pt_state_dict.items()}

      snake_case				:    Optional[Any]           =	flax_model.base_model_prefix

      # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict
      if "batch_stats" in flax_model.params:
         snake_case				:    Optional[Any]           =	flax_model.params["""params"""]

         snake_case				:    str           =	flatten_dict(lowerCAmelCase__					)
         random_flax_state_dict.update(flatten_dict(flax_model.params["""batch_stats"""]					)					)
      else:
         snake_case				:    int           =	flax_model.params
         snake_case				:    Optional[Any]           =	flatten_dict(lowerCAmelCase__					)

      snake_case				:    str           =	(model_prefix not in flax_model_params) and (
          model_prefix in {k.split("""."""					)[0] for k in pt_state_dict.keys()}
      )
      snake_case				:    Optional[Any]           =	(model_prefix in flax_model_params) and (
          model_prefix not in {k.split("""."""					)[0] for k in pt_state_dict.keys()}
      )
      # Need to change some parameters name to match Flax names
      for pt_key, pt_tensor in pt_state_dict.items():
         snake_case				:    Tuple           =	tuple(pt_key.split("""."""					)					)

         # remove base model prefix if necessary
         snake_case				:    str           =	pt_tuple_key[0] == model_prefix
         if load_model_with_head_into_base_model and has_base_model_prefix:
            snake_case				:    Tuple           =	pt_tuple_key[1:]

         # Correctly rename weight parameters
         snake_case     , snake_case				:    int           =	rename_key_and_reshape_tensor(
             lowerCAmelCase__						,lowerCAmelCase__						,lowerCAmelCase__						,lowerCAmelCase__					)
         # add model prefix if necessary
         snake_case				:    Optional[int]           =	(model_prefix,) + flax_key in random_flax_state_dict
         if load_base_model_into_model_with_head and require_base_model_prefix:
            snake_case				:    Optional[Any]           =	(model_prefix,) + flax_key

         if flax_key in random_flax_state_dict:
            if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
               raise ValueError(
                   f"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """
                   f"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."""					)

            # add batch stats if the model contains batchnorm layers
         if "batch_stats" in flax_model.params:
            if "mean" in flax_key[-1]:
               snake_case				:    int           =	jnp.asarray(lowerCAmelCase__					)
               continue
            if "var" in flax_key[-1]:
               snake_case				:    List[Any]           =	jnp.asarray(lowerCAmelCase__					)
               continue
            # remove num_batches_tracked key
            if "num_batches_tracked" in flax_key[-1]:
               flax_state_dict.pop(lowerCAmelCase__						,lowerCAmelCase__					)
               continue

            # also add unexpected weight so that warning is thrown
            snake_case				:    Union[str, Any]           =	jnp.asarray(lowerCAmelCase__					)

         else:
            # also add unexpected weight so that warning is thrown
            snake_case				:    Tuple           =	jnp.asarray(lowerCAmelCase__					)
   return unflatten_dict(lowerCAmelCase__					)




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       Optional[Any]:
   snake_case				:    int           =	os.path.abspath(lowerCAmelCase__					)
   logger.info(f"""Loading Flax weights from {flax_checkpoint_path}"""					)

   # import correct flax class
   snake_case				:    Any           =	getattr(lowerCAmelCase__						,"""Flax""" + model.__class__.__name__					)

   # load flax weight dict
   with open(lowerCAmelCase__						,"""rb"""					) as state_f:
      try:
         snake_case				:    List[Any]           =	from_bytes(lowerCAmelCase__						,state_f.read()					)
      except UnpicklingError:
         raise EnvironmentError(f"""Unable to convert {flax_checkpoint_path} to Flax deserializable object. """					)

   return load_flax_weights_in_pytorch_model(lowerCAmelCase__						,lowerCAmelCase__					)




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       Optional[int]:

   try:
      import torch  # noqa: F401
   except ImportError:
      logger.error(
          """Loading a Flax weights in PyTorch, requires both PyTorch and Flax to be installed. Please see"""
          """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation"""
          """ instructions."""					)
      raise

   # check if we have bf16 weights
   snake_case				:    List[str]           =	flatten_dict(jax.tree_util.tree_map(lambda lowercase					: x.dtype == jnp.bfloataa						,lowerCAmelCase__					)					).values()
   if any(lowerCAmelCase__					):
      # convert all weights to fp32 if the are bf16 since torch.from_numpy can-not handle bf16
      # and bf16 is not fully supported in PT yet.
      logger.warning(
          """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """
          """before loading those in PyTorch model."""					)
      snake_case				:    Dict           =	jax.tree_util.tree_map(
          lambda lowercase					: params.astype(np.floataa					) if params.dtype == jnp.bfloataa else params						,lowerCAmelCase__					)

   snake_case				:    int           =	flatten_dict(lowerCAmelCase__					)
   snake_case				:    int           =	pt_model.state_dict()

   snake_case				:    str           =	(pt_model.base_model_prefix in flax_state) and (
       pt_model.base_model_prefix not in {k.split("""."""					)[0] for k in pt_model_dict.keys()}
   )
   snake_case				:    Optional[int]           =	(pt_model.base_model_prefix not in flax_state) and (
       pt_model.base_model_prefix in {k.split("""."""					)[0] for k in pt_model_dict.keys()}
   )

   # keep track of unexpected & missing keys
   snake_case				:    Tuple           =	[]
   snake_case				:    Any           =	set(pt_model_dict.keys()					)

   for flax_key_tuple, flax_tensor in flax_state_dict.items():
      snake_case				:    List[Any]           =	flax_key_tuple[0] == pt_model.base_model_prefix
      snake_case				:    Optional[Any]           =	""".""".join((pt_model.base_model_prefix,) + flax_key_tuple					) in pt_model_dict

      # adapt flax_key to prepare for loading from/to base model only
      if load_model_with_head_into_base_model and has_base_model_prefix:
         snake_case				:    Any           =	flax_key_tuple[1:]
      elif load_base_model_into_model_with_head and require_base_model_prefix:
         snake_case				:    Union[str, Any]           =	(pt_model.base_model_prefix,) + flax_key_tuple

      # rename flax weights to PyTorch format
      if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(lowerCAmelCase__					) not in pt_model_dict:
         # conv layer
         snake_case				:    Optional[Any]           =	flax_key_tuple[:-1] + ("""weight""",)
         snake_case				:    Dict           =	jnp.transpose(lowerCAmelCase__						,(3, 2, 0, 1)					)
      elif flax_key_tuple[-1] == "kernel" and ".".join(lowerCAmelCase__					) not in pt_model_dict:
         # linear layer
         snake_case				:    str           =	flax_key_tuple[:-1] + ("""weight""",)
         snake_case				:    str           =	flax_tensor.T
      elif flax_key_tuple[-1] in ["scale", "embedding"]:
         snake_case				:    List[Any]           =	flax_key_tuple[:-1] + ("""weight""",)

      # adding batch stats from flax batch norm to pt
      elif "mean" in flax_key_tuple[-1]:
         snake_case				:    Optional[int]           =	flax_key_tuple[:-1] + ("""running_mean""",)
      elif "var" in flax_key_tuple[-1]:
         snake_case				:    Optional[int]           =	flax_key_tuple[:-1] + ("""running_var""",)

      if "batch_stats" in flax_state:
         snake_case				:    Dict           =	""".""".join(flax_key_tuple[1:]					)  # Remove the params/batch_stats header
      else:
         snake_case				:    List[str]           =	""".""".join(lowerCAmelCase__					)

      # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation.
      snake_case				:    Dict           =	{}
      # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030
      for key in pt_model_dict:
         snake_case				:    int           =	key.split("""."""					)
         snake_case				:    List[str]           =	None
         if key_components[-3::2] == ["parametrizations", "original0"]:
            snake_case				:    List[str]           =	key_components[-2] + """_g"""
         elif key_components[-3::2] == ["parametrizations", "original1"]:
            snake_case				:    Dict           =	key_components[-2] + """_v"""
         if name is not None:
            snake_case				:    int           =	key_components[:-3] + [name]
            snake_case				:    int           =	""".""".join(lowerCAmelCase__					)
            snake_case				:    int           =	key

      if flax_key in special_pt_names:
         snake_case				:    Optional[Any]           =	special_pt_names[flax_key]

      if flax_key in pt_model_dict:
         if flax_tensor.shape != pt_model_dict[flax_key].shape:
            raise ValueError(
                f"""Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected """
                f"""to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}."""					)
         else:
            # add weight to pytorch dict
            snake_case				:    str           =	np.asarray(lowerCAmelCase__					) if not isinstance(lowerCAmelCase__						,np.ndarray					) else flax_tensor
            snake_case				:    Optional[Any]           =	torch.from_numpy(lowerCAmelCase__					)
            # remove from missing keys
            missing_keys.remove(lowerCAmelCase__					)
      else:
         # weight is not expected by PyTorch model
         unexpected_keys.append(lowerCAmelCase__					)

   pt_model.load_state_dict(lowerCAmelCase__					)

   # re-transform missing_keys to list
   snake_case				:    List[Any]           =	list(lowerCAmelCase__					)

   if len(lowerCAmelCase__					) > 0:
      logger.warning(
          """Some weights of the Flax model were not used when initializing the PyTorch model"""
          f""" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing"""
          f""" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture"""
          """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This"""
          f""" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect"""
          """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a"""
          """ FlaxBertForSequenceClassification model)."""					)
   else:
      logger.warning(f"""All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n"""					)
   if len(lowerCAmelCase__					) > 0:
      logger.warning(
          f"""Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly"""
          f""" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to"""
          """ use it for predictions and inference."""					)
   else:
      logger.warning(
          f"""All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n"""
          """If your task is similar to the task the model of the checkpoint was trained on, """
          f"""you can already use {pt_model.__class__.__name__} for predictions without further training."""					)

   return pt_model





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	:	Optional[Any]				=   logging.get_logger(__name__)

lowerCamelCase	:	Optional[int]				=   {
    '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 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      _snake_case												=						"""bart"""
      _snake_case												=						["""past_key_values"""]
      _snake_case												=						{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

      def __init__(				self    ,				A=5_0_2_6_5    ,				A=1_0_2_4    ,				A=1_2    ,				A=4_0_9_6    ,				A=1_6    ,				A=1_2    ,				A=4_0_9_6    ,				A=1_6    ,				A=0.0    ,				A=0.0    ,				A="gelu"    ,				A=1_0_2_4    ,				A=0.1    ,				A=0.0    ,				A=0.0    ,				A=0.02    ,				A=0.0    ,				A=False    ,				A=True    ,				A=3    ,				A=1    ,				A=0    ,				A=2    ,				A=True    ,				A=2    ,				A=2    ,				**A    ,				)		->    Any:
         snake_case				:    Optional[int]           =	vocab_size
         snake_case				:    Union[str, Any]           =	max_position_embeddings
         snake_case				:    List[str]           =	d_model
         snake_case				:    List[Any]           =	encoder_ffn_dim
         snake_case				:    Optional[Any]           =	encoder_layers
         snake_case				:    Union[str, Any]           =	encoder_attention_heads
         snake_case				:    str           =	decoder_ffn_dim
         snake_case				:    Union[str, Any]           =	decoder_layers
         snake_case				:    Any           =	decoder_attention_heads
         snake_case				:    Union[str, Any]           =	dropout
         snake_case				:    List[str]           =	attention_dropout
         snake_case				:    List[Any]           =	activation_dropout
         snake_case				:    Optional[int]           =	activation_function
         snake_case				:    Union[str, Any]           =	init_std
         snake_case				:    List[str]           =	encoder_layerdrop
         snake_case				:    int           =	decoder_layerdrop
         snake_case				:    str           =	classifier_dropout
         snake_case				:    List[str]           =	use_cache
         snake_case				:    Tuple           =	encoder_layers
         snake_case				:    int           =	scale_embedding  # scale factor will be sqrt(d_model) if True

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

         # ensure backward compatibility for BART CNN models
         if self.forced_bos_token_id is None and kwargs.get("""force_bos_token_to_be_generated"""    ,				A     ):
            snake_case				:    Any           =	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 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





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

            if self.use_past:
               snake_case				:    Tuple           =	{0: """batch"""}
               snake_case				:    List[Any]           =	{0: """batch""", 1: """past_decoder_sequence + sequence"""}
            else:
               snake_case				:    Union[str, Any]           =	{0: """batch""", 1: """decoder_sequence"""}
               snake_case				:    Any           =	{0: """batch""", 1: """decoder_sequence"""}

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

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = None    ,				)		->    Mapping[str, Any]:
         snake_case				:    Optional[Any]           =	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             A    ,				A    ,				A    ,				A    ,				A     )

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

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

            snake_case				:    str           =	torch.cat(
                [common_inputs["""decoder_attention_mask"""], torch.ones(A    ,				A     )]    ,				dim=1     )

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

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

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = None    ,				)		->    Mapping[str, Any]:
         snake_case				:    str           =	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             A    ,				A    ,				A    ,				A    ,				A     )

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

            snake_case				:    Optional[Any]           =	common_inputs["""attention_mask"""].dtype
            snake_case				:    int           =	torch.cat(
                [common_inputs["""attention_mask"""], torch.ones(A    ,				A    ,				dtype=A     )]    ,				dim=1     )
            snake_case				:    Union[str, Any]           =	[
                (torch.zeros(A     ), torch.zeros(A     )) for _ in range(A     )
            ]
         return common_inputs

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = 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
         snake_case				:    int           =	compute_effective_axis_dimension(
             A    ,				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
         snake_case				:    int           =	tokenizer.num_special_tokens_to_add(A     )
         snake_case				:    Tuple           =	compute_effective_axis_dimension(
             A    ,				fixed_dimension=OnnxConfig.default_fixed_sequence    ,				num_token_to_add=A     )

         # Generate dummy inputs according to compute batch and sequence
         snake_case				:    int           =	[""" """.join([tokenizer.unk_token]     ) * seq_length] * batch_size
         snake_case				:    str           =	dict(tokenizer(A    ,				return_tensors=A     )     )
         return common_inputs

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = None    ,				)		->    Mapping[str, Any]:
         if self.task in ["default", "seq2seq-lm"]:
            snake_case				:    Optional[Any]           =	self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                A    ,				batch_size=A    ,				seq_length=A    ,				is_pair=A    ,				framework=A     )

         elif self.task == "causal-lm":
            snake_case				:    Optional[int]           =	self._generate_dummy_inputs_for_causal_lm(
                A    ,				batch_size=A    ,				seq_length=A    ,				is_pair=A    ,				framework=A     )
         else:
            snake_case				:    List[Any]           =	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                A    ,				batch_size=A    ,				seq_length=A    ,				is_pair=A    ,				framework=A     )

         return common_inputs

      def      UpperCAmelCase				(				self    ,				A    ,				A    ,				A    ,				A     )		->    Union[str, Any]:
         if self.task in ["default", "seq2seq-lm"]:
            snake_case				:    Optional[Any]           =	super()._flatten_past_key_values_(A    ,				A    ,				A    ,				A     )
         else:
            snake_case				:    Union[str, Any]           =	super(A    ,				self     )._flatten_past_key_values_(
                A    ,				A    ,				A    ,				A     )





