





"""simple docstring"""






from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


lowerCAmelCase_			=							logging.get_logger(__name__)

lowerCAmelCase_			=							{
    '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json''',
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}



class 	lowerCAmelCase					(      snake_case							):
       lowerCAmelCase__						 =  """blenderbot-small"""
       lowerCAmelCase__						 =  ["""past_key_values"""]
       lowerCAmelCase__						 =  {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}






       def __init__(   self    ,  a__=5_02_65    ,  a__=5_12    ,  a__=8    ,  a__=20_48    ,  a__=16    ,  a__=8    ,  a__=20_48    ,  a__=16    ,  a__=0.0    ,  a__=0.0    ,  a__=True    ,  a__=True    ,  a__="gelu"    ,  a__=5_12    ,  a__=0.1    ,  a__=0.0    ,  a__=0.0    ,  a__=0.02    ,  a__=1    ,  a__=False    ,  a__=0    ,  a__=1    ,  a__=2    ,  a__=2    ,  **a__    ,  ):
              _UpperCAmelCase     =     vocab_size
              _UpperCAmelCase     =     max_position_embeddings
              _UpperCAmelCase     =     d_model
              _UpperCAmelCase     =     encoder_ffn_dim
              _UpperCAmelCase     =     encoder_layers
              _UpperCAmelCase     =     encoder_attention_heads
              _UpperCAmelCase     =     decoder_ffn_dim
              _UpperCAmelCase     =     decoder_layers
              _UpperCAmelCase     =     decoder_attention_heads
              _UpperCAmelCase     =     dropout
              _UpperCAmelCase     =     attention_dropout
              _UpperCAmelCase     =     activation_dropout
              _UpperCAmelCase     =     activation_function
              _UpperCAmelCase     =     init_std
              _UpperCAmelCase     =     encoder_layerdrop
              _UpperCAmelCase     =     decoder_layerdrop
              _UpperCAmelCase     =     use_cache
              _UpperCAmelCase     =     encoder_layers
              _UpperCAmelCase     =     scale_embedding  # scale factor will be sqrt(d_model) if True

              super().__init__(
                  pad_token_id=a__    ,  bos_token_id=a__    ,  eos_token_id=a__    ,  is_encoder_decoder=a__    ,  decoder_start_token_id=a__    ,  forced_eos_token_id=a__    ,  **a__    ,  )



class 	lowerCAmelCase					(      snake_case							):






       @property
       def 							__A (   self     ):
              if self.task in ["default", "seq2seq-lm"]:
                     _UpperCAmelCase     =     OrderedDict(
                         [
                             ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                             ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                         ]     )

                     if self.use_past:
                            _UpperCAmelCase     =     {0: 'batch'}
                            _UpperCAmelCase     =     {0: 'batch', 1: 'past_decoder_sequence + sequence'}
                     else:
                            _UpperCAmelCase     =     {0: 'batch', 1: 'decoder_sequence'}
                            _UpperCAmelCase     =     {0: 'batch', 1: 'decoder_sequence'}

                     if self.use_past:
                            self.fill_with_past_key_values_(a__    ,  direction='inputs'     )
              elif self.task == "causal-lm":
                     # TODO: figure this case out.
                     _UpperCAmelCase     =     OrderedDict(
                         [
                             ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                             ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                         ]     )
                     if self.use_past:
                            _UpperCAmelCase    ,							_UpperCAmelCase     =     self.num_layers
                            for i in range(a__     ):
                                   _UpperCAmelCase     =     {0: 'batch', 2: 'past_sequence + sequence'}
                                   _UpperCAmelCase     =     {0: 'batch', 2: 'past_sequence + sequence'}
              else:
                     _UpperCAmelCase     =     OrderedDict(
                         [
                             ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                             ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                             ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
                             ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
                         ]     )

              return common_inputs






       @property
       def 							__A (   self     ):
              if self.task in ["default", "seq2seq-lm"]:
                     _UpperCAmelCase     =     super().outputs
              else:
                     _UpperCAmelCase     =     super(a__    ,  self     ).outputs
                     if self.use_past:
                            _UpperCAmelCase    ,							_UpperCAmelCase     =     self.num_layers
                            for i in range(a__     ):
                                   _UpperCAmelCase     =     {0: 'batch', 2: 'past_sequence + sequence'}
                                   _UpperCAmelCase     =     {0: 'batch', 2: 'past_sequence + sequence'}
              return common_outputs






       def 							__A (   self    ,  a__    ,  a__ = -1    ,  a__ = -1    ,  a__ = False    ,  a__ = None    ,  ):
              _UpperCAmelCase     =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                  a__    ,  a__    ,  a__    ,  a__    ,  a__     )

              # Generate decoder inputs
              _UpperCAmelCase     =     seq_length if not self.use_past else 1
              _UpperCAmelCase     =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                  a__    ,  a__    ,  a__    ,  a__    ,  a__     )
              _UpperCAmelCase     =     {f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
              _UpperCAmelCase     =     dict(**a__    ,  **a__     )

              if self.use_past:
                     if not is_torch_available():
                            raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'     )
                     else:
                            import torch
                     _UpperCAmelCase    ,							_UpperCAmelCase     =     common_inputs['input_ids'].shape
                     _UpperCAmelCase     =     common_inputs['decoder_input_ids'].shape[1]
                     _UpperCAmelCase    ,							_UpperCAmelCase     =     self.num_attention_heads
                     _UpperCAmelCase     =     (
                         batch,
                         num_encoder_attention_heads,
                         encoder_seq_length,
                         self._config.hidden_size // num_encoder_attention_heads,
                     )
                     _UpperCAmelCase     =     decoder_seq_length + 3
                     _UpperCAmelCase     =     (
                         batch,
                         num_decoder_attention_heads,
                         decoder_past_length,
                         self._config.hidden_size // num_decoder_attention_heads,
                     )

                     _UpperCAmelCase     =     torch.cat(
                         [common_inputs['decoder_attention_mask'], torch.ones(a__    ,  a__     )]    ,  dim=1     )

                     _UpperCAmelCase     =     []
                     # If the number of encoder and decoder layers are present in the model configuration, both are considered
                     _UpperCAmelCase    ,							_UpperCAmelCase     =     self.num_layers
                     _UpperCAmelCase     =     min(a__    ,  a__     )
                     _UpperCAmelCase     =     max(a__    ,  a__     ) - min_num_layers
                     _UpperCAmelCase     =     'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

                     for _ in range(a__     ):
                            common_inputs["past_key_values"].append(
                                (
                                    torch.zeros(a__     ),
                                    torch.zeros(a__     ),
                                    torch.zeros(a__     ),
                                    torch.zeros(a__     ),
                                )     )
                     # TODO: test this.
                     _UpperCAmelCase     =     encoder_shape if remaining_side_name == 'encoder' else decoder_shape
                     for _ in range(a__    ,  a__     ):
                            common_inputs["past_key_values"].append((torch.zeros(a__     ), torch.zeros(a__     ))     )
              return common_inputs






       def 							__A (   self    ,  a__    ,  a__ = -1    ,  a__ = -1    ,  a__ = False    ,  a__ = None    ,  ):
              _UpperCAmelCase     =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                  a__    ,  a__    ,  a__    ,  a__    ,  a__     )

              if self.use_past:
                     if not is_torch_available():
                            raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'     )
                     else:
                            import torch
                     _UpperCAmelCase    ,							_UpperCAmelCase     =     common_inputs['input_ids'].shape
                     # Not using the same length for past_key_values
                     _UpperCAmelCase     =     seqlen + 2
                     _UpperCAmelCase    ,							_UpperCAmelCase     =     self.num_layers
                     _UpperCAmelCase    ,							_UpperCAmelCase     =     self.num_attention_heads
                     _UpperCAmelCase     =     (
                         batch,
                         num_encoder_attention_heads,
                         past_key_values_length,
                         self._config.hidden_size // num_encoder_attention_heads,
                     )

                     _UpperCAmelCase     =     common_inputs['attention_mask'].dtype
                     _UpperCAmelCase     =     torch.cat(
                         [common_inputs['attention_mask'], torch.ones(a__    ,  a__    ,  dtype=a__     )]    ,  dim=1     )
                     _UpperCAmelCase     =     [
                         (torch.zeros(a__     ), torch.zeros(a__     )) for _ in range(a__     )
                     ]
              return common_inputs






       def 							__A (   self    ,  a__    ,  a__ = -1    ,  a__ = -1    ,  a__ = False    ,  a__ = None    ,  ):
              # Copied from OnnxConfig.generate_dummy_inputs
              # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
              # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
              _UpperCAmelCase     =     compute_effective_axis_dimension(
                  a__    ,  fixed_dimension=OnnxConfig.default_fixed_batch    ,  num_token_to_add=0     )

              # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
              _UpperCAmelCase     =     tokenizer.num_special_tokens_to_add(a__     )
              _UpperCAmelCase     =     compute_effective_axis_dimension(
                  a__    ,  fixed_dimension=OnnxConfig.default_fixed_sequence    ,  num_token_to_add=a__     )

              # Generate dummy inputs according to compute batch and sequence
              _UpperCAmelCase     =     [' '.join([tokenizer.unk_token]     ) * seq_length] * batch_size
              _UpperCAmelCase     =     dict(tokenizer(a__    ,  return_tensors=a__     )     )
              return common_inputs






       def 							__A (   self    ,  a__    ,  a__ = -1    ,  a__ = -1    ,  a__ = False    ,  a__ = None    ,  ):
              if self.task in ["default", "seq2seq-lm"]:
                     _UpperCAmelCase     =     self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                         a__    ,  batch_size=a__    ,  seq_length=a__    ,  is_pair=a__    ,  framework=a__     )

              elif self.task == "causal-lm":
                     _UpperCAmelCase     =     self._generate_dummy_inputs_for_causal_lm(
                         a__    ,  batch_size=a__    ,  seq_length=a__    ,  is_pair=a__    ,  framework=a__     )
              else:
                     _UpperCAmelCase     =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                         a__    ,  batch_size=a__    ,  seq_length=a__    ,  is_pair=a__    ,  framework=a__     )

              return common_inputs






       def 							__A (   self    ,  a__    ,  a__    ,  a__    ,  a__     ):
              if self.task in ["default", "seq2seq-lm"]:
                     _UpperCAmelCase     =     super()._flatten_past_key_values_(a__    ,  a__    ,  a__    ,  a__     )
              else:
                     _UpperCAmelCase     =     super(a__    ,  self     )._flatten_past_key_values_(
                         a__    ,  a__    ,  a__    ,  a__     )













"""simple docstring"""






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool



class 	lowerCAmelCase					(      snake_case							):
       lowerCAmelCase__						 =  """philschmid/bart-large-cnn-samsum"""
       lowerCAmelCase__						 =  (
           """This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, """
           """and returns a summary of the text."""
       )
       lowerCAmelCase__						 =  """summarizer"""
       lowerCAmelCase__						 =  AutoTokenizer
       lowerCAmelCase__						 =  AutoModelForSeqaSeqLM

       lowerCAmelCase__						 =  ["""text"""]
       lowerCAmelCase__						 =  ["""text"""]






       def 							__A (   self    ,  a__     ):
              return self.pre_processor(a__    ,  return_tensors='pt'    ,  truncation=a__     )






       def 							__A (   self    ,  a__     ):
              return self.model.generate(**a__     )[0]






       def 							__A (   self    ,  a__     ):
              return self.pre_processor.decode(a__    ,  skip_special_tokens=a__    ,  clean_up_tokenization_spaces=a__     )






