"docs/vscode:/vscode.git/clone" did not exist on "0aaccbbfecc5367f4db2959be93fcf35cf30d1af"
test_eagle_utils.py 2.33 KB
Newer Older
1
import pytest
2
3
import torch
import torch.nn.functional as F
4
from sgl_kernel import verify_tree_greedy
5
6


7
def test_verify_tree_greedy():
8
9
10
11
12
    candidates = torch.tensor(
        [
            [0, 1, 2, 3, 4, 5],
            [7, 8, 9, 10, 11, 12],
        ],
13
        dtype=torch.int64,
14
15
16
17
18
19
20
        device="cuda",
    )
    retrive_index = torch.tensor(
        [
            [0, 1, 2, 3, 4, 5],
            [6, 7, 8, 9, 10, 11],
        ],
21
        dtype=torch.int64,
22
23
24
25
26
27
28
        device="cuda",
    )
    retrive_next_token = torch.tensor(
        [
            [1, 2, -1, 4, 5, -1],
            [4, 2, 3, -1, 5, -1],
        ],
29
        dtype=torch.int64,
30
31
32
33
34
35
36
        device="cuda",
    )
    retrive_next_sibling = torch.tensor(
        [
            [-1, 3, -1, -1, -1, -1],
            [-1, -1, -1, -1, 1, -1],
        ],
37
        dtype=torch.int64,
38
39
40
        device="cuda",
    )

41
    target_logits = torch.full((2, 6, 20), 1, dtype=torch.float32, device="cuda")
42
43
44
45
46
47
48
49
50
51
    target_logits[0, 0, 3] = 10
    target_logits[0, 3, 4] = 10
    target_logits[0, 4, 5] = 10
    target_logits[1, 0, 11] = 10
    target_logits[1, 4, 12] = 10
    for i in range(target_logits.shape[0]):
        for j in range(target_logits.shape[1]):
            if torch.max(target_logits[i][j]) < 10:
                target_logits[i][j][18] = 10

52
    target_predict = torch.argmax(target_logits, dim=-1)
53
54
55
56
57
58
59
60
61
62
63
64
65
    predict_shape = (12,)

    bs = candidates.shape[0]
    num_spec_step = 4

    predicts = torch.full(
        predict_shape, -1, dtype=torch.int32, device="cuda"
    )  # mutable
    accept_index = torch.full(
        (bs, num_spec_step), -1, dtype=torch.int32, device="cuda"
    )  # mutable
    accept_token_num = torch.full((bs,), 0, dtype=torch.int32, device="cuda")  # mutable

66
    verify_tree_greedy(
67
68
69
70
71
72
73
        predicts=predicts,
        accept_index=accept_index,
        accept_token_num=accept_token_num,
        candidates=candidates,
        retrive_index=retrive_index,
        retrive_next_token=retrive_next_token,
        retrive_next_sibling=retrive_next_sibling,
74
        target_predict=target_predict,
75
76
    )

77
    # Check the expected output.
78
79
80
81
82
83
    assert predicts.tolist() == [3, -1, -1, 4, 5, 18, 11, -1, -1, -1, 12, 18]
    assert accept_index.tolist() == [
        [0, 3, 4, 5],
        [6, 10, 11, -1],
    ]
    assert accept_token_num.tolist() == [3, 2]
84
85
86
87


if __name__ == "__main__":
    pytest.main([__file__])