-
-
Notifications
You must be signed in to change notification settings - Fork 22.4k
[Core] enable out-of-tree model register #3871
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
14 commits
Select commit
Hold shift + click to select a range
182d5d7
enable oot model register
youkaichao d639c01
add doc for guiding oot model register
youkaichao 3273706
fix doc lint
youkaichao 85e7553
add test for oot models
youkaichao 8fb7170
fix linter
youkaichao c51050f
register_out_of_tree_model --> register_model
youkaichao c3a3b16
add warning for overwritten
youkaichao 9b6655e
expose ModelRegistry in top-level
youkaichao f6326a0
add guide for openai server
youkaichao e315617
add test for api server
youkaichao 6f3e010
finish openai api server test
youkaichao 9ebcc56
separate test
youkaichao fbbabb4
use dynamic port
youkaichao b6a114f
separate test commands
youkaichao 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
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,66 @@ | ||
| import multiprocessing | ||
| import sys | ||
| import time | ||
|
|
||
| import torch | ||
| from openai import OpenAI, OpenAIError | ||
|
|
||
| from vllm import ModelRegistry | ||
| from vllm.model_executor.models.opt import OPTForCausalLM | ||
| from vllm.model_executor.sampling_metadata import SamplingMetadata | ||
| from vllm.utils import get_open_port | ||
|
|
||
|
|
||
| class MyOPTForCausalLM(OPTForCausalLM): | ||
|
|
||
| def compute_logits(self, hidden_states: torch.Tensor, | ||
| sampling_metadata: SamplingMetadata) -> torch.Tensor: | ||
| # this dummy model always predicts the first token | ||
| logits = super().compute_logits(hidden_states, sampling_metadata) | ||
| logits.zero_() | ||
| logits[:, 0] += 1.0 | ||
| return logits | ||
|
|
||
|
|
||
| def server_function(port): | ||
| # register our dummy model | ||
| ModelRegistry.register_model("OPTForCausalLM", MyOPTForCausalLM) | ||
| sys.argv = ["placeholder.py"] + \ | ||
| ("--model facebook/opt-125m --dtype" | ||
| f" float32 --api-key token-abc123 --port {port}").split() | ||
| import runpy | ||
| runpy.run_module('vllm.entrypoints.openai.api_server', run_name='__main__') | ||
|
|
||
|
|
||
| def test_oot_registration_for_api_server(): | ||
| port = get_open_port() | ||
| server = multiprocessing.Process(target=server_function, args=(port, )) | ||
| server.start() | ||
| client = OpenAI( | ||
| base_url=f"http://localhost:{port}/v1", | ||
| api_key="token-abc123", | ||
| ) | ||
| while True: | ||
| try: | ||
| completion = client.chat.completions.create( | ||
| model="facebook/opt-125m", | ||
| messages=[{ | ||
| "role": "system", | ||
| "content": "You are a helpful assistant." | ||
| }, { | ||
| "role": "user", | ||
| "content": "Hello!" | ||
| }], | ||
| temperature=0, | ||
| ) | ||
| break | ||
| except OpenAIError as e: | ||
| if "Connection error" in str(e): | ||
| time.sleep(3) | ||
| else: | ||
| raise e | ||
| server.kill() | ||
| generated_text = completion.choices[0].message.content | ||
| # make sure only the first token is generated | ||
| rest = generated_text.replace("<s>", "") | ||
| assert rest == "" |
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 @@ | ||
| import torch | ||
|
|
||
| from vllm import LLM, ModelRegistry, SamplingParams | ||
| from vllm.model_executor.models.opt import OPTForCausalLM | ||
| from vllm.model_executor.sampling_metadata import SamplingMetadata | ||
|
|
||
|
|
||
| class MyOPTForCausalLM(OPTForCausalLM): | ||
|
|
||
| def compute_logits(self, hidden_states: torch.Tensor, | ||
| sampling_metadata: SamplingMetadata) -> torch.Tensor: | ||
| # this dummy model always predicts the first token | ||
| logits = super().compute_logits(hidden_states, sampling_metadata) | ||
| logits.zero_() | ||
| logits[:, 0] += 1.0 | ||
| return logits | ||
|
|
||
|
|
||
| def test_oot_registration(): | ||
| # register our dummy model | ||
| ModelRegistry.register_model("OPTForCausalLM", MyOPTForCausalLM) | ||
| prompts = ["Hello, my name is", "The text does not matter"] | ||
| sampling_params = SamplingParams(temperature=0) | ||
| llm = LLM(model="facebook/opt-125m") | ||
| first_token = llm.get_tokenizer().decode(0) | ||
| outputs = llm.generate(prompts, sampling_params) | ||
|
|
||
| for output in outputs: | ||
| generated_text = output.outputs[0].text | ||
| # make sure only the first token is generated | ||
| rest = generated_text.replace(first_token, "") | ||
| assert rest == "" | ||
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
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.