-
Notifications
You must be signed in to change notification settings - Fork 485
fix: Use per-model tokenizer in KvawareRouter for multi-model setups #1066
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -326,7 +326,7 @@ def __init__( | |
| self.instance_id_to_ip = {} | ||
| self.session_key = session_key | ||
| self.hash_ring = HashRing() | ||
| self.tokenizer = None | ||
| self.tokenizers: Dict[str, AutoTokenizer] = {} | ||
| self.threshold = kv_aware_threshold | ||
|
|
||
| def start_kv_manager(self): | ||
|
|
@@ -388,18 +388,28 @@ async def route_request( | |
| token_ids = None | ||
| # Local-first tokenization, fall back to remote "/tokenize" API on failure | ||
| # TODO (Yuhan): Handle chat completions | ||
| model_name = request_json.get("model", "") | ||
| # Find the endpoint serving this model | ||
| model_endpoint = next( | ||
| (ep for ep in endpoints if model_name in ep.model_names), | ||
| endpoints[0] if endpoints else None, | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If To preserve backward compatibility and ensure robustness, we should default model_name = request_json.get("model", "")
if not model_name and endpoints and endpoints[0].model_names:
model_name = endpoints[0].model_names[0]
# Find the endpoint serving this model
model_endpoint = next(
(ep for ep in endpoints if model_name in ep.model_names),
endpoints[0] if endpoints else None,
) |
||
| try: | ||
| if self.tokenizer is None: | ||
| self.tokenizer = AutoTokenizer.from_pretrained( | ||
| endpoints[0].model_names[0] | ||
| if model_name not in self.tokenizers: | ||
| self.tokenizers[model_name] = AutoTokenizer.from_pretrained( | ||
| model_name | ||
| ) | ||
| token_ids = self.tokenizer.encode(request_json.get("prompt", "")) | ||
| token_ids = self.tokenizers[model_name].encode( | ||
| request_json.get("prompt", "") | ||
| ) | ||
| except Exception: | ||
| # Remote /tokenize fallback (let errors bubble up to keep behavior simple) | ||
| remote_url = endpoints[0].url + "/tokenize" | ||
| if model_endpoint is None: | ||
| raise | ||
| remote_url = model_endpoint.url + "/tokenize" | ||
| headers = {"Content-Type": "application/json"} | ||
| data = { | ||
| "model": endpoints[0].model_names[0], | ||
| "model": model_name, | ||
| "prompt": request_json.get("prompt", ""), | ||
| } | ||
| body = requests.post( | ||
|
|
||
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.
Replacing
self.tokenizerwithself.tokenizersinKvawareRouter.__init__will breakLoadAwareRouter(which inherits fromKvawareRouter).LoadAwareRouter.tokenize_promptstill referencesself.tokenizer(e.g.,if self.tokenizer is None:), which will now raise anAttributeErrorbecauseself.tokenizeris no longer initialized inKvawareRouter.__init__.Please update
LoadAwareRouter.tokenize_promptto useself.tokenizersas well, or unify the tokenization logic between the two routers.