


"""simple docstring"""






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 							A_      :
  lowerCAmelCase__      			=	LEDConfig
  lowerCAmelCase__      			=	{}
  lowerCAmelCase__      			=	'gelu'


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







     '''simple docstring'''







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

     _lowerCamelCase   :				Optional[Any]							=     hidden_dropout_prob
     _lowerCamelCase   :				List[Any]							=     attention_probs_dropout_prob
     _lowerCamelCase   :				Tuple							=     max_position_embeddings
     _lowerCamelCase   :				int							=     eos_token_id
     _lowerCamelCase   :				Dict							=     pad_token_id
     _lowerCamelCase   :				List[str]							=     bos_token_id
     _lowerCamelCase   :				str							=     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
     _lowerCamelCase   :				str							=     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
     _lowerCamelCase   :				Any							=     (
         self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
     )


  def 							_lowercase					(			self:						int    ):







     '''simple docstring'''







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

     _lowerCamelCase   :				int							=     ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size    )

     _lowerCamelCase   :				List[str]							=     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							,)
     _lowerCamelCase   :				int							=     prepare_led_inputs_dict(__lowerCAmelCase							,__lowerCAmelCase							,__lowerCAmelCase    )
     _lowerCamelCase   :				Optional[Any]							=     tf.concat(
         [tf.zeros_like(__lowerCAmelCase    )[:, :-1], tf.ones_like(__lowerCAmelCase    )[:, -1:]]							,axis=-1							,)
     _lowerCamelCase   :				Optional[int]							=     global_attention_mask
     return config, inputs_dict






  def 							_lowercase					(			self:						int							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     TFLEDModel(config=__lowerCAmelCase    ).get_decoder()
     _lowerCamelCase   :				str							=     inputs_dict["input_ids"]

     _lowerCamelCase   :				List[str]							=     input_ids[:1, :]
     _lowerCamelCase   :				Any							=     inputs_dict["attention_mask"][:1, :]
     _lowerCamelCase   :				Any							=     1

     # first forward pass
     _lowerCamelCase   :				Union[str, Any]							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,use_cache=__lowerCAmelCase    )

     _lowerCamelCase,					_lowerCamelCase   :				Dict							=     outputs.to_tuple()

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

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

     _lowerCamelCase   :				List[Any]							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase    )[0]
     _lowerCamelCase   :				List[Any]							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,past_key_values=__lowerCAmelCase    )[0]

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

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

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







def        lowerCamelCase_(		_lowerCamelCase			,		_lowerCamelCase			,		_lowerCamelCase			,		_lowerCamelCase=None			,		_lowerCamelCase=None			,		_lowerCamelCase=None			,		_lowerCamelCase=None			,		)    ->     Tuple:
   '''simple docstring'''





   if attention_mask is None:
      _lowerCamelCase   :				List[str]							=     tf.cast(tf.math.not_equal(_lowerCamelCase			,		config.pad_token_id       )			,		tf.inta       )
   if decoder_attention_mask is None:
      _lowerCamelCase   :				Optional[Any]							=     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:
      _lowerCamelCase   :				str							=     tf.ones((config.encoder_layers, config.encoder_attention_heads)       )
   if decoder_head_mask is None:
      _lowerCamelCase   :				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 							A_      (       _a     ,	_a     ,	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 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     TFLEDModelTester(self    )
     _lowerCamelCase   :				str							=     ConfigTester(self							,config_class=__lowerCAmelCase    )


  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







     self.config_tester.run_common_tests()


  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     self.model_tester.prepare_config_and_inputs_for_common()
     self.model_tester.check_decoder_model_past_large_inputs(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase,					_lowerCamelCase   :				List[Any]							=     self.model_tester.prepare_config_and_inputs_for_common()
     _lowerCamelCase   :				Union[str, Any]							=     tf.zeros_like(inputs_dict["attention_mask"]    )
     _lowerCamelCase   :				Dict							=     2
     _lowerCamelCase   :				int							=     tf.where(
         tf.range(self.model_tester.seq_length    )[None, :] < num_global_attn_indices							,1							,inputs_dict["global_attention_mask"]							,)

     _lowerCamelCase   :				List[Any]							=     True
     _lowerCamelCase   :				Optional[Any]							=     self.model_tester.seq_length
     _lowerCamelCase   :				Optional[Any]							=     self.model_tester.encoder_seq_length

     def check_decoder_attentions_output(__lowerCAmelCase:						List[Any]    ):
        _lowerCamelCase   :				Dict							=     outputs.decoder_attentions
        self.assertEqual(len(__lowerCAmelCase    )							,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(__lowerCAmelCase:						Tuple    ):
        _lowerCamelCase   :				Union[str, Any]							=     [t.numpy() for t in outputs.encoder_attentions]
        _lowerCamelCase   :				Optional[Any]							=     [t.numpy() for t in outputs.encoder_global_attentions]
        self.assertEqual(len(__lowerCAmelCase    )							,self.model_tester.num_hidden_layers    )
        self.assertEqual(len(__lowerCAmelCase    )							,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:
        _lowerCamelCase   :				str							=     True
        _lowerCamelCase   :				str							=     False
        _lowerCamelCase   :				Any							=     False
        _lowerCamelCase   :				Optional[Any]							=     model_class(__lowerCAmelCase    )
        _lowerCamelCase   :				Dict							=     model(self._prepare_for_class(__lowerCAmelCase							,__lowerCAmelCase    )    )
        _lowerCamelCase   :				List[str]							=     len(__lowerCAmelCase    )
        self.assertEqual(config.output_hidden_states							,__lowerCAmelCase    )
        check_encoder_attentions_output(__lowerCAmelCase    )

        if self.is_encoder_decoder:
           _lowerCamelCase   :				List[str]							=     model_class(__lowerCAmelCase    )
           _lowerCamelCase   :				Optional[int]							=     model(self._prepare_for_class(__lowerCAmelCase							,__lowerCAmelCase    )    )
           self.assertEqual(config.output_hidden_states							,__lowerCAmelCase    )
           check_decoder_attentions_output(__lowerCAmelCase    )

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

        # Check attention is always last and order is fine
        _lowerCamelCase   :				Any							=     True
        _lowerCamelCase   :				List[Any]							=     True
        _lowerCamelCase   :				Dict							=     model_class(__lowerCAmelCase    )
        _lowerCamelCase   :				Any							=     model(self._prepare_for_class(__lowerCAmelCase							,__lowerCAmelCase    )    )

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


  @unittest.skip("LED keeps using potentially symbolic tensors in conditionals and breaks tracing."    )
  def 							_lowercase					(			self:						int    ):







     '''simple docstring'''







     pass






  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     pass







def        lowerCamelCase_(		_lowerCamelCase       )    ->     Tuple:
   '''simple docstring'''





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


_lowerCAmelCase							:    List[str]					   =	1e-4




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


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     _lowerCamelCase   :				int							=     TFLEDForConditionalGeneration.from_pretrained("allenai/led-base-16384"    ).led

     # change to intended input here
     _lowerCamelCase   :				List[str]							=     _long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]    )
     _lowerCamelCase   :				Tuple							=     _long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]    )
     _lowerCamelCase   :				List[Any]							=     prepare_led_inputs_dict(model.config							,__lowerCAmelCase							,__lowerCAmelCase    )
     _lowerCamelCase   :				List[str]							=     model(**__lowerCAmelCase    )[0]
     _lowerCamelCase   :				List[str]							=     (1, 1_024, 768)
     self.assertEqual(output.shape							,__lowerCAmelCase    )
     # change to expected output here
     _lowerCamelCase   :				List[str]							=     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]							,__lowerCAmelCase							,atol=1e-3    )






  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







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

     # change to intended input here
     _lowerCamelCase   :				Tuple							=     _long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]    )
     _lowerCamelCase   :				Any							=     _long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]    )
     _lowerCamelCase   :				List[str]							=     prepare_led_inputs_dict(model.config							,__lowerCAmelCase							,__lowerCAmelCase    )
     _lowerCamelCase   :				Tuple							=     model(**__lowerCAmelCase    )[0]
     _lowerCamelCase   :				List[Any]							=     (1, 1_024, model.config.vocab_size)
     self.assertEqual(output.shape							,__lowerCAmelCase    )
     # change to expected output here
     _lowerCamelCase   :				List[Any]							=     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]							,__lowerCAmelCase							,atol=1e-3							,rtol=1e-3    )




'''simple docstring'''





import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin



class  snake_case__						(    SCREAMING_SNAKE_CASE_				):
 A__				= ['''image_processor''', '''tokenizer''']
 A__				= '''AutoImageProcessor'''
 A__				= '''AutoTokenizer'''
 def __init__(   self						:							List[str]      ,     __a						:							Optional[int]=None      ,     __a						:							Any=None      ,     **__a						:							Optional[int]  )      ->   Union[str, Any]:







       '''simple docstring'''
       __snake_case :   str													=    None
       if "feature_extractor" in kwargs:
             warnings.warn(
                 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                 ' instead.'      ,     __a      ,     )
             __snake_case :   Tuple													=    kwargs.pop('feature_extractor'  )

       __snake_case :   Union[str, Any]													=    image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
             raise ValueError('You need to specify an `image_processor`.'  )
       if tokenizer is None:
             raise ValueError('You need to specify a `tokenizer`.'  )

       super().__init__(__a      ,     __a  )
       __snake_case :   Any													=    self.image_processor
       __snake_case :   int													=    False
 def __call__(   self						:							str      ,     *__a						:							List[str]      ,     **__a						:							List[str]  )      ->   Dict:







       '''simple docstring'''
       # For backward compatibility
       if self._in_target_context_manager:
             return self.current_processor(*__a      ,     **__a  )

       __snake_case :   List[Any]													=    kwargs.pop('images'      ,     __a  )
       __snake_case :   Any													=    kwargs.pop('text'      ,     __a  )
       if len(__a  ) > 0:
             __snake_case :   List[Any]													=    args[0]
             __snake_case :   Dict													=    args[1:]

       if images is None and text is None:
             raise ValueError('You need to specify either an `images` or `text` input to process.'  )

       if images is not None:
             __snake_case :   int													=    self.image_processor(__a      ,     *__a      ,     **__a  )
       if text is not None:
             __snake_case :   Tuple													=    self.tokenizer(__a      ,     **__a  )

       if text is None:
             return inputs
       elif images is None:
             return encodings
       else:
             __snake_case :   List[str]													=    encodings['input_ids']
             return inputs
 def A_   (   self						:							str      ,     *__a						:							Union[str, Any]      ,     **__a						:							Optional[Any]  )      ->   List[str]:







       '''simple docstring'''
       return self.tokenizer.batch_decode(*__a      ,     **__a  )
 def A_   (   self						:							Any      ,     *__a						:							List[Any]      ,     **__a						:							Dict  )      ->   List[Any]:







       '''simple docstring'''
       return self.tokenizer.decode(*__a      ,     **__a  )
 @contextmanager
 def A_   (   self						:							str  )      ->   List[Any]:







       '''simple docstring'''
       warnings.warn(
           '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
           'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
           'your images inputs, or in a separate call.'  )
       __snake_case :   Union[str, Any]													=    True
       __snake_case :   List[Any]													=    self.tokenizer
       yield
       __snake_case :   str													=    self.image_processor
       __snake_case :   Tuple													=    False
 def A_   (   self						:							List[str]      ,     __a						:							Tuple      ,     __a						:							Union[str, Any]=False      ,     __a						:							str=None  )      ->   List[str]:







       '''simple docstring'''
       if added_vocab is None:
             __snake_case :   List[Any]													=    self.tokenizer.get_added_vocab()

       __snake_case :   Tuple													=    {}

       while tokens:
             __snake_case :   Optional[Any]													=    re.search(r'<s_(.*?)>'      ,     __a      ,     re.IGNORECASE  )
             if start_token is None:
                   break
             __snake_case :   Any													=    start_token.group(1  )
             __snake_case :   Dict													=    re.search(rf'''</s_{key}>'''      ,     __a      ,     re.IGNORECASE  )
             __snake_case :   List[str]													=    start_token.group()
             if end_token is None:
                   __snake_case :   List[Any]													=    tokens.replace(__a      ,     ''  )
             else:
                   __snake_case :   int													=    end_token.group()
                   __snake_case :   Optional[int]													=    re.escape(__a  )
                   __snake_case :   List[str]													=    re.escape(__a  )
                   __snake_case :   Optional[int]													=    re.search(f'''{start_token_escaped}(.*?){end_token_escaped}'''      ,     __a      ,     re.IGNORECASE  )
                   if content is not None:
                         __snake_case :   Tuple													=    content.group(1  ).strip()
                         if r"<s_" in content and r"</s_" in content:  # non-leaf node
                               __snake_case :   str													=    self.tokenajson(__a      ,     is_inner_value=__a      ,     added_vocab=__a  )
                               if value:
                                     if len(__a  ) == 1:
                                           __snake_case :   int													=    value[0]
                                     __snake_case :   Union[str, Any]													=    value
                         else:  # leaf nodes
                               __snake_case :   Any													=    []
                               for leaf in content.split(r'<sep/>'  ):
                                     __snake_case :   str													=    leaf.strip()
                                     if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                                           __snake_case :   Any													=    leaf[1:-2]  # for categorical special tokens
                                     output[key].append(__a  )
                               if len(output[key]  ) == 1:
                                     __snake_case :   int													=    output[key][0]

                   __snake_case :   Union[str, Any]													=    tokens[tokens.find(__a  ) + len(__a  ) :].strip()
                   if tokens[:6] == r"<sep/>":  # non-leaf nodes
                         return [output] + self.tokenajson(tokens[6:]      ,     is_inner_value=__a      ,     added_vocab=__a  )

       if len(__a  ):
             return [output] if is_inner_value else output
       else:
             return [] if is_inner_value else {"text_sequence": tokens}
 @property
 def A_   (   self						:							Optional[int]  )      ->   Any:







       '''simple docstring'''
       warnings.warn(
           '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'      ,     __a      ,     )
       return self.image_processor_class





 @property
 def A_   (   self						:							Union[str, Any]  )      ->   Optional[int]:







       '''simple docstring'''
       warnings.warn(
           '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'      ,     __a      ,     )
       return self.image_processor



