Skip to content

Commit 1c24929

Browse files
fixup mocker
Signed-off-by: CodeNine-CJ <[email protected]>
1 parent fedd8cc commit 1c24929

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

tests/ut/torchair/test_torchair_model_runner.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from unittest.mock import MagicMock, Mock, patch
1+
from unittest.mock import MagicMock, Mock
22

33
import pytest
44
import torch
55
from pytest_mock import MockerFixture
6-
from vllm.config import VllmConfig
6+
from vllm.config import CacheConfig, VllmConfig
77

88
from tests.ut.base import PytestBase
99
from vllm_ascend.torchair.torchair_model_runner import NPUTorchairModelRunner
@@ -17,6 +17,11 @@ def setup_npu_torchair_model_runner(self, mocker: MockerFixture):
1717
vllm_config.model_config = MagicMock()
1818
vllm_config.model_config.hf_config = MagicMock()
1919
vllm_config.model_config.hf_config.index_topk = 2
20+
cache_config = CacheConfig(block_size=16)
21+
vllm_config.cache_config = cache_config
22+
speculative_config = MagicMock()
23+
speculative_config.num_speculative_tokens = 4
24+
vllm_config.speculative_config = speculative_config
2025

2126
device = torch.device("npu:0")
2227

@@ -29,11 +34,11 @@ def setup_npu_torchair_model_runner(self, mocker: MockerFixture):
2934
ascend_config.torchair_graph_config.graph_batch_sizes = [1, 2, 4]
3035
ascend_config.torchair_graph_config.graph_batch_sizes_init = True
3136

32-
mocker.patch(
33-
"vllm_ascend.worker.model_runner_v1.NPUModelRunner.__init__",
34-
return_value=None)
37+
# mocker.patch(
38+
# "vllm_ascend.worker.model_runner_v1.NPUModelRunner.__init__",
39+
# return_value=None)
3540

36-
mocker.patch("vllm_ascend.get_ascend_config",
41+
mocker.patch("vllm_ascend.utils.get_ascend_config",
3742
return_value=ascend_config)
3843
mocker.patch("vllm_ascend.torchair.utils.register_torchair_model")
3944
mocker.patch("vllm_ascend.torchair.utils.torchair_ops_patch")
@@ -45,17 +50,16 @@ def setup_npu_torchair_model_runner(self, mocker: MockerFixture):
4550
mock_attn_builder = Mock()
4651
mock_attn_backend = Mock()
4752
mock_attn_backend.get_builder_cls.return_value = lambda *args, **kwargs: mock_attn_builder
48-
with patch.object(NPUTorchairModelRunner, 'attn_backend',
49-
mock_attn_backend):
50-
with patch.object(NPUTorchairModelRunner, 'speculative_config',
51-
MagicMock()):
52-
NPUTorchairModelRunner.decode_token_per_req = 1
53-
NPUTorchairModelRunner.max_num_tokens = 10
5453

55-
runner = NPUTorchairModelRunner(vllm_config, device)
56-
runner.vllm_config = vllm_config
57-
runner.device = device
58-
runner.attn_backend = mock_attn_backend
54+
NPUTorchairModelRunner.decode_token_per_req = 1
55+
NPUTorchairModelRunner.max_num_tokens = 10
56+
57+
runner = NPUTorchairModelRunner(vllm_config, device)
58+
runner.vllm_config = vllm_config
59+
runner.device = device
60+
runner.attn_backend = mock_attn_backend
61+
runner.ascend_config = ascend_config
62+
runner.model_config = vllm_config.model_config
5963

6064
return runner
6165

tests/ut/torchair/test_torchair_mtp_proposer.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import pytest
44
import torch
5-
from vllm.config import VllmConfig
5+
from pytest_mock import MockerFixture
6+
from vllm.config import CacheConfig, VllmConfig
67

78
from tests.ut.base import PytestBase
89
from vllm_ascend.torchair.torchair_mtp_proposer import TorchairMtpProposer
@@ -15,25 +16,48 @@ class TestTorchairMtpProposer(PytestBase):
1516
def setup_torchair_mtp_proposer(self, mocker: MockerFixture):
1617
vllm_config = MagicMock(spec=VllmConfig)
1718
vllm_config.device_config = MagicMock()
18-
vllm_config.device_config.device = torch.device("npu:0")
19+
vllm_config.device_config.device = torch.device("cpu")
1920
vllm_config.speculative_config = MagicMock()
2021
vllm_config.speculative_config.draft_model_config = MagicMock()
2122
vllm_config.speculative_config.draft_model_config.dtype = torch.float16
23+
# vllm_config.speculative_config.draft_model_config.get_hidden_size = lambda: 4096
24+
vllm_config.speculative_config.method = "deepseek_mtp"
25+
vllm_config.speculative_config.num_speculative_tokens = 5
26+
27+
# vllm_config.model_config = MagicMock(
28+
# dtype=torch.float16,
29+
# max_model_len=2048,
30+
# uses_mrope=False,
31+
# hf_config=MagicMock(index_topk=2)
32+
# )
2233
vllm_config.load_config = MagicMock()
23-
24-
device = torch.device("npu:0")
34+
cache_config = CacheConfig(block_size=16)
35+
vllm_config.cache_config = cache_config
36+
vllm_config.scheduler_config = MagicMock(max_num_batched_tokens=1024,
37+
max_num_seqs=64)
38+
# vllm_config.compilation_config = MagicMock()
39+
# vllm_config.compilation_config.cudagraph_mode = None
40+
41+
device = torch.device("cpu")
2542
runner = MagicMock()
43+
runner.pcp_size = 1
44+
runner.dcp_size = 1
45+
runner.pcp_rank = 0
46+
runner.max_num_tokens = 1024
47+
runner.max_num_reqs = 10
48+
runner._use_aclgraph.return_value = True
2649

27-
mocker.patch("vllm_ascend.torchair_mtp_proposer.__init__",
28-
return_value=None)
50+
mocker.patch(
51+
"vllm_ascend.torchair.torchair_mtp_proposer.MtpProposer.__init__",
52+
return_value=None)
2953

3054
if vllm_version_is("0.11.0"):
3155
mock_set_default_dtype = mocker.patch(
3256
'vllm.model_executor.model_loader.utils.set_default_torch_dtype'
3357
)
3458
else:
3559
mock_set_default_dtype = mocker.patch(
36-
'vllm.utls.torch_utils.set_default_torch_dtype')
60+
'vllm.utils.torch_utils.set_default_torch_dtype')
3761
mock_set_default_dtype.return_value.__enter__.return_value = None
3862

3963
mock_model_loader = MagicMock()
@@ -60,17 +84,15 @@ def setup_torchair_mtp_proposer(self, mocker: MockerFixture):
6084
proposer.vllm_config = vllm_config
6185
proposer.device = device
6286
proposer.runner = runner
87+
proposer.speculative_config = vllm_config.speculative_config
88+
proposer.draft_model_config = vllm_config.speculative_config.draft_model_config
89+
proposer.method = vllm_config.speculative_config.method
6390

6491
return proposer, mock_model_loader, mock_torchair_deepseek_mtp
6592

6693
def test_init(self, setup_torchair_mtp_proposer):
6794
proposer, _, _, = setup_torchair_mtp_proposer
68-
69-
assert isinstance(proposer, setup_torchair_mtp_proposer)
70-
assert proposer.torchair_compiled_model is None
71-
Mock.assert_called_once_with(proposer.__class__.__bases__[0],
72-
proposer.vllm_config, proposer.device,
73-
proposer.runner)
95+
assert isinstance(proposer, TorchairMtpProposer)
7496

7597
def test_load_model(self, setup_torchair_mtp_proposer,
7698
mocker: MockerFixture):

tests/ut/torchair/test_torchair_worker.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ def test_init_device(self, mock_platform, mock_init_dist_env):
5454
mock_platform.mem_get_info.return_value = (1000, 2000)
5555

5656
with patch.object(NPUWorker, "__init__", lambda x, **kwargs: None):
57-
worker = NPUWorker
57+
worker = NPUWorker()
5858
worker.local_rank = 1
5959
worker.model_config = MagicMock()
6060
worker.model_config.seed = 42
61+
worker.vllm_config = MagicMock()
6162

6263
result = worker._init_device()
6364

@@ -85,10 +86,11 @@ def test_init_device_torchair_worker(self, mock_platform,
8586

8687
with patch.object(NPUTorchairWorker, "__init__",
8788
lambda x, **kwargs: None):
88-
worker = NPUTorchairWorker
89+
worker = NPUTorchairWorker()
8990
worker.local_rank = 1
9091
worker.model_config = MagicMock()
9192
worker.model_config.seed = 42
93+
worker.vllm_config = MagicMock()
9294

9395
result = worker._init_device()
9496

0 commit comments

Comments
 (0)