Skip to content

Commit

Permalink
[Model] Adapt the MiniCPM-2B model
Browse files Browse the repository at this point in the history
Signed-off-by: Wang Kunpeng <1289706727@qq.com>
  • Loading branch information
kunpengW-code committed Feb 25, 2025
1 parent 701a287 commit ccc2ffa
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
2 changes: 2 additions & 0 deletions vllm_ascend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@

def register():
"""Register the NPU platform."""
# To ensure that the module is correctly replaced, add it at the beginning
import vllm_ascend.patch_module # noqa: F401
return "vllm_ascend.platform.NPUPlatform"
17 changes: 17 additions & 0 deletions vllm_ascend/patch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
# This file is a part of the vllm-ascend project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import vllm_ascend.patch.patch_minicpm # noqa
40 changes: 40 additions & 0 deletions vllm_ascend/patch/patch_minicpm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
# This file is a part of the vllm-ascend project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import torch

from vllm.attention import AttentionMetadata
from vllm.model_executor.models.minicpm import MiniCPMAttention


def forward(
self,
positions: torch.Tensor,
hidden_states: torch.Tensor,
kv_cache: torch.Tensor,
attn_metadata: AttentionMetadata,
) -> torch.Tensor:
qkv, _ = self.qkv_proj(hidden_states)
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
q, k = self.rotary_emb(positions, q, k)
attn_output = self.attn(q, k, v, kv_cache, attn_metadata)
output, _ = self.o_proj(attn_output)
return output


# The type conversion in the forward function is deleted to support the rope operator.
MiniCPMAttention.forward = forward
21 changes: 21 additions & 0 deletions vllm_ascend/patch_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
# This file is a part of the vllm-ascend project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
import types

# prevent errors caused by triton not supported
sys.modules['vllm.model_executor.layers.fused_moe.fused_moe'] = types.ModuleType('fused_moe_module')
3 changes: 2 additions & 1 deletion vllm_ascend/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ def __init__(
is_driver_worker: bool = False,
model_runner_cls: Optional[Type[ModelRunnerBase]] = None,
) -> None:
# Register ops when worker init.
# Register ops and patch when worker init.
from vllm_ascend import ops # noqa: F401
from vllm_ascend import patch # noqa: F401

WorkerBase.__init__(self, vllm_config=vllm_config)
# Try to import mindie_turbo to accelerate vLLM inference.
Expand Down

0 comments on commit ccc2ffa

Please sign in to comment.