Skip to content

Commit 18d013e

Browse files
committed
Enhance draft model loading and response handling
- Update load_draft function to check for local directory and config.json before downloading. - Refactor response handling in _do_generate to simplify text accumulation and ensure proper chunking. This improves the efficiency of loading draft models and enhances the streaming response mechanism.
1 parent 645586f commit 18d013e

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

‎dflash/model_mlx.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ def load(model_id: str):
130130

131131

132132
def load_draft(draft_id: str) -> DFlashDraftModel:
133-
path = Path(snapshot_download(draft_id, allow_patterns=["*.safetensors", "*.json"]))
133+
local = Path(draft_id)
134+
if local.is_dir() and (local / "config.json").exists():
135+
path = local
136+
else:
137+
path = Path(snapshot_download(draft_id, allow_patterns=["*.safetensors", "*.json"]))
134138
cfg = json.loads((path / "config.json").read_text())
135139
config = DFlashConfig(
136140
hidden_size=cfg["hidden_size"],

‎dflash/server.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,11 @@ def _sync_worker() -> None:
151151
):
152152
if _cancel_event.is_set():
153153
break
154-
new_text = resp.text
155-
if new_text and new_text != text_so_far:
156-
delta = new_text[len(text_so_far) :]
157-
text_so_far = new_text
154+
delta_text = resp.text
155+
if delta_text:
156+
text_so_far += delta_text
158157
gen_tokens.extend(resp.tokens)
159-
_put({"type": "chunk", "content": delta})
158+
_put({"type": "chunk", "content": delta_text})
160159

161160
if resp.finish_reason is not None:
162161
elapsed = time.perf_counter() - tic
@@ -422,10 +421,10 @@ def _load_models(args: argparse.Namespace) -> None:
422421
log.info("Target model loaded successfully")
423422

424423
if args.draft_model:
425-
from dflash.model_mlx import DFlashDraftModel
424+
from dflash.model_mlx import load_draft
426425

427426
log.info("Loading DFlash draft model: %s", args.draft_model)
428-
_draft = DFlashDraftModel.from_pretrained(args.draft_model)
427+
_draft = load_draft(args.draft_model)
429428
log.info("DFlash draft model loaded (block_size=%d)", _draft.config.block_size)
430429

431430
_model_id = args.model_id or model_path.stem

0 commit comments

Comments
 (0)