-
Notifications
You must be signed in to change notification settings - Fork 44
Add http server to JetStream #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a45c32
Add http server to JetStream
JoeZijunZhou 07fe623
Add generate api and cleanup
JoeZijunZhou a8f2563
Add unit tests
JoeZijunZhou cc7316c
format & deps
JoeZijunZhou e485bc6
type & lint
JoeZijunZhou 78e8120
Merge branch 'main' into zijun/http-server
JoeZijunZhou acc64da
Merge refactor
JoeZijunZhou d253afd
fix refactor
JoeZijunZhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,13 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains hidden or 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,32 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Config for JetStream Server (including engine init).""" | ||
|
||
from typing import Type | ||
|
||
from jetstream.core import config_lib | ||
|
||
|
||
def get_server_config( | ||
config_str: str, | ||
) -> config_lib.ServerConfig | Type[config_lib.ServerConfig]: | ||
match config_str: | ||
case "InterleavedCPUTestServer": | ||
server_config = config_lib.InterleavedCPUTestServer | ||
case "CPUTestServer": | ||
server_config = config_lib.CPUTestServer | ||
case _: | ||
raise NotImplementedError | ||
return server_config |
This file contains hidden or 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,13 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains hidden or 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,132 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""JetStream Http API server.""" | ||
|
||
import json | ||
import logging | ||
from typing import Sequence | ||
from absl import app as abslapp | ||
from absl import flags | ||
from fastapi import APIRouter, Response | ||
import fastapi | ||
from fastapi.responses import StreamingResponse | ||
from prometheus_client import start_http_server | ||
import uvicorn | ||
from google.protobuf.json_format import Parse | ||
|
||
from jetstream.core import config_lib, orchestrator, server_lib | ||
from jetstream.core.metrics.prometheus import JetstreamMetricsCollector | ||
from jetstream.core.proto import jetstream_pb2 | ||
from jetstream.entrypoints.config import get_server_config | ||
from jetstream.entrypoints.http.protocol import DecodeRequest | ||
from jetstream.entrypoints.http.utils import proto_to_json_generator | ||
|
||
flags.DEFINE_string("host", "0.0.0.0", "server host address") | ||
flags.DEFINE_integer("port", 8080, "http server port") | ||
flags.DEFINE_string( | ||
"config", | ||
"InterleavedCPUTestServer", | ||
"available servers", | ||
) | ||
flags.DEFINE_integer( | ||
"prometheus_port", | ||
9988, | ||
"prometheus_port", | ||
) | ||
|
||
llm_orchestrator: orchestrator.LLMOrchestrator | ||
|
||
# Define Fast API endpoints (use llm_orchestrator to handle). | ||
router = APIRouter() | ||
|
||
|
||
@router.get("/") | ||
def root(): | ||
"""Root path for Jetstream HTTP Server.""" | ||
return Response( | ||
content=json.dumps({"message": "JetStream HTTP Server"}, indent=4), | ||
media_type="application/json", | ||
) | ||
|
||
|
||
@router.post("/v1/generate") | ||
async def generate(request: DecodeRequest): | ||
proto_request = Parse(request.json(), jetstream_pb2.DecodeRequest()) | ||
generator = llm_orchestrator.Decode(proto_request) | ||
return StreamingResponse( | ||
content=proto_to_json_generator(generator), media_type="text/event-stream" | ||
) | ||
|
||
|
||
@router.get("/v1/health") | ||
async def health() -> Response: | ||
"""Health check.""" | ||
response = await llm_orchestrator.HealthCheck( | ||
jetstream_pb2.HealthCheckRequest() | ||
) | ||
return Response( | ||
content=json.dumps({"is_live": str(response.is_live)}, indent=4), | ||
media_type="application/json", | ||
status_code=200, | ||
) | ||
|
||
|
||
def server(argv: Sequence[str]): | ||
# Init Fast API. | ||
app = fastapi.FastAPI() | ||
app.include_router(router) | ||
|
||
# Init LLMOrchestrator which would be the main handler in the api endpoints. | ||
devices = server_lib.get_devices() | ||
print(f"devices: {devices}") | ||
server_config = get_server_config(flags.FLAGS.config) | ||
JoeZijunZhou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(f"server_config: {server_config}") | ||
del argv | ||
|
||
metrics_server_config: config_lib.MetricsServerConfig | None = None | ||
# Setup Prometheus server | ||
metrics_collector: JetstreamMetricsCollector = None | ||
if flags.FLAGS.prometheus_port != 0: | ||
metrics_server_config = config_lib.MetricsServerConfig( | ||
port=flags.FLAGS.prometheus_port | ||
) | ||
logging.info( | ||
"Starting Prometheus server on port %d", metrics_server_config.port | ||
) | ||
start_http_server(metrics_server_config.port) | ||
metrics_collector = JetstreamMetricsCollector() | ||
else: | ||
logging.info( | ||
"Not starting Prometheus server: --prometheus_port flag not set" | ||
) | ||
|
||
global llm_orchestrator | ||
llm_orchestrator = orchestrator.LLMOrchestrator( | ||
driver=server_lib.create_driver( | ||
config=server_config, | ||
devices=devices, | ||
metrics_collector=metrics_collector, | ||
) | ||
) | ||
|
||
# Start uvicorn http server. | ||
uvicorn.run( | ||
app, host=flags.FLAGS.host, port=flags.FLAGS.port, log_level="info" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Run Abseil app w flags parser. | ||
abslapp.run(server) |
This file contains hidden or 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,36 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Http API server protocol.""" | ||
|
||
from pydantic import BaseModel # type: ignore | ||
|
||
|
||
class TextContent(BaseModel): | ||
text: str | ||
|
||
|
||
class TokenContent(BaseModel): | ||
token_ids: list[int] | ||
|
||
|
||
class DecodeRequest(BaseModel): | ||
max_tokens: int | ||
text_content: TextContent | None = None | ||
token_content: TokenContent | None = None | ||
|
||
# Config to enforce the oneof behavior at runtime. | ||
class Config: | ||
extra = "forbid" # Prevent extra fields. | ||
anystr_strip_whitespace = True |
This file contains hidden or 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,27 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Http API server utilities.""" | ||
|
||
from google.protobuf.json_format import MessageToJson | ||
|
||
|
||
async def proto_to_json_generator(proto_generator): | ||
"""Wraps a generator yielding Protocol Buffer messages into a generator | ||
|
||
yielding JSON messages. | ||
""" | ||
async for proto_message in proto_generator: | ||
json_string = MessageToJson(proto_message) | ||
yield json_string |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.