diff --git a/xtts/llm-adapter/adapter.py b/xtts/llm-adapter/adapter.py index 86451eb..debd701 100644 --- a/xtts/llm-adapter/adapter.py +++ b/xtts/llm-adapter/adapter.py @@ -705,11 +705,17 @@ def _kv_bytes(meta: dict, ctx: int) -> int: return per_tok * ctx + swa_tok * min(ctx, window + UBATCH) -async def _auto_cpu_moe(repo: str, file: dict, vram_mb: int, ctx: int) -> int: +async def _auto_cpu_moe(repo: str, file: dict, vram_mb: int, ctx: int) -> tuple: """Wie viele Schichten mit Experten muessen in den RAM, damit der Rest auf die Karte passt? Holt nur den Kopf der GGUF (Metadaten + Tensor-Verzeichnis, wenige MB per HTTP-Range) — die Datei selbst laedt erst llama.cpp. - 0 = passt komplett, -1 = alle Experten in den RAM (oder unbekannt).""" + Liefert (cpu_moe, ctx_fit): + cpu_moe: 0 = passt komplett, -1 = alle Experten in den RAM (oder unbekannt) + ctx_fit: groesster Kontext <= ctx, dessen KV-Cache selbst mit ALLEN + Experten im RAM noch auf die Karte passt (0 = nicht mal 4k; + None = nicht berechenbar). Ohne diese Pruefung startete z.B. + Qwen3-Coder-30B mit ctx 262144 (24 GB KV-Cache) auf einer 12-GB- + Karte — llama.cpp brach sofort ab, llama-swap meldete nur "500".""" import tempfile url = f"https://huggingface.co/{repo}/resolve/main/{file['path']}" headers = {"Range": "bytes=0-41943039"} @@ -729,19 +735,27 @@ async def _auto_cpu_moe(repo: str, file: dict, vram_mb: int, ctx: int) -> int: os.unlink(tmp) except Exception as e: logger.warning("MoE-Aufteilung fuer %s nicht berechenbar (%s) — alle Experten in den RAM", repo, e) - return -1 + return -1, None exp = meta.get("_expert_bytes_by_layer") or {} if not exp: - return 0 # kein MoE + return 0, None # kein MoE total_mb = int(file.get("size") or 0) / 1048576 exp_mb = sum(exp.values()) / 1048576 - budget = vram_mb * 0.92 - VRAM_OVERHEAD_MB - _kv_bytes(meta, ctx) / 1048576 - (total_mb - exp_mb) + # Was bleibt auf der Karte, wenn ALLE Experten im RAM liegen? Davon muss + # der KV-Cache leben — sonst passt das Modell bei keiner Aufteilung. + free_for_kv = vram_mb * 0.92 - VRAM_OVERHEAD_MB - (total_mb - exp_mb) + ctx_fit = 0 + for c in [s for s in CTX_STEPS if s <= ctx] + ([ctx] if ctx not in CTX_STEPS else []): + if _kv_bytes(meta, c) / 1048576 <= free_for_kv: + ctx_fit = max(ctx_fit, c) + use_ctx = ctx_fit or ctx + budget = free_for_kv - _kv_bytes(meta, use_ctx) / 1048576 per_layer = exp_mb / len(exp) on_gpu = max(0, int(budget // per_layer)) if per_layer else 0 n = max(0, len(exp) - on_gpu) - logger.info("MoE %s: %d von %d Schichten mit Experten in den RAM (Karte %d MB, ctx %d)", - repo, n, len(exp), vram_mb, ctx) - return -1 if n >= len(exp) else n + logger.info("MoE %s: %d von %d Schichten mit Experten in den RAM (Karte %d MB, ctx %d, passt bis ctx %d)", + repo, n, len(exp), vram_mb, ctx, ctx_fit) + return (-1 if n >= len(exp) else n), ctx_fit def _model_files(key: str) -> tuple: @@ -1704,13 +1718,29 @@ async def _do_provision(ws, payload: dict, job: dict) -> None: cpu_moe = int(payload.get("cpuMoe") or 0) except (TypeError, ValueError): cpu_moe = 0 - if cpu_moe == -2: - # "automatisch verteilen": so viele Experten auf die Karte wie passen + if cpu_moe in (-1, -2): + # MoE mit Experten im RAM: Der KV-Cache bleibt IMMER auf der Karte. + # Passt er beim gewuenschten Kontext nicht, Kontext kuerzen statt + # llama.cpp still abstuerzen zu lassen. qfiles = _files_for_quant(repo_files, quant) if repo_files else [] vram = int(((await _gpu_names()).get(card) or {}).get("vramMB") or 0) if len(qfiles) == 1 and vram: - cpu_moe = await _auto_cpu_moe(repo, qfiles[0], vram, ctx_req) - else: + auto, ctx_fit = await _auto_cpu_moe(repo, qfiles[0], vram, ctx_req) + if cpu_moe == -2: + # "automatisch verteilen": so viele Experten auf die Karte wie passen + cpu_moe = auto + if ctx_fit == 0: + msg = (f"{repo}: passt nicht auf Karte {card} ({vram // 1024} GB) — schon " + f"ohne Experten reicht der Platz nicht einmal fuer 4k Kontext.") + await _emit_progress(ws, key, "error", gpu=card, error=msg) + return await _provision_result(ws, key, False, gpu=card, error=msg) + if ctx_fit and ctx_fit < ctx_req: + note = (f"Kontext von {ctx_req // 1024}k auf {ctx_fit // 1024}k gekuerzt — " + f"mehr passt als KV-Cache nicht auf die {vram // 1024}-GB-Karte") + logger.info("Karte %s: %s", card, note) + quant_note = "; ".join(x for x in (quant_note, note) if x) + ctx_req = ctx_fit + elif cpu_moe == -2: cpu_moe = -1 # gesplittete Datei / Karte unbekannt: alle in den RAM entry = { "key": key, "hfRepo": repo, "quant": quant,