forked from mlc-ai/mlc-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34497ea
commit 2ed5c93
Showing
5 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Command line entrypoint of serve.""" | ||
|
||
import json | ||
|
||
from mlc_llm.help import HELP | ||
from mlc_llm.interface.serve import serve | ||
from mlc_llm.support.argparse import ArgumentParser | ||
|
||
|
||
def main(argv): | ||
"""Parse command line arguments and call `mlc_llm.interface.chat`.""" | ||
parser = ArgumentParser("MLC LLM Chat CLI") | ||
|
||
parser.add_argument( | ||
"model", | ||
type=str, | ||
help=HELP["model"] + " (required)", | ||
) | ||
parser.add_argument( | ||
"--opt", | ||
type=str, | ||
default="O2", | ||
help=HELP["opt"] + ' (default: "%(default)s")', | ||
) | ||
parser.add_argument( | ||
"--device", | ||
type=str, | ||
default="auto", | ||
help=HELP["device_deploy"] + ' (default: "%(default)s")', | ||
) | ||
parser.add_argument( | ||
"--model-lib-path", | ||
type=str, | ||
default=None, | ||
help=HELP["model_lib_path"] + ' (default: "%(default)s")', | ||
) | ||
# Todo: help | ||
parser.add_argument( | ||
"--max-batch-size", | ||
type=int, | ||
default=80, | ||
help=HELP["max_batch_size"] + ' (default: "%(default)s")', | ||
) | ||
parser.add_argument( | ||
"--max-total-seq-length", type=int, help=HELP["max_total_sequence_length_serve"] | ||
) | ||
parser.add_argument("--prefill-chunk-size", type=int, help=HELP["prefill_chunk_size_serve"]) | ||
parser.add_argument("--enable-tracing", action="store_true", help=HELP["enable_tracing_serve"]) | ||
parser.add_argument("--host", type=str, default="127.0.0.1", help="host name") | ||
parser.add_argument("--port", type=int, default=8000, help="port") | ||
parser.add_argument("--allow-credentials", action="store_true", help="allow credentials") | ||
parser.add_argument("--allow-origins", type=json.loads, default=["*"], help="allowed origins") | ||
parser.add_argument("--allow-methods", type=json.loads, default=["*"], help="allowed methods") | ||
parser.add_argument("--allow-headers", type=json.loads, default=["*"], help="allowed headers") | ||
parsed = parser.parse_args(argv) | ||
|
||
serve( | ||
model=parsed.model, | ||
device=parsed.device, | ||
opt=parsed.opt, | ||
model_lib_path=parsed.model_lib_path, | ||
max_batch_size=parsed.max_batch_size, | ||
max_total_sequence_length=parsed.max_total_seq_length, | ||
prefill_chunk_size=parsed.prefill_chunk_size, | ||
enable_tracing=parsed.enable_tracing, | ||
host=parsed.host, | ||
port=parsed.port, | ||
allow_credentials=parsed.allow_credentials, | ||
allow_origins=parsed.allow_origins, | ||
allow_methods=parsed.allow_methods, | ||
allow_headers=parsed.allow_headers, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Python entrypoint of serve.""" | ||
|
||
import dataclasses | ||
import json | ||
from typing import Any, List, Optional, Union | ||
|
||
import fastapi | ||
import uvicorn | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
from mlc_llm.serve import async_engine, config | ||
from mlc_llm.serve.server import ServerContext | ||
|
||
|
||
def serve( | ||
model: str, | ||
device: str, | ||
opt: str, | ||
model_lib_path: Optional[str], | ||
max_batch_size: int, | ||
max_total_sequence_length: Optional[int], | ||
prefill_chunk_size: Optional[int], | ||
enable_tracing: bool, | ||
host: str, | ||
port: int, | ||
allow_credentials: bool, | ||
allow_origins: Any, | ||
allow_methods: Any, | ||
allow_headers: Any, | ||
): | ||
|
||
# Initialize model loading info and KV cache config | ||
# Todo: JIT | ||
model_info = async_engine.ModelInfo( | ||
model=model, | ||
model_lib_path=model_lib_path, | ||
device=device, | ||
) | ||
kv_cache_config = config.KVCacheConfig( | ||
max_num_sequence=max_batch_size, | ||
max_total_sequence_length=max_total_sequence_length, | ||
prefill_chunk_size=prefill_chunk_size, | ||
) | ||
# Create engine and start the background loop | ||
engine = async_engine.AsyncThreadedEngine( | ||
model_info, kv_cache_config, enable_tracing=enable_tracing | ||
) | ||
|
||
# Todo: context-based | ||
ServerContext.add_model(model, engine) | ||
|
||
app = fastapi.FastAPI() | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_credentials=allow_credentials, | ||
allow_origins=allow_origins, | ||
allow_methods=allow_methods, | ||
allow_headers=allow_headers, | ||
) | ||
|
||
# Include the routers from subdirectories. | ||
# Todo: move out? | ||
from mlc_llm.serve.entrypoints import debug_entrypoints, openai_entrypoints | ||
|
||
app.include_router(openai_entrypoints.app) | ||
app.include_router(debug_entrypoints.app) | ||
uvicorn.run(app, host=host, port=port, log_level="info") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters