### What this PR does / why we need it?
**Scope of Changes**:
| File Path |
| :--- |
|`vllm_ascend/ops/layer_shard_linear.py`|
|`vllm_ascend/ops/linear.py`|
|`vllm_ascend/ops/linear_op.py`|
|`vllm_ascend/worker/worker.py`|
| ` vllm_ascend/patch/worker/patch_bert.py` |
| ` vllm_ascend/patch/worker/patch_deepseek.py` |
| ` vllm_ascend/patch/worker/patch_distributed.py` |
| ` vllm_ascend/patch/worker/patch_module.py` |
| ` vllm_ascend/patch/worker/patch_multimodal_merge.py` |
| ` vllm_ascend/patch/worker/patch_qwen3_next.py` |
| ` vllm_ascend/patch/worker/patch_qwen3_next_mtp.py` |
| ` vllm_ascend/patch/worker/patch_rejection_sampler.py` |
| ` vllm_ascend/patch/worker/patch_rope.py` |
| ` vllm_ascend/patch/worker/patch_triton.py` |
| ` vllm_ascend/patch/worker/patch_unquantized_gemm.py` |
| ` vllm_ascend/patch/worker/patch_v2_egale.py` |
|` vllm_ascend/worker/npu_input_batch.py`|
|` vllm_ascend/worker/v2/aclgraph_utils.py`|
|` vllm_ascend/worker/v2/attn_utils.py`|
|` vllm_ascend/worker/v2/model_runner.py`|
|` vllm_ascend/worker/v2/sample/gumbel.py`|
|` vllm_ascend/worker/v2/sample/penalties.py`|
|` vllm_ascend/worker/v2/sample/sampler.py`|
|` vllm_ascend/worker/v2/spec_decode/__init__.py`|
|` vllm_ascend/worker/v2/spec_decode/eagle.py`|
|` vllm_ascend/worker/v2/states.py`|
### Does this PR introduce _any_ user-facing change?
### How was this patch tested?
- vLLM version: v0.14.0
- vLLM main:
d68209402d
Signed-off-by: MrZ20 <2609716663@qq.com>
Signed-off-by: SILONG ZENG <2609716663@qq.com>
Signed-off-by: wangxiyuan <wangxiyuan1007@gmail.com>
Co-authored-by: wangxiyuan <wangxiyuan1007@gmail.com>
This commit is contained in:
@@ -23,15 +23,13 @@ from vllm.lora.request import LoRARequest
|
||||
from vllm.pooling_params import PoolingParams
|
||||
from vllm.v1.outputs import LogprobsTensors
|
||||
from vllm.v1.pool.metadata import PoolingStates
|
||||
from vllm.v1.sample.logits_processor import (BatchUpdateBuilder,
|
||||
LogitsProcessors)
|
||||
from vllm.v1.sample.logits_processor import BatchUpdateBuilder, LogitsProcessors
|
||||
from vllm.v1.worker.gpu_input_batch import InputBatch
|
||||
|
||||
from vllm_ascend.worker.block_table import MultiGroupBlockTable
|
||||
|
||||
|
||||
class NPUInputBatch(InputBatch):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
max_num_reqs: int,
|
||||
@@ -72,10 +70,9 @@ class NPUInputBatch(InputBatch):
|
||||
pin_memory=False,
|
||||
)
|
||||
self.token_ids_cpu = self.token_ids_cpu_tensor.numpy()
|
||||
self.is_token_ids_tensor = torch.zeros((max_num_reqs, max_model_len),
|
||||
device="cpu",
|
||||
dtype=bool,
|
||||
pin_memory=False)
|
||||
self.is_token_ids_tensor = torch.zeros(
|
||||
(max_num_reqs, max_model_len), device="cpu", dtype=bool, pin_memory=False
|
||||
)
|
||||
self.is_token_ids = self.is_token_ids_tensor.numpy()
|
||||
# Store prompt embeddings per request to avoid OOM from large upfront
|
||||
# allocation if max_model_len is big.
|
||||
@@ -85,13 +82,12 @@ class NPUInputBatch(InputBatch):
|
||||
self.num_tokens_no_spec = np.zeros(max_num_reqs, dtype=np.int32)
|
||||
self.num_prompt_tokens = np.zeros(max_num_reqs, dtype=np.int32)
|
||||
self.num_computed_tokens_cpu_tensor = torch.zeros(
|
||||
(max_num_reqs, ),
|
||||
(max_num_reqs,),
|
||||
device="cpu",
|
||||
dtype=torch.int32,
|
||||
pin_memory=pin_memory,
|
||||
)
|
||||
self.num_computed_tokens_cpu = self.num_computed_tokens_cpu_tensor.numpy(
|
||||
)
|
||||
self.num_computed_tokens_cpu = self.num_computed_tokens_cpu_tensor.numpy()
|
||||
|
||||
# Block table.
|
||||
self.block_table = MultiGroupBlockTable(
|
||||
@@ -107,34 +103,21 @@ class NPUInputBatch(InputBatch):
|
||||
)
|
||||
|
||||
# Sampling-related.
|
||||
self.temperature = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float32,
|
||||
device=device)
|
||||
self.temperature_cpu_tensor = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float32,
|
||||
device="cpu",
|
||||
pin_memory=pin_memory)
|
||||
self.temperature = torch.empty((max_num_reqs,), dtype=torch.float32, device=device)
|
||||
self.temperature_cpu_tensor = torch.empty(
|
||||
(max_num_reqs,), dtype=torch.float32, device="cpu", pin_memory=pin_memory
|
||||
)
|
||||
self.temperature_cpu = self.temperature_cpu_tensor.numpy()
|
||||
self.greedy_reqs: set[str] = set()
|
||||
self.random_reqs: set[str] = set()
|
||||
|
||||
self.top_p = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float32,
|
||||
device=device)
|
||||
self.top_p_cpu_tensor = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float32,
|
||||
device="cpu",
|
||||
pin_memory=pin_memory)
|
||||
self.top_p = torch.empty((max_num_reqs,), dtype=torch.float32, device=device)
|
||||
self.top_p_cpu_tensor = torch.empty((max_num_reqs,), dtype=torch.float32, device="cpu", pin_memory=pin_memory)
|
||||
self.top_p_cpu = self.top_p_cpu_tensor.numpy()
|
||||
self.top_p_reqs: set[str] = set()
|
||||
|
||||
self.top_k = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.int32,
|
||||
device=device)
|
||||
self.top_k_cpu_tensor = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.int32,
|
||||
device="cpu",
|
||||
pin_memory=pin_memory)
|
||||
self.top_k = torch.empty((max_num_reqs,), dtype=torch.int32, device=device)
|
||||
self.top_k_cpu_tensor = torch.empty((max_num_reqs,), dtype=torch.int32, device="cpu", pin_memory=pin_memory)
|
||||
self.top_k_cpu = self.top_k_cpu_tensor.numpy()
|
||||
self.top_k_reqs: set[str] = set()
|
||||
|
||||
@@ -142,54 +125,37 @@ class NPUInputBatch(InputBatch):
|
||||
self.spec_decode_unsupported_reqs: set[str] = set()
|
||||
|
||||
# Frequency penalty related data structures
|
||||
self.frequency_penalties = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float,
|
||||
device=device)
|
||||
self.frequency_penalties = torch.empty((max_num_reqs,), dtype=torch.float, device=device)
|
||||
self.frequency_penalties_cpu_tensor = torch.empty(
|
||||
(max_num_reqs, ),
|
||||
dtype=torch.float,
|
||||
device="cpu",
|
||||
pin_memory=pin_memory)
|
||||
self.frequency_penalties_cpu = self.frequency_penalties_cpu_tensor.numpy(
|
||||
(max_num_reqs,), dtype=torch.float, device="cpu", pin_memory=pin_memory
|
||||
)
|
||||
self.frequency_penalties_cpu = self.frequency_penalties_cpu_tensor.numpy()
|
||||
self.frequency_penalties_reqs: set[str] = set()
|
||||
|
||||
# Presence penalty related data structures
|
||||
self.presence_penalties = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float,
|
||||
device=device)
|
||||
self.presence_penalties_cpu_tensor = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float,
|
||||
device="cpu",
|
||||
pin_memory=pin_memory)
|
||||
self.presence_penalties_cpu = self.presence_penalties_cpu_tensor.numpy(
|
||||
self.presence_penalties = torch.empty((max_num_reqs,), dtype=torch.float, device=device)
|
||||
self.presence_penalties_cpu_tensor = torch.empty(
|
||||
(max_num_reqs,), dtype=torch.float, device="cpu", pin_memory=pin_memory
|
||||
)
|
||||
self.presence_penalties_cpu = self.presence_penalties_cpu_tensor.numpy()
|
||||
self.presence_penalties_reqs: set[str] = set()
|
||||
|
||||
# Repetition penalty related data structures
|
||||
self.repetition_penalties = torch.empty((max_num_reqs, ),
|
||||
dtype=torch.float,
|
||||
device=device)
|
||||
self.repetition_penalties = torch.empty((max_num_reqs,), dtype=torch.float, device=device)
|
||||
self.repetition_penalties_cpu_tensor = torch.empty(
|
||||
(max_num_reqs, ),
|
||||
dtype=torch.float,
|
||||
device="cpu",
|
||||
pin_memory=pin_memory)
|
||||
self.repetition_penalties_cpu = self.repetition_penalties_cpu_tensor.numpy(
|
||||
(max_num_reqs,), dtype=torch.float, device="cpu", pin_memory=pin_memory
|
||||
)
|
||||
self.repetition_penalties_cpu = self.repetition_penalties_cpu_tensor.numpy()
|
||||
self.repetition_penalties_reqs: set[str] = set()
|
||||
|
||||
# Speculative decoding
|
||||
self.num_accepted_tokens_cpu_tensor = torch.ones((max_num_reqs, ),
|
||||
dtype=torch.int64,
|
||||
device="cpu",
|
||||
pin_memory=pin_memory)
|
||||
self.num_accepted_tokens_cpu = self.num_accepted_tokens_cpu_tensor.numpy(
|
||||
self.num_accepted_tokens_cpu_tensor = torch.ones(
|
||||
(max_num_reqs,), dtype=torch.int64, device="cpu", pin_memory=pin_memory
|
||||
)
|
||||
self.num_accepted_tokens_cpu = self.num_accepted_tokens_cpu_tensor.numpy()
|
||||
|
||||
# lora related
|
||||
self.request_lora_mapping = np.zeros((self.max_num_reqs, ),
|
||||
dtype=np.int64)
|
||||
self.request_lora_mapping = np.zeros((self.max_num_reqs,), dtype=np.int64)
|
||||
self.lora_id_to_request_ids: dict[int, set[str]] = {}
|
||||
self.lora_id_to_lora_request: dict[int, LoRARequest] = {}
|
||||
|
||||
@@ -218,8 +184,7 @@ class NPUInputBatch(InputBatch):
|
||||
# req_index -> bad_words_token_ids
|
||||
self.bad_words_token_ids: dict[int, list[list[int]]] = {}
|
||||
|
||||
self.logits_processing_needs_token_ids = np.zeros(max_num_reqs,
|
||||
dtype=bool)
|
||||
self.logits_processing_needs_token_ids = np.zeros(max_num_reqs, dtype=bool)
|
||||
|
||||
self.req_output_token_ids: list[list[int] | None] = []
|
||||
|
||||
@@ -229,8 +194,7 @@ class NPUInputBatch(InputBatch):
|
||||
self.logitsprocs_need_output_token_ids = logitsprocs_need_output_token_ids
|
||||
|
||||
# Store last speculative tokens for sampler.
|
||||
self.spec_token_ids: list[list[int]] = [[]
|
||||
for _ in range(max_num_reqs)]
|
||||
self.spec_token_ids: list[list[int]] = [[] for _ in range(max_num_reqs)]
|
||||
|
||||
# This is updated each time the batch constituents change.
|
||||
self.sampling_metadata = self._make_sampling_metadata()
|
||||
|
||||
@@ -22,19 +22,16 @@ from typing import Any
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.v1.attention.backend import AttentionMetadataBuilder
|
||||
from vllm.v1.kv_cache_interface import KVCacheConfig
|
||||
from vllm.v1.worker.gpu.block_table import BlockTables
|
||||
from vllm.v1.worker.gpu.cudagraph_utils import CudaGraphManager
|
||||
from vllm.v1.worker.gpu.cudagraph_utils import \
|
||||
prepare_inputs_to_capture as prepare_inputs_to_capture_gpu
|
||||
from vllm.v1.worker.gpu.cudagraph_utils import prepare_inputs_to_capture as prepare_inputs_to_capture_gpu
|
||||
from vllm.v1.worker.gpu.input_batch import InputBuffers
|
||||
from vllm.v1.attention.backend import AttentionMetadataBuilder
|
||||
|
||||
from vllm_ascend.worker.v2.utils import torch_cuda_wrapper
|
||||
|
||||
|
||||
|
||||
|
||||
class AclGraphManager(CudaGraphManager):
|
||||
"""ACL Graph Manager for Ascend NPUs."""
|
||||
|
||||
@@ -51,7 +48,7 @@ class AclGraphManager(CudaGraphManager):
|
||||
attn_metadata_builders: list[AttentionMetadataBuilder],
|
||||
kv_cache_config: KVCacheConfig,
|
||||
) -> None:
|
||||
with (torch_cuda_wrapper(), prepare_capture_inputs_wrapper()):
|
||||
with torch_cuda_wrapper(), prepare_capture_inputs_wrapper():
|
||||
super().capture_graph(
|
||||
num_tokens,
|
||||
model,
|
||||
|
||||
@@ -18,19 +18,17 @@
|
||||
#
|
||||
|
||||
from collections.abc import Sequence
|
||||
from typing import Any, Tuple
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.v1.kv_cache_interface import EncoderOnlyAttentionSpec, KVCacheConfig
|
||||
from vllm.v1.attention.backend import AttentionMetadataBuilder
|
||||
from vllm.v1.kv_cache_interface import EncoderOnlyAttentionSpec, KVCacheConfig
|
||||
|
||||
from vllm_ascend.attention.attention_mask import AttentionMaskBuilder
|
||||
from vllm_ascend.attention.attention_v1 import AscendAttentionState
|
||||
from vllm_ascend.attention.utils import (AscendCommonAttentionMetadata,
|
||||
AscendPrefillContextParallelMetadata)
|
||||
|
||||
from vllm_ascend.attention.utils import AscendCommonAttentionMetadata, AscendPrefillContextParallelMetadata
|
||||
|
||||
_ATTENTION_MASK_BUILDER = None
|
||||
|
||||
@@ -59,8 +57,7 @@ def build_attn_metadata(
|
||||
attn_state: Any | None = None,
|
||||
graph_pad_size: int = -1,
|
||||
num_input_tokens: int = 0,
|
||||
prefill_context_parallel_metadata: AscendPrefillContextParallelMetadata
|
||||
| None = None,
|
||||
prefill_context_parallel_metadata: AscendPrefillContextParallelMetadata | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Build attention metadata for Ascend NPUs."""
|
||||
# TODO(Ronald1995): optimize AscendCommonAttentionMetadata.
|
||||
@@ -92,7 +89,8 @@ def build_attn_metadata(
|
||||
graph_pad_size=graph_pad_size,
|
||||
num_input_tokens=num_input_tokens,
|
||||
prefill_context_parallel_metadata=prefill_context_parallel_metadata,
|
||||
max_seq_len=max_seq_len)
|
||||
max_seq_len=max_seq_len,
|
||||
)
|
||||
|
||||
attn_metadata_builder = attn_metadata_builders[i]
|
||||
metadata = attn_metadata_builder.build(
|
||||
@@ -114,8 +112,8 @@ def build_attn_state(
|
||||
"""Build attention state for npu's attention backend."""
|
||||
if vllm_config.model_config.runner_type == "pooling":
|
||||
if isinstance(
|
||||
vllm_config.kv_cache_config.kv_cache_groups[0].kv_cache_spec,
|
||||
EncoderOnlyAttentionSpec,
|
||||
vllm_config.kv_cache_config.kv_cache_groups[0].kv_cache_spec,
|
||||
EncoderOnlyAttentionSpec,
|
||||
):
|
||||
attn_state = AscendAttentionState.PrefillNoCache
|
||||
else:
|
||||
@@ -126,16 +124,14 @@ def build_attn_state(
|
||||
# but only one token is not hit in cache.
|
||||
elif np.all(num_scheduled_tokens == 1):
|
||||
attn_state = AscendAttentionState.DecodeOnly
|
||||
if (vllm_config.speculative_config
|
||||
and vllm_config.speculative_config.method == 'mtp'):
|
||||
if vllm_config.speculative_config and vllm_config.speculative_config.method == "mtp":
|
||||
# SpecDecoding now supports seq_len=1 and seq_len=2
|
||||
# In Prefilling Decoding Disaggregation scenario, SpecDecoding
|
||||
# need to supports seq_len=1
|
||||
attn_state = AscendAttentionState.SpecDecoding
|
||||
# Speculative decoding.
|
||||
elif np.all(num_valid_tokens == 1):
|
||||
if (vllm_config.speculative_config
|
||||
and vllm_config.speculative_config.method == 'mtp'):
|
||||
if vllm_config.speculative_config and vllm_config.speculative_config.method == "mtp":
|
||||
attn_state = AscendAttentionState.SpecDecoding
|
||||
else:
|
||||
attn_state = AscendAttentionState.ChunkedPrefill
|
||||
|
||||
@@ -22,15 +22,16 @@ import torch
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.logger import init_logger
|
||||
from vllm.v1.core.sched.output import SchedulerOutput
|
||||
from vllm.v1.worker.gpu.input_batch import (InputBatch,
|
||||
combine_sampled_and_draft_tokens,
|
||||
prepare_pos_seq_lens,
|
||||
prepare_prefill_inputs)
|
||||
from vllm.v1.worker.gpu.input_batch import (
|
||||
InputBatch,
|
||||
combine_sampled_and_draft_tokens,
|
||||
prepare_pos_seq_lens,
|
||||
prepare_prefill_inputs,
|
||||
)
|
||||
from vllm.v1.worker.gpu.model_runner import GPUModelRunner
|
||||
|
||||
from vllm_ascend.worker.v2.aclgraph_utils import AclGraphManager
|
||||
from vllm_ascend.worker.v2.attn_utils import (build_attn_metadata,
|
||||
build_attn_state)
|
||||
from vllm_ascend.worker.v2.attn_utils import build_attn_metadata, build_attn_state
|
||||
from vllm_ascend.worker.v2.input_batch import AscendInputBuffers
|
||||
from vllm_ascend.worker.v2.sample.sampler import AscendSampler
|
||||
from vllm_ascend.worker.v2.spec_decode import init_speculator
|
||||
@@ -45,7 +46,7 @@ class NPUModelRunner(GPUModelRunner):
|
||||
"""Model runner for Ascend NPUs."""
|
||||
|
||||
def __init__(self, vllm_config: VllmConfig, device: torch.device):
|
||||
with (torch_cuda_wrapper(), uva_wrapper()):
|
||||
with torch_cuda_wrapper(), uva_wrapper():
|
||||
super().__init__(vllm_config, device)
|
||||
|
||||
# because we will override these attribute, delete these attribute to
|
||||
@@ -94,7 +95,8 @@ class NPUModelRunner(GPUModelRunner):
|
||||
# we need to adjust triton operators in sampler,
|
||||
# so reinitialize sampler here.
|
||||
self.sampler: AscendSampler = AscendSampler(
|
||||
logprobs_mode=self.model_config.logprobs_mode, )
|
||||
logprobs_mode=self.model_config.logprobs_mode,
|
||||
)
|
||||
|
||||
# we need to copy num_computed_tokens back to cpu to help
|
||||
# update actual seq_lens_cpu. gpu attention backend doesn't need these
|
||||
@@ -131,16 +133,12 @@ class NPUModelRunner(GPUModelRunner):
|
||||
|
||||
self._update_seq_lens_cpu(scheduler_output, req_ids)
|
||||
|
||||
num_scheduled_tokens = np.array(
|
||||
[scheduler_output.num_scheduled_tokens[i] for i in req_ids],
|
||||
dtype=np.int32)
|
||||
num_scheduled_tokens = np.array([scheduler_output.num_scheduled_tokens[i] for i in req_ids], dtype=np.int32)
|
||||
num_valid_tokens = num_scheduled_tokens
|
||||
if scheduler_output.scheduled_spec_decode_tokens:
|
||||
num_valid_tokens = np.array(
|
||||
[
|
||||
num_tokens - len(
|
||||
scheduler_output.scheduled_spec_decode_tokens.get(
|
||||
i, []))
|
||||
num_tokens - len(scheduler_output.scheduled_spec_decode_tokens.get(i, []))
|
||||
for num_tokens, i in zip(num_scheduled_tokens, req_ids)
|
||||
],
|
||||
dtype=np.int32,
|
||||
@@ -153,9 +151,7 @@ class NPUModelRunner(GPUModelRunner):
|
||||
num_valid_tokens,
|
||||
)
|
||||
|
||||
idx_mapping_list = [
|
||||
self.req_states.req_id_to_index[req_id] for req_id in req_ids
|
||||
]
|
||||
idx_mapping_list = [self.req_states.req_id_to_index[req_id] for req_id in req_ids]
|
||||
idx_mapping = self.input_buffers.idx_mapping
|
||||
idx_mapping.np[:num_reqs] = idx_mapping_list
|
||||
idx_mapping_np = idx_mapping.np[:num_reqs]
|
||||
@@ -167,16 +163,11 @@ class NPUModelRunner(GPUModelRunner):
|
||||
# No draft token scheduled (common case).
|
||||
total_num_draft_tokens = 0
|
||||
total_num_logits = num_reqs
|
||||
cu_num_logits = torch.arange(num_reqs + 1,
|
||||
device=self.device,
|
||||
dtype=torch.int32)
|
||||
cu_num_logits = torch.arange(num_reqs + 1, device=self.device, dtype=torch.int32)
|
||||
else:
|
||||
draft_tokens = scheduler_output.scheduled_spec_decode_tokens
|
||||
num_draft_tokens = np.array(
|
||||
[
|
||||
len(draft_tokens[req_id]) if req_id in draft_tokens else 0
|
||||
for req_id in req_ids
|
||||
],
|
||||
[len(draft_tokens[req_id]) if req_id in draft_tokens else 0 for req_id in req_ids],
|
||||
dtype=np.int32,
|
||||
)
|
||||
total_num_draft_tokens = int(num_draft_tokens.sum())
|
||||
@@ -184,10 +175,9 @@ class NPUModelRunner(GPUModelRunner):
|
||||
|
||||
np.cumsum(
|
||||
num_draft_tokens + 1,
|
||||
out=self.input_buffers.cu_num_logits.np[1:num_reqs + 1],
|
||||
out=self.input_buffers.cu_num_logits.np[1 : num_reqs + 1],
|
||||
)
|
||||
cu_num_logits = self.input_buffers.cu_num_logits.copy_to_gpu(
|
||||
num_reqs + 1)
|
||||
cu_num_logits = self.input_buffers.cu_num_logits.copy_to_gpu(num_reqs + 1)
|
||||
|
||||
# Block tables: num_kv_cache_groups x [num_reqs, max_num_blocks]
|
||||
block_tables = self.block_tables.gather_block_tables(idx_mapping_npu)
|
||||
@@ -195,20 +185,15 @@ class NPUModelRunner(GPUModelRunner):
|
||||
# Get query_start_loc.
|
||||
np.cumsum(
|
||||
num_scheduled_tokens,
|
||||
out=self.input_buffers.query_start_loc.np[1:num_reqs + 1],
|
||||
out=self.input_buffers.query_start_loc.np[1 : num_reqs + 1],
|
||||
)
|
||||
# Pad for full CUDA graph mode.
|
||||
# Some attention backends like FA3 require query_start_loc to be non-decreasing.
|
||||
self.input_buffers.query_start_loc.np[num_reqs + 1:] = num_tokens
|
||||
self.input_buffers.query_start_loc.np[num_reqs + 1 :] = num_tokens
|
||||
self.input_buffers.query_start_loc.copy_to_gpu()
|
||||
query_start_loc_gpu = self.input_buffers.query_start_loc.gpu[:
|
||||
num_reqs +
|
||||
1]
|
||||
query_start_loc_cpu = self.input_buffers.query_start_loc.cpu[:
|
||||
num_reqs +
|
||||
1]
|
||||
query_start_loc_np = self.input_buffers.query_start_loc.np[:num_reqs +
|
||||
1]
|
||||
query_start_loc_gpu = self.input_buffers.query_start_loc.gpu[: num_reqs + 1]
|
||||
query_start_loc_cpu = self.input_buffers.query_start_loc.cpu[: num_reqs + 1]
|
||||
query_start_loc_np = self.input_buffers.query_start_loc.np[: num_reqs + 1]
|
||||
|
||||
# Get prefill tokens.
|
||||
prepare_prefill_inputs(
|
||||
@@ -249,7 +234,8 @@ class NPUModelRunner(GPUModelRunner):
|
||||
|
||||
# Compute slot mappings: [num_kv_cache_groups, num_tokens]
|
||||
slot_mappings = self.block_tables.compute_slot_mappings(
|
||||
query_start_loc_gpu, self.input_buffers.positions[:num_tokens])
|
||||
query_start_loc_gpu, self.input_buffers.positions[:num_tokens]
|
||||
)
|
||||
|
||||
# Layer name -> attention metadata.
|
||||
# TODO(Ronald1995): try to add a new method `build_attn_metadata` in
|
||||
@@ -263,8 +249,7 @@ class NPUModelRunner(GPUModelRunner):
|
||||
query_start_loc_cpu=query_start_loc_cpu,
|
||||
seq_lens=self.input_buffers.seq_lens,
|
||||
seq_lens_np=self.input_buffers.seq_lens_np,
|
||||
num_computed_tokens_cpu=self.req_states.
|
||||
num_computed_tokens_cpu[idx_mapping_cpu],
|
||||
num_computed_tokens_cpu=self.req_states.num_computed_tokens_cpu[idx_mapping_cpu],
|
||||
block_tables=block_tables,
|
||||
slot_mappings=slot_mappings,
|
||||
kv_cache_config=self.kv_cache_config,
|
||||
@@ -335,16 +320,13 @@ class NPUModelRunner(GPUModelRunner):
|
||||
req_index = self.req_states.req_id_to_index[req_id]
|
||||
# num_computed_tokens_cpu has reverted by num_rejected_tokens already.
|
||||
# in super postprocess method.
|
||||
self.req_states.num_computed_tokens_cpu[
|
||||
req_index] = self.num_computed_tokens_cpu[req_index]
|
||||
self.req_states.num_computed_tokens_cpu[req_index] = self.num_computed_tokens_cpu[req_index]
|
||||
|
||||
# update seq_lens_cpu
|
||||
for i, req_id in enumerate(req_ids):
|
||||
req_index = self.req_states.req_id_to_index[req_id]
|
||||
num_computed_tokens = self.req_states.num_computed_tokens_cpu[
|
||||
req_index]
|
||||
self.input_buffers.seq_lens_cpu[
|
||||
i] = num_computed_tokens + num_scheduled_tokens[req_id]
|
||||
num_computed_tokens = self.req_states.num_computed_tokens_cpu[req_index]
|
||||
self.input_buffers.seq_lens_cpu[i] = num_computed_tokens + num_scheduled_tokens[req_id]
|
||||
|
||||
def eplb_warmup(self):
|
||||
# TODO(Ronald1995): just define the method in case calling error in
|
||||
|
||||
@@ -76,8 +76,7 @@ def _gumbel_sample_kernel(
|
||||
idx = tl.argmax(logits, axis=0)
|
||||
token_id = block_idx * BLOCK_SIZE + idx
|
||||
value = tl.max(logits, axis=0)
|
||||
tl.store(local_argmax_ptr + req_idx * local_argmax_stride + block_idx,
|
||||
token_id)
|
||||
tl.store(local_argmax_ptr + req_idx * local_argmax_stride + block_idx, token_id)
|
||||
tl.store(local_max_ptr + req_idx * local_max_stride + block_idx, value)
|
||||
|
||||
|
||||
|
||||
@@ -68,8 +68,7 @@ def _penalties_and_temperature_kernel(
|
||||
if use_penalty:
|
||||
req_state_idx = tl.load(idx_mapping_ptr + batch_idx)
|
||||
output_bin_counts = tl.load(
|
||||
output_bin_counts_ptr + req_state_idx * output_bin_counts_stride +
|
||||
block,
|
||||
output_bin_counts_ptr + req_state_idx * output_bin_counts_stride + block,
|
||||
mask=mask,
|
||||
)
|
||||
# to use vector core, if use > 0 will use scalar to slow down performance
|
||||
@@ -77,11 +76,9 @@ def _penalties_and_temperature_kernel(
|
||||
|
||||
# Apply repetition penalties.
|
||||
if use_rep_penalty:
|
||||
packed_block = block_idx * BLOCK_SIZE // 32 + tl.arange(
|
||||
0, BLOCK_SIZE // 32)
|
||||
packed_block = block_idx * BLOCK_SIZE // 32 + tl.arange(0, BLOCK_SIZE // 32)
|
||||
packed_mask = tl.load(
|
||||
prompt_bin_mask_ptr + req_state_idx * prompt_bin_mask_stride +
|
||||
packed_block,
|
||||
prompt_bin_mask_ptr + req_state_idx * prompt_bin_mask_stride + packed_block,
|
||||
mask=packed_block < tl.cdiv(vocab_size, 32),
|
||||
)
|
||||
# the compiler itself does not optimize right-shift operations, so we change the same func
|
||||
@@ -97,8 +94,7 @@ def _penalties_and_temperature_kernel(
|
||||
prompt_bin_mask = prompt_bin_mask.reshape(BLOCK_SIZE)
|
||||
|
||||
# If token appears in prompt or output, apply, otherwise use 1.0 for no-op.
|
||||
scale = tl.where(prompt_bin_mask | output_bin_mask, rep_penalty,
|
||||
1.0)
|
||||
scale = tl.where(prompt_bin_mask | output_bin_mask, rep_penalty, 1.0)
|
||||
# If logits are positive, divide by penalty, otherwise multiply by penalty.
|
||||
logits *= tl.where(logits > 0, 1.0 / scale, scale)
|
||||
|
||||
|
||||
@@ -16,18 +16,16 @@
|
||||
#
|
||||
|
||||
import torch
|
||||
from vllm.v1.sample.ops.topk_topp_sampler import apply_top_k_top_p
|
||||
from vllm.v1.sample.metadata import SamplingMetadata
|
||||
from vllm.v1.sample.ops.topk_topp_sampler import apply_top_k_top_p
|
||||
from vllm.v1.worker.gpu.sample.min_p import apply_min_p
|
||||
from vllm.v1.worker.gpu.sample.sampler import Sampler
|
||||
|
||||
from vllm_ascend.worker.v2.sample.gumbel import gumbel_sample
|
||||
from vllm_ascend.worker.v2.sample.penalties import \
|
||||
apply_penalties_and_temperature
|
||||
from vllm_ascend.worker.v2.sample.penalties import apply_penalties_and_temperature
|
||||
|
||||
|
||||
class AscendSampler(Sampler):
|
||||
|
||||
def sample(
|
||||
self,
|
||||
logits: torch.Tensor,
|
||||
@@ -45,8 +43,7 @@ class AscendSampler(Sampler):
|
||||
if sampling_metadata.min_p is not None:
|
||||
apply_min_p(logits, sampling_metadata.min_p)
|
||||
# Apply top_k and/or top_p. This might return a new tensor.
|
||||
logits = apply_top_k_top_p(logits, sampling_metadata.top_k,
|
||||
sampling_metadata.top_p)
|
||||
logits = apply_top_k_top_p(logits, sampling_metadata.top_k, sampling_metadata.top_p)
|
||||
|
||||
sampled = gumbel_sample(
|
||||
logits,
|
||||
|
||||
@@ -30,9 +30,7 @@ def init_speculator(
|
||||
speculative_config = vllm_config.speculative_config
|
||||
assert speculative_config is not None
|
||||
if speculative_config.use_eagle():
|
||||
from vllm_ascend.worker.v2.spec_decode.eagle import \
|
||||
AscendEagleSpeculator
|
||||
from vllm_ascend.worker.v2.spec_decode.eagle import AscendEagleSpeculator
|
||||
|
||||
return AscendEagleSpeculator(vllm_config, device)
|
||||
raise NotImplementedError(
|
||||
f"{speculative_config.method} is not supported yet.")
|
||||
raise NotImplementedError(f"{speculative_config.method} is not supported yet.")
|
||||
|
||||
@@ -30,7 +30,6 @@ from vllm_ascend.worker.v2.attn_utils import build_attn_metadata
|
||||
|
||||
|
||||
class AscendEagleSpeculator(EagleSpeculator):
|
||||
|
||||
def __init__(self, vllm_config: VllmConfig, device: torch.device):
|
||||
"""Override GPU EagleSpeculator.__init__ for Ascend NPUs.
|
||||
attnention metadata building in Ascend backend needs more information,
|
||||
|
||||
@@ -63,8 +63,8 @@ class AscendRequestState(RequestState):
|
||||
# NOTE(Ronald1995): Ascend NPUs do not support UVA yet,
|
||||
# so we use CpuGpuBuffer to allocate prefill_token_ids buffer.
|
||||
self.prefill_token_ids: CpuGpuBuffer = self._make_buffer( # type: ignore
|
||||
(self.max_num_reqs, self.max_model_len),
|
||||
dtype=torch.int32)
|
||||
(self.max_num_reqs, self.max_model_len), dtype=torch.int32
|
||||
)
|
||||
|
||||
def add_request(
|
||||
self,
|
||||
@@ -75,7 +75,6 @@ class AscendRequestState(RequestState):
|
||||
sampling_params,
|
||||
lora_request,
|
||||
):
|
||||
|
||||
super().add_request(
|
||||
req_id,
|
||||
prompt_len,
|
||||
@@ -93,7 +92,6 @@ def uva_wrapper():
|
||||
"""Context manager to disable UVA for Ascend NPUs."""
|
||||
|
||||
class UvaBufferWrapper:
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
import copy
|
||||
import gc
|
||||
from types import NoneType
|
||||
from typing import Optional
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
@@ -29,12 +28,9 @@ import vllm.envs as envs_vllm
|
||||
from torch_npu.op_plugin.atb._atb_ops import _register_atb_extensions
|
||||
from torch_npu.profiler import dynamic_profile as dp
|
||||
from vllm.config import CUDAGraphMode, VllmConfig, set_current_vllm_config
|
||||
from vllm.distributed import (ensure_model_parallel_initialized,
|
||||
init_distributed_environment)
|
||||
from vllm.distributed import ensure_model_parallel_initialized, init_distributed_environment
|
||||
from vllm.distributed.ec_transfer import ensure_ec_transfer_initialized
|
||||
from vllm.distributed.kv_transfer import (ensure_kv_transfer_initialized,
|
||||
get_kv_transfer_group,
|
||||
has_kv_transfer_group)
|
||||
from vllm.distributed.kv_transfer import ensure_kv_transfer_initialized, get_kv_transfer_group, has_kv_transfer_group
|
||||
from vllm.distributed.parallel_state import get_pp_group, get_tp_group
|
||||
from vllm.logger import logger
|
||||
from vllm.lora.request import LoRARequest
|
||||
@@ -44,8 +40,7 @@ from vllm.utils.mem_constants import GiB_bytes
|
||||
from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE
|
||||
from vllm.v1.core.sched.output import GrammarOutput, SchedulerOutput
|
||||
from vllm.v1.kv_cache_interface import KVCacheConfig, KVCacheSpec
|
||||
from vllm.v1.outputs import (EMPTY_MODEL_RUNNER_OUTPUT, AsyncModelRunnerOutput,
|
||||
DraftTokenIds, ModelRunnerOutput)
|
||||
from vllm.v1.outputs import EMPTY_MODEL_RUNNER_OUTPUT, AsyncModelRunnerOutput, DraftTokenIds, ModelRunnerOutput
|
||||
from vllm.v1.worker.worker_base import WorkerBase
|
||||
from vllm.v1.worker.workspace import init_workspace_manager
|
||||
|
||||
@@ -56,37 +51,38 @@ from vllm_ascend.cpu_binding import bind_cpus
|
||||
from vllm_ascend.device_allocator.camem import CaMemAllocator
|
||||
from vllm_ascend.distributed.parallel_state import init_ascend_model_parallel
|
||||
from vllm_ascend.ops.triton.triton_utils import init_device_properties_triton
|
||||
from vllm_ascend.utils import (AscendDeviceType, check_ascend_device_type,
|
||||
enable_sp, get_ascend_device_type,
|
||||
register_ascend_customop)
|
||||
from vllm_ascend.utils import (
|
||||
AscendDeviceType,
|
||||
check_ascend_device_type,
|
||||
enable_sp,
|
||||
get_ascend_device_type,
|
||||
register_ascend_customop,
|
||||
)
|
||||
from vllm_ascend.worker.model_runner_v1 import NPUModelRunner
|
||||
|
||||
torch._dynamo.trace_rules.clear_lru_cache() # noqa: E402
|
||||
from torch._dynamo.variables import TorchInGraphFunctionVariable # noqa: E402
|
||||
|
||||
from vllm.utils.torch_utils import set_random_seed
|
||||
from vllm.utils.torch_utils import set_random_seed # noqa: E402
|
||||
|
||||
torch_non_c_binding_in_graph_functions_npu = dict.fromkeys(
|
||||
["torch.npu.current_stream"],
|
||||
TorchInGraphFunctionVariable,
|
||||
) # noqa: E402
|
||||
torch_non_c_binding_in_graph_functions_npu[
|
||||
"torch.npu.stream"] = TorchInGraphFunctionVariable # noqa: E402
|
||||
torch._dynamo.trace_rules.torch_name_rule_map.append(
|
||||
torch_non_c_binding_in_graph_functions_npu) # noqa: E402
|
||||
torch_non_c_binding_in_graph_functions_npu["torch.npu.stream"] = TorchInGraphFunctionVariable # noqa: E402
|
||||
torch._dynamo.trace_rules.torch_name_rule_map.append(torch_non_c_binding_in_graph_functions_npu) # noqa: E402
|
||||
|
||||
|
||||
class NPUWorker(WorkerBase):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vllm_config: VllmConfig,
|
||||
local_rank: int,
|
||||
rank: int,
|
||||
distributed_init_method: str,
|
||||
is_driver_worker: bool = False,
|
||||
# Additional parameters for compatibility with vllm
|
||||
**kwargs):
|
||||
self,
|
||||
vllm_config: VllmConfig,
|
||||
local_rank: int,
|
||||
rank: int,
|
||||
distributed_init_method: str,
|
||||
is_driver_worker: bool = False,
|
||||
# Additional parameters for compatibility with vllm
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the worker for Ascend."""
|
||||
if not envs_ascend.COMPILE_CUSTOM_KERNELS:
|
||||
logger.warning(
|
||||
@@ -96,14 +92,17 @@ class NPUWorker(WorkerBase):
|
||||
|
||||
# register patch for vllm
|
||||
from vllm_ascend.utils import adapt_patch
|
||||
|
||||
adapt_patch()
|
||||
# Import _inductor for graph mode execution with triton
|
||||
# This lazy import avoids torch_npu re-initialization in patch
|
||||
from vllm.triton_utils import HAS_TRITON
|
||||
|
||||
if HAS_TRITON:
|
||||
import torch_npu._inductor # noqa: F401
|
||||
# Register ops when worker init.
|
||||
from vllm_ascend import ops
|
||||
|
||||
ops.register_dummy_fusion_op()
|
||||
if get_ascend_device_type() != AscendDeviceType.A5:
|
||||
_register_atb_extensions()
|
||||
@@ -112,17 +111,18 @@ class NPUWorker(WorkerBase):
|
||||
init_ascend_config(vllm_config)
|
||||
check_ascend_device_type()
|
||||
|
||||
super().__init__(vllm_config=vllm_config,
|
||||
local_rank=local_rank,
|
||||
rank=rank,
|
||||
distributed_init_method=distributed_init_method,
|
||||
is_driver_worker=is_driver_worker)
|
||||
super().__init__(
|
||||
vllm_config=vllm_config,
|
||||
local_rank=local_rank,
|
||||
rank=rank,
|
||||
distributed_init_method=distributed_init_method,
|
||||
is_driver_worker=is_driver_worker,
|
||||
)
|
||||
|
||||
if self.cache_config.cache_dtype == "auto":
|
||||
self.cache_dtype = self.model_config.dtype
|
||||
else:
|
||||
self.cache_dtype = STR_DTYPE_TO_TORCH_DTYPE[
|
||||
self.cache_config.cache_dtype]
|
||||
self.cache_dtype = STR_DTYPE_TO_TORCH_DTYPE[self.cache_config.cache_dtype]
|
||||
|
||||
self.profiler = self._init_profiler()
|
||||
if vllm_config.model_config and vllm_config.model_config.enable_sleep_mode:
|
||||
@@ -130,8 +130,8 @@ class NPUWorker(WorkerBase):
|
||||
self._sleep_saved_buffers: dict[str, torch.Tensor] = {}
|
||||
|
||||
# FixMe: this is a patch to fix the issue cause by https://github.com/vllm-project/vllm/commit/de94289a98d7ec52a5ef02719e01a1db8b505170
|
||||
from vllm.model_executor.layers.linear import \
|
||||
WEIGHT_LOADER_V2_SUPPORTED
|
||||
from vllm.model_executor.layers.linear import WEIGHT_LOADER_V2_SUPPORTED
|
||||
|
||||
if "UnquantizedLinearMethod" in WEIGHT_LOADER_V2_SUPPORTED:
|
||||
WEIGHT_LOADER_V2_SUPPORTED.remove("UnquantizedLinearMethod")
|
||||
|
||||
@@ -151,33 +151,33 @@ class NPUWorker(WorkerBase):
|
||||
|
||||
# Either SIGTERM or SIGINT will terminate the worker
|
||||
import signal
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
|
||||
def uninstall_static_kernel(self):
|
||||
import os
|
||||
import fcntl
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
ascend_home_path = os.environ["ASCEND_HOME_PATH"]
|
||||
static_kernel_dir_path = os.path.join(ascend_home_path, 'opp/static_kernel')
|
||||
uninstall_script_path = os.path.join(static_kernel_dir_path, 'ai_core/uninstall.sh')
|
||||
lock_file_path = os.path.join(static_kernel_dir_path, 'uninstall.lock')
|
||||
static_kernel_dir_path = os.path.join(ascend_home_path, "opp/static_kernel")
|
||||
uninstall_script_path = os.path.join(static_kernel_dir_path, "ai_core/uninstall.sh")
|
||||
lock_file_path = os.path.join(static_kernel_dir_path, "uninstall.lock")
|
||||
|
||||
if not os.path.exists(uninstall_script_path):
|
||||
return
|
||||
with open(lock_file_path, 'w') as lock_fd:
|
||||
with open(lock_file_path, "w") as lock_fd:
|
||||
try:
|
||||
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
subprocess.Popen(
|
||||
['bash', uninstall_script_path],
|
||||
["bash", uninstall_script_path],
|
||||
stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
start_new_session=True
|
||||
start_new_session=True,
|
||||
)
|
||||
except (BlockingIOError, OSError) as e:
|
||||
except (BlockingIOError, OSError):
|
||||
return
|
||||
finally:
|
||||
try:
|
||||
@@ -187,32 +187,30 @@ class NPUWorker(WorkerBase):
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
def sleep(self, level: int = 1) -> None:
|
||||
free_bytes_before_sleep = torch.npu.mem_get_info()[0]
|
||||
# Save the buffers before level 2 sleep
|
||||
if level == 2:
|
||||
model = self.model_runner.model
|
||||
self._sleep_saved_buffers = {
|
||||
name: buffer.cpu().clone()
|
||||
for name, buffer in model.named_buffers()
|
||||
}
|
||||
self._sleep_saved_buffers = {name: buffer.cpu().clone() for name, buffer in model.named_buffers()}
|
||||
allocator = CaMemAllocator.get_instance()
|
||||
allocator.sleep(offload_tags=("weights", ) if level == 1 else tuple())
|
||||
allocator.sleep(offload_tags=("weights",) if level == 1 else tuple())
|
||||
free_bytes_after_sleep, total = torch.npu.mem_get_info()
|
||||
freed_bytes = free_bytes_after_sleep - free_bytes_before_sleep
|
||||
used_bytes = total - free_bytes_after_sleep
|
||||
assert freed_bytes >= 0, "Memory usage increased after sleeping."
|
||||
logger.info(
|
||||
"Sleep mode freed %.2f GiB memory, "
|
||||
"%.2f GiB memory is still in use.", freed_bytes / GiB_bytes,
|
||||
used_bytes / GiB_bytes)
|
||||
"Sleep mode freed %.2f GiB memory, %.2f GiB memory is still in use.",
|
||||
freed_bytes / GiB_bytes,
|
||||
used_bytes / GiB_bytes,
|
||||
)
|
||||
|
||||
def wake_up(self, tags: Optional[list[str]] = None) -> None:
|
||||
def wake_up(self, tags: list[str] | None = None) -> None:
|
||||
if envs_ascend.VLLM_ASCEND_ENABLE_NZ:
|
||||
raise ValueError(
|
||||
"FRACTAL_NZ mode is enabled. This may cause model parameter precision issues "
|
||||
"in the RL scenarios. Please set VLLM_ASCEND_ENABLE_NZ=0.")
|
||||
"in the RL scenarios. Please set VLLM_ASCEND_ENABLE_NZ=0."
|
||||
)
|
||||
allocator = CaMemAllocator.get_instance()
|
||||
allocator.wake_up(tags=tags)
|
||||
|
||||
@@ -220,22 +218,21 @@ class NPUWorker(WorkerBase):
|
||||
model = self.model_runner.model
|
||||
if tags is None or "weights" in tags:
|
||||
for name, param in model.named_parameters():
|
||||
if 'w2_weight' in name and param.shape[2] == hidden_size:
|
||||
parts = name.split('.')
|
||||
if "w2_weight" in name and param.shape[2] == hidden_size:
|
||||
parts = name.split(".")
|
||||
param_name = parts[-1]
|
||||
parent_module = model.get_submodule(".".join(parts[:-1]))
|
||||
|
||||
w2_data = param.transpose(1, 2)
|
||||
w2_data = torch.nn.Parameter(w2_data, requires_grad=False)
|
||||
setattr(parent_module, param_name, w2_data)
|
||||
elif 'w13_weight' in name and param.shape[1] == hidden_size:
|
||||
parts = name.split('.')
|
||||
elif "w13_weight" in name and param.shape[1] == hidden_size:
|
||||
parts = name.split(".")
|
||||
param_name = parts[-1]
|
||||
parent_module = model.get_submodule(".".join(parts[:-1]))
|
||||
|
||||
w13_data = param.transpose(1, 2)
|
||||
w13_data = torch.nn.Parameter(w13_data,
|
||||
requires_grad=False)
|
||||
w13_data = torch.nn.Parameter(w13_data, requires_grad=False)
|
||||
setattr(parent_module, param_name, w13_data)
|
||||
|
||||
# Restore the buffers after level 2 sleep
|
||||
@@ -245,8 +242,7 @@ class NPUWorker(WorkerBase):
|
||||
buffer.data.copy_(self._sleep_saved_buffers[name].data)
|
||||
self._sleep_saved_buffers = {}
|
||||
|
||||
def initialize_cache(self, num_gpu_blocks: int,
|
||||
num_cpu_blocks: int) -> None:
|
||||
def initialize_cache(self, num_gpu_blocks: int, num_cpu_blocks: int) -> None:
|
||||
self.cache_config.num_gpu_blocks = num_gpu_blocks
|
||||
self.cache_config.num_cpu_blocks = num_cpu_blocks
|
||||
|
||||
@@ -255,18 +251,19 @@ class NPUWorker(WorkerBase):
|
||||
torch.npu.set_device(device)
|
||||
torch.npu.empty_cache()
|
||||
|
||||
if (self.parallel_config.data_parallel_size > 1
|
||||
and self.parallel_config.data_parallel_size_local > 0
|
||||
and self.parallel_config.distributed_executor_backend
|
||||
not in ["ray", "external_launcher"] and
|
||||
self.vllm_config.parallel_config.data_parallel_backend != "ray"
|
||||
and self.vllm_config.parallel_config.nnodes_within_dp == 1):
|
||||
visible_device_count = (torch.npu.device_count()
|
||||
if torch.npu.is_available() else 0)
|
||||
if (
|
||||
self.parallel_config.data_parallel_size > 1
|
||||
and self.parallel_config.data_parallel_size_local > 0
|
||||
and self.parallel_config.distributed_executor_backend not in ["ray", "external_launcher"]
|
||||
and self.vllm_config.parallel_config.data_parallel_backend != "ray"
|
||||
and self.vllm_config.parallel_config.nnodes_within_dp == 1
|
||||
):
|
||||
visible_device_count = torch.npu.device_count() if torch.npu.is_available() else 0
|
||||
assert self.parallel_config.local_world_size <= visible_device_count, (
|
||||
f"local_world_size ({self.parallel_config.local_world_size}) must "
|
||||
f"be less than or equal to the number of visible devices "
|
||||
f"({visible_device_count}).")
|
||||
f"({visible_device_count})."
|
||||
)
|
||||
|
||||
self.init_npu_memory = torch.npu.mem_get_info()[0]
|
||||
# Initialize the distributed environment.
|
||||
@@ -281,9 +278,7 @@ class NPUWorker(WorkerBase):
|
||||
try:
|
||||
bind_cpus(self.local_rank)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"Bind cpus failed in rank{self.local_rank}: {e} Skip binding cpu."
|
||||
)
|
||||
logger.warning(f"Bind cpus failed in rank{self.local_rank}: {e} Skip binding cpu.")
|
||||
return device
|
||||
|
||||
def init_device(self):
|
||||
@@ -296,11 +291,9 @@ class NPUWorker(WorkerBase):
|
||||
init_workspace_manager(self.device, num_ubatches)
|
||||
# Init ModelRunner here, so that we have access to self.device.
|
||||
if self.use_v2_model_runner:
|
||||
logger.warning(
|
||||
"npu model runner v2 is in developing, some features doesn't work for now."
|
||||
)
|
||||
from vllm_ascend.worker.v2.model_runner import \
|
||||
NPUModelRunner as NPUModelRunnerV2
|
||||
logger.warning("npu model runner v2 is in developing, some features doesn't work for now.")
|
||||
from vllm_ascend.worker.v2.model_runner import NPUModelRunner as NPUModelRunnerV2
|
||||
|
||||
self.model_runner = NPUModelRunnerV2(self.vllm_config, self.device)
|
||||
else:
|
||||
self.model_runner = NPUModelRunner(self.vllm_config, self.device)
|
||||
@@ -327,27 +320,22 @@ class NPUWorker(WorkerBase):
|
||||
"Error in memory profiling. "
|
||||
f"Initial free memory {self.init_npu_memory}, current free memory"
|
||||
f" {free_npu_memory}. This happens when the NPU memory was "
|
||||
"not properly cleaned up before initializing the vLLM instance.")
|
||||
"not properly cleaned up before initializing the vLLM instance."
|
||||
)
|
||||
|
||||
# Get the peak memory allocation recorded by torch
|
||||
peak_memory = torch_npu.npu.memory_stats()["allocated_bytes.all.peak"]
|
||||
# TODO: don`t need impl this func after empty_cache in
|
||||
# Worker.determine_num_available_blocks() unified`
|
||||
torch.npu.empty_cache()
|
||||
torch_allocated_bytes = torch_npu.npu.memory_stats(
|
||||
)["allocated_bytes.all.current"]
|
||||
total_allocated_bytes = torch_npu.npu.mem_get_info(
|
||||
)[1] - torch_npu.npu.mem_get_info()[0]
|
||||
torch_allocated_bytes = torch_npu.npu.memory_stats()["allocated_bytes.all.current"]
|
||||
total_allocated_bytes = torch_npu.npu.mem_get_info()[1] - torch_npu.npu.mem_get_info()[0]
|
||||
non_torch_allocations = total_allocated_bytes - torch_allocated_bytes
|
||||
if non_torch_allocations > 0:
|
||||
peak_memory += non_torch_allocations
|
||||
available_kv_cache_memory = int(
|
||||
total_npu_memory * self.cache_config.gpu_memory_utilization -
|
||||
peak_memory)
|
||||
available_kv_cache_memory = int(total_npu_memory * self.cache_config.gpu_memory_utilization - peak_memory)
|
||||
available_kv_cache_memory = int(max(available_kv_cache_memory, 0))
|
||||
logger.info(
|
||||
f"Available memory: {available_kv_cache_memory}, total memory: {total_npu_memory}"
|
||||
)
|
||||
logger.info(f"Available memory: {available_kv_cache_memory}, total memory: {total_npu_memory}")
|
||||
return available_kv_cache_memory
|
||||
|
||||
def execute_model(
|
||||
@@ -361,32 +349,30 @@ class NPUWorker(WorkerBase):
|
||||
intermediate_tensors = None
|
||||
forward_pass = scheduler_output.total_num_scheduled_tokens > 0
|
||||
if forward_pass and not get_pp_group().is_first_rank:
|
||||
# If flashcomm1 is used, this all_gather_group parameter needs to be removed, otherwise it will conflict with the all-gather operation in flashcomm1.
|
||||
# If flashcomm1 is used, this all_gather_group parameter needs to be removed, otherwise
|
||||
# it will conflict with the all-gather operation in flashcomm1.
|
||||
if enable_sp():
|
||||
all_gather_group = None
|
||||
else:
|
||||
all_gather_group = get_tp_group()
|
||||
intermediate_tensors = IntermediateTensors(
|
||||
get_pp_group().recv_tensor_dict(
|
||||
all_gather_group=all_gather_group))
|
||||
get_pp_group().recv_tensor_dict(all_gather_group=all_gather_group)
|
||||
)
|
||||
|
||||
output = self.model_runner.execute_model(scheduler_output,
|
||||
intermediate_tensors)
|
||||
if isinstance(output,
|
||||
(ModelRunnerOutput, AsyncModelRunnerOutput, NoneType)):
|
||||
output = self.model_runner.execute_model(scheduler_output, intermediate_tensors)
|
||||
if isinstance(output, (ModelRunnerOutput, AsyncModelRunnerOutput, NoneType)):
|
||||
return output
|
||||
|
||||
assert isinstance(output, IntermediateTensors)
|
||||
parallel_config = self.vllm_config.parallel_config
|
||||
assert parallel_config.distributed_executor_backend != (
|
||||
"external_launcher") and not get_pp_group().is_last_rank
|
||||
# If flashcomm1 is used, this all_gather_group parameter needs to be removed, otherwise it will conflict with the all-gather operation in flashcomm1.
|
||||
assert parallel_config.distributed_executor_backend != ("external_launcher") and not get_pp_group().is_last_rank
|
||||
# If flashcomm1 is used, this all_gather_group parameter needs to be removed, otherwise
|
||||
# it will conflict with the all-gather operation in flashcomm1.
|
||||
if enable_sp():
|
||||
all_gather_group = None
|
||||
else:
|
||||
all_gather_group = get_tp_group()
|
||||
get_pp_group().send_tensor_dict(output.tensors,
|
||||
all_gather_group=all_gather_group)
|
||||
get_pp_group().send_tensor_dict(output.tensors, all_gather_group=all_gather_group)
|
||||
|
||||
kv_connector_output = output.kv_connector_output
|
||||
if not kv_connector_output:
|
||||
@@ -394,28 +380,24 @@ class NPUWorker(WorkerBase):
|
||||
|
||||
# In case of PP with kv transfer, we need to pass through the
|
||||
# kv_connector_output
|
||||
if (not kv_connector_output.finished_sending
|
||||
and not kv_connector_output.finished_recving):
|
||||
if not kv_connector_output.finished_sending and not kv_connector_output.finished_recving:
|
||||
return EMPTY_MODEL_RUNNER_OUTPUT
|
||||
output = copy.copy(EMPTY_MODEL_RUNNER_OUTPUT)
|
||||
output.kv_connector_output = kv_connector_output
|
||||
return output
|
||||
|
||||
@torch.inference_mode()
|
||||
def sample_tokens(
|
||||
self, grammar_output: "GrammarOutput"
|
||||
) -> ModelRunnerOutput | AsyncModelRunnerOutput:
|
||||
def sample_tokens(self, grammar_output: "GrammarOutput") -> ModelRunnerOutput | AsyncModelRunnerOutput:
|
||||
return self.model_runner.sample_tokens(grammar_output)
|
||||
|
||||
def load_model(self) -> None:
|
||||
if self.vllm_config.model_config.enable_sleep_mode:
|
||||
allocator = CaMemAllocator.get_instance()
|
||||
assert allocator.get_current_usage() == 0, (
|
||||
"Sleep mode can only be "
|
||||
"used for one instance per process.")
|
||||
assert allocator.get_current_usage() == 0, "Sleep mode can only be used for one instance per process."
|
||||
context = allocator.use_memory_pool(tag="weights")
|
||||
else:
|
||||
from contextlib import nullcontext
|
||||
|
||||
context = nullcontext() # type: ignore
|
||||
|
||||
with context, set_current_vllm_config(self.vllm_config):
|
||||
@@ -423,19 +405,15 @@ class NPUWorker(WorkerBase):
|
||||
|
||||
def compile_or_warm_up_model(self) -> None:
|
||||
# Note: need to adapt for graph mode.
|
||||
warmup_sizes = (self.vllm_config.compilation_config.compile_sizes
|
||||
or []).copy()
|
||||
warmup_sizes = (self.vllm_config.compilation_config.compile_sizes or []).copy()
|
||||
if not self.model_config.enforce_eager:
|
||||
cg_capture_sizes: list[int] = []
|
||||
if self.vllm_config.compilation_config.cudagraph_mode != CUDAGraphMode.NONE:
|
||||
cg_sizes = self.vllm_config.compilation_config.cudagraph_capture_sizes
|
||||
cg_capture_sizes = [] if cg_sizes is None else cg_sizes
|
||||
warmup_sizes = [
|
||||
x for x in warmup_sizes if x not in cg_capture_sizes
|
||||
]
|
||||
warmup_sizes = [x for x in warmup_sizes if x not in cg_capture_sizes]
|
||||
|
||||
compile_ranges = self.vllm_config.compilation_config.get_compile_ranges(
|
||||
)
|
||||
compile_ranges = self.vllm_config.compilation_config.get_compile_ranges()
|
||||
# For each compile_range, if none of the batch sizes
|
||||
# in warmup_sizes or cudagraph_capture_sizes are in the range,
|
||||
# add the end of the range to ensure compilation/warmup.
|
||||
@@ -467,7 +445,7 @@ class NPUWorker(WorkerBase):
|
||||
def get_model(self) -> nn.Module:
|
||||
return self.model_runner.get_model()
|
||||
|
||||
def get_kv_connector_handshake_metadata(self) -> Optional[dict]:
|
||||
def get_kv_connector_handshake_metadata(self) -> dict | None:
|
||||
"""Get KV connector metadata from this worker if available."""
|
||||
if not has_kv_transfer_group():
|
||||
return None
|
||||
@@ -503,6 +481,7 @@ class NPUWorker(WorkerBase):
|
||||
context = allocator.use_memory_pool(tag="kv_cache")
|
||||
else:
|
||||
from contextlib import nullcontext
|
||||
|
||||
context = nullcontext() # type: ignore
|
||||
with context:
|
||||
self.model_runner.initialize_kv_cache(kv_cache_config)
|
||||
@@ -528,21 +507,20 @@ class NPUWorker(WorkerBase):
|
||||
return self.model_runner.pin_lora(lora_id)
|
||||
|
||||
def execute_dummy_batch(self) -> None:
|
||||
self.model_runner._dummy_run(
|
||||
num_tokens=self.model_runner.decode_token_per_req,
|
||||
uniform_decode=True)
|
||||
self.model_runner._dummy_run(num_tokens=self.model_runner.decode_token_per_req, uniform_decode=True)
|
||||
|
||||
def _init_worker_distributed_environment(self) -> None:
|
||||
"""Initialize the distributed environment."""
|
||||
init_batch_invariance()
|
||||
init_distributed_environment(self.parallel_config.world_size,
|
||||
self.rank, self.distributed_init_method,
|
||||
self.local_rank, "hccl")
|
||||
init_distributed_environment(
|
||||
self.parallel_config.world_size, self.rank, self.distributed_init_method, self.local_rank, "hccl"
|
||||
)
|
||||
ensure_model_parallel_initialized(
|
||||
self.parallel_config.tensor_parallel_size,
|
||||
self.parallel_config.pipeline_parallel_size,
|
||||
self.parallel_config.prefill_context_parallel_size,
|
||||
self.parallel_config.decode_context_parallel_size)
|
||||
self.parallel_config.decode_context_parallel_size,
|
||||
)
|
||||
init_ascend_model_parallel(self.parallel_config)
|
||||
ensure_kv_transfer_initialized(self.vllm_config)
|
||||
ensure_ec_transfer_initialized(self.vllm_config)
|
||||
@@ -553,12 +531,9 @@ class NPUWorker(WorkerBase):
|
||||
profiler_config = self.vllm_config.profiler_config
|
||||
if profiler_config.profiler == "torch" and profiler_config.torch_profiler_dir:
|
||||
if envs_ascend.MSMONITOR_USE_DAEMON:
|
||||
raise RuntimeError(
|
||||
"MSMONITOR_USE_DAEMON and torch profiler cannot be both enabled at the same time."
|
||||
)
|
||||
raise RuntimeError("MSMONITOR_USE_DAEMON and torch profiler cannot be both enabled at the same time.")
|
||||
torch_profiler_trace_dir = profiler_config.torch_profiler_dir
|
||||
logger.info("Profiling enabled. Traces will be saved to: %s",
|
||||
torch_profiler_trace_dir)
|
||||
logger.info("Profiling enabled. Traces will be saved to: %s", torch_profiler_trace_dir)
|
||||
|
||||
experimental_config = torch_npu.profiler._ExperimentalConfig(
|
||||
export_type=torch_npu.profiler.ExportType.Text,
|
||||
@@ -583,8 +558,8 @@ class NPUWorker(WorkerBase):
|
||||
# The with_stack option in torch_npu.profiler introduces significant time overhead.
|
||||
with_modules=profiler_config.torch_profiler_with_stack,
|
||||
experimental_config=experimental_config,
|
||||
on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(
|
||||
torch_profiler_trace_dir))
|
||||
on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(torch_profiler_trace_dir),
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -594,5 +569,5 @@ class NPUWorker(WorkerBase):
|
||||
def get_supported_tasks(self) -> "tuple[SupportedTask, ...]":
|
||||
return self.model_runner.get_supported_tasks()
|
||||
|
||||
def take_draft_token_ids(self) -> Optional[DraftTokenIds]:
|
||||
def take_draft_token_ids(self) -> DraftTokenIds | None:
|
||||
return self.model_runner.take_draft_token_ids()
|
||||
|
||||
Reference in New Issue
Block a user