-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Generic adapter support in the grpc server #32
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e0f149
:alembic: draft lora changes
joerunde 8ae710f
:sparkles: add validation for LoRA requests
joerunde a4116d9
:loud_sound: add lora_id to request logs
joerunde b9c7a45
:recycle: lora_id -> adapter_id
joerunde 5c1b09a
:recycle: refactoring suggestions from Nick
joerunde 7ebed78
:recycle: move file loads into separate threadpool
joerunde e2be418
Merge branch 'main' into lora-main
joerunde c2bb957
:lock: deny path traversal
joerunde 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 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,118 @@ | ||
"""Contains code to map api requests for adapters (e.g. peft prefixes, LoRA) | ||
into valid LLM engine requests""" | ||
import asyncio | ||
import concurrent.futures | ||
import dataclasses | ||
import json | ||
import os | ||
import re | ||
from pathlib import Path | ||
from typing import Dict, Optional, Union | ||
|
||
from vllm.entrypoints.grpc.pb.generation_pb2 import (BatchedGenerationRequest, | ||
SingleGenerationRequest) | ||
from vllm.entrypoints.grpc.validation import TGISValidationError | ||
from vllm.lora.request import LoRARequest | ||
|
||
global_thread_pool = None # used for loading adapter files from disk | ||
|
||
VALID_ADAPTER_ID_PATTERN = re.compile("[/\\w\\-]+") | ||
|
||
|
||
@dataclasses.dataclass | ||
class AdapterMetadata: | ||
unique_id: int # Unique integer for vllm to identify the adapter | ||
adapter_type: str # The string name of the peft adapter type, e.g. LORA | ||
full_path: str | ||
|
||
|
||
@dataclasses.dataclass | ||
class AdapterStore: | ||
cache_path: str # Path to local store of adapters to load from | ||
adapters: Dict[str, AdapterMetadata] | ||
next_unique_id: int = 1 | ||
|
||
|
||
async def validate_adapters( | ||
request: Union[SingleGenerationRequest, BatchedGenerationRequest], | ||
adapter_store: Optional[AdapterStore]) -> Dict[str, LoRARequest]: | ||
"""Takes the adapter name from the request and constructs a valid | ||
engine request if one is set. Raises if the requested adapter | ||
does not exist or adapter type is unsupported | ||
|
||
Returns the kwarg dictionary to add to an engine.generate() call. | ||
""" | ||
global global_thread_pool | ||
adapter_id = request.adapter_id | ||
|
||
if adapter_id and not adapter_store: | ||
TGISValidationError.AdaptersDisabled.error() | ||
|
||
if not adapter_id or not adapter_store: | ||
return {} | ||
|
||
# If not already cached, we need to validate that files exist and | ||
# grab the type out of the adapter_config.json file | ||
if (adapter_metadata := adapter_store.adapters.get(adapter_id)) is None: | ||
_reject_bad_adapter_id(adapter_id) | ||
local_adapter_path = os.path.join(adapter_store.cache_path, adapter_id) | ||
|
||
loop = asyncio.get_running_loop() | ||
if global_thread_pool is None: | ||
global_thread_pool = concurrent.futures.ThreadPoolExecutor( | ||
max_workers=2) | ||
|
||
adapter_type = await loop.run_in_executor(global_thread_pool, | ||
_get_adapter_type_from_file, | ||
adapter_id, | ||
local_adapter_path) | ||
|
||
# Add to cache | ||
adapter_metadata = AdapterMetadata( | ||
unique_id=adapter_store.next_unique_id, | ||
adapter_type=adapter_type, | ||
full_path=local_adapter_path) | ||
adapter_store.adapters[adapter_id] = adapter_metadata | ||
|
||
# Build the proper vllm request object | ||
if adapter_metadata.adapter_type == "LORA": | ||
lora_request = LoRARequest(lora_name=adapter_id, | ||
lora_int_id=adapter_metadata.unique_id, | ||
lora_local_path=adapter_metadata.full_path) | ||
return {"lora_request": lora_request} | ||
|
||
# All other types unsupported | ||
TGISValidationError.AdapterUnsupported.error(adapter_metadata.adapter_type) | ||
|
||
|
||
def _get_adapter_type_from_file(adapter_id: str, adapter_path: str) -> str: | ||
"""This function does all the filesystem access required to deduce the type | ||
of the adapter. It's run in a separate thread pool executor so that file | ||
access does not block the main event loop.""" | ||
if not os.path.exists(adapter_path): | ||
TGISValidationError.AdapterNotFound.error(adapter_id, | ||
"directory does not exist") | ||
|
||
adapter_config_path = os.path.join(adapter_path, "adapter_config.json") | ||
if not os.path.exists(adapter_config_path): | ||
TGISValidationError.AdapterNotFound.error( | ||
adapter_id, "invalid adapter: no adapter_config.json found") | ||
|
||
# NB: blocks event loop | ||
with open(adapter_config_path) as adapter_config_file: | ||
adapter_config = json.load(adapter_config_file) | ||
|
||
return adapter_config.get("peft_type", None) | ||
|
||
|
||
def _reject_bad_adapter_id(adapter_id: str) -> None: | ||
"""Raise if the adapter id attempts path traversal or has invalid file path | ||
characters""" | ||
if not VALID_ADAPTER_ID_PATTERN.fullmatch(adapter_id): | ||
TGISValidationError.InvalidAdapterID.error(adapter_id) | ||
|
||
# Check for path traversal | ||
root_path = Path("/some/file/root") | ||
derived_path = root_path / adapter_id | ||
if not os.path.normpath(derived_path).startswith(str(root_path) + "/"): | ||
TGISValidationError.InvalidAdapterID.error(adapter_id) |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should sanitize the
adapter_id
here to make sure that the user can't send funny things like../../../etc/passwd
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!