





'''simple docstring'''
import uuid
from typing import Any, Dict, List, Optional, Union

from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
    import tensorflow as tf

if is_torch_available():
    import torch


a__							:							int =logging.get_logger(__name__)

class      snake_case     :




 """simple docstring"""



 def __init__(       self       :							Any							,	__A       :							str = None							,	__A       :							uuid.UUID = None							,	__A       :							List[Any]=None							,	__A       :							Optional[int]=None	):
        if not conversation_id:
               __UpperCamelCase       =  uuid.uuida()
        if past_user_inputs is None:
               __UpperCamelCase       =  []
        if generated_responses is None:
               __UpperCamelCase       =  []

        __UpperCamelCase       =  conversation_id
        __UpperCamelCase       =  past_user_inputs
        __UpperCamelCase       =  generated_responses
        __UpperCamelCase       =  text






 def __eq__(       self       :							str							,	__A       :							int	):
        if not isinstance(__A							,	__A	):
               return False
        if self.uuid == other.uuid:
               return True
        return (
            self.new_user_input == other.new_user_input
            and self.past_user_inputs == other.past_user_inputs
            and self.generated_responses == other.generated_responses
        )






 def        _lowerCamelCase					(       self       :							str							,	__A       :							str							,	__A       :							bool = False	):
        if self.new_user_input:
               if overwrite:
                      logger.warning(
                          f'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten '''
                          f'''with: "{text}".'''	)
                      __UpperCamelCase       =  text
               else:
                      logger.warning(
                          f'''User input added while unprocessed input was existing: "{self.new_user_input}" new input '''
                          f'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input'''	)
        else:
               __UpperCamelCase       =  text






 def        _lowerCamelCase					(       self       :							Optional[Any]	):
        if self.new_user_input:
               self.past_user_inputs.append(self.new_user_input	)
        __UpperCamelCase       =  None






 def        _lowerCamelCase					(       self       :							Dict							,	__A       :							str	):
        self.generated_responses.append(__A	)






 def        _lowerCamelCase					(       self       :							Tuple	):
        for user_input, generated_response in zip(self.past_user_inputs							,	self.generated_responses	):
               yield True, user_input
               yield False, generated_response
        if self.new_user_input:
               yield True, self.new_user_input






 def __repr__(       self       :							Tuple	):
        __UpperCamelCase       =  f'''Conversation id: {self.uuid} \n'''
        for is_user, text in self.iter_texts():
               __UpperCamelCase       =  'user' if is_user else 'bot'
               output += f'''{name} >> {text} \n'''
        return output






@add_end_docstrings(
    __lowerCamelCase			,       r"\n        min_length_for_response (`int`, *optional*, defaults to 32):\n            The minimum length (in number of tokens) for a response.\n        minimum_tokens (`int`, *optional*, defaults to 10):\n            The minimum length of tokens to leave for a response.\n    "			,       )
class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 def __init__(       self       :							Tuple							,	*__A       :							Union[str, Any]							,	**__A       :							Any	):
        super().__init__(*__A							,	**__A	)
        if self.tokenizer.pad_token_id is None:
               __UpperCamelCase       =  self.tokenizer.eos_token






 def        _lowerCamelCase					(       self       :							int							,	__A       :							int=None							,	__A       :							Union[str, Any]=None							,	__A       :							Tuple=None							,	**__A       :							List[Any]	):
        __UpperCamelCase       =  {}
        __UpperCamelCase       =  {}
        __UpperCamelCase       =  {}

        if min_length_for_response is not None:
               __UpperCamelCase       =  min_length_for_response
        if minimum_tokens is not None:
               __UpperCamelCase       =  minimum_tokens

        if "max_length" in generate_kwargs:
               __UpperCamelCase       =  generate_kwargs['max_length']
               # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length)
        if clean_up_tokenization_spaces is not None:
               __UpperCamelCase       =  clean_up_tokenization_spaces

        if generate_kwargs:
               forward_params.update(__A	)
        return preprocess_params, forward_params, postprocess_params






 def __call__(       self       :							Any							,	__A       :							Union[Conversation, List[Conversation]]							,	__A       :							List[Any]=0							,	**__A       :							Any	):
        __UpperCamelCase       =  super().__call__(__A							,	num_workers=__A							,	**__A	)
        if isinstance(__A							,	__A	) and len(__A	) == 1:
               return outputs[0]
        return outputs






 def        _lowerCamelCase					(       self       :							List[str]							,	__A       :							Conversation							,	__A       :							Tuple=3_2	):
        if not isinstance(__A							,	__A	):
               raise ValueError('ConversationalPipeline, expects Conversation as inputs'	)
        if conversation.new_user_input is None:
               raise ValueError(
                   f'''Conversation with UUID {type(conversation.uuid	)} does not contain new user input to process. '''
                   'Add user inputs with the conversation\'s `add_user_input` method'	)
        if hasattr(self.tokenizer							,	'_build_conversation_input_ids'	):
               __UpperCamelCase       =  self.tokenizer._build_conversation_input_ids(__A	)
        else:
               # If the tokenizer cannot handle conversations, we default to only the old version
               __UpperCamelCase       =  self._legacy_parse_and_tokenize(__A	)

        if self.framework == "pt":
               __UpperCamelCase       =  torch.LongTensor([input_ids]	)
        elif self.framework == "tf":
               __UpperCamelCase       =  tf.constant([input_ids]	)
        return {"input_ids": input_ids, "conversation": conversation}






 def        _lowerCamelCase					(       self       :							str							,	__A       :							List[Any]							,	__A       :							List[Any]=1_0							,	**__A       :							str	):
        __UpperCamelCase       =  generate_kwargs.get('max_length'							,	self.model.config.max_length	)

        __UpperCamelCase       =  model_inputs['input_ids'].shape[1]
        if max_length - minimum_tokens < n:
               logger.warning(f'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})'''	)
               __UpperCamelCase       =  max_length - minimum_tokens
               __UpperCamelCase       =  model_inputs['input_ids'][:, -trim:]
               if "attention_mask" in model_inputs:
                      __UpperCamelCase       =  model_inputs['attention_mask'][:, -trim:]
        __UpperCamelCase       =  model_inputs.pop('conversation'	)
        __UpperCamelCase       =  max_length
        __UpperCamelCase       =  self.model.generate(**__A							,	**__A	)
        if self.model.config.is_encoder_decoder:
               __UpperCamelCase       =  1
        else:
               __UpperCamelCase       =  n
        return {"output_ids": output_ids[:, start_position:], "conversation": conversation}






 def        _lowerCamelCase					(       self       :							Optional[int]							,	__A       :							Union[str, Any]							,	__A       :							Optional[Any]=True	):
        __UpperCamelCase       =  model_outputs['output_ids']
        __UpperCamelCase       =  self.tokenizer.decode(
            output_ids[0]							,	skip_special_tokens=__A							,	clean_up_tokenization_spaces=__A							,	)
        __UpperCamelCase       =  model_outputs['conversation']
        conversation.mark_processed()
        conversation.append_response(__A	)
        return conversation






 def        _lowerCamelCase					(       self       :							List[Any]							,	__A       :							Conversation	):
        __UpperCamelCase       =  self.tokenizer.eos_token_id
        __UpperCamelCase       =  []
        for is_user, text in conversation.iter_texts():
               if eos_token_id is not None:
                      input_ids.extend(self.tokenizer.encode(__A							,	add_special_tokens=__A	) + [eos_token_id]	)
               else:
                      input_ids.extend(self.tokenizer.encode(__A							,	add_special_tokens=__A	)	)

        if len(__A	) > self.tokenizer.model_max_length:
               __UpperCamelCase       =  input_ids[-self.tokenizer.model_max_length :]
        return input_ids







'''simple docstring'''
from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast
from ...utils import logging


a__							:							Any =logging.get_logger(__name__)

a__							:							Optional[Any] ={
    '''EleutherAI/gpt-neo-1.3B''': '''https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json''',
    # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo
}

class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					Dict					="gpt_neo"
 SCREAMING_SNAKE_CASE_	:					Optional[int]					=["past_key_values"]
 SCREAMING_SNAKE_CASE_	:					List[Any]					={"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"}






 def __init__(       self       :							Union[str, Any]							,	__A       :							Union[str, Any]=5_0_2_5_7							,	__A       :							Any=2_0_4_8							,	__A       :							Optional[Any]=2_0_4_8							,	__A       :							Any=2_4							,	__A       :							Union[str, Any]=[[["global", "local"], 1_2]]							,	__A       :							str=1_6							,	__A       :							Optional[int]=None							,	__A       :							Union[str, Any]=2_5_6							,	__A       :							Any="gelu_new"							,	__A       :							Dict=0.0							,	__A       :							Optional[int]=0.0							,	__A       :							int=0.0							,	__A       :							List[str]=0.1							,	__A       :							Any=1e-5							,	__A       :							int=0.02							,	__A       :							List[str]=True							,	__A       :							Tuple=5_0_2_5_6							,	__A       :							Optional[Any]=5_0_2_5_6							,	**__A       :							Optional[Any]							,	):
        __UpperCamelCase       =  vocab_size
        __UpperCamelCase       =  max_position_embeddings
        __UpperCamelCase       =  hidden_size
        __UpperCamelCase       =  num_layers
        __UpperCamelCase       =  num_heads
        __UpperCamelCase       =  intermediate_size
        __UpperCamelCase       =  window_size
        __UpperCamelCase       =  activation_function
        __UpperCamelCase       =  resid_dropout
        __UpperCamelCase       =  embed_dropout
        __UpperCamelCase       =  attention_dropout
        __UpperCamelCase       =  classifier_dropout
        __UpperCamelCase       =  layer_norm_epsilon
        __UpperCamelCase       =  initializer_range
        __UpperCamelCase       =  use_cache

        __UpperCamelCase       =  bos_token_id
        __UpperCamelCase       =  eos_token_id

        __UpperCamelCase       =  attention_types
        __UpperCamelCase       =  self.expand_attention_types_params(__A	)

        if len(self.attention_layers	) != self.num_layers:
               raise ValueError(
                   'Configuration for convolutional module is incorrect. '
                   'It is required that `len(config.attention_layers)` == `config.num_layers` '
                   f'''but is `len(config.attention_layers) = {len(self.attention_layers	)}`, '''
                   f'''`config.num_layers = {self.num_layers}`. '''
                   '`config.attention_layers` is prepared using `config.attention_types`. '
                   'Please verify the value of `config.attention_types` argument.'	)

        super().__init__(bos_token_id=__A							,	eos_token_id=__A							,	**__A	)






 @staticmethod
 def        _lowerCamelCase					(       __A       :							Tuple	):
        __UpperCamelCase       =  []
        for item in attention_types:
               for _ in range(item[1]	):
                      attentions.extend(item[0]	)
        return attentions






def 				lowercase__   (   __lowercase						:  Tuple				,  __lowercase						:  Any				,  __lowercase						:  Union[str, Any]				,  __lowercase						:  List[str]     ) ->      Any:



       """simple docstring"""






       import torch

       __UpperCamelCase       =  input.size()
       __UpperCamelCase       =  len(__lowercase     )
       __UpperCamelCase       =  shape[dimension]

       __UpperCamelCase       =  torch.arange(0				,  __lowercase				,  __lowercase     )
       __UpperCamelCase       =  torch.div(sizedim - size				,  __lowercase				,  rounding_mode='floor'     ) + 1
       __UpperCamelCase       =  torch.arange(__lowercase     ) + low_indices[:min_length][:, None]

       __UpperCamelCase       =  [slice(__lowercase     )] * rank
       __UpperCamelCase       =  indices
       __UpperCamelCase       =  input[s]

       __UpperCamelCase       =  list(range(0				,  rank + 1     )     )
       perm.append(perm.pop(dimension + 1     )     )

       return sliced.permute(__lowercase     )
def 				lowercase__   (   __lowercase						:  Union[str, Any]				,  __lowercase						:  Optional[int]     ) ->      Optional[int]:



       """simple docstring"""






       import torch

       __UpperCamelCase       =  torch.arange(1				,  __lowercase     )
       __UpperCamelCase       =  torch.remainder(__lowercase				,  __lowercase     )
       __UpperCamelCase       =  remainders == 0
       __UpperCamelCase       =  candidates[divisor_indices]
       __UpperCamelCase       =  torch.max(__lowercase     )
       return largest_divisor, torch.div(__lowercase				,  __lowercase				,  rounding_mode='floor'     )


class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 @property
 def        _lowerCamelCase					(       self       :							Tuple	):
        __UpperCamelCase       =  OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}	)
        if self.use_past:
               self.fill_with_past_key_values_(__A							,	direction='inputs'	)
               __UpperCamelCase       =  {0: 'batch', 1: 'past_sequence + sequence'}
        else:
               __UpperCamelCase       =  {0: 'batch', 1: 'sequence'}

        return common_inputs






 @property
 def        _lowerCamelCase					(       self       :							int	):
        return self._config.num_heads






 def        _lowerCamelCase					(       self       :							List[str]							,	__A       :							PreTrainedTokenizer							,	__A       :							int = -1							,	__A       :							int = -1							,	__A       :							bool = False							,	__A       :							Optional[TensorType] = None							,	):
        __UpperCamelCase       =  super(__A							,	self	).generate_dummy_inputs(
            __A							,	batch_size=__A							,	seq_length=__A							,	is_pair=__A							,	framework=__A	)

        # We need to order the input in the way they appears in the forward()
        __UpperCamelCase       =  OrderedDict({'input_ids': common_inputs['input_ids']}	)

        # Need to add the past_keys
        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       =  (
                          batch,
                          self.num_attention_heads,
                          past_key_values_length,
                          self._config.hidden_size // self.num_attention_heads,
                      )
                      __UpperCamelCase       =  [
                          (torch.zeros(__A	), torch.zeros(__A	)) for _ in range(self.num_layers	)
                      ]

        __UpperCamelCase       =  common_inputs['attention_mask']
        if self.use_past:
               __UpperCamelCase       =  ordered_inputs['attention_mask'].dtype
               __UpperCamelCase       =  torch.cat(
                   [ordered_inputs['attention_mask'], torch.ones(__A							,	__A							,	dtype=__A	)]							,	dim=1	)

        return ordered_inputs






 @property
 def        _lowerCamelCase					(       self       :							Dict	):
        return 1_3
