





"""simple docstring"""






import inspect

import jax
import jax.lax as lax
import jax.numpy as jnp

from ..utils import add_start_docstrings
from ..utils.logging import get_logger


A			      =	get_logger(__name__)


A			      =	r'''
    Args:
        input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):
            Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam
            search or log softmax for each vocabulary token when using beam search
        kwargs (`Dict[str, Any]`, *optional*):
            Additional logits processor specific kwargs.

    Return:
        `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.

'''




class 					__lowercase      :







					'''simple docstring'''




					@add_start_docstrings(_UpperCAmelCase  )
					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""  )






class 					__lowercase      :







					'''simple docstring'''




					@add_start_docstrings(_UpperCAmelCase  )
					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									raise NotImplementedError(
									    f"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""  )






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					@add_start_docstrings(_UpperCAmelCase  )
					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase						,      **_UpperCAmelCase  ):
									for processor in self:
													__a			:    int      							=	inspect.signature(processor.__call__  ).parameters
													if len(_UpperCAmelCase  ) > 3:
																	if not all(arg in kwargs for arg in list(function_args.keys()  )[2:]  ):
																					raise ValueError(
																					    f"""Make sure that all the required parameters: {list(function_args.keys()  )} for """
																					    f"""{processor.__class__} are passed to the logits processor."""  )
																	__a			:    Optional[Any]      							=	processor(_UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase						,      **_UpperCAmelCase  )
													else:
																	__a			:    Union[str, Any]      							=	processor(_UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  )
									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase  ):
									if not isinstance(_UpperCAmelCase						,      _UpperCAmelCase  ) or not (temperature > 0):
													raise ValueError(f"""`temperature` has to be a strictly positive float, but is {temperature}"""  )

									__a			:    int      							=	temperature







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    Tuple      							=	scores / self.temperature
									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase = -float('''Inf'''  )						,      _UpperCAmelCase = 1  ):
									if not isinstance(_UpperCAmelCase						,      _UpperCAmelCase  ) or (top_p < 0 or top_p > 1.0):
													raise ValueError(f"""`top_p` has to be a float > 0 and < 1, but is {top_p}"""  )
									if not isinstance(_UpperCAmelCase						,      _UpperCAmelCase  ) or (min_tokens_to_keep < 1):
													raise ValueError(f"""`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}"""  )

									__a			:    List[str]      							=	top_p
									__a			:    str      							=	filter_value
									__a			:    List[str]      							=	min_tokens_to_keep







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a							,    __a			:    str      							=	lax.top_k(_UpperCAmelCase						,      scores.shape[-1]  )

									__a			:    Any      							=	jnp.full_like(_UpperCAmelCase						,      self.filter_value  )
									__a			:    List[Any]      							=	jax.nn.softmax(_UpperCAmelCase						,      axis=-1  ).cumsum(axis=-1  )
									__a			:    int      							=	cumulative_probs < self.top_p

									# include the token that is higher than top_p as well
									__a			:    Any      							=	jnp.roll(_UpperCAmelCase						,      1  )
									score_mask |= score_mask.at[:, 0].set(_UpperCAmelCase  )

									# min tokens to keep
									__a			:    List[str]      							=	score_mask.at[:, : self.min_tokens_to_keep].set(_UpperCAmelCase  )

									__a			:    Tuple      							=	jnp.where(_UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  )
									__a			:    Any      							=	jax.lax.sort_key_val(_UpperCAmelCase						,      _UpperCAmelCase  )[-1]

									return next_scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase = -float('''Inf'''  )						,      _UpperCAmelCase = 1  ):
									if not isinstance(_UpperCAmelCase						,      _UpperCAmelCase  ) or top_k <= 0:
													raise ValueError(f"""`top_k` has to be a strictly positive integer, but is {top_k}"""  )

									__a			:    Any      							=	max(_UpperCAmelCase						,      _UpperCAmelCase  )
									__a			:    int      							=	filter_value







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a							,    __a			:    Optional[int]      							=	scores.shape
									__a			:    Optional[Any]      							=	jnp.full(batch_size * vocab_size						,      self.filter_value  )

									__a			:    Any      							=	min(self.top_k						,      scores.shape[-1]  )  # Safety check
									__a							,    __a			:    str      							=	lax.top_k(_UpperCAmelCase						,      _UpperCAmelCase  )
									__a			:    Tuple      							=	jnp.broadcast_to((jnp.arange(_UpperCAmelCase  ) * vocab_size)[:, None]						,      (batch_size, topk)  ).flatten()
									__a			:    Dict      							=	topk_scores.flatten()
									__a			:    Optional[Any]      							=	topk_indices.flatten() + shift

									__a			:    Tuple      							=	next_scores_flat.at[topk_indices_flat].set(_UpperCAmelCase  )
									__a			:    Any      							=	next_scores_flat.reshape(_UpperCAmelCase						,      _UpperCAmelCase  )
									return next_scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase  ):
									__a			:    int      							=	bos_token_id







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    Dict      							=	jnp.full(scores.shape						,      -float('''inf'''  )  )

									__a			:    str      							=	1 - jnp.bool_(cur_len - 1  )

									__a			:    Tuple      							=	jnp.where(_UpperCAmelCase						,      new_scores.at[:, self.bos_token_id].set(0  )						,      _UpperCAmelCase  )

									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    List[Any]      							=	max_length
									__a			:    Optional[int]      							=	eos_token_id







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    List[Any]      							=	jnp.full(scores.shape						,      -float('''inf'''  )  )

									__a			:    Optional[int]      							=	1 - jnp.bool_(cur_len - self.max_length + 1  )

									__a			:    Tuple      							=	jnp.where(_UpperCAmelCase						,      new_scores.at[:, self.eos_token_id].set(0  )						,      _UpperCAmelCase  )

									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									if not isinstance(_UpperCAmelCase						,      _UpperCAmelCase  ) or min_length < 0:
													raise ValueError(f"""`min_length` has to be a positive integer, but is {min_length}"""  )

									if not isinstance(_UpperCAmelCase						,      _UpperCAmelCase  ) or eos_token_id < 0:
													raise ValueError(f"""`eos_token_id` has to be a positive integer, but is {eos_token_id}"""  )

									__a			:    Union[str, Any]      							=	min_length
									__a			:    int      							=	eos_token_id







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									# create boolean flag to decide if min length penalty should be applied
									__a			:    Dict      							=	1 - jnp.clip(cur_len - self.min_length						,      0						,      1  )

									__a			:    Dict      							=	jnp.where(_UpperCAmelCase						,      scores.at[:, self.eos_token_id].set(-float('''inf'''  )  )						,      _UpperCAmelCase  )

									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    int      							=	list(_UpperCAmelCase  )
									__a			:    List[Any]      							=	begin_index







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    List[Any]      							=	1 - jnp.bool_(cur_len - self.begin_index  )

									__a			:    int      							=	jnp.where(_UpperCAmelCase						,      scores.at[:, self.begin_suppress_tokens].set(-float('''inf'''  )  )						,      _UpperCAmelCase  )

									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase  ):
									__a			:    Union[str, Any]      							=	list(_UpperCAmelCase  )







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    Dict      							=	scores.at[..., self.suppress_tokens].set(-float('''inf'''  )  )

									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase  ):
									__a			:    Optional[int]      							=	dict(_UpperCAmelCase  )
									# Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the
									# index of the array corresponds to the index of the token to be forced, for XLA compatibility.
									# Indexes without forced tokens will have a negative value.
									__a			:    Optional[int]      							=	jnp.ones((max(force_token_map.keys()  ) + 1)						,      dtype=jnp.intaa  ) * -1
									for index, token in force_token_map.items():
													if token is not None:
																	__a			:    Optional[int]      							=	force_token_array.at[index].set(_UpperCAmelCase  )
									__a			:    List[Any]      							=	jnp.intaa(_UpperCAmelCase  )







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									def _force_token(_UpperCAmelCase  ):
													__a			:    Tuple      							=	scores.shape[0]
													__a			:    Tuple      							=	self.force_token_array[generation_idx]

													__a			:    int      							=	jnp.ones_like(_UpperCAmelCase						,      dtype=scores.dtype  ) * -float('''inf'''  )
													__a			:    int      							=	jnp.zeros((batch_size, 1)						,      dtype=scores.dtype  )
													__a			:    Dict      							=	lax.dynamic_update_slice(_UpperCAmelCase						,      _UpperCAmelCase						,      (0, current_token)  )
													return new_scores

									__a			:    Union[str, Any]      							=	lax.cond(
									    cur_len >= self.force_token_array.shape[0]						,      lambda: scores						,      lambda: lax.cond(
									        self.force_token_array[cur_len] >= 0						,      lambda: _force_token(_UpperCAmelCase  )						,      lambda: scores						,      )						,      )
									return scores






class 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




					def __init__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									__a			:    Union[str, Any]      							=	generate_config.eos_token_id
									__a			:    Dict      							=	generate_config.no_timestamps_token_id
									__a			:    Optional[int]      							=	generate_config.no_timestamps_token_id + 1

									__a			:    List[Any]      							=	decoder_input_length + 1

									if generate_config.is_multilingual:
													# room for language token and task token
													self.begin_index += 2
									if hasattr(_UpperCAmelCase						,      '''max_initial_timestamp_index'''  ):
													__a			:    List[Any]      							=	generate_config.max_initial_timestamp_index
									else:
													__a			:    List[str]      							=	model_config.vocab_size
									if self.max_initial_timestamp_index is None:
													__a			:    Tuple      							=	model_config.vocab_size







					def __call__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase  ):
									# suppress <|notimestamps|> which is handled by without_timestamps
									__a			:    int      							=	scores.at[:, self.no_timestamps_token_id].set(-float('''inf'''  )  )

									def handle_pairs(_UpperCAmelCase						,      _UpperCAmelCase  ):
													__a			:    Any      							=	jnp.where((cur_len - self.begin_index) >= 1						,      _UpperCAmelCase						,      _UpperCAmelCase  )
													__a			:    int      							=	jnp.where(
													    input_ids_k[cur_len - 1] >= self.timestamp_begin						,      True and last_was_timestamp						,      _UpperCAmelCase						,      )

													__a			:    List[Any]      							=	jnp.where((cur_len - self.begin_index) < 2						,      _UpperCAmelCase						,      _UpperCAmelCase  )
													__a			:    Optional[Any]      							=	jnp.where(
													    input_ids_k[cur_len - 2] >= self.timestamp_begin						,      _UpperCAmelCase						,      _UpperCAmelCase						,      )

													return jnp.where(
													    _UpperCAmelCase						,      jnp.where(
													        penultimate_was_timestamp > 0						,      scores_k.at[self.timestamp_begin :].set(-float('''inf'''  )  )						,      scores_k.at[: self.eos_token_id].set(-float('''inf'''  )  )						,      )						,      _UpperCAmelCase						,      )

									__a			:    Optional[int]      							=	jax.vmap(_UpperCAmelCase  )(_UpperCAmelCase						,      _UpperCAmelCase  )

									__a			:    int      							=	jnp.where(cur_len == self.begin_index						,      _UpperCAmelCase						,      _UpperCAmelCase  )
									__a			:    Union[str, Any]      							=	jnp.where(
									    self.max_initial_timestamp_index is not None						,      True and apply_max_initial_timestamp						,      _UpperCAmelCase						,      )

									__a			:    List[str]      							=	self.timestamp_begin + self.max_initial_timestamp_index

									__a			:    Optional[int]      							=	jnp.where(
									    _UpperCAmelCase						,      scores.at[:, last_allowed + 1 :].set(-float('''inf'''  )  )						,      _UpperCAmelCase						,      )

									# if sum of probability over timestamps is above any other token, sample timestamp
									__a			:    Optional[Any]      							=	jax.nn.log_softmax(_UpperCAmelCase						,      axis=-1  )

									def handle_cumulative_probs(_UpperCAmelCase						,      _UpperCAmelCase  ):
													__a			:    Any      							=	jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]						,      axis=-1  )
													__a			:    Tuple      							=	jnp.max(logprobs_k[: self.timestamp_begin]  )
													return jnp.where(
													    timestamp_logprob > max_text_token_logprob						,      scores_k.at[: self.timestamp_begin].set(-float('''inf'''  )  )						,      _UpperCAmelCase						,      )

									__a			:    Dict      							=	jax.vmap(_UpperCAmelCase  )(_UpperCAmelCase						,      _UpperCAmelCase  )

									return scores
from __future__ import annotations

import unittest

from transformers import LEDConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFLEDForConditionalGeneration, TFLEDModel




@require_tf
class 			__magic_name__	:





   """simple docstring"""




   lowerCAmelCase   :     Optional[int]	      = LEDConfig
   lowerCAmelCase   :     List[str]	      = {}
   lowerCAmelCase   :     str	      = '''gelu'''



   def __init__(						self       :	int		,	_lowercase       :	Tuple		,	_lowercase       :	Dict=13		,	_lowercase       :	Union[str, Any]=7		,	_lowercase       :	Optional[int]=True		,	_lowercase       :	Any=False		,	_lowercase       :	Optional[int]=99		,	_lowercase       :	Dict=32		,	_lowercase       :	Optional[Any]=2		,	_lowercase       :	Union[str, Any]=4		,	_lowercase       :	List[str]=37		,	_lowercase       :	Optional[Any]=0.1		,	_lowercase       :	List[Any]=0.1		,	_lowercase       :	Union[str, Any]=20		,	_lowercase       :	Tuple=2		,	_lowercase       :	str=1		,	_lowercase       :	Optional[int]=0		,	_lowercase       :	List[str]=4		,	):



    """simple docstring"""



    _UpperCamelCase:  Optional[Any]    						=			parent
    _UpperCamelCase:  Union[str, Any]    						=			batch_size
    _UpperCamelCase:  Optional[Any]    						=			seq_length
    _UpperCamelCase:  str    						=			is_training
    _UpperCamelCase:  Dict    						=			use_labels
    _UpperCamelCase:  List[Any]    						=			vocab_size
    _UpperCamelCase:  Dict    						=			hidden_size
    _UpperCamelCase:  int    						=			num_hidden_layers
    _UpperCamelCase:  Dict    						=			num_attention_heads
    _UpperCamelCase:  Dict    						=			intermediate_size

    _UpperCamelCase:  Union[str, Any]    						=			hidden_dropout_prob
    _UpperCamelCase:  Any    						=			attention_probs_dropout_prob
    _UpperCamelCase:  Optional[Any]    						=			max_position_embeddings
    _UpperCamelCase:  Union[str, Any]    						=			eos_token_id
    _UpperCamelCase:  Union[str, Any]    						=			pad_token_id
    _UpperCamelCase:  Optional[Any]    						=			bos_token_id
    _UpperCamelCase:  Optional[Any]    						=			attention_window

    # `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size
    # [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention
    # returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1]
    # because its local attention only attends to `self.attention_window` and one before and one after
    _UpperCamelCase:  Any    						=			self.attention_window + 2

    # because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for
    # the `test_attention_outputs` and `test_hidden_states_output` tests
    _UpperCamelCase:  Tuple    						=			(
        self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
    )



   def 	lowerCAmelCase    (						self       :	int				):



    """simple docstring"""



    _UpperCamelCase:  Union[str, Any]    						=			ids_tensor([self.batch_size, self.seq_length - 1]		,	self.vocab_size				)
    _UpperCamelCase:  Union[str, Any]    						=			tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size				)		,	1				)
    _UpperCamelCase:  Union[str, Any]    						=			tf.concat([input_ids, eos_tensor]		,	axis=1				)

    _UpperCamelCase:  Union[str, Any]    						=			ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size				)

    _UpperCamelCase:  Any    						=			self.config_cls(
        vocab_size=self.vocab_size		,	d_model=self.hidden_size		,	encoder_layers=self.num_hidden_layers		,	decoder_layers=self.num_hidden_layers		,	encoder_attention_heads=self.num_attention_heads		,	decoder_attention_heads=self.num_attention_heads		,	encoder_ffn_dim=self.intermediate_size		,	decoder_ffn_dim=self.intermediate_size		,	dropout=self.hidden_dropout_prob		,	attention_dropout=self.attention_probs_dropout_prob		,	max_position_embeddings=self.max_position_embeddings		,	eos_token_ids=[2]		,	bos_token_id=self.bos_token_id		,	pad_token_id=self.pad_token_id		,	decoder_start_token_id=self.pad_token_id		,	attention_window=self.attention_window		,	**self.config_updates		,	)
    _UpperCamelCase:  Dict    						=			prepare_led_inputs_dict(_lowercase		,	_lowercase		,	_lowercase				)
    _UpperCamelCase:  Tuple    						=			tf.concat(
        [tf.zeros_like(_lowercase				)[:, :-1], tf.ones_like(_lowercase				)[:, -1:]]		,	axis=-1		,	)
    _UpperCamelCase:  List[Any]    						=			global_attention_mask
    return config, inputs_dict



   def 	lowerCAmelCase    (						self       :	Dict		,	_lowercase       :	List[str]		,	_lowercase       :	Union[str, Any]				):



    """simple docstring"""



    _UpperCamelCase:  Optional[Any]    						=			TFLEDModel(config=_lowercase				).get_decoder()
    _UpperCamelCase:  Optional[int]    						=			inputs_dict['''input_ids''']

    _UpperCamelCase:  Dict    						=			input_ids[:1, :]
    _UpperCamelCase:  Optional[Any]    						=			inputs_dict['''attention_mask'''][:1, :]
    _UpperCamelCase:  Optional[int]    						=			1

    # first forward pass
    _UpperCamelCase:  Optional[Any]    						=			model(_lowercase		,	attention_mask=_lowercase		,	use_cache=_lowercase				)

    _UpperCamelCase						,				_UpperCamelCase:  Union[str, Any]    						=			outputs.to_tuple()

    # create hypothetical next token and extent to next_input_ids
    _UpperCamelCase:  Any    						=			ids_tensor((self.batch_size, 3)		,	config.vocab_size				)
    _UpperCamelCase:  List[str]    						=			tf.cast(ids_tensor((self.batch_size, 3)		,	2				)		,	tf.inta				)

    # append to next input_ids and
    _UpperCamelCase:  Optional[int]    						=			tf.concat([input_ids, next_tokens]		,	axis=-1				)
    _UpperCamelCase:  Union[str, Any]    						=			tf.concat([attention_mask, next_attn_mask]		,	axis=-1				)

    _UpperCamelCase:  str    						=			model(_lowercase		,	attention_mask=_lowercase				)[0]
    _UpperCamelCase:  Optional[int]    						=			model(_lowercase		,	attention_mask=_lowercase		,	past_key_values=_lowercase				)[0]

    self.parent.assertEqual(next_tokens.shape[1]		,	output_from_past.shape[1]				)

    # select random slice
    _UpperCamelCase:  Dict    						=			int(ids_tensor((1,)		,	output_from_past.shape[-1]				)				)
    _UpperCamelCase:  Any    						=			output_from_no_past[:, -3:, random_slice_idx]
    _UpperCamelCase:  Optional[Any]    						=			output_from_past[:, :, random_slice_idx]

    # test that outputs are equal for slice
    tf.debugging.assert_near(_lowercase		,	_lowercase		,	rtol=1E-3				)




def    lowerCAmelCase_  (		lowercase:				Tuple	,       lowercase:				List[str]	,       lowercase:				List[str]	,       lowercase:				int=None	,       lowercase:				int=None	,       lowercase:				List[str]=None	,       lowercase:				List[str]=None	,       )     ->					List[str]:







 '''simple docstring'''
 if attention_mask is None:
  _UpperCamelCase:  Optional[Any]    						=			tf.cast(tf.math.not_equal(lowercase	,       config.pad_token_id		)	,       tf.inta		)
 if decoder_attention_mask is None:
  _UpperCamelCase:  int    						=			tf.concat(
      [
          tf.ones(decoder_input_ids[:, :1].shape	,       dtype=tf.inta		),
          tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]	,       config.pad_token_id		)	,       tf.inta		),
      ]	,       axis=-1	,       )
 if head_mask is None:
  _UpperCamelCase:  str    						=			tf.ones((config.encoder_layers, config.encoder_attention_heads)		)
 if decoder_head_mask is None:
  _UpperCamelCase:  Optional[Any]    						=			tf.ones((config.decoder_layers, config.decoder_attention_heads)		)
 return {
     "input_ids": input_ids,
     "attention_mask": attention_mask,
     "decoder_input_ids": decoder_input_ids,
     "decoder_attention_mask": decoder_attention_mask,
     "head_mask": head_mask,
     "decoder_head_mask": decoder_head_mask,
 }




@require_tf
class 			__magic_name__	(		__a   ,						__a   ,						unittest.TestCase     ):





   """simple docstring"""




   lowerCAmelCase   :     Optional[int]	      = (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
   lowerCAmelCase   :     List[str]	      = (TFLEDForConditionalGeneration,) if is_tf_available() else ()
   lowerCAmelCase   :     Union[str, Any]	      = (
       {
           '''conversational''': TFLEDForConditionalGeneration,
           '''feature-extraction''': TFLEDModel,
           '''summarization''': TFLEDForConditionalGeneration,
           '''text2text-generation''': TFLEDForConditionalGeneration,
           '''translation''': TFLEDForConditionalGeneration,
       }
       if is_tf_available()
       else {}
   )
   lowerCAmelCase   :     str	      = True
   lowerCAmelCase   :     Any	      = False
   lowerCAmelCase   :     Union[str, Any]	      = False
   lowerCAmelCase   :     Any	      = False



   def 	lowerCAmelCase    (						self       :	Any				):



    """simple docstring"""



    _UpperCamelCase:  Dict    						=			TFLEDModelTester(self				)
    _UpperCamelCase:  Any    						=			ConfigTester(self		,	config_class=_lowercase				)



   def 	lowerCAmelCase    (						self       :	Optional[int]				):



    """simple docstring"""



    self.config_tester.run_common_tests()



   def 	lowerCAmelCase    (						self       :	Tuple				):



    """simple docstring"""



    _UpperCamelCase:  Union[str, Any]    						=			self.model_tester.prepare_config_and_inputs_for_common()
    self.model_tester.check_decoder_model_past_large_inputs(*_lowercase				)



   def 	lowerCAmelCase    (						self       :	Optional[Any]				):



    """simple docstring"""



    _UpperCamelCase						,				_UpperCamelCase:  str    						=			self.model_tester.prepare_config_and_inputs_for_common()
    _UpperCamelCase:  str    						=			tf.zeros_like(inputs_dict['''attention_mask''']				)
    _UpperCamelCase:  Any    						=			2
    _UpperCamelCase:  List[str]    						=			tf.where(
        tf.range(self.model_tester.seq_length				)[None, :] < num_global_attn_indices		,	1		,	inputs_dict['''global_attention_mask''']		,	)

    _UpperCamelCase:  int    						=			True
    _UpperCamelCase:  str    						=			self.model_tester.seq_length
    _UpperCamelCase:  Dict    						=			self.model_tester.encoder_seq_length

    def check_decoder_attentions_output(_lowercase       :	Union[str, Any]				):
     _UpperCamelCase:  Tuple    						=			outputs.decoder_attentions
     self.assertEqual(len(_lowercase				)		,	self.model_tester.num_hidden_layers				)
     self.assertListEqual(
         list(decoder_attentions[0].shape[-3:]				)		,	[self.model_tester.num_attention_heads, seq_length, seq_length]		,	)

    def check_encoder_attentions_output(_lowercase       :	int				):
     _UpperCamelCase:  str    						=			[t.numpy() for t in outputs.encoder_attentions]
     _UpperCamelCase:  Any    						=			[t.numpy() for t in outputs.encoder_global_attentions]
     self.assertEqual(len(_lowercase				)		,	self.model_tester.num_hidden_layers				)
     self.assertEqual(len(_lowercase				)		,	self.model_tester.num_hidden_layers				)
     self.assertListEqual(
         list(attentions[0].shape[-3:]				)		,	[self.model_tester.num_attention_heads, seq_length, seq_length]		,	)
     self.assertListEqual(
         list(global_attentions[0].shape[-3:]				)		,	[self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices]		,	)

    for model_class in self.all_model_classes:
     _UpperCamelCase:  Optional[int]    						=			True
     _UpperCamelCase:  List[Any]    						=			False
     _UpperCamelCase:  Optional[int]    						=			False
     _UpperCamelCase:  Union[str, Any]    						=			model_class(_lowercase				)
     _UpperCamelCase:  Optional[int]    						=			model(self._prepare_for_class(_lowercase		,	_lowercase				)				)
     _UpperCamelCase:  List[str]    						=			len(_lowercase				)
     self.assertEqual(config.output_hidden_states		,	_lowercase				)
     check_encoder_attentions_output(_lowercase				)

     if self.is_encoder_decoder:
      _UpperCamelCase:  Optional[int]    						=			model_class(_lowercase				)
      _UpperCamelCase:  Union[str, Any]    						=			model(self._prepare_for_class(_lowercase		,	_lowercase				)				)
      self.assertEqual(config.output_hidden_states		,	_lowercase				)
      check_decoder_attentions_output(_lowercase				)

     # Check that output attentions can also be changed via the config
     del inputs_dict["output_attentions"]
     _UpperCamelCase:  Union[str, Any]    						=			True
     _UpperCamelCase:  List[str]    						=			model_class(_lowercase				)
     _UpperCamelCase:  Dict    						=			model(self._prepare_for_class(_lowercase		,	_lowercase				)				)
     self.assertEqual(config.output_hidden_states		,	_lowercase				)
     check_encoder_attentions_output(_lowercase				)

     # Check attention is always last and order is fine
     _UpperCamelCase:  str    						=			True
     _UpperCamelCase:  Dict    						=			True
     _UpperCamelCase:  int    						=			model_class(_lowercase				)
     _UpperCamelCase:  Optional[int]    						=			model(self._prepare_for_class(_lowercase		,	_lowercase				)				)

     self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)		,	len(_lowercase				)				)
     self.assertEqual(model.config.output_hidden_states		,	_lowercase				)
     check_encoder_attentions_output(_lowercase				)



   @unittest.skip('''LED keeps using potentially symbolic tensors in conditionals and breaks tracing.'''				)
   def 	lowerCAmelCase    (						self       :	List[Any]				):



    """simple docstring"""



    pass



   def 	lowerCAmelCase    (						self       :	Optional[int]				):



    """simple docstring"""



    pass




def    lowerCAmelCase_  (		lowercase:				Tuple		)     ->					List[str]:







 '''simple docstring'''
 return tf.constant(lowercase	,       dtype=tf.intaa		)


UpperCAmelCase_           =				1E-4





@slow
@require_tf
class 			__magic_name__	(		unittest.TestCase     ):





   """simple docstring"""




   def 	lowerCAmelCase    (						self       :	str				):



    """simple docstring"""



    _UpperCamelCase:  List[str]    						=			TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384'''				).led

    # change to intended input here
    _UpperCamelCase:  List[str]    						=			_long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]				)
    _UpperCamelCase:  int    						=			_long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]				)
    _UpperCamelCase:  Union[str, Any]    						=			prepare_led_inputs_dict(model.config		,	_lowercase		,	_lowercase				)
    _UpperCamelCase:  Optional[Any]    						=			model(**_lowercase				)[0]
    _UpperCamelCase:  Optional[int]    						=			(1, 1_024, 768)
    self.assertEqual(output.shape		,	_lowercase				)
    # change to expected output here
    _UpperCamelCase:  Optional[Any]    						=			tf.convert_to_tensor(
        [[2.3050, 2.8279, 0.6531], [-1.8457, -0.1455, -3.5661], [-1.0186, 0.4586, -2.2043]]		,	)
    tf.debugging.assert_near(output[:, :3, :3]		,	_lowercase		,	atol=1E-3				)



   def 	lowerCAmelCase    (						self       :	Union[str, Any]				):



    """simple docstring"""



    _UpperCamelCase:  Optional[Any]    						=			TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384'''				)

    # change to intended input here
    _UpperCamelCase:  Dict    						=			_long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]				)
    _UpperCamelCase:  List[str]    						=			_long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]				)
    _UpperCamelCase:  Tuple    						=			prepare_led_inputs_dict(model.config		,	_lowercase		,	_lowercase				)
    _UpperCamelCase:  Any    						=			model(**_lowercase				)[0]
    _UpperCamelCase:  Dict    						=			(1, 1_024, model.config.vocab_size)
    self.assertEqual(output.shape		,	_lowercase				)
    # change to expected output here
    _UpperCamelCase:  Optional[Any]    						=			tf.convert_to_tensor(
        [[33.6507, 6.4572, 16.8089], [5.8739, -2.4238, 11.2902], [-3.2139, -4.3149, 4.2783]]		,	)
    tf.debugging.assert_near(output[:, :3, :3]		,	_lowercase		,	atol=1E-3		,	rtol=1E-3				)