| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | # Copyright 2024 The vLLM team. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | """Transformers modeling backend mixin for legacy models.""" |
| 18 | |
| 19 | from typing import TYPE_CHECKING |
| 20 | |
| 21 | import torch |
| 22 | |
| 23 | from vllm.model_executor.models.utils import WeightsMapper |
| 24 | from vllm.sequence import IntermediateTensors |
| 25 | |
| 26 | from .base import Base |
| 27 | |
| 28 | if TYPE_CHECKING: |
| 29 | from vllm.config import VllmConfig |
| 30 | |
| 31 | |
| 32 | class LegacyMixin(Base): |
| 33 | def __init__(self, *, vllm_config: "VllmConfig", prefix: str = ""): |
| 34 | super().__init__(vllm_config=vllm_config, prefix=prefix) |
| 35 | |
| 36 | self.hf_to_vllm_mapper |= WeightsMapper( |
| 37 | # Drop unsupported/unwanted output embeddings layers. |
| 38 | orig_to_new_prefix={ |
| 39 | "model.lm_head.": None, |
| 40 | "model.predictions.": None, |
| 41 | "model.qa_outputs.": None, |
| 42 | "model.embeddings_project.": None, |
| 43 | "model.discriminator_predictions.": None, |
| 44 | }, |
| 45 | orig_to_new_substr={ |
| 46 | # Some encoder models have the position_ids buffer in the checkpoint. |
| 47 | # vLLM always passes position_ids as an argument, so drop the buffer. |
| 48 | "position_ids": None, |
| 49 | # Some encoder models have the bias of the final classifier layer in |
| 50 | # the checkpoint. vLLM does not use this bias, so drop it. |
| 51 | "score.bias": None, |
| 52 | }, |
| 53 | ) |
| 54 | |
| 55 | # roberta-like models an extra padding in positions. |
| 56 | # FIXME(Isotr0py): This is quite hacky for roberta edge case, |
| 57 | # we should find a better way to handle this. |
| 58 | self.is_roberta = "roberta" in self.text_config.model_type |
| 59 | self.padding_idx = self.text_config.pad_token_id |
| 60 | |
| 61 | def forward( |
| 62 | self, |
| 63 | input_ids: torch.Tensor | None, |
| 64 | positions: torch.Tensor, |
| 65 | intermediate_tensors: IntermediateTensors | None = None, |
| 66 | inputs_embeds: torch.Tensor | None = None, |
| 67 | **kwargs, |
| 68 | ) -> torch.Tensor | IntermediateTensors: |
| 69 | if self.is_roberta: |
| 70 | # RoBERTa positions start at padding_idx + 1. |
| 71 | # Non-in-place add to avoid mutating the persistent GPU buffer -- |
| 72 | # in-place += would accumulate on CUDA graph padding slots. |
| 73 | positions = positions + self.padding_idx + 1 |
| 74 | return super().forward( |
| 75 | input_ids=input_ids, |
| 76 | positions=positions, |
| 77 | intermediate_tensors=intermediate_tensors, |
| 78 | inputs_embeds=inputs_embeds, |
| 79 | **kwargs, |
| 80 | ) |