utils.py 2.4 KB
Newer Older
Jee Jee Li's avatar
Jee Jee Li committed
1
2
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

Jee Jee Li's avatar
Jee Jee Li committed
4
from dataclasses import dataclass
5
from enum import Enum
Jee Jee Li's avatar
Jee Jee Li committed
6
7
8
9
10

import torch
import torch.nn as nn


11
12
13
14
15
16
class LoRAMappingType(Enum):
    LANGUAGE = 1
    TOWER = 2
    CONNECTOR = 3


Jee Jee Li's avatar
Jee Jee Li committed
17
@dataclass
18
19
20
class LoRAMapping:
    index_mapping: tuple[int, ...]
    prompt_mapping: tuple[int, ...]
Jee Jee Li's avatar
Jee Jee Li committed
21
    is_prefill: bool = False
22
    type: LoRAMappingType = LoRAMappingType.LANGUAGE
Jee Jee Li's avatar
Jee Jee Li committed
23

24
25
26
27
    def __post_init__(self):
        self.index_mapping = tuple(self.index_mapping)
        self.prompt_mapping = tuple(self.prompt_mapping)

Jee Jee Li's avatar
Jee Jee Li committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

def _get_lora_device(base_layer: nn.Module) -> torch.device:
    # code borrowed from https://github.com/fmmoret/vllm/blob/fm-support-lora-on-quantized-models/vllm/lora/layers.py#L34
    """Returns the device for where to place the LoRA tensors."""
    # unquantizedLinear
    if hasattr(base_layer, "weight"):
        return base_layer.weight.device
    # Compressed Tensor
    elif hasattr(base_layer, "weight_packed"):
        return base_layer.weight_packed.device
    # GPTQ/AWQ
    elif hasattr(base_layer, "qweight"):
        return base_layer.qweight.device
    # HQQ marlin
    elif hasattr(base_layer, "W_q"):
        return base_layer.W_q.device
44
45
46
47
48
49
50
51
52
    # MoE layer
    elif hasattr(base_layer, "w2_weight"):
        return base_layer.w2_weight.device
    # MoE Compressed Tensor
    elif hasattr(base_layer, "w2_weight_packed"):
        return base_layer.w2_weight_packed.device
    # MoE GPTQ/AWQ/GGUF
    elif hasattr(base_layer, "w2_qweight"):
        return base_layer.w2_qweight.device
Jee Jee Li's avatar
Jee Jee Li committed
53
54
55
56
57
58
59
60
61
62
63
64
    else:
        raise ValueError(f"Unsupported base layer: {base_layer}")


def _not_fully_sharded_can_replace(can_replace):
    """
    decorator which adds the condition of not using fully sharded loras
    intended to wrap can_replace_layer()
    """

    def dec(*args, **kwargs):
        decorate = kwargs.pop("decorate") if "decorate" in kwargs else True
65
        condition = not kwargs["lora_config"].fully_sharded_loras if decorate else True
Jee Jee Li's avatar
Jee Jee Li committed
66
67
68
69
70
71
72
73
74
75
76
77
        return can_replace(*args, **kwargs) and condition

    return dec


def _fully_sharded_can_replace(can_replace):
    """
    decorator which adds the condition of fully sharded loras
    intended to wrap can_replace_layer()
    """

    def dec(*args, **kwargs):
78
79
80
        return (
            can_replace(*args, **kwargs) and kwargs["lora_config"].fully_sharded_loras
        )
Jee Jee Li's avatar
Jee Jee Li committed
81
82

    return dec