|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 3 | +# Copyright 2023 The vLLM team. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# This file is a part of the vllm-ascend project. |
| 17 | +# Adapted from vllm-project/vllm/vllm/worker/gpu_worker.py |
| 18 | +# |
| 19 | + |
| 20 | +import torch |
| 21 | +import torch_npu |
| 22 | +from vllm.logger import logger |
| 23 | + |
| 24 | +import vllm_ascend.envs as envs_ascend |
| 25 | +from vllm_ascend.ascend_config import get_ascend_config |
| 26 | +from vllm_ascend.device_allocator.camem import CaMemAllocator |
| 27 | +from vllm_ascend.platform import NPUPlatform |
| 28 | +from vllm_ascend.torchair.utils import (check_kv_cache_bytes_cache_exist, |
| 29 | + check_torchair_cache_exist, |
| 30 | + delete_torchair_cache_file, |
| 31 | + read_kv_cache_bytes_from_file, |
| 32 | + ) |
| 33 | +from vllm_ascend.torchair.model_runner_torchair import NPUTorchairModelRunner |
| 34 | +from vllm_ascend.worker.worker_v1 import NPUWorker |
| 35 | + |
| 36 | +class NPUTorchairWorker(NPUWorker): |
| 37 | + def init_device(self): |
| 38 | + device = torch.device(f"npu:{self.local_rank}") |
| 39 | + NPUPlatform.set_device(device) |
| 40 | + NPUPlatform.empty_cache() |
| 41 | + self.init_npu_memory = NPUPlatform.mem_get_info()[0] |
| 42 | + |
| 43 | + # Initialize the distributed environment. |
| 44 | + self._init_worker_distributed_environment() |
| 45 | + # Set random seed. |
| 46 | + NPUPlatform.seed_everything(self.model_config.seed) |
| 47 | + |
| 48 | + # Init ModelRunner here, so that we have access to self.device. |
| 49 | + self.model_runner = NPUTorchairModelRunner(self.vllm_config, device) |
| 50 | + |
| 51 | + def determine_available_memory(self) -> int: |
| 52 | + # Profile the memory usage of the model and get the maximum number of |
| 53 | + # cache blocks that can be allocated with the remaining free memory. |
| 54 | + NPUPlatform.clear_npu_memory() |
| 55 | + |
| 56 | + # Execute a forward pass with dummy inputs to profile the memory usage |
| 57 | + # of the model. |
| 58 | + _, total_npu_memory = NPUPlatform.mem_get_info() |
| 59 | + self.model_runner.profile_run() |
| 60 | + |
| 61 | + # Calculate the number of blocks that can be allocated with the |
| 62 | + # profiled peak memory. |
| 63 | + free_npu_memory, _ = NPUPlatform.mem_get_info() |
| 64 | + # NOTE(woosuk): Here we assume that the other processes using the same |
| 65 | + # GPU did not change their memory usage during the profiling. |
| 66 | + assert self.init_npu_memory > free_npu_memory, ( |
| 67 | + "Error in memory profiling. " |
| 68 | + f"Initial free memory {self.init_npu_memory}, current free memory" |
| 69 | + f" {free_npu_memory}. This happens when the NPU memory was " |
| 70 | + "not properly cleaned up before initializing the vLLM instance.") |
| 71 | + |
| 72 | + # Get the peak memory allocation recorded by torch |
| 73 | + peak_memory = torch_npu.npu.memory_stats()["allocated_bytes.all.peak"] |
| 74 | + # TODO: don`t need impl this func after empty_cache in |
| 75 | + # Worker.determine_num_available_blocks() unified` |
| 76 | + NPUPlatform.empty_cache() |
| 77 | + torch_allocated_bytes = torch_npu.npu.memory_stats( |
| 78 | + )["allocated_bytes.all.current"] |
| 79 | + total_allocated_bytes = torch_npu.npu.mem_get_info( |
| 80 | + )[1] - torch_npu.npu.mem_get_info()[0] |
| 81 | + non_torch_allocations = total_allocated_bytes - torch_allocated_bytes |
| 82 | + if non_torch_allocations > 0: |
| 83 | + peak_memory += non_torch_allocations |
| 84 | + available_kv_cache_memory = int( |
| 85 | + total_npu_memory * self.cache_config.gpu_memory_utilization - |
| 86 | + peak_memory) |
| 87 | + available_kv_cache_memory = int(max(available_kv_cache_memory, 0)) |
| 88 | + logger.info( |
| 89 | + f"Available memory: {available_kv_cache_memory}, total memory: {total_npu_memory}" |
| 90 | + ) |
| 91 | + if get_ascend_config().torchair_graph_config.enabled: |
| 92 | + if check_torchair_cache_exist( |
| 93 | + ) and check_kv_cache_bytes_cache_exist(): |
| 94 | + old_kv_cache_bytes = read_kv_cache_bytes_from_file( |
| 95 | + torch.distributed.get_rank()) |
| 96 | + if 0 < old_kv_cache_bytes <= available_kv_cache_memory: |
| 97 | + logger.info( |
| 98 | + f"Use cached torchair kv_cache_bytes: {old_kv_cache_bytes}" |
| 99 | + ) |
| 100 | + self.model_runner.new_kv_cache_bytes = old_kv_cache_bytes |
| 101 | + return old_kv_cache_bytes |
| 102 | + else: |
| 103 | + logger.info( |
| 104 | + "Cached torchair kv_cache_bytes is too big, invalidate old torchair_cache" |
| 105 | + ) |
| 106 | + delete_torchair_cache_file() |
| 107 | + bytes_floating_tolerance = 1024 * 1024 * envs_ascend.VLLM_ASCEND_KV_CACHE_MEGABYTES_FLOATING_TOLERANCE |
| 108 | + available_kv_cache_memory -= bytes_floating_tolerance |
| 109 | + logger.info(f"Use new kv_cache_bytes: {available_kv_cache_memory}") |
| 110 | + self.model_runner.new_kv_cache_bytes = available_kv_cache_memory |
| 111 | + |
| 112 | + return available_kv_cache_memory |
| 113 | + |
| 114 | + def execute_dummy_batch(self) -> None: |
| 115 | + runner = self.model_runner |
| 116 | + max_num_tokens = 1 |
| 117 | + with_prefill = False |
| 118 | + if runner.dp_size > 1: |
| 119 | + max_num_tokens, with_prefill = runner._get_forward_metadata_across_dp( |
| 120 | + max_num_tokens, with_prefill) |
| 121 | + if runner.torchair_graph_enabled and not with_prefill: |
| 122 | + max_num_tokens = runner.select_torchair_padded_batch_size( |
| 123 | + max_num_tokens) |
| 124 | + runner._dummy_run(max_num_tokens, |
| 125 | + is_compile=False, |
| 126 | + with_prefill=with_prefill) |
0 commit comments