


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




 def __init__(		self ,  _lowerCamelCase ,  _lowerCamelCase=13 ,  _lowerCamelCase=7 ,  _lowerCamelCase=True ,  _lowerCamelCase=False ,  _lowerCamelCase=99 ,  _lowerCamelCase=32 ,  _lowerCamelCase=2 ,  _lowerCamelCase=4 ,  _lowerCamelCase=37 ,  _lowerCamelCase=0.1 ,  _lowerCamelCase=0.1 ,  _lowerCamelCase=20 ,  _lowerCamelCase=2 ,  _lowerCamelCase=1 ,  _lowerCamelCase=0 ,  _lowerCamelCase=4 ,  ):
       a						:Dict										=	parent
       a						:Tuple										=	batch_size
       a						:List[Any]										=	seq_length
       a						:Any										=	is_training
       a						:Tuple										=	use_labels
       a						:Any										=	vocab_size
       a						:List[str]										=	hidden_size
       a						:Dict										=	num_hidden_layers
       a						:Optional[int]										=	num_attention_heads
       a						:Dict										=	intermediate_size

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




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

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

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




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:str										=	TFLEDModel(config=_lowerCamelCase   ).get_decoder()
       a						:Optional[Any]										=	inputs_dict['''input_ids''']

       a						:str										=	input_ids[:1, :]
       a						:str										=	inputs_dict['''attention_mask'''][:1, :]
       a						:int										=	1

       # first forward pass
       a						:Dict										=	model(_lowerCamelCase ,  attention_mask=_lowerCamelCase ,  use_cache=_lowerCamelCase   )

       a							,    a						:str										=	outputs.to_tuple()

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

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

       a						:Any										=	model(_lowerCamelCase ,  attention_mask=_lowerCamelCase   )[0]
       a						:Union[str, 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
       a						:Union[str, Any]										=	int(ids_tensor((1,) ,  output_from_past.shape[-1]   )   )
       a						:str										=	output_from_no_past[:, -3:, random_slice_idx]
       a						: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						(			UpperCAmelCase_						:		List[str]			,  UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Optional[Any]=None			,  UpperCAmelCase_						:		str=None			,  UpperCAmelCase_						:		Any=None			,  UpperCAmelCase_						:		int=None			,  ):




      """simple docstring"""





      if attention_mask is None:
            a						:int										=	tf.cast(tf.math.not_equal(UpperCAmelCase_			,  config.pad_token_id						)			,  tf.inta						)
      if decoder_attention_mask is None:
            a						:Optional[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:
            a						:Optional[int]										=	tf.ones((config.encoder_layers, config.encoder_attention_heads)						)
      if decoder_head_mask is None:
            a						:Tuple										=	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  (				_snake_case						,   _snake_case						,   unittest.TestCase							):
 SCREAMING_SNAKE_CASE__				=	(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
 SCREAMING_SNAKE_CASE__				=	(TFLEDForConditionalGeneration,) if is_tf_available() else ()
 SCREAMING_SNAKE_CASE__				=	(
     {
         'conversational': TFLEDForConditionalGeneration,
         'feature-extraction': TFLEDModel,
         'summarization': TFLEDForConditionalGeneration,
         'text2text-generation': TFLEDForConditionalGeneration,
         'translation': TFLEDForConditionalGeneration,
     }
     if is_tf_available()
     else {}
 )
 SCREAMING_SNAKE_CASE__				=	True
 SCREAMING_SNAKE_CASE__				=	False
 SCREAMING_SNAKE_CASE__				=	False
 SCREAMING_SNAKE_CASE__				=	False




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Optional[int]										=	TFLEDModelTester(self   )
       a						:Any										=	ConfigTester(self ,  config_class=_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       self.config_tester.run_common_tests()




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:List[Any]										=	self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_decoder_model_past_large_inputs(*_lowerCamelCase   )




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

       a						:List[str]										=	True
       a						:int										=	self.model_tester.seq_length
       a						:List[str]										=	self.model_tester.encoder_seq_length

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

             if self.is_encoder_decoder:
                   a						:List[Any]										=	model_class(_lowerCamelCase   )
                   a						:Optional[Any]										=	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"]
             a						:str										=	True
             a						:Dict										=	model_class(_lowerCamelCase   )
             a						:Optional[int]										=	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
             a						:Optional[Any]										=	True
             a						:Optional[Any]										=	True
             a						:Optional[int]										=	model_class(_lowerCamelCase   )
             a						:Optional[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 	SCREAMING_SNAKE_CASE__    (		self   ):
       pass




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       # TODO: Head-masking not yet implement
       pass







def     __lowerCamelCase						(			UpperCAmelCase_						:		List[str]						):




      """simple docstring"""





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


snake_case    :   Optional[Any]	    =					1E-4







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




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Dict										=	TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384'''   ).led

       # change to intended input here
       a						:Tuple										=	_long_tensor([512 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]   )
       a						:int										=	_long_tensor([128 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]   )
       a						:Optional[int]										=	prepare_led_inputs_dict(model.config ,  _lowerCamelCase ,  _lowerCamelCase   )
       a						:int										=	model(**_lowerCamelCase   )[0]
       a						:Optional[int]										=	(1, 1024, 768)
       self.assertEqual(output.shape ,  _lowerCamelCase   )
       # change to expected output here
       a						:List[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] ,  _lowerCamelCase ,  atol=1e-3   )




 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Dict										=	TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384'''   )

       # change to intended input here
       a						:str										=	_long_tensor([512 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]   )
       a						:Tuple										=	_long_tensor([128 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]   )
       a						:Any										=	prepare_led_inputs_dict(model.config ,  _lowerCamelCase ,  _lowerCamelCase   )
       a						:List[str]										=	model(**_lowerCamelCase   )[0]
       a						:Dict										=	(1, 1024, model.config.vocab_size)
       self.assertEqual(output.shape ,  _lowerCamelCase   )
       # change to expected output here
       a						:str										=	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] ,  _lowerCamelCase ,  atol=1e-3 ,  rtol=1e-3   )









from collections import OrderedDict
from typing import List, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


snake_case    :   Union[str, Any]	    =					logging.get_logger(__name__)

snake_case    :   List[str]	    =					{
    '''google/efficientnet-b7''': '''https://huggingface.co/google/efficientnet-b7/resolve/main/config.json''',
}







class 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	'efficientnet'




 def __init__(		self ,  _lowerCamelCase = 3 ,  _lowerCamelCase = 600 ,  _lowerCamelCase = 2.0 ,  _lowerCamelCase = 3.1 ,  _lowerCamelCase = 8 ,  _lowerCamelCase = [3, 3, 5, 3, 5, 5, 3] ,  _lowerCamelCase = [32, 16, 24, 40, 80, 112, 192] ,  _lowerCamelCase = [16, 24, 40, 80, 112, 192, 320] ,  _lowerCamelCase = [] ,  _lowerCamelCase = [1, 2, 2, 2, 1, 2, 1] ,  _lowerCamelCase = [1, 2, 2, 3, 3, 4, 1] ,  _lowerCamelCase = [1, 6, 6, 6, 6, 6, 6] ,  _lowerCamelCase = 0.25 ,  _lowerCamelCase = "swish" ,  _lowerCamelCase = 2560 ,  _lowerCamelCase = "mean" ,  _lowerCamelCase = 0.02 ,  _lowerCamelCase = 0.001 ,  _lowerCamelCase = 0.99 ,  _lowerCamelCase = 0.5 ,  _lowerCamelCase = 0.2 ,  **_lowerCamelCase ,  ):
       super().__init__(**_lowerCamelCase   )

       a						:Optional[int]										=	num_channels
       a						:List[str]										=	image_size
       a						:int										=	width_coefficient
       a						:Optional[Any]										=	depth_coefficient
       a						:Any										=	depth_divisor
       a						:Any										=	kernel_sizes
       a						:Tuple										=	in_channels
       a						:Union[str, Any]										=	out_channels
       a						:Any										=	depthwise_padding
       a						:Any										=	strides
       a						:Optional[Any]										=	num_block_repeats
       a						:Tuple										=	expand_ratios
       a						:Dict										=	squeeze_expansion_ratio
       a						:int										=	hidden_act
       a						:Dict										=	hidden_dim
       a						:Tuple										=	pooling_type
       a						:Any										=	initializer_range
       a						:Tuple										=	batch_norm_eps
       a						:Optional[int]										=	batch_norm_momentum
       a						:List[Any]										=	dropout_rate
       a						:Optional[int]										=	drop_connect_rate
       a						:Tuple										=	sum(_lowerCamelCase   ) * 4







class 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	version.parse('1.11'							)




 @property
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       return OrderedDict(
           [
               ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
           ]   )




 @property
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       return 1e-5





