Spaces:
Running on Zero
Running on Zero
Fix CUDA int8 GEMM: 32-align pads, exact float fallback; remove triage code
Browse files
app.py
CHANGED
|
@@ -206,12 +206,7 @@ def _encode_decode(audio_path: str, levels: tuple) -> dict:
|
|
| 206 |
with torch.inference_mode(): # untimed warmup so the first row's RTF isn't inflated
|
| 207 |
codec.sig_to_sig(sig)
|
| 208 |
if NATIVE_LEVEL in focal_levels:
|
| 209 |
-
|
| 210 |
-
codec_i8.sig_to_sig(sig)
|
| 211 |
-
except NotImplementedError: # TEMP triage
|
| 212 |
-
import traceback
|
| 213 |
-
|
| 214 |
-
raise gr.Error(traceback.format_exc()) from None
|
| 215 |
for level in focal_levels:
|
| 216 |
model = codec_i8 if level == NATIVE_LEVEL else codec
|
| 217 |
if level != NATIVE_LEVEL:
|
|
@@ -219,12 +214,7 @@ def _encode_decode(audio_path: str, levels: tuple) -> dict:
|
|
| 219 |
torch.cuda.synchronize()
|
| 220 |
start = time.perf_counter()
|
| 221 |
with torch.inference_mode():
|
| 222 |
-
|
| 223 |
-
rec = model.sig_to_sig(sig).squeeze(0).float().cpu().numpy()
|
| 224 |
-
except NotImplementedError: # TEMP triage: surface the bare error's traceback
|
| 225 |
-
import traceback
|
| 226 |
-
|
| 227 |
-
raise gr.Error(traceback.format_exc()) from None
|
| 228 |
torch.cuda.synchronize()
|
| 229 |
rtf = (time.perf_counter() - start) / seconds
|
| 230 |
rows.append(core.evaluate(ref, rec, sr, rtf, SIZES[level], level))
|
|
@@ -404,4 +394,4 @@ with gr.Blocks(title="FocalCodec quantization lab") as demo:
|
|
| 404 |
|
| 405 |
gr.Markdown(FOOTER)
|
| 406 |
|
| 407 |
-
demo.launch(mcp_server=True, theme=gr.themes.Soft()
|
|
|
|
| 206 |
with torch.inference_mode(): # untimed warmup so the first row's RTF isn't inflated
|
| 207 |
codec.sig_to_sig(sig)
|
| 208 |
if NATIVE_LEVEL in focal_levels:
|
| 209 |
+
codec_i8.sig_to_sig(sig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
for level in focal_levels:
|
| 211 |
model = codec_i8 if level == NATIVE_LEVEL else codec
|
| 212 |
if level != NATIVE_LEVEL:
|
|
|
|
| 214 |
torch.cuda.synchronize()
|
| 215 |
start = time.perf_counter()
|
| 216 |
with torch.inference_mode():
|
| 217 |
+
rec = model.sig_to_sig(sig).squeeze(0).float().cpu().numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
torch.cuda.synchronize()
|
| 219 |
rtf = (time.perf_counter() - start) / seconds
|
| 220 |
rows.append(core.evaluate(ref, rec, sr, rtf, SIZES[level], level))
|
|
|
|
| 394 |
|
| 395 |
gr.Markdown(FOOTER)
|
| 396 |
|
| 397 |
+
demo.launch(mcp_server=True, theme=gr.themes.Soft())
|
int8nn.py
CHANGED
|
@@ -17,7 +17,7 @@ import torch.nn.functional as F
|
|
| 17 |
from torch import nn
|
| 18 |
|
| 19 |
SCALE_PREFIX = "_scale/"
|
| 20 |
-
_ROUND =
|
| 21 |
|
| 22 |
|
| 23 |
def _up(n: int) -> int:
|
|
@@ -37,14 +37,17 @@ def _act_quant(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
|
| 37 |
def _int_mm(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
|
| 38 |
"""(M, K) int8 x (K, N) int8 -> (M, N) int32. M is padded: CUDA rejects M <= 16."""
|
| 39 |
m = a.shape[0]
|
| 40 |
-
mp = max(
|
| 41 |
if mp != m:
|
| 42 |
padded = torch.zeros((mp, a.shape[1]), dtype=a.dtype, device=a.device)
|
| 43 |
padded[:m] = a
|
| 44 |
a = padded
|
| 45 |
try:
|
| 46 |
return torch._int_mm(a, b)[:m]
|
| 47 |
-
except RuntimeError: # backend refuses the
|
|
|
|
|
|
|
|
|
|
| 48 |
return (a.to(torch.int32) @ b.to(torch.int32))[:m]
|
| 49 |
|
| 50 |
|
|
|
|
| 17 |
from torch import nn
|
| 18 |
|
| 19 |
SCALE_PREFIX = "_scale/"
|
| 20 |
+
_ROUND = 32 # cublasLt int8 GEMM (via torch._int_mm) wants %32 dims on CUDA; %16 is not enough (K=16 fails)
|
| 21 |
|
| 22 |
|
| 23 |
def _up(n: int) -> int:
|
|
|
|
| 37 |
def _int_mm(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
|
| 38 |
"""(M, K) int8 x (K, N) int8 -> (M, N) int32. M is padded: CUDA rejects M <= 16."""
|
| 39 |
m = a.shape[0]
|
| 40 |
+
mp = max(_ROUND, _up(m))
|
| 41 |
if mp != m:
|
| 42 |
padded = torch.zeros((mp, a.shape[1]), dtype=a.dtype, device=a.device)
|
| 43 |
padded[:m] = a
|
| 44 |
a = padded
|
| 45 |
try:
|
| 46 |
return torch._int_mm(a, b)[:m]
|
| 47 |
+
except (RuntimeError, NotImplementedError): # backend refuses the shape
|
| 48 |
+
if a.is_cuda:
|
| 49 |
+
# exact in float32 (|int8 * int8 * K| << 2^24); int32 matmul has no CUDA kernel
|
| 50 |
+
return (a.to(torch.float32) @ b.to(torch.float32))[:m]
|
| 51 |
return (a.to(torch.int32) @ b.to(torch.int32))[:m]
|
| 52 |
|
| 53 |
|