backend.py 3.79 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
import weakref
5
from collections.abc import Callable, Sequence
6
7
from copy import deepcopy

8
from torch import fx
9
from torch._ops import OpOverload
10

11
from vllm.compilation.fx_utils import find_op_nodes
12
from vllm.compilation.inductor_pass import InductorPass
13
14
15
16
17
18
19
20
21
22
23
24
from vllm.compilation.pass_manager import with_pattern_match_debug
from vllm.compilation.vllm_inductor_pass import VllmInductorPass
from vllm.config import VllmConfig, get_current_vllm_config


class LazyInitPass(InductorPass):
    """
    If there's a pass that we want to initialize lazily in a test,
    we can wrap it in LazyInitPass, which will initialize the pass when invoked
    and then immediately invoke it.
    """

25
    def __init__(self, pass_cls: type[VllmInductorPass], vllm_config: VllmConfig):
26
27
28
29
30
31
        self.pass_cls = pass_cls
        self.vllm_config = weakref.proxy(vllm_config)  # avoid cycle

    def __call__(self, graph: fx.Graph) -> None:
        self.pass_ = self.pass_cls(self.vllm_config)
        self.pass_(graph)
32
33
34
35
36
37
38


class TestBackend:
    """
    This class provides a simple Inductor backend that can be used for testing.
    It takes a list of custom passes and runs them after Inductor's passes.
    It also saves the graph before and after the custom passes for inspection.
39
40
41
42

    Inductor config can be modified directly by editing the inductor_config
    property. This can be helpful for adding passes like the
    'pre_grad_custom_pass' and the 'post_grad_custom_pre_pass'.
Michael Goin's avatar
Michael Goin committed
43
    Inductor config is default-initialized from VllmConfig.CompilationConfig.
44
45
    """

46
    def __init__(self, *passes: InductorPass | Callable[[fx.Graph], None]):
47
        self.custom_passes = list(passes)
Michael Goin's avatar
Michael Goin committed
48
49
        compile_config = get_current_vllm_config().compilation_config
        self.inductor_config = compile_config.inductor_compile_config
50
51
        self.inductor_config["force_disable_caches"] = True
        self.inductor_config["post_grad_custom_post_pass"] = self.post_pass
52

53
    def __call__(self, graph: fx.GraphModule, example_inputs):
54
        self.graph_pre_compile = deepcopy(graph)
55
        from torch._inductor.compile_fx import compile_fx
56
57

        return compile_fx(graph, example_inputs, config_patches=self.inductor_config)
58

59
    @with_pattern_match_debug
60
    def post_pass(self, graph: fx.Graph):
61
        self.graph_pre_pass = deepcopy(graph)
62
63

        VllmInductorPass.dump_prefix = 0
64
65
        for pass_ in self.custom_passes:
            pass_(graph)
66
67
68
            VllmInductorPass.dump_prefix += 1

        VllmInductorPass.dump_prefix = None
69
70
71
72

        self.graph_post_pass = deepcopy(graph)
        # assign by reference, will reflect the final state of the graph
        self.final_graph = graph
73

74
    def check_before_ops(self, ops: Sequence[OpOverload], fully_replaced=True):
75
        for op in ops:
76
77
78
79
80
            num_pre = len(list(find_op_nodes(op, self.graph_pre_pass)))
            num_post = len(list(find_op_nodes(op, self.graph_post_pass)))
            assert num_pre > 0, f"Op {op.name()} not found in pre-pass graph"
            assert num_pre > num_post, f"All nodes remain for op {op.name()}"
            if fully_replaced:
81
                assert num_post == 0, f"Unexpected op {op.name()} in post-pass graph"
82

83
    def check_after_ops(self, ops: Sequence[OpOverload]):
84
        for op in ops:
85
86
87
            num_pre = len(list(find_op_nodes(op, self.graph_pre_pass)))
            num_post = len(list(find_op_nodes(op, self.graph_post_pass)))
            assert num_pre == 0, f"Unexpected op {op.name()} in pre-pass graph"
88
89
90
91
92
            assert num_post > 0, f"Op {op.name()} not found in post-pass graph"

    def op_count(self, op: OpOverload, before=False) -> int:
        graph = self.graph_pre_pass if before else self.graph_post_pass
        return len(list(find_op_nodes(op, graph)))