Upload cuda_backend.py
Browse files- cuda_backend.py +26 -1
cuda_backend.py
CHANGED
|
@@ -935,6 +935,16 @@ def _apply_dc_nudge_cuda(
|
|
| 935 |
return codes_out.reshape(n, QK_K)
|
| 936 |
|
| 937 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 938 |
def quantize_cuda(
|
| 939 |
weights: np.ndarray,
|
| 940 |
importance: np.ndarray | None,
|
|
@@ -981,7 +991,22 @@ def quantize_cuda(
|
|
| 981 |
clamp_val = seed_clamp_value(x, cfg)
|
| 982 |
dev = torch.device(device)
|
| 983 |
gpu_name = cuda_device_name(device)
|
| 984 |
-
batch_blocks =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 985 |
|
| 986 |
candidate_errors = np.empty((n_blocks, TOTAL_CANDIDATES), dtype=np.float32)
|
| 987 |
scales_seed = np.empty((n_blocks, NS), dtype=np.float32)
|
|
|
|
| 935 |
return codes_out.reshape(n, QK_K)
|
| 936 |
|
| 937 |
|
| 938 |
+
# VRAM-aware CUDA batching for the Q2_K factored pipeline. The candidate
|
| 939 |
+
# kernels materialize several live (B, K, 256) fp32 tensors (K = n_cand_d *
|
| 940 |
+
# n_cand_m), so the per-block footprint scales with K (~6 fp32 temporaries of
|
| 941 |
+
# 256 weights each). The floor is the historical 4096-block batch (known to
|
| 942 |
+
# fit even on small cards); on large GPUs the batch grows with the free VRAM.
|
| 943 |
+
Q2_K_CUDA_FLOOR_BLOCKS = 4096
|
| 944 |
+
Q2_K_CUDA_VRAM_FRACTION = 0.45
|
| 945 |
+
Q2_K_CUDA_FP32_TEMPS_PER_CANDIDATE = 6
|
| 946 |
+
|
| 947 |
+
|
| 948 |
def quantize_cuda(
|
| 949 |
weights: np.ndarray,
|
| 950 |
importance: np.ndarray | None,
|
|
|
|
| 991 |
clamp_val = seed_clamp_value(x, cfg)
|
| 992 |
dev = torch.device(device)
|
| 993 |
gpu_name = cuda_device_name(device)
|
| 994 |
+
batch_blocks = Q2_K_CUDA_FLOOR_BLOCKS
|
| 995 |
+
try:
|
| 996 |
+
# VRAM-aware default: scale the batch to free memory at start-up,
|
| 997 |
+
# never below the floor. Footprint per block: K live (B, K, 256)
|
| 998 |
+
# fp32 temporaries drive the peak (see _factor_candidates_cuda).
|
| 999 |
+
k_eff = int(cfg.n_cand_d) * int(cfg.n_cand_m)
|
| 1000 |
+
bytes_per_block = (
|
| 1001 |
+
Q2_K_CUDA_FP32_TEMPS_PER_CANDIDATE * k_eff * QK_K * 4
|
| 1002 |
+
)
|
| 1003 |
+
free_bytes, _ = torch.cuda.mem_get_info(dev)
|
| 1004 |
+
batch_blocks = max(
|
| 1005 |
+
Q2_K_CUDA_FLOOR_BLOCKS,
|
| 1006 |
+
int(Q2_K_CUDA_VRAM_FRACTION * free_bytes) // max(bytes_per_block, 1),
|
| 1007 |
+
)
|
| 1008 |
+
except Exception:
|
| 1009 |
+
batch_blocks = Q2_K_CUDA_FLOOR_BLOCKS
|
| 1010 |
|
| 1011 |
candidate_errors = np.empty((n_blocks, TOTAL_CANDIDATES), dtype=np.float32)
|
| 1012 |
scales_seed = np.empty((n_blocks, NS), dtype=np.float32)
|