-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathtest_dspark_speculator.py
More file actions
100 lines (78 loc) · 3.78 KB
/
Copy pathtest_dspark_speculator.py
File metadata and controls
100 lines (78 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#
# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved.
# Copyright 2023 The vLLM team.
#
# 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.
# This file is a part of the vllm-ascend project.
#
"""Unit tests for ``AscendDSparkSpeculator.load_draft_model`` fc rotation."""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
import torch
from vllm.v1.worker.gpu.spec_decode.dspark.speculator import DSparkSpeculator
from vllm_ascend.worker.v2.spec_decode.dspark.speculator import (
AscendDSparkSpeculator,
)
_HIDDEN = 8
_FC_IN = 5 * _HIDDEN # concatenated aux hidden states
# Patch where load_draft_model looks it up (the speculator module binding).
_ROT_MATRIX = "vllm_ascend.worker.v2.spec_decode.dspark.speculator.get_rotation_matrix"
def _spec(vllm_config: SimpleNamespace) -> AscendDSparkSpeculator:
"""Bypass the heavy ``__init__``; ``load_draft_model`` only reads
``self.vllm_config`` and the patched parent call."""
spec = AscendDSparkSpeculator.__new__(AscendDSparkSpeculator)
spec.vllm_config = vllm_config
return spec
def _fake_draft() -> SimpleNamespace:
fc = torch.nn.Linear(_FC_IN, _HIDDEN, bias=False)
with torch.no_grad():
fc.weight.copy_(torch.randn_like(fc.weight))
return SimpleNamespace(model=SimpleNamespace(fc=fc))
def _quarot_config() -> SimpleNamespace:
quarot = {"rotation_map": {"global_rotation": "x.safetensors"}}
return SimpleNamespace(
quant_config=SimpleNamespace(quant_description={"optional": {"quarot": quarot}}),
model_config=SimpleNamespace(model="/fake"),
)
def _bf16_config() -> SimpleNamespace:
return SimpleNamespace(quant_config=None, model_config=SimpleNamespace())
def _no_call(*args, **kwargs):
raise AssertionError("get_rotation_matrix must not be called without a rotation path")
class TestLoadDraftModel:
"""``load_draft_model`` rotates fc for a QuaRot target and is a no-op otherwise."""
@pytest.fixture
def captured(self, monkeypatch):
"""Stub the heavy parent ``load_draft_model`` to return a fake draft and
snapshot its fc weight before the override mutates it in place."""
out: dict = {}
def _load(self, target_model, target_attn_layer_names):
draft = _fake_draft()
out["before"] = draft.model.fc.weight.data.clone()
out["draft"] = draft
return draft
monkeypatch.setattr(DSparkSpeculator, "load_draft_model", _load)
return out
def test_rotates_fc_for_quarot_target(self, captured, monkeypatch):
# R = 2*I -> W @ R == 2*W, an expectation independent of process_weight.
monkeypatch.setattr(_ROT_MATRIX, lambda path: torch.eye(_HIDDEN) * 2.0)
draft = _spec(_quarot_config()).load_draft_model(MagicMock(), set())
before = captured["before"]
assert draft is captured["draft"]
assert torch.allclose(draft.model.fc.weight.data, 2.0 * before, atol=1e-6)
assert not torch.allclose(draft.model.fc.weight.data, before)
def test_noop_for_bf16_target(self, captured, monkeypatch):
monkeypatch.setattr(_ROT_MATRIX, _no_call)
draft = _spec(_bf16_config()).load_draft_model(MagicMock(), set())
assert torch.equal(draft.model.fc.weight.data, captured["before"])