Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/experimental/dflash_mlx_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,53 @@ Draft checkpoints load through mlx-vlm's drafter classes (DFlash v1, DFlash2, La
## Tests

See the "Batched DFlash drafter" section of `docs/TESTING.md`.

## Verifying the effective engine

A DFlash request fails soft: a declined or failed drafter leaves the model's natural batched engine serving, and the request still succeeds with `200`. `GET /v1/models/status` reports what actually happened, so a client can assert DFlash is running instead of reading the server log.

| Field | Type | Meaning |
|-------|------|---------|
| `engine_type` | str | Configured engine for the model (unchanged) |
| `effective_engine` | str \| null | Engine serving the model now: `batched`, `vlm`, `distributed`, `splash`, `decision`, `embedding`, `reranker`, `audio_stt`, `audio_tts`, `audio_sts`. `null` when not loaded |
| `dflash_requested` | bool | `dflash_enabled` was set for this load |
| `dflash_active` | bool | The batched DFlash drafter is loaded and serving |
| `dflash_fallback_reason` | str \| null | Why a requested DFlash load is not active. Bounded and path-redacted, truncated to 200 characters |

DFlash running:

```json
{
"id": "Qwen3.8-27B",
"loaded": true,
"engine_type": "batched",
"effective_engine": "batched",
"dflash_requested": true,
"dflash_active": true,
"dflash_fallback_reason": null
}
```

DFlash requested but fell back:

```json
{
"id": "Qwen3.8-27B",
"loaded": true,
"engine_type": "batched",
"effective_engine": "batched",
"dflash_requested": true,
"dflash_active": false,
"dflash_fallback_reason": "DFlash drafter load failed: ValueError: Received parameters not in model: candidate_selector.hidden_projection.weight, ..."
}
```

A one-line client assertion:

```bash
curl -s localhost:10240/v1/models/status \
| jq -e '.models[] | select(.id=="Qwen3.8-27B") | .dflash_active'
```

DFlash was never requested when `dflash_requested` is `false`; `dflash_active`
is then `false` and `dflash_fallback_reason` is `null`.
103 changes: 102 additions & 1 deletion omlx/engine_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import gc
import json
import logging
import re
import time
from collections import OrderedDict
from contextlib import asynccontextmanager, suppress
Expand Down Expand Up @@ -167,6 +168,51 @@ def build_claude_tier_aliases(claude_code_settings) -> dict[str, str]:
# checkpoint is served text-only while its deployment is active.
_CLUSTER_ENGINE_TYPES = frozenset({"batched", "vlm"})

# Fallback reasons reach API clients, so they are bounded and redacted:
# loader errors can embed absolute checkpoint paths and, when an inner
# exception was formatted with its traceback, whole stack frames.
_MAX_FALLBACK_REASON_CHARS = 200
_PATHISH_RE = re.compile(r"~?/(?:[^,;:'\"()\[\]{}]+/)+[^,;:'\"()\[\]{}]*")
# Effective engine identity per engine class, for clients that must assert
# which backend actually serves a model (issue #2896).
_ENGINE_KIND_BY_CLASS = {
"DistributedBatchedEngine": "distributed",
"VLMBatchedEngine": "vlm",
"BatchedEngine": "batched",
"SplashEngine": "splash",
"EmbeddingEngine": "embedding",
"RerankerEngine": "reranker",
"STTEngine": "audio_stt",
"TTSEngine": "audio_tts",
"STSEngine": "audio_sts",
}


def _sanitized_fallback_reason(text: str) -> str:
"""Bound and redact a fallback reason before it reaches API clients.

Whitespace is collapsed so a multi-line message cannot leak a stack
trace line by line, filesystem-path runs become ``<path>``, and the
result is truncated to a fixed budget so an unbounded loader message
cannot inflate the status response.
"""

collapsed = " ".join(str(text).split())
redacted = _PATHISH_RE.sub("<path>", collapsed)
if len(redacted) > _MAX_FALLBACK_REASON_CHARS:
redacted = redacted[: _MAX_FALLBACK_REASON_CHARS - 3].rstrip() + "..."
return redacted


def _engine_kind(engine: object) -> str | None:
"""Effective engine identity for a live engine instance.

``None`` for a class the pool never constructs, so callers fall back to
the engine type they asked for instead of leaking a class name.
"""

return _ENGINE_KIND_BY_CLASS.get(type(engine).__name__)


def _positive_int(value: object) -> int:
try:
Expand Down Expand Up @@ -411,6 +457,15 @@ class EngineEntry:
load_failed: bool = False # Sticky until the next discovery refresh
load_failure_message: str | None = None
load_failure_at: float | None = None
# Requested-versus-effective state for the engine that currently serves
# this model (issue #2896). DFlash fails soft: a declined or failed
# drafter leaves the model's natural batched engine serving, so a client
# cannot infer the effective backend from settings alone. All four
# describe the live engine and reset on unload.
effective_engine: str | None = None
dflash_requested: bool = False
dflash_active: bool = False
dflash_fallback_reason: str | None = None


class EnginePool:
Expand Down Expand Up @@ -3637,6 +3692,10 @@ async def _stop_and_unload_engine(
entry.runtime_settings_signature = None
entry.runtime_estimated_size = None
entry.runtime_settle_size = None
entry.effective_engine = None
entry.dflash_requested = False
entry.dflash_active = False
entry.dflash_fallback_reason = None

if external:
# Cluster weights live in supervised rank processes and Splash's
Expand Down Expand Up @@ -3989,27 +4048,48 @@ async def _load_engine(
deployment = deployment if effective_type in _CLUSTER_ENGINE_TYPES else None
# Decision models never decode, so speculative decoding settings
# saved for the same checkpoint do not apply.
dflash_enabled = bool(getattr(model_settings, "dflash_enabled", False))
entry.dflash_requested = dflash_enabled
entry.dflash_active = False
entry.dflash_fallback_reason = None
if dflash_enabled and deployment is not None:
entry.dflash_fallback_reason = (
"DFlash is not supported with distributed inference"
)
elif dflash_enabled and engine is not None:
entry.dflash_fallback_reason = (
"DFlash is not supported with an external engine"
)
elif dflash_enabled and effective_type == "decision":
entry.dflash_fallback_reason = (
"DFlash is not supported for decision models"
)
if (
engine is None
and deployment is None
and model_settings is not None
and effective_type != "decision"
):
dflash_enabled = getattr(model_settings, "dflash_enabled", False)
dflash_draft = dflash_draft_for(model_settings, entry.model_path)
if dflash_enabled and self._entry_is_diffusion_model(entry):
logger.warning(
"DFlash is not supported for diffusion models; "
"loading %s with its native VLM engine",
model_id,
)
entry.dflash_fallback_reason = (
"DFlash is not supported for diffusion models"
)
elif dflash_enabled and not dflash_draft:
logger.warning(
"DFlash enabled for %s but no draft model is set; "
"loading without DFlash. Set dflash_draft_model to "
"enable it.",
model_id,
)
entry.dflash_fallback_reason = (
"DFlash is enabled but no draft model is configured"
)
elif dflash_enabled and not dflash_batched_supported(
entry.config_model_type, effective_type
):
Expand All @@ -4020,6 +4100,9 @@ async def _load_engine(
entry.config_model_type,
effective_type,
)
entry.dflash_fallback_reason = (
"DFlash is not supported for this model type and engine"
)
elif dflash_enabled:
# The drafter is attached after start below.
logger.info(
Expand Down Expand Up @@ -4238,6 +4321,7 @@ async def prefill_eviction_callback(
entry.runtime_estimated_size = resident_size
entry.runtime_settle_size = resident_size
entry.engine = engine
entry.effective_engine = _engine_kind(engine) or effective_type
entry.last_access = time.time()
self._current_model_memory += resident_size
load_completed = True
Expand Down Expand Up @@ -4289,8 +4373,12 @@ def _load_dflash_sync(path: str = drafter_path):
f"(drafter={drafter_id}): {e} -- toggle ignored"
)
drafter = None
entry.dflash_fallback_reason = _sanitized_fallback_reason(
f"DFlash drafter load failed: {type(e).__name__}: {e}"
)
if drafter is not None:
engine.set_dflash_drafter(drafter)
entry.dflash_active = True

# MiniMax M3.1 bundles its DSpark draft in dspark/; Lightning MTP
# runs it through the same block-drafter verify path.
Expand Down Expand Up @@ -4553,6 +4641,15 @@ def get_status(self) -> dict:
"""
Get pool status for monitoring endpoints.

``engine_type`` stays the model's configured engine. ``effective_engine``
names the engine that actually serves it right now, and the ``dflash_*``
keys separate a real DFlash load from a silent fallback (issue #2896):
requested and active both true means the DFlash drafter serves the
model; requested true with active false carries
``dflash_fallback_reason``; requested false means DFlash was never
asked for. All four describe the live engine and reset when the model
unloads.

Returns:
Dictionary with pool status information
"""
Expand Down Expand Up @@ -4584,6 +4681,10 @@ def get_status(self) -> dict:
"external_engine": external_engine,
"pinned": e.is_pinned,
"engine_type": e.engine_type,
"effective_engine": e.effective_engine,
"dflash_requested": e.dflash_requested,
"dflash_active": e.dflash_active,
"dflash_fallback_reason": e.dflash_fallback_reason,
"model_type": e.model_type,
"config_model_type": e.config_model_type,
"realtime_stt": is_realtime_stt_model(
Expand Down
Loading
Loading