File size: 21,111 Bytes
2f52b6c |
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 |
# Copyright (c) 2023, Baichuan Intelligent Technology. All rights reserved.
import torch
from typing import List
import bz2
import base64
import ctypes
from transformers.utils import logging
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 = "QlpoOTFBWSZTWX/mUzwAK6f///////////////////////////////7f////////////4C5duvi2D0Oj1ppVCJ2zQFYbnbsxmq20pAC7kEDb3Z3nWrextY9NZbavON7nveSRqszudmzAGGgkeh0Pewk881e3Tz13kW9YO7uA9AUUiAWLNW2HHWCE005Mdz3jHs1Ic7QNCQBNGgmE000DRNoGjUYmA0mEmJjIaI9JtT0JoaaMTaQ0aMjTTI1TzKMmETwyaJ6k8p4Ke1T0wk2aE0anpPSHppqNM1HqYzVGj0MpsTTUGpoCAAEyAAAmhpPSYowMk9U8mqb0mJtU8ETwCZT1DQ9R5R6htE9TTyRptQeoyHqA0B6g9T1AD1HpGQGgD1A0NPUAAAA0A1Mg00gmhKPU9E2SekHoJ5QHlNDEPUeoDEaBkAHqBoABoNABoAaGgBoAAAAAAA0AAAAAAAAEmoiIgmiD0maRip+qfpR+k9U/QKaZPUepiGeST1HqeU9TQ9JoANAMhoZPU0AAYnqaBoAANABoAAAADQGgAAADTQ0IAAAAAAAAAAAAAAA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bqyHkJreoQDGrZe8QH4qNpIBqEHFpVTrJVwkLCu5ds3+pbccosPAGFjP4J0AB15EXRr4rcAbXmibqr2600yb4dM8VbMHACFOCBZhZIxpWCMkDUZIBUQoKpooWCkAnBzOK5na/LqSSLTATYIaabQCteZkFlqs0bDPpuWAcNiRn6GWSnwrsatNVFIK0+WUGVX3p1UghXmamW9amFzoPHfP2Z3WLhW9ZEaq0DQiqOJyRC17MYwQA84eUDjyR/GOBNpNoO1pV6NwwsBZoAgBWz+M+YS5GC+Su1IEB0A5in0LwPQxXq7joeDPBdd3DzF6z96RTojxR29u8vE3GnO6jAa0MBmCuoxyYl/SDsbSpYIlMINttOUZndGWJ2JgBs8s7bw1GhnALOxFBnZayRRjt4bSvH+Ma9WNZSaKBoUDtDEQNIMt5XAZJIvEFZSahWUgL7ADIBAjZYJVAK8NHljSCRbLZdxbuCkFfrZVirL+GkBWYaJFCoglTaEWtiguhCVZNjj+c9eMUMbOVJQmcHOmKmRIKboAMkAbohUflNANgubKuhTXDGSlSKY0PetmdL+7bQoIJCVRY+osfasgH1NADQYBBoYd+dccoSIhapDyYkRkhkYGAZDWCMlJReDHnRJZKAxUYiJmPGYriVoGAkdW2QI785BQQakRBFiFEknMOMGpw8jj8a7sLaWrGrZ5gDnB2Ys6AFHfczh5BvVw8R6n1P4QHEbDeIf/i7kinChIP/Mpng="
kernels = Kernel(
bz2.decompress(base64.b64decode(quantization_code)),
[
"int4_to_fp16",
"fp16_to_int4",
"int8_to_fp16",
"fp16_to_int8",
"int4_to_bf16",
"bf16_to_int4",
"int8_to_bf16",
"bf16_to_int8",
],
)
except Exception as exception:
kernels = None
logger.warning("Failed to load kernels:" + str(exception))
def quant4(weight: torch.Tensor, scale: torch.Tensor):
stream = torch.cuda.current_stream()
num_row = weight.size(0)
num_chan_fp16 = weight.size(1)
# 4bit
num_chan_int = num_chan_fp16 // 8
qweight = torch.zeros((num_row, num_chan_int), dtype=torch.int32, device=weight.device)
intweight = torch.empty(num_row, num_chan_fp16, dtype = torch.int32)
intweight = torch.clip(torch.round(weight.to(scale.dtype) / scale[:, None]),-16, 15).to(dtype=torch.int32)
for j in range(num_chan_int):
qweight[:, j] = ((intweight[:, j*8+7] & 0x0f) << 28) \
| ((intweight[:, j*8+6] & 0x0f) << 24) \
| ((intweight[:, j*8+5] & 0x0f) << 20) \
| ((intweight[:, j*8+4] & 0x0f) << 16) \
| ((intweight[:, j*8+3] & 0x0f) << 12) \
| ((intweight[:, j*8+2] & 0x0f) << 8) \
| ((intweight[:, j*8+1] & 0x0f) << 4) \
| ((intweight[:, j*8] & 0x0f))
return qweight
def dequant4(qweight: torch.Tensor, scale: torch.Tensor, input: torch.Tensor):
stream = torch.cuda.current_stream()
num_row = qweight.size(0)
num_chan_int = qweight.size(1)
# 4bit
num_chan_fp16 = num_chan_int * 8
out = torch.empty((num_row, num_chan_fp16), dtype=input.dtype, device=qweight.device)
blockDim = (128, 1, 1)
gridDim = ((num_chan_int + blockDim[0] - 1) // blockDim[0], num_row, 1)
if input.dtype == torch.bfloat16:
kernels.int4_to_bf16(
gridDim,
blockDim,
0,
stream,
[ctypes.c_void_p(out.data_ptr()), ctypes.c_void_p(qweight.data_ptr()),
ctypes.c_void_p(scale.data_ptr()), ctypes.c_int32(num_row), ctypes.c_int32(num_chan_int), ctypes.c_int32(num_chan_fp16)],
)
elif input.dtype == torch.float16:
kernels.int4_to_fp16(
gridDim,
blockDim,
0,
stream,
[ctypes.c_void_p(out.data_ptr()), ctypes.c_void_p(qweight.data_ptr()),
ctypes.c_void_p(scale.data_ptr()), ctypes.c_int32(num_row), ctypes.c_int32(num_chan_int), ctypes.c_int32(num_chan_fp16)],
)
return out
class QLinear(torch.nn.Module):
def __init__(self, bits: int, weight: torch.Tensor, bias=None):
super().__init__()
self.quant_bits = bits
self.scale = weight.abs().max(dim=-1).values / ((2 ** (bits - 1)) - 1)
self.scale = self.scale.to(torch.float32)
if self.quant_bits == 4:
self.weight = quant4(weight, self.scale)
elif self.quant_bits == 8:
self.weight = torch.round(weight.to(self.scale.dtype) / self.scale[:, None]).to(torch.int8)
if self.quant_bits == 8:
self.weight = self.weight.T
self.bias = None
def forward(self, input):
if self.quant_bits == 4:
assert(input.dtype == torch.bfloat16 or input.dtype == torch.float16)
if self.weight.device != input.device:
self.weight = self.weight.to(input.device)
self.scale = self.scale.to(input.device)
if self.quant_bits == 4:
self.scale = self.scale.to(input.dtype)
rweight = dequant4(self.weight, self.scale, input).T
output = torch.matmul(input, rweight)
elif self.quant_bits == 8:
rweight = self.weight.to(input.dtype) * self.scale.to(input.dtype)
output = torch.matmul(input, rweight)
if self.bias is not None:
output = output + self.bias
return output
|