Upload 2 files
Browse files
q3_k.py
CHANGED
|
@@ -79,6 +79,12 @@ _log = logging.getLogger(__name__)
|
|
| 79 |
# The CPU default stays small (numpy temporaries stay cache-resident).
|
| 80 |
Q3_K_CHUNK_BLOCKS_CUDA = 16384
|
| 81 |
Q3_K_CHUNK_BLOCKS_CPU = 1024
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
def _to_dev(x, dev: "torch.device") -> "torch.Tensor":
|
|
@@ -1011,7 +1017,20 @@ def quantize_tensor_q3_k(
|
|
| 1011 |
)
|
| 1012 |
dev = torch.device("cuda" if cfg.device == "auto" else cfg.device) if use_cuda else None
|
| 1013 |
if chunk_blocks is None:
|
| 1014 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1015 |
chunk_blocks = max(1, int(chunk_blocks))
|
| 1016 |
_log.info(
|
| 1017 |
"Q3_K routing: cfg.device=%r cuda_is_available=%s n_blocks=%d -> %s (chunk_blocks=%d)",
|
|
|
|
| 79 |
# The CPU default stays small (numpy temporaries stay cache-resident).
|
| 80 |
Q3_K_CHUNK_BLOCKS_CUDA = 16384
|
| 81 |
Q3_K_CHUNK_BLOCKS_CPU = 1024
|
| 82 |
+
# Empirical peak VRAM per 256-weight super-block on the CUDA pipeline (same
|
| 83 |
+
# 4 GiB per reference chunk as Q4_0, over an 8x-wider block — identical
|
| 84 |
+
# 32768 bytes/block); used to auto-scale the GPU chunk to the free memory
|
| 85 |
+
# actually available on the device.
|
| 86 |
+
Q3_K_CUDA_BYTES_PER_BLOCK = 32768
|
| 87 |
+
Q3_K_CUDA_VRAM_FRACTION = 0.45
|
| 88 |
|
| 89 |
|
| 90 |
def _to_dev(x, dev: "torch.device") -> "torch.Tensor":
|
|
|
|
| 1017 |
)
|
| 1018 |
dev = torch.device("cuda" if cfg.device == "auto" else cfg.device) if use_cuda else None
|
| 1019 |
if chunk_blocks is None:
|
| 1020 |
+
if use_cuda and dev is not None:
|
| 1021 |
+
# VRAM-aware default: size the chunk from the free memory at
|
| 1022 |
+
# start-up, never below the reference chunk (which always fits
|
| 1023 |
+
# even on small cards).
|
| 1024 |
+
try:
|
| 1025 |
+
free_bytes, _ = torch.cuda.mem_get_info(dev)
|
| 1026 |
+
chunk_blocks = max(
|
| 1027 |
+
Q3_K_CHUNK_BLOCKS_CUDA,
|
| 1028 |
+
int(Q3_K_CUDA_VRAM_FRACTION * free_bytes) // Q3_K_CUDA_BYTES_PER_BLOCK,
|
| 1029 |
+
)
|
| 1030 |
+
except Exception:
|
| 1031 |
+
chunk_blocks = Q3_K_CHUNK_BLOCKS_CUDA
|
| 1032 |
+
else:
|
| 1033 |
+
chunk_blocks = Q3_K_CHUNK_BLOCKS_CPU
|
| 1034 |
chunk_blocks = max(1, int(chunk_blocks))
|
| 1035 |
_log.info(
|
| 1036 |
"Q3_K routing: cfg.device=%r cuda_is_available=%s n_blocks=%d -> %s (chunk_blocks=%d)",
|
q4_0.py
CHANGED
|
@@ -60,6 +60,11 @@ _log = logging.getLogger(__name__)
|
|
| 60 |
# allocating multi-hundred-MB arrays per chunk.
|
| 61 |
Q4_0_CHUNK_BLOCKS_CUDA = 131072
|
| 62 |
Q4_0_CHUNK_BLOCKS_CPU = 8192
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def dequantize_q4_0_bytes(raw: bytes | np.ndarray, n_blocks: int) -> np.ndarray:
|
|
@@ -388,10 +393,10 @@ def _quantize_q4_0_hpc_cuda(
|
|
| 388 |
e_u = X[:, None, :] - all_d[:, :, None] * (q_u.to(torch.float32) - 8.0)
|
| 389 |
sse_u = torch.sum(W[:, None, :] * e_u * e_u, dim=2)
|
| 390 |
pick_u = torch.argmin(sse_u, dim=1)
|
| 391 |
-
del e_u, q_u, qi_u, sh_u, sse_u
|
| 392 |
best_d16 = all_b[rows, pick_u]
|
| 393 |
best_d = all_d[rows, pick_u]
|
| 394 |
best_q = q_u[rows, pick_u].to(torch.int64)
|
|
|
|
| 395 |
|
| 396 |
# 5. Closed-Form 1-D Quadratic Vesica + DC Code Projection within sse_budget:
|
| 397 |
if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
|
|
@@ -609,7 +614,20 @@ def quantize_tensor_q4_0(
|
|
| 609 |
)
|
| 610 |
dev = torch.device("cuda" if cfg.device == "auto" else cfg.device) if use_cuda else None
|
| 611 |
if chunk_blocks is None:
|
| 612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
chunk_blocks = max(1, int(chunk_blocks))
|
| 614 |
_log.info(
|
| 615 |
"Q4_0 routing: cfg.device=%r cuda_is_available=%s n_blocks=%d -> %s (chunk_blocks=%d)",
|
|
|
|
| 60 |
# allocating multi-hundred-MB arrays per chunk.
|
| 61 |
Q4_0_CHUNK_BLOCKS_CUDA = 131072
|
| 62 |
Q4_0_CHUNK_BLOCKS_CPU = 8192
|
| 63 |
+
# Empirical peak VRAM per 32-weight block on the CUDA pipeline (~4 GiB
|
| 64 |
+
# observed for the 131072-block reference chunk); used to auto-scale the GPU
|
| 65 |
+
# chunk to the free memory actually available on the device.
|
| 66 |
+
Q4_0_CUDA_BYTES_PER_BLOCK = 32768
|
| 67 |
+
Q4_0_CUDA_VRAM_FRACTION = 0.45
|
| 68 |
|
| 69 |
|
| 70 |
def dequantize_q4_0_bytes(raw: bytes | np.ndarray, n_blocks: int) -> np.ndarray:
|
|
|
|
| 393 |
e_u = X[:, None, :] - all_d[:, :, None] * (q_u.to(torch.float32) - 8.0)
|
| 394 |
sse_u = torch.sum(W[:, None, :] * e_u * e_u, dim=2)
|
| 395 |
pick_u = torch.argmin(sse_u, dim=1)
|
|
|
|
| 396 |
best_d16 = all_b[rows, pick_u]
|
| 397 |
best_d = all_d[rows, pick_u]
|
| 398 |
best_q = q_u[rows, pick_u].to(torch.int64)
|
| 399 |
+
del e_u, q_u, qi_u, sh_u, sse_u
|
| 400 |
|
| 401 |
# 5. Closed-Form 1-D Quadratic Vesica + DC Code Projection within sse_budget:
|
| 402 |
if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
|
|
|
|
| 614 |
)
|
| 615 |
dev = torch.device("cuda" if cfg.device == "auto" else cfg.device) if use_cuda else None
|
| 616 |
if chunk_blocks is None:
|
| 617 |
+
if use_cuda and dev is not None:
|
| 618 |
+
# VRAM-aware default: size the chunk from the free memory at
|
| 619 |
+
# start-up, never below the reference chunk (which always fits
|
| 620 |
+
# even on small cards).
|
| 621 |
+
try:
|
| 622 |
+
free_bytes, _ = torch.cuda.mem_get_info(dev)
|
| 623 |
+
chunk_blocks = max(
|
| 624 |
+
Q4_0_CHUNK_BLOCKS_CUDA,
|
| 625 |
+
int(Q4_0_CUDA_VRAM_FRACTION * free_bytes) // Q4_0_CUDA_BYTES_PER_BLOCK,
|
| 626 |
+
)
|
| 627 |
+
except Exception:
|
| 628 |
+
chunk_blocks = Q4_0_CHUNK_BLOCKS_CUDA
|
| 629 |
+
else:
|
| 630 |
+
chunk_blocks = Q4_0_CHUNK_BLOCKS_CPU
|
| 631 |
chunk_blocks = max(1, int(chunk_blocks))
|
| 632 |
_log.info(
|
| 633 |
"Q4_0 routing: cfg.device=%r cuda_is_available=%s n_blocks=%d -> %s (chunk_blocks=%d)",
|