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

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

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
Michael Goin's avatar
Michael Goin committed
13
from vllm.config import get_current_vllm_config
14
15
16
17
18
19
20


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.
21
22
23
24

    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
25
    Inductor config is default-initialized from VllmConfig.CompilationConfig.
26
27
    """

28
29
30
    def __init__(self, *passes: Union[InductorPass, Callable[[fx.Graph],
                                                             None]]):
        self.custom_passes = list(passes)
Michael Goin's avatar
Michael Goin committed
31
32
        compile_config = get_current_vllm_config().compilation_config
        self.inductor_config = compile_config.inductor_compile_config
33
34
        self.inductor_config['force_disable_caches'] = True
        self.inductor_config['post_grad_custom_post_pass'] = self.post_pass
35

36
    def __call__(self, graph: fx.GraphModule, example_inputs):
37
        self.graph_pre_compile = deepcopy(graph)
38
39
40
        from torch._inductor.compile_fx import compile_fx
        return compile_fx(graph,
                          example_inputs,
41
                          config_patches=self.inductor_config)
42

43
    def post_pass(self, graph: fx.Graph):
44
45
46
47
48
49
50
        self.graph_pre_pass = deepcopy(graph)
        for pass_ in self.custom_passes:
            pass_(graph)

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

52
    def check_before_ops(self, ops: Sequence[OpOverload], fully_replaced=True):
53
        for op in ops:
54
55
56
57
58
59
60
            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:
                assert num_post == 0, \
                    f"Unexpected op {op.name()} in post-pass graph"
61

62
    def check_after_ops(self, ops: Sequence[OpOverload]):
63
        for op in ops:
64
65
66
67
            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"
            assert num_post > 0, f"Op {op.name()} not found in post-pass graph"