File size: 10,391 Bytes
4ca17e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import bz2
import torch
import base64
import ctypes
from transformers.utils import logging
from typing import List

logger = logging.get_logger(__name__)

try:
    from cpm_kernels.kernels.base import LazyKernelCModule, KernelFunction, round_up

    class Kernel:
        def __init__(self, code: bytes, function_names: List[str]):
            self.code = code
            self._function_names = function_names
            self._cmodule = LazyKernelCModule(self.code)

            for name in self._function_names:
                setattr(self, name, KernelFunction(self._cmodule, name))

    quantization_code = "QlpoOTFBWSZTWapgbn4ALTZ/////////9f/n9+/r/v//3/Tt7cDwfe5sdXXdZNR/9++P4BkfAfIVSVACQUFCEIJQoJUqkgAACJRSqAAAQgEqKqqoKoAAVINADQDTCMhoAGCZBpoNMg0Bk0AGRiGmQBghkNGmgAaGgGhkyAAADJoMhBoAaAaYRkNAAwTINNBpkGgMmgAyMQ0yAMEMho00ADQ0A0MmQAAAZNBkINADQDTCMhoAGCZBpoNMg0Bk0AGRiGmQBghkNGmgAaGgGhkyAAADJoMhBoAaAaYRkNAAwTINNBpkGgMmgAyMQ0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plHv2RyzDe41pTmr9dquVk5WI+7tqN7xMY51NrCwxYxWMMMUODVd94IflreuiuDS4Q8B+01PdWIeR7Z2PSboDmZBzTZbm0vlm+YamDe71cHDeLe3NLQx35tNze/Zd4OtZJ4H5jleDtaO1dLTc/Yf6Tit0uDFV/VZHc77k3njf+BwXQY7l3VpE6HQ1bjTVdzLwTiWkXW61jv/bfz2MftP2mn8hsbNmnfe1dlci/cpeBZe9O9Mb3ad5J4ZMoeVvXnvE3Vf8IfuPGfXMf/K309N6h2v/Rji5HmfdXovWedzucx3N7e+s7GzZvK6n3jxvabNmzc+0/BWl3nefl8G5cr7RzMaaP8jFsvuXMcFe0713O7ZfWn3pPaq4rdKejPByrzPUN6cBH6LZsTzcXrHkrrdfMONXA5hhyybH4Ru4LktC3T4dvvdXM7ThXPdPp7m9yTlH6VdKvbzBd4ZjJll3nAi+2sX31iU4m52d+92rlq6g9cftu+HkO1bx/+LuSKcKEhVMDc/A="

    kernels = Kernel(
        bz2.decompress(base64.b64decode(quantization_code)),
        [
            "weightInt8_int4",
            "weightInt4_fp16",
            "weightInt4_bf16"
        ],
    )
except Exception as exception:
    kernels = None
    logger.warning("Failed to load cpm_kernels:" + str(exception))


def quantize_int8(weight: torch.Tensor, bit_length: int):
    weight_scale = weight.abs().max(dim=-1).values / ((2 ** (bit_length - 1)) - 1)
    weight_scale = weight_scale.to(torch.float32)

    weight = torch.round(weight.to(weight_scale.dtype) / weight_scale[:, None]).to(torch.int8)
    return weight, weight_scale


def compress_int4_weight(weight: torch.Tensor):
    with torch.cuda.device(weight.device):
        num_row, num_chan = weight.size(0), weight.size(1)
        num_chan = num_chan // 2

        int8_weight = torch.empty(num_row, num_chan, dtype=torch.int8, device="cuda")
        stream = torch.cuda.current_stream()
        dim_grid = (num_row, 1, 1)
        dim_block = (min(round_up(num_chan, 32), 1024), 1, 1)

        kernels.weightInt8_int4(
            dim_grid,
            dim_block,
            0,
            stream,
            [
                ctypes.c_void_p(weight.data_ptr()), 
                ctypes.c_void_p(int8_weight.data_ptr()), 
                ctypes.c_int32(num_row), 
                ctypes.c_int32(num_chan)
            ],
        )

        return int8_weight


def dequantize_float(weight: torch.Tensor, weight_scale: torch.Tensor, bit_length: int, input: torch.Tensor):
    if bit_length == 8:
        float_weight = weight.to(input.dtype) * weight_scale.to(input.dtype)[:, None]
        return float_weight
    
    assert bit_length == 4, f"unsupported bit length: {bit_length}"
    
    func = (
        kernels.weightInt4_fp16 if input.dtype == torch.half else kernels.weightInt4_bf16
    )
    with torch.cuda.device(weight.device):
        num_row, num_chan = weight.size(0), weight.size(1)

        float_weight = torch.empty(num_row, num_chan * 2, dtype=input.dtype, device="cuda")
        stream = torch.cuda.current_stream()
        dim_grid = (num_row, 1, 1)
        dim_block = (min(round_up(num_chan, 32), 1024), 1, 1)

        func(
            dim_grid,
            dim_block,
            0,
            stream,
            [
                ctypes.c_void_p(weight.data_ptr()),
                ctypes.c_void_p(weight_scale.data_ptr()),
                ctypes.c_void_p(float_weight.data_ptr()),
                ctypes.c_int32(num_row),
                ctypes.c_int32(num_chan),
            ],
        )
        return float_weight

class QuantizationLinear(torch.nn.Module):
    def __init__(self, bit_length: int, weight: torch.Tensor, device="cuda"):
        super().__init__()

        self.bit_length = bit_length

        weight, weight_scale = quantize_int8(weight=weight, bit_length=bit_length)
        if bit_length == 4:
            weight = compress_int4_weight(weight)

        self.weight = torch.nn.Parameter(weight.to(device), requires_grad=False)
        self.weight_scale = torch.nn.Parameter(weight_scale.to(device), requires_grad=False)

    def forward(self, input: torch.Tensor):
        input_size = input.size()

        input = input.contiguous().view(-1, input.size(-1))
        original_weight = dequantize_float(self.weight, self.weight_scale, self.bit_length, input)

        output = torch.matmul(input, original_weight.t())
        return output.view(*(input_size[:-1] + (self.weight.size(0),)))