"vscode:/vscode.git/clone" did not exist on "ed344f41164f6f3b6c33fcbfda61f94b1a8ac790"
test_compilation.py 2.8 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
6
7
8
9
import glob
import os
import tempfile

import depyf

10

11
12
13
14
15
16
17
18
19
20
21
def test_tpu_compilation():
    temp_dir = tempfile.mkdtemp()
    with depyf.prepare_debug(temp_dir):
        from vllm import LLM, SamplingParams

        prompts = [
            "A robot may not injure a human being",
            "It is only with the heart that one can see rightly;",
            "The greatest glory in living lies not in never falling,",
        ]
        answers = [
22
23
24
            " or, through inaction",
            " what is essential ",
            " but in rising ",
25
        ]
26

27
        # Currently, top-p sampling is disabled. `top_p` should be 1.0.
28
        N = 1
29
        sampling_params = SamplingParams(temperature=0.7, top_p=1.0, n=N, max_tokens=16)
30

31
32
33
34
35
36
37
        llm = LLM(
            model="Qwen/Qwen2-1.5B-Instruct",
            max_num_batched_tokens=256,
            max_model_len=256,
            max_num_seqs=32,
            enforce_eager=False,
        )
38

39
40
41
42
43
44
45
46
        outputs = llm.generate(prompts, sampling_params)
        for output, answer in zip(outputs, answers):
            prompt = output.prompt
            generated_text = output.outputs[0].text
            print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
            assert generated_text.startswith(answer)

    compiled_codes = sorted(
47
48
        glob.glob(os.path.join(temp_dir, "__transformed_code*for_forward.py"))
    )
49
50
51
52

    for i, compiled_code in enumerate(compiled_codes):
        print("{} file: {}".format(i + 1, compiled_code))

53
54
55
    # We should only trigger Dynamo compilation 2 times:
    # 1. Forward pass without kv_caches
    # 2. Forward pass with kv_caches
56
    # Check we have 2 compiled codes
57
    assert len(compiled_codes) == 2
58
59
60
61

    kv_cache_prefix = "kv_cache"
    attn_prefix = "ragged_paged_attention"

62
63
64
65
66
    def extract_compiled_index(s):
        parts = s.replace(".", "_").split("_")
        numbers = [int(part) for part in parts if part.isdigit()]
        return numbers[0]

67
68
    # Check all the compilations are as expected. The dump files include the
    # captured graph for the forward function of the nn.Module.
69
70
71
72
    compiled_fns = sorted(
        glob.glob(os.path.join(temp_dir, "__compiled_fn*Forward_graph*.py")),
        key=lambda s: extract_compiled_index(s),
    )
73
74
75
76

    for i, compiled_fn in enumerate(compiled_fns):
        print("{} file: {}".format(i + 1, compiled_fn))

77
    # The first compilation should not have any kv_caches
78
79
80
81
    with open(compiled_fns[0]) as f:
        content = f.read()
        assert kv_cache_prefix not in content

82
    # The second compilation should have kv_caches and the
83
    # ragged_paged_attention
84
    with open(compiled_fns[1]) as f:
85
        content = f.read()
86
        assert kv_cache_prefix in content and attn_prefix in content