1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
| import transformers
def generate( self, inputs: Optional[torch.Tensor] = None, # 有两种输入方法, 一种是输入序列input_ids传入inputs, 一种是input_ids和attention_mask以字典的形式传入**kwargs generation_config: Optional[GenerationConfig] = None, # 解码超参数, 如top_k, top_p, max_length等, 可以加载GenerationConfig类创建对象来配置参数, 类似解码超参数的**kwargs; 也可以在model.generate()中直接传入参数 logits_processor: Optional[LogitsProcessorList] = None, # 解码超参数会将不同的策略函数放到一个处理器中, 貌似是用于加载自定义解码超参数的 stopping_criteria: Optional[StoppingCriteriaList] = None, # 停止条件, 用于控制生成的长度, 例如max_length, max_time等 prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None, # 前缀抑制token方程, 也与解码策略有关 synced_gpus: Optional[bool] = None, # 不同GPU之间的同步, 用于多GPU环境下, 避免某个GPU提前完成生成导致其他GPU阻塞 assistant_model: Optional["PreTrainedModel"] = None, # 用于投机解码, 小模型辅助大模型生成 streamer: Optional["BaseStreamer"] = None, # 流式处理, 用于处理生成的token, 例如将token写入文件 negative_prompt_ids: Optional[torch.Tensor] = None, # 负面提示, 一些前沿研究算法会用到, 下同 negative_prompt_attention_mask: Optional[torch.Tensor] = None, **kwargs, ) -> Union[GenerateOutput, torch.LongTensor]: r"""
Generates sequences of token ids for models with a language modeling head.
<Tip warning={true}>
Most generation-controlling parameters are set in `generation_config` which, if not passed, will be set to the model's default generation configuration. You can override any `generation_config` by passing the corresponding parameters to generate(), e.g. `.generate(inputs, num_beams=4, do_sample=True)`.
For an overview of generation strategies and code examples, check out the [following guide](../generation_strategies).
</Tip>
Parameters: inputs (`torch.Tensor` of varying shape depending on the modality, *optional*): The sequence used as a prompt for the generation or as model inputs to the encoder. If `None` the method initializes it with `bos_token_id` and a batch size of 1. For decoder-only models `inputs` should of in the format of `input_ids`. For encoder-decoder models *inputs* can represent any of `input_ids`, `input_values`, `input_features`, or `pixel_values`. generation_config (`~generation.GenerationConfig`, *optional*): The generation configuration to be used as base parametrization for the generation call. `**kwargs` passed to generate matching the attributes of `generation_config` will override them. If `generation_config` is not provided, the default will be used, which had the following loading priority: 1) from the `generation_config.json` model file, if it exists; 2) from the model configuration. Please note that unspecified parameters will inherit [`~generation.GenerationConfig`]'s default values, whose documentation should be checked to parameterize generation. logits_processor (`LogitsProcessorList`, *optional*): Custom logits processors that complement the default logits processors built from arguments and generation config. If a logit processor is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. stopping_criteria (`StoppingCriteriaList`, *optional*): Custom stopping criteria that complement the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. If your stopping criteria depends on the `scores` input, make sure you pass `return_dict_in_generate=True, output_scores=True` to `generate`. This feature is intended for advanced users. prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID `batch_id` and `input_ids`. It has to return a list with the allowed tokens for the next generation step conditioned on the batch ID `batch_id` and the previously generated tokens `inputs_ids`. This argument is useful for constrained generation conditioned on the prefix, as described in [Autoregressive Entity Retrieval](https://arxiv.org/abs/2010.00904). synced_gpus (`bool`, *optional*): Whether to continue running the while loop until max_length. Unless overridden this flag will be set to `True` under DeepSpeed ZeRO Stage 3 multiple GPUs environment to avoid hanging if one GPU finished generating before other GPUs. Otherwise it'll be set to `False`. assistant_model (`PreTrainedModel`, *optional*): An assistant model that can be used to accelerate generation. The assistant model must have the exact same tokenizer. The acceleration is achieved when forecasting candidate tokens with the assistent model is much faster than running generation with the model you're calling generate from. As such, the assistant model should be much smaller. streamer (`BaseStreamer`, *optional*): Streamer object that will be used to stream the generated sequences. Generated tokens are passed through `streamer.put(token_ids)` and the streamer is responsible for any further processing. negative_prompt_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): The negative prompt needed for some processors such as CFG. The batch size must match the input batch size. This is an experimental feature, subject to breaking API changes in future versions. negative_prompt_attention_mask (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Attention_mask for `negative_prompt_ids`. kwargs (`Dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*.
Return: [`~utils.ModelOutput`] or `torch.LongTensor`: A [`~utils.ModelOutput`] (if `return_dict_in_generate=True` or when `config.return_dict_in_generate=True`) or a `torch.FloatTensor`.
If the model is *not* an encoder-decoder model (`model.config.is_encoder_decoder=False`), the possible [`~utils.ModelOutput`] types are:
- [`~generation.GenerateDecoderOnlyOutput`], - [`~generation.GenerateBeamDecoderOnlyOutput`]
If the model is an encoder-decoder model (`model.config.is_encoder_decoder=True`), the possible [`~utils.ModelOutput`] types are:
- [`~generation.GenerateEncoderDecoderOutput`], - [`~generation.GenerateBeamEncoderDecoderOutput`] """
# 模型参数分到多个GPU上, 需要同步多个GPU, 防止某个GPU生成EOS结束生成, 其他GPU阻塞. FSDP或ZERO3会涉及模型参数分片 if synced_gpus is None: if is_deepspeed_zero3_enabled() and dist.get_world_size() > 1: synced_gpus = True else: synced_gpus = False
# 1. Handle `generation_config` and kwargs that might update it, and validate the `.generate()` call # 整合了传入的 generation_config (对象) 和 kwargs 中的参数, 并对传入的参数进行验证 self._validate_model_class() # 看当前任务是否支持使用 GenerationMixin 下的 generate 函数
# priority: `generation_config` argument > `model.generation_config` (the default generation config) # 优先级: 传入的 generation_config 参数 > model.generation_config, 即在 generation_config 参数中设置的参数会覆盖 model.generation_config 中的参数, 如果前者没有传入就去找后者, 如若两者不相同报 Warning if generation_config is None: # legacy: users may modify the model configuration to control generation. To trigger this legacy behavior, # three conditions must be met # 1) the generation config must have been created from the model config (`_from_model_config` field); # 2) the generation config must have seen no modification since its creation (the hash is the same); # 3) the user must have set generation parameters in the model config. if ( self.generation_config._from_model_config and self.generation_config._original_object_hash == hash(self.generation_config) and self.config._has_non_default_generation_parameters() ): new_generation_config = GenerationConfig.from_model_config(self.config) if new_generation_config != self.generation_config: warnings.warn( "You have modified the pretrained model configuration to control generation. This is a" " deprecated strategy to control generation and will be removed soon, in a future version." " Please use and modify the model generation configuration (see" " https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )" ) self.generation_config = new_generation_config generation_config = self.generation_config
## 将 kwargs 中的参数更新到 generation_config 中, 这里的generate_config.update是GenerationMixin类中的方法, 不是python字典的update generation_config = copy.deepcopy(generation_config) model_kwargs = generation_config.update(**kwargs) # All unused kwargs must be model kwargs generation_config.validate() self._validate_model_kwargs(model_kwargs.copy())
# 2. Set generation parameters if not already defined logits_processor = logits_processor if logits_processor is not None else LogitsProcessorList() # 解码处理器列表, 用于处理logits stopping_criteria = stopping_criteria if stopping_criteria is not None else StoppingCriteriaList() # 停止条件列表, 用于控制生成的长度
if generation_config.pad_token_id is None and generation_config.eos_token_id is not None: if model_kwargs.get("attention_mask", None) is None: logger.warning( "The attention mask and the pad token id were not set. As a consequence, you may observe " "unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results." ) eos_token_id = generation_config.eos_token_id if isinstance(eos_token_id, list): eos_token_id = eos_token_id[0] logger.warning(f"Setting `pad_token_id` to `eos_token_id`:{eos_token_id} for open-end generation.") generation_config.pad_token_id = eos_token_id
# 3. Define model inputs # inputs_tensor has to be defined # model_input_name is defined if model-specific keyword input is passed # otherwise model_input_name is None # all model-specific keyword inputs are removed from `model_kwargs` # 两种输入方式: 1. inpusts 2. input_ids 和 attention_mask 输入进 key words(kwargs) # 下面的函数自适应输入方式, 将输入整合到 inputs_tensor 中, 并返回 model_input_name 和 model_kwargs # self._prepare_model_inputs 是 GenerationMixin 类中的方法, generate 函数后面也有贴 inputs_tensor, model_input_name, model_kwargs = self._prepare_model_inputs( inputs, generation_config.bos_token_id, model_kwargs ) batch_size = inputs_tensor.shape[0]
# 4. Define other model kwargs model_kwargs["output_attentions"] = generation_config.output_attentions model_kwargs["output_hidden_states"] = generation_config.output_hidden_states # decoder-only models with inputs_embeds forwarding must use caching (otherwise we can't detect whether we are generating the first new token or not, and we only want to use the embeddings for the first new token) # 这段话的意思是, Decoder-Only 模型仅在生成第一个 token 时使用 input_embeds, 后续 token 都用 input_ids 生成. 这是因为, 如果所有 token 都用 input_embeds 生成, 模型无法判断当前处于生成过程的哪个阶段 (每次都是输入一个 embedding, 那现在是第一次输入还是自回归预测过程呢?), 所以只在第一个 token 时使用 input_embeds # Decoder-Only 模型有 input_embeds 输入时, 必须使用缓存, 否则无法判断是否生成第一个新 token, 仅在第一个 token 时使用 input_embeds, 后续 token 都用 input_ids 生成 # TODO: 不理解上面这段话 if not self.config.is_encoder_decoder and model_input_name == "inputs_embeds": model_kwargs["use_cache"] = True else: model_kwargs["use_cache"] = generation_config.use_cache
# 判断在self.forward 中是否输入了 attention mask, accepts_attention_mask = "attention_mask" in set(inspect.signature(self.forward).parameters.keys()) # 判断在model_kwargs 中是否输入了 encoder_outputs, 即是否是encoder-decoder模型 requires_attention_mask = "encoder_outputs" not in model_kwargs
# 设置 attention_mask, 如果没有输入且需要输入, 则根据输入生成 attention_mask, 一般生成的 attention_mask 为全 1, 特殊情况如 inputs 里已经有 padding 或输入 pad_token_id if model_kwargs.get("attention_mask", None) is None and requires_attention_mask and accepts_attention_mask: model_kwargs["attention_mask"] = self._prepare_attention_mask_for_generation( inputs_tensor, generation_config.pad_token_id, generation_config.eos_token_id )
# decoder-only models should use left-padding for generation # Decoder-Only 模型应该使用左填充进行生成 # Decoder-Only 模型推理时采用 left-padding 的原因是, 模型的输入是对模型输入的延续 (模型的输出中会带着输入, 并在输入后边补充输出), 如果采用 right-padding, 会导致大量的 [pad]token 夹在模型的输入和输入之间, 不利于处理结果. 并且模型的输出句子的语义也被pad打乱了, 输入并不直观. 此外, Decoder-Only 的模型并不需要 cls 等开头的 token 来做额外的处理, right-padding 在 Decoder-Only 的模型中没有任何优势. # 右对齐应该也能实现同样的效果, 但是要多算一个 mask 矩阵, 算一遍 index, 还是左对齐优雅 # 参考: https://zhuanlan.zhihu.com/p/646852375 if not self.config.is_encoder_decoder: # If `input_ids` was given, check if the last id in any sequence is `pad_token_id` # Note: If using, `inputs_embeds` this check does not work, because we want to be more hands-off. if ( generation_config.pad_token_id is not None and len(inputs_tensor.shape) == 2 and torch.sum(inputs_tensor[:, -1] == generation_config.pad_token_id) > 0 ): logger.warning( "A decoder-only architecture is being used, but right-padding was detected! For correct " "generation results, please set `padding_side='left'` when initializing the tokenizer." )
# Encoder-Decoder 模型需要 encoder_outputs, 如果在 model_kwargs 中没有传入, 则需要准备 encoder_outputs # TODO: 这里的 encoder_outputs 是什么, 是只有解码阶段需要准备吗 if self.config.is_encoder_decoder and "encoder_outputs" not in model_kwargs: # if model is encoder decoder encoder_outputs are created and added to `model_kwargs` model_kwargs = self._prepare_encoder_decoder_kwargs_for_generation( inputs_tensor, model_kwargs, model_input_name )
# 5. Prepare `input_ids` which will be used for auto-regressive generation # TODO: 这里 Encoder-Decoder 模型输入处理没读 if self.config.is_encoder_decoder: input_ids, model_kwargs = self._prepare_decoder_input_ids_for_generation( batch_size=batch_size, model_input_name=model_input_name, model_kwargs=model_kwargs, decoder_start_token_id=generation_config.decoder_start_token_id, bos_token_id=generation_config.bos_token_id, device=inputs_tensor.device, ) else: # Decoder-Only 模型, 直接使用输入的 input_ids input_ids = inputs_tensor if model_input_name == "input_ids" else model_kwargs.pop("input_ids") # transformers 4.47.1 加了 token healing, 利于解码. 这里 4.37.1 还没有
# 流式输出 if streamer is not None: streamer.put(input_ids.cpu())
# 6. Prepare `max_length` depending on other stopping criteria. # 这里的 max_length 是 prompt+生成的 token 的总长度, max_new_length 是生成的 token 的总长度. 二者同时存在时, max_new_length 优先级更高, 但最后要更新到 max_length input_ids_length = input_ids.shape[-1] has_default_max_length = kwargs.get("max_length") is None and generation_config.max_length is not None if generation_config.max_new_tokens is not None: if not has_default_max_length and generation_config.max_length is not None: logger.warning( f"Both `max_new_tokens` (={generation_config.max_new_tokens}) and `max_length`(=" f"{generation_config.max_length}) seem to have been set. `max_new_tokens` will take precedence. " "Please refer to the documentation for more information. " "(https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)" ) generation_config.max_length = generation_config.max_new_tokens + input_ids_length # max_length 和 min_length 设置, 主要是安全检查报 warning self._validate_generated_length(generation_config, input_ids_length, has_default_max_length) # transformers 4.47.1 加了一个 num_logits_to_keep 的参数, 用于控制保留最近生成的几个 token 的logit, 这个还挺好玩的 # 新版本有 _prepare_cache_for_generation, 支持用户输入缓存, 还有很多其他小玩意. 这部分单独做了一个小节
# 7. determine generation mode # 决定生成模式 generation_mode = self._get_generation_mode(generation_config, assistant_model)
# 流式输出不能用束搜索 if streamer is not None and (generation_config.num_beams > 1): raise ValueError( "`streamer` cannot be used with beam search (yet!). Make sure that `num_beams` is set to 1." )
if self.device.type != input_ids.device.type: warnings.warn( "You are calling .generate() with the `input_ids` being on a device type different" f" than your model's device. `input_ids` is on {input_ids.device.type}, whereas the model" f" is on {self.device.type}. You may experience unexpected behaviors or slower generation." " Please make sure that you have put `input_ids` to the" f" correct device by calling for example input_ids = input_ids.to('{self.device.type}') before" " running `.generate()`.", UserWarning, )
# 8. prepare distribution pre_processing samplers # 设置解码策略 # TODO: 没有看 prepared_logits_processor = self._get_logits_processor( generation_config=generation_config, input_ids_seq_length=input_ids_length, encoder_input_ids=inputs_tensor, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn, logits_processor=logits_processor, model_kwargs=model_kwargs, negative_prompt_ids=negative_prompt_ids, negative_prompt_attention_mask=negative_prompt_attention_mask, )
# 9. prepare stopping criteria # 准备生成 eos 结束 token. 生成步数判断, 生成时间判断. 生成 token 数是可以大于最大编码数的, 但是效果会差很多 prepared_stopping_criteria = self._get_stopping_criteria( generation_config=generation_config, stopping_criteria=stopping_criteria ) # 10. go into different generation modes # 根据生成模式进行生成 if generation_mode == GenerationMode.ASSISTED_GENERATION: if generation_config.num_return_sequences > 1: raise ValueError( "num_return_sequences has to be 1 when doing assisted generate, " f"but is {generation_config.num_return_sequences}." ) if batch_size > 1: raise ValueError("assisted generate is only supported for batch_size = 1") if not model_kwargs["use_cache"]: raise ValueError("assisted generate requires `use_cache=True`")
# 11. Get the candidate generator, given the parameterization candidate_generator = self._get_candidate_generator( generation_config=generation_config, input_ids=input_ids, inputs_tensor=inputs_tensor, assistant_model=assistant_model, logits_processor=logits_processor, model_kwargs=model_kwargs, )
# 12. run assisted generate return self.assisted_decoding( input_ids, candidate_generator=candidate_generator, do_sample=generation_config.do_sample, logits_processor=prepared_logits_processor, logits_warper=self._get_logits_warper(generation_config) if generation_config.do_sample else None, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, streamer=streamer, **model_kwargs, ) if generation_mode == GenerationMode.GREEDY_SEARCH: # 11. run greedy search return self.greedy_search( input_ids, logits_processor=prepared_logits_processor, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, streamer=streamer, **model_kwargs, )
elif generation_mode == GenerationMode.CONTRASTIVE_SEARCH: if not model_kwargs["use_cache"]: raise ValueError("Contrastive search requires `use_cache=True`")
return self.contrastive_search( input_ids, top_k=generation_config.top_k, penalty_alpha=generation_config.penalty_alpha, logits_processor=prepared_logits_processor, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, streamer=streamer, sequential=generation_config.low_memory, **model_kwargs, )
elif generation_mode == GenerationMode.SAMPLE: # 11. prepare logits warper logits_warper = self._get_logits_warper(generation_config)
# 12. expand input_ids with `num_return_sequences` additional sequences per batch input_ids, model_kwargs = self._expand_inputs_for_generation( input_ids=input_ids, expand_size=generation_config.num_return_sequences, is_encoder_decoder=self.config.is_encoder_decoder, **model_kwargs, )
# 13. run sample return self.sample( input_ids, logits_processor=prepared_logits_processor, logits_warper=logits_warper, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, streamer=streamer, **model_kwargs, )
elif generation_mode == GenerationMode.BEAM_SEARCH: # 11. prepare beam search scorer beam_scorer = BeamSearchScorer( batch_size=batch_size, num_beams=generation_config.num_beams, device=inputs_tensor.device, length_penalty=generation_config.length_penalty, do_early_stopping=generation_config.early_stopping, num_beam_hyps_to_keep=generation_config.num_return_sequences, max_length=generation_config.max_length, ) # 12. interleave input_ids with `num_beams` additional sequences per batch input_ids, model_kwargs = self._expand_inputs_for_generation( input_ids=input_ids, expand_size=generation_config.num_beams, is_encoder_decoder=self.config.is_encoder_decoder, **model_kwargs, ) # 13. run beam search return self.beam_search( input_ids, beam_scorer, logits_processor=prepared_logits_processor, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, **model_kwargs, )
elif generation_mode == GenerationMode.BEAM_SAMPLE: # 11. prepare logits warper logits_warper = self._get_logits_warper(generation_config)
# 12. prepare beam search scorer beam_scorer = BeamSearchScorer( batch_size=batch_size, num_beams=generation_config.num_beams, device=inputs_tensor.device, length_penalty=generation_config.length_penalty, do_early_stopping=generation_config.early_stopping, num_beam_hyps_to_keep=generation_config.num_return_sequences, max_length=generation_config.max_length, )
# 13. interleave input_ids with `num_beams` additional sequences per batch input_ids, model_kwargs = self._expand_inputs_for_generation( input_ids=input_ids, expand_size=generation_config.num_beams, is_encoder_decoder=self.config.is_encoder_decoder, **model_kwargs, )
# 14. run beam sample return self.beam_sample( input_ids, beam_scorer, logits_processor=prepared_logits_processor, logits_warper=logits_warper, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, **model_kwargs, )
elif generation_mode == GenerationMode.GROUP_BEAM_SEARCH: # 11. prepare beam search scorer beam_scorer = BeamSearchScorer( batch_size=batch_size, num_beams=generation_config.num_beams, device=inputs_tensor.device, length_penalty=generation_config.length_penalty, do_early_stopping=generation_config.early_stopping, num_beam_hyps_to_keep=generation_config.num_return_sequences, num_beam_groups=generation_config.num_beam_groups, max_length=generation_config.max_length, ) # 12. interleave input_ids with `num_beams` additional sequences per batch input_ids, model_kwargs = self._expand_inputs_for_generation( input_ids=input_ids, expand_size=generation_config.num_beams, is_encoder_decoder=self.config.is_encoder_decoder, **model_kwargs, ) # 13. run beam search return self.group_beam_search( input_ids, beam_scorer, logits_processor=prepared_logits_processor, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, **model_kwargs, )
elif generation_mode == GenerationMode.CONSTRAINED_BEAM_SEARCH: final_constraints = [] if generation_config.constraints is not None: final_constraints = generation_config.constraints
if generation_config.force_words_ids is not None:
def typeerror(): raise ValueError( "`force_words_ids` has to either be a `List[List[List[int]]]` or `List[List[int]]` " f"of positive integers, but is {generation_config.force_words_ids}." )
if ( not isinstance(generation_config.force_words_ids, list) or len(generation_config.force_words_ids) == 0 ): typeerror()
for word_ids in generation_config.force_words_ids: if isinstance(word_ids[0], list): if not isinstance(word_ids, list) or len(word_ids) == 0: typeerror() if any(not isinstance(token_ids, list) for token_ids in word_ids): typeerror() if any( any((not isinstance(token_id, int) or token_id < 0) for token_id in token_ids) for token_ids in word_ids ): typeerror()
constraint = DisjunctiveConstraint(word_ids) else: if not isinstance(word_ids, list) or len(word_ids) == 0: typeerror() if any((not isinstance(token_id, int) or token_id < 0) for token_id in word_ids): typeerror()
constraint = PhrasalConstraint(word_ids) final_constraints.append(constraint)
# 11. prepare beam search scorer constrained_beam_scorer = ConstrainedBeamSearchScorer( constraints=final_constraints, batch_size=batch_size, num_beams=generation_config.num_beams, device=inputs_tensor.device, length_penalty=generation_config.length_penalty, do_early_stopping=generation_config.early_stopping, num_beam_hyps_to_keep=generation_config.num_return_sequences, max_length=generation_config.max_length, ) # 12. interleave input_ids with `num_beams` additional sequences per batch input_ids, model_kwargs = self._expand_inputs_for_generation( input_ids=input_ids, expand_size=generation_config.num_beams, is_encoder_decoder=self.config.is_encoder_decoder, **model_kwargs, ) # 13. run beam search return self.constrained_beam_search( input_ids, constrained_beam_scorer=constrained_beam_scorer, logits_processor=prepared_logits_processor, stopping_criteria=prepared_stopping_criteria, pad_token_id=generation_config.pad_token_id, eos_token_id=generation_config.eos_token_id, output_scores=generation_config.output_scores, return_dict_in_generate=generation_config.return_dict_in_generate, synced_gpus=synced_gpus, **model_kwargs, ) # 作用是生成一个[batch_size, 1]大小的张量, 也即标志开始生成的 bos token # 输入有三种方式,inputs(model.generate()的输入参数)、input_ids(放在kwargs中的输入id)、inputs_embeds(通常在 encoder-decoder 模型中使用,可以传入编码器输出的 embedding),这个部分就是来回确认是否传错 def _prepare_model_inputs( self, inputs: Optional[torch.Tensor] = None, bos_token_id: Optional[int] = None, model_kwargs: Optional[Dict[str, torch.Tensor]] = None, ) -> Tuple[torch.Tensor, Optional[str], Dict[str, torch.Tensor]]: """ This function extracts the model-specific `inputs` for generation. """ # 1. retrieve all kwargs that are non-None or non-model input related. # some encoder-decoder models have different names for model and encoder # 一些encoder-decoder模型有不同的名称, 例如encoder和model, 这里是为了统一输入名称 if ( self.config.is_encoder_decoder and hasattr(self, "encoder") and self.encoder.main_input_name != self.main_input_name ): input_name = self.encoder.main_input_name else: input_name = self.main_input_name
# 从 model_kwargs 中去掉 input_name: None 的键值对 model_kwargs = {k: v for k, v in model_kwargs.items() if v is not None or k != input_name}
# 2. check whether model_input_name is passed as kwarg # if yes and `inputs` is None use kwarg inputs # generate 的输入参数 inputs 和 kwarg 中的 input_name (可能是 input_ids 或 input_embeds) 不能同时传入, 否则报错 (可以输入都为空, 也即没有前置输入让模型随意输出, 不过一般不这样用). 输入合法则将二者整合到 inputs中 inputs_kwarg = model_kwargs.pop(input_name, None) if inputs_kwarg is not None and inputs is not None: raise ValueError( f"`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. " f"Make sure to either pass {inputs} or {input_name}=..." ) elif inputs_kwarg is not None: inputs = inputs_kwarg
# 3. In the presence of `inputs_embeds` for text models: # - decoder-only models should complain if the user attempts to pass `inputs_embeds`, but the model doesn't have its forwarding implemented. `inputs_embeds` is kept in `model_kwargs` and can coexist with input_ids (`inputs_embeds` will be used in the 1st generation step, as opposed to `input_ids`) # - encoder-decoder models should complain if the user attempts to pass `inputs_embeds` and `input_ids`, and pull the former to inputs. It will be used in place of `input_ids` to get the encoder hidden states. # 在存在 `inputs_embeds` 的情况下: # - 如果是 Decoder-Only 模型, 如果用户尝试传入 `inputs_embeds`, 但模型 forward 没有 inputs_embeds 参数, 则会报错. `inputs_embeds` 保留在 `model_kwargs` 中, 并且可以与 `input_ids` 共存 (`inputs_embeds` 将在生成第一个 token 时使用, 而不是 `input_ids`) # - 如果是 Encoder-Decoder 模型, 如果用户尝试传入 `inputs_embeds` 和 `input_ids`, 则会报错, 并将前者提取到 inputs 中. 它将代替 `input_ids` 用于获取编码器隐藏状态 ### 人话: input_name=="input_ids" 且传入 inputs_embeds 时, 模型 forward 函数如果有 inputs_embeds 参数, 则将 inputs_embeds 保留在 model_kwargs 中, 并将 input_ids 初始化为 bos_token_id, inputs 替换为 inputs_embeds; 否则报错 if input_name == "input_ids" and "inputs_embeds" in model_kwargs: # 如果是 Decoder-Only 模型 if not self.config.is_encoder_decoder: has_inputs_embeds_forwarding = "inputs_embeds" in set( inspect.signature(self.prepare_inputs_for_generation).parameters.keys() ) # Decoder-Only 模型如果 forward 函数中没有 input_embeds 参数, 却还收到了 input_embeds, 报错 if not has_inputs_embeds_forwarding: raise ValueError( f"You passed `inputs_embeds` to `.generate()`, but the model class {self.__class__.__name__} " "doesn't have its forwarding implemented. See the GPT2 implementation for an example " "(https://github.com/huggingface/transformers/pull/21405), and feel free to open a PR with it!" ) # In this case, `input_ids` is moved to the `model_kwargs`, so a few automations (like the creation of the attention mask) can rely on the actual model input. # 如果模型有 input_embeds 参数, 则将 input_ids 移到 model_kwargs 中, 以便一些自动化操作(如创建 attention mask)可以依赖于真实模型输入 # self._maybe_initialize_input_ids_for_generation 是 GenerationMixin 类中的方法, 解读已附在本函数下面. 该函数的作用是初始化 input_ids 为 bos_token_id, 即标志开始生成的 token model_kwargs["input_ids"] = self._maybe_initialize_input_ids_for_generation( inputs, bos_token_id, model_kwargs=model_kwargs ) else: if inputs is not None: raise ValueError("You passed `inputs_embeds` and `input_ids` to `.generate()`. Please pick one.") # 将 inputs 换成 inputs_embeds, input_name 换成 "inputs_embeds" inputs, input_name = model_kwargs["inputs_embeds"], "inputs_embeds"
# 4. if `inputs` is still None, try to create `input_ids` from BOS token # 如果没有输入 input_embeds (也即没有进入上面的 if 语句), 则尝试使用 bos_token_id 初始化 input_ids; 否则没有变化 inputs = self._maybe_initialize_input_ids_for_generation(inputs, bos_token_id, model_kwargs) return inputs, input_name, model_kwargs
def _maybe_initialize_input_ids_for_generation( self, inputs: Optional[torch.Tensor] = None, bos_token_id: Optional[int] = None, model_kwargs: Optional[Dict[str, torch.Tensor]] = None, ) -> torch.LongTensor: """Initializes input ids for generation, if necessary.""" if inputs is not None: return inputs
encoder_outputs = model_kwargs.get("encoder_outputs") if self.config.is_encoder_decoder and encoder_outputs is not None: # make dummy input_ids with value -100, as a sanity check ensuring that they won't be used for encoding shape = encoder_outputs.last_hidden_state.size()[:-1] return torch.ones(shape, dtype=torch.long, device=self.device) * -100
if bos_token_id is None: raise ValueError("`bos_token_id` has to be defined when no `input_ids` are provided.")
# If there is some tensor in `model_kwargs`, we can infer the batch size from it. This is helpful with # soft-prompting or in multimodal implementations built on top of decoder-only language models. # 从 model_kwargs 中推断 batch_size, 创建大小为 [batch_size, 1] 的张量, 并初始化为 bos_token_id batch_size = 1 for value in model_kwargs.values(): if isinstance(value, torch.Tensor): batch_size = value.shape[0] break return torch.ones((batch_size, 1), dtype=torch.long, device=self.device) * bos_token_id
def _prepare_attention_mask_for_generation( self, inputs: torch.Tensor, pad_token_id: Optional[int], eos_token_id: Optional[Union[int, List[int]]], ) -> torch.LongTensor: is_input_ids = len(inputs.shape) == 2 and inputs.dtype in [torch.int, torch.long] is_pad_token_in_inputs = (pad_token_id is not None) and (pad_token_id in inputs) if isinstance(eos_token_id, int): eos_token_id = [eos_token_id] is_pad_token_not_equal_to_eos_token_id = (eos_token_id is None) or (pad_token_id not in eos_token_id)
# Check if input is input_ids and padded -> only then is attention_mask defined if is_input_ids and is_pad_token_in_inputs and is_pad_token_not_equal_to_eos_token_id: return inputs.ne(pad_token_id).long() else: return torch.ones(inputs.shape[:2], dtype=torch.long, device=inputs.device) def _get_generation_mode( self, generation_config: GenerationConfig, assistant_model: Optional["PreTrainedModel"] ) -> GenerationMode: """ Returns the generation mode triggered by a [`GenerationConfig`] instance. 根据 generation_config 输入决定不同的生成模式 1. 输入 constrint 或 force_words_ids, 使用约束解码 CONSTRAINED_BEAM_SEARCH 2. num_beams=1 2.1 do_sample=False 2.1.1 top_k>1 and penalty_alpha>0, 对比搜索 CONTRASTIVE_SEARCH 2.1.2 贪心搜索 GREEDY_SEARCH 2.2 do_sample=True, 随机采样 SAMPLE 3. num_beams>1 3.1 num_beam_groups>1, 分组束搜索 GROUP_BEAM_SEARCH 3.2 do_sample=True, 束采样 BEAM_SAMPLE 3.3 其他情况, 束搜索 BEAM_SEARCH (新版 transformers 库添加) 根据上面参数输入, 如果生成模式是贪婪搜索或随机采样 (1) 输入 assistant_model 或 prompt_lookup_num_tokens, 则生成模式为辅助生成 ASSISTED_GENERATION (2) 输入 dola_layers, 则生成模式为DoLa生成 DOLA_GENERATION """ if generation_config.constraints is not None or generation_config.force_words_ids is not None: generation_mode = GenerationMode.CONSTRAINED_BEAM_SEARCH elif generation_config.num_beams == 1: if generation_config.do_sample is False: if ( generation_config.top_k is not None and generation_config.top_k > 1 and generation_config.penalty_alpha is not None and generation_config.penalty_alpha > 0 ): generation_mode = GenerationMode.CONTRASTIVE_SEARCH else: generation_mode = GenerationMode.GREEDY_SEARCH else: generation_mode = GenerationMode.SAMPLE else: if generation_config.num_beam_groups > 1: generation_mode = GenerationMode.GROUP_BEAM_SEARCH elif generation_config.do_sample is True: generation_mode = GenerationMode.BEAM_SAMPLE else: generation_mode = GenerationMode.BEAM_SEARCH
# Assisted generation may extend some generation modes if assistant_model is not None or generation_config.prompt_lookup_num_tokens is not None: if generation_mode in ("greedy_search", "sample"): generation_mode = GenerationMode.ASSISTED_GENERATION else: raise ValueError( "You've set `assistant_model`, which triggers assisted generate. Currently, assisted generate " "is only supported with Greedy Search and Sample." ) return generation_mode
|