CoolFace
Apppublic

andreslu/orion

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
bart_with_group_beam.cpython-38.pyc257 linesDownload Raw Back to __pycache__
1U

2��bvw�@s�ddlmZddlZddlmZddlmZmZddlm	Z	ddl3mZmZm
Z
mZmZddlmZddlmZmZdd	lmZdd4lmZddlZe
eefZGdd�de�ZdS)
�)�BartForConditionalGenerationN)�5BeamScorer)�ABC�abstractmethod)�UserDict)�Optional�Tuple�Union�Dict�Any)�LogitsProcessorList)�BeamSearchEncoderDecoderOutput�BeamSearchDecoderOnlyOutput)�6functional)�ModelOutputc@s�eZdZdejeeeeeeeeeee	ee	ee	ee	e7eejfd�dd�Zd	ejeeeeeeeeeee	ee	ee	ee	d�8dd�Z
dS)9�&BartForConditionalGeneration_GroupBeamN)�	input_ids�beam_scorer�logits_processor�10max_length�pad_token_id�eos_token_id�output_attentions�output_hidden_states�
output_scores�return_dict_in_generate�returnc%	Ks8|dk	r|nt�}|dk	r|n|jj}|dk	r2|n|jj}|dk	rF|n|jj}|	dk	rZ|	n|jj}	|dk	rn|n|jj}|dk	r�|n|jj}|11dk	r�|12n|jj}13|14r�|	r�dnd}|15r�|r�dnd}
|16r�|r�dnd}|17�r|jj	�r|r�|d�18d�nd}|�r|d�19d�nd}t|j�}|j
}|j\}}|||k�s<td��tj||ftj|jd�}d|dd�d	d�f<|�||f�}||k�r�|j|f|�}|f|d20||d���}|jdd�ddd�f}|j|||d
�}tj|dd�}|||�}||dd�df�|�}|21�r^|	�r||f7}|�r:|
|jj	�r0|jfn|jf7}
|�r^||jj	�rT|jfn|jf7}|jd}|�|||�}tj|dd22d�� |d�|}t!|�"d��D]N}|d|D]:}t!|�D]*}||||||||||<�q��q��q�tj#|d|d	d23d24d�\}}||} ||}|j$|||| ||d�}!|!d}|!d}"|!d}#tj%||#dd�f|"�&d�gdd�}|d	}|j'|||jj	d�}|ddk	�r�|�(|d|#�|d<|j)�rx�q��qx|j*|||| ||d�}$|25�r,|	�s�d|$d<|jj	�rt+|$d|$d||||
|d�St,|$d|$d||
|d�Sn|$dSdS)a�26        Generates sequences for models with a language modeling head using beam search decoding.27 28        Parameters:29 30            input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):31                The sequence used as a prompt for the generation. If :obj:`None` the method initializes it as an empty32                :obj:`torch.LongTensor` of shape :obj:`(1,)`.33            beam_scorer (:obj:`BeamScorer`):34                An derived instance of :class:`~transformers.BeamScorer` that defines how beam hypotheses are35                constructed, stored and sorted during generation. For more information, the documentation of36                :class:`~transformers.BeamScorer` should be read.37            logits_processor (:obj:`LogitsProcessorList`, `optional`):38                An instance of :class:`~transformers.LogitsProcessorList`. List of instances of class derived from39                :class:`~transformers.LogitsProcessor` used to modify the prediction scores of the language modeling40                head applied at each generation step.41            max_length (:obj:`int`, `optional`, defaults to 20):42                The maximum length of the sequence to be generated.43            pad_token_id (:obj:`int`, `optional`):44                The id of the `padding` token.45            eos_token_id (:obj:`int`, `optional`):46                The id of the `end-of-sequence` token.47            output_attentions (:obj:`bool`, `optional`, defaults to `False`):48                Whether or not to return the attentions tensors of all attention layers. See ``attentions`` under49                returned tensors for more details.50            output_hidden_states (:obj:`bool`, `optional`, defaults to `False`):51                Whether or not to return trhe hidden states of all layers. See ``hidden_states`` under returned tensors52                for more details.53            output_scores (:obj:`bool`, `optional`, defaults to `False`):54                Whether or not to return the prediction scores. See ``scores`` under returned tensors for more details.55            return_dict_in_generate (:obj:`bool`, `optional`, defaults to `False`):56                Whether or not to return a :class:`~transformers.file_utils.ModelOutput` instead of a plain tuple.57            model_kwargs:58                Additional model specific kwargs will be forwarded to the :obj:`forward` function of the model. If59                model is an encoder-decoder model the kwargs should include :obj:`encoder_outputs`.60 61        Return:62            :class:`~transformers.generation_utilsBeamSearchDecoderOnlyOutput`,63            :class:`~transformers.generation_utils.BeamSearchEncoderDecoderOutput` or obj:`torch.LongTensor`: A64            :obj:`torch.LongTensor` containing the generated tokens (default behaviour) or a65            :class:`~transformers.generation_utils.BeamSearchDecoderOnlyOutput` if66            ``model.config.is_encoder_decoder=False`` and ``return_dict_in_generate=True`` or a67            :class:`~transformers.generation_utils.BeamSearchEncoderDecoderOutput` if68            ``model.config.is_encoder_decoder=True``.69 70 71        Examples::72 73            >>> from transformers import (74            ...    AutoTokenizer,75            ...    AutoModelForSeq2SeqLM,76            ...    LogitsProcessorList,77            ...    MinLengthLogitsProcessor,78            ...    BeamSearchScorer,79            ... )80            >>> import torch81 82            >>> tokenizer = AutoTokenizer.from_pretrained("t5-base")83            >>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")84 85            >>> encoder_input_str = "translate English to German: How old are you?"86            >>> encoder_input_ids = tokenizer(encoder_input_str, return_tensors="pt").input_ids87 88 89            >>> # lets run beam search using 3 beams90            >>> num_beams = 391            >>> # define decoder start token ids92            >>> input_ids = torch.ones((num_beams, 1), device=model.device, dtype=torch.long)93            >>> input_ids = input_ids * model.config.decoder_start_token_id94 95            >>> # add encoder_outputs to model keyword arguments96            >>> model_kwargs = {97            ...     "encoder_outputs": model.get_encoder()(encoder_input_ids.repeat_interleave(num_beams, dim=0), return_dict=True)98            ... }99 100            >>> # instantiate beam scorer101            >>> beam_scorer = BeamSearchScorer(102            ...     batch_size=1,103            ...     max_length=model.config.max_length,104            ...     num_beams=num_beams,105            ...     device=model.device,106            ... )107 108            >>> # instantiate logits processors109            >>> logits_processor = LogitsProcessorList([110            ...     MinLengthLogitsProcessor(5, eos_token_id=model.config.eos_token_id),111            ... ])112 113            >>> outputs = model.beam_search(input_ids, beam_scorer, logits_processor=logits_processor, **model_kwargs)114 115            >>> print("Generated:", tokenizer.batch_decode(outputs, skip_special_tokens=True))116        N��encoder_outputs�117attentions�
hidden_statesz\Batch dimension of `input_ids` should be {num_beams * batch_size}, but is {batch_beam_size}.��dtype�device�e����T��return_dictrr�������cur_lenr��dimr�r,�keepdim�decoder_ori_input_ids��r,�largest�sorted�rr�next_beam_scores�next_beam_tokens�next_beam_indices��is_encoder_decoder�past�sequence_scores�	sequences�r<�sequences_scores�scores�encoder_attentions�encoder_hidden_states�decoder_attentions�decoder_hidden_states�r<r>r?rr )-r�configrrrrrrrr9�get�len�118_beam_hyps�	num_beams�shape�AssertionError�torch�zeros�floatr#�view�prepare_inputs_for_generation�logits�adjust_logits_during_generation�F�log_softmax�	expand_asrBrrCr �sum�expand�range�size�topk�process�cat�	unsqueeze�#_update_model_kwargs_for_generation�_reorder_cache�is_done�finalizer
r)%�selfrrrrrrrrrr�model_kwargsr?rBrCr@rA�119batch_sizerI�batch_beam_sizer*�beam_scores�model_inputs�outputs�next_token_logits�next_token_scores�120vocab_size�next_token_scores_group�i�t�j�next_tokens�next_indices�beam_outputs�beam_next_tokens�beam_idx�sequence_outputsrr�D/Users/lusheng/毕业论文/Orion-master/src/bart_with_group_beam.py�beam_searchs�l���121122123��124�125�126127���1280�129�$��130�131�z2BartForConditionalGeneration_GroupBeam.beam_search)132rrrrrrrrrrc0	s�|dk	r|nt�}|dk	r|n|jj}|dk	r2|n|jj}|dk	rF|n|jj}|	dk	rZ|	n|jj}	|dk	rn|n|jj}|dk	r�|n|jj}|133dk	r�|134n|jj}135|136r�|	r�dnd}|137r�|r�dnd}
|138r�|r�dnd}|139�r|jj	�r|r�|d�140d�nd}|�r|d�141d�nd}t|j�}|j
�|j}�|}|j}|j\}}�||k�sbtd�|�d|�d���tj|�fd	tj|d142�}d|dd�dd|�f<|�|�f�}||k�rtj|�|j|d143�}tj|�tj|d144�}|j|f|�}|f|d||d
���}t|�D�]F}||}t||��}||}g} |	�rRt�|jdd�ddd�f���}!t|�D]&�| ���fdd�t||�D���qZ|| }"|j| ddd�f}#|j |#||d�}#t!j"|#dd�}$|$jd}%||"|$||d�}$|$|| �#d��$|$�}$|	�r|$��|!| <|$�|||%�}$tj%|$ddd��&|d�|}&t|$�'d��D]N}'|d|'D]:}(t|�D]*})|$|'|)|%|(|&|'|)|%|(<�qV�qJ�q:tj(|&d|dddd�\}$}*|*|%}+|*|%}*|j)|"|$|*|+||d�},|,d|| <|,d}-|,d}.|"|.|| <tj*|"|.dd�f|-�#d�gdd�}"|"dd�df|| <�|.|||.||| <�q|145�r�|	�rb||!f7}|�r�|
|jj	�r||j+fn|j,f7}
|�r�||jj	�r�|j-fn|j.f7}|j/|||jj	d�}|ddk	�r�|�0|d|�|d<tj*||�#d�gdd�}|d}|j1�r��q�q�|j2|||*|+|||d�}/|146�r�|	�s<|/d |jj	�rdt3|/d!|/d ||||
|d"�St4|/d!|/d ||
|d#�Sn|/d!SdS)$a�147        Generates sequences for models with a language modeling head using beam search decoding.148 149        Parameters:150 151            input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):152                The sequence used as a prompt for the generation. If :obj:`None` the method initializes it as an empty153                :obj:`torch.LongTensor` of shape :obj:`(1,)`.154            beam_scorer (:obj:`BeamScorer`):155                An derived instance of :class:`~transformers.BeamScorer` that defines how beam hypotheses are156                constructed, stored and sorted during generation. For more information, the documentation of157                :class:`~transformers.BeamScorer` should be read.158            logits_processor (:obj:`LogitsProcessorList`, `optional`):159                An instance of :class:`~transformers.LogitsProcessorList`. List of instances of class derived from160                :class:`~transformers.LogitsProcessor` used to modify the prediction scores of the language modeling161                head applied at each generation step.162            max_length (:obj:`int`, `optional`, defaults to 20):163                The maximum length of the sequence to be generated.164            pad_token_id (:obj:`int`, `optional`):165                The id of the `padding` token.166            eos_token_id (:obj:`int`, `optional`):167                The id of the `end-of-sequence` token.168            output_attentions (:obj:`bool`, `optional`, defaults to `False`):169                Whether or not to return the attentions tensors of all attention layers. See ``attentions`` under170                returned tensors for more details.171            output_hidden_states (:obj:`bool`, `optional`, defaults to `False`):172                Whether or not to return trhe hidden states of all layers. See ``hidden_states`` under returned tensors173                for more details.174            output_scores (:obj:`bool`, `optional`, defaults to `False`):175                Whether or not to return the prediction scores. See ``scores`` under returned tensors for more details.176            return_dict_in_generate (:obj:`bool`, `optional`, defaults to `False`):177                Whether or not to return a :class:`~transformers.file_utils.ModelOutput` instead of a plain tuple.178            model_kwargs:179                Additional model specific kwargs that will be forwarded to the :obj:`forward` function of the model. If180                model is an encoder-decoder model the kwargs should include :obj:`encoder_outputs`.181 182        Return:183            :class:`~transformers.generation_utils.BeamSearchDecoderOnlyOutput`,184            :class:`~transformers.generation_utils.BeamSearchEncoderDecoderOutput` or obj:`torch.LongTensor`: A185            :obj:`torch.LongTensor` containing the generated tokens (default behaviour) or a186            :class:`~transformers.generation_utils.BeamSearchDecoderOnlyOutput` if187            :class:`~transformers.generation_utils.BeamSearchDecoderOnlyOutput` if188            ``model.config.is_encoder_decoder=False`` and ``return_dict_in_generate=True`` or a189            :class:`~transformers.generation_utils.BeamSearchEncoderDecoderOutput` if190            ``model.config.is_encoder_decoder=True``.191 192        Examples::193 194            >>> from transformers import (195            ...    AutoTokenizer,196            ...    AutoModelForSeq2SeqLM,197            ...    LogitsProcessorList,198            ...    MinLengthLogitsProcessor,199            ...    HammingDiversityLogitsProcessor,200            ...    BeamSearchScorer,201            ... )202            >>> import torch203 204            >>> tokenizer = AutoTokenizer.from_pretrained("t5-base")205            >>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")206 207            >>> encoder_input_str = "translate English to German: How old are you?"208            >>> encoder_input_ids = tokenizer(encoder_input_str, return_tensors="pt").input_ids209 210 211            >>> # lets run diverse beam search using 6 beams212            >>> num_beams = 6213            >>> # define decoder start token ids214            >>> input_ids = torch.ones((num_beams, 1), device=model.device, dtype=torch.long)215            >>> input_ids = input_ids * model.config.decoder_start_token_id216 217            >>> # add encoder_outputs to model keyword arguments218            >>> model_kwargs = {219            ...     "encoder_outputs": model.get_encoder()(encoder_input_ids.repeat_interleave(num_beams, dim=0), return_dict=True)220            ... }221 222            >>> # instantiate beam scorer223            >>> beam_scorer = BeamSearchScorer(224            ...     batch_size=1,225            ...     max_length=model.config.max_length,226            ...     num_beams=num_beams,227            ...     device=model.device,228            ...     num_beam_groups=3229            ... )230 231            >>> # instantiate logits processors232            >>> logits_processor = LogitsProcessorList([233            ...     HammingDiversityLogitsProcessor(5.5, num_beams=6, num_beam_groups=3),234            ...     MinLengthLogitsProcessor(5, eos_token_id=model.config.eos_token_id),235            ... ])236 237            >>> outputs = model.group_beam_search(input_ids, beam_scorer, logits_processor=logits_processor, **model_kwargs)238 239            >>> print("Generated:", tokenizer.batch_decode(outputs, skip_special_tokens=True))240        Nrrrr z)Batch dimension of `input_ids` should be z	, but is �.r$r!rTr&r(csg|]}��|�qSrr)�.0�idx��	batch_idxrIrrv�241<listcomp>�szLBartForConditionalGeneration_GroupBeam.group_beam_search.<locals>.<listcomp>r)r+)�current_tokens�beam_group_idxr-r/r0r%r1r4r5r6r7r8r:)rrrr;r<r=rD)5rrErrrrrrrr9rFrGrHrI�num_beam_groupsr#rJrKrL�fullrNrOrMr"�longrPrX�min�242zeros_likerQ�half�extendrRrSrTr]rUrVrWrYrZr[r\rBrrCr r^r_r`rar
r)0rbrrrrrrrrrrrcr?rBrCr@rArdr��
num_sub_beamsr#rer*rfr~�reordering_indicesrgrhr�group_start_idx�
group_end_idx�243group_size�batch_group_indices�processed_score�group_input_idsrirjrkrlrmrnrorprqrrrsrtrurr{rv�group_beam_search(s@o���244245246��247�248"��249����0�250	�$�251252�����253�254�z8BartForConditionalGeneration_GroupBeam.group_beam_search)NNNNNNNN)NNNNNNNN)�__name__�255__module__�__qualname__rL�256LongTensorrrr�int�boolr	�BeamSearchOutputrwr�rrrrvrsV����r)Ztransformers.models.bartrrLZ#transformers.generation_beam_searchr�abcrr�collectionsr�typingrrr	r257rZ&transformers.generation_logits_processrZtransformers.generation_utilsr
r�torch.nnrrSZtransformers.file_utilsrr�rrrrrv�<module>s