| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | # Copyright 2025 The Baidu team. |
| 5 | # Copyright 2023 The vLLM team. |
| 6 | # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. |
| 7 | # |
| 8 | # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX |
| 9 | # and OPT implementations in this library. It has been modified from its |
| 10 | # original forms to accommodate minor architectural differences compared |
| 11 | # to GPT-NeoX and OPT used by the Meta AI team that trained the model. |
| 12 | # |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | # you may not use this file except in compliance with the License. |
| 15 | # You may obtain a copy of the License at |
| 16 | # |
| 17 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | # |
| 19 | # Unless required by applicable law or agreed to in writing, software |
| 20 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | # See the License for the specific language governing permissions and |
| 23 | # limitations under the License. |
| 24 | """Inference-only Erine model compatible with HuggingFace weights.""" |
| 25 | |
| 26 | from vllm.compilation.decorators import support_torch_compile |
| 27 | from vllm.config import VllmConfig |
| 28 | from vllm.model_executor.models.llama import LlamaForCausalLM |
| 29 | |
| 30 | from .utils import PPMissingLayer |
| 31 | |
| 32 | |
| 33 | @support_torch_compile( |
| 34 | # set dynamic_arg_dims to support mrope |
| 35 | dynamic_arg_dims={ |
| 36 | "input_ids": 0, |
| 37 | "positions": -1, |
| 38 | "intermediate_tensors": 0, |
| 39 | "inputs_embeds": 0, |
| 40 | } |
| 41 | ) |
| 42 | class Ernie4_5ForCausalLM(LlamaForCausalLM): |
| 43 | def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): |
| 44 | super().__init__(vllm_config=vllm_config, prefix=prefix) |
| 45 | # Hack Llama model to fit HF format Ernie4.5 dense implementation |
| 46 | # Attention difference between Ernie and Llama: |
| 47 | # 1. rotary_dim and no Neox style. |
| 48 | # 2. There is no bias for o_proj in attention |
| 49 | for layer in self.model.layers: |
| 50 | if not isinstance(layer, PPMissingLayer): |
| 51 | layer.self_attn.rotary_emb.is_neox_style = False |
| 52 | layer.self_attn.o_proj.bias = None |
| 53 | layer.self_attn.o_proj.skip_bias_add = True |