



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__:
    lowercase__				=					LEDConfig
    lowercase__				=					{}
    lowercase__				=					"""gelu"""







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

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







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

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

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







    def     lowercase_ (   self				:						int		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Any			):
      a		:      str       =  TFLEDModel(config=__snake_case			).get_decoder()
      a		:      Tuple       =  inputs_dict['input_ids']

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

      # first forward pass
      a		:      Tuple       =  model(__snake_case		,    attention_mask=__snake_case		,    use_cache=__snake_case			)

      a		:      Dict       =  outputs.to_tuple()

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

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

      a		:      List[str]       =  model(__snake_case		,    attention_mask=__snake_case			)[0]
      a		:      str       =  model(__snake_case		,    attention_mask=__snake_case		,    past_key_values=__snake_case			)[0]

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

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

      # test that outputs are equal for slice
      tf.debugging.assert_near(__snake_case		,    __snake_case		,    rtol=1e-3			)
def    lowerCamelCase__ (		_A				,    _A				,    _A				,    _A=None				,    _A=None				,    _A=None				,    _A=None				,    ):
  if attention_mask is None:
    a		:      Union[str, Any]       =  tf.cast(tf.math.not_equal(_A				,    config.pad_token_id       )				,    tf.inta       )
  if decoder_attention_mask is None:
    a		:      Tuple       =  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		:      Union[str, Any]       =  tf.ones((config.encoder_layers, config.encoder_attention_heads)       )
  if decoder_head_mask is None:
    a		:      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 							a__(      lowerCamelCase__	,				lowerCamelCase__	,				unittest.TestCase			):
    lowercase__				=					(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
    lowercase__				=					(TFLEDForConditionalGeneration,) if is_tf_available() else ()
    lowercase__				=					(
        {
            """conversational""": TFLEDForConditionalGeneration,
            """feature-extraction""": TFLEDModel,
            """summarization""": TFLEDForConditionalGeneration,
            """text2text-generation""": TFLEDForConditionalGeneration,
            """translation""": TFLEDForConditionalGeneration,
        }
        if is_tf_available()
        else {}
    )
    lowercase__				=					True
    lowercase__				=					False
    lowercase__				=					False
    lowercase__				=					False







    def     lowercase_ (   self				:						Dict			):
      a		:      Tuple       =  TFLEDModelTester(self			)
      a		:      Optional[int]       =  ConfigTester(self		,    config_class=__snake_case			)







    def     lowercase_ (   self				:						Dict			):
      self.config_tester.run_common_tests()







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







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

      a		:      int       =  True
      a		:      Union[str, Any]       =  self.model_tester.seq_length
      a		:      Optional[int]       =  self.model_tester.encoder_seq_length

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

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

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

        # Check attention is always last and order is fine
        a		:      Optional[Any]       =  True
        a		:      Optional[Any]       =  True
        a		:      str       =  model_class(__snake_case			)
        a		:      Union[str, Any]       =  model(self._prepare_for_class(__snake_case		,    __snake_case			)			)

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







    @unittest.skip('LED keeps using potentially symbolic tensors in conditionals and breaks tracing.'			)
    def     lowercase_ (   self				:						str			):
      pass







    def     lowercase_ (   self				:						int			):
      # TODO: Head-masking not yet implement
      pass
def    lowerCamelCase__ (		_A       ):
  return tf.constant(_A				,    dtype=tf.intaa       )


lowerCAmelCase:      int		 =		1e-4

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







    def     lowercase_ (   self				:						List[str]			):
      a		:      List[str]       =  TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'			).led

      # change to intended input here
      a		:      int       =  _long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]			)
      a		:      Any       =  _long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]			)
      a		:      List[str]       =  prepare_led_inputs_dict(model.config		,    __snake_case		,    __snake_case			)
      a		:      Any       =  model(**__snake_case			)[0]
      a		:      Tuple       =  (1, 10_24, 7_68)
      self.assertEqual(output.shape		,    __snake_case			)
      # change to expected output here
      a		:      Optional[int]       =  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]		,    __snake_case		,    atol=1e-3			)







    def     lowercase_ (   self				:						List[Any]			):
      a		:      int       =  TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'			)

      # change to intended input here
      a		:      Tuple       =  _long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]			)
      a		:      int       =  _long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]]			)
      a		:      Tuple       =  prepare_led_inputs_dict(model.config		,    __snake_case		,    __snake_case			)
      a		:      int       =  model(**__snake_case			)[0]
      a		:      List[Any]       =  (1, 10_24, model.config.vocab_size)
      self.assertEqual(output.shape		,    __snake_case			)
      # change to expected output here
      a		:      Dict       =  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]		,    __snake_case		,    atol=1e-3		,    rtol=1e-3			)




'''simple docstring'''






import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available
from . import BaseDiffusersCLICommand
def    lowerCamelCase__ (		_A       ):
  return EnvironmentCommand()

class 							a__(      lowerCamelCase__			):







    @staticmethod
    def     lowercase_ (   __snake_case				:						ArgumentParser			):
      a		:      Tuple       =  parser.add_parser('env'			)
      download_parser.set_defaults(func=__snake_case			)







    def     lowercase_ (   self				:						List[str]			):
      a		:      str       =  huggingface_hub.__version__

      a		:      List[str]       =  'not installed'
      a		:      List[str]       =  'NA'
      if is_torch_available():
        import torch

        a		:      Optional[Any]       =  torch.__version__
        a		:      int       =  torch.cuda.is_available()

      a		:      Optional[int]       =  'not installed'
      if is_transformers_available():
        import transformers

        a		:      Tuple       =  transformers.__version__

      a		:      Dict       =  'not installed'
      if is_accelerate_available():
        import accelerate

        a		:      int       =  accelerate.__version__

      a		:      Any       =  'not installed'
      if is_xformers_available():
        import xformers

        a		:      Optional[int]       =  xformers.__version__

      a		:      List[str]       =  {
          '`diffusers` version': version,
          'Platform': platform.platform(),
          'Python version': platform.python_version(),
          'PyTorch version (GPU?)': F"""{pt_version} ({pt_cuda_available})""",
          'Huggingface_hub version': hub_version,
          'Transformers version': transformers_version,
          'Accelerate version': accelerate_version,
          'xFormers version': xformers_version,
          'Using GPU in script?': '<fill in>',
          'Using distributed or parallel set-up in script?': '<fill in>',
      }

      print('\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n'			)
      print(self.format_dict(__snake_case			)			)

      return info







    @staticmethod
    def     lowercase_ (   __snake_case				:						Union[str, Any]			):
      return "\n".join([F"""- {prop}: {val}""" for prop, val in d.items()]			) + "\n"