Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -241,6 +241,38 @@ def _should_quantize(path: str, n_layers: int) -> bool:
|
|
| 241 |
return True
|
| 242 |
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
@torch.no_grad()
|
| 245 |
def quantize_linear_(root: torch.nn.Module, mode: str = "4bit", n_layers: int = 0) -> tuple:
|
| 246 |
"""把 root 下的 nn.Linear 原地替换为 bnb 量化层。必须在 CUDA 上执行。"""
|
|
@@ -300,6 +332,7 @@ def quantize_linear_(root: torch.nn.Module, mode: str = "4bit", n_layers: int =
|
|
| 300 |
|
| 301 |
def ensure_model_ready():
|
| 302 |
"""把 pipeline 搬到真卡并完成一次性量化。必须在 @spaces.GPU 内部调用。"""
|
|
|
|
| 303 |
dev = torch.device("cuda", args.device)
|
| 304 |
if str(pipe.device) != str(dev):
|
| 305 |
pipe.to(dev)
|
|
@@ -307,10 +340,17 @@ def ensure_model_ready():
|
|
| 307 |
return
|
| 308 |
t0 = time.time()
|
| 309 |
n_layers = len(pipe.transformer.model.layers)
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
if torch.cuda.is_available():
|
| 315 |
torch.cuda.empty_cache()
|
| 316 |
mem = torch.cuda.memory_allocated() / 2**30
|
|
|
|
| 241 |
return True
|
| 242 |
|
| 243 |
|
| 244 |
+
def probe_bnb():
|
| 245 |
+
"""探测 bitsandbytes 是否可用,并打印诊断信息。
|
| 246 |
+
|
| 247 |
+
⚠️ 构建期(Docker build)没有 GPU,pip 能装上 bnb(纯 wheel),但 import 时
|
| 248 |
+
CUDA 不可见,bnb 会打印 "compiled without GPU support" 并回退 CPU 后端。
|
| 249 |
+
所以探测只用于给出可读日志,真正的判定放在 GPU 函数里。
|
| 250 |
+
"""
|
| 251 |
+
info = {"available": False, "version": None, "error": None}
|
| 252 |
+
try:
|
| 253 |
+
import bitsandbytes as bnb
|
| 254 |
+
|
| 255 |
+
info["available"] = True
|
| 256 |
+
info["version"] = getattr(bnb, "__version__", "?")
|
| 257 |
+
except Exception as e: # ImportError / CUDA 相关的初始化失败
|
| 258 |
+
info["error"] = repr(e)
|
| 259 |
+
print(
|
| 260 |
+
"[bnb] available=%s version=%s torch=%s cuda=%s%s"
|
| 261 |
+
% (
|
| 262 |
+
info["available"],
|
| 263 |
+
info["version"],
|
| 264 |
+
torch.__version__,
|
| 265 |
+
torch.version.cuda,
|
| 266 |
+
" error=%s" % info["error"] if info["error"] else "",
|
| 267 |
+
),
|
| 268 |
+
flush=True,
|
| 269 |
+
)
|
| 270 |
+
return info
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
_BNB_PROBE = probe_bnb() if QUANT_MODE != "none" else {"available": False}
|
| 274 |
+
|
| 275 |
+
|
| 276 |
@torch.no_grad()
|
| 277 |
def quantize_linear_(root: torch.nn.Module, mode: str = "4bit", n_layers: int = 0) -> tuple:
|
| 278 |
"""把 root 下的 nn.Linear 原地替换为 bnb 量化层。必须在 CUDA 上执行。"""
|
|
|
|
| 332 |
|
| 333 |
def ensure_model_ready():
|
| 334 |
"""把 pipeline 搬到真卡并完成一次性量化。必须在 @spaces.GPU 内部调用。"""
|
| 335 |
+
global QUANT_MODE
|
| 336 |
dev = torch.device("cuda", args.device)
|
| 337 |
if str(pipe.device) != str(dev):
|
| 338 |
pipe.to(dev)
|
|
|
|
| 340 |
return
|
| 341 |
t0 = time.time()
|
| 342 |
n_layers = len(pipe.transformer.model.layers)
|
| 343 |
+
try:
|
| 344 |
+
converted, skipped = quantize_linear_(pipe.transformer, QUANT_MODE, n_layers)
|
| 345 |
+
if args.quant_vae:
|
| 346 |
+
c2, s2 = quantize_linear_(pipe.vae, QUANT_MODE, 0)
|
| 347 |
+
converted, skipped = converted + c2, skipped + s2
|
| 348 |
+
except Exception as e:
|
| 349 |
+
# bnb 未安装 / CUDA 后端不可用 → 优雅降级到原精度,不能让整个生成失败
|
| 350 |
+
QUANT_MODE = "none"
|
| 351 |
+
_QUANT_STATE["log"] = f"FALLBACK to {args.precision}: {e!r}"
|
| 352 |
+
print("[quant]", _QUANT_STATE["log"], flush=True)
|
| 353 |
+
return
|
| 354 |
if torch.cuda.is_available():
|
| 355 |
torch.cuda.empty_cache()
|
| 356 |
mem = torch.cuda.memory_allocated() / 2**30
|