import unittest

from transformers import SPIECE_UNDERLINE, ReformerTokenizer, ReformerTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


__lowerCamelCase      :			Any							       =		get_tests_dir('''fixtures/test_sentencepiece.model''')







@require_sentencepiece
@require_tokenizers
class __snake_case    (						lowerCamelCase_       ,		unittest.TestCase		):
   lowerCAmelCase_														=							ReformerTokenizer
   lowerCAmelCase_														=							ReformerTokenizerFast
   lowerCAmelCase_														=							True
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							True




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






     super().setUp()

     SCREAMING_SNAKE_CASE__      							=				ReformerTokenizer(_lowercase	,     keep_accents=_lowercase				)
     tokenizer.save_pretrained(self.tmpdirname				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				"""<s>"""
     SCREAMING_SNAKE_CASE__      							=				1

     self.assertEqual(self.get_tokenizer()._convert_token_to_id(_lowercase				)	,     _lowercase				)
     self.assertEqual(self.get_tokenizer()._convert_id_to_token(_lowercase				)	,     _lowercase				)




   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				list(self.get_tokenizer().get_vocab().keys()				)

     self.assertEqual(vocab_keys[0]	,     """<unk>"""				)
     self.assertEqual(vocab_keys[1]	,     """<s>"""				)
     self.assertEqual(vocab_keys[-1]	,     """j"""				)
     self.assertEqual(len(_lowercase				)	,     10_00				)




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     self.assertEqual(self.get_tokenizer().vocab_size	,     10_00				)




   def      __a						(   self      :    List[str]				):



     """simple docstring"""






     if not self.test_rust_tokenizer:
       return

     SCREAMING_SNAKE_CASE__      							=				self.get_tokenizer()
     SCREAMING_SNAKE_CASE__      							=				self.get_rust_tokenizer()

     SCREAMING_SNAKE_CASE__      							=				"""I was born in 92000, and this is falsé."""

     SCREAMING_SNAKE_CASE__      							=				tokenizer.tokenize(_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				rust_tokenizer.tokenize(_lowercase				)
     self.assertListEqual(_lowercase	,     _lowercase				)

     SCREAMING_SNAKE_CASE__      							=				tokenizer.encode(_lowercase	,     add_special_tokens=_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				rust_tokenizer.encode(_lowercase	,     add_special_tokens=_lowercase				)
     self.assertListEqual(_lowercase	,     _lowercase				)

     SCREAMING_SNAKE_CASE__      							=				self.get_rust_tokenizer()
     SCREAMING_SNAKE_CASE__      							=				tokenizer.encode(_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				rust_tokenizer.encode(_lowercase				)
     self.assertListEqual(_lowercase	,     _lowercase				)




   def      __a						(   self      :    Union[str, Any]	,     _lowercase      :    Optional[Any]=15				):



     """simple docstring"""






     for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
       with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""				):
         SCREAMING_SNAKE_CASE__      							=				self.rust_tokenizer_class.from_pretrained(_lowercase	,     **_lowercase				)

         # Simple input
         SCREAMING_SNAKE_CASE__      							=				"""This is a simple input"""
         SCREAMING_SNAKE_CASE__      							=				["""This is a simple input 1""", """This is a simple input 2"""]
         SCREAMING_SNAKE_CASE__      							=				("""This is a simple input""", """This is a pair""")
         SCREAMING_SNAKE_CASE__      							=				[
             ("""This is a simple input 1""", """This is a simple input 2"""),
             ("""This is a simple pair 1""", """This is a simple pair 2"""),
         ]

         # Simple input tests
         self.assertRaises(_lowercase	,     tokenizer_r.encode	,     _lowercase	,     max_length=_lowercase	,     padding="""max_length"""				)

         # Simple input
         self.assertRaises(_lowercase	,     tokenizer_r.encode_plus	,     _lowercase	,     max_length=_lowercase	,     padding="""max_length"""				)

         # Simple input
         self.assertRaises(
             _lowercase	,     tokenizer_r.batch_encode_plus	,     _lowercase	,     max_length=_lowercase	,     padding="""max_length"""	,     )

         # Pair input
         self.assertRaises(_lowercase	,     tokenizer_r.encode	,     _lowercase	,     max_length=_lowercase	,     padding="""max_length"""				)

         # Pair input
         self.assertRaises(_lowercase	,     tokenizer_r.encode_plus	,     _lowercase	,     max_length=_lowercase	,     padding="""max_length"""				)

         # Pair input
         self.assertRaises(
             _lowercase	,     tokenizer_r.batch_encode_plus	,     _lowercase	,     max_length=_lowercase	,     padding="""max_length"""	,     )




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     pass




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				ReformerTokenizer(_lowercase	,     keep_accents=_lowercase				)

     SCREAMING_SNAKE_CASE__      							=				tokenizer.tokenize("""This is a test"""				)
     self.assertListEqual(_lowercase	,     ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""]				)

     self.assertListEqual(
         tokenizer.convert_tokens_to_ids(_lowercase				)	,     [2_85, 46, 10, 1_70, 3_82]	,     )

     SCREAMING_SNAKE_CASE__      							=				tokenizer.tokenize("""I was born in 92000, and this is falsé."""				)
     self.assertListEqual(
         _lowercase	,     [
             SPIECE_UNDERLINE + """I""",
             SPIECE_UNDERLINE + """was""",
             SPIECE_UNDERLINE + """b""",
             """or""",
             """n""",
             SPIECE_UNDERLINE + """in""",
             SPIECE_UNDERLINE + """""",
             """9""",
             """2""",
             """0""",
             """0""",
             """0""",
             """,""",
             SPIECE_UNDERLINE + """and""",
             SPIECE_UNDERLINE + """this""",
             SPIECE_UNDERLINE + """is""",
             SPIECE_UNDERLINE + """f""",
             """al""",
             """s""",
             """é""",
             """.""",
         ]	,     )
     SCREAMING_SNAKE_CASE__      							=				tokenizer.convert_tokens_to_ids(_lowercase				)
     self.assertListEqual(
         _lowercase	,     [8, 21, 84, 55, 24, 19, 7, 0, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 0, 4]	,     )

     SCREAMING_SNAKE_CASE__      							=				tokenizer.convert_ids_to_tokens(_lowercase				)
     self.assertListEqual(
         _lowercase	,     [
             SPIECE_UNDERLINE + """I""",
             SPIECE_UNDERLINE + """was""",
             SPIECE_UNDERLINE + """b""",
             """or""",
             """n""",
             SPIECE_UNDERLINE + """in""",
             SPIECE_UNDERLINE + """""",
             """<unk>""",
             """2""",
             """0""",
             """0""",
             """0""",
             """,""",
             SPIECE_UNDERLINE + """and""",
             SPIECE_UNDERLINE + """this""",
             SPIECE_UNDERLINE + """is""",
             SPIECE_UNDERLINE + """f""",
             """al""",
             """s""",
             """<unk>""",
             """.""",
         ]	,     )




   @cached_property
   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     return ReformerTokenizer.from_pretrained("""google/reformer-crime-and-punishment"""				)




   @slow
   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				"""Hello World!"""
     SCREAMING_SNAKE_CASE__      							=				[1_26, 32, 2_62, 1_52, 38, 72, 2_87]

     self.assertListEqual(_lowercase	,     self.big_tokenizer.encode(_lowercase				)				)




   @slow
   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				(
         """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will"""
         """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth"""
     )
     SCREAMING_SNAKE_CASE__      							=				[
         1_08,
         2_65,
         24,
         1_11,
         4,
         2_58,
         1_56,
         35,
         28,
         2_75,
         3,
         2_59,
         2_97,
         2_60,
         84,
         4,
         35,
         1_10,
         44,
         8,
         2_59,
         91,
         2_68,
         21,
         11,
         2_09,
         2_74,
         1_09,
         2_66,
         2_77,
         1_17,
         86,
         93,
         3_15,
         2_58,
         2_78,
         2_58,
         2_77,
         2_58,
         0,
         2_58,
         2_88,
         2_58,
         3_19,
         2_58,
         0,
         2_58,
         0,
         2_58,
         0,
         2_58,
         0,
         2_58,
         2_87,
         2_58,
         3_15,
         2_58,
         2_89,
         2_58,
         2_78,
         99,
         2_69,
         2_66,
         2_62,
         8,
         2_59,
         2_41,
         4,
         2_17,
         2_30,
         2_68,
         2_66,
         55,
         1_68,
         1_06,
         75,
         1_93,
         2_66,
         2_23,
         27,
         49,
         26,
         2_82,
         25,
         2_64,
         2_99,
         19,
         26,
         0,
         2_58,
         2_77,
         1_17,
         86,
         93,
         1_76,
         1_83,
         2_70,
         11,
         2_62,
         42,
         61,
         2_65,
     ]

     self.assertListEqual(_lowercase	,     self.big_tokenizer.encode(_lowercase				)				)




   @require_torch
   @slow
   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     import torch

     from transformers import ReformerConfig, ReformerModel

     # Build sequence
     SCREAMING_SNAKE_CASE__      							=				list(self.big_tokenizer.get_vocab().keys()				)[:10]
     SCREAMING_SNAKE_CASE__      							=				""" """.join(_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				self.big_tokenizer.encode_plus(_lowercase	,     return_tensors="""pt"""				)
     SCREAMING_SNAKE_CASE__      							=				self.big_tokenizer.batch_encode_plus([sequence, sequence]	,     return_tensors="""pt"""				)

     SCREAMING_SNAKE_CASE__      							=				ReformerConfig()
     # The input gets padded during training so adjust the axial position encodings from the pretrained model value of (512, 1024)
     SCREAMING_SNAKE_CASE__      							=				encoded_sequence["""input_ids"""].shape
     SCREAMING_SNAKE_CASE__      							=				ReformerModel(_lowercase				)

     # Reformer has config.vocab_size == tokenizer.vocab_size == len(tokenizer) - 1 = 320; len(tokenizer) is 321 (including a pad token with id 320)
     assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size

     with torch.no_grad():
       model(**_lowercase				)
       model(**_lowercase				)




   @slow
   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				{"""input_ids""": [[1_08, 2_65, 24, 1_11, 4, 2_58, 1_56, 7, 51, 2_79, 58, 7, 76, 25, 69, 2_78], [1_40, 2_43, 2_64, 1_34, 17, 2_67, 77, 2_63, 22, 2_62, 2_97, 2_58, 3_04, 1_77, 2_79, 2_66, 14, 89, 13, 35, 2_61, 2_99, 2_72, 1_37, 2_75, 2_78]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
     # fmt: on

     # This tokenizer does not know some characters like ")".
     # That is the reason why we use very simple texts here.
     # Also see https://github.com/huggingface/transformers/pull/11737#issuecomment-850769064
     SCREAMING_SNAKE_CASE__      							=				[
         """This is a very simple sentence.""",
         """The quick brown fox jumps over the lazy dog.""",
     ]

     self.tokenizer_integration_test_util(
         expected_encoding=_lowercase	,     model_name="""google/reformer-crime-and-punishment"""	,     revision="""0e6c3decb8211d49bf881013425dc8b0448b3f5a"""	,     padding=_lowercase	,     sequences=_lowercase	,     )

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 __snake_case    :
   lowerCAmelCase_														=							LEDConfig
   lowerCAmelCase_														=							{}
   lowerCAmelCase_														=							"gelu"




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



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				parent
     SCREAMING_SNAKE_CASE__      							=				batch_size
     SCREAMING_SNAKE_CASE__      							=				seq_length
     SCREAMING_SNAKE_CASE__      							=				is_training
     SCREAMING_SNAKE_CASE__      							=				use_labels
     SCREAMING_SNAKE_CASE__      							=				vocab_size
     SCREAMING_SNAKE_CASE__      							=				hidden_size
     SCREAMING_SNAKE_CASE__      							=				num_hidden_layers
     SCREAMING_SNAKE_CASE__      							=				num_attention_heads
     SCREAMING_SNAKE_CASE__      							=				intermediate_size

     SCREAMING_SNAKE_CASE__      							=				hidden_dropout_prob
     SCREAMING_SNAKE_CASE__      							=				attention_probs_dropout_prob
     SCREAMING_SNAKE_CASE__      							=				max_position_embeddings
     SCREAMING_SNAKE_CASE__      							=				eos_token_id
     SCREAMING_SNAKE_CASE__      							=				pad_token_id
     SCREAMING_SNAKE_CASE__      							=				bos_token_id
     SCREAMING_SNAKE_CASE__      							=				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
     SCREAMING_SNAKE_CASE__      							=				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
     SCREAMING_SNAKE_CASE__      							=				(
         self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
     )




   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				ids_tensor([self.batch_size, self.seq_length - 1]	,     self.vocab_size				)
     SCREAMING_SNAKE_CASE__      							=				tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size				)	,     1				)
     SCREAMING_SNAKE_CASE__      							=				tf.concat([input_ids, eos_tensor]	,     axis=1				)

     SCREAMING_SNAKE_CASE__      							=				ids_tensor([self.batch_size, self.seq_length]	,     self.vocab_size				)

     SCREAMING_SNAKE_CASE__      							=				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	,     )
     SCREAMING_SNAKE_CASE__      							=				prepare_led_inputs_dict(_lowercase	,     _lowercase	,     _lowercase				)
     SCREAMING_SNAKE_CASE__      							=				tf.concat(
         [tf.zeros_like(_lowercase				)[:, :-1], tf.ones_like(_lowercase				)[:, -1:]]	,     axis=-1	,     )
     SCREAMING_SNAKE_CASE__      							=				global_attention_mask
     return config, inputs_dict




   def      __a						(   self      :    Tuple	,     _lowercase      :    int	,     _lowercase      :    int				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFLEDModel(config=_lowercase				).get_decoder()
     SCREAMING_SNAKE_CASE__      							=				inputs_dict["""input_ids"""]

     SCREAMING_SNAKE_CASE__      							=				input_ids[:1, :]
     SCREAMING_SNAKE_CASE__      							=				inputs_dict["""attention_mask"""][:1, :]
     SCREAMING_SNAKE_CASE__      							=				1

     # first forward pass
     SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     attention_mask=_lowercase	,     use_cache=_lowercase				)

     SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__      							=				outputs.to_tuple()

     # create hypothetical next token and extent to next_input_ids
     SCREAMING_SNAKE_CASE__      							=				ids_tensor((self.batch_size, 3)	,     config.vocab_size				)
     SCREAMING_SNAKE_CASE__      							=				tf.cast(ids_tensor((self.batch_size, 3)	,     2				)	,     tf.inta				)

     # append to next input_ids and
     SCREAMING_SNAKE_CASE__      							=				tf.concat([input_ids, next_tokens]	,     axis=-1				)
     SCREAMING_SNAKE_CASE__      							=				tf.concat([attention_mask, next_attn_mask]	,     axis=-1				)

     SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     attention_mask=_lowercase				)[0]
     SCREAMING_SNAKE_CASE__      							=				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
     SCREAMING_SNAKE_CASE__      							=				int(ids_tensor((1,)	,     output_from_past.shape[-1]				)				)
     SCREAMING_SNAKE_CASE__      							=				output_from_no_past[:, -3:, random_slice_idx]
     SCREAMING_SNAKE_CASE__      							=				output_from_past[:, :, random_slice_idx]

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





def      __SCREAMING_SNAKE_CASE  ( __UpperCamelCase   :	Optional[Any]			,					__UpperCamelCase   :	Dict			,					__UpperCamelCase   :	int			,					__UpperCamelCase   :	List[str]=None			,					__UpperCamelCase   :	Union[str, Any]=None			,					__UpperCamelCase   :	Dict=None			,					__UpperCamelCase   :	Optional[Any]=None			,					)							-> int:







  """simple docstring"""





  if attention_mask is None:
    SCREAMING_SNAKE_CASE__      							=				tf.cast(tf.math.not_equal(__UpperCamelCase			,					config.pad_token_id     )			,					tf.inta     )
  if decoder_attention_mask is None:
    SCREAMING_SNAKE_CASE__      							=				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:
    SCREAMING_SNAKE_CASE__      							=				tf.ones((config.encoder_layers, config.encoder_attention_heads)     )
  if decoder_head_mask is None:
    SCREAMING_SNAKE_CASE__      							=				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 __snake_case    (						lowerCamelCase_       ,		lowerCamelCase_       ,		unittest.TestCase		):
   lowerCAmelCase_														=							(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
   lowerCAmelCase_														=							(TFLEDForConditionalGeneration,) if is_tf_available() else ()
   lowerCAmelCase_														=							(
       {
           "conversational": TFLEDForConditionalGeneration,
           "feature-extraction": TFLEDModel,
           "summarization": TFLEDForConditionalGeneration,
           "text2text-generation": TFLEDForConditionalGeneration,
           "translation": TFLEDForConditionalGeneration,
       }
       if is_tf_available()
       else {}
   )
   lowerCAmelCase_														=							True
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFLEDModelTester(self				)
     SCREAMING_SNAKE_CASE__      							=				ConfigTester(self	,     config_class=_lowercase				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     self.config_tester.run_common_tests()




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.model_tester.prepare_config_and_inputs_for_common()
     self.model_tester.check_decoder_model_past_large_inputs(*_lowercase				)




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__      							=				self.model_tester.prepare_config_and_inputs_for_common()
     SCREAMING_SNAKE_CASE__      							=				tf.zeros_like(inputs_dict["""attention_mask"""]				)
     SCREAMING_SNAKE_CASE__      							=				2
     SCREAMING_SNAKE_CASE__      							=				tf.where(
         tf.range(self.model_tester.seq_length				)[None, :] < num_global_attn_indices	,     1	,     inputs_dict["""global_attention_mask"""]	,     )

     SCREAMING_SNAKE_CASE__      							=				True
     SCREAMING_SNAKE_CASE__      							=				self.model_tester.seq_length
     SCREAMING_SNAKE_CASE__      							=				self.model_tester.encoder_seq_length

     def check_decoder_attentions_output(_lowercase      :    List[str]				):
       SCREAMING_SNAKE_CASE__      							=				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      :    List[str]				):
       SCREAMING_SNAKE_CASE__      							=				[t.numpy() for t in outputs.encoder_attentions]
       SCREAMING_SNAKE_CASE__      							=				[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:
       SCREAMING_SNAKE_CASE__      							=				True
       SCREAMING_SNAKE_CASE__      							=				False
       SCREAMING_SNAKE_CASE__      							=				False
       SCREAMING_SNAKE_CASE__      							=				model_class(_lowercase				)
       SCREAMING_SNAKE_CASE__      							=				model(self._prepare_for_class(_lowercase	,     _lowercase				)				)
       SCREAMING_SNAKE_CASE__      							=				len(_lowercase				)
       self.assertEqual(config.output_hidden_states	,     _lowercase				)
       check_encoder_attentions_output(_lowercase				)

       if self.is_encoder_decoder:
         SCREAMING_SNAKE_CASE__      							=				model_class(_lowercase				)
         SCREAMING_SNAKE_CASE__      							=				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"]
       SCREAMING_SNAKE_CASE__      							=				True
       SCREAMING_SNAKE_CASE__      							=				model_class(_lowercase				)
       SCREAMING_SNAKE_CASE__      							=				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
       SCREAMING_SNAKE_CASE__      							=				True
       SCREAMING_SNAKE_CASE__      							=				True
       SCREAMING_SNAKE_CASE__      							=				model_class(_lowercase				)
       SCREAMING_SNAKE_CASE__      							=				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      __a						(   self      :    List[Any]				):



     """simple docstring"""






     pass




   def      __a						(   self      :    List[str]				):



     """simple docstring"""






     pass





def      __SCREAMING_SNAKE_CASE  ( __UpperCamelCase   :	List[str]     )							-> Dict:







  """simple docstring"""





  return tf.constant(__UpperCamelCase			,					dtype=tf.intaa     )


__lowerCamelCase      :			List[str]							       =		1e-4







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




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384"""				).led

     # change to intended input here
     SCREAMING_SNAKE_CASE__      							=				_long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]				)
     SCREAMING_SNAKE_CASE__      							=				_long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]				)
     SCREAMING_SNAKE_CASE__      							=				prepare_led_inputs_dict(model.config	,     _lowercase	,     _lowercase				)
     SCREAMING_SNAKE_CASE__      							=				model(**_lowercase				)[0]
     SCREAMING_SNAKE_CASE__      							=				(1, 10_24, 7_68)
     self.assertEqual(output.shape	,     _lowercase				)
     # change to expected output here
     SCREAMING_SNAKE_CASE__      							=				tf.convert_to_tensor(
         [[2.30_50, 2.82_79, 0.65_31], [-1.84_57, -0.14_55, -3.56_61], [-1.01_86, 0.45_86, -2.20_43]]	,     )
     tf.debugging.assert_near(output[:, :3, :3]	,     _lowercase	,     atol=1E-3				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384"""				)

     # change to intended input here
     SCREAMING_SNAKE_CASE__      							=				_long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]				)
     SCREAMING_SNAKE_CASE__      							=				_long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]				)
     SCREAMING_SNAKE_CASE__      							=				prepare_led_inputs_dict(model.config	,     _lowercase	,     _lowercase				)
     SCREAMING_SNAKE_CASE__      							=				model(**_lowercase				)[0]
     SCREAMING_SNAKE_CASE__      							=				(1, 10_24, model.config.vocab_size)
     self.assertEqual(output.shape	,     _lowercase				)
     # change to expected output here
     SCREAMING_SNAKE_CASE__      							=				tf.convert_to_tensor(
         [[33.65_07, 6.45_72, 16.80_89], [5.87_39, -2.42_38, 11.29_02], [-3.21_39, -4.31_49, 4.27_83]]	,     )
     tf.debugging.assert_near(output[:, :3, :3]	,     _lowercase	,     atol=1E-3	,     rtol=1E-3				)
