|
| 1 | +from types import SimpleNamespace |
| 2 | + |
| 3 | +import torch |
| 4 | + |
| 5 | +from lightllm.common.basemodel.basemodel import TpPartBaseModel |
| 6 | +from lightllm.common.basemodel.hidden_collector import FinalHiddenCollector |
| 7 | +from lightllm.common.basemodel.layer_weights.meta_weights import NoTpGEMMANormWeight |
| 8 | +from lightllm.models.llama.layer_infer import post_layer_infer as llama_post |
| 9 | +from lightllm.models.qwen3_5.layer_infer.post_layer_infer import Qwen35PostLayerInfer |
| 10 | +from lightllm.models.qwen3_5_mtp.layer_infer.pre_layer_infer import Qwen3_5MTPPreLayerInfer |
| 11 | + |
| 12 | + |
| 13 | +def test_qwen35_main_to_draft_two_steps(monkeypatch) -> None: |
| 14 | + def norm_weight(values): |
| 15 | + norm = NoTpGEMMANormWeight.__new__(NoTpGEMMANormWeight) |
| 16 | + norm.dim = 3 |
| 17 | + norm.data_type_ = torch.float32 |
| 18 | + norm.weight_name = "norm.weight" |
| 19 | + norm.weight = torch.empty(3) |
| 20 | + norm.load_hf_weights({"norm.weight": torch.tensor(values)}) |
| 21 | + norm._forward = norm._native_forward |
| 22 | + return norm |
| 23 | + |
| 24 | + main_norm = norm_weight([0.5, -0.25, 1.0]) |
| 25 | + draft_norm = norm_weight([-0.5, 1.0, 0.25]) |
| 26 | + enorm = norm_weight([0.0, 0.25, -0.25]) |
| 27 | + hnorm = norm_weight([0.25, -0.5, 0.5]) |
| 28 | + projection = torch.arange(1, 16, dtype=torch.float32).reshape(5, 3) / 10 |
| 29 | + fusion = torch.tensor( |
| 30 | + [[1.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0, 0.0, 0.0]] |
| 31 | + ) |
| 32 | + |
| 33 | + class LMHead: |
| 34 | + vocab_size = 5 |
| 35 | + |
| 36 | + def __call__(self, input, alloc_func): |
| 37 | + return projection @ input |
| 38 | + |
| 39 | + main_weight = SimpleNamespace(final_norm_weight_=main_norm, lm_head_weight_=LMHead()) |
| 40 | + draft_weight = SimpleNamespace( |
| 41 | + final_norm_weight_=draft_norm, |
| 42 | + lm_head_weight_=LMHead(), |
| 43 | + enorm_weight_=enorm, |
| 44 | + hnorm_weight_=hnorm, |
| 45 | + eh_proj_weight_=SimpleNamespace(mm=lambda hidden: hidden @ fusion.T), |
| 46 | + ) |
| 47 | + post = Qwen35PostLayerInfer.__new__(Qwen35PostLayerInfer) |
| 48 | + post.eps_ = 1e-6 |
| 49 | + post.tp_world_size_ = 1 |
| 50 | + post.alloc_tensor = lambda shape, dtype, **kwargs: torch.empty(shape, dtype=dtype) |
| 51 | + pre = Qwen3_5MTPPreLayerInfer.__new__(Qwen3_5MTPPreLayerInfer) |
| 52 | + pre.eps_ = post.eps_ |
| 53 | + model = TpPartBaseModel.__new__(TpPartBaseModel) |
| 54 | + model.layers_num = 0 |
| 55 | + model.post_infer = post |
| 56 | + post._tpsp_allgather = lambda input, infer_state: input |
| 57 | + |
| 58 | + def forward(hidden, weight): |
| 59 | + model.pre_post_weight = weight |
| 60 | + model.pre_infer = SimpleNamespace( |
| 61 | + token_forward=lambda *args: hidden, |
| 62 | + _tpsp_sp_split=lambda input, infer_state: input, |
| 63 | + ) |
| 64 | + return model._token_forward(state) |
| 65 | + |
| 66 | + state = SimpleNamespace( |
| 67 | + is_prefill=False, |
| 68 | + is_cuda_graph=False, |
| 69 | + input_ids=torch.ones(2, dtype=torch.long), |
| 70 | + is_draft_model=False, |
| 71 | + batch_size=2, |
| 72 | + prompt_logics=None, |
| 73 | + hidden_collector=FinalHiddenCollector(), |
| 74 | + ) |
| 75 | + monkeypatch.setattr( |
| 76 | + llama_post, |
| 77 | + "get_env_start_args", |
| 78 | + lambda: SimpleNamespace(target_vocab_topk_sampling=None, draft_vocab_topk_sampling=None), |
| 79 | + ) |
| 80 | + |
| 81 | + def reference_norm(hidden, weight): |
| 82 | + return torch.nn.functional.rms_norm(hidden, (3,), weight.weight, eps=post.eps_) |
| 83 | + |
| 84 | + hidden = torch.tensor([[1.0, 2.0, -3.0], [4.0, -2.0, 1.0]]) |
| 85 | + expected = reference_norm(hidden, main_norm) |
| 86 | + output = forward(hidden, main_weight) |
| 87 | + torch.testing.assert_close(output.mtp_collector.spec_hidden, expected) |
| 88 | + torch.testing.assert_close(output.logits, expected @ projection.T) |
| 89 | + assert state.hidden_collector.final_hidden is None |
| 90 | + |
| 91 | + state.is_draft_model = True |
| 92 | + for step in range(2): |
| 93 | + embedding = torch.tensor([[2.0, -1.0, 3.0], [-3.0, 2.0, 1.0]]) + step |
| 94 | + state.mtp_draft_input_hiddens = output.mtp_collector.spec_hidden |
| 95 | + expected_fused = ( |
| 96 | + torch.cat( |
| 97 | + (reference_norm(embedding, enorm), reference_norm(expected, hnorm)), |
| 98 | + dim=-1, |
| 99 | + ) |
| 100 | + @ fusion.T |
| 101 | + ) |
| 102 | + fused = pre._mtp_fuse(embedding, state, draft_weight) |
| 103 | + torch.testing.assert_close(fused, expected_fused) |
| 104 | + expected = reference_norm(expected_fused, draft_norm) |
| 105 | + output = forward(fused, draft_weight) |
| 106 | + torch.testing.assert_close(output.mtp_collector.spec_hidden, expected) |
| 107 | + torch.testing.assert_close(output.logits, expected @ projection.T) |
| 108 | + assert state.hidden_collector.final_hidden is None |
| 109 | + assert not torch.allclose(expected, reference_norm(expected_fused, main_norm)) |
0 commit comments