ggml-quantization

GGUF quantization kernels from llama.cpp, computing directly on the packed blocks of a quantized checkpoint rather than on a dense copy of its weights.

  • mul_mat_vec — fused dequantize + gemv, for up to MAX_GEMV_ROWS rows
  • dequantize — blocks to values
  • get_rows — gathers rows, unpacking as it goes
  • mul_mat_id — one dispatch for a bank of routed experts, given the router's choices

GEMV_TYPES lists the quantization types this build has a gemv for.

Usage

import torch
from kernels import get_kernel

k = get_kernel("marcsun13/ggml-quantization", version=1)

Q4_K = 12                          # ggml type id; `k.GEMV_TYPES` lists what this build covers
out_features = in_features = 4096
# a GGUF weight as stored: one row per output feature, 144 bytes per 256-element Q4_K block
blocks = torch.randint(0, 256, (out_features, in_features // 256 * 144), dtype=torch.uint8, device="mps")
x = torch.randn(1, in_features, device="mps")

y = k.mul_mat_vec(blocks, x, Q4_K, out_features)                          # (1, 4096) f32
w = k.dequantize(blocks, Q4_K, out_features, in_features, torch.bfloat16)  # (4096, 4096)
rows = k.get_rows(blocks, torch.tensor([3, 7], device="mps"), Q4_K, in_features, torch.bfloat16)
Downloads last month
-
kernel
mit
Supported hardwares new
Metal
Apple Silicon
Apple MacBook Neo
8GB
Apple Silicon
Apple M1
8GB
Apple Silicon Pro
Apple M1 Pro
16GB
Apple Silicon Max
Apple M1 Max
16GB
Apple Silicon Ultra
Apple M1 Ultra
16GB
Apple Silicon
Apple M2
8GB
Apple Silicon Pro
Apple M2 Pro
16GB
Apple Silicon Max
Apple M2 Max
32GB
Apple Silicon Ultra
Apple M2 Ultra
64GB
Apple Silicon
Apple M3
8GB
Apple Silicon Pro
Apple M3 Pro
18GB
Apple Silicon Max
Apple M3 Max
36GB
Apple Silicon Ultra
Apple M3 Ultra
96GB
Apple Silicon
Apple M4
16GB
Apple Silicon Pro
Apple M4 Pro
24GB
Apple Silicon Max
Apple M4 Max
36GB
Apple Silicon
Apple M5
16GB
Apple Silicon Pro
Apple M5 Pro
24GB
Apple Silicon Max
Apple M5 Max
36GB
OS
macos
Arch
aarch64
Kernel Builder
81f55ea