|
17 | 17 | # Adapted from vllm-project/vllm/vllm/worker/gpu_model_runner.py |
18 | 18 | # |
19 | 19 |
|
| 20 | +from typing import Optional |
| 21 | + |
20 | 22 | import torch |
21 | 23 | from vllm.config import VllmConfig |
22 | 24 |
|
| 25 | +import vllm_ascend.envs as envs_ascend |
23 | 26 | from vllm_ascend.worker.model_runner_v1 import NPUModelRunner |
24 | 27 |
|
25 | 28 |
|
26 | 29 | class NPUTorchairModelRunner(NPUModelRunner): |
27 | 30 |
|
28 | 31 | def __init__(self, vllm_config: VllmConfig, device: torch.device): |
29 | 32 | super().__init__(vllm_config, device) |
| 33 | + |
| 34 | + def _get_forward_metadata_across_dp_and_pad( |
| 35 | + self, num_tokens: int, with_prefill: bool, enable_dbo: bool |
| 36 | + ) -> tuple[int, Optional[torch.Tensor], bool, bool]: |
| 37 | + if self.dp_size == 1: |
| 38 | + return num_tokens, None, with_prefill, enable_dbo |
| 39 | + |
| 40 | + if self.is_kv_producer and not envs_ascend.VLLM_ASCEND_ENABLE_CHUNK_MC2: |
| 41 | + num_tokens_across_dp = torch.tensor([num_tokens] * self.dp_size, |
| 42 | + device="cpu", |
| 43 | + dtype=torch.int32) |
| 44 | + return num_tokens, num_tokens_across_dp, True, enable_dbo |
| 45 | + |
| 46 | + if self.is_kv_consumer and len(self.torchair_graph_batch_sizes |
| 47 | + ) == 1 and not self.in_profile_run: |
| 48 | + max_num_decode_tokens = self.torchair_graph_batch_sizes[0] |
| 49 | + num_tokens_across_dp = torch.tensor([max_num_decode_tokens] * |
| 50 | + self.dp_size, |
| 51 | + device="cpu", |
| 52 | + dtype=torch.int32) |
| 53 | + return max_num_decode_tokens, num_tokens_across_dp, False, enable_dbo |
| 54 | + |
| 55 | + num_tokens_across_dp, with_prefill, enable_dbo = self._get_forward_metadata_across_dp( |
| 56 | + num_tokens, with_prefill, enable_dbo) |
| 57 | + |
| 58 | + if not with_prefill: |
| 59 | + max_num_token = num_tokens_across_dp.max().item() |
| 60 | + maybe_padded_num_tokens = self.select_torchair_padded_batch_size( |
| 61 | + max_num_token) |
| 62 | + num_tokens_across_dp = torch.full((self.dp_size, ), |
| 63 | + maybe_padded_num_tokens, |
| 64 | + dtype=torch.int32, |
| 65 | + device="cpu") |
| 66 | + else: |
| 67 | + maybe_padded_num_tokens = num_tokens |
| 68 | + |
| 69 | + return maybe_padded_num_tokens, num_tokens_across_dp, with_prefill, enable_dbo |
0 commit comments