mirror of
https://github.com/huggingface/text-generation-inference.git
synced 2025-04-21 23:12:07 +00:00
<!-- Congratulations! You've made it this far! You're not quite done yet though. Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution. Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change. Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost. --> <!-- Remove if not applicable --> Fixes # (issue) - [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case). - [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests), Pull Request section? - [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link to it if that's the case. - [ ] Did you make sure to update the documentation with your changes? Here are the [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation). - [ ] Did you write any new necessary tests? Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR. <!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @ @OlivierDehaene OR @Narsil -->
133 lines
3.8 KiB
Python
133 lines
3.8 KiB
Python
import torch
|
|
from exllama_kernels import make_q4, q4_matmul, prepare_buffers, set_tuning_params
|
|
|
|
# Dummy tensor to pass instead of g_idx since there is no way to pass "None" to a C++ extension
|
|
none_tensor = torch.empty((1, 1), device="meta")
|
|
|
|
|
|
def ext_make_q4(qweight, qzeros, scales, g_idx, device):
|
|
"""Construct Q4Matrix, return handle"""
|
|
return make_q4(
|
|
qweight, qzeros, scales, g_idx if g_idx is not None else none_tensor, device
|
|
)
|
|
|
|
|
|
def ext_q4_matmul(x, q4, q4_width):
|
|
"""Matrix multiplication, returns x @ q4"""
|
|
outshape = x.shape[:-1] + (q4_width,)
|
|
x = x.view(-1, x.shape[-1])
|
|
output = torch.empty((x.shape[0], q4_width), dtype=torch.float16, device=x.device)
|
|
|
|
q4_matmul(x, q4, output)
|
|
|
|
return output.view(outshape)
|
|
|
|
|
|
MAX_DQ = 1
|
|
MAX_INNER = 1
|
|
ACT_ORDER = False
|
|
DEVICE = None
|
|
|
|
TEMP_STATE = None
|
|
TEMP_DQ = None
|
|
|
|
|
|
def set_device(device):
|
|
global DEVICE
|
|
DEVICE = device
|
|
|
|
|
|
def create_exllama_buffers(max_total_tokens: int):
|
|
global MAX_DQ, MAX_INNER, ACT_ORDER, DEVICE, TEMP_STATE, TEMP_DQ
|
|
|
|
assert DEVICE is not None, "call set_device first"
|
|
|
|
if not ACT_ORDER:
|
|
max_total_tokens = 1
|
|
|
|
# This temp_state buffer is required to reorder X in the act-order case.
|
|
temp_state = torch.zeros(
|
|
(max_total_tokens, MAX_INNER), dtype=torch.float16, device=DEVICE
|
|
)
|
|
temp_dq = torch.zeros((1, MAX_DQ), dtype=torch.float16, device=DEVICE)
|
|
|
|
# This temp_dq buffer is required to dequantize weights when using cuBLAS, typically for the prefill.
|
|
prepare_buffers(DEVICE, temp_state, temp_dq)
|
|
|
|
matmul_recons_thd = 8
|
|
matmul_fused_remap = False
|
|
matmul_no_half2 = False
|
|
set_tuning_params(matmul_recons_thd, matmul_fused_remap, matmul_no_half2)
|
|
|
|
TEMP_STATE, TEMP_DQ = temp_state, temp_dq
|
|
|
|
|
|
class Ex4bitLinear(torch.nn.Module):
|
|
"""Linear layer implementation with per-group 4-bit quantization of the weights"""
|
|
|
|
def __init__(self, qweight, qzeros, scales, g_idx, bias, bits, groupsize):
|
|
super().__init__()
|
|
global MAX_DQ, MAX_INNER, ACT_ORDER, DEVICE
|
|
assert bits == 4
|
|
|
|
self.device = qweight.device
|
|
self.qweight = qweight
|
|
self.qzeros = qzeros
|
|
self.scales = scales
|
|
self.g_idx = g_idx.cpu() if g_idx is not None else None
|
|
self.bias = bias if bias is not None else None
|
|
|
|
if self.g_idx is not None and (
|
|
(self.g_idx == 0).all()
|
|
or torch.equal(
|
|
g_idx.cpu(),
|
|
torch.tensor(
|
|
[i // groupsize for i in range(g_idx.shape[0])], dtype=torch.int32
|
|
),
|
|
)
|
|
):
|
|
self.empty_g_idx = True
|
|
self.g_idx = None
|
|
|
|
assert self.device.type == "cuda"
|
|
assert self.device.index is not None
|
|
|
|
self.q4 = ext_make_q4(
|
|
self.qweight, self.qzeros, self.scales, self.g_idx, self.device.index
|
|
)
|
|
|
|
self.height = qweight.shape[0] * 8
|
|
self.width = qweight.shape[1]
|
|
|
|
# Infer groupsize from height of qzeros
|
|
self.groupsize = None
|
|
if self.qzeros.shape[0] > 1:
|
|
self.groupsize = (self.qweight.shape[0] * 8) // (self.qzeros.shape[0])
|
|
|
|
if self.groupsize is not None:
|
|
assert groupsize == self.groupsize
|
|
|
|
# Handle act-order matrix
|
|
if self.g_idx is not None:
|
|
if self.groupsize is None:
|
|
raise ValueError("Found group index but no groupsize. What do?")
|
|
self.act_order = True
|
|
else:
|
|
self.act_order = False
|
|
|
|
DEVICE = self.qweight.device
|
|
|
|
MAX_DQ = max(MAX_DQ, self.qweight.numel() * 8)
|
|
|
|
if self.act_order:
|
|
MAX_INNER = max(MAX_INNER, self.height, self.width)
|
|
|
|
ACT_ORDER = True
|
|
|
|
def forward(self, x):
|
|
out = ext_q4_matmul(x, self.q4, self.width)
|
|
|
|
if self.bias is not None:
|
|
out.add_(self.bias)
|
|
return out
|